2018-09-27 11:08:07 +00:00
|
|
|
from src import ModuleManager, Utils
|
2016-04-18 15:50:19 +00:00
|
|
|
|
2018-09-30 12:28:26 +00:00
|
|
|
class UserNotFoundException(Exception):
|
|
|
|
pass
|
|
|
|
class InvalidTimeoutException(Exception):
|
|
|
|
pass
|
|
|
|
|
2018-09-27 11:08:07 +00:00
|
|
|
@Utils.export("channelset", {"setting": "highlight-spam-threshold",
|
|
|
|
"help": "Set the number of nicknames in a message that qualifies as spam",
|
|
|
|
"validate": Utils.int_or_none})
|
|
|
|
@Utils.export("channelset", {"setting": "highlight-spam-protection",
|
|
|
|
"help": "Enable/Disable highlight spam protection",
|
|
|
|
"validate": Utils.bool_or_none})
|
|
|
|
@Utils.export("channelset", {"setting": "highlight-spam-ban",
|
|
|
|
"help": "Enable/Disable banning highlight spammers "
|
|
|
|
"instead of just kicking", "validate": Utils.bool_or_none})
|
|
|
|
@Utils.export("channelset", {"setting": "ban-format",
|
|
|
|
"help": "Set ban format ($n = nick, $u = username, $h = hostname)"})
|
|
|
|
class Module(ModuleManager.BaseModule):
|
2016-04-18 15:50:19 +00:00
|
|
|
_name = "Channel Op"
|
|
|
|
|
2018-09-30 12:28:26 +00:00
|
|
|
@Utils.hook("timer.unban")
|
|
|
|
def _timer_unban(self, event):
|
|
|
|
server = self.bot.get_server(event["server_id"])
|
|
|
|
if server.has_channel(event["channel_name"]):
|
|
|
|
channel = server.get_channel(event["channel_name"])
|
|
|
|
channel.send_unban(event["hostmask"])
|
|
|
|
|
|
|
|
def _kick(self, server, channel, nickname, reason):
|
|
|
|
target_user = server.get_user(nickname)
|
|
|
|
if channel.has_user(target_user):
|
|
|
|
channel.send_kick(nickname, reason)
|
|
|
|
else:
|
|
|
|
raise UserNotFoundException("That user is not in this channel")
|
|
|
|
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.kick|k", channel_only=True, min_args=1)
|
2016-04-18 15:50:19 +00:00
|
|
|
def kick(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Kick a user from the current channel
|
|
|
|
:usage: <nickname> [reason]
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-04-18 15:50:19 +00:00
|
|
|
target = event["args_split"][0]
|
2018-09-30 12:28:26 +00:00
|
|
|
reason = " ".join(event["args_split"][1:]) or None
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._kick(event["server"], event["target"], target, reason)
|
|
|
|
except UserNotFoundException:
|
|
|
|
event["stderr"].set_prefix("Kick")
|
|
|
|
event["stderr"].write(str(e))
|
2016-04-18 15:50:19 +00:00
|
|
|
|
2018-07-19 13:39:10 +00:00
|
|
|
def _ban_format(self, user, s):
|
|
|
|
return s.replace("$n", user.nickname).replace("$u", user.username
|
|
|
|
).replace("$h", user.hostname)
|
2018-09-30 12:28:26 +00:00
|
|
|
def _ban_user(self, channel, ban, user):
|
|
|
|
if channel.has_user(user):
|
|
|
|
format = channel.get_setting("ban-format", "*!$u@$h")
|
|
|
|
hostmask_split = format.split("$$")
|
|
|
|
hostmask_split = [self._ban_format(user, s) for s in hostmask_split]
|
|
|
|
hostmask = "".join(hostmask_split)
|
|
|
|
if ban:
|
|
|
|
channel.send_ban(hostmask)
|
|
|
|
else:
|
|
|
|
channel.send_unban(hostmask)
|
|
|
|
return hostmask
|
2018-07-19 13:39:10 +00:00
|
|
|
else:
|
2018-09-30 12:28:26 +00:00
|
|
|
raise UserNotFoundException("That user is not in this channel")
|
|
|
|
|
|
|
|
def _ban(self, server, channel, ban, target):
|
|
|
|
target_user = server.get_user(target)
|
|
|
|
if channel.has_user(target_user):
|
|
|
|
return this._ban_user(channel, ban, target_user)
|
|
|
|
else:
|
|
|
|
if ban:
|
|
|
|
event["target"].send_ban(target)
|
|
|
|
else:
|
|
|
|
event["target"].send_unban(target)
|
|
|
|
return target
|
2018-09-26 17:27:17 +00:00
|
|
|
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.ban", channel_only=True, min_args=1)
|
2016-04-18 15:50:19 +00:00
|
|
|
def ban(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Ban a user/hostmask from the current channel
|
|
|
|
:usage: <nickname/hostmask>
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 12:28:26 +00:00
|
|
|
self._ban(event["server"], event["target"], True,
|
|
|
|
event["args_split"][0])
|
|
|
|
|
|
|
|
def _temp_ban(self, event, accept_hostmask):
|
|
|
|
timeout = Utils.from_pretty_time(event["args_split"][1])
|
|
|
|
if not timeout:
|
|
|
|
raise InvalidTimeoutException(
|
|
|
|
"Please provided a valid time above 0 seconds")
|
|
|
|
|
|
|
|
if accept_hostmask:
|
|
|
|
hostmask = self._ban(event["server"], event["target"], True,
|
|
|
|
event["args_split"][0])
|
2016-04-18 15:50:19 +00:00
|
|
|
else:
|
2018-09-30 12:28:26 +00:00
|
|
|
hostmask = self._ban_user(event["target"], True,
|
|
|
|
event["server"].get_user(event["args_split"][0]))
|
|
|
|
|
|
|
|
self.bot.timers.add_persistent("unban", timeout,
|
|
|
|
server_id=event["server"].id,
|
|
|
|
channel_name=event["target"].name, hostmask=hostmask)
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.tempban|tb", channel_only=True, min_args=2)
|
2018-09-30 12:28:26 +00:00
|
|
|
def temp_ban(self, event):
|
2018-09-30 16:29:09 +00:00
|
|
|
"""
|
|
|
|
:help: Temporarily ban someone from the current channel
|
|
|
|
:usage: <nickname/hostmask>
|
|
|
|
:require_mode: o
|
|
|
|
"""
|
2018-09-30 12:28:26 +00:00
|
|
|
try:
|
|
|
|
self._temp_ban(event, True)
|
|
|
|
except InvalidTimeoutException as e:
|
|
|
|
event["stderr"].set_prefix("Tempban")
|
|
|
|
event["stderr"].write(str(e))
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.tempkickban|tkb", channel_only=True,
|
|
|
|
min_args=2)
|
2018-09-30 12:28:26 +00:00
|
|
|
def temp_kick_ban(self, event):
|
2018-09-30 16:29:09 +00:00
|
|
|
"""
|
|
|
|
:help: Temporarily kick and ban someone from the current channel
|
|
|
|
:usage: <nickname>
|
|
|
|
:require_mode: o
|
|
|
|
"""
|
2018-09-30 12:28:26 +00:00
|
|
|
reason = " ".join(event["args_split"][2:]) or None
|
|
|
|
event["stderr"].set_prefix("TKB")
|
|
|
|
try:
|
|
|
|
self._temp_ban(event, False)
|
|
|
|
self._kick(event["server"], event["target"], event["args_split"][0],
|
|
|
|
reason)
|
|
|
|
except InvalidTimeoutException as e:
|
|
|
|
event["stderr"].write(str(e))
|
|
|
|
except UserNotFoundException as e:
|
|
|
|
event["stderr"].write(str(e))
|
|
|
|
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.unban", channel_only=True, min_args=1)
|
2018-07-19 13:39:10 +00:00
|
|
|
def unban(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Unban a user/hostmask from the current channel
|
|
|
|
:usage: <nickname/hostmask>
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 12:28:26 +00:00
|
|
|
self._ban(event["server"], event["target"], False,
|
|
|
|
event["args_split"][0])
|
2016-04-18 15:50:19 +00:00
|
|
|
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.kickban|kb", channel_only=True, min_args=1)
|
2016-04-18 15:50:19 +00:00
|
|
|
def kickban(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Kick and ban a user from the current channel
|
|
|
|
:usage: <nickname> [reason]
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 12:28:26 +00:00
|
|
|
target = event["args_split"][0]
|
|
|
|
reason = " ".join(event["args_split"][1:]) or None
|
|
|
|
try:
|
|
|
|
self._ban(event["server"], event["target"], True, target)
|
|
|
|
self._kick(event["server"], event["target"], target, reason)
|
|
|
|
except UserNotFoundException as e:
|
|
|
|
event["stderr"].set_prefix("Kickban")
|
|
|
|
event["stderr"].write(str(e))
|
2016-04-22 10:48:28 +00:00
|
|
|
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.op", channel_only=True)
|
2016-04-22 10:48:28 +00:00
|
|
|
def op(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Op a user in the current channel
|
|
|
|
:usage: [nickname]
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-07-05 11:17:22 +00:00
|
|
|
target = event["user"].nickname if not event["args_split"] else event[
|
|
|
|
"args_split"][0]
|
|
|
|
event["target"].send_mode("+o", target)
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.deop", channel_only=True)
|
2016-05-17 13:51:47 +00:00
|
|
|
def deop(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Remove op from a user in the current channel
|
|
|
|
:usage: [nickname]
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-07-05 11:17:22 +00:00
|
|
|
target = event["user"].nickname if not event["args_split"] else event[
|
|
|
|
"args_split"][0]
|
|
|
|
event["target"].send_mode("-o", target)
|
2018-09-26 17:27:17 +00:00
|
|
|
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.voice", channel_only=True)
|
2016-05-17 13:51:47 +00:00
|
|
|
def voice(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Voice a user in the current channel
|
|
|
|
:usage: [nickname]
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-07-05 11:17:22 +00:00
|
|
|
target = event["user"].nickname if not event["args_split"] else event[
|
|
|
|
"args_split"][0]
|
|
|
|
event["target"].send_mode("+v", target)
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.devoice", channel_only=True)
|
2016-05-17 13:51:47 +00:00
|
|
|
def devoice(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Remove voice from a user in the current channel
|
|
|
|
:usage: [nickname]
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-07-05 11:17:22 +00:00
|
|
|
target = event["user"].nickname if not event["args_split"] else event[
|
|
|
|
"args_split"][0]
|
|
|
|
event["target"].send_mode("-v", target)
|
2017-12-26 11:50:18 +00:00
|
|
|
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.topic", min_args=1, channel_only=True)
|
2018-09-06 16:25:38 +00:00
|
|
|
def topic(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Set the topic in the current channel
|
|
|
|
:usage: <topic>
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-06 16:25:38 +00:00
|
|
|
event["target"].send_topic(event["args"])
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.tappend", min_args=1, channel_only=True)
|
2018-09-06 16:25:38 +00:00
|
|
|
def tappend(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Append to the topic in the current channel
|
|
|
|
:usage: <topic>
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-06 16:25:38 +00:00
|
|
|
event["target"].send_topic(event["target"].topic + event["args"])
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.message.channel")
|
2017-12-26 11:50:18 +00:00
|
|
|
def highlight_spam(self, event):
|
2018-08-06 13:27:49 +00:00
|
|
|
if event["channel"].get_setting("highlight-spam-protection", False):
|
|
|
|
nicknames = list(map(lambda user: user.nickname,
|
|
|
|
event["channel"].users)) + [event["server"].nickname]
|
|
|
|
|
2018-08-30 10:14:19 +00:00
|
|
|
highlights = set(nicknames) & set(event["message_split"])
|
|
|
|
if len(highlights) > 1 and len(highlights) >= event["channel"
|
|
|
|
].get_setting("highlight-spam-threshold", 10):
|
2018-08-06 13:27:49 +00:00
|
|
|
has_mode = event["channel"].mode_or_above(event["user"], "v")
|
|
|
|
should_ban = event["channel"].get_setting("highlight-spam-ban",
|
|
|
|
False)
|
|
|
|
if not has_mode:
|
|
|
|
if should_ban:
|
|
|
|
event["channel"].send_ban("*!%s@%s" % (
|
|
|
|
event["user"].username, event["user"].hostname))
|
|
|
|
event["channel"].send_kick(event["user"].nickname,
|
|
|
|
"highlight spam detected")
|