fix read-only behavior

This commit is contained in:
2026-08-02 16:07:16 -04:00
parent 604fb38937
commit eee315fe3d
+14 -4
View File
@@ -225,6 +225,12 @@ def parse_accounts(config: dict) -> list[dict]:
def resolve_account(accounts: list[dict], account_id: str) -> dict:
# No ambiguity possible with a single configured account — use it
# even if the caller passed an account_id that doesn't match (e.g. a
# hallucinated or stale id), rather than hard-failing.
if len(accounts) == 1:
return accounts[0]
if not account_id or not account_id.strip():
raise ValueError(
f"account_id is required. Available accounts: {[a['id'] for a in accounts]}. "
@@ -479,10 +485,14 @@ async def _forward_uid_batch(
len(uids),
_format_uid_list(uids),
)
raw_messages = await asyncio.to_thread(client.fetch, uids, ["RFC822"])
# BODY.PEEK[] (not RFC822/BODY[]) so the fetch never implicitly sets
# \Seen — some servers (e.g. iCloud) apply \Seen on a plain RFC822
# fetch even when the folder was SELECTed read-only, which then queues
# an unsolicited FETCH FLAGS response that breaks the next IDLE call.
raw_messages = await asyncio.to_thread(client.fetch, uids, ["BODY.PEEK[]"])
for uid in uids:
data = raw_messages.get(uid, {})
raw = data.get(b"RFC822", b"")
raw = data.get(b"BODY[]", b"")
if not raw:
logger.debug(
"[%s/%s] Skipping UID %s because RFC822 payload was empty",
@@ -912,10 +922,10 @@ async def read_email(
client = get_imap_client(acc)
try:
client.select_folder(folder, readonly=True)
data = client.fetch([uid], ["RFC822"])
data = client.fetch([uid], ["BODY.PEEK[]"])
if uid not in data:
return {"error": f"Email UID {uid} not found in {folder}"}
return parse_email_message(data[uid][b"RFC822"])
return parse_email_message(data[uid][b"BODY[]"])
finally:
client.logout()