fix(calendar): TZID Europe/Berlin for timed events/tasks, resolve real resource href on delete/update, fix _report_tasks XML; sync deployed servers (owamail, openwearables, ghostfolio), docs and tests

This commit is contained in:
Stefan Lohmaier committed 2026-10-01 04:29:57 +02:00
1 parent 7c01e7be53
commit 75185ef2ba
41 files changed
+8834 -324

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
"""Inkrementeller Maildir-Index-Update fuer alle User (stefan, kati).
Aufruf: python3 index_maildirs.py # inkrementell, alle User
python3 index_maildirs.py --full # erzwingt Reindex (fuer Tests)
python3 index_maildirs.py --user kati # nur ein User
Wird vom systemd-Timer mail-index.timer nach jedem mail-backup-Sync aufgerufen.
"""
import os
import sys
import time
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_DIR)
sys.path.insert(0, os.path.join(BASE_DIR, "mail"))
from common import load_config as _lc # noqa: E402
from mail.mailindex import update_user, connect, _SCHEMA # noqa: E402
LOG_FILE = "/var/log/mail-index.log"
def log(msg):
line = f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] {msg}"
print(line, flush=True)
try:
with open(LOG_FILE, "a") as f:
f.write(line + "\n")
except OSError:
pass
def main():
args = [a for a in sys.argv[1:]]
full = "--full" in args
only_user = None
if "--user" in args:
only_user = args[args.index("--user") + 1]
cfg = _lc()
roots = cfg.get("mail_roots", {})
if only_user:
roots = {only_user: roots.get(only_user)}
for user, root in sorted(roots.items()):
if not root or not os.path.isdir(root):
log(f"SKIP {user}: root fehlt ({root})")
continue
t0 = time.monotonic()
if full:
# Reindex: DB zuruecksetzen, dann komplett neu
con = connect(user)
con.executescript("DROP TABLE IF EXISTS messages_fts; DROP TABLE IF EXISTS messages;")
con.executescript(_SCHEMA)
con.commit()
con.close()
log(f"REINDEX {user}: Tabellen neu erstellt")
added, updated, removed = update_user(user, root, log=log)
dur = time.monotonic() - t0
# Erst-Build abgeschlossen → Suche darf den Index nutzen
from mail.mailindex import _ready_path
with open(_ready_path(user), "w") as f:
f.write(time.strftime("%Y-%m-%d %H:%M:%S"))
log(f"OK {user}: added={added} updated={updated} removed={removed} ({dur:.1f}s)")
if __name__ == "__main__":
main()
+377
View File
@@ -0,0 +1,377 @@
#!/usr/bin/env python3
"""Maildir-Volltextindex (SQLite FTS5) fuer den MCP-Mail-Server.
Hintergrund: search_mail scannte bei jeder Anfrage das komplette Maildir
(Roh-Byte-Vorfilter ueber alle Dateien). Bei Katis Gmail-Archiv (16 GB,
~30k Mails) dauerte das laenger als das Gateway-Request-Timeout → "Request
timed out". Dieser Index macht Suchen zu FTS5-SQL-Queries (Millisekunden).
Design:
- Eine SQLite-DB pro User unter /var/lib/mail-index/<user>.sqlite
- Tabelle messages (Stammdaten + body) + virtuelle Tabelle messages_fts
(FTS5, content='messages', mit Trigger-Sync via externem Content)
- Inkrementell: Datei-Pfad + mtime + size in messages; beim Update nur
neue/geaenderte Dateien neu einlesen, entfernte loeschen
- Such-Strategie: FTS5-MATCH mit Prefix-Termen; Fallback auf LIKE ueber
sender/to/subject (deckt Substrings und E-Mail-Adressen ab), zuletzt
LIKE ueber body
"""
import os
import re
import sqlite3
import sys
import time
import html as htmlmod
from email import policy
from email.parser import BytesParser
from email.utils import parsedate_to_datetime
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_DIR)
from common import load_config as _lc # noqa: E402
INDEX_DIR = "/var/lib/mail-index"
_SCHEMA = """
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY,
path TEXT UNIQUE NOT NULL,
account TEXT NOT NULL,
folder TEXT NOT NULL,
key TEXT NOT NULL,
mtime REAL NOT NULL,
size INTEGER NOT NULL,
date_ts INTEGER DEFAULT 0,
sent_from TEXT DEFAULT '',
sent_to TEXT DEFAULT '',
subject TEXT DEFAULT '',
body TEXT DEFAULT ''
);
CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts USING fts5(
sent_from, sent_to, subject, body,
content='messages', content_rowid='id',
tokenize='unicode61'
);
CREATE TRIGGER IF NOT EXISTS messages_ai AFTER INSERT ON messages BEGIN
INSERT INTO messages_fts(rowid, sent_from, sent_to, subject, body)
VALUES (new.id, new.sent_from, new.sent_to, new.subject, new.body);
END;
CREATE TRIGGER IF NOT EXISTS messages_ad AFTER DELETE ON messages BEGIN
INSERT INTO messages_fts(messages_fts, rowid, sent_from, sent_to, subject, body)
VALUES ('delete', old.id, old.sent_from, old.sent_to, old.subject, old.body);
END;
CREATE TRIGGER IF NOT EXISTS messages_au AFTER UPDATE ON messages BEGIN
INSERT INTO messages_fts(messages_fts, rowid, sent_from, sent_to, subject, body)
VALUES ('delete', old.id, old.sent_from, old.sent_to, old.subject, old.body);
INSERT INTO messages_fts(rowid, sent_from, sent_to, subject, body)
VALUES (new.id, new.sent_from, new.sent_to, new.subject, new.body);
END;
"""
def _term_list(query):
"""Nicht-alphanumerische Zeichen als Trenner; Lowercase."""
return [t for t in re.split(r"[^0-9A-Za-z_]+", query.lower()) if t]
def _decode_hdr(raw):
from email.header import decode_header
if not raw:
return ""
parts = decode_header(str(raw))
out = []
for data, charset in parts:
if isinstance(data, bytes):
for cs in (charset, "utf-8", "latin-1"):
if not cs:
continue
try:
out.append(data.decode(cs, errors="replace"))
break
except (LookupError, TypeError):
continue
else:
out.append(data.decode("latin-1", errors="replace"))
else:
out.append(str(data))
return " ".join(out)
def _extract_textparts(msg):
"""Text-Bodies sammeln: text/plain bevorzugt, sonst text/html (ohne Tags)."""
plains, htmls = [], []
if msg.is_multipart():
for part in msg.walk():
ct = part.get_content_type()
payload = part.get_payload(decode=True)
if not payload:
continue
try:
text = payload.decode(part.get_content_charset() or "utf-8", errors="replace")
except (LookupError, TypeError):
text = payload.decode("latin-1", errors="replace")
if ct == "text/plain":
plains.append(text)
elif ct == "text/html":
htmls.append(text)
else:
payload = msg.get_payload(decode=True)
if payload:
try:
plains.append(payload.decode(msg.get_content_charset() or "utf-8", errors="replace"))
except (LookupError, TypeError):
plains.append(payload.decode("latin-1", errors="replace"))
if plains:
return "\n".join(plains)
if htmls:
out = []
for h in htmls:
h = re.sub(r"<style.*?</style>", " ", h, flags=re.S | re.I)
h = re.sub(r"<script.*?</script>", " ", h, flags=re.S | re.I)
h = re.sub(r"<[^>]+>", " ", h)
out.append(htmlmod.unescape(h))
return "\n".join(out)
return ""
def _parse_file(path, root):
"""Mail-Datei einlesen → (account, folder, key, date_ts, sender, to, subject, body).
Pfadform unter root: <account>/<folder>/cur/<key>
"""
rel = os.path.relpath(path, root)
parts = rel.split(os.sep)
# erwartet: [account, folder, "cur", key] — aber Ordner koennen verschachtelt sein
key = parts[-1]
folder = os.sep.join(parts[1:-2]) if len(parts) > 3 else parts[-3]
account = parts[0]
try:
with open(path, "rb") as f:
raw = f.read()
except OSError:
return None
try:
msg = BytesParser(policy=policy.default).parsebytes(raw)
except Exception:
return None
date_str = str(msg.get("Date", ""))
date_ts = 0
if date_str:
try:
dt = parsedate_to_datetime(date_str)
if dt:
date_ts = int(dt.timestamp())
except Exception:
pass
sender = _decode_hdr(msg.get("From", ""))
to = _decode_hdr(msg.get("To", ""))
subject = _decode_hdr(msg.get("Subject", ""))
body = _extract_textparts(msg)
return account, folder, key, date_ts, sender, to, subject, body
def index_db_path(user):
return os.path.join(INDEX_DIR, f"{user}.sqlite")
def _ready_path(user):
"""Marker: Erst-Build abgeschlossen → Suche darf den Index nutzen."""
return os.path.join(INDEX_DIR, f"{user}.ready")
def connect(user, create=True):
os.makedirs(INDEX_DIR, exist_ok=True)
db = index_db_path(user)
con = sqlite3.connect(db, timeout=30)
con.execute("PRAGMA journal_mode=WAL")
con.execute("PRAGMA synchronous=NORMAL")
if create:
con.executescript(_SCHEMA)
return con
def update_user(user, root, log=print):
"""Inkrementeller Index-Update fuer einen User-Maildir-Root.
Gibt (added, updated, removed) zurueck.
"""
if not os.path.isdir(root):
log(f" [{user}] Root fehlt: {root}")
return 0, 0, 0
con = connect(user)
try:
seen = set()
added = updated = removed = 0
# cur/ + new/-Dateien aller Ordner einsammeln (followlinks: erlaubt Account-Symlinks wie nova->.com).
# new/ = ungelesene Mails (mbsync legt \Unseen dort ab); ohne new/ fehlten sie im Index.
for dirpath, dirnames, filenames in os.walk(root, followlinks=True):
if os.path.basename(dirpath) not in ("cur", "new"):
continue
for fn in filenames:
path = os.path.join(dirpath, fn)
try:
st = os.stat(path)
except OSError:
continue
mtime, size = st.st_mtime, st.st_size
seen.add(path)
row = con.execute(
"SELECT id FROM messages WHERE path=?", (path,)
).fetchone()
if row:
cur = con.execute(
"SELECT mtime, size FROM messages WHERE id=?", (row[0],)
).fetchone()
if cur and abs(cur[0] - mtime) < 0.001 and cur[1] == size:
continue # unveraendert
updated += 1
elif size == 0:
continue # leere Datei beim Sync noch nicht fertig
else:
added += 1
parsed = _parse_file(path, root)
if not parsed:
con.execute("DELETE FROM messages WHERE path=?", (path,))
continue
account, folder, key, date_ts, sender, to, subject, body = parsed
con.execute(
"""INSERT INTO messages
(path, account, folder, key, mtime, size, date_ts,
sent_from, sent_to, subject, body)
VALUES (?,?,?,?,?,?,?,?,?,?,?)
ON CONFLICT(path) DO UPDATE SET
account=excluded.account, folder=excluded.folder,
key=excluded.key, mtime=excluded.mtime, size=excluded.size,
date_ts=excluded.date_ts, sent_from=excluded.sent_from,
sent_to=excluded.sent_to, subject=excluded.subject,
body=excluded.body""",
(path, account, folder, key, mtime, size, date_ts,
sender, to, subject, body),
)
# in DB, aber Datei weg → loeschen
for (path,) in con.execute("SELECT path FROM messages").fetchall():
if path not in seen:
con.execute("DELETE FROM messages WHERE path=?", (path,))
removed += 1
con.commit()
return added, updated, removed
finally:
con.close()
def _fts_query(query):
"""FTS5-MATCH-String: alle Terme als Praefix-OR innerhalb eines Terms
(ADDED-Optimierung: mehrere Woerter muessen ALLE vorkommen → AND)."""
terms = _term_list(query)
if not terms:
return None
# jedes Wort als Prefix-Term: "term"* — escapen von Anführungszeichen
parts = []
for t in terms:
t = t.replace('"', "")
parts.append(f'"{t}"*')
return " AND ".join(parts)
def search(user, query, account="", folder="", limit=20, log=print):
"""Volltextsuche im Index. Liefert Liste von Dicts
{account, folder, key, date, sender, to, subject} — gleiches Format
wie der alte Vollscan, damit search_mail unveraendert weitermacht.
"""
db = index_db_path(user)
if not os.path.exists(db) or not os.path.exists(_ready_path(user)):
return None # Index noch nicht (vollstaendig) gebaut → Fallback im Server
con = connect(user, create=False)
try:
term_like = f"%{query.lower()}%"
base = "SELECT account, folder, key, date_ts, sent_from, sent_to, subject FROM messages"
where_clauses = []
params = []
# E-Mail-Adressen: FTS5 zerlegt sie in Einzelwoerter mit zu striktem
# AND → direkt ueber den LIKE-Pfad (exakt auf sender/to/subject).
has_email = "@" in query
if not has_email:
fts = _fts_query(query)
if fts:
where_clauses.append("id IN (SELECT rowid FROM messages_fts WHERE messages_fts MATCH ?)")
params.append(fts)
if account:
where_clauses.append("account=?")
params.append(account)
if folder:
where_clauses.append("folder LIKE ?")
params.append(f"%{folder}%")
out = []
if where_clauses:
where_sql = " WHERE " + " AND ".join(where_clauses)
rows = con.execute(
f"{base}{where_sql} ORDER BY date_ts DESC, id DESC LIMIT ?",
params + [limit],
).fetchall()
for account_, folder_, key_, date_ts, sender, to, subject in rows:
out.append({
"account": account_, "folder": folder_, "key": key_,
"date": _fmt_date(date_ts), "sender": sender, "to": to,
"subject": subject,
})
# Fallback: LIKE-Suche (deckt Substrings + E-Mail-Adressen ab)
if not out:
cols = ["sent_from LIKE ?", "sent_to LIKE ?", "subject LIKE ?"]
all_p = [term_like] * len(cols)
extra_where = []
if account:
extra_where.append("account=?")
all_p.append(account)
if folder:
extra_where.append("folder LIKE ?")
all_p.append(f"%{folder}%")
sql = f"{base} WHERE ({' OR '.join(cols)})"
if extra_where:
sql += " AND " + " AND ".join(extra_where)
sql += " ORDER BY date_ts DESC, id DESC LIMIT ?"
all_p.append(limit)
rows2 = con.execute(sql, all_p).fetchall()
for account_, folder_, key_, date_ts, sender, to, subject in rows2:
out.append({
"account": account_, "folder": folder_, "key": key_,
"date": _fmt_date(date_ts), "sender": sender, "to": to,
"subject": subject,
})
return out
finally:
con.close()
def _fmt_date(ts):
if not ts:
return ""
return time.strftime("%a, %d %b %Y %H:%M:%S %z", time.localtime(ts))
if __name__ == "__main__":
# CLI: python3 mailindex.py <user> --update|--search "<query>" [limit]
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("user")
ap.add_argument("--update", action="store_true", help="inkrementeller Update")
ap.add_argument("--search", nargs="?", default=None)
ap.add_argument("--limit", type=int, default=10)
args = ap.parse_args()
cfg = _lc()
root = cfg["mail_roots"].get(args.user)
if args.update:
if not root:
print(f"kein root fuer {args.user}")
sys.exit(1)
a, u, r = update_user(args.user, root)
print(f"[{args.user}] added={a} updated={u} removed={r}")
elif args.search:
res = search(args.user, args.search, limit=args.limit)
if res is None:
print("kein index")
elif not res:
print("keine treffer")
else:
for r in res:
print(f"[{r['date']}] {r['sender']} -> {r['to']}\n {r['subject']}\n {r['account']}/{r['folder']}/{r['key']}")
else:
ap.print_help()
+203 -15
View File
@@ -12,6 +12,9 @@ import time
import ctypes
from email.header import decode_header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from email.utils import formatdate, parsedate_to_datetime
from pathlib import Path
from typing import Annotated
@@ -20,7 +23,8 @@ from pydantic import Field
from mcp.server.fastmcp import FastMCP
from mcp.types import TextContent, ImageContent, EmbeddedResource, BlobResourceContents
from starlette.applications import Starlette
from starlette.routing import Mount
from starlette.routing import Mount, Route
from starlette.responses import Response
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from common import get_current_user, OAUTH_ROUTES, BearerAuthMiddleware
@@ -46,6 +50,13 @@ IMAP_ACCOUNTS = {
"drafts_folder": "[Google Mail]/Entw&APw-rfe",
"from_addr": "akpolke@gmail.com",
},
"nova": {
"host": "imap.1blu.de",
"user": "d370128_0-stefanlohmaier",
"password": _cfg["imap_passwords"]["nova"],
"drafts_folder": "Drafts",
"from_addr": "stefan@slohmaier.com",
},
}
mcp = FastMCP("Mail", stateless_http=True,
@@ -148,15 +159,20 @@ def _discover_accounts(user):
root = MAIL_ROOTS.get(user)
if not root or not os.path.isdir(root):
return {}
return {a: os.path.join(root, a) for a in sorted(os.listdir(root)) if os.path.isdir(os.path.join(root, a))}
allow = _cfg.get("mail_account_allow", {}).get(user)
accounts = {a: os.path.join(root, a) for a in sorted(os.listdir(root)) if os.path.isdir(os.path.join(root, a))}
if not allow:
return accounts
return {a: p for a, p in accounts.items() if a in allow}
def _discover_folders(acct_path):
folders = []
for entry in sorted(Path(acct_path).rglob("cur")):
rel = str(entry.parent.relative_to(acct_path))
if rel != "." and rel not in folders:
folders.append(rel)
for dirpath, dirnames, _ in os.walk(acct_path, followlinks=True):
if "cur" in dirnames or "new" in dirnames:
rel = os.path.relpath(dirpath, acct_path)
if rel != "." and rel not in folders:
folders.append(rel)
if "INBOX" in folders:
folders.remove("INBOX")
folders.insert(0, "INBOX")
@@ -176,6 +192,30 @@ MAX_SCAN = 2000 # Obergrenze gesammelter Treffer (Speicher-Schutz bei breiten Q
DEADLINE_S = 120 # Wall-Clock-Limit: Server heilt sich selbst, nie wieder endloser Haenger
def _norm_key(key: str) -> str:
"""Normalize an index key to the Python-mailbox key (strip Dovecot ':2,<flags>'
suffix). FTS index stores the full file name (...:2,T), while mailbox.Maildir
keys drop the flags suffix; md.get() would otherwise miss every message."""
if key and ":" in key:
return key.split(":", 1)[0]
return key
def _imap_quote_folder(folder: str) -> str:
"""Quote an IMAP mailbox name for commands that take a bare atom.
imaplib unterdrueckt das Quoting bei append()/select() mit Literal-Syntax
({n}), daher bricht Gmail bei Ordnernamen mit Sonderzeichen (z.B.
'[Google Mail]/Entw&APw-rfe' mit '&') mit 'BAD Could not parse command' ab.
Nur wenn der Name Zeichen enthaelt, die ein Atom stoeren, in Quotes setzen.
"""
if not folder:
return folder
if any(c in folder for c in ' &[]\\"'):
return '"' + folder.replace('"', '\\"') + '"'
return folder
def _iter_messages(md):
"""Yield (key, msg); ueberspringt unparsbare Mails statt die Suche zu killen."""
try:
@@ -236,10 +276,23 @@ def search_mail(
user = get_current_user()
if not user:
return "Error: not authenticated"
# FTS5-Index (schnell); Fallback auf Vollscan wenn noch kein Index gebaut wurde.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import mailindex
t0 = time.monotonic()
hits = mailindex.search(user, query, account=account, folder=folder, limit=limit)
if hits is not None:
if not hits:
return "No results found"
lines = [f"[{h['date']}] {h['sender']} -> {h['to']}\n Subject: {h['subject']}\n Account: {h['account']}, Folder: {h['folder']}, Key: {h['key']}"
for h in hits]
return "\n\n".join(lines)
# Fallback: klassischer Maildir-Scan (nur solange der Index fehlt)
query_lower = query.lower()
matches = []
truncated = False
t0 = time.monotonic()
for acct_name, acct_path in _discover_accounts(user).items():
if account and account not in acct_name:
continue
@@ -318,7 +371,7 @@ def read_mail(
md = _open_folder(acct_path, folder)
if not md:
return f"Folder not found: {folder}"
msg = md.get(key)
msg = md.get(_norm_key(key))
if not msg:
return f"Message not found: {key}"
out = (
@@ -355,7 +408,7 @@ def read_attachment(
md = _open_folder(acct_path, folder)
if not md:
return [TextContent(type="text", text=f"Folder not found: {folder}")]
msg = md.get(key)
msg = md.get(_norm_key(key))
if not msg:
return [TextContent(type="text", text=f"Message not found: {key}")]
attachments = _get_attachments(msg)
@@ -377,6 +430,81 @@ def read_attachment(
blob=base64.b64encode(payload).decode(), mimeType=mime))]
def _sanitize_filename(name):
"""Safe filename: keep letters/digits/._-, no path separators."""
import re
base = re.sub(r"[^A-Za-z0-9._-]+", "_", name).strip("._")
return base[:160] or "attachment.bin"
@mcp.tool()
def download_attachment(
account: Annotated[str, Field(description="Account name from search results")],
folder: Annotated[str, Field(description="Folder name from search results")],
key: Annotated[str, Field(description="Message key from search results")],
attachment_index: Annotated[int, Field(description="Attachment number from the read_mail attachment list (1-based)")],
) -> str:
"""Download an email attachment as a file into the agent workspace and return the file path. Use the attachment index from read_mail; the returned path is directly readable by the agent inside the container. Falls back to listing matching files if the message key is not found literally."""
_trim_memory()
user = get_current_user()
if not user:
return "Error: not authenticated"
acct_path = _discover_accounts(user).get(account)
if not acct_path:
return f"Account not found: {account}"
md = _open_folder(acct_path, folder)
if not md:
return f"Folder not found: {folder}"
msg = md.get(_norm_key(key))
if not msg:
# Fallback: Key waechst manchmal (Maildir-Read), suche erste passende Nachricht
found = None
try:
for k in list(md.keys()):
if k.startswith(key):
found = k
break
except Exception:
pass
if found:
msg = md.get(found)
if not msg:
return f"Message not found: {key}"
attachments = _get_attachments(msg)
att = next((a for a in attachments if a["index"] == attachment_index), None)
if not att:
return f"Anhang {attachment_index} nicht gefunden. {len(attachments)} Anhaenge vorhanden."
dirs = _cfg.get("mail_attachment_dirs", {})
d = dirs.get(user)
if not d:
return f"Kein Download-Verzeichnis fuer User {user} konfiguriert"
host_dir = os.path.expanduser(d["host"])
container_dir = os.path.expanduser(d["container"])
try:
os.makedirs(host_dir, exist_ok=True)
except Exception as e:
return f"Fehler beim Anlegen des Zielverzeichnisses: {e}"
filename = _sanitize_filename(att["filename"])
dest_host = os.path.join(host_dir, filename)
# Kollision aufloesen: (1), (2), ...
base, ext = os.path.splitext(dest_host)
n = 1
while os.path.exists(dest_host):
dest_host = f"{base} ({n}){ext}"
n += 1
if n > 100:
return f"Zu viele gleichnamige Dateien in {host_dir}"
try:
with open(dest_host, "wb") as fh:
fh.write(att["payload"])
except Exception as e:
return f"Fehler beim Schreiben: {e}"
dest_container = os.path.join(container_dir, os.path.basename(dest_host))
return (f"Anhang heruntergeladen: {dest_container}\n"
f"Datei: {att['filename']} ({att['mime']}, {att['size']:,} bytes)\n"
f"Gespeichert als: {dest_container}")
@mcp.tool()
def create_draft(
to: Annotated[str, Field(description="Recipient email address, e.g. 'max@example.com'")],
@@ -384,14 +512,22 @@ def create_draft(
body: Annotated[str, Field(description="Email body text (plain text)")],
cc: Annotated[str, Field(description="CC recipients, comma-separated")] = "",
account: Annotated[str, Field(description="Which account to create the draft in. Leave empty for default account.")] = "",
attachment_filenames: Annotated[list, Field(description="List of attachment file names, e.g. ['angebot.pdf']. Optional. Must match attachment_contents order.")] = None,
attachment_contents: Annotated[list, Field(description="List of attachment contents as Base64-encoded strings. Optional. Must match attachment_filenames order.")] = None,
attachment_mimetypes: Annotated[list, Field(description="List of attachment MIME types, e.g. ['application/pdf']. Optional. If omitted, guessed from file extension.")] = None,
) -> str:
"""Create an email draft in the Drafts folder. The draft can then be reviewed and sent from Roundcube or phone. Does NOT send the email."""
"""Create an email draft in the Drafts folder, optionally with attachments. Attachments are passed as Base64 content (attachment_contents) with matching filenames. The draft can then be reviewed and sent from Roundcube or phone. Does NOT send the email."""
user = get_current_user()
if not user: return "Error: not authenticated"
acct = IMAP_ACCOUNTS.get(user)
if not acct: return f"Kein IMAP-Account fuer {user} konfiguriert"
msg = MIMEText(body, "plain", "utf-8")
has_attachments = (attachment_filenames and attachment_contents) and len(attachment_filenames) == len(attachment_contents)
if has_attachments:
msg = MIMEMultipart()
else:
msg = MIMEText(body, "plain", "utf-8")
msg["From"] = acct["from_addr"]
msg["To"] = to
if cc:
@@ -400,18 +536,68 @@ def create_draft(
msg["Date"] = formatdate(localtime=True)
msg["X-Mailer"] = "MCP Mail Server"
if has_attachments:
msg.attach(MIMEText(body, "plain", "utf-8"))
for i, fname in enumerate(attachment_filenames):
try:
content = base64.b64decode(attachment_contents[i])
except Exception as e:
return f"Fehler: Anhang '{fname}' nicht als gueltiges Base64 decodierbar: {e}"
mime = attachment_mimetypes[i] if (attachment_mimetypes and i < len(attachment_mimetypes)) else "application/octet-stream"
part = MIMEBase(*mime.split("/", 1))
part.set_payload(content)
encoders.encode_base64(part)
part.add_header("Content-Disposition", "attachment", filename=fname)
msg.attach(part)
try:
imap = imaplib.IMAP4_SSL(acct["host"])
imap.login(acct["user"], acct["password"])
status, _ = imap.append(acct["drafts_folder"], "\\Draft", None, msg.as_bytes())
status, _ = imap.append(_imap_quote_folder(acct["drafts_folder"]), "\\Draft", None, msg.as_bytes())
imap.logout()
if status == "OK":
return f"Entwurf erstellt: An {to}, Betreff \"{subject}\". Oeffne Roundcube oder die Mail-App um ihn zu senden."
n_att = len(attachment_filenames) if has_attachments else 0
att_str = f" mit {n_att} Anhang/Anhaengen" if n_att else ""
return f"Entwurf erstellt{att_str}: An {to}, Betreff \"{subject}\". Oeffne Roundcube oder die Mail-App um ihn zu senden."
return f"Fehler beim Erstellen des Entwurfs: {status}"
except Exception as e:
return f"IMAP-Fehler: {e}"
async def raw_mail(request):
"""Roher Anhang-Download (beliebige Binaerdateien).
GET /raw?account=&folder=&key=&index= -> Anhang als Rohbytes.
Token-geschuetzt ueber BearerAuthMiddleware.
"""
user = get_current_user()
if not user:
return Response("unauthorized", status_code=401)
q = request.query_params
account = q.get("account", "")
folder = q.get("folder", "")
key = q.get("key", "")
try:
index = int(q.get("index", "0"))
except ValueError:
return Response("invalid index", status_code=400)
acct_path = _discover_accounts(user).get(account)
if not acct_path:
return Response(f"Account not found: {account}", status_code=404)
md = _open_folder(acct_path, folder)
if not md:
return Response(f"Folder not found: {folder}", status_code=404)
msg = md.get(_norm_key(key))
if not msg:
return Response(f"Message not found: {key}", status_code=404)
att = next((a for a in _get_attachments(msg) if a["index"] == index), None)
if not att:
return Response(f"Anhang {index} nicht gefunden", status_code=404)
fn = _sanitize_filename(att["filename"])
return Response(att["payload"], media_type=att["mime"],
headers={"Content-Disposition": f'attachment; filename="{fn}"'})
def create_app():
from contextlib import asynccontextmanager
mcp_app = mcp.streamable_http_app()
@@ -422,7 +608,9 @@ def create_app():
await stack.enter_async_context(mcp_app.router.lifespan_context(mcp_app))
yield
routes = list(OAUTH_ROUTES) + [Mount("/", app=mcp_app)]
routes = list(OAUTH_ROUTES) + [
Route("/raw", raw_mail, methods=["GET"]),
] + [Mount("/", app=mcp_app)]
app = Starlette(routes=routes, lifespan=lifespan)
app.add_middleware(BearerAuthMiddleware)
return app
@@ -430,4 +618,4 @@ def create_app():
if __name__ == "__main__":
import uvicorn
uvicorn.run(create_app(), host="127.0.0.1", port=5100)
uvicorn.run(create_app(), host="0.0.0.0", port=5100)