def connect(self): try: self.socket.settimeout(300) try: self.socket.connect((self.server, self.port)) if self.ssl: self.socket = ssl.wrap_socket(self.socket) # TODO error handling :o if self.password: self.send('PASS', self.password) self.send('NICK', self.nick) # 12 = +iw self.send('USER', self.user, '12', '*', ':' + self.realname) self.on('RPL_WELCOME', self.join_channels) self.on('RPL_WELCOME', self.update_nick) self.on('NICK', self.update_nick) except socket.timeout: self.log_error("Timeout connecting. Check your config and internet connection and try again.") sys.exit(1) buffer = '' while not self.ended: try: buffer += self.socket.recv(1024) lines = buffer.split("\n") buffer = lines.pop() for raw in lines: self.recv(raw) (prefix, command, args) = parse_irc_line(raw) handled = self.dispatch(prefix, command, args) if not handled: handled &= self.dispatch(prefix, command, args, fallback=True) if not handled: if command == 'PING': self.send('PONG', ':' + args[0]) except KeyboardInterrupt: self.log_error("Caught keyboard interrupt, quitting...") sys.exit(0) except socket.timeout as e: self.log_error("Timeout (%s), reconnecting..." % e) self.reconnect() except socket.error as e: self.log_error("Socket error (%s), reconnecting..." % e) self.reconnect() finally: self.socket.close()
def connect(self): self.socket.settimeout(300) try: self.socket.connect((self.server, self.port)) # TODO error handling :o if self.password: self.send('PASS', self.password) self.send('NICK', self.nick) # 12 = +iw self.send('USER', self.user, '12', '*', ':' + self.realname) for channel in self.channels: self.join(channel) except socket.timeout: self.log_error("Timeout connecting. Check your config and internet connection and try again.") sys.exit(1) try: buffer = '' while True: try: buffer += self.socket.recv(1024) lines = buffer.split("\n") buffer = lines.pop() for raw in lines: self.recv(raw) (prefix, command, args) = parse_irc_line(raw) handled = self.dispatch(prefix, command, args) if not handled: handled &= self.dispatch(prefix, command, args, fallback=True) if not handled: if command == 'PING': self.send('PONG', ':' + args[0]) except socket.timeout: self.log_error("Timeout, reconnecting...") self.connect() except KeyboardInterrupt, SystemExit: self.log_error("Caught interrupt, quitting...") sys.exit(0) finally: self.socket.close()