From 9b47d394ad13f169f9c165a355ab6d98bc187b0f Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Thu, 11 Feb 2010 16:29:45 -0700 Subject: [PATCH] add fake IRC class to help tracking leaks --- bot.py | 2 +- core/db.py | 4 ++-- core/irc.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- plugins/sieve.py | 21 +++++++++++++-------- 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/bot.py b/bot.py index 3654ca6..e19b51c 100755 --- a/bot.py +++ b/bot.py @@ -32,7 +32,7 @@ try: if name in bot.conns: print 'ERROR: more than one connection named "%s"' % name raise ValueError - bot.conns[name] = irc(conf['server'], conf['nick'], + bot.conns[name] = IRC(conf['server'], conf['nick'], port=conf.get('port', 6667), channels=conf['channels'], conf=conf) except Exception, e: print 'ERROR: malformed config file', Exception, e diff --git a/core/db.py b/core/db.py index ee04428..4230139 100644 --- a/core/db.py +++ b/core/db.py @@ -4,6 +4,6 @@ import sqlite3 def get_db_connection(server, name='skybot.%s.db'): "returns an sqlite3 connection to a persistent database" filename = os.path.join(bot.persist_dir, name % server) - return sqlite3.connect(filename) + return sqlite3.connect(filename, timeout=10) -bot.get_db_connection = get_db_connection +bot.get_db_connection = get_db_connection \ No newline at end of file diff --git a/core/irc.py b/core/irc.py index c8c050c..7b6e591 100644 --- a/core/irc.py +++ b/core/irc.py @@ -52,7 +52,7 @@ class crlf_tcp(object): self.socket.close() return continue - + while '\r\n' in self.ibuffer: line, self.ibuffer = self.ibuffer.split('\r\n', 1) self.iqueue.put(decode(line)) @@ -72,7 +72,7 @@ irc_netmask_rem = re.compile(r':?([^!@]*)!?([^@]*)@?(.*)').match irc_param_ref = re.compile(r'(?:^|(?<= ))(:.*|[^ ]+)').findall -class irc(object): +class IRC(object): "handles the IRC protocol" #see the docs/ folder for more information on the protocol def __init__(self, server, nick, port=6667, channels=[], conf={}): @@ -136,3 +136,47 @@ class irc(object): def send(self, str): self.conn.oqueue.put(str) + +class FakeIRC(IRC): + "handles the IRC protocol" + #see the docs/ folder for more information on the protocol + def __init__(self, server, nick, port=6667, channels=[], conf={}, fn=""): + self.channels = channels + self.conf = conf + self.server = server + self.port = port + self.nick = nick + + self.out = Queue.Queue() #responses from the server are placed here + # format: [rawline, prefix, command, params, + # nick, user, host, paramlist, msg] + + self.f = open(fn, 'rb') + + thread.start_new_thread(self.parse_loop, ()) + + def parse_loop(self): + while True: + msg = decode(self.f.readline()[9:]) + + if msg == '': + print "!!!!DONE READING FILE!!!!" + return + + if msg.startswith(":"): #has a prefix + prefix, command, params = irc_prefix_rem(msg).groups() + else: + prefix, command, params = irc_noprefix_rem(msg).groups() + nick, user, host = irc_netmask_rem(prefix).groups() + paramlist = irc_param_ref(params) + lastparam = "" + if paramlist and paramlist[-1].startswith(':'): + lastparam = paramlist[-1][1:] + self.out.put([msg, prefix, command, params, nick, user, host, + paramlist, lastparam]) + while self.out.qsize() > 5: time.sleep(.1) + if command == "PING": + self.cmd("PONG", [params]) + + def cmd(self, command, params=None): + pass \ No newline at end of file diff --git a/plugins/sieve.py b/plugins/sieve.py index fc97d10..e6dc4e5 100644 --- a/plugins/sieve.py +++ b/plugins/sieve.py @@ -14,16 +14,21 @@ def sieve_suite(bot, input, func, args): if input.nick.lower()[-3:] == 'bot' and args.get('ignorebots', True): return None - hook = args.get('hook', r'(.*)') + regex = args.get('re') + if not regex: + hook = args.get('hook', r'(.*)') - if args.get('prefix', True): - if input.chan == input.nick: # private message, prefix not required - prefix = r'^(?:[.!]?|' - else: - prefix = r'^(?:[.!]|' - hook = prefix + input.conn.nick + r'[:,]*\s)' + hook + if args.get('prefix', True): + if input.chan == input.nick: # private message, prefix not required + prefix = r'^(?:[.!]?|' + else: + prefix = r'^(?:[.!]|' + hook = prefix + input.conn.nick + r'[:,]*\s)' + hook - input.re = re.match(hook, input.msg, flags=re.I) + regex = re.compile(hook, flags=re.I) + args['re'] = regex + + input.re = regex.match(input.msg) if input.re is None: return None