87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
sync_dynamodb_to_sieve.py - Sync DynamoDB rules to Dovecot Sieve
|
|
"""
|
|
import boto3
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Config
|
|
REGION = 'us-east-2'
|
|
TABLE = 'email-rules'
|
|
VMAIL_BASE = '/var/mail'
|
|
|
|
dynamodb = boto3.resource('dynamodb', region_name=REGION)
|
|
table = dynamodb.Table(TABLE)
|
|
|
|
def generate_sieve(email, rules):
|
|
"""Generate Sieve script from DynamoDB rules"""
|
|
script = [
|
|
'require ["copy","vacation","variables"];',
|
|
'',
|
|
'# Skip if already processed by worker',
|
|
'if header :contains "X-SES-Worker-Processed" "" {',
|
|
' keep;',
|
|
' stop;',
|
|
'}',
|
|
''
|
|
]
|
|
|
|
# Forwards
|
|
forwards = rules.get('forwards', [])
|
|
if forwards:
|
|
script.append('# rule:[forward]')
|
|
for fwd in forwards:
|
|
script.append(f'redirect :copy "{fwd}";')
|
|
|
|
# OOO
|
|
if rules.get('ooo_active'):
|
|
msg = rules.get('ooo_message', 'I am away')
|
|
script.append('# rule:[reply]')
|
|
script.append(f'vacation :days 1 :from "{email}" "{msg}";')
|
|
|
|
return '\n'.join(script)
|
|
|
|
def sync():
|
|
"""Sync all rules from DynamoDB to Sieve"""
|
|
response = table.scan()
|
|
|
|
for item in response.get('Items', []):
|
|
email = item['email_address']
|
|
domain = email.split('@')[1]
|
|
user = email.split('@')[0]
|
|
|
|
# Path: /var/mail/domain.de/user/.dovecot.sieve
|
|
mailbox_dir = Path(VMAIL_BASE) / domain / user
|
|
|
|
# Skip if mailbox doesn't exist
|
|
if not mailbox_dir.exists():
|
|
print(f'⚠ Skipped {email} (mailbox not found)')
|
|
continue
|
|
|
|
sieve_path = mailbox_dir / '.dovecot.sieve'
|
|
|
|
# Generate & write (wie bisher)
|
|
script = generate_sieve(email, item)
|
|
sieve_path.write_text(script)
|
|
|
|
# Compile
|
|
os.system(f'sievec {sieve_path}')
|
|
|
|
# ZUSÄTZLICH: In ~/sieve/ kopieren für doveadm
|
|
sieve_dir = mailbox_dir / 'sieve'
|
|
sieve_dir.mkdir(exist_ok=True)
|
|
managed_script = sieve_dir / 'default.sieve'
|
|
managed_script.write_text(script)
|
|
os.system(f'sievec {managed_script}')
|
|
|
|
# Aktivieren
|
|
os.system(f'doveadm sieve activate -u {email} default')
|
|
|
|
# Ownership
|
|
os.system(f'chown -R docker:docker {sieve_dir}')
|
|
|
|
print(f'✓ {email}')
|
|
|
|
if __name__ == '__main__':
|
|
sync() |