+ def negotiate_telnet_options(self):
+ """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(IAC, position)
+
+ # if there wasn't an IAC in the input, skip to the end
+ if position < 0: position = len(text)
+
+ # replace a double (literal) IAC and move on
+ elif len(text) > position+1 and text[position+1] == IAC:
+ text = text.replace(IAC+IAC, IAC)
+ position += 1
+
+ # this must be an option negotiation
+ elif len(text) > position+2 and text[position+1] in (DO, DONT, WILL, WONT):
+
+ # if we turned echo off, ignore the confirmation
+ if not self.echoing and text[position+1:position+3] == DO+ECHO: pass
+
+ # we don't want to allow anything else
+ elif text[position+1] in (DO, WILL): self.send(IAC+WONT+text[position+2])
+
+ # strip the negotiation from the input
+ text = text.replace(text[position:position+3], "")
+
+ # otherwise, strip out a two-byte IAC command
+ else: text = text.replace(text[position:position+2], "")
+
+ # replace the input with our cleaned-up text
+ self.partial_input = text
+