chiark / gitweb /
Found in /home/matthew/programming/irc/bot - live version, all files as found
[irc.git] / commands.py.orig
1 # Part of Acrobat.
2 import string, cPickle, random, urllib, sys, time
3 from irclib import irc_lower, nm_to_n
4
5 # query karma
6 def karmaq(bot, cmd, nick, conn, public, karma):
7     try:
8         item=cmd.split()[1].lower()
9     except IndexError:
10         item=None
11     if item==None:
12         bot.automsg(public,nick,"I have karma on %s items." %
13                          len(karma.keys()))
14     elif karma.has_key(item):
15         bot.automsg(public,nick,"%s has karma %s."
16                      %(item,karma[item]))
17     else:
18         bot.automsg(public,nick, "%s has no karma set." % item)
19
20 # delete karma
21 def karmadelq(bot, cmd, nick, conn, public, karma):
22     try:
23         item=cmd.split()[1].lower()
24     except IndexError:
25         conn.notice(nick, "What should I delete?")
26         return
27     if nick != bot.owner:
28         conn.notice(nick, "You are not my owner.")
29         return
30     if karma.has_key(item):
31         del karma[item]
32         conn.notice(nick, "Item %s deleted."%item)
33     else:
34         conn.notice(nick, "There is no karma stored for %s."%item)
35
36 # query bot status
37 def infoq(bot, cmd, nick, conn, public, karma):
38     bot.automsg(public,nick,
39         ("I am Acrobat %s, on %s, as nick %s.  "+
40         "My owner is %s; I have karma on %s items.") %
41         (bot.revision.split()[1], bot.channel, conn.get_nickname(),
42          bot.owner, len(karma.keys())))
43
44 # Check on fish stocks
45 def fish_quota(pond):
46     if pond.DoS:
47         if time.time()>=pond.quotatime:
48             pond.DoS=0
49         else:
50             return
51     if (time.time()-pond.quotatime)>pond.fish_time_inc:
52         pond.cur_fish+=(((time.time()-pond.quotatime)
53                          /pond.fish_time_inc)*pond.fish_inc)
54         if pond.cur_fish>pond.max_fish:
55             pond.cur_fish=pond.max_fish
56         pond.quotatime=time.time()
57
58 # trout someone, or flirt with them
59 def troutq(bot, cmd, nick, conn, public, cfg):
60     fishlist=cfg[0]
61     selftrout=cfg[1]
62     quietmsg=cfg[2]
63     notargetmsg=cfg[3]
64     nofishmsg=cfg[4]
65     fishpond=cfg[5]
66     selftroutchance=cfg[6]
67
68     fish_quota(fishpond)
69     if fishpond.DoS:
70         conn.notice(nick, quietmsg%fishpond.Boring_Git)
71         return
72     if fishpond.cur_fish<=0:
73         conn.notice(nick, nofishmsg)
74         return
75     target = string.join(cmd.split()[1:])
76     if len(target)==0:
77         conn.notice(nick, notargetmsg)
78         return
79     me = bot.connection.get_nickname()
80     trout_msg = random.choice(fishlist)
81     # The bot won't trout or flirt with itself;
82     if irc_lower(me) == irc_lower(target):
83         target = nick
84     # There's a chance the game may be given away if the request was not
85     # public...
86     if not public:
87         if random.random()<=selftroutchance:
88             trout_msg=trout_msg+(selftrout%nick)
89
90     conn.action(bot.channel, trout_msg % target)
91     fishpond.cur_fish-=1
92
93 # Shut up trouting for a minute
94 def nofishq(bot, cmd, nick, conn, public, fish):
95     fish.cur_fish=0
96     fish.DoS=1
97     fish.Boring_Git=nick
98     fish.quotatime=time.time()
99     fish.quotatime+=fish.nofish_time
100     conn.notice(nick, "Fish stocks depleted, as you wish.")
101
102 # rehash bot config
103 def reloadq(bot, cmd, nick, conn, public):
104     if not public and irc_lower(nick) == irc_lower(bot.owner):
105         try:
106             reload(bot.config)
107             conn.notice(nick, "Config reloaded.")
108         except ImportError:
109             conn.notice(nick, "Config reloading failed!")
110     else:
111         bot.automsg(public,nick,
112                 "Configuration can only be reloaded by my owner, by /msg.")
113
114 # quit irc
115 def quitq(bot, cmd, nick, conn, public):
116     if irc_lower(nick) == irc_lower(bot.owner):
117         bot.die(msg = "I have been chosen!")
118     elif public:
119         conn.notice(nick, "Such aggression in public!")
120     else:
121         conn.notice(nick, "You're not my owner.")
122
123 # google for something
124 def googleq(bot, cmd, nick, conn, public):
125     cmdrest = string.join(cmd.split()[1:])
126     # "I'm Feeling Lucky" rather than try and parse the html
127     targ = ("http://www.google.com/search?q=%s&btnI=I'm+Feeling+Lucky"
128             % urllib.quote_plus(cmdrest))
129     try:
130         # get redirected and grab the resulting url for returning
131         gsearch = urllib.urlopen(targ).geturl()
132         if gsearch != targ: # we've found something
133             bot.automsg(public,nick,str(gsearch))
134         else: # we haven't found anything.
135             bot.automsg(public,nick,"No pages found.")
136     except IOError: # if the connection times out. This blocks. :(
137         bot.automsg(public,nick,"The web's broken. Waah!")
138
139 ### say to msg/channel            
140 def sayq(bot, cmd, nick, conn, public):
141     if irc_lower(nick) == irc_lower(bot.owner):
142         conn.privmsg(bot.channel, string.join(cmd.split()[1:]))
143     else:
144         if not public:
145             conn.notice(nick, "You're not my owner!")
146
147 ### action to msg/channel
148 def doq(bot, cmd, nick, conn, public):
149     sys.stderr.write(irc_lower(bot.owner))
150     sys.stderr.write(irc_lower(nick))
151     if not public:
152         if irc_lower(nick) == irc_lower(bot.owner):
153             conn.action(bot.channel, string.join(cmd.split()[1:]))
154         else:
155             conn.notice(nick, "You're not my owner!")
156
157 ###disconnect
158 def disconnq(bot, cmd, nick, conn, public):
159     if cmd == "disconnect": # hop off for 60s
160         bot.disconnect(msg="Be right back.")
161
162 ### list keys of a dictionary
163 def listkeysq(bot, cmd, nick, conn, public, dict):
164     bot.automsg(public,nick,string.join(dict.keys()))