Session 2026-09-15 code: imap_client (auth+metrics), handlers (TG date format), cron fix (venv python), openspec imap-realtime-sync proposal

This commit is contained in:
2026-09-15 17:48:05 +00:00
parent 65c167acc2
commit defcf53d75
10 changed files with 982 additions and 28 deletions
+9 -22
View File
@@ -480,26 +480,16 @@ def fetch_attachments_imaplib(uid, folder, dest_dir):
import email as emailmod
import email.header as email_header
try:
creds = _himalaya_imap_credentials()
except Exception as e:
print(f" [WARN] нет IMAP-учётных из конфига himalaya: {e}", file=sys.stderr)
return False
sock = None
try:
sock = socket.create_connection((creds["host"], creds["port"]), timeout=30)
sock.settimeout(30)
greet = sock.recv(1024)
if not greet.startswith(b"* OK"):
raise RuntimeError(f"bad greeting: {greet[:80]!r}")
sock.sendall(b"a1 STARTTLS\r\n")
resp = sock.recv(1024)
if b"OK" not in resp:
raise RuntimeError(f"STARTTLS failed: {resp[:80]!r}")
ctx = sslmod.create_default_context()
sock = ctx.wrap_socket(sock, server_hostname=creds["host"])
sock.settimeout(30)
# Авторизация — через общий imap_client.imap_connect() (отдельная функция,
# с re-try против rate-limit Exchange). Креды читаются из конфига himalaya
# внутри imap_connect(). Возвращает (sock, creds).
import sys as _sys
from pathlib import Path as _Path
_sys.path.insert(0, str(_Path(__file__).resolve().parent))
from imap_client import imap_connect
sock, creds = imap_connect()
def cmd(tag, line, expect_literal=None):
sock.sendall(f"{tag} {line}\r\n".encode())
@@ -525,9 +515,6 @@ def fetch_attachments_imaplib(uid, folder, dest_dir):
break
return buf
r = cmd("a2", f'LOGIN {creds["login"]} {creds["password"]}')
if b"OK LOGIN" not in r:
raise RuntimeError(f"LOGIN failed: {r[-120:]!r}")
# Папка — в IMAP modified UTF-7 (кириллица иначе не находится на Exchange)
mbox = _imap_utf7_encode(folder)
r = cmd("a3", f'SELECT "{mbox}"')
@@ -589,7 +576,7 @@ def fetch_attachments_imaplib(uid, folder, dest_dir):
pass
def get_attachments(uid, folder, dest_dir, timeout=25):
def get_attachments(uid, folder, dest_dir, timeout=120):
"""
Скачать вложения письма в dest_dir.