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