paul@83 | 1 | #!/usr/bin/env python |
paul@83 | 2 | |
paul@146 | 3 | """ |
paul@146 | 4 | Mail preparation support. |
paul@146 | 5 | |
paul@1210 | 6 | Copyright (C) 2014, 2015, 2016, 2017 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@1210 | 22 | from imiptools.config import settings |
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@1210 | 28 | LOCAL_PREFIX = settings["LOCAL_PREFIX"] |
paul@1210 | 29 | MESSAGE_SENDER = settings["MESSAGE_SENDER"] |
paul@1210 | 30 | OUTGOING_PREFIX = settings["OUTGOING_PREFIX"] |
paul@1210 | 31 | |
paul@1005 | 32 | # Fake gettext function for strings to be translated later. |
paul@1005 | 33 | |
paul@1005 | 34 | _ = lambda s: s |
paul@83 | 35 | |
paul@1005 | 36 | MESSAGE_SUBJECT = _("Calendar system message") |
paul@83 | 37 | |
paul@1005 | 38 | PREAMBLE_TEXT = _("""\ |
paul@87 | 39 | This message contains several different parts, one of which will contain |
paul@87 | 40 | calendar information that will only be understood by a suitable program. |
paul@1005 | 41 | """) |
paul@87 | 42 | |
paul@83 | 43 | class Messenger: |
paul@83 | 44 | |
paul@83 | 45 | "Sending of outgoing messages." |
paul@83 | 46 | |
paul@1309 | 47 | def __init__(self, lmtp_socket=None, local_smtp=False, sender=None, |
paul@1309 | 48 | subject=None, preamble_text=None, suppress_bcc=False): |
paul@607 | 49 | |
paul@607 | 50 | """ |
paul@666 | 51 | Deliver to a local mail system using LMTP if 'lmtp_socket' is provided |
paul@666 | 52 | or if 'local_smtp' is given as a true value. |
paul@1309 | 53 | |
paul@1309 | 54 | Use 'sender' as the sender identity (or the configured default if not |
paul@1309 | 55 | specified). Use any explicitly specified 'subject' and 'preamble_text' |
paul@1309 | 56 | in sent messages. |
paul@1309 | 57 | |
paul@1309 | 58 | Where 'suppress_bcc' is set to a true value, messages will not be sent |
paul@1309 | 59 | to an outgoing destination for the sender even if this is requested. |
paul@607 | 60 | """ |
paul@607 | 61 | |
paul@607 | 62 | self.lmtp_socket = lmtp_socket |
paul@666 | 63 | self.local_smtp = local_smtp |
paul@83 | 64 | self.sender = sender or MESSAGE_SENDER |
paul@1005 | 65 | self.subject = subject |
paul@1005 | 66 | self.preamble_text = preamble_text |
paul@1309 | 67 | self.suppress_bcc = suppress_bcc |
paul@1005 | 68 | |
paul@1005 | 69 | # The translation method is set by the client once locale information is |
paul@1005 | 70 | # known. |
paul@1005 | 71 | |
paul@1005 | 72 | self.gettext = None |
paul@83 | 73 | |
paul@607 | 74 | def local_delivery(self): |
paul@607 | 75 | |
paul@607 | 76 | "Return whether local delivery is performed using this messenger." |
paul@607 | 77 | |
paul@666 | 78 | return self.lmtp_socket is not None or self.local_smtp |
paul@607 | 79 | |
paul@666 | 80 | def sendmail(self, recipients, data, sender=None, outgoing_bcc=None): |
paul@83 | 81 | |
paul@83 | 82 | """ |
paul@83 | 83 | Send a mail to the given 'recipients' consisting of the given 'data', |
paul@128 | 84 | using the given 'sender' identity if indicated, indicating an |
paul@607 | 85 | 'outgoing_bcc' identity if indicated. |
paul@445 | 86 | |
paul@445 | 87 | The 'outgoing_bcc' argument is required when sending on behalf of a user |
paul@445 | 88 | from the calendar@domain address, since this will not be detected as a |
paul@445 | 89 | valid participant and handled using the outgoing transport. |
paul@83 | 90 | """ |
paul@83 | 91 | |
paul@607 | 92 | if self.lmtp_socket: |
paul@607 | 93 | smtp = LMTP(self.lmtp_socket) |
paul@83 | 94 | else: |
paul@83 | 95 | smtp = SMTP("localhost") |
paul@83 | 96 | |
paul@1309 | 97 | if outgoing_bcc and not self.suppress_bcc: |
paul@128 | 98 | recipients = list(recipients) + ["%s+%s" % (OUTGOING_PREFIX, outgoing_bcc)] |
paul@666 | 99 | elif self.local_smtp: |
paul@666 | 100 | recipients = [self.make_local(recipient) for recipient in recipients] |
paul@128 | 101 | |
paul@86 | 102 | smtp.sendmail(sender or self.sender, recipients, data) |
paul@83 | 103 | smtp.quit() |
paul@83 | 104 | |
paul@666 | 105 | def make_local(self, recipient): |
paul@666 | 106 | |
paul@666 | 107 | """ |
paul@666 | 108 | Make the 'recipient' an address for local delivery. For this to function |
paul@666 | 109 | correctly, a virtual alias or equivalent must be defined for addresses |
paul@666 | 110 | of the following form: |
paul@666 | 111 | |
paul@666 | 112 | local+NAME@DOMAIN |
paul@666 | 113 | |
paul@666 | 114 | Such aliases should direct delivery to the local recipient. |
paul@666 | 115 | """ |
paul@666 | 116 | |
paul@666 | 117 | parts = recipient.split("+", 1) |
paul@894 | 118 | return "%s+%s" % (LOCAL_PREFIX, parts[-1]) |
paul@666 | 119 | |
paul@178 | 120 | def make_outgoing_message(self, parts, recipients, sender=None, outgoing_bcc=None): |
paul@83 | 121 | |
paul@86 | 122 | """ |
paul@86 | 123 | Make a message from the given 'parts' for the given 'recipients', using |
paul@128 | 124 | the given 'sender' identity if indicated, indicating an 'outgoing_bcc' |
paul@128 | 125 | identity if indicated. |
paul@86 | 126 | """ |
paul@83 | 127 | |
paul@178 | 128 | message = self._make_summary_for_parts(parts) |
paul@83 | 129 | |
paul@86 | 130 | message["From"] = sender or self.sender |
paul@83 | 131 | for recipient in recipients: |
paul@83 | 132 | message["To"] = recipient |
paul@1309 | 133 | |
paul@1309 | 134 | if outgoing_bcc and not self.suppress_bcc: |
paul@128 | 135 | message["Bcc"] = "%s+%s" % (OUTGOING_PREFIX, outgoing_bcc) |
paul@1309 | 136 | |
paul@1005 | 137 | message["Subject"] = self.subject or \ |
paul@1005 | 138 | self.gettext and self.gettext(MESSAGE_SUBJECT) or MESSAGE_SUBJECT |
paul@83 | 139 | |
paul@83 | 140 | return message |
paul@83 | 141 | |
paul@178 | 142 | def make_summary_message(self, msg, parts): |
paul@178 | 143 | |
paul@178 | 144 | """ |
paul@178 | 145 | Return a simple summary using details from 'msg' and the given 'parts'. |
paul@1200 | 146 | Information messages provided amongst the parts by the handlers will be |
paul@1379 | 147 | merged into a single text part so that mail programs will show them |
paul@1200 | 148 | immediately. |
paul@178 | 149 | """ |
paul@178 | 150 | |
paul@1200 | 151 | message = self._make_summary_for_parts(parts, True) |
paul@178 | 152 | self._copy_headers(message, msg) |
paul@178 | 153 | return message |
paul@178 | 154 | |
paul@83 | 155 | def wrap_message(self, msg, parts): |
paul@83 | 156 | |
paul@1200 | 157 | """ |
paul@1200 | 158 | Wrap 'msg' and provide the given 'parts' as the primary content. |
paul@1200 | 159 | Information messages provided amongst the parts by the handlers will be |
paul@1379 | 160 | merged into a single text part so that mail programs will show them |
paul@1200 | 161 | immediately. |
paul@1200 | 162 | """ |
paul@83 | 163 | |
paul@1200 | 164 | message = self._make_container_for_parts(parts, True) |
paul@178 | 165 | payload = message.get_payload() |
paul@1379 | 166 | |
paul@1379 | 167 | # The original message is placed in a mailbox part for reference, |
paul@1379 | 168 | # attached to the rest of the primary content parts. |
paul@1379 | 169 | |
paul@178 | 170 | payload.append(MIMEMessage(msg)) |
paul@178 | 171 | self._copy_headers(message, msg) |
paul@178 | 172 | return message |
paul@178 | 173 | |
paul@1200 | 174 | def _make_summary_for_parts(self, parts, merge=False): |
paul@178 | 175 | |
paul@1200 | 176 | """ |
paul@1200 | 177 | Return a simple summary for the given 'parts', merging information parts if |
paul@1200 | 178 | 'merge' is specified and set to a true value. |
paul@1200 | 179 | """ |
paul@178 | 180 | |
paul@178 | 181 | if len(parts) == 1: |
paul@178 | 182 | return parts[0] |
paul@178 | 183 | else: |
paul@1200 | 184 | return self._make_container_for_parts(parts, merge) |
paul@1200 | 185 | |
paul@1200 | 186 | def _make_container_for_parts(self, parts, merge=False): |
paul@1200 | 187 | |
paul@1200 | 188 | """ |
paul@1200 | 189 | Return a container for the given 'parts', merging information parts if |
paul@1200 | 190 | 'merge' is specified and set to a true value. |
paul@1200 | 191 | """ |
paul@178 | 192 | |
paul@1200 | 193 | # Merge calendar information if requested. |
paul@178 | 194 | |
paul@1200 | 195 | if merge: |
paul@1200 | 196 | info, parts = self._merge_calendar_info_parts(parts) |
paul@1200 | 197 | else: |
paul@1200 | 198 | info = [] |
paul@1200 | 199 | |
paul@1379 | 200 | # Provide a preamble message before any other data. |
paul@1379 | 201 | |
paul@1379 | 202 | preamble = self.preamble_text or \ |
paul@1379 | 203 | self.gettext and self.gettext(PREAMBLE_TEXT) or PREAMBLE_TEXT |
paul@1379 | 204 | |
paul@1379 | 205 | # Provide a mixture of aggregated message parts. |
paul@1379 | 206 | |
paul@1379 | 207 | message = parts and MIMEMultipart("mixed", _subparts=parts) or None |
paul@1379 | 208 | |
paul@1379 | 209 | # Provide an informational message using the aggregated information. |
paul@1200 | 210 | |
paul@1379 | 211 | info_message = info and MIMEText("\n\n".join(info)) or None |
paul@1379 | 212 | |
paul@1379 | 213 | # Wrap the information and message parts. |
paul@178 | 214 | |
paul@1379 | 215 | subparts = [] |
paul@1379 | 216 | if info_message: |
paul@1379 | 217 | subparts.append(info_message) |
paul@1379 | 218 | if message: |
paul@1379 | 219 | subparts.append(message) |
paul@1379 | 220 | |
paul@1379 | 221 | top = MIMEMultipart("alternative", _subparts=subparts) |
paul@1379 | 222 | top.preamble = preamble |
paul@1379 | 223 | return top |
paul@83 | 224 | |
paul@1200 | 225 | def _merge_calendar_info_parts(self, parts): |
paul@1200 | 226 | |
paul@1200 | 227 | """ |
paul@1200 | 228 | Return a collection of plain text calendar information messages from |
paul@1200 | 229 | 'parts', together with a collection of the remaining parts. |
paul@1200 | 230 | """ |
paul@1200 | 231 | |
paul@1200 | 232 | info = [] |
paul@1200 | 233 | remaining = [] |
paul@1200 | 234 | |
paul@1200 | 235 | for part in parts: |
paul@1200 | 236 | |
paul@1200 | 237 | # Attempt to acquire informational messages. |
paul@1200 | 238 | |
paul@1200 | 239 | if part.get("X-IMIP-Agent") == "info": |
paul@1200 | 240 | |
paul@1200 | 241 | # Ignore the preamble of any multipart message and just |
paul@1200 | 242 | # collect its parts. |
paul@1200 | 243 | |
paul@1200 | 244 | if part.is_multipart(): |
paul@1200 | 245 | i, r = self._merge_calendar_info_parts(part.get_payload()) |
paul@1200 | 246 | remaining += r |
paul@1200 | 247 | |
paul@1200 | 248 | # Obtain any single-part messages. |
paul@1200 | 249 | |
paul@1200 | 250 | else: |
paul@1200 | 251 | info.append(part.get_payload(decode=True)) |
paul@1200 | 252 | |
paul@1200 | 253 | # Accumulate other parts regardless of their purpose. |
paul@1200 | 254 | |
paul@1200 | 255 | else: |
paul@1200 | 256 | remaining.append(part) |
paul@1200 | 257 | |
paul@1200 | 258 | return info, remaining |
paul@1200 | 259 | |
paul@178 | 260 | def _copy_headers(self, message, msg): |
paul@1200 | 261 | |
paul@1200 | 262 | "Copy to 'message' certain headers from 'msg'." |
paul@1200 | 263 | |
paul@83 | 264 | message["From"] = msg["From"] |
paul@83 | 265 | message["To"] = msg["To"] |
paul@83 | 266 | message["Subject"] = msg["Subject"] |
paul@83 | 267 | |
paul@83 | 268 | # vim: tabstop=4 expandtab shiftwidth=4 |