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