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