1 #!/usr/bin/env python 2 3 """ 4 Interpretation and preparation of iMIP content, together with a content handling 5 mechanism employed by specific recipients. 6 7 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 from datetime import datetime, timedelta 24 from email.mime.text import MIMEText 25 from imiptools.config import MANAGER_PATH, MANAGER_URL 26 from imiptools.data import Object, parse_object, \ 27 get_address, get_uri, get_value, \ 28 is_new_object, uri_dict, uri_item 29 from imiptools.dates import format_datetime, to_timezone 30 from imiptools.period import can_schedule, insert_period, remove_period, \ 31 remove_from_freebusy, \ 32 remove_from_freebusy_for_other, \ 33 update_freebusy, update_freebusy_for_other 34 from pytz import timezone 35 from socket import gethostname 36 import imip_store 37 38 try: 39 from cStringIO import StringIO 40 except ImportError: 41 from StringIO import StringIO 42 43 # Handler mechanism objects. 44 45 def handle_itip_part(part, handlers): 46 47 """ 48 Handle the given iTIP 'part' using the given 'handlers' dictionary. 49 50 Return a list of responses, each response being a tuple of the form 51 (outgoing-recipients, message-part). 52 """ 53 54 method = part.get_param("method") 55 56 # Decode the data and parse it. 57 58 f = StringIO(part.get_payload(decode=True)) 59 60 itip = parse_object(f, part.get_content_charset(), "VCALENDAR") 61 62 # Ignore the part if not a calendar object. 63 64 if not itip: 65 return 66 67 # Require consistency between declared and employed methods. 68 69 if get_value(itip, "METHOD") == method: 70 71 # Look for different kinds of sections. 72 73 all_results = [] 74 75 for name, items in itip.items(): 76 77 # Get a handler for the given section. 78 79 handler = handlers.get(name) 80 if not handler: 81 continue 82 83 for item in items: 84 85 # Dispatch to a handler and obtain any response. 86 87 handler.set_object(Object({name : item})) 88 methods[method](handler)() 89 90 # References to the Web interface. 91 92 def get_manager_url(): 93 url_base = MANAGER_URL or "http://%s/" % gethostname() 94 return "%s/%s" % (url_base.rstrip("/"), MANAGER_PATH.lstrip("/")) 95 96 def get_object_url(uid): 97 return "%s/%s" % (get_manager_url().rstrip("/"), uid) 98 99 class Handler: 100 101 "General handler support." 102 103 def __init__(self, senders=None, recipient=None, messenger=None): 104 105 """ 106 Initialise the handler with the calendar 'obj' and the 'senders' and 107 'recipient' of the object (if specifically indicated). 108 """ 109 110 self.senders = senders and set(map(get_address, senders)) 111 self.recipient = recipient and get_address(recipient) 112 self.messenger = messenger 113 114 self.results = [] 115 self.outgoing_methods = set() 116 117 self.obj = None 118 self.uid = None 119 self.sequence = None 120 self.dtstamp = None 121 122 self.store = imip_store.FileStore() 123 124 try: 125 self.publisher = imip_store.FilePublisher() 126 except OSError: 127 self.publisher = None 128 129 def set_object(self, obj): 130 self.obj = obj 131 self.uid = self.obj.get_value("UID") 132 self.sequence = self.obj.get_value("SEQUENCE") 133 self.dtstamp = self.obj.get_value("DTSTAMP") 134 135 def wrap(self, text, link=True): 136 137 "Wrap any valid message for passing to the recipient." 138 139 texts = [] 140 texts.append(text) 141 if link: 142 texts.append("If your mail program cannot handle this " 143 "message, you may view the details here:\n\n%s" % 144 get_object_url(self.uid)) 145 146 return self.add_result(None, None, MIMEText("\n".join(texts))) 147 148 # Result registration. 149 150 def add_result(self, method, outgoing_recipients, part): 151 152 """ 153 Record a result having the given 'method', 'outgoing_recipients' and 154 message part. 155 """ 156 157 if outgoing_recipients: 158 self.outgoing_methods.add(method) 159 self.results.append((outgoing_recipients, part)) 160 161 def get_results(self): 162 return self.results 163 164 def get_outgoing_methods(self): 165 return self.outgoing_methods 166 167 # Access to calendar structures and other data. 168 169 def remove_from_freebusy(self, freebusy, attendee): 170 remove_from_freebusy(freebusy, attendee, self.uid, self.store) 171 172 def remove_from_freebusy_for_other(self, freebusy, user, other): 173 remove_from_freebusy_for_other(freebusy, user, other, self.uid, self.store) 174 175 def update_freebusy(self, freebusy, attendee, periods): 176 return update_freebusy(freebusy, attendee, periods, self.obj.get_value("TRANSP"), self.uid, self.store) 177 178 def update_freebusy_for_other(self, freebusy, user, other, periods): 179 return update_freebusy_for_other(freebusy, user, other, periods, self.obj.get_value("TRANSP"), self.uid, self.store) 180 181 def can_schedule(self, freebusy, periods): 182 return can_schedule(freebusy, periods, self.uid) 183 184 def filter_by_senders(self, mapping): 185 186 """ 187 Return a list of items from 'mapping' filtered using sender information. 188 """ 189 190 if self.senders: 191 192 # Get a mapping from senders to identities. 193 194 identities = self.get_sender_identities(mapping) 195 196 # Find the senders that are valid. 197 198 senders = map(get_address, identities) 199 valid = self.senders.intersection(senders) 200 201 # Return the true identities. 202 203 return [identities[get_uri(address)] for address in valid] 204 else: 205 return mapping 206 207 def filter_by_recipient(self, mapping): 208 209 """ 210 Return a list of items from 'mapping' filtered using recipient 211 information. 212 """ 213 214 if self.recipient: 215 addresses = set(map(get_address, mapping)) 216 return map(get_uri, addresses.intersection([self.recipient])) 217 else: 218 return mapping 219 220 def require_organiser_and_attendees(self, from_organiser=True): 221 222 """ 223 Return the organiser and attendees for the current object, filtered for 224 the recipient of interest. Return None if no identities are eligible. 225 226 Organiser and attendee identities are provided as lower case values. 227 """ 228 229 attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) 230 organiser_item = uri_item(self.obj.get_item("ORGANIZER")) 231 232 # Only provide details for attendees who sent/receive the message. 233 234 attendee_filter_fn = from_organiser and self.filter_by_recipient or self.filter_by_senders 235 236 attendees = {} 237 for attendee in attendee_filter_fn(attendee_map): 238 attendees[attendee] = attendee_map[attendee] 239 240 if not attendees or not organiser_item: 241 return None 242 243 # Only provide details for an organiser who sent/receives the message. 244 245 organiser_filter_fn = from_organiser and self.filter_by_senders or self.filter_by_recipient 246 247 if not organiser_filter_fn(dict([organiser_item])): 248 return None 249 250 return organiser_item, attendees 251 252 def get_sender_identities(self, mapping): 253 254 """ 255 Return a mapping from actual senders to the identities for which they 256 have provided data, extracting this information from the given 257 'mapping'. 258 """ 259 260 senders = {} 261 262 for value, attr in mapping.items(): 263 sent_by = attr.get("SENT-BY") 264 if sent_by: 265 senders[get_uri(sent_by)] = value 266 else: 267 senders[value] = value 268 269 return senders 270 271 def get_object(self, user): 272 273 """ 274 Return the stored object to which the current object refers for the 275 given 'user' and for the given 'objtype'. 276 """ 277 278 f = self.store.get_event(user, self.uid) 279 fragment = f and parse_object(f, "utf-8") 280 return fragment and Object(fragment) 281 282 def have_new_object(self, attendee, obj=None): 283 284 """ 285 Return whether the current object is new to the 'attendee' (or if the 286 given 'obj' is new). 287 """ 288 289 obj = obj or self.get_object(attendee) 290 291 # If found, compare SEQUENCE and potentially DTSTAMP. 292 293 if obj: 294 sequence = obj.get_value("SEQUENCE") 295 dtstamp = obj.get_value("DTSTAMP") 296 297 # If the request refers to an older version of the object, ignore 298 # it. 299 300 return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, 301 self.is_partstat_updated(obj)) 302 303 return True 304 305 def is_partstat_updated(self, obj): 306 307 """ 308 Return whether the participant status has been updated in the current 309 object in comparison to the given 'obj'. 310 311 NOTE: Some clients like Claws Mail erase time information from DTSTAMP 312 NOTE: and make it invalid. Thus, such attendance information may also be 313 NOTE: incorporated into any new object assessment. 314 """ 315 316 old_attendees = obj.get_value_map("ATTENDEE") 317 new_attendees = self.obj.get_value_map("ATTENDEE") 318 319 for attendee, attr in old_attendees.items(): 320 old_partstat = attr.get("PARTSTAT") 321 new_attr = new_attendees.get(attendee) 322 new_partstat = new_attr and new_attr.get("PARTSTAT") 323 324 if old_partstat == "NEEDS-ACTION" and new_partstat and \ 325 new_partstat != old_partstat: 326 327 return True 328 329 return False 330 331 def update_dtstamp(self): 332 333 "Update the DTSTAMP in the current object." 334 335 dtstamp = self.obj.get_utc_datetime("DTSTAMP") 336 utcnow = to_timezone(datetime.utcnow(), "UTC") 337 self.obj["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] 338 339 # Handler registry. 340 341 methods = { 342 "ADD" : lambda handler: handler.add, 343 "CANCEL" : lambda handler: handler.cancel, 344 "COUNTER" : lambda handler: handler.counter, 345 "DECLINECOUNTER" : lambda handler: handler.declinecounter, 346 "PUBLISH" : lambda handler: handler.publish, 347 "REFRESH" : lambda handler: handler.refresh, 348 "REPLY" : lambda handler: handler.reply, 349 "REQUEST" : lambda handler: handler.request, 350 } 351 352 # vim: tabstop=4 expandtab shiftwidth=4