chiark / gitweb /
FishPond: Use in all other bots
[irc.git] / socbi.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 = "irc.eu.freenode.net"
27 port = 6667
28 nickname = "HBB"
29 channel = "#soc.bi"
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         ' (at the instigation of %s)',
62         "Sorry, but %s is being a spoilsport.",
63         "Who do you wish me to trout?",
64         "Fish stocks exhausted.",
65         fish,
66         0.1)
67
68 flirtcfg = (
69         __load("flirts"),
70         ' (but %s is their secret admirer)',
71         "Sorry, but %s made me take Holy Orders.",
72         "Who do you wish me to flirt with?",
73         "My libido is over-used!",
74         fish,
75         0.1)
76
77 # Hacky command to output the current fishpond state
78 def fishq(bot, cmd, nick, conn, public,f):
79         from irclib import irc_lower
80         if not public and irc_lower(nick) == irc_lower(bot.owner):
81                 state=("Fishpond state: cur_fish=%d, max_fish=%d, nofish_time=%d, "
82                        +"fish_time_inc=%d, fish_inc=%d, DoS=%d, Boring_Git=%s, "
83                        +"quotatime=%d")%(f.cur_fish,f.max_fish,f.nofish_time,
84                                          f.fish_time_inc,f.fish_inc,f.DoS,f.Boring_Git,
85                                          f.quotatime)
86                 bot.automsg(public,nick,state)
87                     
88 # Karma implementation
89 import cPickle
90 karmafilename = "karmadump"
91 # load the karma db
92 try:
93     f = open(karmafilename, "r")
94     karmadb = cPickle.load(f)
95     f.close()
96 except IOError:
97     karmadb = {}
98 # Modify karma
99 def karma(cmd, amount):
100     thing=cmd.split()[0][:-2].lower()
101     if karmadb.has_key(thing):
102         karmadb[thing] += amount
103     else:
104         karmadb[thing] = amount
105 def savekarma():
106     try:
107         f = open(karmafilename, "w")
108         cPickle.dump(karmadb, f)
109         f.close()
110     except IOError:
111         sys.stderr.write("Problems dumping karma: probably lost :(")
112
113 # When the bot exits we should save the karma db
114 def quit(bot,cmd,nick,conn,public):
115     savekarma()
116     c.quitq(bot,cmd,nick,conn,public)
117 def reload(bot,cmd,nick,conn,public):
118     savekarma()
119     c.reloadq(bot,cmd,nick,conn,public)
120
121 # Command processing: whenever something is said that the bot can hear,
122 # "command" is invoked and must decide what to do.  This configuration
123 # defines a couple of special cases (for karma) but is otherwise driven
124 # by a dictionary of commands.
125
126 commands = {"karma": (c.karmaq,karmadb),
127             "karmalist": (c.listkeysq,karmadb),
128             "karmadel": (c.karmadelq,karmadb),
129             "info": (c.infoq,karmadb),
130             "trout": (c.troutq,troutcfg),
131             "fish": (fishq,fish),
132             "flirt": (c.troutq,flirtcfg),
133             "quiet": (c.nofishq,fish),
134             "reload": reload,
135             "quit": quit,
136             "die": quit,
137             "google": c.googleq,
138             "say": c.sayq,
139             "do": c.doq }
140 # disconnect and hop annoy people
141 #            "disconnect": c.disconnq,
142 #            "hop": c.disconnq }
143 commands["list"]=(c.listkeysq,commands)
144
145 triggers = ("!", "~") # what character should the bot be invoked by:
146                       # eg !trout, ~trout etc.
147
148 def command(bot, cmd, nick, conn, public):
149     ours=0
150     try:
151             if public and cmd[0] in triggers:
152                     ours=1
153                     cmd=cmd[1:]
154             if not public:
155                     ours=1
156             command = cmd.split()[0]
157     except IndexError:
158             command=""
159     # karma: up
160     if command.endswith("++"):
161         karma(cmd,1)
162     # karma: down
163     if command.endswith("--"):
164         karma(cmd,-1)
165
166     if ours and command.lower() in commands.keys():
167         e=commands[command]
168         if callable(e):
169             e(bot,cmd,nick,conn,public)
170         else:
171             e[0](bot,cmd,nick,conn,public,*e[1:])