163 lines
5.4 KiB
Python
163 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Centrale configuratie voor alle mailcat-scripts.
|
|
|
|
Setup: python3 config.py --setup
|
|
Gebruik in andere scripts:
|
|
import config
|
|
cfg = config.load() # leest --account uit sys.argv, anders actief account
|
|
mail.login(cfg.username, cfg.password)
|
|
|
|
Wisselen van account zonder config te wijzigen:
|
|
python3 maak_mappen.py --account backup
|
|
python3 verplaats_bestaand.py --account backup --uitvoeren
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
import getpass
|
|
from pathlib import Path
|
|
from dataclasses import dataclass
|
|
|
|
CONFIG_FILE = Path(__file__).parent / "config.json"
|
|
|
|
|
|
@dataclass
|
|
class Account:
|
|
name: str
|
|
username: str
|
|
password: str
|
|
imap_host: str
|
|
imap_port: int
|
|
smtp_host: str
|
|
smtp_port: int
|
|
sieve_host: str
|
|
sieve_port: int
|
|
|
|
|
|
def load(account_name: str | None = None) -> Account:
|
|
"""
|
|
Laad het opgegeven account (of het actieve account uit config.json).
|
|
account_name kan ook via --account <naam> op de commandoregel worden opgegeven.
|
|
"""
|
|
# --account vlag heeft prioriteit boven argument
|
|
args = sys.argv[1:]
|
|
if "--account" in args:
|
|
idx = args.index("--account")
|
|
if idx + 1 < len(args):
|
|
account_name = args[idx + 1]
|
|
|
|
if not CONFIG_FILE.exists():
|
|
print("Geen config gevonden. Draai eerst: python3 config.py --setup")
|
|
sys.exit(1)
|
|
|
|
raw = json.loads(CONFIG_FILE.read_text(encoding="utf-8"))
|
|
|
|
name = account_name or raw.get("active", "")
|
|
if not name:
|
|
print("Geen actief account ingesteld. Gebruik --account <naam> of stel 'active' in config.json in.")
|
|
sys.exit(1)
|
|
|
|
accounts = raw.get("accounts", {})
|
|
if name not in accounts:
|
|
beschikbaar = ", ".join(accounts.keys())
|
|
print(f"Account '{name}' niet gevonden. Beschikbaar: {beschikbaar}")
|
|
sys.exit(1)
|
|
|
|
acc = accounts[name]
|
|
return Account(
|
|
name = name,
|
|
username = acc["username"],
|
|
password = acc["password"],
|
|
imap_host = raw.get("imap_host", "australius.nl"),
|
|
imap_port = raw.get("imap_port", 993),
|
|
smtp_host = raw.get("smtp_host", "australius.nl"),
|
|
smtp_port = raw.get("smtp_port", 587),
|
|
sieve_host = raw.get("sieve_host", "australius.nl"),
|
|
sieve_port = raw.get("sieve_port", 4190),
|
|
)
|
|
|
|
|
|
def active_account_name() -> str:
|
|
"""Geef de naam van het actief te gebruiken account (voor weergave)."""
|
|
args = sys.argv[1:]
|
|
if "--account" in args:
|
|
idx = args.index("--account")
|
|
if idx + 1 < len(args):
|
|
return args[idx + 1]
|
|
if CONFIG_FILE.exists():
|
|
raw = json.loads(CONFIG_FILE.read_text(encoding="utf-8"))
|
|
return raw.get("active", "?")
|
|
return "?"
|
|
|
|
|
|
# ── setup ─────────────────────────────────────────────────────────────────────
|
|
|
|
def setup():
|
|
"""Interactief: voeg een account toe of wijzig een bestaand account."""
|
|
raw: dict = {}
|
|
if CONFIG_FILE.exists():
|
|
raw = json.loads(CONFIG_FILE.read_text(encoding="utf-8"))
|
|
|
|
raw.setdefault("imap_host", "australius.nl")
|
|
raw.setdefault("imap_port", 993)
|
|
raw.setdefault("smtp_host", "australius.nl")
|
|
raw.setdefault("smtp_port", 587)
|
|
raw.setdefault("sieve_host", "australius.nl")
|
|
raw.setdefault("sieve_port", 4190)
|
|
raw.setdefault("accounts", {})
|
|
|
|
print("─" * 50)
|
|
print("Mailcat account setup")
|
|
print("─" * 50)
|
|
|
|
bestaande = list(raw["accounts"].keys())
|
|
if bestaande:
|
|
print(f"Bestaande accounts: {', '.join(bestaande)}")
|
|
|
|
naam = input("\nAccountnaam (bijv. hans of backup): ").strip()
|
|
if not naam:
|
|
print("Afgebroken.")
|
|
return
|
|
|
|
huidig = raw["accounts"].get(naam, {})
|
|
huidig_user = huidig.get("username", "")
|
|
|
|
username = input(f"E-mailadres [{huidig_user}]: ").strip() or huidig_user
|
|
password = getpass.getpass("Wachtwoord: ")
|
|
|
|
raw["accounts"][naam] = {"username": username, "password": password}
|
|
|
|
# Vraag of dit het actieve account moet zijn
|
|
huidig_actief = raw.get("active", "")
|
|
instellen = input(f"\nDit als actief account instellen? (huidig: {huidig_actief or 'geen'}) [J/n]: ").strip().lower()
|
|
if instellen != "n":
|
|
raw["active"] = naam
|
|
|
|
CONFIG_FILE.write_text(json.dumps(raw, indent=2, ensure_ascii=False), encoding="utf-8")
|
|
CONFIG_FILE.chmod(0o600)
|
|
print(f"\n✓ Account '{naam}' opgeslagen. Actief: {raw['active']}")
|
|
print(f" Config: {CONFIG_FILE}")
|
|
|
|
if len(raw["accounts"]) > 1:
|
|
print(f"\nWisselen: voeg --account <naam> toe aan elk script.")
|
|
print(f" Bijv: python3 maak_mappen.py --account backup")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if "--setup" in sys.argv:
|
|
setup()
|
|
else:
|
|
# Toon huidige status
|
|
if not CONFIG_FILE.exists():
|
|
print("Geen config. Draai: python3 config.py --setup")
|
|
else:
|
|
raw = json.loads(CONFIG_FILE.read_text(encoding="utf-8"))
|
|
actief = raw.get("active", "?")
|
|
accounts = list(raw.get("accounts", {}).keys())
|
|
print(f"Actief account : {actief}")
|
|
print(f"Alle accounts : {', '.join(accounts)}")
|
|
print(f"IMAP : {raw.get('imap_host')}:{raw.get('imap_port')}")
|
|
print(f"\nSetup: python3 config.py --setup")
|
|
print(f"Wisselen in script: python3 <script>.py --account <naam>")
|