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@55 | 15 | def add(self): |
paul@55 | 16 | pass |
paul@55 | 17 | |
paul@55 | 18 | def cancel(self): |
paul@55 | 19 | pass |
paul@55 | 20 | |
paul@55 | 21 | def counter(self): |
paul@55 | 22 | |
paul@55 | 23 | "Since this handler does not send requests, it will not handle replies." |
paul@55 | 24 | |
paul@55 | 25 | pass |
paul@55 | 26 | |
paul@55 | 27 | def declinecounter(self): |
paul@55 | 28 | |
paul@55 | 29 | """ |
paul@55 | 30 | Since this handler does not send counter proposals, it will not handle |
paul@55 | 31 | replies to such proposals. |
paul@55 | 32 | """ |
paul@55 | 33 | |
paul@55 | 34 | pass |
paul@55 | 35 | |
paul@55 | 36 | def publish(self): |
paul@55 | 37 | pass |
paul@55 | 38 | |
paul@55 | 39 | def refresh(self): |
paul@55 | 40 | pass |
paul@55 | 41 | |
paul@61 | 42 | def _record_and_deliver(self, queue=False): |
paul@55 | 43 | |
paul@55 | 44 | oa = self.require_organiser_and_attendees() |
paul@55 | 45 | if not oa: |
paul@61 | 46 | return False |
paul@55 | 47 | |
paul@55 | 48 | (organiser, organiser_attr), attendees = oa |
paul@55 | 49 | |
paul@55 | 50 | # Process each attendee separately. |
paul@55 | 51 | |
paul@55 | 52 | for attendee, attendee_attr in attendees.items(): |
paul@55 | 53 | |
paul@55 | 54 | if not self.have_new_object(attendee, "VEVENT"): |
paul@55 | 55 | continue |
paul@55 | 56 | |
paul@61 | 57 | # Store the event and queue any request. |
paul@55 | 58 | |
paul@55 | 59 | self.store.set_event(attendee, self.uid, to_node( |
paul@55 | 60 | {"VEVENT" : [(self.details, {})]} |
paul@55 | 61 | )) |
paul@55 | 62 | |
paul@61 | 63 | if queue: |
paul@61 | 64 | self.store.queue_request(attendee, self.uid) |
paul@61 | 65 | |
paul@61 | 66 | return True |
paul@61 | 67 | |
paul@61 | 68 | def reply(self): |
paul@61 | 69 | |
paul@61 | 70 | "Record replies and notify the recipient." |
paul@61 | 71 | |
paul@61 | 72 | self._record_and_deliver(False) |
paul@61 | 73 | |
paul@61 | 74 | # The message is now wrapped and passed on to the recipient. |
paul@61 | 75 | |
paul@61 | 76 | return "REPLY", MIMEText("A reply has been received.") |
paul@61 | 77 | |
paul@61 | 78 | def request(self): |
paul@61 | 79 | |
paul@61 | 80 | "Hold requests and notify the recipient." |
paul@61 | 81 | |
paul@61 | 82 | self._record_and_deliver(True) |
paul@55 | 83 | |
paul@55 | 84 | # The message is now wrapped and passed on to the recipient. |
paul@55 | 85 | |
paul@60 | 86 | return "REQUEST", MIMEText("A request has been queued.") |
paul@60 | 87 | |
paul@55 | 88 | class Freebusy(Handler): |
paul@55 | 89 | |
paul@55 | 90 | "A free/busy handler." |
paul@55 | 91 | |
paul@55 | 92 | def publish(self): |
paul@55 | 93 | pass |
paul@55 | 94 | |
paul@55 | 95 | def reply(self): |
paul@55 | 96 | |
paul@55 | 97 | "Since this handler does not send requests, it will not handle replies." |
paul@55 | 98 | |
paul@55 | 99 | pass |
paul@55 | 100 | |
paul@55 | 101 | def request(self): |
paul@55 | 102 | |
paul@55 | 103 | """ |
paul@55 | 104 | Respond to a request by preparing a reply containing free/busy |
paul@55 | 105 | information for each indicated attendee. |
paul@55 | 106 | """ |
paul@55 | 107 | |
paul@55 | 108 | # NOTE: This is currently the same as the resource handler but should be |
paul@55 | 109 | # NOTE: subject to policy/preferences. |
paul@55 | 110 | |
paul@55 | 111 | oa = self.require_organiser_and_attendees() |
paul@55 | 112 | if not oa: |
paul@55 | 113 | return None |
paul@55 | 114 | |
paul@55 | 115 | (organiser, organiser_attr), attendees = oa |
paul@55 | 116 | |
paul@55 | 117 | # Construct an appropriate fragment. |
paul@55 | 118 | |
paul@55 | 119 | calendar = [] |
paul@55 | 120 | cwrite = calendar.append |
paul@55 | 121 | |
paul@55 | 122 | # Get the details for each attendee. |
paul@55 | 123 | |
paul@55 | 124 | for attendee, attendee_attr in attendees.items(): |
paul@55 | 125 | freebusy = self.store.get_freebusy(attendee) |
paul@55 | 126 | |
paul@60 | 127 | record = [] |
paul@60 | 128 | rwrite = record.append |
paul@55 | 129 | |
paul@60 | 130 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@60 | 131 | rwrite(("ATTENDEE", attendee_attr, attendee)) |
paul@60 | 132 | rwrite(("UID", {}, self.uid)) |
paul@55 | 133 | |
paul@60 | 134 | if freebusy: |
paul@55 | 135 | for start, end, uid in freebusy: |
paul@55 | 136 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end])) |
paul@55 | 137 | |
paul@60 | 138 | cwrite(("VFREEBUSY", {}, record)) |
paul@55 | 139 | |
paul@55 | 140 | # Return the reply. |
paul@55 | 141 | |
paul@60 | 142 | return "REPLY", to_part("REPLY", calendar) |
paul@55 | 143 | |
paul@55 | 144 | class Journal(Handler): |
paul@55 | 145 | |
paul@55 | 146 | "A journal entry handler." |
paul@55 | 147 | |
paul@55 | 148 | def add(self): |
paul@55 | 149 | pass |
paul@55 | 150 | |
paul@55 | 151 | def cancel(self): |
paul@55 | 152 | pass |
paul@55 | 153 | |
paul@55 | 154 | def publish(self): |
paul@55 | 155 | pass |
paul@55 | 156 | |
paul@55 | 157 | class Todo(Handler): |
paul@55 | 158 | |
paul@55 | 159 | "A to-do item handler." |
paul@55 | 160 | |
paul@55 | 161 | def add(self): |
paul@55 | 162 | pass |
paul@55 | 163 | |
paul@55 | 164 | def cancel(self): |
paul@55 | 165 | pass |
paul@55 | 166 | |
paul@55 | 167 | def counter(self): |
paul@55 | 168 | |
paul@55 | 169 | "Since this handler does not send requests, it will not handle replies." |
paul@55 | 170 | |
paul@55 | 171 | pass |
paul@55 | 172 | |
paul@55 | 173 | def declinecounter(self): |
paul@55 | 174 | |
paul@55 | 175 | """ |
paul@55 | 176 | Since this handler does not send counter proposals, it will not handle |
paul@55 | 177 | replies to such proposals. |
paul@55 | 178 | """ |
paul@55 | 179 | |
paul@55 | 180 | pass |
paul@55 | 181 | |
paul@55 | 182 | def publish(self): |
paul@55 | 183 | pass |
paul@55 | 184 | |
paul@55 | 185 | def refresh(self): |
paul@55 | 186 | pass |
paul@55 | 187 | |
paul@55 | 188 | def reply(self): |
paul@55 | 189 | |
paul@55 | 190 | "Since this handler does not send requests, it will not handle replies." |
paul@55 | 191 | |
paul@55 | 192 | pass |
paul@55 | 193 | |
paul@55 | 194 | def request(self): |
paul@55 | 195 | pass |
paul@55 | 196 | |
paul@55 | 197 | # Handler registry. |
paul@55 | 198 | |
paul@55 | 199 | handlers = [ |
paul@55 | 200 | ("VFREEBUSY", Freebusy), |
paul@55 | 201 | ("VEVENT", Event), |
paul@55 | 202 | ("VTODO", Todo), |
paul@55 | 203 | ("VJOURNAL", Journal), |
paul@55 | 204 | ] |
paul@55 | 205 | |
paul@55 | 206 | # vim: tabstop=4 expandtab shiftwidth=4 |