From f331cdc8039a18f16cb4b3b58bee45729ade9d92 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Thu, 4 Oct 2007 15:15:41 -0600 Subject: [PATCH] Fix locking + arsenic updates --- arsenic.py | 49 ++++++++++++++++++++++++++++++++++++++----------- firebot.py | 1 - infobot.py | 54 +++++++++++++++++++++++++++++++----------------------- irc.py | 23 +++++++++++++---------- seedyb.py | 8 ++++---- 5 files changed, 86 insertions(+), 49 deletions(-) diff --git a/arsenic.py b/arsenic.py index 2ecb0d6..55ef026 100755 --- a/arsenic.py +++ b/arsenic.py @@ -11,19 +11,37 @@ import os import time import socket +def esc(arg): + return "'" + arg.replace("'", r"'\''") + "'" + +def lesc(args): + return [esc(arg) for arg in args] + class Arsenic(firebot.FireBot, ProcBot): - debug = True + debug = False bindings = [] ping_interval = 120 + chatty = False # #x can't play nice def __init__(self, *args, **kwargs): firebot.FireBot.__init__(self, *args, **kwargs) self.seen = {} self.lusers = {} - self.heartbeat_interval=3 + self.heartbeat_interval=0.01 self.lag = 0 self.whinecount = 0 + def runcmd(self, sender, forum, addl, match): + command = match.group('command') + args = lesc(match.group('args').split(' ')) + argstr = ' '.join(args) + Runner('%s %s' % (command, argstr), + lambda l,r: self.proc_cb('%s: ' % command, sender, forum, l, r)) + bindings.append((re.compile(r"^(?Pwhois) +(?P.*)$"), + runcmd)) + bindings.append((re.compile(r"^(?Phost) +(?P.*)$"), + runcmd)) + def rp(self, sender, forum, addl, match): command = 'rp' argstr = match.group('args') @@ -52,7 +70,6 @@ class Arsenic(firebot.FireBot, ProcBot): what = match.group('what') when = time.time() key = '\013notes:%s' % whom - print key note = (when, sender.name(), what) try: n = self.db[key] @@ -78,17 +95,17 @@ class Arsenic(firebot.FireBot, ProcBot): # get back into invite-only channels after a disconnect. who = luser.name() self.lusers[channel.name()][who] = luser - for chan in self.lusers.keys(): + for chan, l in self.lusers.iteritems(): if chan == channel.name(): continue - t = self.lusers[chan].get(who) + t = l.get(who) if t and t.host == luser.host: - self.write('INVITE %s %s' % (who, chan)) + self.write(['INVITE', who, chan]) def cmd_join(self, sender, forum, addl): if sender.name() == self.nick: # If it was me, get a channel listing and beg for ops - self.write('WHO %s' % (forum.name())) + self.write(['WHO', forum.name()]) forum.notice('If you op me, I will op everyone who joins this channel.') self.lusers[forum.name()] = {} else: @@ -108,10 +125,10 @@ class Arsenic(firebot.FireBot, ProcBot): def cmd_pong(self, sender, forum, addl): now = time.time() - print now self.lag = now - float(addl[0]) def cmd_482(self, sender, forum, addl): + forum = self.recipient(addl[0]) self.whinecount += 1 if (self.whinecount == 2 or self.whinecount == 4 or @@ -123,6 +140,15 @@ class Arsenic(firebot.FireBot, ProcBot): if __name__ == '__main__': + import daemon + + debug = False + + if not debug: + # Become a daemon + log = file('arsenic.log', 'a') + daemon.daemon('arsenic.pid', log, log) + # Short URL server us = shorturl.start(('', 0)) firebot.URLSERVER = (socket.gethostbyaddr(socket.gethostname())[0], @@ -131,10 +157,11 @@ if __name__ == '__main__': NICK = ['arsenic'] INFO = "I'm a little printf, short and stdout" - l1 = Arsenic(('greywolf.lanl.gov', 6697), + l1 = Arsenic(('irc.lanl.gov', 6667), NICK, INFO, ["#x"], - ssl=True) + ssl=False) + l1.debug = debug - irc.run_forever() + irc.run_forever(0.01) diff --git a/firebot.py b/firebot.py index d2344f5..d0b2c26 100755 --- a/firebot.py +++ b/firebot.py @@ -47,7 +47,6 @@ class FireBot(infobot.InfoBot, procbot.ProcBot): infobot.InfoBot.__init__(self, host, nicks, gecos, channels, **kwargs) self.ssl = ssl - self.nosy = True self.seen = {} def handle_connect(self): diff --git a/infobot.py b/infobot.py index b6ffcdc..d072418 100644 --- a/infobot.py +++ b/infobot.py @@ -12,6 +12,7 @@ class InfoBot(BindingsBot): msg_cat = {} msg_cat.update(BindingsBot.msg_cat) bindings = [] + chatty = True def __init__(self, host, nicks, gecos, channels, dbname='info.cdb'): BindingsBot.__init__(self, host, nicks, gecos, channels) @@ -186,6 +187,9 @@ class InfoBot(BindingsBot): # Look something up in the DB def lookup(self, sender, forum, addl, match): + if not self.chatty: + if not match.group('me'): + return True key = match.group('key') # Try looking it up verbatim @@ -218,34 +222,38 @@ class InfoBot(BindingsBot): resp = False old = self.getall(key) okay = self.gettext('okay', sender=sender.name()) - if old: - if val in old: - if me: - resp = self.gettext('same', key=key, val=val, old=old, - sender=sender.name()) + try: + if old: + if val in old: + if me: + resp = self.gettext('same', key=key, val=val, old=old, + sender=sender.name()) + else: + # Ignore duplicates + resp = self.gettext('same', key=key, val=val, old=old, + sender=sender.name()) + pass + elif me: + if also: + self.set(key, old + [val]) + resp = okay + elif no: + self.set(key, [val]) + resp = okay + else: + if len(old) == 1: + old = old[0] + resp = self.gettext('but', key=key, val=val, old=old, + sender=sender.name()) else: - # Ignore duplicates - resp = self.gettext('same', key=key, val=val, old=old, - sender=sender.name()) - pass - elif me: - if also: self.set(key, old + [val]) resp = okay - elif no: - self.set(key, [val]) - resp = okay - else: - if len(old) == 1: - old = old[0] - resp = self.gettext('but', key=key, val=val, old=old, - sender=sender.name()) else: - self.set(key, old + [val]) + self.set(key, (val,)) resp = okay - else: - self.set(key, (val,)) - resp = okay + except seedyb.Locked: + resp = self.gettext('locked', key=key, + sender=sender.name()) if resp: if me: diff --git a/irc.py b/irc.py index 67d3887..e0eac99 100644 --- a/irc.py +++ b/irc.py @@ -292,6 +292,9 @@ class SmartIRCHandler(IRCHandler): """ + def recipient(self, name): + return recipient(self, name) + def err(self, exception): if self.debug: traceback.print_exception(*exception) @@ -349,14 +352,14 @@ class SmartIRCHandler(IRCHandler): # PRIVMSG ['neale!~user@127.0.0.1', 'PRIVMSG', 'firebot'] firebot, foo try: if args[2][0] in '#&': - forum = recipient(self, args[2]) + forum = self.recipient(args[2]) else: forum = sender addl = (text,) except IndexError: addl = (text, args[1]) elif op in ("CPRIVMSG", "CNOTICE"): - forum = recipient(self, args[2]) + forum = self.recipient(args[2]) splits = text.split(" ") if splits[0] == "DCC": op = "DC" + op @@ -364,16 +367,16 @@ class SmartIRCHandler(IRCHandler): else: addl = (splits[0],) + tuple(splits[1:]) elif op in ("KICK",): - forum = recipient(self, args[2]) - addl = (recipient(self, args[3]), text) + forum = self.recipient(args[2]) + addl = (self.recipient(args[3]), text) elif op in ("MODE",): - forum = recipient(self, args[2]) + forum = self.recipient(args[2]) addl = args[3:] elif op in ("JOIN", "PART"): try: - forum = recipient(self, args[2]) + forum = self.recipient(args[2]) except IndexError: - forum = recipient(self, text) + forum = self.recipient(text) elif op in ("QUIT",): addl = (text,) elif op in ("PING", "PONG"): @@ -389,12 +392,12 @@ class SmartIRCHandler(IRCHandler): # Apparently there are two different standards for this # command. if text: - sender = recipient(self, text) + sender = self.recipient(text) else: - sender = recipient(self, args[2]) + sender = self.recipient(args[2]) addl = (unpack_nuhost(args)[0],) elif op in ("INVITE",): - forum = recipient(self, text) + forum = self.recipient(text) else: try: int(op) diff --git a/seedyb.py b/seedyb.py index b9fa9fd..a92af62 100644 --- a/seedyb.py +++ b/seedyb.py @@ -131,16 +131,16 @@ class SeedyB: ## def lock(self, key): - self.set(key, [''], special='lock') + self.set(key, ['locked'], special='lock') def unlock(self, key): self.set(key, [], special='lock') def is_locked(self, key): l = self.get(key, special='lock') - if l: - return True - return False + if l is None: + return False + return True open = SeedyB