BastionGuard/data/extension/bastionguard-tb-extension/sync-config-to-extension.py

102 lines
3 KiB
Python
Executable file

#!/usr/bin/env python3
"""
sync-config-to-extension.py
Legge ~/.config/BastionGuard/mail.json e lo scrive nello storage
dell'estensione Thunderbird (IndexedDB/storage.local simulato via
il file di storage nativo delle WebExtension).
Thunderbird salva lo storage.local delle estensioni in:
~/.thunderbird/<profilo>/storage/default/moz-extension+++<uuid>/idb/
oppure come file JSON semplice in:
~/.thunderbird/<profilo>/browser-extension-data/<extension-id>/storage.js
Questo script scrive nel formato JSON semplice che Thunderbird
legge all'avvio.
"""
import json
import os
import sys
import glob
EXTENSION_ID = "bastionguard-mail@codelinsoft.it"
MAIL_CONFIG_PATH = os.path.expanduser("~/.config/BastionGuard/mail.json")
TB_DIR = os.path.expanduser("~/.thunderbird")
def find_tb_profile():
profiles_ini = os.path.join(TB_DIR, "profiles.ini")
if not os.path.exists(profiles_ini):
raise FileNotFoundError(f"profiles.ini non trovato: {profiles_ini}")
with open(profiles_ini) as f:
content = f.read()
# Trova il profilo Default
lines = content.splitlines()
in_install = False
default_rel = None
for line in lines:
line = line.strip()
if line.startswith("[Install"):
in_install = True
elif line.startswith("["):
in_install = False
elif in_install and line.startswith("Default="):
default_rel = line.split("=", 1)[1]
break
if not default_rel:
# Fallback: primo Path= trovato
for line in lines:
line = line.strip()
if line.startswith("Path="):
default_rel = line.split("=", 1)[1]
break
if not default_rel:
raise ValueError("Nessun profilo trovato in profiles.ini")
if os.path.isabs(default_rel):
return default_rel
return os.path.join(TB_DIR, default_rel)
def sync_config(profile_path):
# Leggi mail.json
if not os.path.exists(MAIL_CONFIG_PATH):
print(f"[sync] mail.json non trovato: {MAIL_CONFIG_PATH}")
return False
with open(MAIL_CONFIG_PATH) as f:
mail_config = json.load(f)
# Percorso storage estensione Thunderbird
storage_dir = os.path.join(profile_path, "browser-extension-data", EXTENSION_ID)
os.makedirs(storage_dir, exist_ok=True)
storage_file = os.path.join(storage_dir, "storage.js")
# Formato: {"mailConfig": <contenuto mail.json>}
storage_data = {"mailConfig": mail_config}
with open(storage_file, "w") as f:
json.dump(storage_data, f, indent=2)
os.chmod(storage_file, 0o600)
print(f"[sync] ✔ Configurazione sincronizzata in: {storage_file}")
print(f"[sync] Profili: {len(mail_config.get('profiles', []))}")
print(f"[sync] inject_signature: {mail_config.get('inject_signature', True)}")
return True
if __name__ == "__main__":
try:
profile = find_tb_profile()
print(f"[sync] Profilo Thunderbird: {profile}")
sync_config(profile)
except Exception as e:
print(f"[sync] ERRORE: {e}", file=sys.stderr)
sys.exit(1)