diff --git a/chatmaild/src/chatmaild/metadata.py b/chatmaild/src/chatmaild/metadata.py index 52152cc2..f11027ae 100644 --- a/chatmaild/src/chatmaild/metadata.py +++ b/chatmaild/src/chatmaild/metadata.py @@ -22,54 +22,59 @@ def handle_dovecot_protocol(rfile, wfile, tokens, requests_session, config: Conf if not msg: break - short_command = msg[0] - if short_command == "L": - wfile.write(b"N\n") - elif short_command == "S": - # See header of - # - # for the documentation on the structure of the key. + res = handle_dovecot_request(msg, tokens, requests_session, config) + if res: + wfile.write(res.encode("ascii")) + wfile.flush() - # Request GETMETADATA "INBOX" /private/chatmail - # results in a query for - # priv/dd72550f05eadc65542a1200cac67ad7/chatmail - # - # Request GETMETADATA "" /private/chatmail - # results in - # priv/dd72550f05eadc65542a1200cac67ad7/vendor/vendor.dovecot/pvt/server/chatmail - parts = msg[1:].split("\t") - transaction_id = parts[0] - keyname = parts[1].split("/") - value = parts[2] if len(parts) > 2 else "" - if keyname[0] == "priv" and keyname[2] == "devicetoken": - tokens[keyname[1]] = value - elif keyname[0] == "priv" and keyname[2] == "messagenew": - guid = keyname[1] - token = tokens.get(guid) - if 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] - else: - # Transaction failed. - transactions[transaction_id] = b"F\n" - elif short_command == "B": - # Begin transaction. - transaction_id = msg[1:].split("\t")[0] - transactions[transaction_id] = b"O\n" - elif short_command == "C": - # Commit transaction. - transaction_id = msg[1:].split("\t")[0] - wfile.write(transactions.pop(transaction_id, b"N\n")) +def handle_dovecot_request(msg, tokens, requests_session, config: Config): + short_command = msg[0] + if short_command == "L": + return b"N\n" + elif short_command == "S": + # See header of + # + # for the documentation on the structure of the key. - wfile.flush() + # Request GETMETADATA "INBOX" /private/chatmail + # results in a query for + # priv/dd72550f05eadc65542a1200cac67ad7/chatmail + # + # Request GETMETADATA "" /private/chatmail + # results in + # priv/dd72550f05eadc65542a1200cac67ad7/vendor/vendor.dovecot/pvt/server/chatmail + + parts = msg[1:].split("\t") + transaction_id = parts[0] + keyname = parts[1].split("/") + value = parts[2] if len(parts) > 2 else "" + if keyname[0] == "priv" and keyname[2] == "devicetoken": + tokens[keyname[1]] = value + elif keyname[0] == "priv" and keyname[2] == "messagenew": + guid = keyname[1] + token = tokens.get(guid) + if 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] + else: + # Transaction failed. + transactions[transaction_id] = b"F\n" + elif short_command == "B": + # Begin transaction. + transaction_id = msg[1:].split("\t")[0] + transactions[transaction_id] = b"O\n" + elif short_command == "C": + # Commit transaction. + transaction_id = msg[1:].split("\t")[0] + return transactions.pop(transaction_id, b"N\n") class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer):