]> git.pond.sub.org Git - empserver/blob - doc/clients-howto
Minor clarifications. Document play's use of C_EXIT for errors.
[empserver] / doc / clients-howto
1 This file contains material useful for client writers.
2
3 Protocol
4 ========
5
6 Empire's client server protocol is plain text.  It is simple enough
7 that you could play using nothing more than telnet.  That's a feature.
8
9 A session uses either ASCII or UTF-8, controlled by session option
10 utf-8.  See below for session options.  The session always starts in
11 ASCII.
12
13 Client-server communication is line-oriented.
14
15 The server sends lines of output.  Each output line starts with a
16 character identifying the line, followed by a space, then arbitrary
17 text, then a line feed.
18
19 Identification characters encode small integers called output ids.
20 Ids less than 10 are encoded as decimal digits, and larger ids as
21 lower case letters, starting with 'a'.  Symbolic names for ids are
22 defined in proto.h.
23
24 Clients shall be able to safely handle output lines of arbitrary
25 length.  Naturally, a client may not be able to handle a sufficiently
26 large line without loss of functionality, but safety must be ensured.
27
28 When using ASCII, the most significant bit in text characters
29 indicates highlighting.
30
31 The following control characters have a defined meaning:
32
33   ASCII name         decimal  meaning
34   ----------------------------------------------------
35   horizontal tab         9    move to next tab stop(1)
36   line feed             10    end of line
37   shift out             14    begin highlighting(2)
38   shift in              15    end highlighting(2)
39
40   (1) Tab stops are every eight columns.
41   (2) Only if session uses UTF-8
42
43 Other ASCII control characters should not occur and may be ignored by
44 clients.  Likewise, overlong or malformed UTF-8 sequences should not
45 occur and may be ignored.
46
47 The server prompts for input.  Each prompt `consumes' one line of
48 input.  Input lines are arbitrary text, terminated by line feed, which
49 is optionally preceded by carriage return (decimal 13).  Lines should
50 not contain ASCII control characters other than horizontal tab.
51 Clients should not send overlong or malformed UTF-8 sequences.
52
53 A client is called synchronous if it waits for a prompt before it
54 sends another line of input.  Else it is called asynchronous.
55
56 Asynchronous clients must take care to avoid blocking on sending
57 input.  If the client process blocks that way, it can't receive server
58 output until the server reads more input.  That may never happen,
59 because the server may well block on output, which then deadlocks the
60 session.
61
62 An Empire session consists of two phases: login and playing.
63 emp_client is synchronous during the former and asynchronous during
64 the latter.  It can currently deadlock as described above.
65
66 Login phase
67 -----------
68
69 In the login phase, the server prompts for login commands.
70
71 The server starts with a C_INIT prompt.  The login phase ends when the
72 server sends another C_INIT prompt, which starts the playing phase, or
73 when it closes the connection.
74
75 The server replies to a login command with another prompt.  Except as
76 noted below, the server replies C_BADCMD for syntax errors, C_CMDERR
77 for other errors, and C_CMDOK on success.  In any case, the prompt
78 text contains further information intended for humans.
79
80 Login commands are:
81
82 * client CLIENT-ID...
83
84   Identify the client.  This is optional.  If given, version
85   information should be included.
86
87 * coun COUNTRY
88
89   Set country name to COUNTRY.
90
91 * kill
92
93   If another connection is open for this country, forcibly close it,
94   else do nothing.  Country must be authenticated.
95
96   Reply is C_EXIT regardless of success.
97
98 * options OPTION[=VALUE]...
99
100   Negotiate session options.  Each argument requests setting OPTION to
101   VALUE.  The string VALUE is interpreted in an option-specific way.
102   The form without the `=' sets it to an option-specific implied
103   value.
104
105   The server accepts the request by sending C_CMDOK.  If the server
106   encounters an unknown option, it rejects the request by sending
107   C_BADCMD.  It rejects unsupported values by sending C_CMDERR.  It
108   may or may not process valid parts of rejected requests.
109
110   If there are no arguments, the server lists its options.  For each
111   option, it sends a C_DATA line with OPTION=VALUE as text.
112
113   See below for supported session options.
114
115 * pass PASSWORD
116
117   Authenticate.  Country name must be set already.
118
119 * play [USER [COUNTRY [PASSWORD]]]
120
121   Start playing.
122
123   Optional argument USER sets the user name.
124
125   Optional argument COUNTRY sets the country name.
126
127   Optional argument PASSWORD authenticates.  If it isn't present, the
128   country must be authenticated already.
129
130   Some error conditions result in a C_EXIT reply.  Clients should
131   treat it just like C_CMDERR.
132
133   On success, the server sends C_INIT.  The text is the protocol
134   version number in decimal, currently 2.  The client should terminate
135   on protocol versions it doesn't know how to handle.
136
137   The protocol version is not expected to change often.  In fact, it
138   hasn't changed since the oldest known versions of Empire.
139
140   Unlike the first C_INIT, the second one is not a prompt, i.e the
141   server does not consume a line of input for it.
142
143   The session then proceeds to the playing phase.
144
145 * quit
146
147   Terminate the session.  The server replies with C_EXIT and closes
148   the connection.
149
150 * sanc
151
152   List all countries that are still in sanctuary.  The output consists
153   of a number of C_DATA lines with human-readable text.
154
155   This command is only recognized if option BLITZ is enabled.
156
157 * user NAME
158
159   Set the user name.  This is optional and defaults to "".
160
161 Playing phase
162 -------------
163
164 In the playing phase, the server sends data, control and prompt lines.
165
166 Clients signal `EOF' by sending a line "ctld\n", and `interrupt' by
167 sending "aborted\n".  The server treats these conditions specially, as
168 described below.  Anything else is either a command or input for a
169 command, depending on how the server prompts for the line.
170
171 The following ids occur:
172
173 * Command prompt C_PROMPT
174
175   The server consumes a line of input.  On EOF, the server terminates
176   the session.  Interrupt is ignored.  Anything else is interpreted as
177   Empire command.
178
179   Text is minutes used, space, BTUs left.  Both numbers are in
180   decimal.  Clients shall ignore additional text separated by another
181   space.
182
183   emp_client prints this prompt using format "[%d:%d] Command : ".
184   Clients with a tty-like user interface are advised to use a similar
185   format, to minimize differences to the examples in info.
186
187 * Sub-prompt C_FLUSH
188
189   The server consumes a line of input and passes it to the currently
190   executing command.  Commands usually fail on EOF or interrupt.
191
192   Text is a human-readable prompt supplied by the command.
193
194   emp_client prints the text verbatim.
195
196 * Data C_DATA
197
198   Text is human-readable server output.
199
200   emp_client prints the text verbatim.
201
202 * Control C_EXECUTE
203
204   Request execution of a script.  The text is a copy of the execute
205   command's first argument.  Its syntax and semantics is left to the
206   client.
207
208   emp_client interprets the text as file name, and sends the contents
209   of that file.
210
211   The security considerations on C_PIPE (below) apply to C_EXECUTE as
212   well.
213
214   The client shall mark the end of the script by signalling EOF as
215   described above.  It may signal interrupt if it is unable or
216   unwilling to send the complete script.
217
218   While executing the script, the server sends no C_PROMPT command
219   prompts.  It still sends C_FLUSH sub-prompts.
220
221   Certain bad failures make the server ignore the rest of the script
222   file.  This feature is too hard to predict to be really useful.
223
224   Strictly asynchronous clients cannot support C_EXECUTE correctly.
225   By the time C_EXECUTE arrives, the client may have sent more input
226   already.  That input `overtakes' the script in the input stream.
227
228   emp_client has this problem.
229
230   Clients are not required to support C_EXECUTE.  Clients are
231   encouraged to offer alternative means for scripting.
232
233 * Control C_EXIT
234
235   End of session.  The server will close the connection.  Text is a
236   human-readable farewell.
237
238   emp_client prints this text prepended with "Exit: ".
239
240 * Control C_FLASH
241
242   Asynchronous message.  The client should display the text
243   immediately.
244
245   emp_client prints the text verbatim, prepended by a newline.  This
246   is clearly sub-optimal, because it can be inserted in the middle of
247   user input.  Clients wishing to to display asynchronous messages
248   together with normal I/O should insert them before the current
249   prompt.
250
251   Although asynchronous messages can be switched off with the toggle
252   command, client support for C_FLASH is not really optional, because
253   a C_FLASH could arrive before the client manages to switch them off.
254   And if the client lets the user send arbitrary commands, the user
255   can switch it back on at any time.  A session option controlling
256   C_FLASH would make more sense.  Since all popular clients support
257   C_FLASH, it's not worth the trouble.
258
259 * Control C_INFORM
260
261   Notification that the number of unread telegrams changed.  The text
262   is the notification in human-readable form.
263
264   emp_client prints the last received C_INFORM text right before each
265   prompt.  It also repeats the last prompt when a C_INFORM arrives.
266   This is sub-optimal just like its treatment of C_FLASH.
267
268   The user can switch these off with the toggle command.  Client
269   support is not really optional for the same reasons as for C_FLASH.
270
271 * Control C_PIPE
272
273   When a command is redirected to a pipeline, its output is prededed
274   by a C_PIPE line.  The text is a copy of the redirection, starting
275   with '|'.  Syntax and semantics of the text after the '|' are left
276   to the client.
277
278   emp_client executes text after '|' as shell command, with standard
279   input connected to the Empire command's output.
280
281   For obvious security reasons, clients supporting pipes shall ensure
282   that the text is identical to whatever was sent to the server.  Note
283   that the server recognizes redirection only in command lines, not
284   argument lines.  Asynchronous clients cannot distinguish the two.
285
286   emp_client prepares for redirections being recognized in any line,
287   and copes with only some of them being actually recognized.
288
289 * Control C_REDIR
290
291   When a command is redirected to a file, its output is prededed by a
292   C_REDIR line.  The text is a copy of the redirection, starting with
293   '>', optionally followed by '>' or '!'.  Syntax and semantics of the
294   remaining text are left to the client.
295
296   emp_client interprets the first word (sequence of non-space
297   characters) in the remaining text as file name, and redirects the
298   command's output to that file.  The use of the first word is
299   unfortunate, as it arbitrarily limits the user's choice of file
300   names.  If the text starts with '>!', it silently overwrites any
301   existing file, with '>>' it appends to the file, and with just '>'
302   it refuses to overwrite an existing file.
303
304   The security considerations on C_PIPE apply to C_REDIR as well.
305
306 * Other ids
307
308   Other ids do not occur currently.  Clients shall deal gracefully
309   with ids they don't know.
310
311   emp_client treats unknown ids like C_DATA.  For historical reasons,
312   it prepends "Aborted\n" to C_ABORT lines, and "Error; " to C_CMDERR
313   and C_BADCMD lines.
314
315
316 Session Options
317 ===============
318
319 Session options control client preferences.  They are not persistent;
320 each session starts with the same default session options.
321
322 The only session option so far is utf-8.  It controls whether to use
323 ASCII (disabled) or UTF-8 (enabled).  Initial value is disabled.
324 Setting it to 0 disables, setting it to 1 enables, and the implied
325 value is enabled.
326
327 Session options should not be confused with command toggle, which
328 controls player preferences.  Clients should leave those accessible to
329 their users.
330
331
332 Commands Useful for Clients
333 ===========================
334
335 Traditional dumps
336 -----------------
337
338 A number of commands are available for clients that wish to maintain
339 their own game state.  These commands are called dumps.
340
341       dump  - Dump sector information
342       ldump - Dump land unit information
343       lost  - Report lost items
344       ndump - Dump nuclear stockpile information
345       pdump - Dump plane information
346       sdump - Dump ship information
347
348 See the various info pages on these for complete documentation on how
349 they work and how you can use them to help improve your clients.
350
351 Each of the above commands prints a timestamp, which is a decimal
352 number.  This together with the timestamp selector enables you to dump
353 incrementally, i.e.  retrieve only what changed since another dump.
354 For instance, if `dump *' printed timestamp 855544597, `dump *
355 ?timestamp>855544596' dumps everything changed since.
356
357 Note that the condition compares with the timestamp value minus one.
358 That's because timestamps have a noticeable granularity: things may
359 change between a dump and the next timestamp increase.
360
361 Timestamp values are currently seconds since the epoch, but this might
362 change, and clients are advised not to rely on it.
363
364 Experimental extended dump
365 --------------------------
366
367 Traditional dumps have a number of shortcomings.  They cover only the
368 most important part of the game state: sectors, ships, planes, land
369 units, nukes, but not game configuration, loans, news, and so forth.
370 They are not quite complete even for what they attempt to cover.
371 Finally, their output is harder to parse than necessary.
372
373 The new `xdump' command is designed to overcome these deficiencies.
374 It still needs significant work, and is subject to change in
375 incompatible ways.  To protect real games from known and unknown flaws
376 in xdump, it is only accessible when option GUINEA_PIGS is enabled.
377
378 Technical xdump documentation to be written.
379
380
381 Advice on parsing human-readable command output
382 ===============================================
383
384 To be written.