Files
leocrm/app/plugins/builtins/mail/plugin.py
T

76 lines
2.4 KiB
Python
Raw Normal View History

"""Mail plugin — IMAP/SMTP, threading, templates, rules, PGP, delegates."""
from __future__ import annotations
import asyncio
import logging
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: sync all active mail accounts every 5 minutes."""
from app.plugins.builtins.mail.services import auto_sync_all_accounts
while True:
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.0.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"],
permissions=[],
)
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")
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)