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