Clean up docstrings
[mudpy.git] / lib / mudpy / telnet.py
1 # -*- coding: utf-8 -*-
2 """Telnet functions and constants for the mudpy engine."""
3
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.
7
8 import mudpy
9
10 # telnet options (from bsd's arpa/telnet.h since telnetlib's are ambiguous)
11 TELOPT_BINARY = 0  # transmit 8-bit data by the receiver (rfc 856)
12 TELOPT_ECHO = 1  # echo received data back to the sender (rfc 857)
13 TELOPT_SGA = 3  # suppress transmission of the go ahead character (rfc 858)
14 # TELOPT_TTYPE = 24 # exchange terminal type information (rfc 1091)
15 TELOPT_EOR = 25  # transmit end-of-record after transmitting data (rfc 885)
16 TELOPT_NAWS = 31  # negotiate about window size (rfc 1073)
17 TELOPT_LINEMODE = 34  # cooked line-by-line input mode (rfc 1184)
18
19 supported = (
20     TELOPT_BINARY,
21     TELOPT_ECHO,
22     TELOPT_SGA,
23     TELOPT_EOR,
24     TELOPT_NAWS,
25     TELOPT_LINEMODE
26 )
27
28 # telnet commands
29 EOR = 239  # end-of-record signal (rfc 885)
30 SE = 240  # end of subnegotiation parameters (rfc 854)
31 GA = 249  # go ahead signal (rfc 854)
32 SB = 250  # what follows is subnegotiation of the indicated option (rfc 854)
33 WILL = 251  # desire or confirmation of performing an option (rfc 854)
34 WONT = 252  # refusal or confirmation of performing an option (rfc 854)
35 DO = 253  # request or confirm performing an option (rfc 854)
36 DONT = 254  # demand or confirm no longer performing an option (rfc 854)
37 IAC = 255  # interpret as command escape character (rfc 854)
38
39 # RFC 1143 option negotiation states
40 NO = 0  # option is disabled
41 YES = 1  # option is enabled
42 WANTNO = 2  # demanded disabling option
43 WANTYES = 3  # requested enabling option
44 WANTNO_OPPOSITE = 4  # demanded disabling option but queued an enable after it
45 WANTYES_OPPOSITE = 5  # requested enabling option but queued a disable after it
46
47 # RFC 1143 option negotiation parties
48 HIM = 0
49 US = 1
50
51
52 def telnet_proto(*arguments):
53     """Return a concatenated series of Telnet protocol commands."""
54     return bytes((arguments))
55
56
57 def send_command(user, *command):
58     """Sends a Telnet command string to the specified user's socket."""
59     user.send(telnet_proto(IAC, *command), raw=True)
60
61
62 def is_enabled(user, telopt, party, state=YES):
63     """Indicates whether a specified Telnet option is enabled."""
64     if (telopt, party) in user.telopts and user.telopts[
65        (telopt, party)
66        ] is state:
67         return True
68     else:
69         return False
70
71
72 def enable(user, telopt, party):
73     """Negotiates enabling a Telnet option for the indicated user's socket."""
74     if party is HIM:
75         txpos = DO
76     else:
77         txpos = WILL
78     if not (telopt, party) in user.telopts or user.telopts[
79        (telopt, party)
80        ] is NO:
81         user.telopts[(telopt, party)] = WANTYES
82         send_command(user, txpos, telopt)
83     elif user.telopts[(telopt, party)] is WANTNO:
84         user.telopts[(telopt, party)] = WANTNO_OPPOSITE
85     elif user.telopts[(telopt, party)] is WANTYES_OPPOSITE:
86         user.telopts[(telopt, party)] = WANTYES
87
88
89 def disable(user, telopt, party):
90     """Negotiates disabling a Telnet option for the user's socket."""
91     if party is HIM:
92         txneg = DONT
93     else:
94         txneg = WONT
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
104
105
106 def negotiate_telnet_options(user):
107     """Reply to and remove telnet negotiation options from partial_input."""
108
109     # make a local copy to play with
110     text = user.partial_input
111
112     # start at the begining of the input
113     position = 0
114
115     # as long as we haven't checked it all
116     len_text = len(text)
117     while position < len_text:
118
119         # jump to the first IAC you find
120         position = text.find(telnet_proto(IAC), position)
121
122         # if there wasn't an IAC in the input or it's at the end, we're done
123         if position < 0 or position >= len_text - 1:
124             break
125
126         # the byte following the IAC is our command
127         command = text[position+1]
128
129         # replace a double (literal) IAC if there's an LF later
130         if command is IAC:
131             if text.find("\n", position) > 0:
132                 position += 1
133                 text = text[:position] + text[position + 1:]
134             else:
135                 position += 2
136
137         # implement an RFC 1143 option negotiation queue here
138         elif len_text > position + 2 and WILL <= command <= DONT:
139             telopt = text[position+2]
140             if telopt in supported:
141                 if command <= WONT:
142                     party = HIM
143                     rxpos = WILL
144                     txpos = DO
145                     txneg = DONT
146                 else:
147                     party = US
148                     rxpos = DO
149                     txpos = WILL
150                     txneg = WONT
151                 if (telopt, party) not in user.telopts:
152                     user.telopts[(telopt, party)] = NO
153                 if command is rxpos:
154                     if user.telopts[(telopt, party)] is NO:
155                         user.telopts[(telopt, party)] = YES
156                         send_command(user, txpos, telopt)
157                     elif user.telopts[(telopt, party)] is WANTNO:
158                         user.telopts[(telopt, party)] = NO
159                     elif user.telopts[(telopt, party)] is WANTNO_OPPOSITE:
160                         user.telopts[(telopt, party)] = YES
161                     elif user.telopts[(telopt, party)] is WANTYES_OPPOSITE:
162                         user.telopts[(telopt, party)] = WANTNO
163                         send_command(user, txneg, telopt)
164                     else:
165                         user.telopts[(telopt, party)] = YES
166                 else:
167                     if user.telopts[(telopt, party)] is YES:
168                         user.telopts[(telopt, party)] = NO
169                         send_command(user, txneg, telopt)
170                     elif user.telopts[(telopt, party)] is WANTNO_OPPOSITE:
171                         user.telopts[(telopt, party)] = WANTYES
172                         send_command(user, txpos, telopt)
173                     else:
174                         user.telopts[(telopt, party)] = NO
175             elif command is WILL:
176                 send_command(user, DONT, telopt)
177             else:
178                 send_command(user, WONT, telopt)
179             text = text[:position] + text[position + 3:]
180
181         # subnegotiation options
182         elif len_text > position + 4 and command is SB:
183             telopt = ord(text[position + 2])
184             if telopt is TELOPT_NAWS:
185                 user.columns = ord(text[position + 3]) * \
186                     256 + ord(text[position + 4])
187             end_subnegotiation = text.find(telnet_proto(IAC, SE), position)
188             if end_subnegotiation > 0:
189                 text = text[:position] + text[end_subnegotiation + 2:]
190             else:
191                 position += 1
192
193         # otherwise, strip out a two-byte IAC command
194         elif len_text > position + 2:
195             mudpy.misc.log("Unknown Telnet IAC command %s ignored." % command)
196             text = text[:position] + text[position + 2:]
197
198         # and this means we got the begining of an IAC
199         else:
200             position += 1
201
202     # replace the input with our cleaned-up text
203     user.partial_input = text