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