Proper RFC 1143 Telnet option negotiation queue.
authorJeremy Stanley <fungi@yuggoth.org>
Tue, 4 May 2010 03:01:14 +0000 (03:01 +0000)
committerJeremy Stanley <fungi@yuggoth.org>
Tue, 4 May 2010 03:01:14 +0000 (03:01 +0000)
* lib/mudpy/__init__.py: Added telnet to the modules list.

* lib/mudpy/misc.py (Element.send, User.__init__)
(User.adjust_echoing, User.enqueue_input, User.flush)
(User.replace_old_connections, User.send, check_for_connection)
(get_echo_sequence, handle_user_input): Minor adjustments to
accomodate new code in telnet.py.
(User.negotiate_telnet_options, telnet_proto): Moved to telnet.py
and reworked option negotiation stack to eliminate a possible loop,
bringing the code fully in compliance with the "Q" method described
in IETF RFC 1143.

* lib/mudpy/telnet.py: New file for constants and functions related
to support of the Telnet protocol.

lib/mudpy/__init__.py
lib/mudpy/misc.py
lib/mudpy/telnet.py [new file with mode: 0644]

index 0676090..d462528 100644 (file)
@@ -21,5 +21,5 @@ def load():
       except NameError: exec(u"import %s" % module)
 
 # load the modules contained in this package
-modules = [ u"misc" ]
+modules = [ u"misc", u"telnet" ]
 load()
index 3464744..3b11a12 100644 (file)
@@ -185,11 +185,22 @@ class Element:
       raw=False,
       flush=False,
       add_prompt=True,
-      just_prompt=False
+      just_prompt=False,
+      add_terminator=False,
+      prepend_padding=True
    ):
       u"""Convenience method to pass messages to an owner."""
       if self.owner:
-         self.owner.send(message, eol, raw, flush, add_prompt, just_prompt)
+         self.owner.send(
+            message,
+            eol,
+            raw,
+            flush,
+            add_prompt,
+            just_prompt,
+            add_terminator,
+            prepend_padding
+         )
 
    def can_run(self, command):
       u"""Check if the user can run this command object."""
@@ -642,15 +653,13 @@ class User:
 
    def __init__(self):
       u"""Default values for the in-memory user variables."""
+      import telnet
       self.account = None
       self.address = u""
       self.authenticated = False
       self.avatar = None
-      self.client_displays_binary = False
-      self.client_sends_binary = False
       self.columns = 79
       self.connection = None
-      self.echoing = True
       self.error = u""
       self.input_queue = []
       self.last_address = u""
@@ -661,9 +670,8 @@ class User:
       self.output_queue = []
       self.partial_input = ""
       self.password_tries = 0
-      self.received_newline = True
       self.state = u"initial"
-      self.terminator = telnet_proto([u"IAC",u"GA"])
+      self.telopts = {}
 
    def quit(self):
       u"""Log, close the connection and remove."""
@@ -776,14 +784,6 @@ class User:
             old_user.connection = self.connection
             old_user.last_address = old_user.address
             old_user.address = self.address
-            old_user.client_displays_binary = self.client_displays_binary
-            old_user.client_sends_binary = self.client_sends_binary
-
-            # may need to tell the new connection to echo
-            if old_user.echoing:
-               old_user.send(
-                  get_echo_sequence(old_user.state, self.echoing), raw=True
-               )
 
             # take this one out of the list and delete
             self.remove()
@@ -823,8 +823,12 @@ class User:
 
    def adjust_echoing(self):
       u"""Adjust echoing to match state menu requirements."""
-      if self.echoing and not menu_echo_on(self.state): self.echoing = False
-      elif not self.echoing and menu_echo_on(self.state): self.echoing = True
+      import telnet
+      if telnet.is_enabled(self, telnet.TELOPT_ECHO, telnet.US):
+         if menu_echo_on(self.state):
+            telnet.disable(self, telnet.TELOPT_ECHO, telnet.US)
+      elif not menu_echo_on(self.state):
+         telnet.enable(self, telnet.TELOPT_ECHO, telnet.US)
 
    def remove(self):
       u"""Remove a user from the list of connected users."""
@@ -838,9 +842,11 @@ class User:
       flush=False,
       add_prompt=True,
       just_prompt=False,
-      add_terminator=False
+      add_terminator=False,
+      prepend_padding=True
    ):
       u"""Send arbitrary text to a connected user."""
+      import telnet
 
       # unless raw mode is on, clean it up all nice and pretty
       if not raw:
@@ -856,11 +862,10 @@ class User:
          # start with a newline, append the message, then end
          # with the optional eol string passed to this function
          # and the ansi escape to return to normal text
-         if not just_prompt:
-            if not self.output_queue or not self.output_queue[-1].endswith(
-               "\r\n"
-            ):
-               output = u"$(eol)$(eol)" + output
+         if not just_prompt and prepend_padding:
+            if not self.output_queue \
+               or not self.output_queue[-1].endswith("\r\n"):
+               output = u"$(eol)" + output
             elif not self.output_queue[-1].endswith(
                "\r\n\x1b[0m\r\n"
             ) and not self.output_queue[-1].endswith(
@@ -887,18 +892,18 @@ class User:
             output = wrap_ansi_text(output, wrap)
 
          # if supported by the client, encode it utf-8
-         if self.client_displays_binary:
+         if telnet.is_enabled(self, telnet.TELOPT_BINARY, telnet.US):
             encoded_output = output.encode(u"utf-8")
 
          # otherwise just send ascii
-         else:
-            encoded_output = output.encode(u"ascii", u"replace")
-
-         # inject appropriate echoing changes
-         encoded_output += get_echo_sequence(self.state, self.echoing)
+         else: encoded_output = output.encode(u"ascii", u"replace")
 
          # end with a terminator if requested
-         if add_terminator: encoded_output += self.terminator
+         if add_prompt or add_terminator:
+            if telnet.is_enabled(self, telnet.TELOPT_EOR, telnet.US):
+               encoded_output += telnet.telnet_proto(telnet.IAC, telnet.EOR)
+            elif not telnet.is_enabled(self, telnet.TELOPT_SGA, telnet.US):
+               encoded_output += telnet.telnet_proto(telnet.IAC, telnet.GA)
 
          # and tack it onto the queue
          self.output_queue.append(encoded_output)
@@ -946,10 +951,6 @@ class User:
    def flush(self):
       u"""Try to send the last item in the queue and remove it."""
       if self.output_queue:
-         if self.received_newline:
-            self.received_newline = False
-            if self.output_queue[0].startswith("\r\n"):
-               self.output_queue[0] = self.output_queue[0][2:]
          try:
             self.connection.send(self.output_queue[0])
             del self.output_queue[0]
@@ -966,7 +967,7 @@ class User:
 
    def enqueue_input(self):
       u"""Process and enqueue any new input."""
-      import unicodedata
+      import telnet, unicodedata
 
       # check for some input
       try:
@@ -981,7 +982,7 @@ class User:
          self.partial_input += raw_input
 
          # reply to and remove any IAC negotiation codes
-         self.negotiate_telnet_options()
+         telnet.negotiate_telnet_options(self)
 
          # separate multiple input lines
          new_input_lines = self.partial_input.split("\n")
@@ -1015,9 +1016,7 @@ class User:
                line = ""
 
             # log non-printable characters remaining
-            if not hasattr(
-               self, u"client_sends_binary"
-            ) or not self.client_sends_binary:
+            if telnet.is_enabled(self, telnet.TELOPT_BINARY, telnet.HIM):
                asciiline = filter(lambda x: " " <= x <= "~", line)
                if line != asciiline:
                   logline = u"Non-ASCII characters from "
@@ -1033,147 +1032,6 @@ class User:
                unicodedata.normalize( u"NFKC", unicode(line, u"utf-8") )
             )
 
-   def negotiate_telnet_options(self):
-      u"""Reply to/remove partial_input telnet negotiation options."""
-
-      # start at the begining of the input
-      position = 0
-
-      # make a local copy to play with
-      text = self.partial_input
-
-      # as long as we haven't checked it all
-      while position < len(text):
-
-         # jump to the first IAC you find
-         position = text.find(telnet_proto([u"IAC"]), position)
-
-         # if there wasn't an IAC in the input, we're done
-         if position < 0: break
-
-         # replace a double (literal) IAC if there's an LF later
-         elif len(text) > position+1 and text[position+1] == telnet_proto(
-            [u"IAC"]
-         ):
-            if text.find("\n", position) > 0:
-               text = text.replace(
-                  telnet_proto([u"IAC",u"IAC"]), telnet_proto([u"IAC"])
-               )
-            else: position += 1
-            position += 1
-
-         # implement an RFC 1143 option negotiation queue here
-         elif len(text) > position+2 and text[position+1] in (
-            telnet_proto([u"DO",u"DONT",u"WILL",u"WONT"])
-         ):
-            negotiation = text[position+1:position+3]
-
-            # if we turned echo off, ignore the confirmation
-            if not self.echoing and negotiation == telnet_proto(
-               [u"DO",u"TELOPT_ECHO"]
-            ):
-               self.send(
-                  telnet_proto([u"IAC",u"WILL",u"TELOPT_ECHO"]), raw=True
-               )
-
-            # BINARY mode handling for unicode support (RFC 856)
-            elif negotiation == telnet_proto([u"DO",u"TELOPT_BINARY"]):
-               self.send(
-                  telnet_proto([u"IAC",u"WILL",u"TELOPT_BINARY"]), raw=True
-               )
-               self.client_displays_binary = True
-            elif negotiation == telnet_proto([u"DONT",u"TELOPT_BINARY"]):
-               self.send(
-                  telnet_proto([u"IAC",u"WONT",u"TELOPT_BINARY"]), raw=True
-               )
-               self.client_displays_binary = False
-            elif negotiation == telnet_proto([u"WILL",u"TELOPT_BINARY"]):
-               self.send(
-                  telnet_proto([u"IAC",u"DO",u"TELOPT_BINARY"]), raw=True
-               )
-               self.client_sends_binary = True
-            elif negotiation == telnet_proto([u"WONT",u"TELOPT_BINARY"]):
-               self.send(
-                  telnet_proto([u"IAC",u"DONT",u"TELOPT_BINARY"]), raw=True
-               )
-               self.client_sends_binary = False
-
-            # allow LINEMODE (RFC 1184)
-            elif negotiation == telnet_proto([u"WILL",u"TELOPT_LINEMODE"]):
-               self.send(
-                  telnet_proto([u"IAC",u"DO",u"TELOPT_LINEMODE"]), raw=True
-               )
-            elif negotiation == telnet_proto([u"WONT",u"TELOPT_LINEMODE"]):
-               self.send(
-                  telnet_proto([u"IAC",u"DONT",u"TELOPT_LINEMODE"]), raw=True
-               )
-
-            # allow NAWS (RFC 1073)
-            elif negotiation == telnet_proto([u"WILL",u"TELOPT_NAWS"]):
-               self.send(
-                  telnet_proto([u"IAC",u"DO",u"TELOPT_NAWS"]), raw=True
-               )
-            elif negotiation == telnet_proto([u"WONT",u"TELOPT_NAWS"]):
-               self.send(
-                  telnet_proto([u"IAC",u"DONT",u"TELOPT_NAWS"]), raw=True
-               )
-
-            # if the client likes EOR (RFC 885) instead of GA, note it
-            elif negotiation == telnet_proto([u"DO",u"TELOPT_EOR"]):
-               self.send(
-                  telnet_proto([u"IAC",u"WILL",u"TELOPT_EOR"]), raw=True
-               )
-               self.terminator = telnet_proto([u"IAC",u"EOR"])
-            elif negotiation == telnet_proto([u"DONT",u"TELOPT_EOR"]):
-               self.send(
-                  telnet_proto([u"IAC",u"WONT",u"TELOPT_EOR"]), raw=True
-               )
-               if self.terminator == telnet_proto([u"IAC",u"EOR"]):
-                  self.terminator = telnet_proto([u"IAC",u"GA"])
-
-            # if the client doesn't want GA, oblige (RFC 858)
-            elif negotiation == telnet_proto([u"DO",u"TELOPT_SGA"]):
-               self.send(telnet_proto([u"IAC",u"WILL",u"TELOPT_SGA"]),
-                  raw=True)
-               if self.terminator == telnet_proto([u"IAC",u"GA"]):
-                  self.terminator = ""
-
-            # we don't want to allow anything else
-            elif text[position+1] == telnet_proto([u"DO"]):
-               self.send(
-                  telnet_proto([u"IAC",u"WONT"])+text[position+2], raw=True
-               )
-            elif text[position+1] == telnet_proto([u"WILL"]):
-               self.send(
-                  telnet_proto([u"IAC",u"DONT"])+text[position+2], raw=True
-               )
-
-            # strip the negotiation from the input
-            text = text.replace(text[position:position+3], "")
-
-         # subnegotiation options
-         elif len(text) > position+4 and text[
-            position:position+2
-         ] == telnet_proto([u"IAC",u"SB"]):
-            if text[position+2] == telnet_proto([u"TELOPT_NAWS"]):
-               self.columns = ord(text[position+3])*256+ord(text[position+4])
-            end_subnegotiation = text.find(
-               telnet_proto([u"IAC",u"SE"]), position
-            )
-            if end_subnegotiation > 0:
-               text = text[:position] + text[end_subnegotiation+2:]
-            else: position += 1
-
-         # otherwise, strip out a two-byte IAC command
-         elif len(text) > position+2:
-            text = text.replace(text[position:position+2], "")
-
-         # and this means we got the begining of an IAC
-         else: position += 1
-
-      # replace the input with our cleaned-up text
-      self.partial_input = text
-
    def new_avatar(self):
       u"""Instantiate a new, unconfigured avatar for this user."""
       counter = 0
@@ -1247,34 +1105,6 @@ def makedict(value):
    elif value.find(u":") > 0: return eval(u"{" + value + u"}")
    else: return { value: None }
 
-def telnet_proto(arguments):
-   u"""Return a concatenated series of Telnet protocol commands."""
-
-   # same names as bsd's arpa/telnet.h (telnetlib's are ambiguous)
-   telnet_commands = {
-      u"TELOPT_BINARY": 0, # RFC 856
-      u"TELOPT_ECHO": 1, # RFC 857
-      u"TELOPT_SGA": 3, # RFC 858
-      u"TELOPT_EOR": 25, # RFC 885
-      u"TELOPT_NAWS": 31, # RFC 1073
-      u"TELOPT_LINEMODE": 34, # RFC 1184
-      u"EOR": 239,
-      u"SE": 240,
-      u"GA": 249,
-      u"SB": 250,
-      u"WILL": 251,
-      u"WONT": 252,
-      u"DO": 253,
-      u"DONT": 254,
-      u"IAC": 255
-   }
-
-   # (this will need to be byte type during 2to3 migration)
-   command_series = ""
-   for argument in arguments:
-      command_series += chr(telnet_commands[argument])
-   return command_series
-
 def broadcast(message, add_prompt=True):
    u"""Send a message to all connected users."""
    for each_user in universe.userlist:
@@ -1699,6 +1529,7 @@ def reload_data():
 
 def check_for_connection(listening_socket):
    u"""Check for a waiting connection and return a new user object."""
+   import telnet
 
    # try to accept a new connection
    try:
@@ -1722,7 +1553,7 @@ def check_for_connection(listening_socket):
    user.address = address[0]
 
    # let the client know we WILL EOR (RFC 885)
-   user.send(telnet_proto([u"IAC",u"WILL",u"TELOPT_EOR"]), raw=True)
+   telnet.enable(user, telnet.TELOPT_EOR, telnet.US)
    user.negotiation_pause = 2
 
    # return the new user object
@@ -1756,22 +1587,6 @@ def menu_echo_on(state):
    u"""True if echo is on, false if it is off."""
    return universe.categories[u"menu"][state].getboolean(u"echo", True)
 
-def get_echo_sequence(state, echoing):
-   u"""Build the appropriate IAC WILL/WONT ECHO sequence as needed."""
-
-   # if the user has echo on and the menu specifies it should be turned
-   # off, send: iac + will + echo + null
-   if echoing and not menu_echo_on(state):
-      return telnet_proto([u"IAC",u"WILL",u"TELOPT_ECHO"])
-
-   # if echo is not set to off in the menu and the user curently has echo
-   # off, send: iac + wont + echo + null
-   elif not echoing and menu_echo_on(state):
-      return telnet_proto([u"IAC",u"WONT",u"TELOPT_ECHO"])
-
-   # default is not to send an echo control sequence at all
-   else: return ""
-
 def get_echo_message(state):
    u"""Return a message indicating that echo is off."""
    if menu_echo_on(state): return u""
@@ -1899,9 +1714,11 @@ def get_choice_action(user, choice):
 
 def handle_user_input(user):
    u"""The main handler, branches to a state-specific handler."""
+   import telnet
 
    # if the user's client echo is off, send a blank line for aesthetics
-   if user.echoing: user.received_newline = True
+   if telnet.is_enabled(user, telnet.TELOPT_ECHO, telnet.US):
+      user.send(u"", add_prompt=False, prepend_padding=False)
 
    # check to make sure the state is expected, then call that handler
    if u"handler_" + user.state in globals():
diff --git a/lib/mudpy/telnet.py b/lib/mudpy/telnet.py
new file mode 100644 (file)
index 0000000..293d997
--- /dev/null
@@ -0,0 +1,185 @@
+# -*- coding: utf-8 -*-
+u"""Telnet functions and constants for the mudpy engine."""
+
+# Copyright (c) 2004-2010 Jeremy Stanley <fungi@yuggoth.org>. Permission
+# to use, copy, modify, and distribute this software is granted under
+# terms provided in the LICENSE file distributed with this software.
+
+# telnet options (from bsd's arpa/telnet.h since telnetlib's are ambiguous)
+TELOPT_BINARY = 0 # transmit 8-bit data by the receiver (rfc 856)
+TELOPT_ECHO = 1 # echo received data back to the sender (rfc 857)
+TELOPT_SGA = 3 # suppress transmission of the go ahead character (rfc 858)
+#TELOPT_TTYPE = 24 # exchange terminal type information (rfc 1091)
+TELOPT_EOR = 25 # transmit end-of-record after transmitting data (rfc 885)
+TELOPT_NAWS = 31 # negotiate about window size (rfc 1073)
+TELOPT_LINEMODE = 34 # cooked line-by-line input mode (rfc 1184)
+
+supported = (
+   TELOPT_BINARY,
+   TELOPT_ECHO,
+   TELOPT_SGA,
+   TELOPT_EOR,
+   TELOPT_NAWS,
+   TELOPT_LINEMODE
+)
+
+# telnet commands
+EOR = 239 # end-of-record signal (rfc 885)
+SE = 240 # end of subnegotiation parameters (rfc 854)
+GA = 249 # go ahead signal (rfc 854)
+SB = 250 # what follows is subnegotiation of the indicated option (rfc 854)
+WILL = 251 # desire or confirmation of performing an option (rfc 854)
+WONT = 252 # refusal or confirmation of performing an option (rfc 854)
+DO = 253 # request or confirm performing an option (rfc 854)
+DONT = 254 # demand or confirm no longer performing an option (rfc 854)
+IAC = 255 # interpret as command escape character (rfc 854)
+
+# RFC 1143 option negotiation states
+NO = 0 # option is disabled
+YES = 1 # option is enabled
+WANTNO = 2 # demanded disabling option
+WANTYES = 3 # requested enabling option
+WANTNO_OPPOSITE = 4 # demanded disabling option but queued an enable after it
+WANTYES_OPPOSITE = 5 # requested enabling option but queued a disable after it
+
+# RFC 1143 option negotiation parties
+HIM = 0
+US = 1
+
+def telnet_proto(*arguments):
+   u"""Return a concatenated series of Telnet protocol commands."""
+   # (this will need to be byte type during 2to3 migration)
+   return "".join( [chr(x) for x in arguments] )
+
+def send_command(user, *command):
+   u"""Sends a Telnet command string to the specified user's socket."""
+   user.send( telnet_proto(IAC, *command), raw=True )
+
+def is_enabled(user, telopt, party, state=YES):
+   u"""Returns True if the indicated Telnet option is enabled, False if not."""
+   if (telopt, party) in user.telopts and user.telopts[
+      (telopt, party)
+   ] is state: return True
+   else: return False
+
+def enable(user, telopt, party):
+   u"""Negotiates enabling a Telnet option for the indicated user's socket."""
+   if party is HIM: txpos = DO
+   else: txpos = WILL
+   if not (telopt, party) in user.telopts or user.telopts[
+      (telopt, party)
+   ] is NO:
+      user.telopts[ (telopt, party) ] = WANTYES
+      send_command(user, txpos, telopt)
+   elif user.telopts[ (telopt, party) ] is WANTNO:
+      user.telopts[ (telopt, party) ] = WANTNO_OPPOSITE
+   elif user.telopts[ (telopt, party) ] is WANTYES_OPPOSITE:
+      user.telopts[ (telopt, party) ] = WANTYES
+
+def disable(user, telopt, party):
+   u"""Negotiates disabling a Telnet option for the indicated user's socket."""
+   if party is HIM: txneg = DONT
+   else: txneg = WONT
+   if not (telopt, party) in user.telopts: user.telopts[ (telopt, party) ] = NO
+   elif user.telopts[ (telopt, party) ] is YES:
+      user.telopts[ (telopt, party) ] = WANTNO
+      send_command(user, txneg, telopt)
+   elif user.telopts[ (telopt, party) ] is WANTYES:
+      user.telopts[ (telopt, party) ] = WANTYES_OPPOSITE
+   elif user.telopts[ (telopt, party) ] is WANTNO_OPPOSITE:
+      user.telopts[ (telopt, party) ] = WANTNO
+
+def negotiate_telnet_options(user):
+   u"""Reply to and remove telnet negotiation options from partial_input."""
+   import misc
+
+   # make a local copy to play with
+   text = user.partial_input
+
+   # start at the begining of the input
+   position = 0
+
+   # as long as we haven't checked it all
+   len_text = len(text)
+   while position < len_text:
+
+      # jump to the first IAC you find
+      position = text.find( telnet_proto(IAC), position )
+
+      # if there wasn't an IAC in the input or it's at the end, we're done
+      if position < 0 or position >= len_text-1: break
+
+      # the byte following the IAC is our command
+      # (this will need to be byte type during 2to3 migration)
+      command = ord( text[position+1] )
+
+      # replace a double (literal) IAC if there's an LF later
+      if command is IAC:
+         if text.find("\n", position) > 0:
+            position += 1
+            text = text[:position] + text[position+1:]
+         else: position += 2
+
+      # implement an RFC 1143 option negotiation queue here
+      elif len_text > position+2 and WILL <= command <= DONT:
+         # this will need to be byte type during 2to3 migration
+         telopt = ord( text[position+2] )
+         if telopt in supported:
+            if command <= WONT:
+               party = HIM
+               rxpos = WILL
+               txpos = DO
+               txneg = DONT
+            else:
+               party = US
+               rxpos = DO
+               txpos = WILL
+               txneg = WONT
+            if (telopt, party) not in user.telopts:
+               user.telopts[ (telopt, party) ] = NO
+            if command is rxpos:
+               if user.telopts[ (telopt, party) ] is NO:
+                  user.telopts[ (telopt, party) ] = YES
+                  send_command(user, txpos, telopt)
+               elif user.telopts[ (telopt, party) ] is WANTNO:
+                  user.telopts[ (telopt, party) ] = NO
+               elif user.telopts[ (telopt, party) ] is WANTNO_OPPOSITE:
+                  user.telopts[ (telopt, party) ] = YES
+               elif user.telopts[ (telopt, party) ] is WANTYES_OPPOSITE:
+                  user.telopts[ (telopt, party) ] = WANTNO
+                  send_command(user, txneg, telopt)
+               else: user.telopts[ (telopt, party) ] = YES
+            else:
+               if user.telopts[ (telopt, party) ] is YES:
+                  user.telopts[ (telopt, party) ] = NO
+                  send_command(user, txneg, telopt)
+               elif user.telopts[ (telopt, party) ] is WANTNO_OPPOSITE:
+                  user.telopts[ (telopt, party) ] = WANTYES
+                  send_command(user, txpos, telopt)
+               else: user.telopts[ (telopt, party) ] = NO
+         elif command is WILL: send_command(user, DONT, telopt )
+         else: send_command(user, WONT, telopt)
+         text = text[:position] + text[position+3:]
+
+      # subnegotiation options
+      elif len_text > position+4 and command is SB:
+         # this will need to be byte type during 2to3 migration
+         telopt = ord( text[position+2] )
+         if telopt is TELOPT_NAWS:
+            # this will need to be byte type during 2to3 migration
+            user.columns = ord(text[position+3])*256+ord(text[position+4])
+         end_subnegotiation = text.find( telnet_proto(IAC, SE), position )
+         if end_subnegotiation > 0:
+            text = text[:position] + text[end_subnegotiation+2:]
+         else: position += 1
+
+      # otherwise, strip out a two-byte IAC command
+      elif len_text > position+2:
+         misc.log(u"Unknown Telnet IAC command %s ignored." % command)
+         text = text[:position] + text[position+2:]
+
+      # and this means we got the begining of an IAC
+      else: position += 1
+
+   # replace the input with our cleaned-up text
+   user.partial_input = text