aboutsummaryrefslogtreecommitdiff
path: root/modules/resume.py
blob: 0f074642a8f86016877f4ad41ba6313325174cfc (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from src import ModuleManager, utils

CAP = "draft/resume-0.2"

class Module(ModuleManager.BaseModule):
    def _get_token(self, server):
        return server.connection_params.args.get("resume", [None, None])

    @utils.hook("received.cap.ls")
    def on_cap_new(self, event):
        username, token = self._get_token(event["server"])
        if CAP in event["capabilities"] and (not username or not token):
            event["server"].queue_capability(CAP)

    @utils.hook("received.cap.ack")
    def on_cap_ack(self, event):
        username, token = self._get_token(event["server"])
        if CAP in event["capabilities"] and username and token:
            event["server"].send("RESUME %s %s" % (username, token))
            event["server"].cap_started = False

    @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":
            event["server"].connection_params.args["new-token"
                ] = event["args"][1]

    @utils.hook("received.numeric.001")
    def on_connect(self, event):
        new_token = event["server"].connection_params.args.get("new-token",
            None)
        if new_token:
            event["server"].connection_params.args["resume"] = [
                event["server"].nickname, new_token]
            del event["server"].connection_params.args["resume"]