paul@96 | 1 | #!/usr/bin/env python |
paul@96 | 2 | |
paul@96 | 3 | """ |
paul@96 | 4 | Handlers for a person for whom scheduling is performed, inspecting outgoing |
paul@96 | 5 | messages to obtain scheduling done externally. |
paul@146 | 6 | |
paul@146 | 7 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 8 | |
paul@146 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 12 | version. |
paul@146 | 13 | |
paul@146 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 17 | details. |
paul@146 | 18 | |
paul@146 | 19 | You should have received a copy of the GNU General Public License along with |
paul@146 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@96 | 21 | """ |
paul@96 | 22 | |
paul@213 | 23 | from imiptools.content import Handler |
paul@213 | 24 | from imiptools.data import uri_item |
paul@96 | 25 | |
paul@96 | 26 | class PersonHandler(Handler): |
paul@96 | 27 | |
paul@96 | 28 | "Handling mechanisms specific to people." |
paul@96 | 29 | |
paul@213 | 30 | def _get_identity(self, from_organiser=True): |
paul@130 | 31 | |
paul@140 | 32 | """ |
paul@140 | 33 | Get the identity of interest in a usable form for any unprocessed |
paul@140 | 34 | object. |
paul@140 | 35 | """ |
paul@130 | 36 | |
paul@213 | 37 | identity, attr = item = uri_item(self.obj.get_item(from_organiser and "ORGANIZER" or "ATTENDEE")) |
paul@96 | 38 | |
paul@97 | 39 | # Check for event using UID. |
paul@97 | 40 | |
paul@213 | 41 | if not self.have_new_object(identity): |
paul@140 | 42 | return None |
paul@140 | 43 | |
paul@140 | 44 | return item |
paul@140 | 45 | |
paul@213 | 46 | def _record(self, from_organiser=True, update_freebusy=False): |
paul@140 | 47 | |
paul@140 | 48 | "Record free/busy and object information." |
paul@140 | 49 | |
paul@213 | 50 | item = self._get_identity(from_organiser) |
paul@140 | 51 | if not item: |
paul@99 | 52 | return False |
paul@97 | 53 | |
paul@140 | 54 | identity, attr = item |
paul@140 | 55 | |
paul@268 | 56 | # Update the object. |
paul@96 | 57 | |
paul@268 | 58 | if from_organiser: |
paul@268 | 59 | self.store.set_event(identity, self.uid, self.obj.to_node()) |
paul@268 | 60 | else: |
paul@268 | 61 | organiser_item, attendees = self.require_organiser_and_attendees(from_organiser) |
paul@268 | 62 | self.merge_attendance(attendees, identity) |
paul@96 | 63 | |
paul@160 | 64 | # Remove any associated request. |
paul@160 | 65 | |
paul@160 | 66 | self.store.dequeue_request(identity, self.uid) |
paul@160 | 67 | |
paul@96 | 68 | # Update free/busy information. |
paul@96 | 69 | |
paul@96 | 70 | if update_freebusy: |
paul@97 | 71 | |
paul@97 | 72 | # If newer than any old version, discard old details from the |
paul@97 | 73 | # free/busy record and check for suitability. |
paul@97 | 74 | |
paul@256 | 75 | periods = self.obj.get_periods() |
paul@173 | 76 | freebusy = self.store.get_freebusy(identity) |
paul@97 | 77 | |
paul@284 | 78 | if attr.get("PARTSTAT") != "DECLINED": |
paul@122 | 79 | self.update_freebusy(freebusy, identity, periods) |
paul@122 | 80 | else: |
paul@122 | 81 | self.remove_from_freebusy(freebusy, identity) |
paul@97 | 82 | |
paul@96 | 83 | if self.publisher: |
paul@96 | 84 | self.publisher.set_freebusy(identity, freebusy) |
paul@96 | 85 | |
paul@96 | 86 | return True |
paul@96 | 87 | |
paul@261 | 88 | def _remove(self, from_organiser=True, update_freebusy=False): |
paul@140 | 89 | |
paul@140 | 90 | "Remove free/busy information for any unprocessed object." |
paul@140 | 91 | |
paul@213 | 92 | item = self._get_identity(from_organiser) |
paul@140 | 93 | if not item: |
paul@140 | 94 | return False |
paul@140 | 95 | |
paul@140 | 96 | identity, attr = item |
paul@140 | 97 | |
paul@261 | 98 | # Store the object, updating any sequence information. |
paul@261 | 99 | |
paul@261 | 100 | self.store.set_event(identity, self.uid, self.obj.to_node()) |
paul@261 | 101 | |
paul@264 | 102 | # Cancel the event. |
paul@264 | 103 | |
paul@264 | 104 | self.store.cancel_event(identity, self.uid) |
paul@264 | 105 | |
paul@261 | 106 | # Remove any associated request. |
paul@261 | 107 | |
paul@261 | 108 | self.store.dequeue_request(identity, self.uid) |
paul@261 | 109 | |
paul@261 | 110 | # Update free/busy information. |
paul@261 | 111 | |
paul@261 | 112 | if update_freebusy: |
paul@261 | 113 | freebusy = self.store.get_freebusy(identity) |
paul@261 | 114 | self.remove_from_freebusy(freebusy, identity) |
paul@261 | 115 | |
paul@261 | 116 | if self.publisher: |
paul@261 | 117 | self.publisher.set_freebusy(identity, freebusy) |
paul@140 | 118 | |
paul@140 | 119 | return True |
paul@140 | 120 | |
paul@96 | 121 | class Event(PersonHandler): |
paul@96 | 122 | |
paul@96 | 123 | "An event handler." |
paul@96 | 124 | |
paul@96 | 125 | def add(self): |
paul@96 | 126 | pass |
paul@96 | 127 | |
paul@96 | 128 | def cancel(self): |
paul@261 | 129 | self._remove(True, True) |
paul@96 | 130 | |
paul@96 | 131 | def counter(self): |
paul@96 | 132 | pass |
paul@96 | 133 | |
paul@96 | 134 | def declinecounter(self): |
paul@96 | 135 | pass |
paul@96 | 136 | |
paul@96 | 137 | def publish(self): |
paul@213 | 138 | self._record(True, True) |
paul@96 | 139 | |
paul@96 | 140 | def refresh(self): |
paul@213 | 141 | self._record(True, True) |
paul@96 | 142 | |
paul@96 | 143 | def reply(self): |
paul@213 | 144 | self._record(False, True) |
paul@96 | 145 | |
paul@96 | 146 | def request(self): |
paul@213 | 147 | self._record(True, True) |
paul@96 | 148 | |
paul@96 | 149 | class Freebusy(PersonHandler): |
paul@96 | 150 | |
paul@96 | 151 | "A free/busy handler." |
paul@96 | 152 | |
paul@96 | 153 | def publish(self): |
paul@96 | 154 | pass |
paul@96 | 155 | |
paul@96 | 156 | def reply(self): |
paul@96 | 157 | pass |
paul@96 | 158 | |
paul@96 | 159 | def request(self): |
paul@96 | 160 | pass |
paul@96 | 161 | |
paul@96 | 162 | class Journal(PersonHandler): |
paul@96 | 163 | |
paul@96 | 164 | "A journal entry handler." |
paul@96 | 165 | |
paul@96 | 166 | def add(self): |
paul@96 | 167 | pass |
paul@96 | 168 | |
paul@96 | 169 | def cancel(self): |
paul@213 | 170 | self._remove(True) |
paul@96 | 171 | |
paul@96 | 172 | def publish(self): |
paul@213 | 173 | self._record(True) |
paul@96 | 174 | |
paul@96 | 175 | class Todo(PersonHandler): |
paul@96 | 176 | |
paul@96 | 177 | "A to-do item handler." |
paul@96 | 178 | |
paul@96 | 179 | def add(self): |
paul@96 | 180 | pass |
paul@96 | 181 | |
paul@96 | 182 | def cancel(self): |
paul@213 | 183 | self._remove(True) |
paul@96 | 184 | |
paul@96 | 185 | def counter(self): |
paul@96 | 186 | pass |
paul@96 | 187 | |
paul@96 | 188 | def declinecounter(self): |
paul@96 | 189 | pass |
paul@96 | 190 | |
paul@96 | 191 | def publish(self): |
paul@213 | 192 | self._record(True) |
paul@96 | 193 | |
paul@96 | 194 | def refresh(self): |
paul@213 | 195 | self._record(True) |
paul@96 | 196 | |
paul@96 | 197 | def reply(self): |
paul@213 | 198 | self._record(False) |
paul@96 | 199 | |
paul@96 | 200 | def request(self): |
paul@213 | 201 | self._record(True) |
paul@96 | 202 | |
paul@96 | 203 | # Handler registry. |
paul@96 | 204 | |
paul@96 | 205 | handlers = [ |
paul@96 | 206 | ("VFREEBUSY", Freebusy), |
paul@96 | 207 | ("VEVENT", Event), |
paul@96 | 208 | ("VTODO", Todo), |
paul@96 | 209 | ("VJOURNAL", Journal), |
paul@96 | 210 | ] |
paul@96 | 211 | |
paul@96 | 212 | # vim: tabstop=4 expandtab shiftwidth=4 |