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