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@96 | 56 | # Store the object. |
paul@96 | 57 | |
paul@213 | 58 | self.store.set_event(identity, self.uid, self.obj.to_node()) |
paul@96 | 59 | |
paul@160 | 60 | # Remove any associated request. |
paul@160 | 61 | |
paul@160 | 62 | self.store.dequeue_request(identity, self.uid) |
paul@160 | 63 | |
paul@96 | 64 | # Update free/busy information. |
paul@96 | 65 | |
paul@96 | 66 | if update_freebusy: |
paul@97 | 67 | |
paul@97 | 68 | # If newer than any old version, discard old details from the |
paul@97 | 69 | # free/busy record and check for suitability. |
paul@97 | 70 | |
paul@256 | 71 | periods = self.obj.get_periods() |
paul@173 | 72 | freebusy = self.store.get_freebusy(identity) |
paul@97 | 73 | |
paul@126 | 74 | if from_organiser or attr.get("PARTSTAT") != "DECLINED": |
paul@122 | 75 | self.update_freebusy(freebusy, identity, periods) |
paul@122 | 76 | else: |
paul@122 | 77 | self.remove_from_freebusy(freebusy, identity) |
paul@97 | 78 | |
paul@96 | 79 | if self.publisher: |
paul@96 | 80 | self.publisher.set_freebusy(identity, freebusy) |
paul@96 | 81 | |
paul@96 | 82 | return True |
paul@96 | 83 | |
paul@261 | 84 | def _remove(self, from_organiser=True, update_freebusy=False): |
paul@140 | 85 | |
paul@140 | 86 | "Remove free/busy information for any unprocessed object." |
paul@140 | 87 | |
paul@213 | 88 | item = self._get_identity(from_organiser) |
paul@140 | 89 | if not item: |
paul@140 | 90 | return False |
paul@140 | 91 | |
paul@140 | 92 | identity, attr = item |
paul@140 | 93 | |
paul@261 | 94 | # Store the object, updating any sequence information. |
paul@261 | 95 | |
paul@261 | 96 | self.store.set_event(identity, self.uid, self.obj.to_node()) |
paul@261 | 97 | |
paul@264 | 98 | # Cancel the event. |
paul@264 | 99 | |
paul@264 | 100 | self.store.cancel_event(identity, self.uid) |
paul@264 | 101 | |
paul@261 | 102 | # Remove any associated request. |
paul@261 | 103 | |
paul@261 | 104 | self.store.dequeue_request(identity, self.uid) |
paul@261 | 105 | |
paul@261 | 106 | # Update free/busy information. |
paul@261 | 107 | |
paul@261 | 108 | if update_freebusy: |
paul@261 | 109 | freebusy = self.store.get_freebusy(identity) |
paul@261 | 110 | self.remove_from_freebusy(freebusy, identity) |
paul@261 | 111 | |
paul@261 | 112 | if self.publisher: |
paul@261 | 113 | self.publisher.set_freebusy(identity, freebusy) |
paul@140 | 114 | |
paul@140 | 115 | return True |
paul@140 | 116 | |
paul@96 | 117 | class Event(PersonHandler): |
paul@96 | 118 | |
paul@96 | 119 | "An event handler." |
paul@96 | 120 | |
paul@96 | 121 | def add(self): |
paul@96 | 122 | pass |
paul@96 | 123 | |
paul@96 | 124 | def cancel(self): |
paul@261 | 125 | self._remove(True, True) |
paul@96 | 126 | |
paul@96 | 127 | def counter(self): |
paul@96 | 128 | pass |
paul@96 | 129 | |
paul@96 | 130 | def declinecounter(self): |
paul@96 | 131 | pass |
paul@96 | 132 | |
paul@96 | 133 | def publish(self): |
paul@213 | 134 | self._record(True, True) |
paul@96 | 135 | |
paul@96 | 136 | def refresh(self): |
paul@213 | 137 | self._record(True, True) |
paul@96 | 138 | |
paul@96 | 139 | def reply(self): |
paul@213 | 140 | self._record(False, True) |
paul@96 | 141 | |
paul@96 | 142 | def request(self): |
paul@213 | 143 | self._record(True, True) |
paul@96 | 144 | |
paul@96 | 145 | class Freebusy(PersonHandler): |
paul@96 | 146 | |
paul@96 | 147 | "A free/busy handler." |
paul@96 | 148 | |
paul@96 | 149 | def publish(self): |
paul@96 | 150 | pass |
paul@96 | 151 | |
paul@96 | 152 | def reply(self): |
paul@96 | 153 | pass |
paul@96 | 154 | |
paul@96 | 155 | def request(self): |
paul@96 | 156 | pass |
paul@96 | 157 | |
paul@96 | 158 | class Journal(PersonHandler): |
paul@96 | 159 | |
paul@96 | 160 | "A journal entry handler." |
paul@96 | 161 | |
paul@96 | 162 | def add(self): |
paul@96 | 163 | pass |
paul@96 | 164 | |
paul@96 | 165 | def cancel(self): |
paul@213 | 166 | self._remove(True) |
paul@96 | 167 | |
paul@96 | 168 | def publish(self): |
paul@213 | 169 | self._record(True) |
paul@96 | 170 | |
paul@96 | 171 | class Todo(PersonHandler): |
paul@96 | 172 | |
paul@96 | 173 | "A to-do item handler." |
paul@96 | 174 | |
paul@96 | 175 | def add(self): |
paul@96 | 176 | pass |
paul@96 | 177 | |
paul@96 | 178 | def cancel(self): |
paul@213 | 179 | self._remove(True) |
paul@96 | 180 | |
paul@96 | 181 | def counter(self): |
paul@96 | 182 | pass |
paul@96 | 183 | |
paul@96 | 184 | def declinecounter(self): |
paul@96 | 185 | pass |
paul@96 | 186 | |
paul@96 | 187 | def publish(self): |
paul@213 | 188 | self._record(True) |
paul@96 | 189 | |
paul@96 | 190 | def refresh(self): |
paul@213 | 191 | self._record(True) |
paul@96 | 192 | |
paul@96 | 193 | def reply(self): |
paul@213 | 194 | self._record(False) |
paul@96 | 195 | |
paul@96 | 196 | def request(self): |
paul@213 | 197 | self._record(True) |
paul@96 | 198 | |
paul@96 | 199 | # Handler registry. |
paul@96 | 200 | |
paul@96 | 201 | handlers = [ |
paul@96 | 202 | ("VFREEBUSY", Freebusy), |
paul@96 | 203 | ("VEVENT", Event), |
paul@96 | 204 | ("VTODO", Todo), |
paul@96 | 205 | ("VJOURNAL", Journal), |
paul@96 | 206 | ] |
paul@96 | 207 | |
paul@96 | 208 | # vim: tabstop=4 expandtab shiftwidth=4 |