1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#!/usr/bin/python3
import miniirc
import miniirc_extras
import sys
import time
assert miniirc.ver >= (1, 4, 0), "This bot requires miniirc >= v1.4.0."
exec("from " + sys.argv[1] + " import *")
def flags(channel, user):
try:
return chandata[channel.name][user.raw_hostmask]
except KeyError:
return set()
def grant(channel, user, flag):
try:
chandata[channel.name][user.raw_hostmask].add(flag)
return chandata[channel.name][user.raw_hostmask]
except KeyError:
chandata[channel.name][user.raw_hostmask] = {flag}
return chandata[channel.name][user.raw_hostmask]
def revoke(channel, user, flag):
try:
chandata[channel.name][user.raw_hostmask].remove(flag)
return chandata[channel.name][user.raw_hostmask]
except KeyError:
return False
def hammer(channel, user, poop):
try:
chaneww[channel.name][user.raw_hostmask].add(poop)
return chaneww[channel.name][user.raw_hostmask]
except KeyError:
chaneww[channel.name][user.raw_hostmask] = {poop}
return chaneww[channel.name][user.raw_hostmask]
def lift(channel, user, poop):
try:
chaneww[channel.name][user.raw_hostmask].remove(poop)
return chaneww[channel.name][user.raw_hostmask]
except KeyError:
return False
def devoice(irc, channel, target):
if target.nick in channel.modes.getset("+v"):
irc.send("MODE", channel.name, "-v", target.nick)
return True
return False
def voice(irc, channel, target):
if not target.nick in channel.modes.getset("+v"):
irc.send("MODE", channel.name, "+v", target.nick)
return True
return False
def deop(irc, channel, target):
print(72 * "-")
print(channel.modes.getset("o"))
if target.nick in channel.modes.getset("o"):
irc.send("MODE", channel.name, "-o", target.nick)
return True
return False
def op(irc, channel, target):
if not target.nick in channel.modes.getset("o"):
irc.send("MODE", channel.name, "+o", target.nick)
return True
return False
def quiet(irc, channel, target, seconds, reason):
deop(irc, channel, target)
hammer(channel, target, "quiet")
irc.send("MODE", channel.name, "+q", f"*!{target.ident}@{target.host}")
if int(seconds) > 0:
time.sleep(int(seconds))
irc.send("MODE", channel.name, "-q", f"*!{target.ident}@{target.host}")
lift(channel, target, "quiet")
def kick(irc, channel, target, reason):
irc.send("KICK", channel.name, target.nick, reason)
def ban(irc, channel, target, seconds, reason):
deop(irc, channel, target)
hammer(channel, target, "ban")
irc.send("MODE", channel.name, "+b", f"*!{target.ident}@{target.host}")
kick(irc, channel, target, reason)
if int(seconds) > 0:
time.sleep(int(seconds))
irc.send("MODE", channel.name, "-b", f"*!{target.ident}@{target.host}")
lift(channel, target, "ban")
print("Welcome to {}!".format(nick), file=sys.stderr)
irc = miniirc.IRC(
ip,
port,
nick,
channels,
ident=ident,
realname=realname,
ns_identity=identity,
debug=debug,
auto_connect=False,
)
@irc.Handler("PRIVMSG", colon=False)
def handle_privmsg(irc, hostmask, args):
channame = args[0]
try:
channel = irc.chans[channame]
except AssertionError:
pass
text = args[-1].split(" ")
cmd = text[0].lower()
if cmd.startswith(prefix) and channame.startswith("#"):
cmd = cmd[len(prefix) :]
p = False
elif channame == irc.nick:
p = True
else:
return
try:
if cmd == "help":
irc.notice(
hostmask[0],
f"\x02Guard\x02 is a client-bot channel management utility. Documentation is in the source code at git://git.andrewyu.org/ircbots. The prefix charactar is '{prefix}'.",
)
# the below are channel-only
elif p:
return
elif cmd == "ban":
if "ban" in flags(channel, irc.users[hostmask[0]]):
ban(irc, channel, irc.users[text[1]], text[2], " ".join(text[3:]))
else:
irc.notice(hostmask[0], f"[{channame}] Permission denied.")
elif cmd == "op":
if "op" in flags(channel, irc.users[hostmask[0]]):
if len(text) < 2:
irc.send("MODE", channame, "+o", hostmask[0])
elif text[1] == "":
irc.send("MODE", channame, "+o", hostmask[0])
else:
irc.send("MODE", channame, "+o", text[1])
else:
irc.notice(hostmask[0], f"[{channame}] Permission denied.")
elif cmd == "grant":
if "flags" in flags(channel, irc.users[hostmask[0]]):
grant(channel, irc.users[text[1]], text[2])
else:
irc.notice(hostmask[0], f"[{channame}] Permission denied.")
elif cmd == "revoke":
if "flags" in flags(channel, irc.users[hostmask[0]]):
revoke(channel, irc.users[text[1]], text[2])
else:
irc.notice(hostmask[0], f"[{channame}] Permission denied.")
elif cmd == "quiet":
if "ban" in flags(channel, irc.users[hostmask[0]]):
quiet(irc, channel, irc.users[text[1]], text[2], " ".join(text[3:]))
else:
irc.notice(hostmask[0], f"[{channame}] Permission denied.")
elif cmd == "flags":
_ = flags(channel, irc.users[text[1]])
if _:
irc.notice(
hostmask[0],
f"[{channame}] Flags of {text[1]}: " + ", ".join(_) + ".",
)
else:
irc.notice(hostmask[0], f"[{channame}] {text[1]} has no flags.")
else:
irc.notice(hostmask[0], f"[{channame}] Invalid command.")
except (KeyError, IndexError) as e:
irc.notice(hostmask[0], f"[{channame}] {str(type(e))}: {str(e)}.")
# @irc.Handler("JOIN", colon=False)
# def handle_privmsg(irc, hostmask, args):
if __name__ == "__main__":
# make sure you have miniirc_extras
irc.require("chans")
irc.require("users")
irc.connect()
|