# HG changeset patch # User Paul Boddie # Date 1440161273 -7200 # Node ID b25b2b903d88997cf8ad92da2da2cdff8730f8de # Parent 7fe375ab86eccaae018e05260ad847a97bdbbdf7 Added support for delivery to local recipients via SMTP (at least for Postfix). Made the recipients parameter type consistent for the different sendmail method invocation possibilities. diff -r 7fe375ab86ec -r b25b2b903d88 README.txt --- a/README.txt Thu Aug 20 23:52:18 2015 +0200 +++ b/README.txt Fri Aug 21 14:47:53 2015 +0200 @@ -129,7 +129,10 @@ In this simpler environment, recipient details must be manually edited in the virtual alias map files, but this permits a very transparent way of -administering the system. +administering the system. To add support for delivery to local mailboxes, the +following alternative to virtual_alias_maps is provided as an example: + + virtual_alias_maps_local Defines recipients and local users Naturally, the above recipient identification configuration examples can be disregarded in favour of other ways of defining mail recipients, subject to diff -r 7fe375ab86ec -r b25b2b903d88 conf/postfix/master.cf.items --- a/conf/postfix/master.cf.items Thu Aug 20 23:52:18 2015 +0200 +++ b/conf/postfix/master.cf.items Fri Aug 21 14:47:53 2015 +0200 @@ -1,6 +1,7 @@ imip_resources unix - n n - - pipe flags=FR user=imip-agent:lmtp argv=/var/lib/imip-agent/imip_resource.py -o ${original_recipient} +# Replace "-l $lmtp_socket" with "-L" for local SMTP delivery. imip_people unix - n n - - pipe flags=FR user=imip-agent:lmtp argv=/var/lib/imip-agent/imip_person.py -o ${original_recipient} -l $lmtp_socket diff -r 7fe375ab86ec -r b25b2b903d88 conf/postfix/simple/virtual_alias_maps_local --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/conf/postfix/simple/virtual_alias_maps_local Fri Aug 21 14:47:53 2015 +0200 @@ -0,0 +1,7 @@ +paul.boddie@example.com people+paul.boddie@example.com +vincent.vole@example.com people+vincent.vole@example.com +resource-car-porsche911@example.com resources+resource-car-porsche911@example.com +resource-room-confroom@example.com resources+resource-room-confroom@example.com +resource-room-sauna@example.com resources+resource-room-sauna@example.com +local+paul.boddie@example.com paulb +local+vincent.vole@example.com vole diff -r 7fe375ab86ec -r b25b2b903d88 imiptools/__init__.py --- a/imiptools/__init__.py Thu Aug 20 23:52:18 2015 +0200 +++ b/imiptools/__init__.py Fri Aug 21 14:47:53 2015 +0200 @@ -111,6 +111,7 @@ store_dir = [] publishing_dir = [] preferences_dir = [] + local_smtp = False outgoing_only = False l = [] @@ -124,7 +125,7 @@ # Switch to collecting recipients. - if arg == "-o": + elif arg == "-o": l = original_recipients # Switch to collecting senders. @@ -137,6 +138,11 @@ elif arg == "-l": l = lmtp + # Detect sending to local users via SMTP. + + elif arg == "-L": + local_smtp = True + # Switch to getting the store directory. elif arg == "-S": @@ -159,7 +165,7 @@ else: l.append(arg) - self.messenger = Messenger(lmtp_socket=lmtp and lmtp[0] or None, sender=senders and senders[0] or None) + self.messenger = Messenger(lmtp_socket=lmtp and lmtp[0] or None, local_smtp=local_smtp, sender=senders and senders[0] or None) self.store_dir = store_dir and store_dir[0] or None self.publishing_dir = publishing_dir and publishing_dir[0] or None self.preferences_dir = preferences_dir and preferences_dir[0] or None @@ -317,7 +323,7 @@ print >>sys.stderr, "Forwarded parts..." print message elif self.messenger.local_delivery(): - self.messenger.sendmail(get_address(self.user), message.as_string()) + self.messenger.sendmail([get_address(self.user)], message.as_string()) # Unhandled messages are delivered as they are. @@ -326,7 +332,7 @@ print >>sys.stderr, "Unhandled parts..." print msg elif self.messenger.local_delivery(): - self.messenger.sendmail(get_address(self.user), msg.as_string()) + self.messenger.sendmail([get_address(self.user)], msg.as_string()) def can_provide_freebusy(self, handlers): diff -r 7fe375ab86ec -r b25b2b903d88 imiptools/mail.py --- a/imiptools/mail.py Thu Aug 20 23:52:18 2015 +0200 +++ b/imiptools/mail.py Fri Aug 21 14:47:53 2015 +0200 @@ -40,13 +40,15 @@ "Sending of outgoing messages." - def __init__(self, lmtp_socket=None, sender=None, subject=None, body_text=None, preamble_text=None): + def __init__(self, lmtp_socket=None, local_smtp=False, sender=None, subject=None, body_text=None, preamble_text=None): """ - Deliver to a local mail system using LMTP if 'lmtp_socket' is provided. + Deliver to a local mail system using LMTP if 'lmtp_socket' is provided + or if 'local_smtp' is given as a true value. """ self.lmtp_socket = lmtp_socket + self.local_smtp = local_smtp self.sender = sender or MESSAGE_SENDER self.subject = subject or MESSAGE_SUBJECT self.body_text = body_text or MESSAGE_TEXT @@ -56,9 +58,9 @@ "Return whether local delivery is performed using this messenger." - return self.lmtp_socket is not None + return self.lmtp_socket is not None or self.local_smtp - def sendmail(self, recipients, data, sender=None, outgoing_bcc=None, lmtp_socket=None): + def sendmail(self, recipients, data, sender=None, outgoing_bcc=None): """ Send a mail to the given 'recipients' consisting of the given 'data', @@ -77,10 +79,27 @@ if outgoing_bcc: recipients = list(recipients) + ["%s+%s" % (OUTGOING_PREFIX, outgoing_bcc)] + elif self.local_smtp: + recipients = [self.make_local(recipient) for recipient in recipients] smtp.sendmail(sender or self.sender, recipients, data) smtp.quit() + def make_local(self, recipient): + + """ + Make the 'recipient' an address for local delivery. For this to function + correctly, a virtual alias or equivalent must be defined for addresses + of the following form: + + local+NAME@DOMAIN + + Such aliases should direct delivery to the local recipient. + """ + + parts = recipient.split("+", 1) + return "%s+%s" % ("local", parts[-1]) + def make_outgoing_message(self, parts, recipients, sender=None, outgoing_bcc=None): """