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@418 | 6 | Copyright (C) 2014, 2015 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@606 | 25 | from imiptools.data import get_address, get_uri, get_sender_identities, \ |
paul@606 | 26 | 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@639 | 47 | publisher=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@563 | 56 | The optional 'store' and 'publisher' can be specified to override the |
paul@563 | 57 | default store and publisher objects. |
paul@418 | 58 | """ |
paul@418 | 59 | |
paul@639 | 60 | ClientForObject.__init__(self, None, recipient and get_uri(recipient), messenger, store, publisher, preferences_dir) |
paul@468 | 61 | |
paul@418 | 62 | self.senders = senders and set(map(get_address, senders)) |
paul@418 | 63 | self.recipient = recipient and get_address(recipient) |
paul@418 | 64 | |
paul@418 | 65 | self.results = [] |
paul@418 | 66 | self.outgoing_methods = set() |
paul@418 | 67 | |
paul@418 | 68 | def wrap(self, text, link=True): |
paul@418 | 69 | |
paul@418 | 70 | "Wrap any valid message for passing to the recipient." |
paul@418 | 71 | |
paul@418 | 72 | texts = [] |
paul@418 | 73 | texts.append(text) |
paul@668 | 74 | if link and self.have_manager(): |
paul@418 | 75 | texts.append("If your mail program cannot handle this " |
paul@418 | 76 | "message, you may view the details here:\n\n%s" % |
paul@418 | 77 | get_object_url(self.uid, self.recurrenceid)) |
paul@418 | 78 | |
paul@418 | 79 | return self.add_result(None, None, MIMEText("\n".join(texts))) |
paul@418 | 80 | |
paul@418 | 81 | # Result registration. |
paul@418 | 82 | |
paul@418 | 83 | def add_result(self, method, outgoing_recipients, part): |
paul@418 | 84 | |
paul@418 | 85 | """ |
paul@418 | 86 | Record a result having the given 'method', 'outgoing_recipients' and |
paul@864 | 87 | message 'part'. |
paul@418 | 88 | """ |
paul@418 | 89 | |
paul@418 | 90 | if outgoing_recipients: |
paul@418 | 91 | self.outgoing_methods.add(method) |
paul@418 | 92 | self.results.append((outgoing_recipients, part)) |
paul@418 | 93 | |
paul@864 | 94 | def add_results(self, methods, outgoing_recipients, parts): |
paul@864 | 95 | |
paul@864 | 96 | """ |
paul@864 | 97 | Record results having the given 'methods', 'outgoing_recipients' and |
paul@864 | 98 | message 'parts'. |
paul@864 | 99 | """ |
paul@864 | 100 | |
paul@864 | 101 | if outgoing_recipients: |
paul@864 | 102 | self.outgoing_methods.update(methods) |
paul@864 | 103 | for part in parts: |
paul@864 | 104 | self.results.append((outgoing_recipients, part)) |
paul@864 | 105 | |
paul@418 | 106 | def get_results(self): |
paul@418 | 107 | return self.results |
paul@418 | 108 | |
paul@418 | 109 | def get_outgoing_methods(self): |
paul@418 | 110 | return self.outgoing_methods |
paul@418 | 111 | |
paul@418 | 112 | # Logic, filtering and access to calendar structures and other data. |
paul@418 | 113 | |
paul@418 | 114 | def filter_by_senders(self, mapping): |
paul@418 | 115 | |
paul@418 | 116 | """ |
paul@418 | 117 | Return a list of items from 'mapping' filtered using sender information. |
paul@418 | 118 | """ |
paul@418 | 119 | |
paul@418 | 120 | if self.senders: |
paul@418 | 121 | |
paul@418 | 122 | # Get a mapping from senders to identities. |
paul@418 | 123 | |
paul@606 | 124 | identities = get_sender_identities(mapping) |
paul@418 | 125 | |
paul@418 | 126 | # Find the senders that are valid. |
paul@418 | 127 | |
paul@418 | 128 | senders = map(get_address, identities) |
paul@418 | 129 | valid = self.senders.intersection(senders) |
paul@418 | 130 | |
paul@418 | 131 | # Return the true identities. |
paul@418 | 132 | |
paul@606 | 133 | return reduce(lambda a, b: a + b, [identities[get_uri(address)] for address in valid], []) |
paul@418 | 134 | else: |
paul@418 | 135 | return mapping |
paul@418 | 136 | |
paul@418 | 137 | def filter_by_recipient(self, mapping): |
paul@418 | 138 | |
paul@418 | 139 | """ |
paul@418 | 140 | Return a list of items from 'mapping' filtered using recipient |
paul@418 | 141 | information. |
paul@418 | 142 | """ |
paul@418 | 143 | |
paul@418 | 144 | if self.recipient: |
paul@418 | 145 | addresses = set(map(get_address, mapping)) |
paul@418 | 146 | return map(get_uri, addresses.intersection([self.recipient])) |
paul@418 | 147 | else: |
paul@418 | 148 | return mapping |
paul@418 | 149 | |
paul@418 | 150 | def require_organiser(self, from_organiser=True): |
paul@418 | 151 | |
paul@418 | 152 | """ |
paul@418 | 153 | Return the organiser for the current object, filtered for the sender or |
paul@418 | 154 | recipient of interest. Return None if no identities are eligible. |
paul@418 | 155 | |
paul@418 | 156 | The organiser identity is normalized. |
paul@418 | 157 | """ |
paul@418 | 158 | |
paul@712 | 159 | organiser, organiser_attr = organiser_item = uri_item(self.obj.get_item("ORGANIZER")) |
paul@712 | 160 | |
paul@712 | 161 | if not organiser: |
paul@712 | 162 | return None |
paul@418 | 163 | |
paul@418 | 164 | # Only provide details for an organiser who sent/receives the message. |
paul@418 | 165 | |
paul@418 | 166 | organiser_filter_fn = from_organiser and self.filter_by_senders or self.filter_by_recipient |
paul@418 | 167 | |
paul@418 | 168 | if not organiser_filter_fn(dict([organiser_item])): |
paul@418 | 169 | return None |
paul@418 | 170 | |
paul@717 | 171 | # Test against any previously-received organiser details. |
paul@717 | 172 | |
paul@728 | 173 | if not self.is_recognised_organiser(organiser): |
paul@734 | 174 | replacement = self.get_organiser_replacement() |
paul@728 | 175 | |
paul@728 | 176 | # Allow any organiser as a replacement where indicated. |
paul@728 | 177 | |
paul@728 | 178 | if replacement == "any": |
paul@728 | 179 | pass |
paul@728 | 180 | |
paul@728 | 181 | # Allow any recognised attendee as a replacement where indicated. |
paul@728 | 182 | |
paul@728 | 183 | elif replacement != "attendee" or not self.is_recognised_attendee(organiser): |
paul@717 | 184 | return None |
paul@717 | 185 | |
paul@418 | 186 | return organiser_item |
paul@418 | 187 | |
paul@418 | 188 | def require_attendees(self, from_organiser=True): |
paul@418 | 189 | |
paul@418 | 190 | """ |
paul@418 | 191 | Return the attendees for the current object, filtered for the sender or |
paul@418 | 192 | recipient of interest. Return None if no identities are eligible. |
paul@418 | 193 | |
paul@418 | 194 | The attendee identities are normalized. |
paul@418 | 195 | """ |
paul@418 | 196 | |
paul@418 | 197 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@418 | 198 | |
paul@418 | 199 | # Only provide details for attendees who sent/receive the message. |
paul@418 | 200 | |
paul@418 | 201 | attendee_filter_fn = from_organiser and self.filter_by_recipient or self.filter_by_senders |
paul@418 | 202 | |
paul@418 | 203 | attendees = {} |
paul@418 | 204 | for attendee in attendee_filter_fn(attendee_map): |
paul@712 | 205 | if attendee: |
paul@712 | 206 | attendees[attendee] = attendee_map[attendee] |
paul@418 | 207 | |
paul@418 | 208 | return attendees |
paul@418 | 209 | |
paul@418 | 210 | def require_organiser_and_attendees(self, from_organiser=True): |
paul@418 | 211 | |
paul@418 | 212 | """ |
paul@418 | 213 | Return the organiser and attendees for the current object, filtered for |
paul@418 | 214 | the recipient of interest. Return None if no identities are eligible. |
paul@418 | 215 | |
paul@418 | 216 | Organiser and attendee identities are normalized. |
paul@418 | 217 | """ |
paul@418 | 218 | |
paul@418 | 219 | organiser_item = self.require_organiser(from_organiser) |
paul@418 | 220 | attendees = self.require_attendees(from_organiser) |
paul@418 | 221 | |
paul@418 | 222 | if not attendees or not organiser_item: |
paul@418 | 223 | return None |
paul@418 | 224 | |
paul@418 | 225 | return organiser_item, attendees |
paul@418 | 226 | |
paul@418 | 227 | # vim: tabstop=4 expandtab shiftwidth=4 |