1 """Telnet functions and constants for the mudpy engine."""
3 # Copyright (c) 2004-2019 mudpy authors. Permission to use, copy,
4 # modify, and distribute this software is granted under terms
5 # provided in the LICENSE file distributed with this software.
9 # telnet options (from bsd's arpa/telnet.h since telnetlib's are ambiguous)
10 TELOPT_BINARY = 0 # transmit 8-bit data by the receiver (rfc 856)
11 TELOPT_ECHO = 1 # echo received data back to the sender (rfc 857)
12 TELOPT_SGA = 3 # suppress transmission of the go ahead character (rfc 858)
13 TELOPT_TTYPE = 24 # exchange terminal type information (rfc 1091)
14 TELOPT_EOR = 25 # transmit end-of-record after transmitting data (rfc 885)
15 TELOPT_NAWS = 31 # negotiate about window size (rfc 1073)
16 TELOPT_LINEMODE = 34 # cooked line-by-line input mode (rfc 1184)
18 TELOPT_BINARY: "8-bit binary",
20 TELOPT_SGA: "suppress go-ahead",
21 TELOPT_TTYPE: "terminal type",
22 TELOPT_EOR: "end of record",
23 TELOPT_NAWS: "negotiate about window size",
24 TELOPT_LINEMODE: "line mode",
27 # TODO(fungi): implement support for RFC 1091 terminal type information
38 EOR = 239 # end-of-record signal (rfc 885)
39 SE = 240 # end of subnegotiation parameters (rfc 854)
40 GA = 249 # go ahead signal (rfc 854)
41 SB = 250 # what follows is subnegotiation of the indicated option (rfc 854)
42 WILL = 251 # desire or confirmation of performing an option (rfc 854)
43 WONT = 252 # refusal or confirmation of performing an option (rfc 854)
44 DO = 253 # request or confirm performing an option (rfc 854)
45 DONT = 254 # demand or confirm no longer performing an option (rfc 854)
46 IAC = 255 # interpret as command escape character (rfc 854)
49 SE: "subnegotiation end",
51 SB: "subnegotiation begin",
56 IAC: "interpret as command",
59 # RFC 1143 option negotiation states
60 NO = 0 # option is disabled
61 YES = 1 # option is enabled
62 WANTNO = 2 # demanded disabling option
63 WANTYES = 3 # requested enabling option
64 WANTNO_OPPOSITE = 4 # demanded disabling option but queued an enable after it
65 WANTYES_OPPOSITE = 5 # requested enabling option but queued a disable after it
69 WANTNO: "demand disabling",
70 WANTYES: "request enabling",
71 WANTNO_OPPOSITE: "want no queued opposite",
72 WANTYES_OPPOSITE: "want yes queued opposite",
75 # RFC 1143 option negotiation parties
84 def log(message, user):
85 """Log debugging info for Telnet client/server interactions."""
87 client = user.account.get("name", user)
90 mudpy.misc.log('[telnet] %s %s.' % (message, client), 0)
93 def telnet_proto(*arguments):
94 """Return a concatenated series of Telnet protocol commands."""
95 return bytes((arguments))
98 def translate_action(*command):
99 """Convert a Telnet command sequence into text suitable for logging."""
101 command_name = command_names[command[0]]
103 # This should never happen since we filter unknown commands from
104 # the input queue, but added here for completeness since logging
105 # should never crash the process
106 command_name = str(command[0])
108 option_name = option_names[command[1]]
110 # This can happen for any of the myriad of Telnet options missing
111 # from the option_names dict
112 option_name = str(command[1])
113 return "%s %s" % (command_name, option_name)
116 def send_command(user, *command):
117 """Sends a Telnet command string to the specified user's socket."""
118 user.send(telnet_proto(IAC, *command), raw=True)
119 log('Sent "%s" to' % translate_action(*command), user)
122 def is_enabled(user, telopt, party, state=YES):
123 """Indicates whether a specified Telnet option is enabled."""
124 if (telopt, party) in user.telopts and user.telopts[
132 def enable(user, telopt, party):
133 """Negotiates enabling a Telnet option for the indicated user's socket."""
138 if not (telopt, party) in user.telopts or user.telopts[
141 user.telopts[(telopt, party)] = WANTYES
142 send_command(user, txpos, telopt)
143 elif user.telopts[(telopt, party)] is WANTNO:
144 user.telopts[(telopt, party)] = WANTNO_OPPOSITE
145 elif user.telopts[(telopt, party)] is WANTYES_OPPOSITE:
146 user.telopts[(telopt, party)] = WANTYES
149 def disable(user, telopt, party):
150 """Negotiates disabling a Telnet option for the user's socket."""
155 if not (telopt, party) in user.telopts:
156 user.telopts[(telopt, party)] = NO
157 elif user.telopts[(telopt, party)] is YES:
158 user.telopts[(telopt, party)] = WANTNO
159 send_command(user, txneg, telopt)
160 elif user.telopts[(telopt, party)] is WANTYES:
161 user.telopts[(telopt, party)] = WANTYES_OPPOSITE
162 elif user.telopts[(telopt, party)] is WANTNO_OPPOSITE:
163 user.telopts[(telopt, party)] = WANTNO
166 def negotiate_telnet_options(user):
167 """Reply to and remove telnet negotiation options from partial_input."""
169 # make a local copy to play with
170 text = user.partial_input
172 # start at the begining of the input
175 # as long as we haven't checked it all
177 while position < len_text:
179 # jump to the first IAC you find
180 position = text.find(telnet_proto(IAC), position)
182 # if there wasn't an IAC in the input or it's at the end, we're done
183 if position < 0 or position >= len_text - 1:
186 # the byte following the IAC is our command
187 command = text[position+1]
189 # replace a double (literal) IAC if there's a CR+NUL or CR+LF later
192 text.find(b"\r\0", position) > 0 or
193 text.find(b"\r\n", position) > 0):
195 text = text[:position] + text[position + 1:]
196 log('Escaped IAC from', user)
200 # implement an RFC 1143 option negotiation queue here
201 elif len_text > position + 2 and WILL <= command <= DONT:
202 telopt = text[position+2]
203 log('Received "%s" from' % translate_action(command, telopt), user)
204 if telopt in supported:
215 if (telopt, party) not in user.telopts:
216 user.telopts[(telopt, party)] = NO
218 if user.telopts[(telopt, party)] is NO:
219 user.telopts[(telopt, party)] = YES
220 send_command(user, txpos, telopt)
221 elif user.telopts[(telopt, party)] is WANTNO:
222 user.telopts[(telopt, party)] = NO
223 elif user.telopts[(telopt, party)] is WANTNO_OPPOSITE:
224 user.telopts[(telopt, party)] = YES
225 elif user.telopts[(telopt, party)] is WANTYES_OPPOSITE:
226 user.telopts[(telopt, party)] = WANTNO
227 send_command(user, txneg, telopt)
229 user.telopts[(telopt, party)] = YES
231 if user.telopts[(telopt, party)] is YES:
232 user.telopts[(telopt, party)] = NO
233 send_command(user, txneg, telopt)
234 elif user.telopts[(telopt, party)] is WANTNO_OPPOSITE:
235 user.telopts[(telopt, party)] = WANTYES
236 send_command(user, txpos, telopt)
238 user.telopts[(telopt, party)] = NO
239 elif command is WILL:
240 send_command(user, DONT, telopt)
242 send_command(user, WONT, telopt)
243 text = text[:position] + text[position + 3:]
245 # subnegotiation options
246 elif len_text > position + 4 and command is SB:
247 telopt = text[position + 2]
248 if telopt is TELOPT_NAWS:
250 text[position + 3] * 256 + text[position + 4])
251 end_subnegotiation = text.find(telnet_proto(IAC, SE), position)
252 if end_subnegotiation > 0:
253 text = text[:position] + text[end_subnegotiation + 2:]
257 # otherwise, strip out a two-byte IAC command
258 elif len_text > position + 2:
259 log("Ignored unknown command %s from" % command, user)
260 text = text[:position] + text[position + 2:]
262 # and this means we got the begining of an IAC
266 # replace the input with our cleaned-up text
267 user.partial_input = text