paul@48 | 1 | #!/usr/bin/env python |
paul@48 | 2 | |
paul@48 | 3 | """ |
paul@48 | 4 | Handlers for a resource. |
paul@48 | 5 | """ |
paul@48 | 6 | |
paul@89 | 7 | from imiptools.content import Handler, format_datetime, get_address, get_uri, to_part |
paul@108 | 8 | from imiptools.handlers.common import CommonFreebusy |
paul@48 | 9 | from vCalendar import to_node |
paul@48 | 10 | |
paul@131 | 11 | class ResourceHandler(Handler): |
paul@131 | 12 | |
paul@131 | 13 | "Handling mechanisms specific to resources." |
paul@131 | 14 | |
paul@131 | 15 | def _record_and_respond(self, handle_for_attendee): |
paul@131 | 16 | |
paul@131 | 17 | oa = self.require_organiser_and_attendees() |
paul@131 | 18 | if not oa: |
paul@131 | 19 | return None |
paul@131 | 20 | |
paul@131 | 21 | organiser_item, attendees = oa |
paul@131 | 22 | |
paul@131 | 23 | # Validate the organiser, ignoring spoofed requests. |
paul@131 | 24 | |
paul@131 | 25 | if not self.validate_identities([organiser_item]): |
paul@131 | 26 | return None |
paul@131 | 27 | |
paul@131 | 28 | # Process each attendee separately. |
paul@131 | 29 | |
paul@131 | 30 | calendar = [] |
paul@131 | 31 | |
paul@131 | 32 | for attendee, attendee_attr in attendees.items(): |
paul@131 | 33 | |
paul@131 | 34 | # Check for event using UID. |
paul@131 | 35 | |
paul@131 | 36 | if not self.have_new_object(attendee, "VEVENT"): |
paul@131 | 37 | continue |
paul@131 | 38 | |
paul@131 | 39 | # Collect response objects produced when handling the request. |
paul@131 | 40 | |
paul@131 | 41 | response = handle_for_attendee(attendee, attendee_attr) |
paul@131 | 42 | if response: |
paul@131 | 43 | calendar.append(response) |
paul@131 | 44 | |
paul@131 | 45 | return calendar |
paul@131 | 46 | |
paul@131 | 47 | def _schedule_for_attendee(self, attendee, attendee_attr): |
paul@131 | 48 | |
paul@131 | 49 | # If newer than any old version, discard old details from the |
paul@131 | 50 | # free/busy record and check for suitability. |
paul@131 | 51 | |
paul@131 | 52 | periods = self.get_periods() |
paul@131 | 53 | freebusy = self.store.get_freebusy(attendee) or [] |
paul@131 | 54 | scheduled = self.can_schedule(freebusy, periods) |
paul@131 | 55 | |
paul@131 | 56 | attendee_attr["PARTSTAT"] = scheduled and "ACCEPTED" or "DECLINED" |
paul@131 | 57 | if self.messenger and self.messenger.sender != get_address(attendee): |
paul@131 | 58 | attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@131 | 59 | |
paul@131 | 60 | # Make a version of the request with just this attendee. |
paul@131 | 61 | |
paul@131 | 62 | self.details["ATTENDEE"] = [(attendee, attendee_attr)] |
paul@131 | 63 | |
paul@131 | 64 | event = to_node({"VEVENT" : [(self.details, {})]}) |
paul@131 | 65 | self.store.set_event(attendee, self.uid, event) |
paul@131 | 66 | |
paul@131 | 67 | # Only update free/busy details if the event is scheduled. |
paul@131 | 68 | |
paul@131 | 69 | if scheduled: |
paul@131 | 70 | self.update_freebusy(freebusy, attendee, periods) |
paul@131 | 71 | else: |
paul@131 | 72 | self.remove_from_freebusy(freebusy, attendee) |
paul@131 | 73 | |
paul@131 | 74 | if self.publisher: |
paul@131 | 75 | self.publisher.set_freebusy(attendee, freebusy) |
paul@131 | 76 | |
paul@131 | 77 | return event |
paul@131 | 78 | |
paul@131 | 79 | def _cancel_for_attendee(self, attendee, attendee_attr): |
paul@131 | 80 | |
paul@131 | 81 | freebusy = self.store.get_freebusy(attendee) or [] |
paul@131 | 82 | self.remove_from_freebusy(freebusy, attendee) |
paul@131 | 83 | |
paul@131 | 84 | if self.publisher: |
paul@131 | 85 | self.publisher.set_freebusy(attendee, freebusy) |
paul@131 | 86 | |
paul@131 | 87 | return None |
paul@131 | 88 | |
paul@131 | 89 | class Event(ResourceHandler): |
paul@48 | 90 | |
paul@48 | 91 | "An event handler." |
paul@48 | 92 | |
paul@48 | 93 | def add(self): |
paul@48 | 94 | pass |
paul@48 | 95 | |
paul@48 | 96 | def cancel(self): |
paul@131 | 97 | |
paul@131 | 98 | "Cancel attendance for attendees." |
paul@131 | 99 | |
paul@131 | 100 | self._record_and_respond(self._cancel_for_attendee) |
paul@131 | 101 | return None |
paul@48 | 102 | |
paul@48 | 103 | def counter(self): |
paul@48 | 104 | |
paul@48 | 105 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 106 | |
paul@48 | 107 | pass |
paul@48 | 108 | |
paul@48 | 109 | def declinecounter(self): |
paul@48 | 110 | |
paul@48 | 111 | """ |
paul@48 | 112 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 113 | replies to such proposals. |
paul@48 | 114 | """ |
paul@48 | 115 | |
paul@48 | 116 | pass |
paul@48 | 117 | |
paul@48 | 118 | def publish(self): |
paul@48 | 119 | pass |
paul@48 | 120 | |
paul@48 | 121 | def refresh(self): |
paul@48 | 122 | pass |
paul@48 | 123 | |
paul@48 | 124 | def reply(self): |
paul@48 | 125 | |
paul@48 | 126 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 127 | |
paul@48 | 128 | pass |
paul@48 | 129 | |
paul@48 | 130 | def request(self): |
paul@48 | 131 | |
paul@48 | 132 | """ |
paul@48 | 133 | Respond to a request by preparing a reply containing accept/decline |
paul@48 | 134 | information for each indicated attendee. |
paul@48 | 135 | |
paul@48 | 136 | No support for countering requests is implemented. |
paul@48 | 137 | """ |
paul@48 | 138 | |
paul@131 | 139 | response = self._record_and_respond(self._schedule_for_attendee) |
paul@131 | 140 | if response: |
paul@131 | 141 | return "REPLY", to_part("REPLY", response) |
paul@131 | 142 | else: |
paul@94 | 143 | return None |
paul@48 | 144 | |
paul@131 | 145 | class Freebusy(ResourceHandler, CommonFreebusy): |
paul@48 | 146 | |
paul@48 | 147 | "A free/busy handler." |
paul@48 | 148 | |
paul@48 | 149 | def publish(self): |
paul@48 | 150 | pass |
paul@48 | 151 | |
paul@48 | 152 | def reply(self): |
paul@48 | 153 | |
paul@48 | 154 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 155 | |
paul@48 | 156 | pass |
paul@48 | 157 | |
paul@108 | 158 | # request provided by CommonFreeBusy.request |
paul@48 | 159 | |
paul@131 | 160 | class Journal(ResourceHandler): |
paul@48 | 161 | |
paul@48 | 162 | "A journal entry handler." |
paul@48 | 163 | |
paul@48 | 164 | def add(self): |
paul@48 | 165 | pass |
paul@48 | 166 | |
paul@48 | 167 | def cancel(self): |
paul@48 | 168 | pass |
paul@48 | 169 | |
paul@48 | 170 | def publish(self): |
paul@48 | 171 | pass |
paul@48 | 172 | |
paul@131 | 173 | class Todo(ResourceHandler): |
paul@48 | 174 | |
paul@48 | 175 | "A to-do item handler." |
paul@48 | 176 | |
paul@48 | 177 | def add(self): |
paul@48 | 178 | pass |
paul@48 | 179 | |
paul@48 | 180 | def cancel(self): |
paul@48 | 181 | pass |
paul@48 | 182 | |
paul@48 | 183 | def counter(self): |
paul@48 | 184 | |
paul@48 | 185 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 186 | |
paul@48 | 187 | pass |
paul@48 | 188 | |
paul@48 | 189 | def declinecounter(self): |
paul@48 | 190 | |
paul@48 | 191 | """ |
paul@48 | 192 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 193 | replies to such proposals. |
paul@48 | 194 | """ |
paul@48 | 195 | |
paul@48 | 196 | pass |
paul@48 | 197 | |
paul@48 | 198 | def publish(self): |
paul@48 | 199 | pass |
paul@48 | 200 | |
paul@48 | 201 | def refresh(self): |
paul@48 | 202 | pass |
paul@48 | 203 | |
paul@48 | 204 | def reply(self): |
paul@48 | 205 | |
paul@48 | 206 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 207 | |
paul@48 | 208 | pass |
paul@48 | 209 | |
paul@48 | 210 | def request(self): |
paul@48 | 211 | pass |
paul@48 | 212 | |
paul@48 | 213 | # Handler registry. |
paul@48 | 214 | |
paul@48 | 215 | handlers = [ |
paul@48 | 216 | ("VFREEBUSY", Freebusy), |
paul@48 | 217 | ("VEVENT", Event), |
paul@48 | 218 | ("VTODO", Todo), |
paul@48 | 219 | ("VJOURNAL", Journal), |
paul@48 | 220 | ] |
paul@48 | 221 | |
paul@48 | 222 | # vim: tabstop=4 expandtab shiftwidth=4 |