52 lines
1.5 KiB
Python
Executable File
52 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def fail(error):
|
|
raise error
|
|
|
|
|
|
def quote(path):
|
|
value = str(path)
|
|
if "\n" in value or "\r" in value:
|
|
raise ValueError(f"Nom de dossier incompatible avec NeoMutt : {value!r}")
|
|
for character in ("\\", '"', "$", "`"):
|
|
value = value.replace(character, "\\" + character)
|
|
return f'"{value}"'
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) > 2:
|
|
raise ValueError("Utilisation : neomutt-mailboxes CHEMIN_DU_COMPTE")
|
|
|
|
account = sys.argv[1] if len(sys.argv) == 2 else os.environ.get("NEOMUTT_MAILDIR")
|
|
if not account:
|
|
raise ValueError("Indiquez le chemin du compte ou définissez NEOMUTT_MAILDIR")
|
|
root = Path(account).expanduser()
|
|
if not root.is_dir():
|
|
raise ValueError(f"Répertoire de courrier introuvable : {root}")
|
|
|
|
mailboxes = []
|
|
for current, directories, _ in os.walk(root, onerror=fail, followlinks=False):
|
|
names = set(directories)
|
|
if {"cur", "new", "tmp"} <= names:
|
|
mailboxes.append(Path(current))
|
|
directories[:] = [name for name in directories if name not in {"cur", "new", "tmp"}]
|
|
|
|
if not mailboxes:
|
|
raise ValueError(f"Aucun Maildir trouvé dans : {root}")
|
|
|
|
commands = ["unmailboxes *"]
|
|
commands.extend(f"mailboxes {quote(path)}" for path in sorted(mailboxes))
|
|
print("\n".join(commands))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except (OSError, ValueError) as error:
|
|
print(f"neomutt-mailboxes : {error}", file=sys.stderr)
|
|
sys.exit(1)
|