move sasl logic to it's own module

This commit is contained in:
jesopo 2018-07-15 23:56:06 +01:00
parent 2b30ffc71b
commit 2012a388da
4 changed files with 60 additions and 65 deletions

View file

@ -64,9 +64,9 @@ class Server(object):
if self.password:
self.send_pass(self.password)
# In principle, this belongs in the NS module. In reality, it's more practical to put this
# One-off case here for SASL
if "Nickserv" in self.bot.modules.modules and self.get_setting("nickserv-password"):
sasl = self.get_setting("sasl")
if sasl:
self.send_capability_request("sasl")
self.send_user(self.original_username, self.original_realname)
@ -234,7 +234,6 @@ class Server(object):
] in self.attempted_join:
self.send_join(event["channel_name"], event["key"])
def send_join(self, channel_name, key=None):
self.attempted_join[channel_name.lower()] = key
self.send("JOIN %s%s" % (channel_name,
"" if key == None else " %s" % key))
def send_part(self, channel_name, reason=None):

View file

@ -2,36 +2,34 @@
class Module(object):
def __init__(self, bot):
bot.events.on("self").on("part").hook(self.on_self_part)
bot.events.on("self").on("join").hook(self.on_join)
bot.events.on("self").on("kick").hook(self.on_kick)
bot.events.on("received").on("numeric").on("366").hook(
self.on_identify_trigger)
bot.events.on("received").on("numeric").on("001").hook(
self.on_identify_trigger)
bot.events.on("received.numeric.001").hook(
self.on_connect)
bot.events.on("self.join").hook(self.on_join)
bot.events.on("self.kick").hook(self.on_kick)
def on_self_part(self, event):
pass
def on_connect(self, event):
channels = event["server"].get_setting("autojoin", [])
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_join(
",".join(channels_sorted), ",".join(keys_sorted))
def on_join(self, event):
channels = set(event["server"].get_setting("autojoin", []))
channels.add(event["channel"].name)
event["server"].set_setting("autojoin", list(channels))
def on_identify_trigger(self, event):
if event["number"]=="001" and not event["server"].sasl_success: return
if event["line_split"][3].lower() == "#bitbot" or event["number"]=="001":
channels = event["server"].get_setting("autojoin", [])
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 = map(lambda x: x[1],
sorted(chan_keys.items(),
key=lambda x: channels_sorted.index(x[0])))
event["server"].send_join(
",".join(channels_sorted), ",".join(keys_sorted))
def on_kick(self, event):
channels = event["server"].get_setting("autojoin", [])
if event["channel"].name in channels:

View file

@ -1,27 +1,19 @@
import base64
import EventManager
class Module(object):
def __init__(self, bot):
bot.events.on("new").on("server").hook(self.on_new_server)
bot.events.on("received").on("numeric").on("001"
).hook(self.on_connect)
).hook(self.on_connect, priority=EventManager.PRIORITY_URGENT)
bot.events.on("received").on("command").on("setnickserv"
).hook(self.set_nickserv, min_args=1, permission="setnickserv",
help="Set bot's nickserv password", usage="<password>",
private_only=True)
bot.events.on("received").on("cap").hook(self.on_cap)
bot.events.on("received").on("authenticate").hook(self.on_authenticate)
for code in ["902", "903", "904", "905", "906", "907", "908"]:
bot.events.on("received").on("numeric").on(code).hook(self.on_90x)
def on_new_server(self, event):
event["server"].attempted_auth = False
event["server"].sasl_success = False
def on_connect(self, event):
nickserv_password = event["server"].get_setting(
"nickserv-password")
if nickserv_password and not event["server"].sasl_success:
if nickserv_password:
event["server"].attempted_auth = True
event["server"].send_message("nickserv",
"identify %s" % nickserv_password)
@ -30,30 +22,3 @@ class Module(object):
nickserv_password = event["args"]
event["server"].set_setting("nickserv-password", nickserv_password)
event["stdout"].write("Nickserv password saved")
def on_cap(self, event):
if event["subcommand"] == "NAK":
event["server"].send_capability_end()
elif event["subcommand"] == "ACK":
if not "sasl" in event["capabilities"]:
event["server"].send_capability_end()
else:
event["server"].send_authenticate("PLAIN")
else:
pass
def on_authenticate(self, event):
if event["message"] != "+":
event["server"].send_authenticate("*")
else:
nick = event["server"].original_nickname
password = event["server"].get_setting("nickserv-password")
event["server"].attempted_auth = True
event["server"].send_authenticate(
base64.b64encode(("%s\0%s\0%s" % (nick, nick, password)).encode("utf8")).decode("utf8")
)
def on_90x(self, event):
if event["number"]=="903":
event["server"].sasl_success = True
event["server"].send_capability_end()

33
modules/sasl.py Normal file
View file

@ -0,0 +1,33 @@
import base64
class Module(object):
def __init__(self, bot):
self.bot = bot
bot.events.on("received").on("cap").hook(self.on_cap)
bot.events.on("received").on("authenticate").hook(self.on_authenticate)
bot.events.on("received").on("numeric").on(
"902", "903", "904", "905", "906", "907", "908").hook(self.on_90x)
def on_cap(self, event):
if event["subcommand"] == "NAK":
event["server"].send_capability_end()
elif event["subcommand"] == "ACK":
if not "sasl" in event["capabilities"]:
event["server"].send_capability_end()
else:
event["server"].send_authenticate("PLAIN")
def on_authenticate(self, event):
if event["message"] != "+":
event["server"].send_authenticate("*")
else:
sasl_nick, sasl_pass = event["server"].get_setting("sasl")
auth_text = "%s\0%s\0%s" % (
sasl_nick, sasl_nick, sasl_pass)
auth_text = base64.b64encode(auth_text.encode("utf8"))
auth_text = auth_text.decode("utf8")
event["server"].send_authenticate(auth_text)
def on_90x(self, event):
event["server"].send_capability_end()