paul@55 | 1 | #!/usr/bin/env python |
paul@55 | 2 | |
paul@55 | 3 | """ |
paul@55 | 4 | Handlers for a person for whom scheduling is performed. |
paul@146 | 5 | |
paul@146 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 7 | |
paul@146 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 11 | version. |
paul@146 | 12 | |
paul@146 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 16 | details. |
paul@146 | 17 | |
paul@146 | 18 | You should have received a copy of the GNU General Public License along with |
paul@146 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@55 | 20 | """ |
paul@55 | 21 | |
paul@213 | 22 | from imiptools.content import Handler |
paul@213 | 23 | from imiptools.data import get_address, get_uri, uri_dict, uri_items |
paul@216 | 24 | from imiptools.handlers.common import CommonFreebusy |
paul@179 | 25 | from imiptools.profile import Preferences |
paul@55 | 26 | |
paul@67 | 27 | class PersonHandler(Handler): |
paul@55 | 28 | |
paul@67 | 29 | "Handling mechanisms specific to people." |
paul@55 | 30 | |
paul@213 | 31 | def _record_and_deliver(self, from_organiser=True, queue=False, cancel=False): |
paul@55 | 32 | |
paul@100 | 33 | oa = self.require_organiser_and_attendees(from_organiser) |
paul@55 | 34 | if not oa: |
paul@61 | 35 | return False |
paul@55 | 36 | |
paul@94 | 37 | (organiser, organiser_attr), attendees = organiser_item, attendees = oa |
paul@94 | 38 | |
paul@110 | 39 | # Handle notifications and invitations. |
paul@55 | 40 | |
paul@100 | 41 | if from_organiser: |
paul@110 | 42 | |
paul@110 | 43 | # Process each attendee separately. |
paul@110 | 44 | |
paul@100 | 45 | for attendee, attendee_attr in attendees.items(): |
paul@100 | 46 | |
paul@213 | 47 | if not self.have_new_object(attendee): |
paul@100 | 48 | continue |
paul@100 | 49 | |
paul@100 | 50 | # Store the object and queue any request. |
paul@55 | 51 | |
paul@213 | 52 | self.store.set_event(attendee, self.uid, self.obj.to_node()) |
paul@55 | 53 | |
paul@100 | 54 | if queue: |
paul@100 | 55 | self.store.queue_request(attendee, self.uid) |
paul@142 | 56 | elif cancel: |
paul@142 | 57 | self.store.cancel_event(attendee, self.uid) |
paul@100 | 58 | |
paul@261 | 59 | # No return message will occur to update the free/busy |
paul@261 | 60 | # information, so this is done here. |
paul@261 | 61 | |
paul@261 | 62 | freebusy = self.store.get_freebusy(attendee) |
paul@261 | 63 | self.remove_from_freebusy(freebusy, attendee) |
paul@261 | 64 | |
paul@261 | 65 | if self.publisher: |
paul@261 | 66 | self.publisher.set_freebusy(attendee, freebusy) |
paul@261 | 67 | |
paul@268 | 68 | self.update_freebusy_from_organiser(attendee, organiser_item) |
paul@268 | 69 | |
paul@100 | 70 | # As organiser, update attendance. |
paul@55 | 71 | |
paul@100 | 72 | else: |
paul@268 | 73 | if self.merge_attendance(attendees, organiser): |
paul@268 | 74 | self.update_freebusy_from_attendees(organiser, attendees) |
paul@61 | 75 | |
paul@61 | 76 | return True |
paul@61 | 77 | |
paul@110 | 78 | def _record_freebusy(self, from_organiser=True): |
paul@110 | 79 | |
paul@110 | 80 | "Record free/busy information for the received information." |
paul@110 | 81 | |
paul@218 | 82 | senders = self.obj.get_items(from_organiser and "ORGANIZER" or "ATTENDEE") |
paul@218 | 83 | |
paul@218 | 84 | if not senders: |
paul@218 | 85 | return |
paul@218 | 86 | |
paul@110 | 87 | freebusy = [] |
paul@111 | 88 | |
paul@213 | 89 | for value in self.obj.get_values("FREEBUSY") or []: |
paul@110 | 90 | if not isinstance(value, list): |
paul@110 | 91 | value = [value] |
paul@110 | 92 | for v in value: |
paul@110 | 93 | try: |
paul@110 | 94 | start, end = v.split("/", 1) |
paul@110 | 95 | freebusy.append((start, end)) |
paul@110 | 96 | except ValueError: |
paul@110 | 97 | pass |
paul@110 | 98 | |
paul@218 | 99 | for sender, sender_attr in uri_items(senders): |
paul@179 | 100 | self.store.set_freebusy_for_other(get_uri(self.recipient), freebusy, sender) |
paul@110 | 101 | |
paul@216 | 102 | class Event(PersonHandler): |
paul@67 | 103 | |
paul@67 | 104 | "An event handler." |
paul@67 | 105 | |
paul@63 | 106 | def add(self): |
paul@63 | 107 | |
paul@63 | 108 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 109 | |
paul@182 | 110 | return self.wrap("An addition to an event has been received.", link=False) |
paul@63 | 111 | |
paul@63 | 112 | def cancel(self): |
paul@63 | 113 | |
paul@142 | 114 | "Queue a cancellation of any active event." |
paul@63 | 115 | |
paul@213 | 116 | self._record_and_deliver(from_organiser=True, queue=False, cancel=True) |
paul@182 | 117 | return self.wrap("A cancellation has been received.", link=False) |
paul@63 | 118 | |
paul@63 | 119 | def counter(self): |
paul@63 | 120 | |
paul@63 | 121 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 122 | |
paul@182 | 123 | return self.wrap("A counter proposal has been received.", link=False) |
paul@63 | 124 | |
paul@63 | 125 | def declinecounter(self): |
paul@63 | 126 | |
paul@63 | 127 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 128 | |
paul@182 | 129 | return self.wrap("A declining counter proposal has been received.", link=False) |
paul@63 | 130 | |
paul@63 | 131 | def publish(self): |
paul@63 | 132 | |
paul@139 | 133 | "Register details of any relevant event." |
paul@63 | 134 | |
paul@213 | 135 | self._record_and_deliver(from_organiser=True, queue=False) |
paul@182 | 136 | return self.wrap("Details of an event have been received.") |
paul@63 | 137 | |
paul@63 | 138 | def refresh(self): |
paul@63 | 139 | |
paul@139 | 140 | "Update details of any active event." |
paul@63 | 141 | |
paul@213 | 142 | self._record_and_deliver(from_organiser=True, queue=False) |
paul@182 | 143 | return self.wrap("An event update has been received.") |
paul@63 | 144 | |
paul@61 | 145 | def reply(self): |
paul@61 | 146 | |
paul@61 | 147 | "Record replies and notify the recipient." |
paul@61 | 148 | |
paul@213 | 149 | self._record_and_deliver(from_organiser=False, queue=False) |
paul@182 | 150 | return self.wrap("A reply has been received.") |
paul@61 | 151 | |
paul@61 | 152 | def request(self): |
paul@61 | 153 | |
paul@61 | 154 | "Hold requests and notify the recipient." |
paul@61 | 155 | |
paul@213 | 156 | self._record_and_deliver(from_organiser=True, queue=True) |
paul@216 | 157 | return self.wrap("A request has been received.") |
paul@60 | 158 | |
paul@108 | 159 | class Freebusy(PersonHandler, CommonFreebusy): |
paul@55 | 160 | |
paul@55 | 161 | "A free/busy handler." |
paul@55 | 162 | |
paul@55 | 163 | def publish(self): |
paul@63 | 164 | |
paul@110 | 165 | "Register free/busy information." |
paul@110 | 166 | |
paul@180 | 167 | self._record_freebusy(from_organiser=True) |
paul@180 | 168 | |
paul@180 | 169 | # Produce a message if configured to do so. |
paul@63 | 170 | |
paul@180 | 171 | preferences = Preferences(get_uri(self.recipient)) |
paul@180 | 172 | if preferences.get("freebusy_messages") == "notify": |
paul@182 | 173 | return self.wrap("A free/busy update has been received.", link=False) |
paul@55 | 174 | |
paul@55 | 175 | def reply(self): |
paul@55 | 176 | |
paul@63 | 177 | "Record replies and notify the recipient." |
paul@63 | 178 | |
paul@180 | 179 | self._record_freebusy(from_organiser=False) |
paul@180 | 180 | |
paul@180 | 181 | # Produce a message if configured to do so. |
paul@139 | 182 | |
paul@180 | 183 | preferences = Preferences(get_uri(self.recipient)) |
paul@180 | 184 | if preferences.get("freebusy_messages") == "notify": |
paul@182 | 185 | return self.wrap("A reply to a free/busy request has been received.", link=False) |
paul@55 | 186 | |
paul@55 | 187 | def request(self): |
paul@55 | 188 | |
paul@55 | 189 | """ |
paul@55 | 190 | Respond to a request by preparing a reply containing free/busy |
paul@55 | 191 | information for each indicated attendee. |
paul@55 | 192 | """ |
paul@55 | 193 | |
paul@180 | 194 | # Produce a reply if configured to do so. |
paul@55 | 195 | |
paul@180 | 196 | preferences = Preferences(get_uri(self.recipient)) |
paul@180 | 197 | if preferences.get("freebusy_sharing") == "share": |
paul@180 | 198 | return CommonFreebusy.request(self) |
paul@55 | 199 | |
paul@67 | 200 | class Journal(PersonHandler): |
paul@55 | 201 | |
paul@55 | 202 | "A journal entry handler." |
paul@55 | 203 | |
paul@55 | 204 | def add(self): |
paul@63 | 205 | |
paul@63 | 206 | # NOTE: Queue a suggested modification to any active entry. |
paul@63 | 207 | |
paul@182 | 208 | return self.wrap("An addition to a journal entry has been received.", link=False) |
paul@55 | 209 | |
paul@55 | 210 | def cancel(self): |
paul@63 | 211 | |
paul@63 | 212 | # NOTE: Queue a suggested modification to any active entry. |
paul@63 | 213 | |
paul@182 | 214 | return self.wrap("A cancellation has been received.", link=False) |
paul@55 | 215 | |
paul@55 | 216 | def publish(self): |
paul@63 | 217 | |
paul@63 | 218 | # NOTE: Register details of any relevant entry. |
paul@63 | 219 | |
paul@213 | 220 | self._record_and_deliver(from_organiser=True, queue=False) |
paul@182 | 221 | return self.wrap("Details of a journal entry have been received.") |
paul@55 | 222 | |
paul@67 | 223 | class Todo(PersonHandler): |
paul@55 | 224 | |
paul@55 | 225 | "A to-do item handler." |
paul@55 | 226 | |
paul@55 | 227 | def add(self): |
paul@63 | 228 | |
paul@63 | 229 | # NOTE: Queue a suggested modification to any active item. |
paul@63 | 230 | |
paul@182 | 231 | return self.wrap("An addition to an item has been received.", link=False) |
paul@55 | 232 | |
paul@55 | 233 | def cancel(self): |
paul@63 | 234 | |
paul@63 | 235 | # NOTE: Queue a suggested modification to any active item. |
paul@63 | 236 | |
paul@182 | 237 | return self.wrap("A cancellation has been received.", link=False) |
paul@55 | 238 | |
paul@55 | 239 | def counter(self): |
paul@55 | 240 | |
paul@63 | 241 | # NOTE: Queue a suggested modification to any active item. |
paul@55 | 242 | |
paul@182 | 243 | return self.wrap("A counter proposal has been received.", link=False) |
paul@55 | 244 | |
paul@55 | 245 | def declinecounter(self): |
paul@55 | 246 | |
paul@63 | 247 | # NOTE: Queue a suggested modification to any active item. |
paul@55 | 248 | |
paul@182 | 249 | return self.wrap("A declining counter proposal has been received.", link=False) |
paul@55 | 250 | |
paul@55 | 251 | def publish(self): |
paul@63 | 252 | |
paul@139 | 253 | "Register details of any relevant item." |
paul@63 | 254 | |
paul@213 | 255 | self._record_and_deliver(from_organiser=True, queue=False) |
paul@182 | 256 | return self.wrap("Details of an item have been received.") |
paul@55 | 257 | |
paul@55 | 258 | def refresh(self): |
paul@63 | 259 | |
paul@139 | 260 | "Update details of any active item." |
paul@63 | 261 | |
paul@213 | 262 | self._record_and_deliver(from_organiser=True, queue=False) |
paul@182 | 263 | return self.wrap("An item update has been received.") |
paul@55 | 264 | |
paul@55 | 265 | def reply(self): |
paul@55 | 266 | |
paul@63 | 267 | "Record replies and notify the recipient." |
paul@63 | 268 | |
paul@213 | 269 | self._record_and_deliver(from_organiser=False, queue=False) |
paul@182 | 270 | return self.wrap("A reply has been received.") |
paul@55 | 271 | |
paul@55 | 272 | def request(self): |
paul@63 | 273 | |
paul@63 | 274 | "Hold requests and notify the recipient." |
paul@63 | 275 | |
paul@213 | 276 | self._record_and_deliver(from_organiser=True, queue=True) |
paul@182 | 277 | return self.wrap("A request has been received.") |
paul@55 | 278 | |
paul@55 | 279 | # Handler registry. |
paul@55 | 280 | |
paul@55 | 281 | handlers = [ |
paul@55 | 282 | ("VFREEBUSY", Freebusy), |
paul@55 | 283 | ("VEVENT", Event), |
paul@55 | 284 | ("VTODO", Todo), |
paul@55 | 285 | ("VJOURNAL", Journal), |
paul@55 | 286 | ] |
paul@55 | 287 | |
paul@55 | 288 | # vim: tabstop=4 expandtab shiftwidth=4 |