chiark / gitweb /
New trouts, and update the flirts to fix a typo
[irc.git] / commands.py
1 # Part of Acrobat.
2 import string, cPickle, random, urllib, sys
3 from irclib import irc_lower, nm_to_n
4
5 # query karma
6 def karmaq(bot, cmd, nick, conn, public):
7     # in public
8     if public == 1:
9         try:
10             if bot.karma.dict.has_key(cmd.split()[1]):
11                 conn.privmsg(bot.channel, "%s has karma %s."
12                              %(cmd.split()[1],
13                                    bot.karma.dict[cmd.split()[1]]))
14             else:
15                 conn.privmsg(bot.channel, "%s has no karma set." %
16                              cmd.split()[1])
17         except IndexError:
18             conn.privmsg(bot.channel, "I have karma on %s items." %
19                          len(bot.karma.dict.keys()))
20     # in private
21     else:
22         try:
23             if bot.karma.dict.has_key(cmd.split()[1]):
24                 conn.notice(nick, "%s has karma %s." %
25                             (cmd.split()[1],
26                              bot.karma.dict[cmd.split()[1]]))
27             else:
28                 conn.notice(nick, "I have karma on %s items." %
29                             len(bot.karma.dict.keys()))
30         except IndexError:
31             conn.notice(nick, "I have karma on %s items." %
32                         len(bot.karma.dict.keys()))
33 # query bot status
34 def infoq(bot, cmd, nick, conn, public):
35     if public == 1:
36         conn.privmsg(bot.channel,
37                      "I am Acrobat %s, on %s, as nick %s." %
38                     (bot.revision.split()[1], bot.channel, conn.get_nickname()))
39         conn.privmsg(bot.channel,
40                      "My owner is %s; I have karma on %s items." %
41                      (bot.owner, len(bot.karma.dict.keys())))
42     else:
43         conn.notice(nick, "I am Acrobat %s, on %s, as nick %s." %
44                     (bot.revision.split()[1], bot.channel, conn.get_nickname()))
45         conn.notice(nick, "My owner is %s; I have karma on %s items." %
46                     (bot.owner, len(bot.karma.dict.keys())))
47
48 # trout someone
49 def troutq(bot, cmd, nick, conn, public):
50     try:
51         target = string.join(cmd.split()[1:])
52         me = bot.connection.get_nickname()
53         trout_msg = random.choice(bot.trouts)
54         # The bot won't trout itself;
55         if irc_lower(me) == irc_lower(target):
56             target = nick
57         conn.action(bot.channel, trout_msg % target)
58         if public == 0:
59             if random.random() <= bot.config.selftroutrisk:
60                 conn.action(bot.channel,
61                  "notes %s is conducting a whispering campaign." % nick)
62     except IndexError:
63         conn.notice(nick, "Who do you wish me to trout?")
64
65 # rehash bot config
66 def reloadq(bot, cmd, nick, conn, public):
67     if irc_lower(nick) == irc_lower(bot.owner):
68         try:
69             reload(bot.config)
70             bot.trouts = bot.config.trouts
71             conn.privmsg(nick, "Config reloaded.")
72         except ImportError:
73             conn.notice(nick, "Config reloading failed!")
74     else:
75         conn.notice(nick, "This command can only be invoked by my owner.")
76
77 # quit irc
78 def quitq(bot, cmd, nick, conn, public):
79     if irc_lower(nick) == irc_lower(bot.owner):
80         try:
81             f = open(bot.karmafilename, "w")
82             cPickle.dump(bot.karma, f)
83             f.close()
84         except IOError:
85             sys.stderr.write("Problems dumping karma: probably lost :(")
86         bot.die(msg = "I have been chosen!")
87     elif public == 1:
88         conn.privmsg(nick, "Such aggression in public!")
89     else:
90         conn.notice(nick, "You're not my owner.")
91
92 # google for something
93 def googleq(bot, cmd, nick, conn, public):
94     cmdrest = string.join(cmd.split()[1:])
95     #sys.stderr.write(conn)
96     # "I'm Feeling Lucky" rather than try and parse the html
97     targ = ("http://www.google.com/search?q=%s&btnI=I'm+Feeling+Lucky"
98             % urllib.quote_plus(cmdrest))
99     try:
100         # get redirected and grab the resulting url for returning
101         gsearch = urllib.urlopen(targ).geturl()
102         if gsearch != targ: # we've found something
103             if public == 0:
104                 conn.notice(nick, str(gsearch))
105             else: # we haven't found anything.
106                 conn.privmsg(nick, str(gsearch))
107         else:
108             if public == 0:
109                 conn.notice(nick, "No pages found.")
110             else:
111                 conn.privmsg(nick, "No pages found.")
112     except IOError: # if the connection times out. This blocks. :(
113         if public == 0:
114             conn.notice(nick, "The web's broken. Waah!")
115         else:
116             conn.privmsg(nick, "The web's broken. Waah!")
117
118 ### say to msg/channel            
119 def sayq(bot, cmd, nick, conn, public):
120     if irc_lower(nick) == irc_lower(bot.owner):
121         conn.privmsg(bot.channel, string.join(cmd.split()[1:]))
122     else:
123         conn.privmsg(nick, "You're not my owner!")
124
125 ### action to msg/channel
126 def doq(bot, cmd, nick, conn, public):
127     sys.stderr.write(irc_lower(bot.owner))
128     sys.stderr.write(irc_lower(nick))
129     if public == 0:
130         if irc_lower(nick) == irc_lower(bot.owner):
131             conn.action(bot.channel, string.join(cmd.split()[1:]))
132         else:
133             conn.privmsg(nick, "You're not my owner!")
134
135 ###disconnect
136 def disconnq(bot, cmd, nick, conn, public):
137     if cmd == "disconnect": # hop off for 60s
138         bot.disconnect(msg="Be right back.")
139