#!/usr/bin/env python3 """Report current system state: CRM hits for a token, INBOX/Trash mails.""" import os import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..")) from projektmatch import mailer # noqa: E402 from projektmatch.espocrm import EspoClient # noqa: E402 token = sys.argv[1] if len(sys.argv) > 1 else "" espo = EspoClient(os.environ["PM_ESPO_BASE"], os.environ["PM_ESPO_API_KEY"]) if token: hits = espo.search("Opportunity", "contains", "cProjektlink", token, select="name,cProjektlink") print(f"CRM hits for '{token}': {len(hits)}") for h in hits: print(f" {h['id']} {h['name']}") box = mailer.MailBox("chancen@destengs.com", os.environ["PM_IMAP_PASSWORD"]) for folder in ("INBOX", box.trash_folder()): box.conn.select(folder) typ, data = box.conn.uid("SEARCH", None, "ALL") uids = data[0].split() if data and data[0] else [] print(f"{folder}: {len(uids)} mails") for uid in uids[-8:]: typ, d = box.conn.uid("FETCH", uid.decode(), "(BODY.PEEK[HEADER.FIELDS (SUBJECT)])") print(" ", d[0][1].decode(errors="replace").strip()) box.close()