Monitor an IMAP mailbox via IDLE for iMIP emails (invitations, RSVPs, cancellations) and ICS attachments, then update a CalDAV calendar. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
"""Entry point for imap-idle-caldav daemon."""
|
|
|
|
import logging
|
|
import signal
|
|
import sys
|
|
import threading
|
|
|
|
from .caldav_client import CalDAVHandler
|
|
from .config import load_config, parse_args
|
|
from .email_processor import process_email
|
|
from .imap_client import run_idle_loop
|
|
|
|
log = logging.getLogger("imap_idle_caldav")
|
|
|
|
|
|
def main() -> None:
|
|
config_path = parse_args()
|
|
config = load_config(config_path)
|
|
|
|
logging.basicConfig(
|
|
level=getattr(logging, config.logging.level.upper(), logging.INFO),
|
|
format="%(asctime)s %(levelname)s [%(name)s] %(message)s",
|
|
stream=sys.stderr,
|
|
)
|
|
|
|
caldav_handler = CalDAVHandler(config.caldav)
|
|
caldav_handler.connect()
|
|
|
|
shutdown = threading.Event()
|
|
|
|
def on_signal(signum, _frame):
|
|
log.info("Received signal %s, shutting down", signal.Signals(signum).name)
|
|
shutdown.set()
|
|
|
|
signal.signal(signal.SIGTERM, on_signal)
|
|
signal.signal(signal.SIGINT, on_signal)
|
|
|
|
def on_message(raw_bytes: bytes) -> None:
|
|
events = process_email(raw_bytes)
|
|
for event in events:
|
|
caldav_handler.handle_event(event)
|
|
|
|
log.info("Starting IMAP IDLE daemon")
|
|
run_idle_loop(config.imap, on_message, shutdown_event=shutdown)
|
|
log.info("Daemon stopped")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|