X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~matthewv/git?p=irc.git;a=blobdiff_plain;f=acrobat.py;h=6bed11a18dcc70e92640353b39c77c269d748d4d;hp=0305f943c40391c2f5dc36798f8c3f65ee380736;hb=81a023d3f85a8c2f89c2d8a5002819c3846dd880;hpb=295d92cb0299714aa2694f4e68992cb5ba53ec91 diff --git a/acrobat.py b/acrobat.py index 0305f94..6bed11a 100755 --- a/acrobat.py +++ b/acrobat.py @@ -9,6 +9,8 @@ # Contributors: # Peter Corbett # Matthew Vernon +# +# Stephen Early mostly deleted stuff # # This file is part of Acrobat. # @@ -31,39 +33,22 @@ Acrobat - an extensible, minmalist irc bot. """ -import string, urllib, sys, cPickle, os, random +import string, sys from ircbot import SingleServerIRCBot from irclib import nm_to_n, irc_lower -import config - -#splitting out the configuration to a separate (source, but this is incidental- -#it's just the nearest free parser) file. - -import config - -class Karma: - def __init__(self): - self.dict = {} class Acrobat(SingleServerIRCBot): - def __init__(self, channel, nickname, server, owner, port=6667): + def __init__(self,config): + server=config.server + port=config.port + nickname=config.nickname SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) - self.channel = channel - self.owner = owner - self.revision = "$Revision$" # global version number - self.trouts = config.trouts - self.karmafilename = config.karmafilename + self.channel = config.channel + self.owner = config.owner + self.revision = "$Revision: 1.1 $" # global version number self.config = config - # load the karma db - try: - f = open(self.karmafilename, "r") - self.karma = cPickle.load(f) - f.close() - except IOError: - self.karma = Karma() - ## EVENT HANDLERS def on_welcome(self, conn, evt): @@ -77,80 +62,32 @@ class Acrobat(SingleServerIRCBot): nc = string.split(payload, " ", 1) if len(nc) > 1 and (irc_lower(nc[0]).startswith( irc_lower(self.connection.get_nickname()))): - self.do_command(self.channel, nc[1].strip(), public = 1) - elif payload[0] in config.triggers and len(payload)>1: - self.do_command(self.channel, payload[1:].strip(), public=1) - elif payload.find("++") != -1 or payload.find("--") != -1: - self.do_command(self.channel, payload.strip(), public=1) + self.do_command(nm_to_n(evt.source()), nc[1].strip(), public = 1) + elif len(payload)>1: + self.do_command(nm_to_n(evt.source()), payload.strip(), public = 1) # General query handler def do_command(self, nick, cmd, public=0): - conn = self.connection - command = cmd.split()[0] - sys.stderr.write(command) - args = (self, cmd, nick, conn, public) + self.config.command(self,cmd,nick,self.connection,public) - # regrettably, these (and anything else with special triggers) - # must be special-cased, which is aesthetically unsatisfying. - - # karma: up - if command.endswith("++"): - self.karmaup(cmd) - # karma: down - if command.endswith("--"): - self.karmadown(cmd) - - # and in the general case (this is slightly magical) - if command.lower() in config.commands.keys(): - config.commands[command](*args) - # else do nothing. - - # What this _means_ is: you write a - # function(bot, cmd, nick, conn, public), where bot is the bot class - # (ie self here), and drop it in commands.py; then add a trigger command - # to config.py for it, in the dictionary "commands", and it will - # just start working on bot restart or config reload. - - # This is, IMO, quite nifty. :) - - # And now the karma commands, as these pretty much have to be here :( - # increment karma - def karmaup(self, cmd): - if self.karma.dict.has_key(cmd.split()[0][:-2]): - self.karma.dict[cmd.split()[0][:-2]] += 1 - else: - self.karma.dict[cmd.split()[0][:-2]] = 1 - #decrement karma - def karmadown(self, cmd): - if self.karma.dict.has_key(cmd.split()[0][:-2]): - self.karma.dict[cmd.split()[0][:-2]] -= 1 - else: - self.karma.dict[cmd.split()[0][:-2]] = -1 - + # Convenience function - reply to a public message publicly, or + # a private message privately + def automsg(self,public,nick,msg): + if public: + self.connection.privmsg(self.channel,msg) + else: + self.connection.notice(nick, msg) def main(): - # initialize the bot - bot = Acrobat(config.channel, config.nickname, config.server, - config.owner, config.port) - sys.stderr.write("Trying to connect...\n") - # and the event loop + if len(sys.argv)!=2: + print "acrobat: provide configuration module name on command line" + sys.exit(1) + try: + c=__import__(sys.argv[1]) + except ImportError: + print "acrobat: configuration module "+sys.argv[1]+".py not found" + sys.exit(1) + bot = Acrobat(c) bot.start() -# if len(sys.argv) != 5: # insufficient arguments -# print "Usage: acrobat owner" -# sys.exit(1) -# sv_port = string.split(sys.argv[1], ":", 1) # tuple; (server, port) -# server = sv_port[0] -# if len(sv_port) == 2: -# try: -# port = int(sv_port[1]) -# except ValueError: -# print "Error: Erroneous port." -# sys.exit(1) -# else: -# port = 6667 # default irc port -# channel = sys.argv[2] -# nickname = sys.argv[3] -# owner = sys.argv[4] - if __name__ == "__main__": main()