Created
January 6, 2021 18:51
-
-
Save trickeydan/074b333aaa7e8564e760f34315851318 to your computer and use it in GitHub Desktop.
Dovecot Wildcard Email Sorter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /home/dgt/venv/bin/python3 | |
""" | |
Dovecot Wildcard Email Sorter | |
Used to sort inbound email to a wildcard address into category subfolders. | |
Used by me on Mythic Beasts hosted webmail. | |
Copyright 2021 Dan Trickey | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
from pathlib import Path | |
from re import compile | |
from typing import Dict, List, Set | |
from jinja2 import Template | |
from pydantic import BaseModel | |
from ruamel.yaml import YAML | |
# Settings | |
HOMEDIR = Path("/home/dgt") | |
MAILCONF = HOMEDIR / "mail.yml" | |
PREFIX = "trickey" | |
MAILDIR = HOMEDIR / "popboxes/dan%trickey.uk/Maildir" | |
SUBSCRIPTIONS = MAILDIR / "subscriptions" | |
FORWARD_TEMPLATE_PATH = HOMEDIR / "forward-template.j2" | |
SUB_TEMPLATE_PATH = HOMEDIR / "subscriptions.j2" | |
# Static | |
FORWARD_RE = compile(f"^\.forward-{PREFIX}-([a-z0-9]+)$") | |
MAILBOX_RE = compile(f"^\.INBOX.([A-Za-z0-9]+)$") | |
yaml = YAML(typ='safe') | |
with FORWARD_TEMPLATE_PATH.open("r") as fh: | |
FORWARD_TEMPLATE = Template(fh.read()) | |
with SUB_TEMPLATE_PATH.open("r") as fh: | |
SUB_TEMPLATE = Template(fh.read()) | |
class MailConfig(BaseModel): | |
folders: Dict[str, List[str]] | |
@classmethod | |
def load(cls) -> 'MailConfig': | |
with MAILCONF.open("r") as fh: | |
mc = cls(**yaml.load(fh)) | |
mc.verify_config() | |
return mc | |
@property | |
def all_addresses(self) -> List[str]: | |
return [i for s in self.folders.values() for i in s] | |
def verify_config(self) -> None: | |
all_addresses_set = set(self.all_addresses) | |
# Check for duplicate addresses | |
if len(self.all_addresses) != len(all_addresses_set): | |
raise ValueError("Duplicate addresses in config.") | |
# Check for default in use. We cannot use this. | |
if "default" in all_addresses_set: | |
raise ValueError("default is a reserved mailbox and cannot be assigned.") | |
def get_folder(self, address: str) -> str: | |
for f, v in self.folders.items(): | |
if address in v: | |
return f | |
raise IndexError("Unable to find folder for address") | |
class ForwardManager: | |
def __init__(self) -> None: | |
self._addresses: Set[str] = set() | |
glob = HOMEDIR.glob(f".forward-{PREFIX}-*") | |
for i in glob: | |
res = FORWARD_RE.match(i.stem) | |
if res: | |
address, = res.groups() | |
self._addresses.add(address) | |
else: | |
raise ValueError(f"{i.stem} does not match the right format") | |
# Check that default is there, and remove it. | |
if "default" not in self._addresses: | |
raise ValueError("No default forward rule.") | |
self._addresses.remove("default") | |
@property | |
def addresses(self) -> Set[str]: | |
return self._addresses | |
class MailboxManager: | |
def __init__(self) -> None: | |
self._mailboxes: Set[str] = set() | |
glob = MAILDIR.glob(".INBOX.*") | |
for i in glob: | |
res = MAILBOX_RE.match(i.name) | |
if res: | |
mailbox_name, = res.groups() | |
self._mailboxes.add(mailbox_name) | |
else: | |
raise ValueError(f"{i.name} does not match the right format") | |
@property | |
def mailboxes(self) -> Set[str]: | |
return self._mailboxes | |
if __name__ == "__main__": | |
mc = MailConfig.load() | |
fm = ForwardManager() | |
mb = MailboxManager() | |
missing_addresses = set(mc.all_addresses) - fm.addresses | |
spurious_addresses = fm.addresses - set(mc.all_addresses) | |
existing_addresses = set(mc.all_addresses) & fm.addresses | |
print("Finding .forward files") | |
print(f"\t{len(mc.all_addresses)} addresses configured.") | |
print(f"\t{len(existing_addresses)} configured and defined.") | |
print(f"\tNeed to add {len(missing_addresses)} address definitions") | |
if len(spurious_addresses) > 0: | |
print(f"\tSpurious addresses: {spurious_addresses}") | |
raise ValueError("Unable to handle spurious addresses.") | |
# Check that existing forward files are correct | |
print("Checking existing .forward files are correct.") | |
for i in existing_addresses: | |
path = HOMEDIR / f".forward-{PREFIX}-{i}" | |
print(f"\tChecking .forward-{PREFIX}-{i} => {mc.get_folder(i)}") | |
with path.open("r") as fh: | |
dat = fh.read() | |
render = FORWARD_TEMPLATE.render(maildir=MAILDIR, folder=mc.get_folder(i)) | |
if dat != render: | |
raise ValueError(f"{i} does not match template.") | |
missing_mailboxes = set(mc.folders.keys()) - mb.mailboxes | |
spurious_mailboxes = mb.mailboxes - set(mc.folders.keys()) | |
existing_mailboxes = set(mc.folders.keys()) & mb.mailboxes | |
print("Finding Mailboxes") | |
print(f"\t{len(mc.folders)} mailboxes configured.") | |
print(f"\t{len(existing_mailboxes)} configured and defined.") | |
print(f"\tNeed to add {len(missing_mailboxes)} mailboxes") | |
if len(spurious_mailboxes) > 0: | |
print(f"\t{len(spurious_mailboxes)} spurious mailboxes found.") | |
raise ValueError("Unable to handle spurious mailboxes.") | |
# Add missing mailboxes | |
print(f"Adding {len(missing_mailboxes)} mailboxes.") | |
for i in missing_mailboxes: | |
path = MAILDIR / f".INBOX.{i}" | |
print(f"\tAdding {path}") | |
path.mkdir() | |
# Add forward files for addresses | |
print(f"Adding {len(missing_addresses)} .forward files.") | |
for i in missing_addresses: | |
print(f"\tAdding .forward-{PREFIX}-{i} => {mc.get_folder(i)}") | |
render = FORWARD_TEMPLATE.render(maildir=MAILDIR, folder=mc.get_folder(i)) | |
path = HOMEDIR / f".forward-{PREFIX}-{i}" | |
with path.open("w") as fh: | |
fh.write(render) | |
# Update subscriptions file | |
print("Updating dovecot subscriptions file.") | |
render = SUB_TEMPLATE.render(folders=mc.folders.keys()) | |
sub_path = MAILDIR / "subscriptions" | |
with sub_path.open("w") as fh: | |
fh.write(render) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment