Compare commits

...

4 Commits

Author SHA1 Message Date
holger krekel
bcd9d615d4 remove superflous check in tests 2024-03-20 17:58:59 +01:00
holger krekel
d820644f66 fixes 2024-03-20 17:38:39 +01:00
holger krekel
4e97e4dc3d implement persistence via marshal 2024-03-20 17:02:04 +01:00
holger krekel
92a27983be make notifier take a directory 2024-03-20 16:41:41 +01:00
3 changed files with 77 additions and 31 deletions

View File

@@ -2,7 +2,7 @@
Description=Chatmail dict proxy for IMAP METADATA Description=Chatmail dict proxy for IMAP METADATA
[Service] [Service]
ExecStart={execpath} /run/dovecot/metadata.socket vmail {config_path} ExecStart={execpath} /run/dovecot/metadata.socket vmail {config_path} /home/vmail/metadata
Restart=always Restart=always
RestartSec=30 RestartSec=30

View File

@@ -1,5 +1,6 @@
import pwd import pwd
import pathlib
from queue import Queue from queue import Queue
from threading import Thread from threading import Thread
from socketserver import ( from socketserver import (
@@ -12,6 +13,7 @@ import sys
import logging import logging
import os import os
import requests import requests
import marshal
DICTPROXY_LOOKUP_CHAR = "L" DICTPROXY_LOOKUP_CHAR = "L"
@@ -23,12 +25,37 @@ DICTPROXY_TRANSACTION_CHARS = "SBC"
class Notifier: class Notifier:
def __init__(self): def __init__(self, metadata_dir):
self.guid2token = {} self.metadata_dir = metadata_dir
self.to_notify_queue = Queue() self.to_notify_queue = Queue()
def get_metadata(self, guid):
guid_path = self.metadata_dir.joinpath(guid)
if guid_path.exists():
with guid_path.open("rb") as f:
return marshal.load(f)
return {}
def set_metadata(self, guid, guid_data):
guid_path = self.metadata_dir.joinpath(guid)
write_path = guid_path.with_suffix(".tmp")
with write_path.open("wb") as f:
marshal.dump(guid_data, f)
os.rename(write_path, guid_path)
def set_token(self, guid, token): def set_token(self, guid, token):
self.guid2token[guid] = token guid_data = self.get_metadata(guid)
guid_data["token"] = token
self.set_metadata(guid, guid_data)
def del_token(self, guid):
guid_data = self.get_metadata(guid)
if "token" in guid_data:
del guid_data["token"]
self.set_metadata(guid, guid_data)
def get_token(self, guid):
return self.get_metadata(guid).get("token")
def new_message_for_guid(self, guid): def new_message_for_guid(self, guid):
self.to_notify_queue.put(guid) self.to_notify_queue.put(guid)
@@ -40,7 +67,7 @@ class Notifier:
def thread_run_one(self, requests_session): def thread_run_one(self, requests_session):
guid = self.to_notify_queue.get() guid = self.to_notify_queue.get()
token = self.guid2token.get(guid) token = self.get_token(guid)
if token: if token:
response = requests_session.post( response = requests_session.post(
"https://notifications.delta.chat/notify", "https://notifications.delta.chat/notify",
@@ -50,7 +77,7 @@ class Notifier:
if response.status_code == 410: if response.status_code == 410:
# 410 Gone status code # 410 Gone status code
# means the token is no longer valid. # means the token is no longer valid.
del self.guid2token[guid] self.del_token(guid)
def handle_dovecot_protocol(rfile, wfile, notifier): def handle_dovecot_protocol(rfile, wfile, notifier):
@@ -119,12 +146,15 @@ class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer):
def main(): def main():
socket, username, config = sys.argv[1:] socket, username, config, metadata_dir = sys.argv[1:]
passwd_entry = pwd.getpwnam(username) passwd_entry = pwd.getpwnam(username)
# XXX config is not currently used # XXX config is not currently used
config = read_config(config) config = read_config(config)
notifier = Notifier() metadata_dir = pathlib.Path(metadata_dir)
if not metadata_dir.exists():
metadata_dir.mkdir()
notifier = Notifier(metadata_dir)
class Handler(StreamRequestHandler): class Handler(StreamRequestHandler):
def handle(self): def handle(self):

View File

@@ -1,4 +1,5 @@
import io import io
import pytest
from chatmaild.metadata import ( from chatmaild.metadata import (
handle_dovecot_request, handle_dovecot_request,
@@ -7,40 +8,60 @@ from chatmaild.metadata import (
) )
def test_handle_dovecot_request_lookup_fails(): @pytest.fixture
notifier = Notifier() def notifier(tmp_path):
metadata_dir = tmp_path.joinpath("metadata")
metadata_dir.mkdir()
return Notifier(metadata_dir)
def test_notifier_persistence(tmp_path):
metadata_dir = tmp_path.joinpath("metadata")
metadata_dir.mkdir()
notifier1 = Notifier(metadata_dir)
notifier2 = Notifier(metadata_dir)
assert notifier1.get_token(guid="guid00") is None
assert notifier2.get_token(guid="guid00") is None
notifier1.set_token("guid00", "01234")
notifier1.set_token("guid03", "456")
assert notifier2.get_token("guid00") == "01234"
assert notifier2.get_token("guid03") == "456"
notifier2.del_token("guid00")
assert notifier1.get_token("guid00") is None
def test_handle_dovecot_request_lookup_fails(notifier):
res = handle_dovecot_request("Lpriv/123/chatmail", {}, notifier) res = handle_dovecot_request("Lpriv/123/chatmail", {}, notifier)
assert res == "N\n" assert res == "N\n"
def test_handle_dovecot_request_happy_path(): def test_handle_dovecot_request_happy_path(notifier):
notifier = Notifier()
transactions = {} transactions = {}
# lookups return the same NOTFOUND result # lookups return the same NOTFOUND result
res = handle_dovecot_request("Lpriv/123/chatmail", transactions, notifier) res = handle_dovecot_request("Lpriv/123/chatmail", transactions, notifier)
assert res == "N\n" assert res == "N\n"
assert not notifier.guid2token and not transactions assert notifier.get_token("guid00") is None and not transactions
# set device token in a transaction # set device token in a transaction
tx = "1111" tx = "1111"
msg = f"B{tx}\tuser" msg = f"B{tx}\tuser"
res = handle_dovecot_request(msg, transactions, notifier) res = handle_dovecot_request(msg, transactions, notifier)
assert not res and not notifier.guid2token assert not res and notifier.get_token("guid00") is None
assert transactions == {tx: "O\n"} assert transactions == {tx: "O\n"}
msg = f"S{tx}\tpriv/guid00/devicetoken\t01234" msg = f"S{tx}\tpriv/guid00/devicetoken\t01234"
res = handle_dovecot_request(msg, transactions, notifier) res = handle_dovecot_request(msg, transactions, notifier)
assert not res assert not res
assert len(transactions) == 1 assert len(transactions) == 1
assert len(notifier.guid2token) == 1 assert notifier.get_token("guid00") == "01234"
assert notifier.guid2token["guid00"] == "01234"
msg = f"C{tx}" msg = f"C{tx}"
res = handle_dovecot_request(msg, transactions, notifier) res = handle_dovecot_request(msg, transactions, notifier)
assert res == "O\n" assert res == "O\n"
assert len(transactions) == 0 assert len(transactions) == 0
assert notifier.guid2token["guid00"] == "01234" assert notifier.get_token("guid00") == "01234"
# trigger notification for incoming message # trigger notification for incoming message
assert handle_dovecot_request(f"B{tx}\tuser", transactions, notifier) is None assert handle_dovecot_request(f"B{tx}\tuser", transactions, notifier) is None
@@ -52,7 +73,7 @@ def test_handle_dovecot_request_happy_path():
assert not transactions assert not transactions
def test_handle_dovecot_protocol_set_devicetoken(): def test_handle_dovecot_protocol_set_devicetoken(notifier):
rfile = io.BytesIO( rfile = io.BytesIO(
b"\n".join( b"\n".join(
[ [
@@ -64,13 +85,12 @@ def test_handle_dovecot_protocol_set_devicetoken():
) )
) )
wfile = io.BytesIO() wfile = io.BytesIO()
notifier = Notifier()
handle_dovecot_protocol(rfile, wfile, notifier) handle_dovecot_protocol(rfile, wfile, notifier)
assert notifier.guid2token["guid00"] == "01234" assert notifier.get_token("guid00") == "01234"
assert wfile.getvalue() == b"O\n" assert wfile.getvalue() == b"O\n"
def test_handle_dovecot_protocol_iterate(): def test_handle_dovecot_protocol_iterate(notifier):
rfile = io.BytesIO( rfile = io.BytesIO(
b"\n".join( b"\n".join(
[ [
@@ -80,12 +100,11 @@ def test_handle_dovecot_protocol_iterate():
) )
) )
wfile = io.BytesIO() wfile = io.BytesIO()
notifier = Notifier()
handle_dovecot_protocol(rfile, wfile, notifier) handle_dovecot_protocol(rfile, wfile, notifier)
assert wfile.getvalue() == b"\n" assert wfile.getvalue() == b"\n"
def test_handle_dovecot_protocol_messagenew(): def test_handle_dovecot_protocol_messagenew(notifier):
rfile = io.BytesIO( rfile = io.BytesIO(
b"\n".join( b"\n".join(
[ [
@@ -97,14 +116,13 @@ def test_handle_dovecot_protocol_messagenew():
) )
) )
wfile = io.BytesIO() wfile = io.BytesIO()
notifier = Notifier()
handle_dovecot_protocol(rfile, wfile, notifier) handle_dovecot_protocol(rfile, wfile, notifier)
assert wfile.getvalue() == b"O\n" assert wfile.getvalue() == b"O\n"
assert notifier.to_notify_queue.get() == "guid00" assert notifier.to_notify_queue.get() == "guid00"
assert notifier.to_notify_queue.qsize() == 0 assert notifier.to_notify_queue.qsize() == 0
def test_notifier_thread_run(): def test_notifier_thread_run(notifier):
requests = [] requests = []
class ReqMock: class ReqMock:
@@ -116,16 +134,15 @@ def test_notifier_thread_run():
return Result() return Result()
notifier = Notifier()
notifier.set_token("guid00", "01234") notifier.set_token("guid00", "01234")
notifier.new_message_for_guid("guid00") notifier.new_message_for_guid("guid00")
notifier.thread_run_one(ReqMock()) notifier.thread_run_one(ReqMock())
url, data, timeout = requests[0] url, data, timeout = requests[0]
assert data == "01234" assert data == "01234"
assert len(notifier.guid2token) == 1 assert notifier.get_token("guid00") == "01234"
def test_notifier_thread_run_gone_removes_token(): def test_notifier_thread_run_gone_removes_token(notifier):
requests = [] requests = []
class ReqMock: class ReqMock:
@@ -137,11 +154,10 @@ def test_notifier_thread_run_gone_removes_token():
return Result() return Result()
notifier = Notifier()
notifier.set_token("guid00", "01234") notifier.set_token("guid00", "01234")
notifier.new_message_for_guid("guid00") notifier.new_message_for_guid("guid00")
assert notifier.guid2token["guid00"] == "01234" assert notifier.get_token("guid00") == "01234"
notifier.thread_run_one(ReqMock()) notifier.thread_run_one(ReqMock())
url, data, timeout = requests[0] url, data, timeout = requests[0]
assert data == "01234" assert data == "01234"
assert len(notifier.guid2token) == 0 assert notifier.get_token("guid00") is None