test the protocol

This commit is contained in:
holger krekel
2024-03-07 12:11:23 +01:00
parent aca4b1a4da
commit 8928cb8816
2 changed files with 74 additions and 28 deletions

View File

@@ -4,7 +4,7 @@ from socketserver import (
StreamRequestHandler, StreamRequestHandler,
ThreadingMixIn, ThreadingMixIn,
) )
from .config import read_config, Config from .config import read_config
import sys import sys
import logging import logging
import os import os
@@ -18,39 +18,29 @@ DICTPROXY_COMMIT_TRANSACTION_CHAR = "C"
DICTPROXY_TRANSACTION_CHARS = "SBC" DICTPROXY_TRANSACTION_CHARS = "SBC"
def handle_dovecot_protocol(rfile, wfile, tokens, requests_session, config: Config): def handle_dovecot_protocol(rfile, wfile, tokens, notify_guid):
# HELLO message, ignored. # HELLO message, ignored.
msg = rfile.readline().strip().decode() msg = rfile.readline().strip().decode()
def post_with_token(token):
response = requests_session.post(
"https://notifications.delta.chat/notify",
data=token,
timeout=60,
)
if response.status_code == 410:
# 410 Gone status code
# means the token is no longer valid.
del tokens[guid]
transactions = {} transactions = {}
while True: while True:
msg = rfile.readline().strip().decode() msg = rfile.readline().strip().decode()
if not msg: if not msg:
break break
res = handle_dovecot_request(msg, transactions, tokens, post_with_token) res = handle_dovecot_request(msg, transactions, tokens, notify_guid)
if res: if res:
wfile.write(res.encode("ascii")) wfile.write(res.encode("ascii"))
wfile.flush() wfile.flush()
def handle_dovecot_request(msg, transactions, tokens, post_with_token): def handle_dovecot_request(msg, transactions, tokens, notify_guid):
# see https://doc.dovecot.org/3.0/developer_manual/design/dict_protocol/ # see https://doc.dovecot.org/3.0/developer_manual/design/dict_protocol/
print("got", msg)
short_command = msg[0] short_command = msg[0]
parts = msg[1:].split("\t") parts = msg[1:].split("\t")
if short_command == DICTPROXY_LOOKUP_CHAR: if short_command == DICTPROXY_LOOKUP_CHAR:
return b"N\n" return "N\n"
if short_command not in (DICTPROXY_TRANSACTION_CHARS): if short_command not in (DICTPROXY_TRANSACTION_CHARS):
return return
@@ -58,10 +48,10 @@ def handle_dovecot_request(msg, transactions, tokens, post_with_token):
transaction_id = parts[0] transaction_id = parts[0]
if short_command == DICTPROXY_BEGIN_TRANSACTION_CHAR: if short_command == DICTPROXY_BEGIN_TRANSACTION_CHAR:
transactions[transaction_id] = b"O\n" transactions[transaction_id] = "O\n"
elif short_command == DICTPROXY_COMMIT_TRANSACTION_CHAR: elif short_command == DICTPROXY_COMMIT_TRANSACTION_CHAR:
# returns whether it failed or succeeded. # returns whether it failed or succeeded.
return transactions.pop(transaction_id, b"N\n") return transactions.pop(transaction_id, "N\n")
elif short_command == DICTPROXY_SET_CHAR: elif short_command == DICTPROXY_SET_CHAR:
# See header of # See header of
# <https://github.com/dovecot/core/blob/5e7965632395793d9355eb906b173bf28d2a10ca/src/lib-storage/mailbox-attribute.h> # <https://github.com/dovecot/core/blob/5e7965632395793d9355eb906b173bf28d2a10ca/src/lib-storage/mailbox-attribute.h>
@@ -81,12 +71,10 @@ def handle_dovecot_request(msg, transactions, tokens, post_with_token):
tokens[keyname[1]] = value tokens[keyname[1]] = value
elif keyname[0] == "priv" and keyname[2] == "messagenew": elif keyname[0] == "priv" and keyname[2] == "messagenew":
guid = keyname[1] guid = keyname[1]
token = tokens.get(guid) notify_guid(guid)
if token:
post_with_token(token)
else: else:
# Transaction failed. # Transaction failed.
transactions[transaction_id] = b"F\n" transactions[transaction_id] = "F\n"
class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer): class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer):
@@ -102,11 +90,24 @@ def main():
tokens = {} tokens = {}
requests_session = requests.Session() requests_session = requests.Session()
def notify_guid(guid):
token = tokens.get(guid)
if token:
response = requests_session.post(
"https://notifications.delta.chat/notify",
data=tokens[guid],
timeout=60,
)
if response.status_code == 410:
# 410 Gone status code
# means the token is no longer valid.
del tokens[guid]
class Handler(StreamRequestHandler): class Handler(StreamRequestHandler):
def handle(self): def handle(self):
try: try:
handle_dovecot_protocol( handle_dovecot_protocol(
self.rfile, self.wfile, tokens, requests_session, config self.rfile, self.wfile, tokens, requests_session
) )
except Exception: except Exception:
logging.exception("Exception in the handler") logging.exception("Exception in the handler")

View File

@@ -1,15 +1,23 @@
import io
from chatmaild.metadata import ( from chatmaild.metadata import (
handle_dovecot_request, handle_dovecot_request,
handle_dovecot_protocol,
) )
def test_handle_dovecot_request_lookup_fails():
res = handle_dovecot_request("Lpriv/123/chatmail", {}, {}, None)
assert res == "N\n"
def test_handle_dovecot_request_happy_path(): def test_handle_dovecot_request_happy_path():
tokens = {} tokens = {}
transactions = {} transactions = {}
# lookups return the same NOTFOUND result # lookups return the same NOTFOUND result
res = handle_dovecot_request("Lpriv/123/chatmail", transactions, tokens, None) res = handle_dovecot_request("Lpriv/123/chatmail", transactions, tokens, None)
assert res == b"N\n" assert res == "N\n"
assert not tokens and not transactions assert not tokens and not transactions
# set device token in a transaction # set device token in a transaction
@@ -17,7 +25,7 @@ def test_handle_dovecot_request_happy_path():
msg = f"B{tx}\tuser" msg = f"B{tx}\tuser"
res = handle_dovecot_request(msg, transactions, tokens, None) res = handle_dovecot_request(msg, transactions, tokens, None)
assert not res and not tokens assert not res and not tokens
assert transactions == {tx: b"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, tokens, None) res = handle_dovecot_request(msg, transactions, tokens, None)
@@ -27,7 +35,7 @@ def test_handle_dovecot_request_happy_path():
msg = f"C{tx}" msg = f"C{tx}"
res = handle_dovecot_request(msg, transactions, tokens, None) res = handle_dovecot_request(msg, transactions, tokens, None)
assert res == b"O\n" assert res == "O\n"
assert len(transactions) == 0 assert len(transactions) == 0
assert tokens["guid00"] == "01234" assert tokens["guid00"] == "01234"
@@ -36,6 +44,43 @@ def test_handle_dovecot_request_happy_path():
msg = f"S{tx}\tpriv/guid00/messagenew" msg = f"S{tx}\tpriv/guid00/messagenew"
requests = [] requests = []
assert handle_dovecot_request(msg, transactions, tokens, requests.append) is None assert handle_dovecot_request(msg, transactions, tokens, requests.append) is None
assert requests == ["01234"] assert requests == ["guid00"]
assert handle_dovecot_request(f"C{tx}\tuser", transactions, tokens, None) == b"O\n" assert handle_dovecot_request(f"C{tx}\tuser", transactions, tokens, None) == "O\n"
assert not transactions assert not transactions
def test_handle_dovecot_protocol_set_devicetoken():
rfile = io.BytesIO(
b"\n".join(
[
b"HELLO",
b"Btx00\tuser",
b"Stx00\tpriv/guid00/devicetoken\t01234",
b"Ctx00",
]
)
)
tokens = {}
wfile = io.BytesIO()
handle_dovecot_protocol(rfile, wfile, tokens, None)
assert tokens["guid00"] == "01234"
assert wfile.getvalue() == b"O\n"
def test_handle_dovecot_protocol_messagenew():
rfile = io.BytesIO(
b"\n".join(
[
b"HELLO",
b"Btx01\tuser",
b"Stx01\tpriv/guid00/messagenew",
b"Ctx01",
]
)
)
tokens = {"guid00": "01234"}
wfile = io.BytesIO()
requests = []
handle_dovecot_protocol(rfile, wfile, tokens, requests.append)
assert wfile.getvalue() == b"O\n"
assert requests == ["guid00"]