2013-01-12 17:24:17 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
seen.py - Phenny Seen Module
|
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com
|
2013-10-22 20:35:33 +02:00
|
|
|
Modified by sfan5 2013
|
2013-01-12 17:24:17 +01:00
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
|
|
|
import time
|
2014-07-20 16:13:59 +02:00
|
|
|
from threading import Thread, Lock
|
2013-10-22 20:35:33 +02:00
|
|
|
import sqlite3
|
|
|
|
|
2013-10-22 22:24:00 +02:00
|
|
|
updates = list()
|
2014-07-20 16:13:59 +02:00
|
|
|
update_l = Lock()
|
|
|
|
dblock = Lock()
|
2013-10-22 22:24:00 +02:00
|
|
|
|
2013-10-22 20:35:33 +02:00
|
|
|
def opendb():
|
|
|
|
db = sqlite3.connect("seen.sqlite")
|
|
|
|
return db
|
2013-01-12 17:24:17 +01:00
|
|
|
|
2013-10-22 22:24:00 +02:00
|
|
|
def updatethread():
|
|
|
|
global updates, update_l
|
|
|
|
db = opendb()
|
|
|
|
c = db.cursor()
|
|
|
|
while True:
|
|
|
|
if len(updates) > 0:
|
|
|
|
update_l.acquire()
|
|
|
|
up = updates
|
|
|
|
updates = list()
|
|
|
|
update_l.release()
|
2013-11-14 19:22:09 +01:00
|
|
|
dblock.acquire()
|
2013-10-22 22:24:00 +02:00
|
|
|
for u in up:
|
|
|
|
c.execute("SELECT * FROM seen WHERE nick = ?", (u[2],))
|
|
|
|
if c.fetchone() != None:
|
|
|
|
d = (u[0], u[1], u[2])
|
|
|
|
c.execute('UPDATE seen SET channel = ?, time = ? WHERE nick = ?', d)
|
|
|
|
else:
|
|
|
|
d = (u[2], u[0], u[1])
|
|
|
|
c.execute('INSERT INTO seen VALUES (?,?,?)', d)
|
|
|
|
db.commit()
|
2013-11-14 19:22:09 +01:00
|
|
|
dblock.release()
|
2013-10-22 22:24:00 +02:00
|
|
|
else:
|
|
|
|
time.sleep(5)
|
|
|
|
|
2014-06-28 15:34:53 +02:00
|
|
|
def pushupdate(sender, nick):
|
2014-06-28 16:30:13 +02:00
|
|
|
ts = int(time.mktime(time.gmtime()))
|
2014-06-28 15:34:53 +02:00
|
|
|
nick = nick.lower()
|
2013-10-22 22:24:00 +02:00
|
|
|
update_l.acquire()
|
2014-06-28 16:23:11 +02:00
|
|
|
updates.append((sender, ts, nick))
|
2013-10-22 22:24:00 +02:00
|
|
|
update_l.release()
|
|
|
|
|
2014-07-19 20:13:28 +02:00
|
|
|
def api_seen(nick):
|
|
|
|
dblock.acquire()
|
|
|
|
db = opendb()
|
|
|
|
c = db.cursor()
|
|
|
|
c.execute("SELECT channel, time FROM seen WHERE nick = ?", (nick,))
|
|
|
|
r = c.fetchone()
|
|
|
|
c.close()
|
|
|
|
db.close()
|
|
|
|
dblock.release()
|
|
|
|
return r
|
|
|
|
|
|
|
|
class SomeObject(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
seen_api = SomeObject()
|
|
|
|
seen_api.seen = api_seen
|
|
|
|
|
|
|
|
_export = {
|
|
|
|
'seen': seen_api,
|
|
|
|
}
|
|
|
|
|
2014-06-22 12:10:56 +02:00
|
|
|
def seen(phenny, input):
|
2014-06-22 12:32:15 +02:00
|
|
|
"""seen <nick> - Reports when <nick> was last seen."""
|
2013-10-22 20:35:33 +02:00
|
|
|
nick = input.group(2)
|
|
|
|
if not nick:
|
|
|
|
return phenny.reply("Need a nickname to search for...")
|
|
|
|
nick = nick.lower()
|
|
|
|
|
2014-07-19 20:13:28 +02:00
|
|
|
log.log("event", "%s queried Seen database for '%s'" % (log.fmt_user(input), nick), phenny)
|
2013-10-22 20:35:33 +02:00
|
|
|
|
2014-07-19 20:13:28 +02:00
|
|
|
r = api_seen(nick)
|
2013-11-14 19:16:37 +01:00
|
|
|
|
2013-10-22 20:35:33 +02:00
|
|
|
if r:
|
|
|
|
channel, t = r[0], r[1]
|
|
|
|
t = time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime(t))
|
|
|
|
|
|
|
|
msg = "%s was last seen at %s on %s" % (nick, t, channel)
|
|
|
|
phenny.reply(msg)
|
|
|
|
else:
|
|
|
|
phenny.reply("Sorry, I haven't seen %s around." % nick)
|
|
|
|
|
2013-01-12 17:24:17 +01:00
|
|
|
seen.rule = (['seen'], r'(\S+)')
|
|
|
|
|
2013-10-22 20:35:33 +02:00
|
|
|
def note(phenny, input):
|
|
|
|
if input.sender.startswith('#'):
|
2014-06-28 15:34:53 +02:00
|
|
|
pushupdate(input.sender, input.nick)
|
2013-10-22 20:35:33 +02:00
|
|
|
|
|
|
|
note.rule = r'.*'
|
|
|
|
note.priority = 'low'
|
2014-06-22 12:10:56 +02:00
|
|
|
note.thread = False
|
2014-06-22 12:46:49 +02:00
|
|
|
note.nohook = True
|
2013-01-12 17:24:17 +01:00
|
|
|
|
2013-10-22 22:48:06 +02:00
|
|
|
def note_join(phenny, input):
|
|
|
|
if input.sender.startswith('#'):
|
2014-06-28 15:34:53 +02:00
|
|
|
pushupdate(input.sender, input.nick)
|
2013-10-22 22:48:06 +02:00
|
|
|
|
|
|
|
note_join.rule = r'.*'
|
|
|
|
note_join.event = 'JOIN'
|
|
|
|
note_join.priority = 'low'
|
2014-06-22 12:10:56 +02:00
|
|
|
note_join.thread = False
|
2014-06-22 12:46:49 +02:00
|
|
|
note_join.nohook = True
|
2013-10-22 22:48:06 +02:00
|
|
|
|
|
|
|
def note_part(phenny, input):
|
|
|
|
if input.sender.startswith('#'):
|
2014-06-28 15:34:53 +02:00
|
|
|
pushupdate(input.sender, input.nick)
|
2013-10-22 22:48:06 +02:00
|
|
|
|
|
|
|
note_part.rule = r'.*'
|
|
|
|
note_part.event = 'PART'
|
|
|
|
note_part.priority = 'low'
|
2014-06-22 12:10:56 +02:00
|
|
|
note_part.thread = False
|
2014-06-22 12:46:49 +02:00
|
|
|
note_part.nohook = True
|
2013-10-22 22:48:06 +02:00
|
|
|
|
2013-10-22 22:24:00 +02:00
|
|
|
db = sqlite3.connect("seen.sqlite")
|
|
|
|
c = db.cursor()
|
|
|
|
c.execute('''CREATE TABLE IF NOT EXISTS seen (nick text, channel text, time int)''')
|
|
|
|
c.close()
|
|
|
|
db.close()
|
|
|
|
|
2014-07-20 16:13:59 +02:00
|
|
|
t = Thread(target=updatethread, name="seen.py database thread")
|
|
|
|
t.start()
|
2013-10-22 22:24:00 +02:00
|
|
|
|
2014-06-22 12:10:56 +02:00
|
|
|
if __name__ == '__main__':
|
2014-07-20 16:13:59 +02:00
|
|
|
print(__doc__.strip())
|