예제 #1
0
 def handle_command(self, op, action, data):
     if op == Commands.SB and action == Commands.NAWS:
         try:
             print '* Client screen size: {}x{}'.format(
                 Utils.decode_word(data[0:2]),
                 Utils.decode_word(data[2:4]),
             )
         except:
             exc = traceback.format_exc()
             print exc
             self.socket.send(exc)
예제 #2
0
    def process(self):
        while len(self.stack):
            head = self.stack[0]
            if head == Commands.IAC and len(self.stack) >= 2:
                op = self.stack[1]

                if len(self.stack) >= 3:
                    iac = self.stack.pop(0)
                    op = self.stack.pop(0)
                    action = self.stack.pop(0)
                    print 'Got command:', Commands.decode_cmd(iac, op, action)

                    if op == Commands.SB:
                        data = []
                        while self.stack[0:2] != [Commands.IAC, Commands.SE]:
                            b = self.stack.pop(0)
                            data.append(b)
                        self.stack.pop(0)
                        self.stack.pop(0)

                        print 'Subnegotiation data:', [ord(x) for x in data]

                        self.handle_command(op, action, data)

                    continue
                # elif op == Commands.SB and (Commands.IAC + Commands.SE in ''.join(self.stack)):
                #     iac = self.stack.pop(0)
                #     op = self.stack.pop(0)
                #     action = self.stack.pop(0)
                #
                #
                #     continue
                else:
                    return

            elif Utils.is_ascii(head):
                self.stack.pop(0)
                self.socket.send(head)

                continue

            elif head == '\x0D':
                self.stack.pop(0)
                self.socket.send('\x0D\x0A')

            elif head == '\t':
                self.stack.pop(0)
                self.socket.send('<TAB>')

                continue
            elif head == Commands.ESC:
                # if len(self.stack) >= 2:
                #     self.socket.send(self.stack)
                # else:

                self.stack.pop(0)

                # TODO: Implement escape char parsing
                code_len, value = ANSI.inspect(self.stack)
                self.socket.send('<{}>'.format(value))

                for i in xrange(code_len):
                    self.stack.pop(0)

                    # self.socket.send('<ESCAPE>')

                continue

            elif head == Commands.CTRL_BREAK:
                self.stack.pop(0)
                self.socket.send('<CTRL+C>')

                continue

            elif head == '\x7F':
                self.stack.pop(0)
                self.socket.send('<BACKSPACE>')

                continue

            elif head == Commands.NULL:
                self.stack.pop(0)

            else:
                print 'Unexpected byte - {}'.format(repr(head))
                b = self.stack.pop(0)

                self.socket.send('<0x{}>'.format('%02X' % ord(b)))

                return