#!/usr/bin/env python3
# -*- mode: python -*-

from email import message_from_string
from importlib import import_module
from syslog import syslog, LOG_WARNING
from traceback import format_exc
import sys

modules = [
    ('aur'),
    ('hackage'),
    ('pypi'),
    ('zovem'),
    ('overquote')
]
## A bit tricky way to get reformat functions and only them
## from modules names list
## In future, this module list will be configurable.

methods = map(lambda m:
		vars(import_module("mailsanity.{0}".format(m)))['reformat'],
              modules)
stdout = open('/dev/stdout', mode = 'w', encoding = 'utf-8')
stdin = open('/dev/stdin', mode = 'r', encoding = 'utf-8')
message = message_from_string(stdin.read())

for method in methods:
    try:
        method(message)
    except Exception as e:
        syslog(LOG_WARNING, format_exc(e))

stdout.write(message.as_string())
