raw
logbot_command_ro...    1 #!/usr/bin/env python2
logbot_command_ro... 2 import select
logbot_command_ro... 3 import psycopg2
logbot_command_ro... 4 import psycopg2.extensions
logbot_command_ro... 5 from knobs import router
logbot_command_ro... 6 import postgres_interfacing
logbot_command_ro... 7
logbot_command_ro... 8 class main():
logbot_command_ro... 9 #LISTEN to postgres_channel for postgres-payload and parse for commands intended for bot, routing it to the proper submodule.
logbot_command_ro... 10 #For use with logbot-genesis or logbot-multiple-channels-corrected on a LOCAL postgres server
logbot_command_ro... 11
logbot_command_ro... 12 def __init__(self):
logbot_command_ro... 13 self.my_router = router.router()
logbot_command_ro... 14 self.my_interface = postgres_interfacing.postgres_interfacing()
logbot_command_ro... 15
logbot_command_ro... 16 def parse_message(self, message):
logbot_command_ro... 17 #parse irc message for valid command-calling syntax and return extracted command and arguments
logbot_command_ro... 18 bot_command_prefix = self.my_interface.bot_command_prefix
logbot_command_ro... 19 bot_command_prefix_len = len(bot_command_prefix)
logbot_command_ro... 20 if message[:bot_command_prefix_len] in bot_command_prefix:
logbot_command_ro... 21 command_start_pos = bot_command_prefix_len + (len(message[bot_command_prefix_len:]) - len(message[bot_command_prefix_len:].lstrip()))
logbot_command_ro... 22 if message[command_start_pos:].find(" ") > -1:
logbot_command_ro... 23 command_end_pos = command_start_pos + message[command_start_pos:].find(" ")
logbot_command_ro... 24 command = message[command_start_pos:command_end_pos]
logbot_command_ro... 25 command_arguments = message[command_end_pos:]
logbot_command_ro... 26 else:
logbot_command_ro... 27 command = message[command_start_pos:]
logbot_command_ro... 28 command_arguments = ""
logbot_command_ro... 29 return command, command_arguments
logbot_command_ro... 30
logbot_command_ro... 31 def send_to_command_router(self, target, source, command, command_arguments):
logbot_command_ro... 32 #do stuff with valid commands
logbot_command_ro... 33 command_function = "cmd_%s" % command
logbot_command_ro... 34 try:
logbot_command_ro... 35 route_command = getattr(self.my_router, command_function)(target, source, command_arguments)
logbot_command_ro... 36 except AttributeError:
logbot_command_ro... 37 pass
logbot_command_ro... 38
logbot_command_ro... 39 def listen_for_notifications(self):
logbot_command_ro... 40 #Main Loop ===================
logbot_command_ro... 41 #Execute a LISTEN on the 'log_new_message' channel on local postgres server.
logbot_command_ro... 42 #If NOTIFY is recieved, feed the payload (which corresponds to an id in the 'log' table) into fetch_irc_message() to return the message.
logbot_command_ro... 43 #Parse message and handle as configured in knobs and commands
logbot_command_ro... 44 conn = psycopg2.connect(self.my_interface.connection_string)
logbot_command_ro... 45 conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
logbot_command_ro... 46 cur = conn.cursor()
logbot_command_ro... 47 cur.execute(self.my_interface.postgres_listen_string)
logbot_command_ro... 48 print "Waiting for notifications on postgres channel '%s'" % self.my_interface.postgres_channel
logbot_command_ro... 49 while 1:
logbot_command_ro... 50 if select.select([conn],[],[],5) == ([],[],[]):
logbot_command_ro... 51 print "Timeout"
logbot_command_ro... 52 else:
logbot_command_ro... 53 conn.poll()
logbot_command_ro... 54 while conn.notifies:
logbot_command_ro... 55 notify = conn.notifies.pop(0)
logbot_command_ro... 56 print "Got NOTIFY:", notify.pid, notify.channel, notify.payload
logbot_command_ro... 57 target, source, message = self.my_interface.fetch_irc_message(notify.payload)
logbot_command_ro... 58 print "Message: %s" % message
logbot_command_ro... 59 if source not in self.my_interface.ignore_list:
logbot_command_ro... 60 if self.parse_message(message):
logbot_command_ro... 61 command, command_arguments = self.parse_message(message)
logbot_command_ro... 62 print "Attempting to route command '%s' with arguments '%s'..." % (command, command_arguments)
logbot_command_ro... 63 self.send_to_command_router(target, source, command, command_arguments)
logbot_command_ro... 64 elif self.my_interface.route_all_notifications == True:
logbot_command_ro... 65 print "Routing non-command message: '%s'" % message
logbot_command_ro... 66 self.my_router.passive(target, source, message)
logbot_command_ro... 67 else:
logbot_command_ro... 68 print "Ignoring nick '%s'" % source
logbot_command_ro... 69
logbot_command_ro... 70 if __name__ == "__main__":
logbot_command_ro... 71 print "running"
logbot_command_ro... 72 my_connection = main()
logbot_command_ro... 73 my_connection.listen_for_notifications()