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

+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)