2 import string, cPickle, random, urllib, sys, time, re, os
3 from irclib import irc_lower, nm_to_n
6 def karmaq(bot, cmd, nick, conn, public, karma):
8 item=cmd.split()[1].lower()
12 bot.automsg(public,nick,"I have karma on %s items." %
14 elif karma.has_key(item):
15 bot.automsg(public,nick,"%s has karma %s."
18 bot.automsg(public,nick, "%s has no karma set." % item)
21 def karmadelq(bot, cmd, nick, conn, public, karma):
23 item=cmd.split()[1].lower()
25 conn.notice(nick, "What should I delete?")
28 conn.notice(nick, "You are not my owner.")
30 if karma.has_key(item):
32 conn.notice(nick, "Item %s deleted."%item)
34 conn.notice(nick, "There is no karma stored for %s."%item)
36 # help - provides the URL of the help file
37 def helpq(bot, cmd, nick, conn, public):
38 bot.automsg(public,nick,
39 "For help see http://www.pick.ucam.org/~matthew/irc/servus.html")
43 def infoq(bot, cmd, nick, conn, public, karma):
44 bot.automsg(public,nick,
45 ("I am Acrobat %s, on %s, as nick %s. "+
46 "My owner is %s; I have karma on %s items.") %
47 (bot.revision.split()[1], bot.channel, conn.get_nickname(),
48 bot.owner, len(karma.keys())))
50 # Check on fish stocks
53 if time.time()>=pond.quotatime:
57 if (time.time()-pond.quotatime)>pond.fish_time_inc:
58 pond.cur_fish+=(((time.time()-pond.quotatime)
59 /pond.fish_time_inc)*pond.fish_inc)
60 if pond.cur_fish>pond.max_fish:
61 pond.cur_fish=pond.max_fish
62 pond.quotatime=time.time()
64 # trout someone, or flirt with them
65 def troutq(bot, cmd, nick, conn, public, cfg):
72 selftroutchance=cfg[6]
76 conn.notice(nick, quietmsg%fishpond.Boring_Git)
78 if fishpond.cur_fish<=0:
79 conn.notice(nick, nofishmsg)
81 target = string.join(cmd.split()[1:])
83 conn.notice(nick, notargetmsg)
85 me = bot.connection.get_nickname()
86 trout_msg = random.choice(fishlist)
87 # The bot won't trout or flirt with itself;
88 if irc_lower(me) == irc_lower(target):
90 # There's a chance the game may be given away if the request was not
93 if random.random()<=selftroutchance:
94 trout_msg=trout_msg+(selftrout%nick)
96 conn.action(bot.channel, trout_msg % target)
100 def slashq(bot, cmd, nick, conn, public, cfg):
107 selfslashchance=cfg[6]
111 conn.notice(nick, quietmsg%fishpond.Boring_Git)
113 if fishpond.cur_fish<=0:
114 conn.notice(nick, nofishmsg)
116 target = string.join(cmd.split()[1:])
117 #who = cmd.split()[1:]
118 who = ' '.join(cmd.split()[1:]).split(' / ')
120 conn.notice(nick, "it takes two to tango!")
123 conn.notice(nick, "we'll have none of that round here")
125 me = bot.connection.get_nickname()
126 slash_msg = random.choice(fishlist)
127 # The bot won't slash people with themselves
128 if irc_lower(who[0]) == irc_lower(who[1]):
129 conn.notice(nick, "oooooh no missus!")
131 # The bot won't slash with itself, instead slashing the requester
133 if irc_lower(me) == irc_lower(who[n]):
135 # Perhaps someone asked to slash themselves with the bot then we get
136 if irc_lower(who[0]) == irc_lower(who[1]):
137 conn.notice(nick, "you wish!")
139 # There's a chance the game may be given away if the request was not
142 if random.random()<=selfslashchance:
143 slash_msg=slash_msg+(selfslash%nick)
145 conn.action(bot.channel, slash_msg % (who[0], who[1]))
149 def unitq(bot, cmd, nick, conn, public):
150 args = ' '.join(cmd.split()[1:]).split(' as ')
152 args = ' '.join(cmd.split()[1:]).split(' / ')
154 conn.notice(nick, "syntax: units arg1 as arg2")
157 sin,sout=os.popen2(["units","--verbose",args[0]],"r")
159 sin,sout=os.popen2(["units","--verbose",args[0],args[1]],"r")
162 #popen2 doesn't clean up the child properly. Do this by hand
164 if os.WEXITSTATUS(child[1])==0:
165 bot.automsg(public,nick,res[0].strip())
167 conn.notice(nick,'; '.join(map(lambda x: x.strip(),res)))
169 # Shut up trouting for a minute
170 def nofishq(bot, cmd, nick, conn, public, fish):
174 fish.quotatime=time.time()
175 fish.quotatime+=fish.nofish_time
176 conn.notice(nick, "Fish stocks depleted, as you wish.")
179 def reloadq(bot, cmd, nick, conn, public):
180 if not public and irc_lower(nick) == irc_lower(bot.owner):
183 conn.notice(nick, "Config reloaded.")
185 conn.notice(nick, "Config reloading failed!")
187 bot.automsg(public,nick,
188 "Configuration can only be reloaded by my owner, by /msg.")
190 # lose the game and/or install a new trigger word
191 def gameq(bot, cmd, nick, conn, public, game):
192 #only install a new trigger if it's not too short.
193 if len(' '.join(cmd.split()[1:]))>2:
194 game.trigger=' '.join(cmd.split()[1:])
195 if (time.time()> game.grace):
197 if irc_lower(nick) == irc_lower(bot.owner):
198 conn.action(bot.channel,"loses the game!")
200 conn.privmsg(bot.channel,nick+" just lost the game!")
203 conn.notice(nick, "It's a grace period!")
204 game.grace=time.time()+60*20 #20 minutes' grace
205 game.losetime=time.time()+random.randrange(game.minlose,game.maxlose)
206 conn.notice(bot.owner, str(game.losetime-time.time())+" "+game.trigger)
209 def quitq(bot, cmd, nick, conn, public):
210 if irc_lower(nick) == irc_lower(bot.owner):
211 bot.die(msg = "I have been chosen!")
213 conn.notice(nick, "Such aggression in public!")
215 conn.notice(nick, "You're not my owner.")
217 # google for something
218 def googleq(bot, cmd, nick, conn, public):
219 cmdrest = string.join(cmd.split()[1:])
220 # "I'm Feeling Lucky" rather than try and parse the html
221 targ = ("http://www.google.com/search?q=%s&btnI=I'm+Feeling+Lucky"
222 % urllib.quote_plus(cmdrest))
224 # get redirected and grab the resulting url for returning
225 gsearch = urllib.urlopen(targ).geturl()
226 if gsearch != targ: # we've found something
227 bot.automsg(public,nick,str(gsearch))
228 else: # we haven't found anything.
229 bot.automsg(public,nick,"No pages found.")
230 except IOError: # if the connection times out. This blocks. :(
231 bot.automsg(public,nick,"The web's broken. Waah!")
233 # Look up the definition of something using google
234 def defineq(bot, cmd, nick, conn, public):
235 cmdrest = string.join(cmd.split()[1:])
236 targ = ("http://www.google.co.uk/search?q=define%%3A%s&ie=utf-8&oe=utf-8"
237 % urllib.quote_plus(cmdrest))
239 # Just slurp everything into a string
240 defnpage = urllib.urlopen(targ).read()
241 # For definitions we really do have to parse the HTML, sadly.
242 # This is of course going to be a bit fragile. We first look for
243 # 'Definitions of %s on the Web' -- if this isn't present we
244 # assume we have the 'no definitions found page'.
245 # The first defn starts after the following <p> tag, but as the
246 # first <li> in a <ul type="disc" class=std>
247 # Following that we assume that each definition is all the non-markup
248 # before a <br> tag. Currently we just dump out the first definition.
249 match = re.search(r"Definitions of <b>.*?</b> on the Web.*?<li>\s*([^>]*)((<br>)|(<li>))",defnpage,re.MULTILINE)
251 bot.automsg(public,nick,"Some things defy definition.")
253 # We assume google has truncated the definition for us so this
254 # won't flood the channel with text...
255 defn = " ".join(match.group(1).split("\n"))
256 bot.automsg(public,nick,defn)
257 except IOError: # if the connection times out. This blocks. :(
258 bot.automsg(public,nick,"The web's broken. Waah!")
260 ### say to msg/channel
261 def sayq(bot, cmd, nick, conn, public):
262 if irc_lower(nick) == irc_lower(bot.owner):
263 conn.privmsg(bot.channel, string.join(cmd.split()[1:]))
266 conn.notice(nick, "You're not my owner!")
268 ### action to msg/channel
269 def doq(bot, cmd, nick, conn, public):
270 sys.stderr.write(irc_lower(bot.owner))
271 sys.stderr.write(irc_lower(nick))
273 if irc_lower(nick) == irc_lower(bot.owner):
274 conn.action(bot.channel, string.join(cmd.split()[1:]))
276 conn.notice(nick, "You're not my owner!")
279 def disconnq(bot, cmd, nick, conn, public):
280 if cmd == "disconnect": # hop off for 60s
281 bot.disconnect(msg="Be right back.")
283 ### list keys of a dictionary
284 def listkeysq(bot, cmd, nick, conn, public, dict, sort=False):
288 bot.automsg(public,nick,string.join(d))
290 ### rot13 text (yes, I could have typed out the letters....)
291 ### also "foo".encode('rot13') would have worked
292 def rot13q(bot, cmd, nick, conn, public):
293 a=''.join(map(chr,range((ord('a')),(ord('z')+1))))
295 trans=string.maketrans(a+a.upper(),b+b.upper())
296 conn.notice(nick, string.join(cmd.split()[1:]).translate(trans))