paul@83 | 1 | #!/usr/bin/env python |
paul@83 | 2 | |
paul@146 | 3 | """ |
paul@146 | 4 | Mail preparation support. |
paul@146 | 5 | |
paul@146 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 7 | |
paul@146 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 11 | version. |
paul@146 | 12 | |
paul@146 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 16 | details. |
paul@146 | 17 | |
paul@146 | 18 | You should have received a copy of the GNU General Public License along with |
paul@146 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@146 | 20 | """ |
paul@146 | 21 | |
paul@894 | 22 | from imiptools.config import LOCAL_PREFIX, MESSAGE_SENDER, OUTGOING_PREFIX |
paul@83 | 23 | from email.mime.message import MIMEMessage |
paul@83 | 24 | from email.mime.multipart import MIMEMultipart |
paul@83 | 25 | from email.mime.text import MIMEText |
paul@83 | 26 | from smtplib import LMTP, SMTP |
paul@83 | 27 | |
paul@1005 | 28 | # Fake gettext function for strings to be translated later. |
paul@1005 | 29 | |
paul@1005 | 30 | _ = lambda s: s |
paul@83 | 31 | |
paul@1005 | 32 | MESSAGE_SUBJECT = _("Calendar system message") |
paul@83 | 33 | |
paul@1005 | 34 | PREAMBLE_TEXT = _("""\ |
paul@87 | 35 | This message contains several different parts, one of which will contain |
paul@87 | 36 | calendar information that will only be understood by a suitable program. |
paul@1005 | 37 | """) |
paul@87 | 38 | |
paul@83 | 39 | class Messenger: |
paul@83 | 40 | |
paul@83 | 41 | "Sending of outgoing messages." |
paul@83 | 42 | |
paul@1005 | 43 | def __init__(self, lmtp_socket=None, local_smtp=False, sender=None, subject=None, preamble_text=None): |
paul@607 | 44 | |
paul@607 | 45 | """ |
paul@666 | 46 | Deliver to a local mail system using LMTP if 'lmtp_socket' is provided |
paul@666 | 47 | or if 'local_smtp' is given as a true value. |
paul@607 | 48 | """ |
paul@607 | 49 | |
paul@607 | 50 | self.lmtp_socket = lmtp_socket |
paul@666 | 51 | self.local_smtp = local_smtp |
paul@83 | 52 | self.sender = sender or MESSAGE_SENDER |
paul@1005 | 53 | self.subject = subject |
paul@1005 | 54 | self.preamble_text = preamble_text |
paul@1005 | 55 | |
paul@1005 | 56 | # The translation method is set by the client once locale information is |
paul@1005 | 57 | # known. |
paul@1005 | 58 | |
paul@1005 | 59 | self.gettext = None |
paul@83 | 60 | |
paul@607 | 61 | def local_delivery(self): |
paul@607 | 62 | |
paul@607 | 63 | "Return whether local delivery is performed using this messenger." |
paul@607 | 64 | |
paul@666 | 65 | return self.lmtp_socket is not None or self.local_smtp |
paul@607 | 66 | |
paul@666 | 67 | def sendmail(self, recipients, data, sender=None, outgoing_bcc=None): |
paul@83 | 68 | |
paul@83 | 69 | """ |
paul@83 | 70 | Send a mail to the given 'recipients' consisting of the given 'data', |
paul@128 | 71 | using the given 'sender' identity if indicated, indicating an |
paul@607 | 72 | 'outgoing_bcc' identity if indicated. |
paul@445 | 73 | |
paul@445 | 74 | The 'outgoing_bcc' argument is required when sending on behalf of a user |
paul@445 | 75 | from the calendar@domain address, since this will not be detected as a |
paul@445 | 76 | valid participant and handled using the outgoing transport. |
paul@83 | 77 | """ |
paul@83 | 78 | |
paul@607 | 79 | if self.lmtp_socket: |
paul@607 | 80 | smtp = LMTP(self.lmtp_socket) |
paul@83 | 81 | else: |
paul@83 | 82 | smtp = SMTP("localhost") |
paul@83 | 83 | |
paul@128 | 84 | if outgoing_bcc: |
paul@128 | 85 | recipients = list(recipients) + ["%s+%s" % (OUTGOING_PREFIX, outgoing_bcc)] |
paul@666 | 86 | elif self.local_smtp: |
paul@666 | 87 | recipients = [self.make_local(recipient) for recipient in recipients] |
paul@128 | 88 | |
paul@86 | 89 | smtp.sendmail(sender or self.sender, recipients, data) |
paul@83 | 90 | smtp.quit() |
paul@83 | 91 | |
paul@666 | 92 | def make_local(self, recipient): |
paul@666 | 93 | |
paul@666 | 94 | """ |
paul@666 | 95 | Make the 'recipient' an address for local delivery. For this to function |
paul@666 | 96 | correctly, a virtual alias or equivalent must be defined for addresses |
paul@666 | 97 | of the following form: |
paul@666 | 98 | |
paul@666 | 99 | local+NAME@DOMAIN |
paul@666 | 100 | |
paul@666 | 101 | Such aliases should direct delivery to the local recipient. |
paul@666 | 102 | """ |
paul@666 | 103 | |
paul@666 | 104 | parts = recipient.split("+", 1) |
paul@894 | 105 | return "%s+%s" % (LOCAL_PREFIX, parts[-1]) |
paul@666 | 106 | |
paul@178 | 107 | def make_outgoing_message(self, parts, recipients, sender=None, outgoing_bcc=None): |
paul@83 | 108 | |
paul@86 | 109 | """ |
paul@86 | 110 | Make a message from the given 'parts' for the given 'recipients', using |
paul@128 | 111 | the given 'sender' identity if indicated, indicating an 'outgoing_bcc' |
paul@128 | 112 | identity if indicated. |
paul@86 | 113 | """ |
paul@83 | 114 | |
paul@178 | 115 | message = self._make_summary_for_parts(parts) |
paul@83 | 116 | |
paul@86 | 117 | message["From"] = sender or self.sender |
paul@83 | 118 | for recipient in recipients: |
paul@83 | 119 | message["To"] = recipient |
paul@128 | 120 | if outgoing_bcc: |
paul@128 | 121 | message["Bcc"] = "%s+%s" % (OUTGOING_PREFIX, outgoing_bcc) |
paul@1005 | 122 | message["Subject"] = self.subject or \ |
paul@1005 | 123 | self.gettext and self.gettext(MESSAGE_SUBJECT) or MESSAGE_SUBJECT |
paul@83 | 124 | |
paul@83 | 125 | return message |
paul@83 | 126 | |
paul@178 | 127 | def make_summary_message(self, msg, parts): |
paul@178 | 128 | |
paul@178 | 129 | """ |
paul@178 | 130 | Return a simple summary using details from 'msg' and the given 'parts'. |
paul@178 | 131 | """ |
paul@178 | 132 | |
paul@178 | 133 | message = self._make_summary_for_parts(parts) |
paul@178 | 134 | self._copy_headers(message, msg) |
paul@178 | 135 | return message |
paul@178 | 136 | |
paul@83 | 137 | def wrap_message(self, msg, parts): |
paul@83 | 138 | |
paul@83 | 139 | "Wrap 'msg' and provide the given 'parts' as the primary content." |
paul@83 | 140 | |
paul@178 | 141 | message = self._make_container_for_parts(parts) |
paul@178 | 142 | payload = message.get_payload() |
paul@178 | 143 | payload.append(MIMEMessage(msg)) |
paul@178 | 144 | self._copy_headers(message, msg) |
paul@178 | 145 | return message |
paul@178 | 146 | |
paul@178 | 147 | def _make_summary_for_parts(self, parts): |
paul@178 | 148 | |
paul@178 | 149 | "Return a simple summary for the given 'parts'." |
paul@178 | 150 | |
paul@178 | 151 | if len(parts) == 1: |
paul@178 | 152 | return parts[0] |
paul@178 | 153 | else: |
paul@178 | 154 | return self._make_container_for_parts(parts) |
paul@178 | 155 | |
paul@178 | 156 | def _make_container_for_parts(self, parts): |
paul@178 | 157 | |
paul@178 | 158 | "Return a container for the given 'parts'." |
paul@178 | 159 | |
paul@83 | 160 | message = MIMEMultipart("mixed", _subparts=parts) |
paul@1005 | 161 | message.preamble = self.preamble_text or \ |
paul@1005 | 162 | self.gettext and self.gettext(PREAMBLE_TEXT) or PREAMBLE_TEXT |
paul@178 | 163 | return message |
paul@83 | 164 | |
paul@178 | 165 | def _copy_headers(self, message, msg): |
paul@83 | 166 | message["From"] = msg["From"] |
paul@83 | 167 | message["To"] = msg["To"] |
paul@83 | 168 | message["Subject"] = msg["Subject"] |
paul@83 | 169 | |
paul@83 | 170 | # vim: tabstop=4 expandtab shiftwidth=4 |