From db1997d52922a5cab23a22c6109a29acbae60dec Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Fri, 17 Apr 2009 21:51:14 -0600 Subject: [PATCH] logging basically done, more cleaning --- bot.py | 14 ++++++-------- plugins/babel.py | 2 -- plugins/log.py | 38 ++++++++++++++++++++++++++++++++++++-- plugins/misc.py | 2 -- plugins/remember.py | 3 --- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/bot.py b/bot.py index 8cc155e..d72b8b8 100755 --- a/bot.py +++ b/bot.py @@ -96,13 +96,11 @@ class Input(object): self.host = host self.paraml = paraml self.msg = msg - if command == "PRIVMSG": - if paraml[0] != bot.nick: - self.chan = paraml[0] - else: - self.chan = nick - else: - self.chan = "" + self.chan = paraml[0] + if self.chan == bot.nick: + self.chan = nick + elif command =='JOIN': + self.chan = msg class FakeBot(object): @@ -148,7 +146,7 @@ while True: try: input = sieve(bot, input, func, args) except Exception, e: - print 'filter error:', e + print 'sieve error:', e input = None if input == None: break diff --git a/plugins/babel.py b/plugins/babel.py index 648fd84..d993187 100644 --- a/plugins/babel.py +++ b/plugins/babel.py @@ -45,7 +45,6 @@ def goog_trans(text, slang, tlang): json = urllib.urlopen(url).read() parsed = yaml.load(json) if not 200 <= parsed['responseStatus'] < 300: - print parsed raise IOError('error with the translation server: %d: %s' % ( parsed['responseStatus'], '')) return unescape(parsed['responseData']['translatedText']) @@ -56,7 +55,6 @@ def babel_gen(inp): inp = inp.encode('utf8') trans = goog_trans(inp, 'en', language).encode('utf8') inp = goog_trans(trans, language, 'en') - print language, trans, inp yield language, trans, inp diff --git a/plugins/log.py b/plugins/log.py index 271162f..7df957e 100644 --- a/plugins/log.py +++ b/plugins/log.py @@ -16,6 +16,15 @@ log_fds = {} # '%(net)s %(chan)s' : (filename, fd) timestamp_format = '%H:%M:%S' +formats = {'PRIVMSG': '<%(nick)s> %(msg)s', + 'PART': '-!- %(nick)s [%(user)s@%(host)s] has left %(chan)s', + 'JOIN': '-!- %(nick)s [%(user)s@%(host)s] has joined %(chan)s', + 'MODE': '-!- mode/%(chan)s [%(param_tail)s] by %(nick)s', + 'KICK': '-!- %(param1)s was kicked from %(chan)s by %(nick)s [%(msg)s]', + 'TOPIC': '-!- %(nick)s changed the topic of %(chan)s to: %(msg)s' + } + +ctcp_formats = {'ACTION': '* %(nick)s %(ctcpmsg)s'} def get_log_filename(dir, network, chan): return os.path.join(dir, 'log', gmtime('%Y'), network, @@ -26,6 +35,29 @@ def gmtime(format): return time.strftime(format, time.gmtime()) +def beautify(input): + format = formats.get(input.command, '%(raw)s') + args = vars(input) + leng = len(args['paraml']) + for n, p in enumerate(args['paraml']): + args['param' + str(n)] = p + args['param_' + str(abs(n - leng))] = p + + args['param_tail'] = ' '.join(args['paraml'][1:]) + + if input.command == 'PRIVMSG' and input.msg.count('\x01') >= 2: + #ctcp + ctcp = input.msg.split('\x01', 2)[1].split(' ', 1) + if len(ctcp) == 1: + ctcp += [''] + args['ctcpcmd'], args['ctcpmsg'] = ctcp + format = ctcp_formats.get(args['ctcpcmd'], + '%(nick)s [%(user)s@%(host)s] requested unknown CTCP ' + '%(ctcpcmd)s from %(chan)s: %(ctcpmsg)s') + + return format % args + + def get_log_fd(dir, network, chan): fn = get_log_filename(dir, network, chan) cache_key = '%s %s' % (network, chan) @@ -48,9 +80,11 @@ def get_log_fd(dir, network, chan): def log(bot, input): ".remember -- maps word to data in the memory" with lock: + timestamp = gmtime(timestamp_format) + fd = get_log_fd(bot.persist_dir, bot.network, 'raw') - fd.write(gmtime(timestamp_format) + ' ' + input.raw + '\n') + fd.write(timestamp + ' ' + input.raw + '\n') if input.chan: fd = get_log_fd(bot.persist_dir, bot.network, input.chan) - fd.write(gmtime(timestamp_format) + ' ' + input.raw + '\n') + fd.write(timestamp + ' ' + beautify(input) + '\n') diff --git a/plugins/misc.py b/plugins/misc.py index 64adf53..5b433ef 100644 --- a/plugins/misc.py +++ b/plugins/misc.py @@ -3,8 +3,6 @@ import hook @hook.event('KICK INVITE') def rejoin(bot, input): - print input.command, input.inp - if input.command == 'KICK': if input.paraml[1] == bot.bot.nick: if input.paraml[0] == bot.bot.channel: diff --git a/plugins/remember.py b/plugins/remember.py index ca4155f..b760dcf 100644 --- a/plugins/remember.py +++ b/plugins/remember.py @@ -68,9 +68,7 @@ def forget(bot, input): if not input.inp.strip(): return forget.__doc__ - print input.inp low = input.inp.strip().lower() - print repr(low) if low not in memory[filename]: return "I don't know about that." if not hasattr(input, 'chan'): @@ -88,6 +86,5 @@ def question(bot, input): memory.setdefault(filename, load_memory(filename)) word = input.inp.split()[0].lower() - print memory[filename] if word in memory[filename]: bot.say("%s" % memory[filename][word])