paul@55 | 1 | #!/usr/bin/env python |
paul@55 | 2 | |
paul@55 | 3 | """ |
paul@55 | 4 | Handlers for a person for whom scheduling is performed. |
paul@55 | 5 | """ |
paul@55 | 6 | |
paul@60 | 7 | from email.mime.text import MIMEText |
paul@60 | 8 | from imiptools.content import Handler, to_part |
paul@55 | 9 | from vCalendar import to_node |
paul@55 | 10 | |
paul@67 | 11 | class PersonHandler(Handler): |
paul@55 | 12 | |
paul@67 | 13 | "Handling mechanisms specific to people." |
paul@55 | 14 | |
paul@61 | 15 | def _record_and_deliver(self, queue=False): |
paul@55 | 16 | |
paul@55 | 17 | oa = self.require_organiser_and_attendees() |
paul@55 | 18 | if not oa: |
paul@61 | 19 | return False |
paul@55 | 20 | |
paul@55 | 21 | (organiser, organiser_attr), attendees = oa |
paul@55 | 22 | |
paul@55 | 23 | # Process each attendee separately. |
paul@55 | 24 | |
paul@55 | 25 | for attendee, attendee_attr in attendees.items(): |
paul@55 | 26 | |
paul@55 | 27 | if not self.have_new_object(attendee, "VEVENT"): |
paul@55 | 28 | continue |
paul@55 | 29 | |
paul@61 | 30 | # Store the event and queue any request. |
paul@55 | 31 | |
paul@55 | 32 | self.store.set_event(attendee, self.uid, to_node( |
paul@55 | 33 | {"VEVENT" : [(self.details, {})]} |
paul@55 | 34 | )) |
paul@55 | 35 | |
paul@61 | 36 | if queue: |
paul@61 | 37 | self.store.queue_request(attendee, self.uid) |
paul@61 | 38 | |
paul@61 | 39 | return True |
paul@61 | 40 | |
paul@67 | 41 | class Event(PersonHandler): |
paul@67 | 42 | |
paul@67 | 43 | "An event handler." |
paul@67 | 44 | |
paul@63 | 45 | def add(self): |
paul@63 | 46 | |
paul@63 | 47 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 48 | |
paul@63 | 49 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 50 | |
paul@63 | 51 | return "ADD", MIMEText("An addition to an event has been received.") |
paul@63 | 52 | |
paul@63 | 53 | def cancel(self): |
paul@63 | 54 | |
paul@63 | 55 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 56 | |
paul@63 | 57 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 58 | |
paul@63 | 59 | return "CANCEL", MIMEText("A cancellation has been received.") |
paul@63 | 60 | |
paul@63 | 61 | def counter(self): |
paul@63 | 62 | |
paul@63 | 63 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 64 | |
paul@63 | 65 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 66 | |
paul@63 | 67 | return "COUNTER", MIMEText("A counter proposal has been received.") |
paul@63 | 68 | |
paul@63 | 69 | def declinecounter(self): |
paul@63 | 70 | |
paul@63 | 71 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 72 | |
paul@63 | 73 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 74 | |
paul@63 | 75 | return "DECLINECOUNTER", MIMEText("A declining counter proposal has been received.") |
paul@63 | 76 | |
paul@63 | 77 | def publish(self): |
paul@63 | 78 | |
paul@63 | 79 | # NOTE: Register details of any relevant event. |
paul@63 | 80 | |
paul@63 | 81 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 82 | |
paul@63 | 83 | return "PUBLISH", MIMEText("Details of an event have been received.") |
paul@63 | 84 | |
paul@63 | 85 | def refresh(self): |
paul@63 | 86 | |
paul@63 | 87 | # NOTE: Update details of any active event. |
paul@63 | 88 | |
paul@63 | 89 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 90 | |
paul@63 | 91 | return "REFRESH", MIMEText("An event update has been received.") |
paul@63 | 92 | |
paul@61 | 93 | def reply(self): |
paul@61 | 94 | |
paul@61 | 95 | "Record replies and notify the recipient." |
paul@61 | 96 | |
paul@61 | 97 | self._record_and_deliver(False) |
paul@61 | 98 | |
paul@61 | 99 | # The message is now wrapped and passed on to the recipient. |
paul@61 | 100 | |
paul@61 | 101 | return "REPLY", MIMEText("A reply has been received.") |
paul@61 | 102 | |
paul@61 | 103 | def request(self): |
paul@61 | 104 | |
paul@61 | 105 | "Hold requests and notify the recipient." |
paul@61 | 106 | |
paul@61 | 107 | self._record_and_deliver(True) |
paul@55 | 108 | |
paul@55 | 109 | # The message is now wrapped and passed on to the recipient. |
paul@55 | 110 | |
paul@60 | 111 | return "REQUEST", MIMEText("A request has been queued.") |
paul@60 | 112 | |
paul@67 | 113 | class Freebusy(PersonHandler): |
paul@55 | 114 | |
paul@55 | 115 | "A free/busy handler." |
paul@55 | 116 | |
paul@55 | 117 | def publish(self): |
paul@63 | 118 | |
paul@63 | 119 | # NOTE: Register free/busy information. |
paul@63 | 120 | |
paul@63 | 121 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 122 | |
paul@63 | 123 | return "PUBLISH", MIMEText("Details of a contact's availability have been received.") |
paul@55 | 124 | |
paul@55 | 125 | def reply(self): |
paul@55 | 126 | |
paul@63 | 127 | "Record replies and notify the recipient." |
paul@63 | 128 | |
paul@63 | 129 | self._record_and_deliver(False) |
paul@55 | 130 | |
paul@63 | 131 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 132 | |
paul@63 | 133 | return "REPLY", MIMEText("A reply has been received.") |
paul@55 | 134 | |
paul@55 | 135 | def request(self): |
paul@55 | 136 | |
paul@55 | 137 | """ |
paul@55 | 138 | Respond to a request by preparing a reply containing free/busy |
paul@55 | 139 | information for each indicated attendee. |
paul@55 | 140 | """ |
paul@55 | 141 | |
paul@55 | 142 | # NOTE: This is currently the same as the resource handler but should be |
paul@55 | 143 | # NOTE: subject to policy/preferences. |
paul@55 | 144 | |
paul@55 | 145 | oa = self.require_organiser_and_attendees() |
paul@55 | 146 | if not oa: |
paul@55 | 147 | return None |
paul@55 | 148 | |
paul@55 | 149 | (organiser, organiser_attr), attendees = oa |
paul@55 | 150 | |
paul@55 | 151 | # Construct an appropriate fragment. |
paul@55 | 152 | |
paul@55 | 153 | calendar = [] |
paul@55 | 154 | cwrite = calendar.append |
paul@55 | 155 | |
paul@55 | 156 | # Get the details for each attendee. |
paul@55 | 157 | |
paul@55 | 158 | for attendee, attendee_attr in attendees.items(): |
paul@55 | 159 | freebusy = self.store.get_freebusy(attendee) |
paul@55 | 160 | |
paul@60 | 161 | record = [] |
paul@60 | 162 | rwrite = record.append |
paul@55 | 163 | |
paul@60 | 164 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@60 | 165 | rwrite(("ATTENDEE", attendee_attr, attendee)) |
paul@60 | 166 | rwrite(("UID", {}, self.uid)) |
paul@55 | 167 | |
paul@60 | 168 | if freebusy: |
paul@55 | 169 | for start, end, uid in freebusy: |
paul@55 | 170 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end])) |
paul@55 | 171 | |
paul@60 | 172 | cwrite(("VFREEBUSY", {}, record)) |
paul@55 | 173 | |
paul@55 | 174 | # Return the reply. |
paul@55 | 175 | |
paul@60 | 176 | return "REPLY", to_part("REPLY", calendar) |
paul@55 | 177 | |
paul@67 | 178 | class Journal(PersonHandler): |
paul@55 | 179 | |
paul@55 | 180 | "A journal entry handler." |
paul@55 | 181 | |
paul@55 | 182 | def add(self): |
paul@63 | 183 | |
paul@63 | 184 | # NOTE: Queue a suggested modification to any active entry. |
paul@63 | 185 | |
paul@63 | 186 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 187 | |
paul@63 | 188 | return "ADD", MIMEText("An addition to a journal entry has been received.") |
paul@55 | 189 | |
paul@55 | 190 | def cancel(self): |
paul@63 | 191 | |
paul@63 | 192 | # NOTE: Queue a suggested modification to any active entry. |
paul@63 | 193 | |
paul@63 | 194 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 195 | |
paul@63 | 196 | return "CANCEL", MIMEText("A cancellation has been received.") |
paul@55 | 197 | |
paul@55 | 198 | def publish(self): |
paul@63 | 199 | |
paul@63 | 200 | # NOTE: Register details of any relevant entry. |
paul@63 | 201 | |
paul@63 | 202 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 203 | |
paul@63 | 204 | return "PUBLISH", MIMEText("Details of a journal entry have been received.") |
paul@55 | 205 | |
paul@67 | 206 | class Todo(PersonHandler): |
paul@55 | 207 | |
paul@55 | 208 | "A to-do item handler." |
paul@55 | 209 | |
paul@55 | 210 | def add(self): |
paul@63 | 211 | |
paul@63 | 212 | # NOTE: Queue a suggested modification to any active item. |
paul@63 | 213 | |
paul@63 | 214 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 215 | |
paul@63 | 216 | return "ADD", MIMEText("An addition to an item has been received.") |
paul@55 | 217 | |
paul@55 | 218 | def cancel(self): |
paul@63 | 219 | |
paul@63 | 220 | # NOTE: Queue a suggested modification to any active item. |
paul@63 | 221 | |
paul@63 | 222 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 223 | |
paul@63 | 224 | return "CANCEL", MIMEText("A cancellation has been received.") |
paul@55 | 225 | |
paul@55 | 226 | def counter(self): |
paul@55 | 227 | |
paul@63 | 228 | # NOTE: Queue a suggested modification to any active item. |
paul@55 | 229 | |
paul@63 | 230 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 231 | |
paul@63 | 232 | return "COUNTER", MIMEText("A counter proposal has been received.") |
paul@55 | 233 | |
paul@55 | 234 | def declinecounter(self): |
paul@55 | 235 | |
paul@63 | 236 | # NOTE: Queue a suggested modification to any active item. |
paul@55 | 237 | |
paul@63 | 238 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 239 | |
paul@63 | 240 | return "DECLINECOUNTER", MIMEText("A declining counter proposal has been received.") |
paul@55 | 241 | |
paul@55 | 242 | def publish(self): |
paul@63 | 243 | |
paul@63 | 244 | # NOTE: Register details of any relevant item. |
paul@63 | 245 | |
paul@63 | 246 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 247 | |
paul@63 | 248 | return "PUBLISH", MIMEText("Details of an item have been received.") |
paul@55 | 249 | |
paul@55 | 250 | def refresh(self): |
paul@63 | 251 | |
paul@63 | 252 | # NOTE: Update details of any active item. |
paul@63 | 253 | |
paul@63 | 254 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 255 | |
paul@63 | 256 | return "REFRESH", MIMEText("An item update has been received.") |
paul@55 | 257 | |
paul@55 | 258 | def reply(self): |
paul@55 | 259 | |
paul@63 | 260 | "Record replies and notify the recipient." |
paul@63 | 261 | |
paul@63 | 262 | self._record_and_deliver(False) |
paul@55 | 263 | |
paul@63 | 264 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 265 | |
paul@63 | 266 | return "REPLY", MIMEText("A reply has been received.") |
paul@55 | 267 | |
paul@55 | 268 | def request(self): |
paul@63 | 269 | |
paul@63 | 270 | "Hold requests and notify the recipient." |
paul@63 | 271 | |
paul@63 | 272 | self._record_and_deliver(True) |
paul@63 | 273 | |
paul@63 | 274 | # The message is now wrapped and passed on to the recipient. |
paul@63 | 275 | |
paul@63 | 276 | return "REQUEST", MIMEText("A request has been queued.") |
paul@55 | 277 | |
paul@55 | 278 | # Handler registry. |
paul@55 | 279 | |
paul@55 | 280 | handlers = [ |
paul@55 | 281 | ("VFREEBUSY", Freebusy), |
paul@55 | 282 | ("VEVENT", Event), |
paul@55 | 283 | ("VTODO", Todo), |
paul@55 | 284 | ("VJOURNAL", Journal), |
paul@55 | 285 | ] |
paul@55 | 286 | |
paul@55 | 287 | # vim: tabstop=4 expandtab shiftwidth=4 |