re2o-mail-server/main.py

44 lines
1.1 KiB
Python
Executable file

#!/usr/bin/env python3
from configparser import ConfigParser
import socket
from re2oapi import Re2oAPIClient
from jinja2 import Environment, FileSystemLoader
import requests
import base64
import json
import os.path
config = ConfigParser()
config.read('config.ini')
api_hostname = config.get('Re2o', 'hostname')
api_password = config.get('Re2o', 'password')
api_username = config.get('Re2o', 'username')
api_client = Re2oAPIClient(api_hostname, api_username, api_password)
client_hostname = socket.gethostname().split('.', 1)[0]
all_users = api_client.list("mail/alias")
# Création de l'environnement Jinja
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('templates/list')
aliases_rendered = template.render(data=all_users)
fichier = open('generated/aliases','w')
if os.path.isfile('aliases_local'): # if a local aliases file exist, add it's content at the beginning
local = open('aliases_local','r')
for line in local.readlines():
fichier.write(line)
local.close()
fichier.write(aliases_rendered)
fichier.close()
print('done')