Upgrade resume.py to draft/resume-0.3
and support sending last-read timestamp
This commit is contained in:
parent
8c9626f0a9
commit
144182868c
2 changed files with 26 additions and 22 deletions
|
@ -1,16 +1,22 @@
|
||||||
from src import ModuleManager, utils
|
from src import ModuleManager, utils
|
||||||
|
|
||||||
CAP = "draft/resume-0.2"
|
CAP = "draft/resume-0.3"
|
||||||
|
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
def _get_token(self, server):
|
def _setting(self, new):
|
||||||
return server.connection_params.args.get("resume-token", (None, None))
|
return "resume-token%s" % ("-new" if new else "")
|
||||||
def _set_token(Self, server, nickname, token):
|
def _get_token(self, server, new=False):
|
||||||
server.connection_params.args["resume-token"] = (nickname, token)
|
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")
|
@utils.hook("received.cap.ls")
|
||||||
def on_cap_ls(self, event):
|
def on_cap_ls(self, event):
|
||||||
nickname, token = self._get_token(event["server"])
|
|
||||||
if CAP in event["capabilities"]:
|
if CAP in event["capabilities"]:
|
||||||
event["server"].queue_capability(CAP)
|
event["server"].queue_capability(CAP)
|
||||||
|
|
||||||
|
@ -26,26 +32,20 @@ class Module(ModuleManager.BaseModule):
|
||||||
elif event["args"][0] == "ERR":
|
elif event["args"][0] == "ERR":
|
||||||
self.log.info("Failed to resume session: %s", [event["args"][1]])
|
self.log.info("Failed to resume session: %s", [event["args"][1]])
|
||||||
elif event["args"][0] == "TOKEN":
|
elif event["args"][0] == "TOKEN":
|
||||||
event["server"].connection_params.args["new-resume-token"
|
token = self._get_token(event["server"])
|
||||||
] = event["args"][1]
|
self._set_token(event["server"], event["args"][1], new=True)
|
||||||
nickname, token = self._get_token(event["server"])
|
|
||||||
|
|
||||||
if nickname and token:
|
if token:
|
||||||
event["server"].send("RESUME %s %s" % (nickname, 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"].cap_started = False
|
||||||
event["server"].capability_done("resume")
|
event["server"].capability_done("resume")
|
||||||
|
|
||||||
@utils.hook("received.numeric.001")
|
@utils.hook("received.numeric.001")
|
||||||
def on_connect(self, event):
|
def on_connect(self, event):
|
||||||
new_token = event["server"].connection_params.args.get(
|
new_token = self._get_token(event["server"], new=True)
|
||||||
"new-resume-token", None)
|
|
||||||
if new_token:
|
if new_token:
|
||||||
self._set_token(event["server"], event["server"].nickname,
|
self._set_token(event["server"], new_token)
|
||||||
new_token)
|
self._del_token(event["server"], new=True)
|
||||||
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)
|
|
||||||
|
|
|
@ -233,6 +233,10 @@ class Server(IRCObject.Object):
|
||||||
lines = self.socket.read()
|
lines = self.socket.read()
|
||||||
if lines:
|
if lines:
|
||||||
self.ping_sent = False
|
self.ping_sent = False
|
||||||
|
|
||||||
|
now = datetime.datetime.utcnow()
|
||||||
|
self.set_setting("last-read", datetime.datetime.strftime(now,
|
||||||
|
utils.ISO8601_FORMAT))
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
def send(self, line: str):
|
def send(self, line: str):
|
||||||
|
|
Loading…
Reference in a new issue