def run(self): """ Connect to the IRC server and wait for messages to send. If connection is lost, automatically reconnect. """ while True: # Connect to IRC irc_sock = Irc(self.config) irc_sock.set_parser(IrcParser()) try: irc_sock.connect() except IrcError as e: logging.error('IRC error: {0}'.format(e.value)) time.sleep(1) continue # Send messages until disconnect while(True): out_msg = self.out_queue.get(block=True) out_str = 'PRIVMSG {0} : {1}\r\n'.format( self.config['irc']['channel'], out_msg) timeout = self.config['irc']['send_timeout'] sent_success = irc_sock.send_message(out_str, timeout=timeout) if not sent_success: logging.warning( 'IRC: failed to send chat message {0}'.format(out_str)) break GLib.idle_add(self.glib_func, ['DISCONNECTED'])
def run(self): """ Connect to the IRC server and send events to the GUI. This funtion is an endless loop that never exits, as it waits for a message before sending a signal to the GUI. If the connection to the server is lost, the connection is automatically re-established. In addition to the events defined by Irc, the following events are also sent: - CONNECTING: ['CONNECTING'] Connecting to the server. - CONNECTED: ['CONNECTED'] Connected to the server. - DISCONNECTED: ['DISCONNECTED'] Connection to the server lost. """ self.glib_func = self.func_queue.get() while True: # Connect to IRC GLib.idle_add(self.glib_func, ['CONNECTING']) irc_sock = Irc(self.config) irc_sock.set_parser(IrcParser()) try: irc_sock.connect('TWITCHCLIENT 1\r\n') except IrcError as e: logging.error('IRC error: {0}'.format(e.value)) time.sleep(1) continue GLib.idle_add(self.glib_func, ['CONNECTED']) # Receive messages until disconnect try: while(True): if irc_sock.receive_message(timeout=None): messages = irc_sock.parse_message() for msg in messages: GLib.idle_add(self.glib_func, msg) except IrcError: pass GLib.idle_add(self.glib_func, ['DISCONNECTED'])