"""SubscriptionManager — manages hostname subscription lists from trusted sources.""" from __future__ import annotations from i2p_apps.addressbook.book import Addressbook class SubscriptionManager: """Manages hostname subscription lists from trusted sources.""" def __init__(self) -> None: self._urls: list[str] = [] def add_subscription(self, url: str) -> None: """Add a subscription URL.""" if url not in self._urls: self._urls.append(url) def remove_subscription(self, url: str) -> bool: """Remove a subscription URL. Returns True if existed.""" if url in self._urls: self._urls.remove(url) return True return False def get_subscriptions(self) -> list[str]: """Return list of subscription URLs.""" return list(self._urls) def parse_subscription_response(self, text: str) -> dict[str, str]: """Parse subscription response. Format: one entry per line, hostname=base64destination Lines starting with # are comments. Empty lines skipped. """ result: dict[str, str] = {} for line in text.splitlines(): line = line.strip() if not line or line.startswith("#"): continue if "=" not in line: continue hostname, destination = line.split("=", 1) hostname = hostname.strip().lower() destination = destination.strip() result[hostname] = destination return result def merge_into_addressbook( self, book: Addressbook, new_entries: dict[str, str], overwrite: bool = False, ) -> int: """Merge subscription entries into addressbook. Returns count of entries added or updated. If overwrite=False, existing entries are not updated. """ count = 0 for hostname, destination in new_entries.items(): if not overwrite and book.has_entry(hostname): continue book.add_entry(hostname, destination, source="subscription") count += 1 return count