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