"""Connector administration and live Paperless / IMAP lookup.""" from __future__ import annotations from uuid import UUID from fastapi import APIRouter, Depends from fastapi.responses import Response from psycopg import Connection from app.access.policy import Ctx, element_rights, get_element, parse_query_context from app.api.deps import Principal, get_conn, get_principal from app.api.routes import _created, _ctx_model from app.api.schemas import ConnectorIn, ExternalIn from app.core.errors import not_found, unprocessable from app.ops import create_external from app.sources import ( clean_base_url, delete_connector, imap_mailboxes, imap_messages, imap_preview, list_connectors, paperless_content, paperless_documents, probe, save_connector, ) router = APIRouter() @router.get("/api/v1/connectors") def get_connectors(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): return {"items": list_connectors(conn, principal.account_id)} @router.post("/api/v1/connectors", status_code=201) def post_connector(body: ConnectorIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): name, settings, secret = _connector_parts(body) row = save_connector(conn, principal.account_id, body.kind, name, settings, secret) return row @router.delete("/api/v1/connectors/{connector_id}", status_code=204) def remove_connector(connector_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): delete_connector(conn, principal.account_id, connector_id) @router.post("/api/v1/connectors/{connector_id}/probe") def probe_connector(connector_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): return probe(conn, principal.account_id, connector_id) @router.get("/api/v1/external-refs/{element_id}/content") def view_paperless( element_id: UUID, context_kind: str | None = None, context_id: str | None = None, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): element = get_element(conn, element_id) if element is None or element["kind"] != "external_ref": raise not_found() rights = element_rights(conn, principal.account_id, element, parse_query_context(context_kind, context_id)) if not rights.read: raise not_found() ref = conn.execute( "SELECT source, external_id FROM external_references WHERE element_id = %s", (element_id,), ).fetchone() if ref is None or ref["source"] != "paperless": raise unprocessable("Nur ein Paperless-Dokument lässt sich hier als Datei ansehen.") data, media_type, filename = paperless_content(conn, principal.account_id, ref["external_id"]) safe_name = (filename or f"paperless-{ref['external_id']}.pdf").replace('"', "") return Response( content=data, media_type=media_type, headers={ "Content-Disposition": f'inline; filename="{safe_name}"', "X-Content-Type-Options": "nosniff", "Cache-Control": "private, no-store", }, ) @router.get("/api/v1/external-refs/{element_id}/mail") def view_mail( element_id: UUID, context_kind: str | None = None, context_id: str | None = None, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): element = get_element(conn, element_id) if element is None or element["kind"] != "external_ref": raise not_found() rights = element_rights(conn, principal.account_id, element, parse_query_context(context_kind, context_id)) if not rights.read: raise not_found() ref = conn.execute( "SELECT source, detail FROM external_references WHERE element_id = %s", (element_id,), ).fetchone() detail = (ref or {}).get("detail") or {} if ref is None or ref["source"] != "imap": raise unprocessable("Das ist keine E-Mail.") connector_id = detail.get("connector_id") mailbox = detail.get("mailbox") or "" uid = detail.get("uid") if not connector_id or not mailbox or not uid: raise unprocessable("Für diese E-Mail ist keine Postfach-Vorschau hinterlegt.") return imap_preview(conn, principal.account_id, UUID(str(connector_id)), str(mailbox), int(uid)) @router.get("/api/v1/sources/paperless/documents") def search_paperless(q: str = "", principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): return {"items": paperless_documents(conn, principal.account_id, q.strip())} @router.get("/api/v1/sources/imap/mailboxes") def list_mailboxes( connector_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): return {"items": imap_mailboxes(conn, principal.account_id, connector_id)} @router.get("/api/v1/sources/imap/messages") def search_mail( connector_id: UUID, q: str = "", mailbox: str = "", before: int | None = None, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): if before is not None and before < 1: raise unprocessable("Die Seitenangabe ist ungültig.") return imap_messages(conn, principal.account_id, connector_id, q.strip(), mailbox.strip(), before) @router.get("/api/v1/sources/imap/message") def read_mail( connector_id: UUID, mailbox: str, uid: int, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): if uid < 1: raise unprocessable("Die Nachricht ist ungültig.") return imap_preview(conn, principal.account_id, connector_id, mailbox.strip(), uid) @router.post("/api/v1/sources/link", status_code=201) def link_source(body: ExternalIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): if body.source not in {"paperless", "imap"}: raise unprocessable("Verknüpft werden Paperless-Dokumente und E-Mails.") element = create_external( conn, principal.account_id, principal.scope_id, body.title, body.source, body.external_id, Ctx(body.context.kind, body.context.id), _ctx_model(body.placement), body.chapter_id, body.dates.document_on, body.dates.sent_on, body.dates.received_on, body.detail.model_dump(exclude_none=True) if body.detail else None, ) return _created(f"/api/v1/elements/{element['id']}", {"id": str(element["id"]), "title": element["title"]}) def _connector_parts(body: ConnectorIn) -> tuple[str, dict, str]: if body.kind == "paperless": if not body.base_url or not body.token or len(body.token.strip()) < 8: raise unprocessable("Paperless braucht eine Adresse und einen API-Schlüssel.") return body.name.strip(), {"base_url": clean_base_url(body.base_url)}, body.token.strip() if not body.host or not body.username or not body.password: raise unprocessable("Ein E-Mail-Konto braucht Server, Benutzername und Passwort.") host = body.host.strip() if " " in host or len(host) > 200: raise unprocessable("Der Mailserver ist ungültig.") return body.name.strip(), { "host": host, "port": body.port or 993, "username": body.username.strip(), "mailbox": (body.mailbox or "INBOX").strip() or "INBOX", "security": body.security or "ssl", }, body.password