From 1aacd8c511800d18234e112a48d17b453c409f0d Mon Sep 17 00:00:00 2001 From: xxyy Date: Sun, 2 Mar 2014 00:37:15 +0100 Subject: [PATCH] Update seen and history to support multiple connections at once, Move seen command to history.py to reduce spaghetti code --- plugins/history.py | 51 ++++++++++++++++++++++++++++++++++++---------- plugins/seen.py | 51 ---------------------------------------------- 2 files changed, 40 insertions(+), 62 deletions(-) delete mode 100644 plugins/seen.py diff --git a/plugins/history.py b/plugins/history.py index 8b9e027..c703bcf 100644 --- a/plugins/history.py +++ b/plugins/history.py @@ -1,26 +1,25 @@ from collections import deque -from util import hook +from util import hook, timesince import time import re -db_ready = False +db_ready = [] -def db_init(db): - """check to see that our db has the the seen table and return a connection.""" +def db_init(db, conn_name): + """check to see that our db has the the seen table (connection name is for caching the result per connection)""" global db_ready - if not db_ready: + if db_ready.count(conn_name) < 1: db.execute("create table if not exists seen_user(name, time, quote, chan, host, " "primary key(name, chan))") db.commit() - db_ready = True + db_ready.append(conn_name) -def track_seen(input, message_time, db): +def track_seen(input, message_time, db, conn): """ Tracks messages for the .seen command """ - if not db_ready: - db_init(db) - # keep private messages private + db_init(db, conn) + # keep private messages private if input.chan[:1] == "#" and not re.findall('^s/.*/.*/$', input.msg.lower()): db.execute("insert or replace into seen_user(name, time, quote, chan, host)" "values(?,?,?,?,?)", (input.nick.lower(), message_time, input.msg, @@ -43,7 +42,7 @@ def track_history(input, message_time, conn): @hook.event('PRIVMSG', ignorebots=False) def chat_tracker(paraml, input=None, db=None, conn=None): message_time = time.time() - track_seen(input, message_time, db) + track_seen(input, message_time, db, conn) track_history(input, message_time, conn) @@ -57,4 +56,34 @@ def resethistory(inp, input=None, conn=None): # wat return "There is no history for this channel." +"""seen.py: written by sklnd in about two beers July 2009""" +@hook.command +def seen(inp, nick='', chan='', db=None, input=None, conn=None): + """seen -- Tell when a nickname was last in active in one of this bot's channels.""" + + if input.conn.nick.lower() == inp.lower(): + return "You need to get your eyes checked." + + if inp.lower() == nick.lower(): + return "Have you looked in a mirror lately?" + + if not re.match("^[A-Za-z0-9_|.\-\]\[]*$", inp.lower()): + return "I can't look up that name, its impossible to use!" + + db_init(db, conn.name) + + last_seen = db.execute("select name, time, quote from seen_user where name" + " like ? and chan = ?", (inp, chan)).fetchone() + + if last_seen: + reltime = timesince.timesince(last_seen[1]) + if last_seen[0] != inp.lower(): # for glob matching + inp = last_seen[0] + if last_seen[2][0:1] == "\x01": + return '{} was last seen {} ago: * {} {}'.format(inp, reltime, inp, + last_seen[2][8:-1]) + else: + return '{} was last seen {} ago saying: {}'.format(inp, reltime, last_seen[2]) + else: + return "I've never seen {} talking in this channel.".format(inp) diff --git a/plugins/seen.py b/plugins/seen.py deleted file mode 100644 index 9200e50..0000000 --- a/plugins/seen.py +++ /dev/null @@ -1,51 +0,0 @@ -"""seen.py: written by sklnd in about two beers July 2009""" - -import time -import re - -from util import hook, timesince - - -db_ready = False - - -def db_init(db): - """check to see that our db has the the seen table and return a connection.""" - global db_ready - if not db_ready: - db.execute("create table if not exists seen_user(name, time, quote, chan, host, " - "primary key(name, chan))") - db.commit() - db_ready = True - - -@hook.command -def seen(inp, nick='', chan='', db=None, input=None): - """seen -- Tell when a nickname was last in active in one of this bot's channels.""" - - if input.conn.nick.lower() == inp.lower(): - return "You need to get your eyes checked." - - if inp.lower() == nick.lower(): - return "Have you looked in a mirror lately?" - - if not re.match("^[A-Za-z0-9_|.\-\]\[]*$", inp.lower()): - return "I can't look up that name, its impossible to use!" - - if not db_ready: - db_init(db) - - last_seen = db.execute("select name, time, quote from seen_user where name" - " like ? and chan = ?", (inp, chan)).fetchone() - - if last_seen: - reltime = timesince.timesince(last_seen[1]) - if last_seen[0] != inp.lower(): # for glob matching - inp = last_seen[0] - if last_seen[2][0:1] == "\x01": - return '{} was last seen {} ago: * {} {}'.format(inp, reltime, inp, - last_seen[2][8:-1]) - else: - return '{} was last seen {} ago saying: {}'.format(inp, reltime, last_seen[2]) - else: - return "I've never seen {} talking in this channel.".format(inp)