2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-04-03 12:20:05 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.export("channelset", {"setting": "automode",
|
|
|
|
"help": "Disable/Enable automode", "validate": utils.bool_or_none})
|
2018-09-27 11:08:07 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-09-06 16:58:53 +00:00
|
|
|
_name = "AutoMode"
|
2016-04-03 12:23:47 +00:00
|
|
|
|
2018-10-11 10:20:53 +00:00
|
|
|
def _get_modes(self, channel, user):
|
2018-09-06 17:06:29 +00:00
|
|
|
identified_account = user.get_identified_account()
|
|
|
|
if identified_account and channel.get_setting("automode", False):
|
|
|
|
modes = channel.get_user_setting(user.get_id(), "automodes", [])
|
2018-10-11 10:20:53 +00:00
|
|
|
return modes
|
|
|
|
return []
|
|
|
|
def _check_modes(self, channel, user):
|
|
|
|
modes = self._get_modes(channel, user)
|
|
|
|
if modes:
|
|
|
|
channel.send_mode("+%s" % "".join(modes),
|
|
|
|
" ".join([user.nickname for mode in modes]))
|
2018-09-26 17:27:17 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.join")
|
2018-09-06 17:06:29 +00:00
|
|
|
def on_join(self, event):
|
|
|
|
self._check_modes(event["channel"], event["user"])
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.account")
|
2018-10-11 10:20:53 +00:00
|
|
|
@utils.hook("internal.identified")
|
2018-09-06 17:06:29 +00:00
|
|
|
def on_account(self, event):
|
|
|
|
for channel in event["user"].channels:
|
|
|
|
self._check_modes(channel, event["user"])
|
2018-09-06 16:58:53 +00:00
|
|
|
|
2018-10-11 10:20:53 +00:00
|
|
|
@utils.hook("received.command.syncmodes", channel_only=True)
|
|
|
|
def sync_modes(self, event):
|
|
|
|
"""
|
|
|
|
:help: Check/sync user modes
|
|
|
|
:require_mode: o
|
|
|
|
"""
|
|
|
|
modes = []
|
|
|
|
for user in event["target"].users:
|
|
|
|
user_modes = self._get_modes(event["target"], user)
|
|
|
|
for user_mode in user_modes:
|
|
|
|
if not event["target"].has_mode(user, user_mode):
|
|
|
|
modes.append([user_mode, user.nickname])
|
|
|
|
|
|
|
|
# break up in to chunks of (maximum) 3
|
|
|
|
# https://tools.ietf.org/html/rfc2812.html#section-3.2.3
|
|
|
|
mode_chunks = [modes[i:i+3] for i in range(0, len(modes), 3)]
|
|
|
|
for chunk in mode_chunks:
|
|
|
|
modes = [item[0] for item in chunk]
|
|
|
|
nicknames = [item[1] for item in chunk]
|
|
|
|
event["target"].send_mode(
|
|
|
|
"+%s" % "".join(modes), " ".join(nicknames))
|
|
|
|
|
2018-09-06 16:58:53 +00:00
|
|
|
def _add_mode(self, event, mode, mode_name):
|
|
|
|
target_user = event["server"].get_user(event["args_split"][0])
|
|
|
|
automodes = event["target"].get_user_setting(target_user.get_id(),
|
|
|
|
"automodes", [])
|
|
|
|
if mode in automodes:
|
|
|
|
event["stderr"].write("'%s' already has automode %s" % (
|
|
|
|
target_user.nickname, mode_name))
|
|
|
|
else:
|
2018-09-06 17:10:12 +00:00
|
|
|
automodes.append(mode)
|
2018-09-06 16:58:53 +00:00
|
|
|
event["target"].set_user_setting(target_user.get_id(), "automodes",
|
|
|
|
automodes)
|
2018-09-06 17:13:02 +00:00
|
|
|
if event["target"] in target_user.channels:
|
|
|
|
self._check_modes(event["target"], target_user)
|
|
|
|
|
2018-09-06 16:58:53 +00:00
|
|
|
event["stdout"].write("Added automode %s for '%s'" % (
|
|
|
|
mode_name, target_user.nickname))
|
|
|
|
def _remove_mode(self, event, mode, mode_name):
|
|
|
|
target_user = event["server"].get_user(event["args_split"][0])
|
|
|
|
automodes = event["target"].get_user_setting(target_user.get_id(),
|
|
|
|
"automodes", [])
|
|
|
|
if not mode in automodes:
|
|
|
|
event["stderr"].write("'%s' doesn't have automode %s" % (
|
|
|
|
target_user.nickname, mode_name))
|
|
|
|
else:
|
2018-09-06 17:10:12 +00:00
|
|
|
automodes.remove(mode)
|
2018-09-06 16:58:53 +00:00
|
|
|
event["target"].set_user_setting(target_user.get_id(), "automodes",
|
|
|
|
automodes)
|
|
|
|
event["stdout"].write("Removed automode %s from '%s'" % (
|
|
|
|
mode_name, target_user.nickname))
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.addop", min_args=1, channel_only=True)
|
2018-09-06 16:58:53 +00:00
|
|
|
def add_op(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Add a user to the auto-mode list as an op
|
|
|
|
:usage: <nickname>
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-06 16:58:53 +00:00
|
|
|
self._add_mode(event, "o", "op")
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.removeop", min_args=1, channel_only=True)
|
2018-09-06 16:58:53 +00:00
|
|
|
def remove_op(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Remove a user from the auto-mode list as an op
|
|
|
|
:usage: <nickname>
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-06 16:58:53 +00:00
|
|
|
self._remove_mode(event, "o", "op")
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.addvoice", min_args=1, channel_only=True)
|
2018-09-06 16:58:53 +00:00
|
|
|
def add_voice(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Add a user to the auto-mode list as a voice
|
|
|
|
:usage: <nickname>
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-06 16:58:53 +00:00
|
|
|
self._add_mode(event, "v", "voice")
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.removevoice", min_args=1, channel_only=True)
|
2018-09-06 16:58:53 +00:00
|
|
|
def remove_voice(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Remove a user from the auto-mode list as a voice
|
|
|
|
:usage: <nickname>
|
|
|
|
:require_mode: o
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-06 16:58:53 +00:00
|
|
|
self._remove_mode(event, "v", "voice")
|