paul@48 | 1 | #!/usr/bin/env python |
paul@48 | 2 | |
paul@48 | 3 | """ |
paul@48 | 4 | Handlers for a resource. |
paul@48 | 5 | """ |
paul@48 | 6 | |
paul@60 | 7 | from imiptools.content import Handler, format_datetime, to_part |
paul@48 | 8 | from vCalendar import to_node |
paul@48 | 9 | |
paul@48 | 10 | class Event(Handler): |
paul@48 | 11 | |
paul@48 | 12 | "An event handler." |
paul@48 | 13 | |
paul@48 | 14 | def add(self): |
paul@48 | 15 | pass |
paul@48 | 16 | |
paul@48 | 17 | def cancel(self): |
paul@48 | 18 | pass |
paul@48 | 19 | |
paul@48 | 20 | def counter(self): |
paul@48 | 21 | |
paul@48 | 22 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 23 | |
paul@48 | 24 | pass |
paul@48 | 25 | |
paul@48 | 26 | def declinecounter(self): |
paul@48 | 27 | |
paul@48 | 28 | """ |
paul@48 | 29 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 30 | replies to such proposals. |
paul@48 | 31 | """ |
paul@48 | 32 | |
paul@48 | 33 | pass |
paul@48 | 34 | |
paul@48 | 35 | def publish(self): |
paul@48 | 36 | pass |
paul@48 | 37 | |
paul@48 | 38 | def refresh(self): |
paul@48 | 39 | pass |
paul@48 | 40 | |
paul@48 | 41 | def reply(self): |
paul@48 | 42 | |
paul@48 | 43 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 44 | |
paul@48 | 45 | pass |
paul@48 | 46 | |
paul@48 | 47 | def request(self): |
paul@48 | 48 | |
paul@48 | 49 | """ |
paul@48 | 50 | Respond to a request by preparing a reply containing accept/decline |
paul@48 | 51 | information for each indicated attendee. |
paul@48 | 52 | |
paul@48 | 53 | No support for countering requests is implemented. |
paul@48 | 54 | """ |
paul@48 | 55 | |
paul@48 | 56 | oa = self.require_organiser_and_attendees() |
paul@48 | 57 | if not oa: |
paul@48 | 58 | return None |
paul@48 | 59 | |
paul@48 | 60 | (organiser, organiser_attr), attendees = oa |
paul@48 | 61 | |
paul@48 | 62 | # Process each attendee separately. |
paul@48 | 63 | |
paul@48 | 64 | calendar = [] |
paul@48 | 65 | |
paul@48 | 66 | for attendee, attendee_attr in attendees.items(): |
paul@48 | 67 | |
paul@48 | 68 | # Check for event using UID. |
paul@48 | 69 | |
paul@51 | 70 | if not self.have_new_object(attendee, "VEVENT"): |
paul@51 | 71 | continue |
paul@48 | 72 | |
paul@48 | 73 | # If newer than any old version, discard old details from the |
paul@48 | 74 | # free/busy record and check for suitability. |
paul@48 | 75 | |
paul@75 | 76 | periods = self.get_periods() |
paul@77 | 77 | freebusy = self.store.get_freebusy(attendee) or [] |
paul@77 | 78 | scheduled = self.can_schedule(freebusy, periods) |
paul@48 | 79 | |
paul@76 | 80 | attendee_attr["PARTSTAT"] = scheduled and "ACCEPTED" or "DECLINED" |
paul@76 | 81 | self.details["ATTENDEE"] = [(attendee, attendee_attr)] |
paul@48 | 82 | |
paul@77 | 83 | event = to_node({"VEVENT" : [(self.details, {})]}) |
paul@77 | 84 | calendar.append(event) |
paul@77 | 85 | self.store.set_event(attendee, self.uid, event) |
paul@77 | 86 | |
paul@77 | 87 | # Only update free/busy details if the event is scheduled. |
paul@48 | 88 | |
paul@76 | 89 | if scheduled: |
paul@77 | 90 | self.update_freebusy(freebusy, attendee, periods) |
paul@76 | 91 | if self.publisher: |
paul@76 | 92 | self.publisher.set_freebusy(attendee, freebusy) |
paul@48 | 93 | |
paul@60 | 94 | return "REPLY", to_part("REPLY", calendar) |
paul@48 | 95 | |
paul@48 | 96 | class Freebusy(Handler): |
paul@48 | 97 | |
paul@48 | 98 | "A free/busy handler." |
paul@48 | 99 | |
paul@48 | 100 | def publish(self): |
paul@48 | 101 | pass |
paul@48 | 102 | |
paul@48 | 103 | def reply(self): |
paul@48 | 104 | |
paul@48 | 105 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 106 | |
paul@48 | 107 | pass |
paul@48 | 108 | |
paul@48 | 109 | def request(self): |
paul@48 | 110 | |
paul@48 | 111 | """ |
paul@48 | 112 | Respond to a request by preparing a reply containing free/busy |
paul@48 | 113 | information for each indicated attendee. |
paul@48 | 114 | """ |
paul@48 | 115 | |
paul@48 | 116 | oa = self.require_organiser_and_attendees() |
paul@48 | 117 | if not oa: |
paul@48 | 118 | return None |
paul@48 | 119 | |
paul@48 | 120 | (organiser, organiser_attr), attendees = oa |
paul@48 | 121 | |
paul@48 | 122 | # Construct an appropriate fragment. |
paul@48 | 123 | |
paul@48 | 124 | calendar = [] |
paul@48 | 125 | cwrite = calendar.append |
paul@48 | 126 | |
paul@48 | 127 | # Get the details for each attendee. |
paul@48 | 128 | |
paul@48 | 129 | for attendee, attendee_attr in attendees.items(): |
paul@48 | 130 | freebusy = self.store.get_freebusy(attendee) |
paul@48 | 131 | |
paul@60 | 132 | record = [] |
paul@60 | 133 | rwrite = record.append |
paul@48 | 134 | |
paul@60 | 135 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@60 | 136 | rwrite(("ATTENDEE", attendee_attr, attendee)) |
paul@60 | 137 | rwrite(("UID", {}, self.uid)) |
paul@48 | 138 | |
paul@60 | 139 | if freebusy: |
paul@48 | 140 | for start, end, uid in freebusy: |
paul@48 | 141 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end])) |
paul@48 | 142 | |
paul@60 | 143 | cwrite(("VFREEBUSY", {}, record)) |
paul@48 | 144 | |
paul@48 | 145 | # Return the reply. |
paul@48 | 146 | |
paul@60 | 147 | return "REPLY", to_part("REPLY", calendar) |
paul@48 | 148 | |
paul@48 | 149 | class Journal(Handler): |
paul@48 | 150 | |
paul@48 | 151 | "A journal entry handler." |
paul@48 | 152 | |
paul@48 | 153 | def add(self): |
paul@48 | 154 | pass |
paul@48 | 155 | |
paul@48 | 156 | def cancel(self): |
paul@48 | 157 | pass |
paul@48 | 158 | |
paul@48 | 159 | def publish(self): |
paul@48 | 160 | pass |
paul@48 | 161 | |
paul@48 | 162 | class Todo(Handler): |
paul@48 | 163 | |
paul@48 | 164 | "A to-do item handler." |
paul@48 | 165 | |
paul@48 | 166 | def add(self): |
paul@48 | 167 | pass |
paul@48 | 168 | |
paul@48 | 169 | def cancel(self): |
paul@48 | 170 | pass |
paul@48 | 171 | |
paul@48 | 172 | def counter(self): |
paul@48 | 173 | |
paul@48 | 174 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 175 | |
paul@48 | 176 | pass |
paul@48 | 177 | |
paul@48 | 178 | def declinecounter(self): |
paul@48 | 179 | |
paul@48 | 180 | """ |
paul@48 | 181 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 182 | replies to such proposals. |
paul@48 | 183 | """ |
paul@48 | 184 | |
paul@48 | 185 | pass |
paul@48 | 186 | |
paul@48 | 187 | def publish(self): |
paul@48 | 188 | pass |
paul@48 | 189 | |
paul@48 | 190 | def refresh(self): |
paul@48 | 191 | pass |
paul@48 | 192 | |
paul@48 | 193 | def reply(self): |
paul@48 | 194 | |
paul@48 | 195 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 196 | |
paul@48 | 197 | pass |
paul@48 | 198 | |
paul@48 | 199 | def request(self): |
paul@48 | 200 | pass |
paul@48 | 201 | |
paul@48 | 202 | # Handler registry. |
paul@48 | 203 | |
paul@48 | 204 | handlers = [ |
paul@48 | 205 | ("VFREEBUSY", Freebusy), |
paul@48 | 206 | ("VEVENT", Event), |
paul@48 | 207 | ("VTODO", Todo), |
paul@48 | 208 | ("VJOURNAL", Journal), |
paul@48 | 209 | ] |
paul@48 | 210 | |
paul@48 | 211 | # vim: tabstop=4 expandtab shiftwidth=4 |