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