Files
tsumugu/backend/app/sources.py
T
LarsandCursor b03f571cd8
Deploy Development / deploy (push) Successful in 22s
Mache die Akte zum Sammler und binde Paperless sowie E-Mail an.
Erfassen liegt in Dialogen, Vorgaenge erscheinen als Kacheln in der Akte, und Quellen lassen Dokumente und IMAP-Nachrichten verknuepfen.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-16 22:29:41 +02:00

237 lines
8.5 KiB
Python

"""Paperless and IMAP connectors. Secrets stay on the server."""
from __future__ import annotations
import imaplib
import json
import ssl
from email.header import decode_header
from email.parser import BytesParser
from urllib.error import HTTPError, URLError
from urllib.parse import urlencode, urlparse
from urllib.request import Request, urlopen
from uuid import UUID
from psycopg.types.json import Json
from app.core.errors import not_found, unprocessable
def save_connector(conn, owner_id: UUID, kind: str, name: str, settings: dict, secret: str) -> dict:
if kind == "paperless":
existing = conn.execute(
"SELECT id FROM connectors WHERE owner_id = %s AND kind = 'paperless'",
(owner_id,),
).fetchone()
if existing:
row = conn.execute(
"""
UPDATE connectors
SET name = %s, settings = %s, secret = %s
WHERE id = %s
RETURNING id, kind, name, settings, created_at
""",
(name, Json(settings), secret, existing["id"]),
).fetchone()
return _public(row)
row = conn.execute(
"""
INSERT INTO connectors (owner_id, kind, name, settings, secret)
VALUES (%s, %s, %s, %s, %s)
RETURNING id, kind, name, settings, created_at
""",
(owner_id, kind, name, Json(settings), secret),
).fetchone()
return _public(row)
def list_connectors(conn, owner_id: UUID) -> list[dict]:
rows = conn.execute(
"""
SELECT id, kind, name, settings, created_at
FROM connectors WHERE owner_id = %s
ORDER BY kind, name
""",
(owner_id,),
).fetchall()
return [_public(row) for row in rows]
def delete_connector(conn, owner_id: UUID, connector_id: UUID) -> None:
row = conn.execute(
"DELETE FROM connectors WHERE id = %s AND owner_id = %s RETURNING id",
(connector_id, owner_id),
).fetchone()
if row is None:
raise not_found()
def probe(conn, owner_id: UUID, connector_id: UUID) -> dict:
row = _owned(conn, owner_id, connector_id)
if row["kind"] == "paperless":
count = _paperless_count(row["settings"]["base_url"], row["secret"])
return {"ok": True, "detail": f"Paperless antwortet. {count} Dokumente sichtbar."}
_imap_login(row["settings"], row["secret"])
return {"ok": True, "detail": "Das Postfach ist erreichbar."}
def paperless_documents(conn, owner_id: UUID, query: str) -> list[dict]:
row = conn.execute(
"""
SELECT settings, secret FROM connectors
WHERE owner_id = %s AND kind = 'paperless'
ORDER BY created_at DESC LIMIT 1
""",
(owner_id,),
).fetchone()
if row is None:
raise unprocessable("Paperless ist noch nicht verbunden. Lege die Quelle unter Quellen an.")
base = row["settings"]["base_url"].rstrip("/")
payload = _paperless_get(base, row["secret"], "/api/documents/", {"query": query, "page_size": "20"})
items = []
for doc in payload.get("results") or []:
created = str(doc.get("created") or "")[:10]
title = (doc.get("title") or doc.get("original_file_name") or f"Dokument {doc.get('id')}").strip()
items.append(
{
"id": str(doc.get("id")),
"title": title[:200],
"document_on": created if len(created) == 10 else None,
"correspondent": _correspondent(doc),
"open_url": f"{base}/documents/{doc.get('id')}/",
}
)
return items
def imap_messages(conn, owner_id: UUID, connector_id: UUID, query: str) -> list[dict]:
row = _owned(conn, owner_id, connector_id)
if row["kind"] != "imap":
raise unprocessable("Das ist kein E-Mail-Konto.")
return _imap_list(row["settings"], row["secret"], query)
def _owned(conn, owner_id: UUID, connector_id: UUID) -> dict:
row = conn.execute(
"SELECT id, kind, name, settings, secret FROM connectors WHERE id = %s AND owner_id = %s",
(connector_id, owner_id),
).fetchone()
if row is None:
raise not_found()
return row
def _public(row) -> dict:
return {
"id": str(row["id"]),
"kind": row["kind"],
"name": row["name"],
"settings": row["settings"],
"created_at": row["created_at"].strftime("%Y-%m-%dT%H:%M:%SZ"),
}
def clean_base_url(value: str) -> str:
raw = value.strip()
parsed = urlparse(raw)
if parsed.scheme not in {"http", "https"} or not parsed.netloc or parsed.username or parsed.password:
raise unprocessable("Die Paperless-Adresse muss mit http:// oder https:// beginnen.")
if len(raw) > 300:
raise unprocessable("Die Paperless-Adresse ist zu lang.")
return raw.rstrip("/")
def _paperless_count(base: str, token: str) -> int:
payload = _paperless_get(base, token, "/api/documents/", {"page_size": "1"})
return int(payload.get("count") or 0)
def _paperless_get(base: str, token: str, path: str, params: dict) -> dict:
url = f"{base}{path}?{urlencode(params)}"
request = Request(url, headers={"Authorization": f"Token {token}", "Accept": "application/json"})
try:
with urlopen(request, timeout=12) as response:
return json.loads(response.read().decode("utf-8"))
except HTTPError as exc:
if exc.code in {401, 403}:
raise unprocessable("Paperless hat den Schlüssel abgelehnt.") from exc
raise unprocessable("Paperless hat die Anfrage nicht beantwortet.") from exc
except (URLError, TimeoutError, json.JSONDecodeError) as exc:
raise unprocessable("Paperless ist nicht erreichbar.") from exc
def _correspondent(doc: dict) -> str | None:
value = doc.get("correspondent_name") or doc.get("correspondent")
if isinstance(value, str) and value.strip():
return value.strip()[:200]
return None
def _imap_login(settings: dict, secret: str) -> imaplib.IMAP4:
host = settings["host"]
port = int(settings["port"])
security = settings.get("security") or "ssl"
try:
if security == "starttls":
client = imaplib.IMAP4(host, port, timeout=12)
client.starttls(ssl.create_default_context())
else:
client = imaplib.IMAP4_SSL(host, port, ssl_context=ssl.create_default_context(), timeout=12)
client.login(settings["username"], secret)
return client
except imaplib.IMAP4.error as exc:
raise unprocessable("Das Postfach hat die Anmeldung abgelehnt.") from exc
except (OSError, TimeoutError) as exc:
raise unprocessable("Das Postfach ist nicht erreichbar.") from exc
def _imap_list(settings: dict, secret: str, query: str) -> list[dict]:
client = _imap_login(settings, secret)
try:
mailbox = settings.get("mailbox") or "INBOX"
status, _ = client.select(mailbox, readonly=True)
if status != "OK":
raise unprocessable("Das Postfach enthält dieses Fach nicht.")
status, data = client.search(None, "ALL")
if status != "OK" or not data or not data[0]:
return []
ids = data[0].split()[-25:]
needle = query.strip().lower()
items = []
for mid in reversed(ids):
status, fetched = client.fetch(mid, "(BODY.PEEK[HEADER.FIELDS (FROM SUBJECT DATE MESSAGE-ID)])")
if status != "OK" or not fetched or not isinstance(fetched[0], tuple):
continue
header = BytesParser().parsebytes(fetched[0][1])
subject = _decode(header.get("Subject")) or "(ohne Betreff)"
sender = _decode(header.get("From"))
if needle and needle not in subject.lower() and needle not in sender.lower():
continue
message_id = (header.get("Message-ID") or f"uid:{mid.decode()}").strip()
items.append(
{
"id": message_id[:200],
"title": subject[:200],
"message_from": sender[:200],
"date": _decode(header.get("Date"))[:80],
}
)
return items
finally:
try:
client.logout()
except imaplib.IMAP4.error:
pass
def _decode(value: str | None) -> str:
if not value:
return ""
parts = []
for text, encoding in decode_header(value):
if isinstance(text, bytes):
parts.append(text.decode(encoding or "utf-8", errors="replace"))
else:
parts.append(text)
return "".join(parts).strip()