Replace direct relays with "relay groups"

This commit is contained in:
jesopo 2019-07-01 17:48:43 +01:00
parent fda9869690
commit cda125e5b6

View file

@ -14,22 +14,24 @@ class Module(ModuleManager.BaseModule):
event["server"]._relay_ignore.remove(event["parsed_line"].id) event["server"]._relay_ignore.remove(event["parsed_line"].id)
return return
relays = self._get_relays(channel) relays = {}
for server_id, channel_name in relays: for relay_group in channel.get_setting("relay-groups", []):
server = self.bot.get_server_by_id(server_id) targets = self.bot.get_setting("relay-group-%s" % relay_group, [])
if not server == None and channel_name in server.channels: for server_id, channel_name in targets:
other_channel = server.channels.get(channel_name) server = self.bot.get_server_by_id(server_id)
if server and channel_name in server.channels:
if not self._has_relay_for(other_channel, event["server"].id, relay_channel = server.channels.get(channel_name)
channel.name): if (not server.id == event["server"].id and
self.log.warn( not channel.id == relay_channel.id):
"Tried to relay with one-way config: %s%s -> %s%s", if not server in relays:
[str(event["server"]), channel.name, str(server), relays[server] = []
other_channel.name]) if not relay_channel in relays[server]:
return relays[server].append(relay_channel)
for server in relays.keys():
for relay_channel in relays[server]:
relay_prefix_channel = "" relay_prefix_channel = ""
if not other_channel.name == channel.name: if not relay_channel.name == channel.name:
relay_prefix_channel = channel.name relay_prefix_channel = channel.name
server_name = utils.irc.color(str(event["server"]), server_name = utils.irc.color(str(event["server"]),
@ -38,7 +40,7 @@ class Module(ModuleManager.BaseModule):
relay_message = "[%s%s] %s" % (server_name, relay_message = "[%s%s] %s" % (server_name,
relay_prefix_channel, event["line"]) relay_prefix_channel, event["line"])
message = utils.irc.protocol.privmsg(other_channel.name, message = utils.irc.protocol.privmsg(relay_channel.name,
relay_message) relay_message)
server._relay_ignore.append(message.id) server._relay_ignore.append(message.id)
self.bot.trigger(self._send_factory(server, message)) self.bot.trigger(self._send_factory(server, message))
@ -46,14 +48,6 @@ class Module(ModuleManager.BaseModule):
def _send_factory(self, server, message): def _send_factory(self, server, message):
return lambda: server.send(message) return lambda: server.send(message)
def _has_relay_for(self, channel, server_id, channel_name):
relays = self._get_relays(channel)
for other_server_id, other_channel_name in relays:
if (other_server_id == server_id and
other_channel_name == channel_name):
return True
return False
@utils.hook("formatted.message.channel") @utils.hook("formatted.message.channel")
@utils.hook("formatted.notice.channel") @utils.hook("formatted.notice.channel")
@utils.hook("formatted.join") @utils.hook("formatted.join")
@ -71,55 +65,72 @@ class Module(ModuleManager.BaseModule):
for channel in event["user"].channels: for channel in event["user"].channels:
self._relay(event, channel) self._relay(event, channel)
@utils.hook("received.command.relay", min_args=3, channel_only=True) @utils.hook("received.command.relaygroup")
def relay(self, event): @utils.kwarg("min_args", 1)
""" @utils.kwarg("help", "Edit configured relay groups")
:help: Edit configured relays @utils.kwarg("usage", "list")
:usage: add <server> <channel> @utils.kwarg("usage", "add <name>")
:usage: remove <server> <channel> @utils.kwarg("usage", "remove <name>")
:permission: relay @utils.kwarg("usage", "join <name>")
""" @utils.kwarg("usage", "leave <name>")
target_server_alias = event["args_split"][1].lower() @utils.kwarg("permission", "relay")
target_server = self.bot.get_server_by_alias(target_server_alias) def relay_group(self, event):
if target_server == None:
raise utils.EventError("Unknown server provided")
current_relays = self._get_relays(event["target"])
target_server_relays = list(filter(
lambda relay: relay[0] == target_server.id, current_relays))
target_relays = list(map(lambda relay: relay[1], target_server_relays))
target_channel_name = target_server.irc_lower(event["args_split"][2])
changed = False
message = None
subcommand = event["args_split"][0].lower() subcommand = event["args_split"][0].lower()
if subcommand == "add":
if target_channel_name in target_relays:
raise utils.EventError("Already relaying to that channel")
if not target_channel_name in target_server.channels: group_settings = self.bot.find_settings_prefix("relay-group-")
raise utils.EventError("Cannot find the provided channel") groups = {}
for setting, value in group_settings:
name = setting.replace("relay-group-", "", 1)
groups[name] = value
current_relays.append((target_server.id, target_channel_name)) if subcommand == "list":
event["stdout"].write("Relay groups: %s" % ", ".join(groups.keys()))
return
message = "Relay added" if not len(event["args_split"]) > 1:
elif subcommand == "remove": raise utils.EventError("Please provide a group name")
if not target_channel_name in target_relays:
raise utils.EventError("I'm not relaying to that channel")
for i, (server_id, channel_name) in enumerate(current_relays): name = event["args_split"][1].lower()
if (server_id == target_server.id and
channel_name == target_channel_name):
current_relays.pop(i)
break
message = "Removed relay" event["check_assert"](utils.Check("is-channel"))
current_channel = [event["server"].id, event["target"].name]
channel_groups = event["target"].get_setting("relay-groups", [])
message = None
remove = False
if subcommand == "join":
if not name in groups:
groups[name] = []
if current_channel in groups[name] or name in channel_groups:
raise utils.EventError("Already joined group '%s'" % name)
groups[name].append(current_channel)
channel_groups.append(name)
message = "Joined"
elif subcommand == "leave":
if (not name in groups or
not current_channel in groups[name] or
not name in channel_groups):
raise utils.EventError("Not in group '%s'" % name)
groups[name].remove(current_channel)
channel_groups.remove(name)
message = "Left"
else: else:
raise utils.EventError("Unknown subcommand '%s'" % subcommand) raise utils.EventError("Unknown subcommand '%s'" % subcommand)
if not message == None:
if not groups[name]:
self.bot.del_setting("relay-group-%s" % name)
else:
self.bot.set_setting("relay-group-%s" % name, groups[name])
event["target"].set_setting("channel-relays", current_relays) if channel_groups:
event["stdout"].write(message) event["target"].set_setting("relay-groups", channel_groups)
else:
event["target"].del_setting("relay-groups")
event["stdout"].write("%s group '%s'" % (message, name))