gpg-lacre/lacre.py

74 lines
2.3 KiB
Python
Raw Permalink Normal View History

2010-01-20 21:53:25 +01:00
#!/usr/bin/python
#
# lacre
#
# This file is part of the lacre source code.
#
# lacre is free software: you can redistribute it and/or modify
2022-06-30 22:55:04 +02:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# lacre source code is distributed in the hope that it will be useful,
2022-06-30 22:55:04 +02:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
2022-06-30 22:55:04 +02:00
# You should have received a copy of the GNU General Public License
# along with lacre source code. If not, see <http://www.gnu.org/licenses/>.
#
2010-01-20 21:53:25 +01:00
import email
from email.policy import SMTPUTF8
2010-01-20 21:53:25 +01:00
import sys
import logging
import lacre
import lacre.config as conf
2024-01-05 22:21:20 +01:00
from lacre.stats import time_logger
conf.load_config()
lacre.init_logging(conf.get_item('logging', 'config'))
# This has to be executed *after* logging initialisation.
2022-10-22 10:13:54 +02:00
import lacre.core as core
LOG = logging.getLogger('lacre.py')
2024-01-05 22:21:20 +01:00
def main():
with time_logger('Message delivery', LOG):
2024-01-08 22:14:56 +01:00
missing_params = conf.validate_config()
2024-01-05 22:21:20 +01:00
config_file = conf.config_source()
2024-01-05 22:21:20 +01:00
if missing_params:
LOG.error(f"Aborting delivery! Following mandatory config parameters are missing in {config_file!r}: {missing_params}")
sys.exit(lacre.EX_CONFIG)
2024-01-05 22:21:20 +01:00
delivered = False
try:
# Read e-mail from stdin, parse it
raw = sys.stdin.read()
raw_message = email.message_from_string(raw, policy=SMTPUTF8)
from_addr = raw_message['From']
# Read recipients from the command-line
to_addrs = sys.argv[1:]
2024-01-05 22:21:20 +01:00
# Let's start
core.deliver_message(raw_message, from_addr, to_addrs)
delivered = True
except:
LOG.exception('Could not handle message')
2024-01-05 22:21:20 +01:00
if not delivered:
# It seems we weren't able to deliver the message. In case it was
# some silly message-encoding issue that shouldn't bounce the
# message, we just try recoding the message body and delivering it.
try:
core.failover_delivery(raw_message, to_addrs, from_addr)
except:
LOG.exception('Failover delivery failed too')
2024-01-05 22:21:20 +01:00
if __name__ == '__main__':
main()