bitbot-3.11-fork/modules/relay.py

147 lines
5.6 KiB
Python
Raw Normal View History

2019-07-01 17:22:49 +00:00
#--depends-on commands
#--depends-on format_activity
#--depends-on permissions
from src import EventManager, ModuleManager, utils
2019-06-20 17:08:59 +00:00
@utils.export("channelset", utils.BoolSetting("relay-extras",
"Whether or not to relay joins/parts/quits/modes/etc"))
2019-06-20 17:08:59 +00:00
class Module(ModuleManager.BaseModule):
@utils.hook("new.server")
def new_server(self, event):
event["server"]._relay_ignore = []
2019-06-20 17:08:59 +00:00
def _get_relays(self, channel):
return channel.get_setting("channel-relays", [])
def _relay(self, event, channel):
if ("parsed_line" in event and
event["parsed_line"].id in event["server"]._relay_ignore):
event["server"]._relay_ignore.remove(event["parsed_line"].id)
2019-06-20 17:08:59 +00:00
return
relays = {}
for relay_group in channel.get_setting("relay-groups", []):
targets = self.bot.get_setting("relay-group-%s" % relay_group, [])
for server_id, channel_name in targets:
server = self.bot.get_server_by_id(server_id)
if server and channel_name in server.channels:
relay_channel = server.channels.get(channel_name)
if not channel.id == relay_channel.id:
if not server in relays:
relays[server] = []
if not relay_channel in relays[server]:
relays[server].append(relay_channel)
for server in relays.keys():
for relay_channel in relays[server]:
2019-06-20 17:08:59 +00:00
relay_prefix_channel = ""
if not relay_channel.name == channel.name:
2019-06-20 17:08:59 +00:00
relay_prefix_channel = channel.name
2019-06-30 10:42:59 +00:00
server_name = utils.irc.color(str(event["server"]),
utils.consts.LIGHTBLUE)
server_name = "%s%s" % (server_name, utils.consts.RESET)
relay_message = "[%s%s] %s" % (server_name,
relay_prefix_channel, event["minimal"])
2019-10-28 10:56:33 +00:00
self.bot.trigger(self._send_factory(server, relay_channel.name,
relay_message))
2019-10-28 10:56:33 +00:00
def _send_factory(self, server, channel_name, message):
def _():
2019-10-28 13:56:04 +00:00
line = server.send_message(channel_name, message)
server._relay_ignore.append(line.parsed_line.id)
2019-10-28 10:56:33 +00:00
return _
2019-06-20 17:08:59 +00:00
@utils.hook("formatted.message.channel")
@utils.hook("formatted.notice.channel")
@utils.kwarg("priority", EventManager.PRIORITY_LOW)
def formatted(self, event):
self._relay(event, event["channel"])
2019-06-20 17:08:59 +00:00
@utils.hook("formatted.join")
@utils.hook("formatted.part")
@utils.hook("formatted.nick")
@utils.hook("formatted.mode.channel")
@utils.hook("formatted.kick")
@utils.hook("formatted.quit")
@utils.hook("formatted.rename")
@utils.kwarg("priority", EventManager.PRIORITY_LOW)
def formatted_extra(self, event):
2019-06-20 17:08:59 +00:00
if event["channel"]:
2019-09-02 15:16:28 +00:00
if event["channel"].get_setting("relay-extras", False):
self._relay(event, event["channel"])
2019-06-20 17:08:59 +00:00
elif event["user"]:
for channel in event["user"].channels:
2019-09-02 15:16:28 +00:00
if channel.get_setting("relay-extras", False):
self._relay(event, channel)
2019-06-20 17:08:59 +00:00
@utils.hook("received.command.relaygroup")
@utils.kwarg("min_args", 1)
@utils.kwarg("help", "Edit configured relay groups")
@utils.kwarg("usage", "list")
@utils.kwarg("usage", "join <name>")
@utils.kwarg("usage", "leave <name>")
@utils.kwarg("permission", "relay")
def relay_group(self, event):
subcommand = event["args_split"][0].lower()
2019-06-20 17:08:59 +00:00
group_settings = self.bot.find_settings(prefix="relay-group-")
groups = {}
for setting, value in group_settings:
name = setting.replace("relay-group-", "", 1)
groups[name] = value
2019-06-20 17:08:59 +00:00
if subcommand == "list":
event["stdout"].write("Relay groups: %s" % ", ".join(groups.keys()))
return
2019-06-20 17:08:59 +00:00
if not len(event["args_split"]) > 1:
raise utils.EventError("Please provide a group name")
2019-06-20 17:08:59 +00:00
name = event["args_split"][1].lower()
2019-06-20 17:08:59 +00:00
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
2019-06-20 17:08:59 +00:00
if subcommand == "join":
if not name in groups:
groups[name] = []
2019-06-20 17:08:59 +00:00
if current_channel in groups[name] or name in channel_groups:
raise utils.EventError("Already joined group '%s'" % name)
2019-06-20 17:08:59 +00:00
groups[name].append(current_channel)
channel_groups.append(name)
message = "Joined"
2019-06-20 17:08:59 +00:00
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)
2019-06-20 17:08:59 +00:00
groups[name].remove(current_channel)
channel_groups.remove(name)
message = "Left"
2019-06-20 17:08:59 +00:00
else:
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])
if channel_groups:
event["target"].set_setting("relay-groups", channel_groups)
else:
event["target"].del_setting("relay-groups")
2019-06-20 17:08:59 +00:00
event["stdout"].write("%s group '%s'" % (message, name))