Caught signals that would otherwise be fatal and saveless.
[halbot.git] / panic.py
blobdfa2346bc8555df30211b8c3bcab6897870bb35f
1 import irclib
2 from common import resize
3 from connection import hal, irc
4 import globals
5 from globals import commands, private, nick
6 from basic_commands import reply
7 import perms
8 import whois
10 def panic(who, where, args):
11 "$panic: Engages panic mode in the channel. Hal will set mode +mi, clear all voices, and disable autovoice. The inverse is $unpanic."
12 if where == private:
13 reply(who, where, "Command not available in private, send it in a channel.")
14 return
15 hal.connection.mode(where, "+mi")
16 hal.connection.privmsg("ChanServ", "ACCESS %s ADD %s 100" % (where, nick))
17 hal.connection.privmsg("ChanServ", "CLEAR %s VOICES" % where)
18 hal.connection.privmsg("ChanServ", "ACCESS %s ADD %s 50" % (where, nick))
19 hal.connection.privmsg("ChanServ", "LEVELS %s SET AUTOVOICE 30" % where)
20 commands['panic'] = (perms.op, panic)
22 def unpanic(who, where, args):
23 "$unpanic: Disables $panic mode in the channel. Hall will set mode -i, voice everyone, and re-engage autovoice. Note that he leaves +m, since everyone is autovoiced anyway."
24 if where == private:
25 reply(who, where, "Command not available in private, send it in a channel.")
26 return
27 hal.connection.mode(where, "-i")
28 hal.connection.privmsg("ChanServ", "LEVELS %s SET AUTOVOICE 0" % where)
29 if where in hal.channels:
30 buffer = []
31 buffer_size = 6
32 for user in hal.channels[where].userdict:
33 if len(buffer) < buffer_size:
34 buffer.append(user)
35 else:
36 hal.connection.mode(where, "+%s %s" % (("v" * buffer_size),
37 " ".join(buffer)))
38 buffer = [user]
39 if buffer:
40 hal.connection.mode(where, "+%s %s" % (("v" * len(buffer)),
41 " ".join(buffer)))
42 commands['unpanic'] = (perms.op, unpanic)
44 def kickban(who, where, args):
45 """$kickban <user> <reason>: Kicks and bans a user from the channel. The ban is for *!*@<user's host>. Note that <user> is case-sensitive."""
46 if where == private:
47 reply(who, where, "Channel-only command.")
48 return
49 user, reason = resize(args.split(" ", 1), 2)
50 whois.userhost_callbacks[user] = (do_kickban, (who, where, reason))
51 hal.connection.userhost([user])
52 commands['kickban'] = (perms.hop, kickban)
54 def do_kickban(nm, who, where, reason, akick=True):
55 if akick:
56 hal.connection.privmsg("ChanServ", "ACCESS %s ADD %s 100" % (where, nick))
57 hal.connection.privmsg("ChanServ", "AKICK %s ADD *@%s %s (%s)" %
58 (where, irclib.nm_to_h(nm), reason, who))
59 hal.connection.privmsg("ChanServ", "ACCESS %s ADD %s 50" % (where, nick))
60 hal.connection.mode(where, "+b *!*@%s" % irclib.nm_to_h(nm))
61 hal.connection.kick(where, irclib.nm_to_n(nm), "%s (%s)" % (reason, who))
63 def server_ban(who, where, args):
64 """$server-ban <user> <reason>: Kicks and bans a user from the server. The ban is for *@<user's host>. Note that <user> is case-sensitive."""
65 user, reason = resize(args.split(" ", 1), 2)
66 whois.userhost_callbacks[user] = (do_server_ban, (who, where, reason))
67 hal.connection.userhost([user])
68 commands['server-ban'] = (perms.ircop, server_ban)
70 def do_server_ban(nm, who, where, reason):
71 hal.connection.privmsg("OperServ", "AKILL ADD +0 *@%s %s (%s)" %
72 (irclib.nm_to_h(nm), reason, who))
73 hal.connection.send_raw("KILL %s %s (%s)" % (irclib.nm_to_n(nm), reason, who))
75 def kill_channel(who, where, args):
76 """$kill-channel <reason>: When sent, and whenever anyone joins the channel after, Hal will run /msg OperServ AKILLCHAN KILL +0 <channel> <reason>, causing all non-oper users on the channel to be kicked off the server and permabanned."""
77 reason = args
78 if where == private:
79 reply(who, where, "Channel-only command.")
80 elif not reason:
81 reply(who, where, "Usage: $kill-channel <reason>")
82 else:
83 globals.kill_channels[where] = reason
84 do_kill_channel(where)
85 reply(who, where, "Done.")
86 commands['kill-channel'] = (perms.ircop, kill_channel)
88 def unkill_channel(who, where, args):
89 if where == private:
90 reply(who, where, "Channel-only command.")
91 else:
92 if where in globals.kill_channels:
93 del globals.kill_channels[where]
94 reply(who, where, "Done.")
95 else:
96 reply(who, where, "Channel was not set to autokill.")
97 commands['unkill-channel'] = (perms.ircop, unkill_channel)
99 def do_kill_channel(channel):
100 reason = globals.kill_channels[channel]
101 hal.connection.privmsg("OperServ", "AKILLCHAN KILL +0 %s %s" % (channel, reason))
103 def got_join(conn, event):
104 channel = event.target()
105 if channel in globals.kill_channels:
106 do_kill_channel(channel)
107 irc.add_global_handler("join", got_join)