add fake IRC class to help tracking leaks
This commit is contained in:
parent
826a47837c
commit
9b47d394ad
2
bot.py
2
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
|
||||
|
|
|
@ -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
|
46
core/irc.py
46
core/irc.py
|
@ -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
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue