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