51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
from base64 import b64encode
|
|
from time import sleep
|
|
|
|
import requests
|
|
from aiosmtpd.controller import Controller
|
|
from configargparse import ArgParser
|
|
from xdg import xdg_config_home
|
|
|
|
|
|
def parse_args():
|
|
parser = ArgParser(default_config_files=['/etc/libertyforward/*.conf',
|
|
str(xdg_config_home() / 'LibertyTechForce' / 'libertyforward.conf')])
|
|
parser.add('-c', '--config', is_config_file=True, help='config file path', env_var='LF_CONFIG_FILE')
|
|
parser.add('--mailer', help='name of the mailer backend to use', env_var='LF_MAILER')
|
|
parser.add('--api-token', help='token to use to authenticate against API', env_var='LF_API_TOKEN')
|
|
return parser.parse_args()
|
|
|
|
|
|
class KingMailerHandler(object):
|
|
|
|
async def handle_DATA(self, server, session, envelope):
|
|
payload = {
|
|
'mail_from': envelope.mail_from,
|
|
'rcpt_to': envelope.rcpt_tos,
|
|
'data': b64encode(envelope.original_content).decode()
|
|
}
|
|
headers = {'X-Server-API-Key': self.api_token}
|
|
response = requests.post('https://kingmailer.org/api/v1/send/raw', json=payload, headers=headers)
|
|
resp_dict = response.json()
|
|
if resp_dict['status'] == 'success':
|
|
print('Message accepted', flush=True)
|
|
return '250 Message accepted for delivery'
|
|
else:
|
|
print('{code}: {message}'.format(**resp_dict['data']), flush=True)
|
|
return '500 Could not process your message'
|
|
|
|
|
|
if __name__ == '__main__':
|
|
print('Starting LibertyForward', flush=True)
|
|
args = parse_args()
|
|
handler = KingMailerHandler()
|
|
handler.api_token = args.api_token
|
|
controller = Controller(handler, hostname='0.0.0.0', port=25)
|
|
print('Starting controller thread', flush=True)
|
|
controller.start()
|
|
while True:
|
|
try:
|
|
sleep(1)
|
|
except Exception:
|
|
break
|