2018-11-08 17:09:53 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
|
|
|
CAP = "draft/resume-0.2"
|
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
def _get_token(self, server):
|
2019-02-09 12:33:26 +00:00
|
|
|
return server.connection_params.args.get("resume-token", (None, None))
|
|
|
|
def _set_token(Self, server, nickname, token):
|
|
|
|
server.connection_params.args["resume-token"] = (nickname, token)
|
2018-11-08 17:09:53 +00:00
|
|
|
|
|
|
|
@utils.hook("received.cap.ls")
|
2019-02-09 12:27:13 +00:00
|
|
|
def on_cap_ls(self, event):
|
2019-02-09 12:33:26 +00:00
|
|
|
nickname, token = self._get_token(event["server"])
|
|
|
|
if CAP in event["capabilities"] and (not nickname or not token):
|
2018-11-09 11:15:07 +00:00
|
|
|
event["server"].queue_capability(CAP)
|
2018-11-08 17:09:53 +00:00
|
|
|
|
|
|
|
@utils.hook("received.cap.ack")
|
|
|
|
def on_cap_ack(self, event):
|
2019-02-09 12:33:26 +00:00
|
|
|
nickname, token = self._get_token(event["server"])
|
|
|
|
if CAP in event["capabilities"] and nickname and token:
|
|
|
|
event["server"].send("RESUME %s %s" % (nickname, token))
|
2018-11-09 10:43:05 +00:00
|
|
|
event["server"].cap_started = False
|
2018-11-08 17:09:53 +00:00
|
|
|
|
|
|
|
@utils.hook("received.resume")
|
|
|
|
def on_resume(self, event):
|
|
|
|
if event["args"][0] == "SUCCESS":
|
|
|
|
self.log.info("Successfully resumed session")
|
|
|
|
elif event["args"][0] == "ERR":
|
|
|
|
self.log.info("Failed to resume session: %s" % event["args"][1])
|
|
|
|
elif event["args"][0] == "TOKEN":
|
2019-02-09 12:27:37 +00:00
|
|
|
event["server"].connection_params.args["new-resume-token"
|
2018-11-08 17:09:53 +00:00
|
|
|
] = event["args"][1]
|
|
|
|
|
|
|
|
@utils.hook("received.numeric.001")
|
|
|
|
def on_connect(self, event):
|
2019-02-09 12:23:37 +00:00
|
|
|
new_token = event["server"].connection_params.args.get(
|
|
|
|
"new-resume-token", None)
|
2018-11-08 17:09:53 +00:00
|
|
|
if new_token:
|
2019-02-09 12:33:26 +00:00
|
|
|
self._set_token(event["server"], event["server"].nickname,
|
|
|
|
new_token)
|
2019-02-09 12:23:37 +00:00
|
|
|
del event["server"].connection_params.args["new-resume-token"]
|
2019-02-09 12:33:26 +00:00
|
|
|
|
|
|
|
@utils.hook("self.nick")
|
|
|
|
def nick_change(self, event):
|
|
|
|
nickname, token = self._get_token(event["server"])
|
|
|
|
if nickname and token:
|
|
|
|
self._set_token(event["server"], event["new_nickname"], token)
|