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