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