Upgrade resume.py to draft/resume-0.3 and support sending last-read timestamp

This commit is contained in:
jesopo 2019-02-12 11:14:36 +00:00
parent 8c9626f0a9
commit 144182868c
2 changed files with 26 additions and 22 deletions

View file

@ -1,16 +1,22 @@
from src import ModuleManager, utils
CAP = "draft/resume-0.2"
CAP = "draft/resume-0.3"
class Module(ModuleManager.BaseModule):
def _get_token(self, server):
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)
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))
def _get_timestamp(self, server):
return server.get_setting("last-read", None)
@utils.hook("received.cap.ls")
def on_cap_ls(self, event):
nickname, token = self._get_token(event["server"])
if CAP in event["capabilities"]:
event["server"].queue_capability(CAP)
@ -26,26 +32,20 @@ class Module(ModuleManager.BaseModule):
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-resume-token"
] = event["args"][1]
nickname, token = self._get_token(event["server"])
token = self._get_token(event["server"])
self._set_token(event["server"], event["args"][1], new=True)
if nickname and token:
event["server"].send("RESUME %s %s" % (nickname, token))
if token:
timestamp = self._get_timestamp(event["server"])
event["server"].send("RESUME %s%s" %
(token, " %s" % timestamp if timestamp else ""))
event["server"].cap_started = False
event["server"].capability_done("resume")
@utils.hook("received.numeric.001")
def on_connect(self, event):
new_token = event["server"].connection_params.args.get(
"new-resume-token", None)
new_token = self._get_token(event["server"], new=True)
if new_token:
self._set_token(event["server"], event["server"].nickname,
new_token)
del event["server"].connection_params.args["new-resume-token"]
@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)
self._set_token(event["server"], new_token)
self._del_token(event["server"], new=True)

View file

@ -233,6 +233,10 @@ class Server(IRCObject.Object):
lines = self.socket.read()
if lines:
self.ping_sent = False
now = datetime.datetime.utcnow()
self.set_setting("last-read", datetime.datetime.strftime(now,
utils.ISO8601_FORMAT))
return lines
def send(self, line: str):