2010-01-20 21:53:25 +01:00
|
|
|
#!/usr/bin/python
|
2013-09-28 04:21:55 +02:00
|
|
|
#
|
2024-01-06 14:34:54 +01:00
|
|
|
# lacre
|
2013-09-28 04:21:55 +02:00
|
|
|
#
|
2024-01-06 14:34:54 +01:00
|
|
|
# This file is part of the lacre source code.
|
2013-09-28 04:21:55 +02:00
|
|
|
#
|
2024-01-06 14:34:54 +01:00
|
|
|
# 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.
|
2013-09-28 04:21:55 +02:00
|
|
|
#
|
2024-01-06 14:34:54 +01:00
|
|
|
# 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.
|
2013-09-28 04:21:55 +02:00
|
|
|
#
|
2022-06-30 22:55:04 +02:00
|
|
|
# You should have received a copy of the GNU General Public License
|
2024-01-06 14:34:54 +01:00
|
|
|
# along with lacre source code. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-28 04:21:55 +02:00
|
|
|
#
|
|
|
|
|
2010-01-20 21:53:25 +01:00
|
|
|
import email
|
2023-03-07 20:39:32 +01:00
|
|
|
from email.policy import SMTPUTF8
|
2010-01-20 21:53:25 +01:00
|
|
|
import sys
|
2022-02-25 23:38:08 +01:00
|
|
|
import logging
|
2022-06-11 21:05:27 +02:00
|
|
|
|
2022-02-25 23:38:08 +01:00
|
|
|
import lacre
|
|
|
|
import lacre.config as conf
|
2024-01-05 22:21:20 +01:00
|
|
|
from lacre.stats import time_logger
|
2022-06-02 23:41:14 +02:00
|
|
|
|
2022-02-25 23:38:08 +01:00
|
|
|
conf.load_config()
|
2022-03-26 09:52:56 +01:00
|
|
|
lacre.init_logging(conf.get_item('logging', 'config'))
|
|
|
|
|
2022-06-22 20:30:53 +02:00
|
|
|
# This has to be executed *after* logging initialisation.
|
2022-10-22 10:13:54 +02:00
|
|
|
import lacre.core as core
|
2024-07-12 10:40:38 +02:00
|
|
|
from lacre.lazymessage import LazyMessage
|
2022-06-22 20:30:53 +02:00
|
|
|
|
2024-01-06 14:34:54 +01:00
|
|
|
LOG = logging.getLogger('lacre.py')
|
2022-02-25 23:38:08 +01:00
|
|
|
|
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()
|
2023-11-17 22:51:09 +01:00
|
|
|
|
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)
|
2022-05-31 22:09:10 +02:00
|
|
|
|
2024-01-05 22:21:20 +01:00
|
|
|
delivered = False
|
2024-07-12 10:40:38 +02:00
|
|
|
raw_message = None
|
|
|
|
|
|
|
|
# Read recipients from the command-line
|
|
|
|
to_addrs = sys.argv[1:]
|
|
|
|
|
|
|
|
# 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']
|
2023-04-08 19:14:55 +02:00
|
|
|
|
2024-07-12 10:40:38 +02:00
|
|
|
lmessage = LazyMessage(to_addrs, lambda: raw_message)
|
|
|
|
|
|
|
|
try:
|
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')
|
2023-04-08 19:14:55 +02:00
|
|
|
|
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:
|
2024-07-12 10:40:38 +02:00
|
|
|
from_addr = raw_message['From']
|
2024-01-05 22:21:20 +01:00
|
|
|
core.failover_delivery(raw_message, to_addrs, from_addr)
|
|
|
|
except:
|
|
|
|
LOG.exception('Failover delivery failed too')
|
2023-04-08 20:47:18 +02:00
|
|
|
|
2024-01-05 22:21:20 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|