mirror of
https://github.com/chatmail/relay.git
synced 2026-05-19 20:38:05 +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:
|
if not msg:
|
||||||
break
|
break
|
||||||
|
|
||||||
short_command = msg[0]
|
res = handle_dovecot_request(msg, tokens, requests_session, config)
|
||||||
if short_command == "L":
|
if res:
|
||||||
wfile.write(b"N\n")
|
wfile.write(res.encode("ascii"))
|
||||||
elif short_command == "S":
|
wfile.flush()
|
||||||
# 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.
|
|
||||||
|
|
||||||
# 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")
|
def handle_dovecot_request(msg, tokens, requests_session, config: Config):
|
||||||
transaction_id = parts[0]
|
short_command = msg[0]
|
||||||
keyname = parts[1].split("/")
|
if short_command == "L":
|
||||||
value = parts[2] if len(parts) > 2 else ""
|
return b"N\n"
|
||||||
if keyname[0] == "priv" and keyname[2] == "devicetoken":
|
elif short_command == "S":
|
||||||
tokens[keyname[1]] = value
|
# See header of
|
||||||
elif keyname[0] == "priv" and keyname[2] == "messagenew":
|
# <https://github.com/dovecot/core/blob/5e7965632395793d9355eb906b173bf28d2a10ca/src/lib-storage/mailbox-attribute.h>
|
||||||
guid = keyname[1]
|
# for the documentation on the structure of the key.
|
||||||
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"))
|
|
||||||
|
|
||||||
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):
|
class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer):
|
||||||
|
|||||||
Reference in New Issue
Block a user