#!/usr/bin/env python2 import select import psycopg2 import psycopg2.extensions from knobs import router import postgres_interfacing class main(): #LISTEN to postgres_channel for postgres-payload and parse for commands intended for bot, routing it to the proper submodule. #For use with logbot-genesis or logbot-multiple-channels-corrected on a LOCAL postgres server def __init__(self): self.my_router = router.router() self.my_interface = postgres_interfacing.postgres_interfacing() def parse_message(self, message): #parse irc message for valid command-calling syntax and return extracted command and arguments bot_command_prefix = self.my_interface.bot_command_prefix bot_command_prefix_len = len(bot_command_prefix) if message[:bot_command_prefix_len] in bot_command_prefix: command_start_pos = bot_command_prefix_len + (len(message[bot_command_prefix_len:]) - len(message[bot_command_prefix_len:].lstrip())) if message[command_start_pos:].find(" ") > -1: command_end_pos = command_start_pos + message[command_start_pos:].find(" ") command = message[command_start_pos:command_end_pos] command_arguments = message[command_end_pos:] else: command = message[command_start_pos:] command_arguments = "" return command, command_arguments def send_to_command_router(self, target, source, command, command_arguments): #do stuff with valid commands command_function = "cmd_%s" % command try: route_command = getattr(self.my_router, command_function)(target, source, command_arguments) except AttributeError: pass def listen_for_notifications(self): #Main Loop =================== #Execute a LISTEN on the 'log_new_message' channel on local postgres server. #If NOTIFY is recieved, feed the payload (which corresponds to an id in the 'log' table) into fetch_irc_message() to return the message. #Parse message and handle as configured in knobs and commands conn = psycopg2.connect(self.my_interface.connection_string) conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) cur = conn.cursor() cur.execute(self.my_interface.postgres_listen_string) print "Waiting for notifications on postgres channel '%s'" % self.my_interface.postgres_channel while 1: if select.select([conn],[],[],5) == ([],[],[]): print "Timeout" else: conn.poll() while conn.notifies: notify = conn.notifies.pop(0) print "Got NOTIFY:", notify.pid, notify.channel, notify.payload target, source, message = self.my_interface.fetch_irc_message(notify.payload) print "Message: %s" % message if source not in self.my_interface.ignore_list: if self.parse_message(message): command, command_arguments = self.parse_message(message) print "Attempting to route command '%s' with arguments '%s'..." % (command, command_arguments) self.send_to_command_router(target, source, command, command_arguments) elif self.my_interface.route_all_notifications == True: print "Routing non-command message: '%s'" % message self.my_router.passive(target, source, message) else: print "Ignoring nick '%s'" % source if __name__ == "__main__": print "running" my_connection = main() my_connection.listen_for_notifications()