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