2019-06-03 11:45:18 +00:00
|
|
|
#--depends-on ircv3_server_time
|
2019-05-29 14:53:20 +00:00
|
|
|
|
2018-11-08 17:09:53 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2019-06-09 15:18:25 +00:00
|
|
|
CAP = utils.irc.Capability(None, "draft/resume-0.5")
|
2018-11-08 17:09:53 +00:00
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-02-12 11:14:36 +00:00
|
|
|
def _setting(self, new):
|
|
|
|
return "resume-token%s" % ("-new" if new else "")
|
|
|
|
def _get_token(self, server, new=False):
|
|
|
|
return server.get_setting(self._setting(new), None)
|
|
|
|
def _set_token(self, server, token, new=False):
|
|
|
|
server.set_setting(self._setting(new), token)
|
|
|
|
def _del_token(self, server, new=False):
|
|
|
|
server.del_setting(self._setting(new))
|
|
|
|
|
2018-11-08 17:09:53 +00:00
|
|
|
|
2019-02-15 10:26:33 +00:00
|
|
|
@utils.hook("new.server")
|
|
|
|
def new_server(self, event):
|
2019-05-29 14:53:20 +00:00
|
|
|
# we need to pull this before any data has been exchanged - to make sure
|
|
|
|
# it's not overwritten from the last connection
|
|
|
|
event["server"]._resume_timestamp = event["server"].get_setting(
|
|
|
|
"last-server-time", None)
|
2019-02-15 10:26:33 +00:00
|
|
|
|
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-05-22 07:34:52 +00:00
|
|
|
if CAP.available(event["capabilities"]):
|
2019-05-19 10:01:44 +00:00
|
|
|
cap = CAP.copy()
|
2019-05-11 17:22:40 +00:00
|
|
|
cap.on_ack(lambda: self._cap_ack(event["server"]))
|
|
|
|
return cap
|
2018-11-08 17:09:53 +00:00
|
|
|
|
2019-05-11 17:22:40 +00:00
|
|
|
def _cap_ack(self, server):
|
|
|
|
server.wait_for_capability("resume")
|
2018-11-08 17:09:53 +00:00
|
|
|
|
|
|
|
@utils.hook("received.resume")
|
|
|
|
def on_resume(self, event):
|
2019-02-17 16:00:33 +00:00
|
|
|
cap_done = True
|
2019-02-17 12:48:22 +00:00
|
|
|
|
2018-11-08 17:09:53 +00:00
|
|
|
if event["args"][0] == "SUCCESS":
|
2019-02-16 11:23:40 +00:00
|
|
|
resume_channels = event["server"].get_setting("resume-channels", [])
|
2019-06-06 11:59:47 +00:00
|
|
|
self.log.info("Successfully resumed session")
|
2019-02-17 12:48:22 +00:00
|
|
|
event["server"].cap_started = False
|
2019-02-15 10:26:33 +00:00
|
|
|
|
2018-11-08 17:09:53 +00:00
|
|
|
elif event["args"][0] == "TOKEN":
|
2019-02-12 11:14:36 +00:00
|
|
|
token = self._get_token(event["server"])
|
|
|
|
self._set_token(event["server"], event["args"][1], new=True)
|
2019-02-10 00:03:14 +00:00
|
|
|
|
2019-02-12 11:14:36 +00:00
|
|
|
if token:
|
2019-02-15 10:26:33 +00:00
|
|
|
timestamp = event["server"]._resume_timestamp
|
2019-02-12 11:14:36 +00:00
|
|
|
|
2019-04-28 19:41:55 +00:00
|
|
|
event["server"].send_raw("RESUME %s%s" %
|
2019-02-12 11:14:36 +00:00
|
|
|
(token, " %s" % timestamp if timestamp else ""))
|
2019-02-17 16:00:33 +00:00
|
|
|
cap_done = False
|
2019-02-17 12:48:22 +00:00
|
|
|
|
|
|
|
if cap_done:
|
|
|
|
event["server"].capability_done("resume")
|
2019-02-15 10:26:33 +00:00
|
|
|
|
2018-11-08 17:09:53 +00:00
|
|
|
|
2019-02-16 15:53:14 +00:00
|
|
|
@utils.hook("received.001")
|
2018-11-08 17:09:53 +00:00
|
|
|
def on_connect(self, event):
|
2019-02-16 11:23:40 +00:00
|
|
|
event["server"].del_setting("resume-channels")
|
|
|
|
|
2019-02-12 11:14:36 +00:00
|
|
|
new_token = self._get_token(event["server"], new=True)
|
2018-11-08 17:09:53 +00:00
|
|
|
if new_token:
|
2019-02-12 11:14:36 +00:00
|
|
|
self._set_token(event["server"], new_token)
|
|
|
|
self._del_token(event["server"], new=True)
|
2019-02-16 11:23:40 +00:00
|
|
|
|
|
|
|
@utils.hook("self.join")
|
|
|
|
def on_join(self, event):
|
2019-02-16 15:53:59 +00:00
|
|
|
resume_channels = event["server"].get_setting("resume-channels", [])
|
2019-02-16 11:23:40 +00:00
|
|
|
channel_name = event["server"].irc_lower(event["channel"].name)
|
|
|
|
if not channel_name in resume_channels:
|
|
|
|
resume_channels.append(channel_name)
|
|
|
|
event["server"].set_setting("resume-channels", resume_channels)
|
2019-05-20 15:36:17 +00:00
|
|
|
|
|
|
|
@utils.hook("preprocess.send.quit")
|
|
|
|
def preprocess_send(self, event):
|
|
|
|
if event["line"].command == "QUIT" and event["server"].has_capability(
|
|
|
|
CAP):
|
|
|
|
event["line"].command = "BRB"
|
2019-05-22 10:57:26 +00:00
|
|
|
|
|
|
|
@utils.hook("received.fail.resume")
|
|
|
|
def fail_resume(self, event):
|
|
|
|
event["server"].capability_done("resume")
|