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@129 | 23 | from imiptools.content import Handler, uri_item |
paul@96 | 24 | from vCalendar import to_node |
paul@96 | 25 | |
paul@96 | 26 | class PersonHandler(Handler): |
paul@96 | 27 | |
paul@96 | 28 | "Handling mechanisms specific to people." |
paul@96 | 29 | |
paul@140 | 30 | def _get_identity(self, objtype, 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@140 | 37 | identity, attr = item = uri_item(self.get_item(from_organiser and "ORGANIZER" or "ATTENDEE")) |
paul@96 | 38 | |
paul@97 | 39 | # Check for event using UID. |
paul@97 | 40 | |
paul@97 | 41 | if not self.have_new_object(identity, objtype): |
paul@140 | 42 | return None |
paul@140 | 43 | |
paul@140 | 44 | return item |
paul@140 | 45 | |
paul@140 | 46 | def _record(self, objtype, from_organiser=True, update_freebusy=False): |
paul@140 | 47 | |
paul@140 | 48 | "Record free/busy and object information." |
paul@140 | 49 | |
paul@140 | 50 | item = self._get_identity(objtype, 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@96 | 58 | self.store.set_event(identity, self.uid, to_node( |
paul@96 | 59 | {objtype : [(self.details, {})]} |
paul@96 | 60 | )) |
paul@96 | 61 | |
paul@160 | 62 | # Remove any associated request. |
paul@160 | 63 | |
paul@160 | 64 | self.store.dequeue_request(identity, self.uid) |
paul@160 | 65 | |
paul@96 | 66 | # Update free/busy information. |
paul@96 | 67 | |
paul@96 | 68 | if update_freebusy: |
paul@97 | 69 | |
paul@97 | 70 | # If newer than any old version, discard old details from the |
paul@97 | 71 | # free/busy record and check for suitability. |
paul@97 | 72 | |
paul@96 | 73 | periods = self.get_periods() |
paul@96 | 74 | freebusy = self.store.get_freebusy(identity) or [] |
paul@97 | 75 | |
paul@126 | 76 | if from_organiser or attr.get("PARTSTAT") != "DECLINED": |
paul@122 | 77 | self.update_freebusy(freebusy, identity, periods) |
paul@122 | 78 | else: |
paul@122 | 79 | self.remove_from_freebusy(freebusy, identity) |
paul@97 | 80 | |
paul@96 | 81 | if self.publisher: |
paul@96 | 82 | self.publisher.set_freebusy(identity, freebusy) |
paul@96 | 83 | |
paul@96 | 84 | return True |
paul@96 | 85 | |
paul@140 | 86 | def _remove(self, objtype, from_organiser=True): |
paul@140 | 87 | |
paul@140 | 88 | "Remove free/busy information for any unprocessed object." |
paul@140 | 89 | |
paul@140 | 90 | item = self._get_identity(objtype, from_organiser) |
paul@140 | 91 | if not item: |
paul@140 | 92 | return False |
paul@140 | 93 | |
paul@140 | 94 | identity, attr = item |
paul@140 | 95 | |
paul@140 | 96 | freebusy = self.store.get_freebusy(identity) or [] |
paul@140 | 97 | self.remove_from_freebusy(freebusy, identity) |
paul@140 | 98 | |
paul@140 | 99 | return True |
paul@140 | 100 | |
paul@96 | 101 | class Event(PersonHandler): |
paul@96 | 102 | |
paul@96 | 103 | "An event handler." |
paul@96 | 104 | |
paul@96 | 105 | def add(self): |
paul@96 | 106 | pass |
paul@96 | 107 | |
paul@96 | 108 | def cancel(self): |
paul@140 | 109 | self._remove("VEVENT", True) |
paul@96 | 110 | |
paul@96 | 111 | def counter(self): |
paul@96 | 112 | pass |
paul@96 | 113 | |
paul@96 | 114 | def declinecounter(self): |
paul@96 | 115 | pass |
paul@96 | 116 | |
paul@96 | 117 | def publish(self): |
paul@96 | 118 | self._record("VEVENT", True, True) |
paul@96 | 119 | |
paul@96 | 120 | def refresh(self): |
paul@96 | 121 | self._record("VEVENT", True, True) |
paul@96 | 122 | |
paul@96 | 123 | def reply(self): |
paul@99 | 124 | self._record("VEVENT", False, True) |
paul@96 | 125 | |
paul@96 | 126 | def request(self): |
paul@96 | 127 | self._record("VEVENT", True, True) |
paul@96 | 128 | |
paul@96 | 129 | class Freebusy(PersonHandler): |
paul@96 | 130 | |
paul@96 | 131 | "A free/busy handler." |
paul@96 | 132 | |
paul@96 | 133 | def publish(self): |
paul@96 | 134 | pass |
paul@96 | 135 | |
paul@96 | 136 | def reply(self): |
paul@96 | 137 | pass |
paul@96 | 138 | |
paul@96 | 139 | def request(self): |
paul@96 | 140 | pass |
paul@96 | 141 | |
paul@96 | 142 | class Journal(PersonHandler): |
paul@96 | 143 | |
paul@96 | 144 | "A journal entry handler." |
paul@96 | 145 | |
paul@96 | 146 | def add(self): |
paul@96 | 147 | pass |
paul@96 | 148 | |
paul@96 | 149 | def cancel(self): |
paul@140 | 150 | self._remove("VJOURNAL", True) |
paul@96 | 151 | |
paul@96 | 152 | def publish(self): |
paul@96 | 153 | self._record("VJOURNAL", True) |
paul@96 | 154 | |
paul@96 | 155 | class Todo(PersonHandler): |
paul@96 | 156 | |
paul@96 | 157 | "A to-do item handler." |
paul@96 | 158 | |
paul@96 | 159 | def add(self): |
paul@96 | 160 | pass |
paul@96 | 161 | |
paul@96 | 162 | def cancel(self): |
paul@140 | 163 | self._remove("VTODO", True) |
paul@96 | 164 | |
paul@96 | 165 | def counter(self): |
paul@96 | 166 | pass |
paul@96 | 167 | |
paul@96 | 168 | def declinecounter(self): |
paul@96 | 169 | pass |
paul@96 | 170 | |
paul@96 | 171 | def publish(self): |
paul@96 | 172 | self._record("VTODO", True) |
paul@96 | 173 | |
paul@96 | 174 | def refresh(self): |
paul@96 | 175 | self._record("VTODO", True) |
paul@96 | 176 | |
paul@96 | 177 | def reply(self): |
paul@96 | 178 | self._record("VTODO", False) |
paul@96 | 179 | |
paul@96 | 180 | def request(self): |
paul@96 | 181 | self._record("VTODO", True) |
paul@96 | 182 | |
paul@96 | 183 | # Handler registry. |
paul@96 | 184 | |
paul@96 | 185 | handlers = [ |
paul@96 | 186 | ("VFREEBUSY", Freebusy), |
paul@96 | 187 | ("VEVENT", Event), |
paul@96 | 188 | ("VTODO", Todo), |
paul@96 | 189 | ("VJOURNAL", Journal), |
paul@96 | 190 | ] |
paul@96 | 191 | |
paul@96 | 192 | # vim: tabstop=4 expandtab shiftwidth=4 |