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@76 | 77 | freebusy = self.update_freebusy(attendee, periods) |
paul@76 | 78 | scheduled = freebusy is not None |
paul@48 | 79 | |
paul@76 | 80 | attendee_attr["PARTSTAT"] = scheduled and "ACCEPTED" or "DECLINED" |
paul@48 | 81 | |
paul@76 | 82 | self.details["ATTENDEE"] = [(attendee, attendee_attr)] |
paul@48 | 83 | |
paul@76 | 84 | calendar.append(to_node( |
paul@76 | 85 | {"VEVENT" : [(self.details, {})]} |
paul@76 | 86 | )) |
paul@48 | 87 | |
paul@76 | 88 | if scheduled: |
paul@48 | 89 | self.store.set_event(attendee, self.uid, to_node( |
paul@48 | 90 | {"VEVENT" : [(self.details, {})]} |
paul@48 | 91 | )) |
paul@76 | 92 | if self.publisher: |
paul@76 | 93 | self.publisher.set_freebusy(attendee, freebusy) |
paul@48 | 94 | |
paul@60 | 95 | return "REPLY", to_part("REPLY", calendar) |
paul@48 | 96 | |
paul@48 | 97 | class Freebusy(Handler): |
paul@48 | 98 | |
paul@48 | 99 | "A free/busy handler." |
paul@48 | 100 | |
paul@48 | 101 | def publish(self): |
paul@48 | 102 | pass |
paul@48 | 103 | |
paul@48 | 104 | def reply(self): |
paul@48 | 105 | |
paul@48 | 106 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 107 | |
paul@48 | 108 | pass |
paul@48 | 109 | |
paul@48 | 110 | def request(self): |
paul@48 | 111 | |
paul@48 | 112 | """ |
paul@48 | 113 | Respond to a request by preparing a reply containing free/busy |
paul@48 | 114 | information for each indicated attendee. |
paul@48 | 115 | """ |
paul@48 | 116 | |
paul@48 | 117 | oa = self.require_organiser_and_attendees() |
paul@48 | 118 | if not oa: |
paul@48 | 119 | return None |
paul@48 | 120 | |
paul@48 | 121 | (organiser, organiser_attr), attendees = oa |
paul@48 | 122 | |
paul@48 | 123 | # Construct an appropriate fragment. |
paul@48 | 124 | |
paul@48 | 125 | calendar = [] |
paul@48 | 126 | cwrite = calendar.append |
paul@48 | 127 | |
paul@48 | 128 | # Get the details for each attendee. |
paul@48 | 129 | |
paul@48 | 130 | for attendee, attendee_attr in attendees.items(): |
paul@48 | 131 | freebusy = self.store.get_freebusy(attendee) |
paul@48 | 132 | |
paul@60 | 133 | record = [] |
paul@60 | 134 | rwrite = record.append |
paul@48 | 135 | |
paul@60 | 136 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@60 | 137 | rwrite(("ATTENDEE", attendee_attr, attendee)) |
paul@60 | 138 | rwrite(("UID", {}, self.uid)) |
paul@48 | 139 | |
paul@60 | 140 | if freebusy: |
paul@48 | 141 | for start, end, uid in freebusy: |
paul@48 | 142 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end])) |
paul@48 | 143 | |
paul@60 | 144 | cwrite(("VFREEBUSY", {}, record)) |
paul@48 | 145 | |
paul@48 | 146 | # Return the reply. |
paul@48 | 147 | |
paul@60 | 148 | return "REPLY", to_part("REPLY", calendar) |
paul@48 | 149 | |
paul@48 | 150 | class Journal(Handler): |
paul@48 | 151 | |
paul@48 | 152 | "A journal entry handler." |
paul@48 | 153 | |
paul@48 | 154 | def add(self): |
paul@48 | 155 | pass |
paul@48 | 156 | |
paul@48 | 157 | def cancel(self): |
paul@48 | 158 | pass |
paul@48 | 159 | |
paul@48 | 160 | def publish(self): |
paul@48 | 161 | pass |
paul@48 | 162 | |
paul@48 | 163 | class Todo(Handler): |
paul@48 | 164 | |
paul@48 | 165 | "A to-do item handler." |
paul@48 | 166 | |
paul@48 | 167 | def add(self): |
paul@48 | 168 | pass |
paul@48 | 169 | |
paul@48 | 170 | def cancel(self): |
paul@48 | 171 | pass |
paul@48 | 172 | |
paul@48 | 173 | def counter(self): |
paul@48 | 174 | |
paul@48 | 175 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 176 | |
paul@48 | 177 | pass |
paul@48 | 178 | |
paul@48 | 179 | def declinecounter(self): |
paul@48 | 180 | |
paul@48 | 181 | """ |
paul@48 | 182 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 183 | replies to such proposals. |
paul@48 | 184 | """ |
paul@48 | 185 | |
paul@48 | 186 | pass |
paul@48 | 187 | |
paul@48 | 188 | def publish(self): |
paul@48 | 189 | pass |
paul@48 | 190 | |
paul@48 | 191 | def refresh(self): |
paul@48 | 192 | pass |
paul@48 | 193 | |
paul@48 | 194 | def reply(self): |
paul@48 | 195 | |
paul@48 | 196 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 197 | |
paul@48 | 198 | pass |
paul@48 | 199 | |
paul@48 | 200 | def request(self): |
paul@48 | 201 | pass |
paul@48 | 202 | |
paul@48 | 203 | # Handler registry. |
paul@48 | 204 | |
paul@48 | 205 | handlers = [ |
paul@48 | 206 | ("VFREEBUSY", Freebusy), |
paul@48 | 207 | ("VEVENT", Event), |
paul@48 | 208 | ("VTODO", Todo), |
paul@48 | 209 | ("VJOURNAL", Journal), |
paul@48 | 210 | ] |
paul@48 | 211 | |
paul@48 | 212 | # vim: tabstop=4 expandtab shiftwidth=4 |