import select import psycopg2 import psycopg2.extensions from knobs import config class postgres_interfacing(): def __init__(self): self.connection_string = config.postgres['connection_string'] self.postgres_channel = config.postgres['channel'] self.postgres_listen_string = "LISTEN %s;" % self.postgres_channel self.bot_command_prefix = config.bot['bot_command_prefix'] self.bot_name = config.bot['bot_name'] self.route_all_notifications = config.route_all_notifications self.ignore_list = config.ignore_list def send_to_outbox(self, target, payload): #Insert a target and message into the 'outbox' table on local postgres server conn = psycopg2.connect(self.connection_string) cur = conn.cursor() cur.execute("INSERT INTO outbox(target, message) VALUES(%s,%s)", (target, payload)) conn.commit() cur.close() conn.close() def irc_reply(self, target, source, payload): if source != self.bot_name: #make sure bot doesn't talk to itself if target == self.bot_name: #is the target this bot self.send_to_outbox(source, payload) elif target[:1] == '#': #is the target a channel self.send_to_outbox(target, payload) def fetch_irc_message(self, log_id): #Fetch the irc message in the 'log' table based off of the log id conn = psycopg2.connect(self.connection_string) cur = conn.cursor() cur.execute("SELECT target, source, message FROM log WHERE id = %s;", (log_id,)) row = cur.fetchone() target, source, message = row[0], row[1], row[2] return target, source, message cur.close() conn.close()