chiark / gitweb /
FishPond: Use in all other bots
[irc.git] / config.py
1 # This file is part of Acrobat.
2 #
3 # Acrobat is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published
5 # by the Free Software Foundation; either version 2 of the License,
6 # or (at your option) any later version.
7 #
8 # Acrobat is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Acrobat; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
16 # USA.
17
18 # Andrew Walkingshaw <andrew@lexical.org.uk>
19 # Peter Corbett <ptc24@cam.ac.uk>
20 # Matthew Vernon <matthew@debian.org>
21 # Stephen Early <steve@greenend.org.uk>
22
23 # Acrobat configuration file
24
25 # The following definitions are required to be present in this module:
26 server = "rapun"
27 port = 6667
28 nickname = "Testbot"
29 channel = "#test"
30 owner = "Emperor"
31 # Also a function called "command"; see later.
32
33 # Everything else in this file is configuration-specific.
34
35 # Most command implementations are stored in a separate module.
36 import commands as c
37
38 # This fishpond is shared between trouts and flirts.  It doesn't have to be;
39 # you can define as many ponds as you like.
40 class fish (c.FishPond):
41         cur_fish=5
42         max_fish=5
43         nofish_time=60
44         fish_time_inc=60
45         fish_inc=2
46         Boring_Git='Nobody'
47
48 # load a file full of flirts or trouts
49 def __load(filename):
50     try:
51         f = open(filename, "r")
52         r = [l.strip() for l in f.readlines() if l.find("%s") != -1]
53         f.close()
54     except IOError:
55         r = [ "doesn't know what to do about %s." ]
56     return r
57
58 # (troutlist,selftroutmsg,DoSmsg,notargetmsg,nofishmsg,fishpond,selftroutprob)
59 troutcfg = (
60         __load("trouts"),
61         __load("trouts-local"),
62         ' (at the instigation of %s)',
63         "Sorry, but %s is being a spoilsport.",
64         "Who do you wish me to trout?",
65         "Fish stocks exhausted.",
66         fish,
67         0.1)
68
69 flirtcfg = (
70         __load("flirts"),
71         ' (but %s is their secret admirer)',
72         "Sorry, but %s made me take Holy Orders.",
73         "Who do you wish me to flirt with?",
74         "My libido is over-used!",
75         fish,
76         0.1)
77
78 # Hacky command to output the current fishpond state
79 def fishq(bot, cmd, nick, conn, public,f):
80         from irclib import irc_lower
81         if not public and irc_lower(nick) == irc_lower(bot.owner):
82                 state=("Fishpond state: cur_fish=%d, max_fish=%d, nofish_time=%d, "
83                        +"fish_time_inc=%d, fish_inc=%d, DoS=%d, Boring_Git=%s, "
84                        +"quotatime=%d")%(f.cur_fish,f.max_fish,f.nofish_time,
85                                          f.fish_time_inc,f.fish_inc,f.DoS,f.Boring_Git,
86                                          f.quotatime)
87                 bot.automsg(public,nick,state)
88                                     
89 # Karma implementation
90 import cPickle
91 karmafilename = "karmadump"
92 # load the karma db
93 try:
94     f = open(karmafilename, "r")
95     karmadb = cPickle.load(f)
96     f.close()
97 except IOError:
98     karmadb = {}
99 # Modify karma
100 def karma(cmd, amount):
101     thing=cmd.split()[0][:-2].lower()
102     if karmadb.has_key(thing):
103         karmadb[thing] += amount
104     else:
105         karmadb[thing] = amount
106 def savekarma():
107     try:
108         f = open(karmafilename, "w")
109         cPickle.dump(karmadb, f)
110         f.close()
111     except IOError:
112         sys.stderr.write("Problems dumping karma: probably lost :(")
113
114 # When the bot exits we should save the karma db
115 def quit(bot,cmd,nick,conn,public):
116     savekarma()
117     c.quitq(bot,cmd,nick,conn,public)
118 def reload(bot,cmd,nick,conn,public):
119     savekarma()
120     c.reloadq(bot,cmd,nick,conn,public)
121
122 # Command processing: whenever something is said that the bot can hear,
123 # "command" is invoked and must decide what to do.  This configuration
124 # defines a couple of special cases (for karma) but is otherwise driven
125 # by a dictionary of commands.
126
127 commands = {"karma": (c.karmaq,karmadb),
128             "karmalist": (c.listkeysq,karmadb),
129             "karmadel": (c.karmadelq,karmadb),
130             "info": (c.infoq,karmadb),
131             "trout": (c.troutq,troutcfg),
132             "flirt": (c.troutq,flirtcfg),
133             "fish": (fishq,fish),
134             "quiet": (c.nofishq,fish),
135             "reload": reload,
136             "quit": quit,
137             "google": c.googleq,
138             "define": c.defineq,
139             "say": c.sayq,
140             "do": c.doq }
141 # disconnect and hop annoy people
142 #            "disconnect": c.disconnq,
143 #            "hop": c.disconnq }
144 commands["list"]=(c.listkeysq,commands)
145
146 triggers = ("!", "~") # what character should the bot be invoked by:
147                       # eg !trout, ~trout etc.
148
149 def command(bot, cmd, nick, conn, public):
150     ours=0
151     try:
152             if public and cmd[0] in triggers:
153                     ours=1
154                     cmd=cmd[1:]
155             if not public:
156                     ours=1
157             command = cmd.split()[0]
158     except IndexError:
159             command=""
160     # karma: up
161     if command.endswith("++"):
162         karma(cmd,1)
163     # karma: down
164     if command.endswith("--"):
165         karma(cmd,-1)
166
167     if ours and command.lower() in commands.keys():
168         e=commands[command]
169         if callable(e):
170             e(bot,cmd,nick,conn,public)
171         else:
172             e[0](bot,cmd,nick,conn,public,*e[1:])