paul@418 | 1 | #!/usr/bin/env python |
paul@418 | 2 | |
paul@418 | 3 | """ |
paul@418 | 4 | General handler support for incoming calendar objects. |
paul@418 | 5 | |
paul@1039 | 6 | Copyright (C) 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@418 | 7 | |
paul@418 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@418 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@418 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@418 | 11 | version. |
paul@418 | 12 | |
paul@418 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@418 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@418 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@418 | 16 | details. |
paul@418 | 17 | |
paul@418 | 18 | You should have received a copy of the GNU General Public License along with |
paul@418 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@418 | 20 | """ |
paul@418 | 21 | |
paul@418 | 22 | from email.mime.text import MIMEText |
paul@601 | 23 | from imiptools.client import ClientForObject |
paul@986 | 24 | from imiptools.config import MANAGER_PATH, MANAGER_URL, MANAGER_URL_SCHEME |
paul@1176 | 25 | from imiptools.data import check_delegation, get_address, get_uri, \ |
paul@1176 | 26 | get_sender_identities, uri_dict, uri_item |
paul@418 | 27 | from socket import gethostname |
paul@418 | 28 | |
paul@418 | 29 | # References to the Web interface. |
paul@418 | 30 | |
paul@418 | 31 | def get_manager_url(): |
paul@986 | 32 | url_base = MANAGER_URL or \ |
paul@986 | 33 | "%s%s/" % (MANAGER_URL_SCHEME or "https://", gethostname()) |
paul@418 | 34 | return "%s/%s" % (url_base.rstrip("/"), MANAGER_PATH.lstrip("/")) |
paul@418 | 35 | |
paul@418 | 36 | def get_object_url(uid, recurrenceid=None): |
paul@418 | 37 | return "%s/%s%s" % ( |
paul@418 | 38 | get_manager_url().rstrip("/"), uid, |
paul@418 | 39 | recurrenceid and "/%s" % recurrenceid or "" |
paul@418 | 40 | ) |
paul@418 | 41 | |
paul@601 | 42 | class Handler(ClientForObject): |
paul@418 | 43 | |
paul@418 | 44 | "General handler support." |
paul@418 | 45 | |
paul@563 | 46 | def __init__(self, senders=None, recipient=None, messenger=None, store=None, |
paul@1039 | 47 | publisher=None, journal=None, preferences_dir=None): |
paul@418 | 48 | |
paul@418 | 49 | """ |
paul@601 | 50 | Initialise the handler with any specifically indicated 'senders' and |
paul@601 | 51 | 'recipient' of a calendar object. The object is initially undefined. |
paul@601 | 52 | |
paul@601 | 53 | The optional 'messenger' provides a means of interacting with the mail |
paul@601 | 54 | system. |
paul@563 | 55 | |
paul@1039 | 56 | The optional 'store', 'publisher' and 'journal' can be specified to |
paul@1039 | 57 | override the default store and publisher objects. |
paul@418 | 58 | """ |
paul@418 | 59 | |
paul@1039 | 60 | ClientForObject.__init__(self, None, recipient and get_uri(recipient), |
paul@1039 | 61 | messenger, store, publisher, journal, preferences_dir) |
paul@468 | 62 | |
paul@418 | 63 | self.senders = senders and set(map(get_address, senders)) |
paul@418 | 64 | self.recipient = recipient and get_address(recipient) |
paul@418 | 65 | |
paul@418 | 66 | self.results = [] |
paul@418 | 67 | self.outgoing_methods = set() |
paul@418 | 68 | |
paul@418 | 69 | def wrap(self, text, link=True): |
paul@418 | 70 | |
paul@418 | 71 | "Wrap any valid message for passing to the recipient." |
paul@418 | 72 | |
paul@1005 | 73 | _ = self.get_translator() |
paul@1005 | 74 | |
paul@418 | 75 | texts = [] |
paul@418 | 76 | texts.append(text) |
paul@1200 | 77 | |
paul@1200 | 78 | # Add a link to the manager application if available and requested. |
paul@1200 | 79 | |
paul@668 | 80 | if link and self.have_manager(): |
paul@1005 | 81 | texts.append(_("If your mail program cannot handle this " |
paul@1200 | 82 | "message, you may view the details here:\n\n%s\n") % |
paul@418 | 83 | get_object_url(self.uid, self.recurrenceid)) |
paul@418 | 84 | |
paul@1200 | 85 | # Create the text part, tagging it with a header that allows this part |
paul@1200 | 86 | # to be merged with other calendar information. |
paul@1200 | 87 | |
paul@1200 | 88 | text_part = MIMEText("\n\n".join(texts)) |
paul@1200 | 89 | text_part["X-IMIP-Agent"] = "info" |
paul@1200 | 90 | return self.add_result(None, None, text_part) |
paul@418 | 91 | |
paul@418 | 92 | # Result registration. |
paul@418 | 93 | |
paul@418 | 94 | def add_result(self, method, outgoing_recipients, part): |
paul@418 | 95 | |
paul@418 | 96 | """ |
paul@418 | 97 | Record a result having the given 'method', 'outgoing_recipients' and |
paul@864 | 98 | message 'part'. |
paul@418 | 99 | """ |
paul@418 | 100 | |
paul@418 | 101 | if outgoing_recipients: |
paul@418 | 102 | self.outgoing_methods.add(method) |
paul@418 | 103 | self.results.append((outgoing_recipients, part)) |
paul@418 | 104 | |
paul@864 | 105 | def add_results(self, methods, outgoing_recipients, parts): |
paul@864 | 106 | |
paul@864 | 107 | """ |
paul@864 | 108 | Record results having the given 'methods', 'outgoing_recipients' and |
paul@864 | 109 | message 'parts'. |
paul@864 | 110 | """ |
paul@864 | 111 | |
paul@864 | 112 | if outgoing_recipients: |
paul@864 | 113 | self.outgoing_methods.update(methods) |
paul@864 | 114 | for part in parts: |
paul@864 | 115 | self.results.append((outgoing_recipients, part)) |
paul@864 | 116 | |
paul@418 | 117 | def get_results(self): |
paul@418 | 118 | return self.results |
paul@418 | 119 | |
paul@418 | 120 | def get_outgoing_methods(self): |
paul@418 | 121 | return self.outgoing_methods |
paul@418 | 122 | |
paul@418 | 123 | # Logic, filtering and access to calendar structures and other data. |
paul@418 | 124 | |
paul@418 | 125 | def filter_by_senders(self, mapping): |
paul@418 | 126 | |
paul@418 | 127 | """ |
paul@418 | 128 | Return a list of items from 'mapping' filtered using sender information. |
paul@418 | 129 | """ |
paul@418 | 130 | |
paul@418 | 131 | if self.senders: |
paul@418 | 132 | |
paul@418 | 133 | # Get a mapping from senders to identities. |
paul@418 | 134 | |
paul@606 | 135 | identities = get_sender_identities(mapping) |
paul@418 | 136 | |
paul@418 | 137 | # Find the senders that are valid. |
paul@418 | 138 | |
paul@418 | 139 | senders = map(get_address, identities) |
paul@418 | 140 | valid = self.senders.intersection(senders) |
paul@418 | 141 | |
paul@418 | 142 | # Return the true identities. |
paul@418 | 143 | |
paul@606 | 144 | return reduce(lambda a, b: a + b, [identities[get_uri(address)] for address in valid], []) |
paul@418 | 145 | else: |
paul@418 | 146 | return mapping |
paul@418 | 147 | |
paul@418 | 148 | def filter_by_recipient(self, mapping): |
paul@418 | 149 | |
paul@418 | 150 | """ |
paul@418 | 151 | Return a list of items from 'mapping' filtered using recipient |
paul@418 | 152 | information. |
paul@418 | 153 | """ |
paul@418 | 154 | |
paul@418 | 155 | if self.recipient: |
paul@418 | 156 | addresses = set(map(get_address, mapping)) |
paul@418 | 157 | return map(get_uri, addresses.intersection([self.recipient])) |
paul@418 | 158 | else: |
paul@418 | 159 | return mapping |
paul@418 | 160 | |
paul@1176 | 161 | def is_delegation(self): |
paul@1176 | 162 | |
paul@1176 | 163 | """ |
paul@1176 | 164 | Return whether delegation is occurring by returning any delegator. |
paul@1176 | 165 | """ |
paul@1176 | 166 | |
paul@1176 | 167 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@1176 | 168 | attendee_attr = attendee_map.get(self.user) |
paul@1176 | 169 | return check_delegation(attendee_map, self.user, attendee_attr) |
paul@1176 | 170 | |
paul@418 | 171 | def require_organiser(self, from_organiser=True): |
paul@418 | 172 | |
paul@418 | 173 | """ |
paul@1176 | 174 | Return the normalised organiser for the current object, filtered for the |
paul@1176 | 175 | sender or recipient of interest. Return None if no identities are |
paul@1176 | 176 | eligible. |
paul@418 | 177 | |
paul@1176 | 178 | If the sender is not the organiser but is delegating to the recipient, |
paul@1176 | 179 | the actual organiser is returned. |
paul@418 | 180 | """ |
paul@418 | 181 | |
paul@712 | 182 | organiser, organiser_attr = organiser_item = uri_item(self.obj.get_item("ORGANIZER")) |
paul@712 | 183 | |
paul@712 | 184 | if not organiser: |
paul@712 | 185 | return None |
paul@418 | 186 | |
paul@1176 | 187 | # Check the delegate status of the recipient. |
paul@1176 | 188 | |
paul@1176 | 189 | delegated = from_organiser and self.is_delegation() |
paul@1176 | 190 | |
paul@1176 | 191 | # Only provide details for an organiser who sent/receives the message or |
paul@1176 | 192 | # is presiding over a delegation. |
paul@418 | 193 | |
paul@418 | 194 | organiser_filter_fn = from_organiser and self.filter_by_senders or self.filter_by_recipient |
paul@418 | 195 | |
paul@1176 | 196 | if not delegated and not organiser_filter_fn(dict([organiser_item])): |
paul@418 | 197 | return None |
paul@418 | 198 | |
paul@717 | 199 | # Test against any previously-received organiser details. |
paul@717 | 200 | |
paul@728 | 201 | if not self.is_recognised_organiser(organiser): |
paul@734 | 202 | replacement = self.get_organiser_replacement() |
paul@728 | 203 | |
paul@728 | 204 | # Allow any organiser as a replacement where indicated. |
paul@728 | 205 | |
paul@728 | 206 | if replacement == "any": |
paul@728 | 207 | pass |
paul@728 | 208 | |
paul@728 | 209 | # Allow any recognised attendee as a replacement where indicated. |
paul@728 | 210 | |
paul@728 | 211 | elif replacement != "attendee" or not self.is_recognised_attendee(organiser): |
paul@717 | 212 | return None |
paul@717 | 213 | |
paul@418 | 214 | return organiser_item |
paul@418 | 215 | |
paul@418 | 216 | def require_attendees(self, from_organiser=True): |
paul@418 | 217 | |
paul@418 | 218 | """ |
paul@418 | 219 | Return the attendees for the current object, filtered for the sender or |
paul@418 | 220 | recipient of interest. Return None if no identities are eligible. |
paul@418 | 221 | |
paul@418 | 222 | The attendee identities are normalized. |
paul@418 | 223 | """ |
paul@418 | 224 | |
paul@418 | 225 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@418 | 226 | |
paul@418 | 227 | # Only provide details for attendees who sent/receive the message. |
paul@418 | 228 | |
paul@418 | 229 | attendee_filter_fn = from_organiser and self.filter_by_recipient or self.filter_by_senders |
paul@418 | 230 | |
paul@418 | 231 | attendees = {} |
paul@418 | 232 | for attendee in attendee_filter_fn(attendee_map): |
paul@712 | 233 | if attendee: |
paul@712 | 234 | attendees[attendee] = attendee_map[attendee] |
paul@418 | 235 | |
paul@418 | 236 | return attendees |
paul@418 | 237 | |
paul@418 | 238 | def require_organiser_and_attendees(self, from_organiser=True): |
paul@418 | 239 | |
paul@418 | 240 | """ |
paul@418 | 241 | Return the organiser and attendees for the current object, filtered for |
paul@418 | 242 | the recipient of interest. Return None if no identities are eligible. |
paul@418 | 243 | |
paul@418 | 244 | Organiser and attendee identities are normalized. |
paul@418 | 245 | """ |
paul@418 | 246 | |
paul@418 | 247 | organiser_item = self.require_organiser(from_organiser) |
paul@418 | 248 | attendees = self.require_attendees(from_organiser) |
paul@418 | 249 | |
paul@418 | 250 | if not attendees or not organiser_item: |
paul@418 | 251 | return None |
paul@418 | 252 | |
paul@418 | 253 | return organiser_item, attendees |
paul@418 | 254 | |
paul@418 | 255 | # vim: tabstop=4 expandtab shiftwidth=4 |