1 #!/usr/bin/env python 2 3 """ 4 Interaction with the mail system for the manager interface. 5 6 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from imiptools.client import Client 23 from imiptools.data import get_address, get_uri, make_freebusy, \ 24 to_part, uri_item, uri_values 25 from imiptools.dates import format_datetime, get_timestamp 26 from imiptools.handlers import Handler 27 28 class ManagerHandler(Handler): 29 30 """ 31 A content handler for use by the manager, as opposed to operating within the 32 mail processing pipeline. 33 """ 34 35 def __init__(self, obj, user, messenger): 36 Handler.__init__(self, messenger=messenger) 37 Client.__init__(self, user) # this redefines the Handler initialisation 38 39 self.set_object(obj) 40 41 # Communication methods. 42 43 def send_message(self, method, sender, from_organiser, parts=None): 44 45 """ 46 Create a full calendar object employing the given 'method', and send it 47 to the appropriate recipients, also sending a copy to the 'sender'. The 48 'from_organiser' value indicates whether the organiser is sending this 49 message. 50 """ 51 52 parts = parts or [self.obj.to_part(method)] 53 54 # As organiser, send an invitation to attendees, excluding oneself if 55 # also attending. The updated event will be saved by the outgoing 56 # handler. 57 58 organiser = get_uri(self.obj.get_value("ORGANIZER")) 59 attendees = uri_values(self.obj.get_values("ATTENDEE")) 60 61 if from_organiser: 62 recipients = [get_address(attendee) for attendee in attendees if attendee != self.user] 63 else: 64 recipients = [get_address(organiser)] 65 66 # Bundle free/busy information if appropriate. 67 68 if self.is_sharing() and self.is_bundling(): 69 70 # Invent a unique identifier. 71 72 utcnow = get_timestamp() 73 uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) 74 75 freebusy = self.store.get_freebusy(self.user) 76 77 # Since the outgoing handler updates this user's free/busy details, 78 # the stored details will probably not have the updated details at 79 # this point, so we update our copy for serialisation as the bundled 80 # free/busy object. 81 82 self.update_freebusy(freebusy, 83 self.obj.get_periods(self.get_tzid(), self.get_window_end())) 84 85 user_attr = {} 86 self.update_sender(user_attr) 87 88 parts.append(to_part("PUBLISH", [ 89 make_freebusy(freebusy, uid, self.user, user_attr) 90 ])) 91 92 # Explicitly specify the outgoing BCC recipient since we are sending as 93 # the generic calendar user. 94 95 message = self.messenger.make_outgoing_message(parts, recipients, outgoing_bcc=sender) 96 self.messenger.sendmail(recipients, message.as_string(), outgoing_bcc=sender) 97 98 # Action methods. 99 100 def process_received_request(self): 101 102 """ 103 Process the current request for the current user. Return whether any 104 action was taken. 105 """ 106 107 # Reply only on behalf of this user. 108 109 attendee_attr = self.update_participation(self.obj) 110 111 if not attendee_attr: 112 return False 113 114 self.obj["ATTENDEE"] = [(self.user, attendee_attr)] 115 self.update_dtstamp() 116 self.set_sequence(False) 117 self.send_message("REPLY", get_address(self.user), from_organiser=False) 118 return True 119 120 def process_created_request(self, method, to_cancel=None, to_unschedule=None): 121 122 """ 123 Process the current request, sending a created request of the given 124 'method' to attendees. Return whether any action was taken. 125 126 If 'to_cancel' is specified, a list of participants to be sent cancel 127 messages is provided. 128 """ 129 130 # Here, the organiser should be the current user. 131 132 organiser, organiser_attr = uri_item(self.obj.get_item("ORGANIZER")) 133 134 self.update_sender(organiser_attr) 135 self.update_dtstamp() 136 self.set_sequence(True) 137 138 parts = [self.obj.to_part(method)] 139 140 # Add message parts with cancelled occurrence information. 141 # NOTE: This could probably be merged with the updated event message. 142 143 if to_unschedule: 144 obj = self.obj.copy() 145 obj.remove_all(["RRULE", "RDATE", "DTSTART", "DTEND", "DURATION"]) 146 147 for p in to_unschedule: 148 if not p.origin: 149 continue 150 obj["RECURRENCE-ID"] = [(format_datetime(p.get_start()), {})] 151 parts.append(obj.to_part("CANCEL")) 152 153 # Send the updated event, along with a cancellation for each of the 154 # unscheduled occurrences. 155 156 self.send_message("CANCEL", get_address(organiser), from_organiser=True, parts=parts) 157 158 # When cancelling, replace the attendees with those for whom the event 159 # is now cancelled. 160 161 if to_cancel: 162 obj = self.obj.copy() 163 obj["ATTENDEE"] = to_cancel 164 165 # Send a cancellation to all uninvited attendees. 166 167 self.send_message("CANCEL", get_address(organiser), from_organiser=True) 168 169 return True 170 171 # vim: tabstop=4 expandtab shiftwidth=4