-
Notifications
You must be signed in to change notification settings - Fork 6
/
autocmd.py
185 lines (167 loc) · 5.41 KB
/
autocmd.py
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
# -*- coding: utf-8 -*-
# vim:set ts=8 sts=8 sw=8 tw=80 noet cc=80:
from utils import strip_jid, oneof
from module import Module, PRESENCE, MUC, COMMAND, CONFIG
class AutoCMD(Module):
def __init__(self, trollmsg, **keywords):
super(AutoCMD, self).__init__([PRESENCE, MUC, COMMAND, CONFIG],
name=__name__, **keywords)
self.trollmsg = trollmsg
self.autokick = {'jid': [], 'nick': []}
self.troll = {'jid': [], 'nick': []}
def register(self):
# autokick
self.send_cmd("register_cli", name=["auto", "kick", "nick"],
argc=1, undo=True)
self.send_cmd("register_cli", name=["auto", "kick", "jid"],
argc=1, undo=True)
self.send_cmd("register_cli", name=["show", "auto", "kick",
"nick"], argc=0, undo=False)
self.send_cmd("register_cli", name=["show", "auto", "kick",
"jid"], argc=0, undo=False)
# troll
self.send_cmd("register_cli", name=["troll", "nick"], argc=1,
undo=True)
self.send_cmd("register_cli", name=["troll", "jid"], argc=1,
undo=True)
self.send_cmd("register_cli", name=["show", "troll", "nick"],
argc=0, undo=False)
self.send_cmd("register_cli", name=["show", "troll", "jid"],
argc=0, undo=False)
def unregister(self):
self.send_cmd("unregister_cli", name=["auto", "kick", "nick"],
argc=1)
self.send_cmd("unregister_cli", name=["auto", "kick", "jid"],
argc=1)
self.send_cmd("unregister_cli", name=["show", "auto", "kick",
"nick"], argc=0)
self.send_cmd("unregister_cli", name=["show", "auto", "kick",
"jid"], argc=0)
self.send_cmd("unregister_cli", name=["troll", "nick"], argc=1)
self.send_cmd("unregister_cli", name=["troll", "jid"], argc=1)
self.send_cmd("unregister_cli", name=["show", "troll", "nick"],
argc=0)
self.send_cmd("unregister_cli", name=["show", "troll", "jid"],
argc=0)
def start(self):
self.send_cmd("get_config", key="autokick",
default={'jid': [], 'nick': []})
self.send_cmd("get_config", key="troll",
default={'jid': [], 'nick': []})
self.register()
def stop(self):
self.unregister()
def muc_online(self, nick, jid, **keywords):
jid = strip_jid(jid)
if nick in self.autokick["nick"]:
self.kick(nick, "autokick")
elif jid in self.autokick["jid"]:
self.kick(nick, "autokick")
def muc_msg(self, nick, jid, **keywords):
jid = strip_jid(jid)
if nick in self.troll["nick"]:
self.do_troll(nick)
elif jid in self.troll["jid"]:
self.do_troll(nick)
def do_troll(self, nick):
msg = oneof(self.trollmsg)
if nick == "bw5rws":
msg = oneof([msg,
"Da hattest du wohl wieder keine Ahnung ..."])
self.send_private(nick, msg, muc=True)
def command(self, cmd, **keywords):
if cmd == "reregister_cli":
self.register()
return
elif cmd == "config_value":
key = keywords["key"]
value = keywords["value"]
if key == "autokick":
self.autokick = value
elif key == "troll":
self.troll = value
return
if cmd != "cli_cmd":
return
cmd = keywords["command"]
args = keywords["args"]
undo = keywords["undo"]
if cmd == ["auto", "kick", "nick"]:
self.autokick_nick(undo, *args)
elif cmd == ["auto", "kick", "jid"]:
self.autokick_jid(undo, *args)
elif cmd == ["show", "auto", "kick", "nick"]:
self.show_autokick(True)
elif cmd == ["show", "auto", "kick", "jid"]:
self.show_autokick(False)
elif cmd == ["troll", "nick"]:
self.troll_nick(undo, *args)
elif cmd == ["troll", "jid"]:
self.troll_jid(undo, *args)
elif cmd == ["show", "troll", "nick"]:
self.show_troll(True)
elif cmd == ["show", "troll", "jid"]:
self.show_troll(False)
def autokick_nick(self, undo, nick):
if undo:
if nick in self.autokick["nick"]:
index = self.autokick["nick"].index(nick)
del self.autokick["nick"][index]
elif not nick in self.autokick["nick"]:
self.autokick["nick"] += [ nick ]
self.commit()
def autokick_jid(self, undo, jid):
jid = strip_jid(jid)
if undo:
if jid in self.autokick["jid"]:
index = self.autokick["jid"].index(jid)
del self.autokick["jid"][index]
elif not jid in self.autokick["jid"]:
self.autokick["jid"] += [ jid ]
self.commit()
def troll_nick(self, undo, nick):
if undo:
if nick in self.troll["nick"]:
index = self.troll["nick"].index(nick)
del self.troll["nick"][index]
elif not nick in self.troll["nick"]:
self.troll["nick"] += [ nick ]
self.commit()
def troll_jid(self, undo, jid):
jid = strip_jid(jid)
if undo:
if jid in self.troll["jid"]:
index = self.troll["jid"].index(jid)
del self.troll["jid"][index]
elif not jid in self.troll["jid"]:
self.troll["jid"] += [ jid ]
self.commit()
def commit(self):
self.send_cfg(key="autokick", value=self.autokick)
self.send_cfg(key="troll", value=self.troll)
def show_autokick(self, nicks):
if nicks:
if len(self.autokick["nick"]) == 0:
self.send_muc("no autokick nicks configured")
else:
self.send_muc("autokick nicks: %s" % \
", ".join(self.autokick["nick"]))
else:
if len(self.autokick["jid"]) == 0:
self.send_muc("no autokick jids configured")
else:
self.send_muc("autokick jids: %s" % \
", ".join(self.autokick["jid"]))
def show_troll(self, nicks):
if nicks:
if len(self.troll["nick"]) == 0:
self.send_muc("no troll nicks configured")
else:
self.send_muc("troll nicks: %s" % \
", ".join(self.troll["nick"]))
else:
if len(self.troll["jid"]) == 0:
self.send_muc("no troll jids configured")
else:
self.send_muc("troll jids: %s" % \
", ".join(self.troll["jid"]))