SPB Git

Repository Public

jmap_inbox.py 1.9 KB 22 lines python

1#!/usr/bin/env python32"""SP Mail — liste la boîte de réception d'un compte (sujet, expéditeur, résultats d'authentification), sans afficher les corps.3Usage : JMAP_URL=… JMAP_USER=… JMAP_PASS=… jmap_inbox.py [n]"""4import base64, json, os, sys, urllib.request5URL, USER, PW = os.environ["JMAP_URL"].rstrip("/"), os.environ["JMAP_USER"], os.environ["JMAP_PASS"]6AUTH = "Basic " + base64.b64encode(f"{USER}:{PW}".encode()).decode()7def http(path, body=None):8    req = urllib.request.Request(URL + path, data=json.dumps(body).encode() if body else None, headers={"Authorization": AUTH, "Content-Type": "application/json"})9    return json.load(urllib.request.urlopen(req, timeout=30))10acct = http("/jmap/session")["primaryAccounts"]["urn:ietf:params:jmap:mail"]11n = int(sys.argv[1]) if len(sys.argv) > 1 else 1012r = http("/jmap/", {"using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"], "methodCalls": [13    ["Mailbox/get", {"accountId": acct, "properties": ["role", "name", "totalEmails"]}, "m"],14    ["Email/query", {"accountId": acct, "sort": [{"property": "receivedAt", "isAscending": False}], "limit": n}, "q"],15    ["Email/get", {"accountId": acct, "#ids": {"resultOf": "q", "name": "Email/query", "path": "/ids"},16                   "properties": ["subject", "from", "receivedAt", "mailboxIds", "header:Authentication-Results:asText", "header:X-Spam-Status:asText"]}, "g"]]})["methodResponses"]17names = {m["id"]: (m.get("role") or m["name"]) for m in r[0][1]["list"]}18print("dossiers:", {names[m["id"]]: m["totalEmails"] for m in r[0][1]["list"] if m["totalEmails"]})19for e in r[2][1]["list"]:20    f = (e.get("from") or [{}])[0].get("email", "?")21    print(f'- {e["receivedAt"]} [{",".join(names[i] for i in e["mailboxIds"])}] {f} | {e.get("subject")}\n    auth: {(e.get("header:Authentication-Results:asText") or "—").strip()[:330]}\n    spam: {(e.get("header:X-Spam-Status:asText") or "—").strip()[:120]}')22
Files main
Menu

Theme