Skip to content

Commit

Permalink
simplify handle_set method for dictproxy subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
hpk42 committed Jul 31, 2024
1 parent 1f1d1fd commit 888f7e6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## untagged

- fix metadata dictproxy which would confuse transactions
resulting in missed notifications and other issues.
([#393](https://github.com/deltachat/chatmail/pull/388))
([#394](https://github.com/deltachat/chatmail/pull/389))

- add optional "imap_rawlog" config option. If true,
.in/.out files are created in user home dirs
containing the imap protocol messages.
Expand Down
10 changes: 6 additions & 4 deletions chatmaild/src/chatmaild/dictproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def handle_dovecot_request(self, msg, transactions):
elif short_command == "C":
return self.handle_commit_transaction(transaction_id, parts, transactions)
elif short_command == "S":
return self.handle_set(transaction_id, parts, transactions)
addr = transactions[transaction_id]["addr"]
if not self.handle_set(addr, parts):
transactions[transaction_id]["res"] = "F\n"
logging.error(f"dictproxy-set failed for {addr!r}: {msg!r}")

def handle_lookup(self, parts):
logging.warning(f"lookup ignored: {parts!r}")
Expand All @@ -59,11 +62,10 @@ def handle_begin_transaction(self, transaction_id, parts, transactions):
addr = parts[1]
transactions[transaction_id] = dict(addr=addr, res="O\n")

def handle_set(self, transaction_id, parts, transactions):
def handle_set(self, addr, parts):
# For documentation on key structure see
# https://github.com/dovecot/core/blob/main/src/lib-storage/mailbox-attribute.h

transactions[transaction_id]["res"] = "F\n"
return False

def handle_commit_transaction(self, transaction_id, parts, transactions):
# return whatever "set" command(s) set as result.
Expand Down
11 changes: 5 additions & 6 deletions chatmaild/src/chatmaild/lastlogin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ def __init__(self, config):
super().__init__()
self.config = config

def handle_set(self, transaction_id, parts, transactions):
def handle_set(self, addr, parts):
keyname = parts[1].split("/")
value = parts[2] if len(parts) > 2 else ""
addr = transactions[transaction_id]["addr"]
if keyname[0] == "shared" and keyname[1] == "last-login":
if addr.startswith("echo@"):
return
return True
addr = keyname[2]
timestamp = int(value)
user = self.config.get_user(addr)
user.set_last_login_timestamp(timestamp)
else:
# Transaction failed.
transactions[transaction_id]["res"] = "F\n"
return True

return False


def main():
Expand Down
15 changes: 5 additions & 10 deletions chatmaild/src/chatmaild/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,19 @@ def handle_lookup(self, parts):
logging.warning(f"lookup ignored: {parts!r}")
return "N\n"

def handle_set(self, transaction_id, parts, transactions):
def handle_set(self, addr, parts):
# For documentation on key structure see
# https://github.com/dovecot/core/blob/main/src/lib-storage/mailbox-attribute.h
keyname = parts[1].split("/")
value = parts[2] if len(parts) > 2 else ""
addr = transactions[transaction_id]["addr"]
if keyname[0] == "priv" and keyname[2] == self.metadata.DEVICETOKEN_KEY:
self.metadata.add_token_to_addr(addr, value)
return True
elif keyname[0] == "priv" and keyname[2] == "messagenew":
self.notifier.new_message_for_addr(addr, self.metadata)
else:
# Transaction failed.
try:
transactions[transaction_id]["res"] = "F\n"
except KeyError:
logging.error(
f"could not mark tx as failed: {transaction_id} {transactions}"
)
return True

return False


def main():
Expand Down

0 comments on commit 888f7e6

Please sign in to comment.