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 ClientForObject 23 from imiptools.data import get_address, get_uri, uri_item, uri_values 24 from imiptools.dates import format_datetime 25 26 class ManagerClient(ClientForObject): 27 28 """ 29 A content client for use by the manager, as opposed to operating within the 30 mail processing pipeline. 31 """ 32 33 # Communication methods. 34 35 def send_message(self, method, sender, from_organiser, parts=None): 36 37 """ 38 Create a full calendar object employing the given 'method', and send it 39 to the appropriate recipients, also sending a copy to the 'sender'. The 40 'from_organiser' value indicates whether the organiser is sending this 41 message. 42 """ 43 44 parts = parts or [self.obj.to_part(method)] 45 46 # As organiser, send an invitation to attendees, excluding oneself if 47 # also attending. The updated event will be saved by the outgoing 48 # handler. 49 50 organiser = get_uri(self.obj.get_value("ORGANIZER")) 51 attendees = uri_values(self.obj.get_values("ATTENDEE")) 52 53 if from_organiser: 54 recipients = [get_address(attendee) for attendee in attendees if attendee != self.user] 55 else: 56 recipients = [get_address(organiser)] 57 58 # Since the outgoing handler updates this user's free/busy details, 59 # the stored details will probably not have the updated details at 60 # this point, so we update our copy for serialisation as the bundled 61 # free/busy object. 62 63 freebusy = self.store.get_freebusy(self.user) 64 self.update_freebusy(freebusy, self.user, from_organiser) 65 66 # Bundle free/busy information if appropriate. 67 68 part = self.get_freebusy_part(freebusy) 69 if part: 70 parts.append(part) 71 72 # Explicitly specify the outgoing BCC recipient since we are sending as 73 # the generic calendar user. 74 75 message = self.messenger.make_outgoing_message(parts, recipients, outgoing_bcc=sender) 76 self.messenger.sendmail(recipients, message.as_string(), outgoing_bcc=sender) 77 78 # Action methods. 79 80 def process_received_request(self): 81 82 """ 83 Process the current request for the current user. Return whether any 84 action was taken. 85 """ 86 87 # Reply only on behalf of this user. 88 89 attendee_attr = self.update_participation(self.obj) 90 91 if not attendee_attr: 92 return False 93 94 self.obj["ATTENDEE"] = [(self.user, attendee_attr)] 95 self.update_dtstamp() 96 self.set_sequence(False) 97 self.send_message("REPLY", get_address(self.user), from_organiser=False) 98 return True 99 100 def process_created_request(self, method, to_cancel=None, to_unschedule=None): 101 102 """ 103 Process the current request, sending a created request of the given 104 'method' to attendees. Return whether any action was taken. 105 106 If 'to_cancel' is specified, a list of participants to be sent cancel 107 messages is provided. 108 """ 109 110 # Here, the organiser should be the current user. 111 112 organiser, organiser_attr = uri_item(self.obj.get_item("ORGANIZER")) 113 114 self.update_sender(organiser_attr) 115 self.update_dtstamp() 116 self.set_sequence(True) 117 118 parts = [self.obj.to_part(method)] 119 120 # Add message parts with cancelled occurrence information. 121 # NOTE: This could probably be merged with the updated event message. 122 123 if to_unschedule: 124 obj = self.obj.copy() 125 obj.remove_all(["RRULE", "RDATE", "DTSTART", "DTEND", "DURATION"]) 126 127 for p in to_unschedule: 128 if not p.origin: 129 continue 130 obj["RECURRENCE-ID"] = [(format_datetime(p.get_start()), {})] 131 parts.append(obj.to_part("CANCEL")) 132 133 # Send the updated event, along with a cancellation for each of the 134 # unscheduled occurrences. 135 136 self.send_message("CANCEL", get_address(organiser), from_organiser=True, parts=parts) 137 138 # When cancelling, replace the attendees with those for whom the event 139 # is now cancelled. 140 141 if to_cancel: 142 obj = self.obj.copy() 143 obj["ATTENDEE"] = to_cancel 144 145 # Send a cancellation to all uninvited attendees. 146 147 self.send_message("CANCEL", get_address(organiser), from_organiser=True) 148 149 return True 150 151 # vim: tabstop=4 expandtab shiftwidth=4