Intercept JOINs and attach known keys (channel_keys.py)
This commit is contained in:
parent
bb47e7fa58
commit
cd15b06b65
2 changed files with 35 additions and 17 deletions
33
modules/channel_keys.py
Normal file
33
modules/channel_keys.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
from src import ModuleManager, utils
|
||||
|
||||
@utils.export("channelset", utils.Setting("key", "Channel key (password)",
|
||||
example="hunter2"))
|
||||
class Module(ModuleManager.BaseModule):
|
||||
def _get_key(self, server, channel_name):
|
||||
channel_id = server.channels.get_id(channel_name)
|
||||
return self.bot.database.channel_settings.get(channel_id, "key", None)
|
||||
|
||||
@utils.hook("preprocess.send.join")
|
||||
def preprocess_send_join(self, event):
|
||||
if event["line"].args:
|
||||
channels = event["line"].args[0].split(",")
|
||||
keys = event["line"].args[1:]
|
||||
|
||||
with_keys = {}
|
||||
for channel in channels:
|
||||
if keys:
|
||||
with_keys[channel] = keys.pop(0)
|
||||
else:
|
||||
with_keys[channel] = self._get_key(event["server"], channel)
|
||||
|
||||
channels_out = []
|
||||
keys_out = []
|
||||
|
||||
# sort such that channels with keys are at the start
|
||||
for channel_name, key in sorted(with_keys.items(),
|
||||
key=lambda item: not bool(item[1])):
|
||||
channels_out.append(channel_name)
|
||||
if key:
|
||||
keys_out.append(key)
|
||||
event["line"].args[0] = ",".join(channels_out)
|
||||
event["line"].args[1:] = keys_out
|
|
@ -4,23 +4,8 @@ class Module(ModuleManager.BaseModule):
|
|||
@utils.hook("received.001")
|
||||
def on_connect(self, event):
|
||||
channels = event["server"].get_setting("autojoin", [])
|
||||
if not channels:
|
||||
return
|
||||
|
||||
chan_keys = event["server"].get_setting("channel_keys", {})
|
||||
channels_sorted = sorted(channels,
|
||||
key=lambda x: 0 if x in chan_keys else 1)
|
||||
|
||||
keys_sorted = list(map(lambda x: x[1],
|
||||
sorted(chan_keys.items(),
|
||||
key=lambda x: channels_sorted.index(x[0]))))
|
||||
|
||||
for i in range(len(channels_sorted)):
|
||||
channel = channels_sorted[i]
|
||||
key = None if len(keys_sorted) <= i else keys_sorted[i]
|
||||
event["server"].attempted_join[channel] = key
|
||||
|
||||
event["server"].send_joins(channels_sorted, keys_sorted)
|
||||
if channels:
|
||||
event["server"].send_joins(channels)
|
||||
|
||||
@utils.hook("self.join")
|
||||
def on_join(self, event):
|
||||
|
|
Loading…
Reference in a new issue