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@1005 | 47 | def __init__(self, lmtp_socket=None, local_smtp=False, sender=None, subject=None, preamble_text=None): |
paul@607 | 48 | |
paul@607 | 49 | """ |
paul@666 | 50 | Deliver to a local mail system using LMTP if 'lmtp_socket' is provided |
paul@666 | 51 | or if 'local_smtp' is given as a true value. |
paul@607 | 52 | """ |
paul@607 | 53 | |
paul@607 | 54 | self.lmtp_socket = lmtp_socket |
paul@666 | 55 | self.local_smtp = local_smtp |
paul@83 | 56 | self.sender = sender or MESSAGE_SENDER |
paul@1005 | 57 | self.subject = subject |
paul@1005 | 58 | self.preamble_text = preamble_text |
paul@1005 | 59 | |
paul@1005 | 60 | # The translation method is set by the client once locale information is |
paul@1005 | 61 | # known. |
paul@1005 | 62 | |
paul@1005 | 63 | self.gettext = None |
paul@83 | 64 | |
paul@607 | 65 | def local_delivery(self): |
paul@607 | 66 | |
paul@607 | 67 | "Return whether local delivery is performed using this messenger." |
paul@607 | 68 | |
paul@666 | 69 | return self.lmtp_socket is not None or self.local_smtp |
paul@607 | 70 | |
paul@666 | 71 | def sendmail(self, recipients, data, sender=None, outgoing_bcc=None): |
paul@83 | 72 | |
paul@83 | 73 | """ |
paul@83 | 74 | Send a mail to the given 'recipients' consisting of the given 'data', |
paul@128 | 75 | using the given 'sender' identity if indicated, indicating an |
paul@607 | 76 | 'outgoing_bcc' identity if indicated. |
paul@445 | 77 | |
paul@445 | 78 | The 'outgoing_bcc' argument is required when sending on behalf of a user |
paul@445 | 79 | from the calendar@domain address, since this will not be detected as a |
paul@445 | 80 | valid participant and handled using the outgoing transport. |
paul@83 | 81 | """ |
paul@83 | 82 | |
paul@607 | 83 | if self.lmtp_socket: |
paul@607 | 84 | smtp = LMTP(self.lmtp_socket) |
paul@83 | 85 | else: |
paul@83 | 86 | smtp = SMTP("localhost") |
paul@83 | 87 | |
paul@128 | 88 | if outgoing_bcc: |
paul@128 | 89 | recipients = list(recipients) + ["%s+%s" % (OUTGOING_PREFIX, outgoing_bcc)] |
paul@666 | 90 | elif self.local_smtp: |
paul@666 | 91 | recipients = [self.make_local(recipient) for recipient in recipients] |
paul@128 | 92 | |
paul@86 | 93 | smtp.sendmail(sender or self.sender, recipients, data) |
paul@83 | 94 | smtp.quit() |
paul@83 | 95 | |
paul@666 | 96 | def make_local(self, recipient): |
paul@666 | 97 | |
paul@666 | 98 | """ |
paul@666 | 99 | Make the 'recipient' an address for local delivery. For this to function |
paul@666 | 100 | correctly, a virtual alias or equivalent must be defined for addresses |
paul@666 | 101 | of the following form: |
paul@666 | 102 | |
paul@666 | 103 | local+NAME@DOMAIN |
paul@666 | 104 | |
paul@666 | 105 | Such aliases should direct delivery to the local recipient. |
paul@666 | 106 | """ |
paul@666 | 107 | |
paul@666 | 108 | parts = recipient.split("+", 1) |
paul@894 | 109 | return "%s+%s" % (LOCAL_PREFIX, parts[-1]) |
paul@666 | 110 | |
paul@178 | 111 | def make_outgoing_message(self, parts, recipients, sender=None, outgoing_bcc=None): |
paul@83 | 112 | |
paul@86 | 113 | """ |
paul@86 | 114 | Make a message from the given 'parts' for the given 'recipients', using |
paul@128 | 115 | the given 'sender' identity if indicated, indicating an 'outgoing_bcc' |
paul@128 | 116 | identity if indicated. |
paul@86 | 117 | """ |
paul@83 | 118 | |
paul@178 | 119 | message = self._make_summary_for_parts(parts) |
paul@83 | 120 | |
paul@86 | 121 | message["From"] = sender or self.sender |
paul@83 | 122 | for recipient in recipients: |
paul@83 | 123 | message["To"] = recipient |
paul@128 | 124 | if outgoing_bcc: |
paul@128 | 125 | message["Bcc"] = "%s+%s" % (OUTGOING_PREFIX, outgoing_bcc) |
paul@1005 | 126 | message["Subject"] = self.subject or \ |
paul@1005 | 127 | self.gettext and self.gettext(MESSAGE_SUBJECT) or MESSAGE_SUBJECT |
paul@83 | 128 | |
paul@83 | 129 | return message |
paul@83 | 130 | |
paul@178 | 131 | def make_summary_message(self, msg, parts): |
paul@178 | 132 | |
paul@178 | 133 | """ |
paul@178 | 134 | Return a simple summary using details from 'msg' and the given 'parts'. |
paul@1200 | 135 | Information messages provided amongst the parts by the handlers will be |
paul@1200 | 136 | merged into the preamble so that mail programs will show them |
paul@1200 | 137 | immediately. |
paul@178 | 138 | """ |
paul@178 | 139 | |
paul@1200 | 140 | message = self._make_summary_for_parts(parts, True) |
paul@178 | 141 | self._copy_headers(message, msg) |
paul@178 | 142 | return message |
paul@178 | 143 | |
paul@83 | 144 | def wrap_message(self, msg, parts): |
paul@83 | 145 | |
paul@1200 | 146 | """ |
paul@1200 | 147 | Wrap 'msg' and provide the given 'parts' as the primary content. |
paul@1200 | 148 | Information messages provided amongst the parts by the handlers will be |
paul@1200 | 149 | merged into the preamble so that mail programs will show them |
paul@1200 | 150 | immediately. |
paul@1200 | 151 | """ |
paul@83 | 152 | |
paul@1200 | 153 | message = self._make_container_for_parts(parts, True) |
paul@178 | 154 | payload = message.get_payload() |
paul@178 | 155 | payload.append(MIMEMessage(msg)) |
paul@178 | 156 | self._copy_headers(message, msg) |
paul@178 | 157 | return message |
paul@178 | 158 | |
paul@1200 | 159 | def _make_summary_for_parts(self, parts, merge=False): |
paul@178 | 160 | |
paul@1200 | 161 | """ |
paul@1200 | 162 | Return a simple summary for the given 'parts', merging information parts if |
paul@1200 | 163 | 'merge' is specified and set to a true value. |
paul@1200 | 164 | """ |
paul@178 | 165 | |
paul@178 | 166 | if len(parts) == 1: |
paul@178 | 167 | return parts[0] |
paul@178 | 168 | else: |
paul@1200 | 169 | return self._make_container_for_parts(parts, merge) |
paul@1200 | 170 | |
paul@1200 | 171 | def _make_container_for_parts(self, parts, merge=False): |
paul@1200 | 172 | |
paul@1200 | 173 | """ |
paul@1200 | 174 | Return a container for the given 'parts', merging information parts if |
paul@1200 | 175 | 'merge' is specified and set to a true value. |
paul@1200 | 176 | """ |
paul@178 | 177 | |
paul@1200 | 178 | # Merge calendar information if requested. |
paul@178 | 179 | |
paul@1200 | 180 | if merge: |
paul@1200 | 181 | info, parts = self._merge_calendar_info_parts(parts) |
paul@1200 | 182 | else: |
paul@1200 | 183 | info = [] |
paul@1200 | 184 | |
paul@1200 | 185 | # Insert a preamble message before any calendar information messages. |
paul@1200 | 186 | |
paul@1200 | 187 | info.insert(0, self.preamble_text or |
paul@1200 | 188 | self.gettext and self.gettext(PREAMBLE_TEXT) or PREAMBLE_TEXT) |
paul@178 | 189 | |
paul@83 | 190 | message = MIMEMultipart("mixed", _subparts=parts) |
paul@1200 | 191 | message.preamble = "\n\n".join(info) |
paul@178 | 192 | return message |
paul@83 | 193 | |
paul@1200 | 194 | def _merge_calendar_info_parts(self, parts): |
paul@1200 | 195 | |
paul@1200 | 196 | """ |
paul@1200 | 197 | Return a collection of plain text calendar information messages from |
paul@1200 | 198 | 'parts', together with a collection of the remaining parts. |
paul@1200 | 199 | """ |
paul@1200 | 200 | |
paul@1200 | 201 | info = [] |
paul@1200 | 202 | remaining = [] |
paul@1200 | 203 | |
paul@1200 | 204 | for part in parts: |
paul@1200 | 205 | |
paul@1200 | 206 | # Attempt to acquire informational messages. |
paul@1200 | 207 | |
paul@1200 | 208 | if part.get("X-IMIP-Agent") == "info": |
paul@1200 | 209 | |
paul@1200 | 210 | # Ignore the preamble of any multipart message and just |
paul@1200 | 211 | # collect its parts. |
paul@1200 | 212 | |
paul@1200 | 213 | if part.is_multipart(): |
paul@1200 | 214 | i, r = self._merge_calendar_info_parts(part.get_payload()) |
paul@1200 | 215 | remaining += r |
paul@1200 | 216 | |
paul@1200 | 217 | # Obtain any single-part messages. |
paul@1200 | 218 | |
paul@1200 | 219 | else: |
paul@1200 | 220 | info.append(part.get_payload(decode=True)) |
paul@1200 | 221 | |
paul@1200 | 222 | # Accumulate other parts regardless of their purpose. |
paul@1200 | 223 | |
paul@1200 | 224 | else: |
paul@1200 | 225 | remaining.append(part) |
paul@1200 | 226 | |
paul@1200 | 227 | return info, remaining |
paul@1200 | 228 | |
paul@178 | 229 | def _copy_headers(self, message, msg): |
paul@1200 | 230 | |
paul@1200 | 231 | "Copy to 'message' certain headers from 'msg'." |
paul@1200 | 232 | |
paul@83 | 233 | message["From"] = msg["From"] |
paul@83 | 234 | message["To"] = msg["To"] |
paul@83 | 235 | message["Subject"] = msg["Subject"] |
paul@83 | 236 | |
paul@83 | 237 | # vim: tabstop=4 expandtab shiftwidth=4 |