"""Addressbook — local hostname to destination database.""" from __future__ import annotations import time from dataclasses import dataclass, field @dataclass class AddressbookEntry: """A single hostname→destination mapping.""" hostname: str destination: str # Base64-encoded Destination added_at: float = field(default_factory=time.monotonic) source: str = "local" # "local", "subscription", "manual" class Addressbook: """Local hostname -> destination database.""" def __init__(self) -> None: self._entries: dict[str, AddressbookEntry] = {} # lowercase hostname -> entry def lookup(self, hostname: str) -> str | None: """Look up destination for hostname. Case-insensitive.""" entry = self._entries.get(hostname.lower()) return entry.destination if entry is not None else None def add_entry(self, hostname: str, destination: str, source: str = "local") -> None: """Add or update an entry.""" key = hostname.lower() self._entries[key] = AddressbookEntry( hostname=key, destination=destination, source=source, ) def remove_entry(self, hostname: str) -> bool: """Remove entry. Returns True if existed.""" key = hostname.lower() if key in self._entries: del self._entries[key] return True return False def has_entry(self, hostname: str) -> bool: """Check if hostname exists in the addressbook.""" return hostname.lower() in self._entries def list_all(self) -> dict[str, AddressbookEntry]: """Return a copy of all entries.""" return dict(self._entries) @property def entry_count(self) -> int: """Return the number of entries.""" return len(self._entries)