add fake IRC class to help tracking leaks

This commit is contained in:
Ryan Hitchman 2010-02-11 16:29:45 -07:00
parent 826a47837c
commit 9b47d394ad
4 changed files with 62 additions and 13 deletions

2
bot.py
View File

@ -32,7 +32,7 @@ try:
if name in bot.conns: if name in bot.conns:
print 'ERROR: more than one connection named "%s"' % name print 'ERROR: more than one connection named "%s"' % name
raise ValueError 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) port=conf.get('port', 6667), channels=conf['channels'], conf=conf)
except Exception, e: except Exception, e:
print 'ERROR: malformed config file', Exception, e print 'ERROR: malformed config file', Exception, e

View File

@ -4,6 +4,6 @@ import sqlite3
def get_db_connection(server, name='skybot.%s.db'): def get_db_connection(server, name='skybot.%s.db'):
"returns an sqlite3 connection to a persistent database" "returns an sqlite3 connection to a persistent database"
filename = os.path.join(bot.persist_dir, name % server) 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

View File

@ -72,7 +72,7 @@ irc_netmask_rem = re.compile(r':?([^!@]*)!?([^@]*)@?(.*)').match
irc_param_ref = re.compile(r'(?:^|(?<= ))(:.*|[^ ]+)').findall irc_param_ref = re.compile(r'(?:^|(?<= ))(:.*|[^ ]+)').findall
class irc(object): class IRC(object):
"handles the IRC protocol" "handles the IRC protocol"
#see the docs/ folder for more information on the protocol #see the docs/ folder for more information on the protocol
def __init__(self, server, nick, port=6667, channels=[], conf={}): def __init__(self, server, nick, port=6667, channels=[], conf={}):
@ -136,3 +136,47 @@ class irc(object):
def send(self, str): def send(self, str):
self.conn.oqueue.put(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

View File

@ -14,16 +14,21 @@ def sieve_suite(bot, input, func, args):
if input.nick.lower()[-3:] == 'bot' and args.get('ignorebots', True): if input.nick.lower()[-3:] == 'bot' and args.get('ignorebots', True):
return None 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 args.get('prefix', True):
if input.chan == input.nick: # private message, prefix not required if input.chan == input.nick: # private message, prefix not required
prefix = r'^(?:[.!]?|' prefix = r'^(?:[.!]?|'
else: else:
prefix = r'^(?:[.!]|' prefix = r'^(?:[.!]|'
hook = prefix + input.conn.nick + r'[:,]*\s)' + hook 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: if input.re is None:
return None return None