raw
logbot_command_ro...    1 import select
logbot_command_ro... 2 import psycopg2
logbot_command_ro... 3 import psycopg2.extensions
logbot_command_ro... 4 from knobs import config
logbot_command_ro... 5
logbot_command_ro... 6 class postgres_interfacing():
logbot_command_ro... 7
logbot_command_ro... 8 def __init__(self):
logbot_command_ro... 9 self.connection_string = config.postgres['connection_string']
logbot_command_ro... 10 self.postgres_channel = config.postgres['channel']
logbot_command_ro... 11 self.postgres_listen_string = "LISTEN %s;" % self.postgres_channel
logbot_command_ro... 12 self.bot_command_prefix = config.bot['bot_command_prefix']
logbot_command_ro... 13 self.bot_name = config.bot['bot_name']
logbot_command_ro... 14 self.route_all_notifications = config.route_all_notifications
logbot_command_ro... 15 self.ignore_list = config.ignore_list
logbot_command_ro... 16
logbot_command_ro... 17 def send_to_outbox(self, target, payload):
logbot_command_ro... 18 #Insert a target and message into the 'outbox' table on local postgres server
logbot_command_ro... 19 conn = psycopg2.connect(self.connection_string)
logbot_command_ro... 20 cur = conn.cursor()
logbot_command_ro... 21 cur.execute("INSERT INTO outbox(target, message) VALUES(%s,%s)", (target, payload))
logbot_command_ro... 22 conn.commit()
logbot_command_ro... 23 cur.close()
logbot_command_ro... 24 conn.close()
logbot_command_ro... 25
logbot_command_ro... 26 def irc_reply(self, target, source, payload):
logbot_command_ro... 27 if source != self.bot_name:
logbot_command_ro... 28 #make sure bot doesn't talk to itself
logbot_command_ro... 29 if target == self.bot_name:
logbot_command_ro... 30 #is the target this bot
logbot_command_ro... 31 self.send_to_outbox(source, payload)
logbot_command_ro... 32 elif target[:1] == '#':
logbot_command_ro... 33 #is the target a channel
logbot_command_ro... 34 self.send_to_outbox(target, payload)
logbot_command_ro... 35
logbot_command_ro... 36 def fetch_irc_message(self, log_id):
logbot_command_ro... 37 #Fetch the irc message in the 'log' table based off of the log id
logbot_command_ro... 38 conn = psycopg2.connect(self.connection_string)
logbot_command_ro... 39 cur = conn.cursor()
logbot_command_ro... 40 cur.execute("SELECT target, source, message FROM log WHERE id = %s;", (log_id,))
logbot_command_ro... 41 row = cur.fetchone()
logbot_command_ro... 42 target, source, message = row[0], row[1], row[2]
logbot_command_ro... 43 return target, source, message
logbot_command_ro... 44 cur.close()
logbot_command_ro... 45 conn.close()