make log append work, make bot respond to PMs, other minor cleanups

This commit is contained in:
Ryan Hitchman 2009-04-17 18:13:54 -06:00
parent e46e42d4b9
commit 94d9fd22f0
8 changed files with 11 additions and 31 deletions

5
bot.py
View File

@ -92,7 +92,10 @@ class Input(object):
self.paraml = paraml self.paraml = paraml
self.msg = msg self.msg = msg
if command == "PRIVMSG": if command == "PRIVMSG":
if paraml[0] != bot.nick:
self.chan = paraml[0] self.chan = paraml[0]
else:
self.chan = nick
else: else:
self.chan = "" self.chan = ""
@ -110,7 +113,7 @@ class FakeBot(object):
self.chan = input.chan self.chan = input.chan
def say(self, msg): def say(self, msg):
self.bot.irc.msg(self.input.paraml[0], msg) self.bot.irc.msg(self.chan, msg)
def reply(self, msg): def reply(self, msg):
self.say(self.input.nick + ': ' + msg) self.say(self.input.nick + ': ' + msg)

View File

@ -16,10 +16,7 @@ def sieve_suite(bot, input, func, args):
args.setdefault('prefix', True) args.setdefault('prefix', True)
if args.get('prefix', True): if args.get('prefix', True):
hook = r'^(?:[.!]|' + bot.nick +r'[:,]*\s*)' + hook hook = (r'^(?:[.!]|' if input.chan != input.nick else r'^(?:[.!]?|') + bot.nick +r'[:,]*\s*)' + hook
if input.command == 'INVITE':
print func, hook
input.re = re.match(hook, input.msg, flags=re.I) input.re = re.match(hook, input.msg, flags=re.I)
if input.re is None: if input.re is None:

2
irc.py
View File

@ -8,7 +8,7 @@ import Queue
queue = Queue.Queue queue = Queue.Queue
def decode(txt, codecs=['utf-8', 'iso-8859-1', 'shift_jis', 'cp1252']): def decode(txt):
for codec in ('utf-8', 'iso-8859-1', 'shift_jis', 'cp1252'): for codec in ('utf-8', 'iso-8859-1', 'shift_jis', 'cp1252'):
try: try:
return txt.decode(codec) return txt.decode(codec)

3
plugins/dice.py Executable file → Normal file
View File

@ -1,6 +1,5 @@
#!/usr/bin/env python
""" """
dice.py written by Scaevolus 2008, updated 2009 dice.py: written by Scaevolus 2008, updated 2009
simulates dicerolls simulates dicerolls
""" """
import re import re

View File

@ -18,26 +18,11 @@ timestamp_format = '%H:%M:%S'
def get_log_filename(dir, network, chan): def get_log_filename(dir, network, chan):
return os.path.join(dir, 'log', gmtime('%Y'), network, return os.path.join(dir, 'log', gmtime('%Y'), network,
gmtime('%%s.%m-%d.log') % chan) gmtime('%%s.%m-%d.log') % chan).lower()
def gmtime(format): def gmtime(format):
return time.strftime(format, time.gmtime()) return time.strftime(format, time.gmtime())
def load_memory(filename, mtimes={}):
if not os.path.exists(filename):
return {}
mtime = os.stat(filename).st_mtime
if mtimes.get(filename, 0) != mtime:
mtimes[filename] = mtime
return dict((x.split(None, 1)[0].lower(), x.strip()) for x in
codecs.open(filename, 'r', 'utf-8'))
def save_memory(filename, memory):
out = codecs.open(filename, 'w', 'utf-8')
out.write('\n'.join(sorted(memory.itervalues())))
out.flush()
out.close()
def get_log_fd(dir, network, chan): def get_log_fd(dir, network, chan):
fn = get_log_filename(dir, network, chan) fn = get_log_filename(dir, network, chan)
cache_key = '%s %s' % (network, chan) cache_key = '%s %s' % (network, chan)
@ -50,7 +35,7 @@ def get_log_fd(dir, network, chan):
dir = os.path.split(fn)[0] dir = os.path.split(fn)[0]
if not os.path.exists(dir): if not os.path.exists(dir):
os.makedirs(dir) os.makedirs(dir)
fd = codecs.open(fn, 'wab', 'utf-8') fd = codecs.open(fn, 'a', 'utf-8')
log_fds[cache_key] = (fn, fd) log_fds[cache_key] = (fn, fd)
return fd return fd
@ -59,10 +44,9 @@ def get_log_fd(dir, network, chan):
def log(bot, input): def log(bot, input):
".remember <word> <data> -- maps word to data in the memory" ".remember <word> <data> -- maps word to data in the memory"
with lock: with lock:
fd = get_log_fd(bot.persist_dir, bot.network, 'RAW') fd = get_log_fd(bot.persist_dir, bot.network, 'raw')
fd.write(gmtime(timestamp_format) + ' ' + input.raw + '\n') fd.write(gmtime(timestamp_format) + ' ' + input.raw + '\n')
if input.chan: if input.chan:
fd = get_log_fd(bot.persist_dir, bot.network, input.chan) fd = get_log_fd(bot.persist_dir, bot.network, input.chan)
fd.write(gmtime(timestamp_format) + ' ' + input.raw + '\n') fd.write(gmtime(timestamp_format) + ' ' + input.raw + '\n')

View File

@ -1,4 +1,3 @@
#!/usr/bin/env python
""" """
remember.py: written by Scaevolus 2009 remember.py: written by Scaevolus 2009
""" """

1
plugins/weather.py Executable file → Normal file
View File

@ -1,4 +1,3 @@
#!/usr/bin/python
"weather, thanks to google" "weather, thanks to google"
from __future__ import with_statement from __future__ import with_statement

View File

@ -1,4 +1,3 @@
#!/usr/bin/python
'''Searches wikipedia and returns first sentence of article '''Searches wikipedia and returns first sentence of article
Scaevolus 2009''' Scaevolus 2009'''