# -*- coding: utf-8 -*- """ Satellite-Email Mapper. Ordnet Satellites den konfigurierten E-Mail-Konten zu. Unterstuetzt Matching per Alias und Room-ID — analog zum Calendar-Mapper. """ from trixy_core.utils.debug import pdebug class SatelliteEmailMapper: """ Verwaltet die Zuordnung zwischen Satellites und E-Mail-Konten. Matching-Logik: 1. Satellite-Alias oder Room-ID gegen konfigurierte Eintraege pruefen 2. Globale Accounts werden immer hinzugefuegt 3. Standalone-Modus: Fallback auf ersten Eintrag """ def __init__(self, config: dict, application: object) -> None: self._config = config self._application = application self._accounts_config = config.get("accounts", {}) self._mapping = config.get("satellite_mapping", {}) self._global_accounts = self._mapping.get("global_accounts", []) self._entries = self._mapping.get("satellites", []) def get_accounts_for_satellite(self, satellite_id: str) -> list[str]: """ Gibt alle Account-Keys fuer einen Satellite zurueck. Returns: Liste von Account-Schluesseln (z.B. ["outlook_patrick"]) """ entry = self._find_entry_for_satellite(satellite_id) account_keys: list[str] = [] # Persoenliche Accounts if entry: account_keys.extend(entry.get("accounts", [])) # Globale Accounts for key in self._global_accounts: if key not in account_keys: account_keys.append(key) # Nur existierende Accounts zurueckgeben return [k for k in account_keys if k in self._accounts_config] def get_account_config(self, account_key: str) -> dict: """Gibt die Konfiguration eines Accounts zurueck.""" return self._accounts_config.get(account_key, {}) def get_satellites_for_account(self, account_key: str) -> list[str]: """Gibt alle Satellite-IDs zurueck, die einen Account sehen.""" satellite_ids: list[str] = [] if account_key in self._global_accounts: return self._get_all_connected_satellite_ids() for entry in self._entries: if account_key in entry.get("accounts", []): satellite_ids.extend(self._get_satellite_ids_for_entry(entry)) return satellite_ids def get_all_account_keys(self) -> list[str]: """Gibt alle konfigurierten Account-Keys zurueck.""" return list(self._accounts_config.keys()) def _find_entry_for_satellite(self, satellite_id: str) -> dict | None: """Findet den Mapping-Eintrag fuer einen Satellite.""" satellites = getattr(self._application, "satellites", None) if not satellites: # Standalone-Modus: Ersten Eintrag als Fallback if self._entries: return self._entries[0] return None satellite = satellites.get(satellite_id) if not satellite: return None sat_alias = getattr(satellite, "alias", "").lower() sat_room = getattr(satellite, "room_id", "").lower() for entry in self._entries: alias_list = [a.strip().lower() for a in entry.get("alias", "").split(",")] if sat_alias and sat_alias in alias_list: return entry room_list = [r.strip().lower() for r in entry.get("room", "").split(",")] if sat_room and sat_room in room_list: return entry return None def _get_satellite_ids_for_entry(self, entry: dict) -> list[str]: """Gibt Satellite-IDs fuer einen Mapping-Eintrag zurueck.""" satellites = getattr(self._application, "satellites", None) if not satellites: return [] ids: list[str] = [] alias_list = [a.strip().lower() for a in entry.get("alias", "").split(",")] room_list = [r.strip().lower() for r in entry.get("room", "").split(",")] for sat in satellites: sat_alias = getattr(sat, "alias", "").lower() sat_room = getattr(sat, "room_id", "").lower() if (sat_alias and sat_alias in alias_list) or (sat_room and sat_room in room_list): ids.append(sat.id) return ids def _get_all_connected_satellite_ids(self) -> list[str]: """Gibt alle verbundenen Satellite-IDs zurueck.""" satellites = getattr(self._application, "satellites", None) if not satellites: return [] return [sat.id for sat in satellites if sat.is_connected]