scripts-misc/mailcow/update-adminaliases.py
2024-05-23 10:01:02 +02:00

66 lines
1.8 KiB
Python
Executable file

#!/usr/bin/env python3
import time
import argparse
import logging
import requests
import json
from config import config
start_time = time.time()
headers = {
'X-API-Key': config['apikey'],
'Content-Type': 'application/json'
}
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--skip-git', action='store_true',
help="Disable comitting to git")
parser.add_argument('-q', '--quiet', action='store_true',
help="Run without logging/output. (useful for cron, etc...)")
args = parser.parse_args()
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s')
log = logging.getLogger("update-adminaliasses")
log.setLevel(logging.INFO)
if args.quiet:
log.setLevel(logging.CRITICAL)
# ApiKeyAuth (apiKey)
# Name: X-API-Key
#
# In: header
#
# Value:
all_domains = requests.request("GET", f"https://{config['server']}/api/v1/get/domain/all", headers=headers).json()
needed_aliasses = []
for domain in all_domains:
for name in config['names']:
needed_aliasses.append(f'{name}@{domain["domain_name"]}')
existing_aliasses = requests.request("GET", f"https://{config['server']}/api/v1/get/alias/all", headers=headers).json()
for alias in existing_aliasses:
if alias['address'] in needed_aliasses:
if alias['goto'] == config['admin-mail']:
needed_aliasses.remove(alias['address'])
log.info(f"Skipped {alias['address']}")
if needed_aliasses:
for alias in needed_aliasses:
if alias != config['admin-mail']:
payload = json.dumps({ "active": "1", "address": alias, "goto": config['admin-mail'] })
response = requests.request("POST", f"https://{config['server']}/api/v1/add/alias", headers=headers, data=payload)
log.info(f"Added {alias}")