1 # -*- coding: utf-8 -*-
2 """Telnet functions and constants for the mudpy engine."""
4 # Copyright (c) 2004-2013 Jeremy Stanley <fungi@yuggoth.org>. Permission
5 # to use, copy, modify, and distribute this software is granted under
6 # terms provided in the LICENSE file distributed with this software.
8 # telnet options (from bsd's arpa/telnet.h since telnetlib's are ambiguous)
9 TELOPT_BINARY = 0 # transmit 8-bit data by the receiver (rfc 856)
10 TELOPT_ECHO = 1 # echo received data back to the sender (rfc 857)
11 TELOPT_SGA = 3 # suppress transmission of the go ahead character (rfc 858)
12 # TELOPT_TTYPE = 24 # exchange terminal type information (rfc 1091)
13 TELOPT_EOR = 25 # transmit end-of-record after transmitting data (rfc 885)
14 TELOPT_NAWS = 31 # negotiate about window size (rfc 1073)
15 TELOPT_LINEMODE = 34 # cooked line-by-line input mode (rfc 1184)
27 EOR = 239 # end-of-record signal (rfc 885)
28 SE = 240 # end of subnegotiation parameters (rfc 854)
29 GA = 249 # go ahead signal (rfc 854)
30 SB = 250 # what follows is subnegotiation of the indicated option (rfc 854)
31 WILL = 251 # desire or confirmation of performing an option (rfc 854)
32 WONT = 252 # refusal or confirmation of performing an option (rfc 854)
33 DO = 253 # request or confirm performing an option (rfc 854)
34 DONT = 254 # demand or confirm no longer performing an option (rfc 854)
35 IAC = 255 # interpret as command escape character (rfc 854)
37 # RFC 1143 option negotiation states
38 NO = 0 # option is disabled
39 YES = 1 # option is enabled
40 WANTNO = 2 # demanded disabling option
41 WANTYES = 3 # requested enabling option
42 WANTNO_OPPOSITE = 4 # demanded disabling option but queued an enable after it
43 WANTYES_OPPOSITE = 5 # requested enabling option but queued a disable after it
45 # RFC 1143 option negotiation parties
50 def telnet_proto(*arguments):
51 """Return a concatenated series of Telnet protocol commands."""
52 return bytes((arguments))
55 def send_command(user, *command):
56 """Sends a Telnet command string to the specified user's socket."""
57 user.send(telnet_proto(IAC, *command), raw=True)
60 def is_enabled(user, telopt, party, state=YES):
61 """Returns True if the indicated Telnet option is enabled, False if
63 if (telopt, party) in user.telopts and user.telopts[
71 def enable(user, telopt, party):
72 """Negotiates enabling a Telnet option for the indicated user's socket."""
77 if not (telopt, party) in user.telopts or user.telopts[
80 user.telopts[(telopt, party)] = WANTYES
81 send_command(user, txpos, telopt)
82 elif user.telopts[(telopt, party)] is WANTNO:
83 user.telopts[(telopt, party)] = WANTNO_OPPOSITE
84 elif user.telopts[(telopt, party)] is WANTYES_OPPOSITE:
85 user.telopts[(telopt, party)] = WANTYES
88 def disable(user, telopt, party):
89 """Negotiates disabling a Telnet option for the indicated user's
95 if not (telopt, party) in user.telopts:
96 user.telopts[(telopt, party)] = NO
97 elif user.telopts[(telopt, party)] is YES:
98 user.telopts[(telopt, party)] = WANTNO
99 send_command(user, txneg, telopt)
100 elif user.telopts[(telopt, party)] is WANTYES:
101 user.telopts[(telopt, party)] = WANTYES_OPPOSITE
102 elif user.telopts[(telopt, party)] is WANTNO_OPPOSITE:
103 user.telopts[(telopt, party)] = WANTNO
106 def negotiate_telnet_options(user):
107 """Reply to and remove telnet negotiation options from partial_input."""
110 # make a local copy to play with
111 text = user.partial_input
113 # start at the begining of the input
116 # as long as we haven't checked it all
118 while position < len_text:
120 # jump to the first IAC you find
121 position = text.find(telnet_proto(IAC), position)
123 # if there wasn't an IAC in the input or it's at the end, we're done
124 if position < 0 or position >= len_text - 1:
127 # the byte following the IAC is our command
128 command = text[position+1]
130 # replace a double (literal) IAC if there's an LF later
132 if text.find("\n", position) > 0:
134 text = text[:position] + text[position + 1:]
138 # implement an RFC 1143 option negotiation queue here
139 elif len_text > position + 2 and WILL <= command <= DONT:
140 telopt = text[position+2]
141 if telopt in supported:
152 if (telopt, party) not in user.telopts:
153 user.telopts[(telopt, party)] = NO
155 if user.telopts[(telopt, party)] is NO:
156 user.telopts[(telopt, party)] = YES
157 send_command(user, txpos, telopt)
158 elif user.telopts[(telopt, party)] is WANTNO:
159 user.telopts[(telopt, party)] = NO
160 elif user.telopts[(telopt, party)] is WANTNO_OPPOSITE:
161 user.telopts[(telopt, party)] = YES
162 elif user.telopts[(telopt, party)] is WANTYES_OPPOSITE:
163 user.telopts[(telopt, party)] = WANTNO
164 send_command(user, txneg, telopt)
166 user.telopts[(telopt, party)] = YES
168 if user.telopts[(telopt, party)] is YES:
169 user.telopts[(telopt, party)] = NO
170 send_command(user, txneg, telopt)
171 elif user.telopts[(telopt, party)] is WANTNO_OPPOSITE:
172 user.telopts[(telopt, party)] = WANTYES
173 send_command(user, txpos, telopt)
175 user.telopts[(telopt, party)] = NO
176 elif command is WILL:
177 send_command(user, DONT, telopt)
179 send_command(user, WONT, telopt)
180 text = text[:position] + text[position + 3:]
182 # subnegotiation options
183 elif len_text > position + 4 and command is SB:
184 telopt = ord(text[position + 2])
185 if telopt is TELOPT_NAWS:
186 user.columns = ord(text[position + 3]) * \
187 256 + ord(text[position + 4])
188 end_subnegotiation = text.find(telnet_proto(IAC, SE), position)
189 if end_subnegotiation > 0:
190 text = text[:position] + text[end_subnegotiation + 2:]
194 # otherwise, strip out a two-byte IAC command
195 elif len_text > position + 2:
196 mudpy.misc.log("Unknown Telnet IAC command %s ignored." % command)
197 text = text[:position] + text[position + 2:]
199 # and this means we got the begining of an IAC
203 # replace the input with our cleaned-up text
204 user.partial_input = text