mirror of
https://github.com/chatmail/relay.git
synced 2026-05-10 16:04:37 +00:00
refactor into a handle_dovecot_request function
This commit is contained in:
@@ -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
|
||||
# <https://github.com/dovecot/core/blob/5e7965632395793d9355eb906b173bf28d2a10ca/src/lib-storage/mailbox-attribute.h>
|
||||
# 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
|
||||
# <https://github.com/dovecot/core/blob/5e7965632395793d9355eb906b173bf28d2a10ca/src/lib-storage/mailbox-attribute.h>
|
||||
# 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):
|
||||
|
||||
Reference in New Issue
Block a user