c24a86bc90
- imap_delete_mail: use stored imap_uid instead of Message-ID search, raise on failure - imap_move_mail: same imap_uid fix, raise on failure - New MailSyncQueue model + migration 0007 for pending IMAP operations - delete_mail route: queues failed IMAP delete for retry - move_mail route: queues failed IMAP move for retry - process_sync_queue: retries pending delete/move operations in auto-sync loop - Auto-sync loop: processes sync queue before pulling new mails - Restore soft-deleted mails if they still exist on IMAP server during sync - Upload sent mails to IMAP Sent folder via APPEND after SMTP send - Plugin version bumped to 1.2.0
100 lines
4.9 KiB
Python
100 lines
4.9 KiB
Python
"""Mail plugin — IMAP/SMTP, threading, templates, rules, PGP, delegates."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
from typing import Any
|
|
|
|
from app.plugins.base import BasePlugin
|
|
from app.plugins.manifest import PluginManifest, PluginRouteDef
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def _auto_sync_loop() -> None:
|
|
"""Background loop: process pending sync queue, then sync all active mail accounts every 5 minutes."""
|
|
from app.plugins.builtins.mail.services import auto_sync_all_accounts, process_sync_queue
|
|
from app.core.db import get_session_factory
|
|
|
|
while True:
|
|
try:
|
|
factory = get_session_factory()
|
|
async with factory() as db:
|
|
await process_sync_queue(db)
|
|
await db.commit()
|
|
except Exception as exc:
|
|
logger.warning("process_sync_queue error: %s", exc)
|
|
try:
|
|
await auto_sync_all_accounts()
|
|
except Exception as exc:
|
|
logger.warning("auto_sync error: %s", exc)
|
|
await asyncio.sleep(300)
|
|
|
|
|
|
class MailPlugin(BasePlugin):
|
|
"""Mail plugin for email management: IMAP sync, SMTP send, threading, rules, PGP."""
|
|
|
|
_auto_sync_task: asyncio.Task | None = None
|
|
|
|
manifest = PluginManifest(
|
|
name="mail",
|
|
version="1.2.0",
|
|
display_name="Mail",
|
|
description=(
|
|
"Email management: IMAP sync, SMTP send, threading, "
|
|
"templates, rules, vacation, PGP, delegates, labels."
|
|
),
|
|
dependencies=[],
|
|
routes=[
|
|
PluginRouteDef(
|
|
path="/api/v1/mail",
|
|
module="app.plugins.builtins.mail.routes",
|
|
router_attr="router",
|
|
),
|
|
],
|
|
events=[],
|
|
migrations=["0001_initial.sql", "0006_flag_type.sql", "0007_sync_queue.sql"],
|
|
permissions=["mail:read", "mail:send", "mail:config", "mail:share", "mail:write", "mail:delete"],
|
|
)
|
|
|
|
async def on_activate(
|
|
self, db, service_container, event_bus
|
|
) -> None:
|
|
"""Activate plugin: register events + start auto-sync background task."""
|
|
await super().on_activate(db, service_container, event_bus)
|
|
|
|
if self._auto_sync_task is None or self._auto_sync_task.done():
|
|
self._auto_sync_task = asyncio.create_task(_auto_sync_loop())
|
|
logger.info("Mail plugin: auto-sync background task started")
|
|
|
|
def get_notification_types(self) -> list[dict[str, Any]]:
|
|
"""Return the notification types this mail plugin registers."""
|
|
return [
|
|
{"type_key": "mail_new", "category": "mail", "label": "Neue E-Mail empfangen", "description": "Benachrichtigung bei neuen E-Mails", "is_enabled_by_default": True},
|
|
{"type_key": "mail_error", "category": "mail", "label": "IMAP-Verbindungsfehler", "description": "Fehler bei der Verbindung zum Mailserver", "is_enabled_by_default": True},
|
|
{"type_key": "mail_auth", "category": "mail", "label": "IMAP-Login-Fehler", "description": "Anmeldung am Mailserver fehlgeschlagen", "is_enabled_by_default": True},
|
|
{"type_key": "mail_quota", "category": "mail", "label": "Postfach fast voll", "description": "Warnung bei hohem Postfach-Füllstand", "is_enabled_by_default": True},
|
|
{"type_key": "mail_sync_error", "category": "mail", "label": "Sync-Fehler", "description": "Synchronisierung fehlgeschlagen", "is_enabled_by_default": True},
|
|
{"type_key": "mail_sent", "category": "mail", "label": "E-Mail gesendet", "description": "Bestätigung beim Senden einer E-Mail", "is_enabled_by_default": False},
|
|
{"type_key": "mail_send_error", "category": "mail", "label": "SMTP-Sendefehler", "description": "E-Mail konnte nicht gesendet werden", "is_enabled_by_default": True},
|
|
{"type_key": "mail_draft", "category": "mail", "label": "Entwurf gespeichert", "description": "Bestätigung beim Speichern eines Entwurfs", "is_enabled_by_default": False},
|
|
{"type_key": "mail_account", "category": "mail", "label": "Account deaktiviert", "description": "Warnung bei deaktiviertem Mail-Account", "is_enabled_by_default": True},
|
|
{"type_key": "mail_folder", "category": "mail", "label": "Ordner erstellt/gelöscht", "description": "Bestätigung bei Ordner-Operationen", "is_enabled_by_default": False},
|
|
]
|
|
|
|
async def on_deactivate(
|
|
self, db, service_container, event_bus
|
|
) -> None:
|
|
"""Deactivate plugin: stop auto-sync task + unregister events."""
|
|
if self._auto_sync_task is not None and not self._auto_sync_task.done():
|
|
self._auto_sync_task.cancel()
|
|
try:
|
|
await self._auto_sync_task
|
|
except asyncio.CancelledError:
|
|
pass
|
|
self._auto_sync_task = None
|
|
logger.info("Mail plugin: auto-sync background task stopped")
|
|
|
|
await super().on_deactivate(db, service_container, event_bus)
|