Add seen and tell Module

master
sfan5 2013-01-12 17:24:17 +01:00
parent dceddefbaa
commit 59c507a117
2 changed files with 196 additions and 0 deletions

48
seen.py Normal file
View File

@ -0,0 +1,48 @@
#!/usr/bin/env python
"""
seen.py - Phenny Seen Module
Copyright 2008, Sean B. Palmer, inamidst.com
Modified by Sfan5 2013
Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import time
from tools import deprecated
def seen(phenny, input):
""".seen <nick> - Reports when <nick> was last seen."""
nick = input.group(2)
if not nick:
return phenny.reply("Need a nickname to search for...")
nick = nick.lower()
print("[LOG]: %s queried Seen Result for %s" % (input.nick,nick))
if not hasattr(phenny, 'seen'):
return phenny.reply("?")
if phenny.seen.has_key(nick):
channel, t = phenny.seen[nick]
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)
seen.rule = (['seen'], r'(\S+)')
@deprecated
def f_note(self, origin, match, args):
def note(self, origin, match, args):
if not hasattr(self.bot, 'seen'):
self.bot.seen = {}
if origin.sender.startswith('#'):
self.seen[origin.nick.lower()] = (origin.sender, time.time())
try: note(self, origin, match, args)
except Exception, e: print e
f_note.rule = r'(.*)'
f_note.priority = 'low'
if __name__ == '__main__':
print __doc__.strip()

148
tell.py Normal file
View File

@ -0,0 +1,148 @@
#!/usr/bin/env python
"""
tell.py - Phenny Tell and Ask Module
Copyright 2008, Sean B. Palmer, inamidst.com
Modified by Sfan5 2013
Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import os, re, time, random
import web
maximum =
def loadReminders(fn):
result = {}
f = open(fn)
for line in f:
line = line.strip()
if line:
try: tellee, teller, verb, timenow, msg = line.split('\t', 4)
except ValueError: continue # @@ hmm
result.setdefault(tellee, []).append((teller, verb, timenow, msg))
f.close()
return result
def dumpReminders(fn, data):
f = open(fn, 'w')
for tellee in data.iterkeys():
for remindon in data[tellee]:
line = '\t'.join((tellee,) + remindon)
try: f.write(line + '\n')
except IOError: break
try: f.close()
except IOError: pass
return True
def setup(self):
fn = self.nick + '-' + self.config.host + '.tell.db'
self.tell_filename = os.path.join(os.path.expanduser('~/.phenny'), fn)
if not os.path.exists(self.tell_filename):
try: f = open(self.tell_filename, 'w')
except OSError: pass
else:
f.write('')
f.close()
self.reminders = loadReminders(self.tell_filename) # @@ tell
def f_remind(phenny, input):
teller = input.nick
# @@ Multiple comma-separated tellees? Cf. Terje, #swhack, 2006-04-15
verb, tellee, msg = input.groups()
verb = verb.encode('utf-8')
tellee = tellee.encode('utf-8')
msg = msg.encode('utf-8')
tellee_original = tellee.rstrip('.,:;')
tellee = tellee_original.lower()
if not os.path.exists(phenny.tell_filename):
return
if len(tellee) > 20:
return phenny.reply('That nickname is too long.')
timenow = time.strftime('%d %b %H:%MZ', time.gmtime())
if not tellee in (teller.lower(), phenny.nick, 'me'): # @@
# @@ <deltab> and year, if necessary
warn = False
if not phenny.reminders.has_key(tellee):
phenny.reminders[tellee] = [(teller, verb, timenow, msg)]
else:
# if len(phenny.reminders[tellee]) >= maximum:
# warn = True
phenny.reminders[tellee].append((teller, verb, timenow, msg))
# @@ Stephanie's augmentation
response = "I'll pass that on when %s is around." % tellee_original
# if warn: response += (" I'll have to use a pastebin, though, so " +
# "your message may get lost.")
rand = random.random()
if rand > 0.9999: response = "yeah, yeah"
elif rand > 0.999: response = "yeah, sure, whatever"
phenny.reply(response)
elif teller.lower() == tellee:
phenny.say('You can %s yourself that.' % verb)
else: phenny.say("Hey, I'm not as stupid as Monty you know!")
dumpReminders(phenny.tell_filename, phenny.reminders) # @@ tell
f_remind.rule = ('$nick', ['tell', 'ask'], r'(\S+) (.*)')
def getReminders(phenny, channel, key, tellee):
lines = []
template = "%s: %s <%s> %s %s %s"
today = time.strftime('%d %b', time.gmtime())
for (teller, verb, datetime, msg) in phenny.reminders[key]:
if datetime.startswith(today):
datetime = datetime[len(today)+1:]
lines.append(template % (tellee, datetime, teller, verb, tellee, msg))
try: del phenny.reminders[key]
except KeyError: phenny.msg(channel, 'Er...')
return lines
def message(phenny, input):
if not input.sender.startswith('#'): return
tellee = input.nick
channel = input.sender
if not os: return
if not os.path.exists(phenny.tell_filename):
return
reminders = []
remkeys = list(reversed(sorted(phenny.reminders.keys())))
for remkey in remkeys:
if not remkey.endswith('*') or remkey.endswith(':'):
if tellee.lower() == remkey:
phenny.sending.acquire()
reminders.extend(getReminders(phenny, channel, remkey, tellee))
phenny.sending.release()
elif tellee.lower().strip("0123456789_-[]`") == remkey.rstrip('*:'):
phenny.sending.acquire()
reminders.extend(getReminders(phenny, channel, remkey, tellee))
phenny.sending.release()
for line in reminders[:maximum]:
phenny.say(line)
if reminders[maximum:]:
phenny.say('Further messages sent privately')
for line in reminders[maximum:]:
phenny.msg(tellee, line)
if len(phenny.reminders.keys()) != remkeys:
phenny.sending.acquire()
dumpReminders(phenny.tell_filename, phenny.reminders) # @@ tell
phenny.sending.release()
message.rule = r'(.*)'
message.priority = 'low'
if __name__ == '__main__':
print __doc__.strip()