Skip to content

Commit ed41f02

Browse files
authored
Merge pull request #413 from TotallyNotRobots/cleanup-admin-channel
Clean up admin_channel.py
2 parents 1645778 + 20d4058 commit ed41f02

File tree

2 files changed

+106
-39
lines changed

2 files changed

+106
-39
lines changed

plugins/admin_channel.py

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,26 @@ def check_for_chan_mode(char, mode_warn, event):
2222
return False
2323

2424

25-
def mode_cmd(mode, text, text_inp, chan, nick, event, mode_warn=True):
25+
def mode_cmd(mode, action, param, event, mode_warn=True):
2626
"""generic mode setting function"""
27+
chan = event.chan
28+
nick = event.nick
2729
if not check_for_chan_mode(mode[1], mode_warn, event):
2830
return False
2931

30-
split = text_inp.split(" ")
32+
split = param.split(" ")
3133
if split[0].startswith("#"):
3234
channel = split[0]
3335
target = split[1]
3436
else:
3537
channel = chan
3638
target = split[0]
3739

38-
event.notice("Attempting to {} {} in {}...".format(text, target, channel))
40+
event.notice("Attempting to {} {} in {}...".format(action, target, channel))
3941
event.admin_log(
4042
MODE_CMD_LOG.format(
4143
nick=nick,
42-
cmd=text,
44+
cmd=action,
4345
mode=mode,
4446
target=target,
4547
channel=channel,
@@ -50,22 +52,23 @@ def mode_cmd(mode, text, text_inp, chan, nick, event, mode_warn=True):
5052
return True
5153

5254

53-
def mode_cmd_no_target(mode, text, text_inp, chan, event, mode_warn=True):
55+
def mode_cmd_no_target(mode, action, param, event, mode_warn=True):
5456
"""generic mode setting function without a target"""
57+
chan = event.chan
5558
if not check_for_chan_mode(mode[1], mode_warn, event):
5659
return False
5760

58-
split = text_inp.split(" ")
61+
split = param.split(" ")
5962
if split[0].startswith("#"):
6063
channel = split[0]
6164
else:
6265
channel = chan
6366

64-
event.notice("Attempting to {} {}...".format(text, channel))
67+
event.notice("Attempting to {} {}...".format(action, channel))
6568
event.admin_log(
6669
MODE_CMD_NO_TARGET_LOG.format(
6770
nick=event.nick,
68-
cmd=text,
71+
cmd=action,
6972
mode=mode,
7073
channel=channel,
7174
)
@@ -74,80 +77,80 @@ def mode_cmd_no_target(mode, text, text_inp, chan, event, mode_warn=True):
7477
return True
7578

7679

77-
def do_extban(char, text, text_inp, chan, nick, event, adding=True):
80+
def do_extban(char, action, param, event, adding=True):
7881
serv_info = event.conn.memory["server_info"]
7982
if char not in serv_info.get("extbans", ""):
8083
return False
8184

8285
extban_pfx = serv_info["extban_prefix"]
8386

84-
split = text_inp.split(" ")
87+
split = param.split(" ")
8588
if split[0].startswith("#"):
8689
channel = split[0]
8790
target = split[1]
88-
text_inp = "{} {}{}:{}".format(channel, extban_pfx, char, target)
91+
param = "{} {}{}:{}".format(channel, extban_pfx, char, target)
8992
else:
9093
target = split[0]
91-
text_inp = "{}{}:{}".format(extban_pfx, char, target)
94+
param = "{}{}:{}".format(extban_pfx, char, target)
9295

93-
mode_cmd("+b" if adding else "-b", text, text_inp, chan, nick, event)
96+
mode_cmd("+b" if adding else "-b", action, param, event)
9497
return True
9598

9699

97100
@hook.command(permissions=["op_ban", "op", "chanop"])
98-
def ban(text, chan, nick, event):
101+
def ban(text, event):
99102
"""[channel] <user> - bans <user> in [channel], or in the caller's channel if no channel is specified"""
100-
mode_cmd("+b", "ban", text, chan, nick, event)
103+
mode_cmd("+b", "ban", text, event)
101104

102105

103106
@hook.command(permissions=["op_ban", "op", "chanop"])
104-
def unban(text, chan, nick, event):
107+
def unban(text, event):
105108
"""[channel] <user> - unbans <user> in [channel], or in the caller's channel if no channel is specified"""
106-
mode_cmd("-b", "unban", text, chan, nick, event)
109+
mode_cmd("-b", "unban", text, event)
107110

108111

109112
@hook.command(permissions=["op_quiet", "op", "chanop"])
110-
def quiet(text, chan, nick, event):
113+
def quiet(text, event):
111114
"""[channel] <user> - quiets <user> in [channel], or in the caller's channel if no channel is specified"""
112-
if mode_cmd("+q", "quiet", text, chan, nick, event, False):
115+
if mode_cmd("+q", "quiet", text, event, False):
113116
return
114117

115-
if not do_extban("m", "quiet", text, chan, nick, event, True):
118+
if not do_extban("m", "quiet", text, event, True):
116119
event.notice("Unable to set +q or a mute extban on this network.")
117120

118121

119122
@hook.command(permissions=["op_quiet", "op", "chanop"])
120-
def unquiet(text, chan, nick, event):
123+
def unquiet(text, event):
121124
"""[channel] <user> - unquiets <user> in [channel], or in the caller's channel if no channel is specified"""
122-
if mode_cmd("-q", "unquiet", text, chan, nick, event, False):
125+
if mode_cmd("-q", "unquiet", text, event, False):
123126
return
124127

125-
if not do_extban("m", "unquiet", text, chan, nick, event, False):
128+
if not do_extban("m", "unquiet", text, event, False):
126129
event.notice("Unable to unset +q or a mute extban on this network.")
127130

128131

129132
@hook.command(permissions=["op_voice", "op", "chanop"])
130-
def voice(text, chan, nick, event):
133+
def voice(text, event):
131134
"""[channel] <user> - voices <user> in [channel], or in the caller's channel if no channel is specified"""
132-
mode_cmd("+v", "voice", text, chan, nick, event)
135+
mode_cmd("+v", "voice", text, event)
133136

134137

135138
@hook.command(permissions=["op_voice", "op", "chanop"])
136-
def devoice(text, chan, nick, event):
139+
def devoice(text, event):
137140
"""[channel] <user> - devoices <user> in [channel], or in the caller's channel if no channel is specified"""
138-
mode_cmd("-v", "devoice", text, chan, nick, event)
141+
mode_cmd("-v", "devoice", text, event)
139142

140143

141144
@hook.command(permissions=["op_op", "op", "chanop"])
142-
def op(text, chan, nick, event):
145+
def op(text, event):
143146
"""[channel] <user> - ops <user> in [channel], or in the caller's channel if no channel is specified"""
144-
mode_cmd("+o", "op", text, chan, nick, event)
147+
mode_cmd("+o", "op", text, event)
145148

146149

147150
@hook.command(permissions=["op_op", "op", "chanop"])
148-
def deop(text, chan, nick, event):
151+
def deop(text, event):
149152
"""[channel] <user> - deops <user> in [channel], or in the caller's channel if no channel is specified"""
150-
mode_cmd("-o", "deop", text, chan, nick, event)
153+
mode_cmd("-o", "deop", text, event)
151154

152155

153156
@hook.command(permissions=["op_topic", "op", "chanop"])
@@ -209,24 +212,24 @@ def remove(text, chan, conn, nick, event):
209212

210213

211214
@hook.command(permissions=["op_mute", "op", "chanop"], autohelp=False)
212-
def mute(text, chan, event):
215+
def mute(text, event):
213216
"""[channel] - mutes [channel], or in the caller's channel if no channel is specified"""
214-
mode_cmd_no_target("+m", "mute", text, chan, event)
217+
mode_cmd_no_target("+m", "mute", text, event)
215218

216219

217220
@hook.command(permissions=["op_mute", "op", "chanop"], autohelp=False)
218-
def unmute(text, chan, event):
221+
def unmute(text, event):
219222
"""[channel] - unmutes [channel], or in the caller's channel if no channel is specified"""
220-
mode_cmd_no_target("-m", "unmute", text, chan, event)
223+
mode_cmd_no_target("-m", "unmute", text, event)
221224

222225

223226
@hook.command(permissions=["op_lock", "op", "chanop"], autohelp=False)
224-
def lock(text, chan, event):
227+
def lock(text, event):
225228
"""[channel] - locks [channel], or in the caller's channel if no channel is specified"""
226-
mode_cmd_no_target("+i", "lock", text, chan, event)
229+
mode_cmd_no_target("+i", "lock", text, event)
227230

228231

229232
@hook.command(permissions=["op_lock", "op", "chanop"], autohelp=False)
230-
def unlock(text, chan, event):
233+
def unlock(text, event):
231234
"""[channel] - unlocks [channel], or in the caller's channel if no channel is specified"""
232-
mode_cmd_no_target("-i", "unlock", text, chan, event)
235+
mode_cmd_no_target("-i", "unlock", text, event)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from unittest.mock import MagicMock, call
2+
3+
from plugins import admin_channel
4+
5+
6+
def test_ban_no_char():
7+
event = MagicMock(chan="#bar")
8+
event.conn.memory = {"server_info": {}}
9+
admin_channel.ban("foo", event)
10+
assert event.mock_calls == [
11+
call.notice(
12+
"Mode character 'b' does not seem to exist on this network."
13+
)
14+
]
15+
16+
17+
def test_ban():
18+
event = MagicMock(chan="#bar", nick="test")
19+
event.conn.memory = {"server_info": {"channel_modes": "b"}}
20+
admin_channel.ban("foo", event)
21+
assert event.mock_calls == [
22+
call.notice("Attempting to ban foo in #bar..."),
23+
call.admin_log("test used ban to set +b on foo in #bar."),
24+
call.conn.send("MODE #bar +b foo"),
25+
]
26+
27+
28+
def test_ban_other_chan():
29+
event = MagicMock(chan="#bar", nick="test")
30+
event.conn.memory = {"server_info": {"channel_modes": "b"}}
31+
admin_channel.ban("#baz foo", event)
32+
assert event.mock_calls == [
33+
call.notice("Attempting to ban foo in #baz..."),
34+
call.admin_log("test used ban to set +b on foo in #baz."),
35+
call.conn.send("MODE #baz +b foo"),
36+
]
37+
38+
39+
def test_lock():
40+
event = MagicMock(chan="#bar", nick="test")
41+
event.conn.memory = {"server_info": {"channel_modes": "i"}}
42+
admin_channel.lock("", event)
43+
assert event.mock_calls == [
44+
call.notice("Attempting to lock #bar..."),
45+
call.admin_log("test used lock to set +i in #bar."),
46+
call.conn.send("MODE #bar +i"),
47+
]
48+
49+
50+
def test_quiet():
51+
event = MagicMock(chan="#bar", nick="test")
52+
event.conn.memory = {
53+
"server_info": {
54+
"channel_modes": "b",
55+
"extbans": "m",
56+
"extban_prefix": "",
57+
}
58+
}
59+
admin_channel.quiet("foo", event)
60+
assert event.mock_calls == [
61+
call.notice("Attempting to quiet m:foo in #bar..."),
62+
call.admin_log("test used quiet to set +b on m:foo in #bar."),
63+
call.conn.send("MODE #bar +b m:foo"),
64+
]

0 commit comments

Comments
 (0)