2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on channel_access
|
|
|
|
#--depends-on check_mode
|
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-04-18 15:50:19 +00:00
|
|
|
|
2019-08-14 13:38:47 +00:00
|
|
|
KICK_REASON = "your behavior is not conducive to the desired environment"
|
2018-09-30 12:28:26 +00:00
|
|
|
|
2019-08-14 13:51:16 +00:00
|
|
|
KICK_REASON_SETTING = utils.Setting("default-kick-reason",
|
|
|
|
"Set the default kick reason", example="have a nice trip")
|
|
|
|
|
2019-06-28 22:16:05 +00:00
|
|
|
@utils.export("channelset", utils.Setting("ban-format",
|
|
|
|
"Set ban format ($n = nick, $u = username, $h = hostname)",
|
|
|
|
example="*!$u@$h"))
|
2019-06-28 22:25:24 +00:00
|
|
|
@utils.export("serverset", utils.OptionsSetting("mute-method",
|
2019-07-10 07:56:51 +00:00
|
|
|
["qmode", "insp", "unreal", "none"],
|
|
|
|
"Set this server's method of muting users"))
|
2019-08-14 13:51:16 +00:00
|
|
|
@utils.export("botset", KICK_REASON_SETTING)
|
|
|
|
@utils.export("serverset", KICK_REASON_SETTING)
|
|
|
|
@utils.export("channelset", KICK_REASON_SETTING)
|
2018-09-27 11:08:07 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-08-14 14:08:17 +00:00
|
|
|
_name = "ChanOp"
|
|
|
|
|
2019-08-14 13:38:47 +00:00
|
|
|
def _parse_time(self, args, min_args):
|
|
|
|
if args[0][0] == "+":
|
|
|
|
if len(args[1:]) < min_args:
|
|
|
|
raise utils.EventError("Not enough arguments")
|
|
|
|
time = utils.from_pretty_time(args[0][1:])
|
|
|
|
if time == None:
|
|
|
|
raise utils.EventError("Invalid timeframe")
|
|
|
|
return time, args[1:]
|
|
|
|
return None, args
|
|
|
|
|
2019-08-14 13:51:16 +00:00
|
|
|
def _kick_reason(self, server, channel):
|
|
|
|
return channel.get_setting("default-kick-reason",
|
|
|
|
server.get_setting("default-kick-reason",
|
|
|
|
self.bot.get_setting("default-kick-reson", KICK_REASON)))
|
|
|
|
|
2019-08-14 13:38:47 +00:00
|
|
|
def _kick(self, server, channel, target_nickname, reason):
|
|
|
|
target_user = server.get_user(target_nickname, create=False)
|
|
|
|
if target_user and channel.has_user(target_user):
|
2019-08-14 13:51:16 +00:00
|
|
|
reason = " ".join(reason) or self._kick_reason(server, channel)
|
2019-08-14 13:38:47 +00:00
|
|
|
channel.send_kick(target_user.nickname, reason)
|
2018-09-30 12:28:26 +00:00
|
|
|
else:
|
2019-08-14 13:38:47 +00:00
|
|
|
raise utils.EventError("No such user")
|
|
|
|
|
|
|
|
@utils.hook("received.command.kick")
|
|
|
|
@utils.hook("received.command.k", alias_of="k")
|
|
|
|
@utils.kwarg("min_args", 1)
|
|
|
|
@utils.kwarg("require_mode", "o")
|
|
|
|
@utils.kwarg("require_access", "kick")
|
|
|
|
@utils.kwarg("usage", "<nickname> [reason]")
|
2016-04-18 15:50:19 +00:00
|
|
|
def kick(self, event):
|
2019-08-14 13:38:47 +00:00
|
|
|
self._kick(event["server"], event["target"], event["args_split"][0],
|
|
|
|
event["args_split"][1:])
|
2016-04-18 15:50:19 +00:00
|
|
|
|
2019-08-14 13:38:47 +00:00
|
|
|
def _format_hostmask(self, user, s):
|
2018-07-19 13:39:10 +00:00
|
|
|
return s.replace("$n", user.nickname).replace("$u", user.username
|
|
|
|
).replace("$h", user.hostname)
|
2019-08-14 13:38:47 +00:00
|
|
|
def _get_hostmask(self, channel, user):
|
|
|
|
format = channel.get_setting("ban-format", "*!$u@$h")
|
|
|
|
hostmask_split = [
|
|
|
|
self._format_hostmask(user, s) for s in format.split("$$")]
|
|
|
|
return "$".join(hostmask_split)
|
|
|
|
|
|
|
|
def _ban(self, server, channel, target, allow_hostmask, time, add):
|
|
|
|
hostmask = None
|
|
|
|
target_user = server.get_user(target, create=False)
|
|
|
|
if target_user and channel.has_user(target_user):
|
|
|
|
hostmask = self._get_hostmask(channel, target_user)
|
2018-07-19 13:39:10 +00:00
|
|
|
else:
|
2019-08-14 13:38:47 +00:00
|
|
|
if not allow_hostmask:
|
|
|
|
raise utils.EventError("No such user")
|
|
|
|
hostmask = target
|
|
|
|
if not add:
|
|
|
|
channel.send_unban(hostmask)
|
2018-09-30 12:28:26 +00:00
|
|
|
else:
|
2019-08-14 13:38:47 +00:00
|
|
|
channel.send_ban(hostmask)
|
2018-09-26 17:27:17 +00:00
|
|
|
|
2019-08-14 13:38:47 +00:00
|
|
|
if not time == None:
|
|
|
|
self.timers.add_persistent("unban", time, server_id=server.id,
|
|
|
|
channel_name=channel.name, hostmask=hostmask)
|
2019-06-15 17:42:14 +00:00
|
|
|
|
2019-08-14 13:38:47 +00:00
|
|
|
@utils.hook("timer.unban")
|
|
|
|
def _timer_unban(self, event):
|
|
|
|
server = self.bot.get_server_by_id(event["server_id"])
|
|
|
|
if server and event["channel_name"] in server.channels:
|
|
|
|
channel = server.channels.get(event["channel_name"])
|
|
|
|
channel.send_unban(event["hostmask"])
|
2019-06-14 16:23:22 +00:00
|
|
|
|
2019-08-14 13:38:47 +00:00
|
|
|
@utils.hook("received.command.ban")
|
|
|
|
@utils.kwarg("min_args", 1)
|
|
|
|
@utils.kwarg("require_mode", "o")
|
|
|
|
@utils.kwarg("require_access", "ban")
|
|
|
|
@utils.kwarg("usage", "[+time] <target>")
|
2016-04-18 15:50:19 +00:00
|
|
|
def ban(self, event):
|
2019-08-14 13:38:47 +00:00
|
|
|
time, args = self._parse_time(event["args_split"], 1)
|
|
|
|
self._ban(event["server"], event["target"], args[0], True, time, True)
|
|
|
|
|
|
|
|
@utils.hook("received.command.unban")
|
|
|
|
@utils.kwarg("min_args", 1)
|
|
|
|
@utils.kwarg("require_mode", "o")
|
|
|
|
@utils.kwarg("require_access", "ban")
|
|
|
|
@utils.kwarg("usage", "<target>")
|
2018-07-19 13:39:10 +00:00
|
|
|
def unban(self, event):
|
2019-08-14 13:38:47 +00:00
|
|
|
self._ban(event["server"], event["target"], event["args_split"][0],
|
|
|
|
True, None, False)
|
2016-04-18 15:50:19 +00:00
|
|
|
|
2019-08-14 13:38:47 +00:00
|
|
|
@utils.hook("received.command.kickban")
|
2018-10-10 09:42:41 +00:00
|
|
|
@utils.hook("received.command.kb", alias_of="kickban")
|
2019-08-14 13:38:47 +00:00
|
|
|
@utils.kwarg("min_args", 1)
|
|
|
|
@utils.kwarg("require_mode", "o")
|
|
|
|
@utils.kwarg("require_access", "kickban")
|
|
|
|
@utils.kwarg("usage", "[+time] <nickname> [reason]")
|
2016-04-18 15:50:19 +00:00
|
|
|
def kickban(self, event):
|
2019-08-14 13:38:47 +00:00
|
|
|
time, args = self._parse_time(event["args_split"], 1)
|
2019-08-14 13:41:29 +00:00
|
|
|
self._ban(event["server"], event["target"], args[0], False, time, True)
|
2019-08-14 13:38:47 +00:00
|
|
|
self._kick(event["server"], event["target"], args[0], args[1:])
|
|
|
|
|
|
|
|
@utils.hook("received.command.op")
|
|
|
|
@utils.hook("received.command.deop")
|
|
|
|
@utils.kwarg("channel_only", True)
|
|
|
|
@utils.kwarg("require_mode", "o")
|
|
|
|
@utils.kwarg("require_access", "op")
|
|
|
|
@utils.kwarg("usage", "[nickname]")
|
2016-04-22 10:48:28 +00:00
|
|
|
def op(self, event):
|
2019-08-14 13:38:47 +00:00
|
|
|
add = event.name == "received.command.op"
|
|
|
|
target = event["args_split"][0] if event["args"] else event[
|
|
|
|
"user"].nickname
|
|
|
|
event["target"].send_mode("+o" if add else "-o", target)
|
|
|
|
|
|
|
|
@utils.hook("received.command.voice")
|
|
|
|
@utils.hook("received.command.devoice")
|
|
|
|
@utils.kwarg("channel_only", True)
|
|
|
|
@utils.kwarg("require_mode", "o")
|
|
|
|
@utils.kwarg("require_access", "voice")
|
|
|
|
@utils.kwarg("usage", "[nickname]")
|
2019-08-30 11:26:52 +00:00
|
|
|
def voice(self, event):
|
2019-08-14 13:38:47 +00:00
|
|
|
add = event.name == "received.command.voice"
|
|
|
|
target = event["args_split"][0] if event["args"] else event[
|
|
|
|
"user"].nickname
|
|
|
|
event["target"].send_mode("+v" if add else "-v", target)
|
|
|
|
|
|
|
|
@utils.hook("received.command.topic")
|
|
|
|
@utils.kwarg("min_args", 1)
|
|
|
|
@utils.kwarg("channel_only", True)
|
|
|
|
@utils.kwarg("require_mode", "o")
|
|
|
|
@utils.kwarg("require_access", "topic")
|
|
|
|
@utils.kwarg("remove_empty", False)
|
|
|
|
@utils.kwarg("usage", "<topic>")
|
2018-09-06 16:25:38 +00:00
|
|
|
def topic(self, event):
|
|
|
|
event["target"].send_topic(event["args"])
|
2019-08-14 13:38:47 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.tappend")
|
|
|
|
@utils.kwarg("min_args", 1)
|
|
|
|
@utils.kwarg("channel_only", True)
|
|
|
|
@utils.kwarg("require_mode", "o")
|
|
|
|
@utils.kwarg("require_access", "topic")
|
|
|
|
@utils.kwarg("remove_empty", False)
|
|
|
|
@utils.kwarg("usage", "<topic>")
|
2018-09-06 16:25:38 +00:00
|
|
|
def tappend(self, event):
|
|
|
|
event["target"].send_topic(event["target"].topic + event["args"])
|
|
|
|
|
2019-06-27 19:52:32 +00:00
|
|
|
def _mute_method(self, server, user):
|
2019-06-27 19:38:39 +00:00
|
|
|
mask = "*!*@%s" % user.hostname
|
2019-06-27 19:52:32 +00:00
|
|
|
mute_method = server.get_setting("mute-method", "qmode").lower()
|
2019-06-27 19:38:39 +00:00
|
|
|
|
2019-06-27 19:52:32 +00:00
|
|
|
if mute_method == "qmode":
|
2019-06-27 19:38:39 +00:00
|
|
|
return "q", mask
|
2019-06-27 19:52:32 +00:00
|
|
|
elif mute_method == "insp":
|
2019-06-27 19:38:39 +00:00
|
|
|
return "b", "m:%s" % mask
|
2019-06-27 19:52:32 +00:00
|
|
|
elif mute_method == "unreal":
|
2019-06-27 19:38:39 +00:00
|
|
|
return "b", "~q:%s" % mask
|
2019-07-10 07:56:51 +00:00
|
|
|
elif mute_method == "none":
|
|
|
|
return None, None
|
2019-06-27 19:52:32 +00:00
|
|
|
raise ValueError("Unknown mute-method '%s'" % mute_method)
|
2019-06-27 19:38:39 +00:00
|
|
|
|
2019-08-14 14:07:48 +00:00
|
|
|
@utils.hook("received.command.mute", usage="[+time] <nickname>")
|
2019-06-27 19:52:32 +00:00
|
|
|
@utils.hook("received.command.unmute", usage="<nickname>")
|
2019-06-27 19:38:39 +00:00
|
|
|
@utils.kwarg("min_args", 1)
|
2019-06-27 20:09:26 +00:00
|
|
|
@utils.kwarg("channel_only", True)
|
|
|
|
@utils.kwarg("require_mode", "o")
|
|
|
|
@utils.kwarg("require_access", "mute")
|
2019-06-27 19:52:32 +00:00
|
|
|
@utils.kwarg("help", "Mute a given user")
|
|
|
|
def _mute(self, event):
|
|
|
|
add = event.name == "received.command.mute"
|
2019-08-14 14:07:48 +00:00
|
|
|
time, args = self._parse_time(event["args_split"], 1)
|
2019-06-27 19:38:39 +00:00
|
|
|
|
2019-08-14 14:07:48 +00:00
|
|
|
target_name = args[0]
|
2019-06-27 19:38:39 +00:00
|
|
|
if not event["server"].has_user(target_name):
|
|
|
|
raise utils.EventError("No such user")
|
|
|
|
|
|
|
|
target_user = event["server"].get_user(target_name)
|
|
|
|
if not event["target"].has_user(target_user):
|
|
|
|
raise utils.EventError("No such user")
|
|
|
|
|
2019-06-27 19:52:32 +00:00
|
|
|
mode, mask = self._mute_method(event["server"], target_user)
|
2019-07-10 07:56:51 +00:00
|
|
|
if mode == None:
|
|
|
|
raise utils.EventError("This network doesn't support mutes")
|
2019-06-27 19:52:32 +00:00
|
|
|
|
2019-08-14 14:07:48 +00:00
|
|
|
if add and time:
|
|
|
|
self.timers.add_persistent("unmute", time,
|
2019-06-27 19:52:32 +00:00
|
|
|
server_id=event["server"].id, channel_name=event["target"].name,
|
|
|
|
mode=mode, mask=mask)
|
|
|
|
|
2019-06-27 19:38:39 +00:00
|
|
|
mode_modifier = "+" if add else "-"
|
|
|
|
event["target"].send_mode("%s%s" % (mode_modifier, mode), [mask])
|
2019-06-27 19:52:32 +00:00
|
|
|
|
|
|
|
@utils.hook("timer.unmute")
|
|
|
|
def _timer_unmute(self, event):
|
|
|
|
server = self.bot.get_server_by_id(event["server_id"])
|
|
|
|
if server and event["channel_name"] in server.channels:
|
|
|
|
channel = server.channels.get(event["channel_name"])
|
|
|
|
channel.send_mode("-%s" % event["mode"], [event["mask"]])
|