-#!/usr/bin/env python2
+#!/usr/bin/env python
#
# Bot logic:
# Andrew Walkingshaw <andrew@lexical.org.uk>
# Contributors:
# Peter Corbett <ptc24@cam.ac.uk>
# Matthew Vernon <matthew@debian.org>
+#
+# Stephen Early <steve@greenend.org.uk> mostly deleted stuff
#
# This file is part of Acrobat.
#
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):
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)
+ # Override configuration items from the rest of the command line
+ for opt in sys.argv[2:]:
+ (key,value)=opt.split("=")
+ c.__dict__[key] = value
+ bot = Acrobat(c)
bot.start()
-# if len(sys.argv) != 5: # insufficient arguments
-# print "Usage: acrobat <server[:port]> <channel> <nickname> 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()