chiark / gitweb /
new flirt from jtn
[irc.git] / acrobat.py
1 #!/usr/bin/env python
2 #
3 # Bot logic:
4 # Andrew Walkingshaw <andrew@lexical.org.uk>
5 #
6 # IRC framework:
7 # Joel Rosdahl <joel@rosdahl.net>
8 #
9 # Contributors:
10 # Peter Corbett <ptc24@cam.ac.uk>
11 # Matthew Vernon <matthew@debian.org>
12 #
13 # Stephen Early <steve@greenend.org.uk> mostly deleted stuff
14
15 # This file is part of Acrobat.
16 #
17 # Acrobat is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published
19 # by the Free Software Foundation; either version 2 of the License,
20 # or (at your option) any later version.
21 #
22 # Acrobat is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 #
27 # You should have received a copy of the GNU General Public License
28 # along with Acrobat; if not, write to the Free Software
29 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
30 # USA.
31
32 """
33 Acrobat - an extensible, minmalist irc bot.
34 """
35
36 import string, sys
37 from ircbot import SingleServerIRCBot
38 from irclib import nm_to_n, irc_lower
39
40 class Acrobat(SingleServerIRCBot):
41     def __init__(self,config):
42         server=config.server
43         port=config.port
44         nickname=config.nickname
45         SingleServerIRCBot.__init__(self,
46                                     [(server, port)], nickname, nickname)
47         self.channel = config.channel
48         self.owner = config.owner
49         self.revision = "$Revision: 1.1 $" # global version number
50         self.config = config
51         
52     ## EVENT HANDLERS
53             
54     def on_welcome(self, conn, evt):
55         conn.join(self.channel)
56
57     def on_privmsg(self, conn, evt):
58         self.do_command(nm_to_n(evt.source()), evt.arguments()[0])
59         
60     def on_pubmsg(self, conn, evt):
61         payload = evt.arguments()[0]
62         nc = string.split(payload, " ", 1)
63         if len(nc) > 1 and (irc_lower(nc[0]).startswith(
64             irc_lower(self.connection.get_nickname()))):
65             self.do_command(nm_to_n(evt.source()), nc[1].strip(), public = 1)
66         elif len(payload)>1:
67             self.do_command(nm_to_n(evt.source()), payload.strip(), public = 1)
68     # General query handler
69     def do_command(self, nick, cmd, public=0):
70         self.config.command(self,cmd,nick,self.connection,public)
71
72     # Convenience function - reply to a public message publicly, or
73     # a private message privately
74     def automsg(self,public,nick,msg):
75         if public:
76             self.connection.privmsg(self.channel,msg)
77         else:
78             self.connection.notice(nick, msg)
79
80 def main():
81     if len(sys.argv) < 2:
82         print "acrobat: provide configuration module name on command line"
83         sys.exit(1)
84     try:
85         c=__import__(sys.argv[1])
86     except ImportError:
87         print "acrobat: configuration module "+sys.argv[1]+".py not found"
88         sys.exit(1)
89     # Override configuration items from the rest of the command line
90     for opt in sys.argv[2:]:
91         (key,value)=opt.split("=")
92         c.__dict__[key] = value
93     bot = Acrobat(c)
94     bot.start()
95
96 if __name__ == "__main__":
97     main()