1 # This file is part of Acrobat.
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.
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.
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
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 # Richard Kettlewell <rjk@greenend.org.uk
24 # Acrobat configuration file
26 # The following definitions are required to be present in this module:
27 # You can also override them on the command-line
28 # e.g. python acrobat.py Servus-chiark nickname=testbot channel=\#test owner=MyNick
34 # Also a function called "command"; see later.
36 # Everything else in this file is configuration-specific.
38 import os, time, re, twitter, subprocess, sys, os.path
40 # Most command implementations are stored in a separate module.
43 # This fishpond is shared between trouts and flirts. It doesn't have to be;
44 # you can define as many ponds as you like.
45 class fish (c.FishPond):
53 # load the "blame" details for a file
54 def loadblame(filename):
55 p=subprocess.Popen(["git","blame","-s",filename],
56 stdout=subprocess.PIPE,stderr=subprocess.PIPE)
57 out,err=p.communicate()
59 sys.exit("git blame failure: %s" % err)
64 if len(line.strip())>0:
69 return bdb,keys,filename
71 # load a file full of flirts or trouts
74 f = open(filename, "r")
75 r = [l.strip() for l in f.readlines() if l.find("%s") != -1]
78 r = [ "doesn't know what to do about %s." ]
81 # (troutlist,selftroutmsg,DoSmsg,notargetmsg,nofishmsg,fishpond,selftroutprob)
84 ' (at the instigation of %s)',
85 "Sorry, but %s is being a spoilsport.",
86 "Who do you wish me to trout?",
87 "Fish stocks exhausted.",
95 ' (but %s is their secret admirer)',
96 "Sorry, but %s made me take Holy Orders.",
97 "Who do you wish me to flirt with?",
98 "My libido is over-used!",
106 ' (while %s watches)',
107 "Sorry, but %s stole my pen.",
108 "Who do you want to slash?",
109 "I have writer's block!",
115 # Hacky command to output the current fishpond state
116 def fishq(bot, cmd, nick, conn, public,f):
117 from irclib import irc_lower
118 if not public and irc_lower(nick) == irc_lower(bot.owner):
119 state=("Fishpond state: cur_fish=%d, max_fish=%d, nofish_time=%d, "
120 +"fish_time_inc=%d, fish_inc=%d, DoS=%d, Boring_Git=%s, "
121 +"quotatime=%d")%(f.cur_fish,f.max_fish,f.nofish_time,
122 f.fish_time_inc,f.fish_inc,f.DoS,f.Boring_Git,
124 bot.automsg(public,nick,state)
126 # Karma implementation
128 karmafilename = "chiark-karma-"+channel
131 f = open(karmafilename, "r")
132 karmadb = cPickle.load(f)
137 def karma(cmd, amount):
138 thing=cmd.split()[0][:-2].lower()
139 if karmadb.has_key(thing):
140 karmadb[thing] += amount
142 karmadb[thing] = amount
145 tmp = "%s.tmp" % karmafilename
148 cPickle.dump(karmadb, f)
150 os.rename(tmp, karmafilename)
152 sys.stderr.write("error writing karma: %s" % e)
154 def quit(bot,cmd,nick,conn,public):
155 c.quitq(bot,cmd,nick,conn,public)
156 def reload(bot,cmd,nick,conn,public):
157 c.reloadq(bot,cmd,nick,conn,public)
159 # initialise the urldb on startup
162 #expire urls if not asked about or seen for >71 hours
164 #do an expiry run every hour
168 #path where Oauth details are kept
169 twioauthpath=os.path.expanduser("~/private/servus_twapi_oauth.txt")
172 f=open(twioauthpath,"r")
176 key,val=map(str.strip,line.split(':'))
177 if key == "consumer_key":
179 elif key == "consumer_secret":
181 elif key == "access_token":
183 elif key == "access_token_secret":
186 raise ValueError, "Invalid line in twitter auth details file %s" % line
188 twitapi = twitter.Api(consumer_key = twoaapck,
189 consumer_secret = twoaapcs,
190 access_token_key = twoapat,
191 access_token_secret = twoapats,
192 tweet_mode = "extended")
194 # non-authenticated twitter api instance
195 twitapi = twitter.Api()
197 # Command processing: whenever something is said that the bot can hear,
198 # "command" is invoked and must decide what to do. This configuration
199 # defines a couple of special cases (for karma) but is otherwise driven
200 # by a dictionary of commands.
202 commands = {"karma": (c.karmaq,karmadb),
203 "karmalist": (c.listkeysq,karmadb),
204 "karmadel": (c.karmadelq,karmadb),
205 "info": (c.infoq,karmadb),
206 "trout": (c.troutq,troutcfg),
207 "slash": (c.slashq, slashcfg),
209 "fish": (fishq,fish),
210 "flirt": (c.troutq,flirtcfg),
211 "quiet": (c.nofishq,fish),
217 "url": (c.urlq,urldb),
218 "nsfw": (c.nsfwq,urldb),
219 "nws": (c.nsfwq,urldb),
221 "currency":c.currencyq,
222 "blame": (c.blameq,fish, [troutcfg,flirtcfg,slashcfg]),
226 "twit": (c.twitterq,twitapi) }
227 # disconnect and hop annoy people
228 # "disconnect": c.disconnq,
229 # "hop": c.disconnq }
230 commands["list"]=(c.listkeysq,commands,True)
232 triggers = ("!", "~") # what character should the bot be invoked by:
233 # eg !trout, ~trout etc.
235 def command(bot, cmd, nick, conn, public):
236 global urldb,lastexp,expirelen,expirevery,twitapi
239 if public and cmd[0] in triggers:
244 command = cmd.split()[0]
249 if t - lastexp > expirevery:
250 c.urlexpire(urldb,expirelen)
254 if c.urlre.search(cmd) and command.lower()!="url":
255 c.dourl(bot,conn,nick,cmd,urldb)
258 if command.endswith("++"):
261 if command.endswith("--"):
264 if ours and command.lower() in commands.keys():
265 e=commands[command.lower()]
267 e(bot,cmd,nick,conn,public)
269 e[0](bot,cmd,nick,conn,public,*e[1:])