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