2018-07-15 22:56:06 +00:00
|
|
|
import base64
|
|
|
|
|
|
|
|
class Module(object):
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2018-07-15 22:56:06 +00:00
|
|
|
self.bot = bot
|
2018-09-03 10:14:27 +00:00
|
|
|
events.on("received.cap.ls").hook(self.on_cap)
|
|
|
|
events.on("received.cap.ack").hook(self.on_cap_ack)
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("received.authenticate").hook(self.on_authenticate)
|
2018-09-07 15:13:49 +00:00
|
|
|
events.on("received.numeric.903").hook(self.sasl_success)
|
2018-09-17 11:56:41 +00:00
|
|
|
events.on("received.numeric.904").hook(self.sasl_failure)
|
2018-07-15 22:56:06 +00:00
|
|
|
|
2018-09-09 09:04:21 +00:00
|
|
|
exports.add("serverset", {"setting": "sasl",
|
|
|
|
"help": "Set the sasl username/password for this server",
|
|
|
|
"validate": self._validate})
|
|
|
|
|
|
|
|
def _validate(self, s):
|
2018-09-17 10:31:40 +00:00
|
|
|
mechanism = s
|
2018-09-09 09:04:21 +00:00
|
|
|
if " " in s:
|
2018-09-17 10:31:40 +00:00
|
|
|
mechanism, arguments = s.split(" ", 1)
|
|
|
|
return {"mechanism": mechanism, "args": arguments}
|
2018-09-09 09:04:21 +00:00
|
|
|
|
2018-07-15 22:56:06 +00:00
|
|
|
def on_cap(self, event):
|
2018-09-07 14:51:41 +00:00
|
|
|
has_sasl = "sasl" in event["capabilities"]
|
2018-09-17 10:49:23 +00:00
|
|
|
our_sasl = event["server"].get_setting("sasl", None)
|
2018-09-07 14:51:41 +00:00
|
|
|
|
2018-09-17 10:49:23 +00:00
|
|
|
do_sasl = False
|
|
|
|
if has_sasl and our_sasl:
|
|
|
|
if not event["capabilities"]["sasl"] == None:
|
|
|
|
our_mechanism = our_sasl["mechanism"].upper()
|
|
|
|
do_sasl = our_mechanism in event["capabilities"
|
|
|
|
]["sasl"].split(",")
|
|
|
|
else:
|
|
|
|
do_sasl = False
|
|
|
|
|
|
|
|
if do_sasl:
|
2018-09-03 10:14:27 +00:00
|
|
|
event["server"].queue_capability("sasl")
|
2018-09-07 14:51:41 +00:00
|
|
|
|
2018-09-03 10:14:27 +00:00
|
|
|
def on_cap_ack(self, event):
|
|
|
|
if "sasl" in event["capabilities"]:
|
2018-09-17 10:31:40 +00:00
|
|
|
sasl = event["server"].get_setting("sasl")
|
|
|
|
event["server"].send_authenticate(sasl["mechanism"].upper())
|
2018-09-03 10:14:27 +00:00
|
|
|
event["server"].wait_for_capability("sasl")
|
2018-07-15 22:56:06 +00:00
|
|
|
|
|
|
|
def on_authenticate(self, event):
|
|
|
|
if event["message"] != "+":
|
|
|
|
event["server"].send_authenticate("*")
|
|
|
|
else:
|
2018-09-17 10:31:40 +00:00
|
|
|
sasl = event["server"].get_setting("sasl")
|
|
|
|
mechanism = sasl["mechanism"].upper()
|
|
|
|
|
|
|
|
if mechanism == "PLAIN":
|
|
|
|
sasl_nick, sasl_pass = sasl["args"].split(":", 1)
|
|
|
|
auth_text = "%s\0%s\0%s" % (sasl_nick, sasl_nick, sasl_pass)
|
|
|
|
elif mechanism == "EXTERNAL":
|
|
|
|
auth_text = "+"
|
|
|
|
else:
|
|
|
|
raise ValueError("unknown sasl mechanism '%s'" % mechanism)
|
|
|
|
|
|
|
|
if not auth_text == "+":
|
|
|
|
auth_text = base64.b64encode(auth_text.encode("utf8"))
|
|
|
|
auth_text = auth_text.decode("utf8")
|
2018-07-15 22:56:06 +00:00
|
|
|
event["server"].send_authenticate(auth_text)
|
2018-09-07 15:04:37 +00:00
|
|
|
|
2018-09-17 11:56:41 +00:00
|
|
|
def _end_sasl(self, server):
|
|
|
|
server.capability_done("sasl")
|
2018-09-07 15:04:37 +00:00
|
|
|
def sasl_success(self, event):
|
2018-09-17 11:56:41 +00:00
|
|
|
self._end_sasl(event["server"])
|
|
|
|
def sasl_success(self, event):
|
|
|
|
self._end_sasl(event["server"])
|