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