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@48 | 7 | from datetime import date, datetime, timedelta |
paul@51 | 8 | from imiptools.content import Handler, format_datetime |
paul@48 | 9 | from imiptools.period import insert_period, period_overlaps, remove_period |
paul@48 | 10 | from vCalendar import to_node |
paul@48 | 11 | from vRecurrence import get_parameters, get_rule |
paul@48 | 12 | |
paul@48 | 13 | class Event(Handler): |
paul@48 | 14 | |
paul@48 | 15 | "An event handler." |
paul@48 | 16 | |
paul@48 | 17 | def add(self): |
paul@48 | 18 | pass |
paul@48 | 19 | |
paul@48 | 20 | def cancel(self): |
paul@48 | 21 | pass |
paul@48 | 22 | |
paul@48 | 23 | def counter(self): |
paul@48 | 24 | |
paul@48 | 25 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 26 | |
paul@48 | 27 | pass |
paul@48 | 28 | |
paul@48 | 29 | def declinecounter(self): |
paul@48 | 30 | |
paul@48 | 31 | """ |
paul@48 | 32 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 33 | replies to such proposals. |
paul@48 | 34 | """ |
paul@48 | 35 | |
paul@48 | 36 | pass |
paul@48 | 37 | |
paul@48 | 38 | def publish(self): |
paul@48 | 39 | pass |
paul@48 | 40 | |
paul@48 | 41 | def refresh(self): |
paul@48 | 42 | pass |
paul@48 | 43 | |
paul@48 | 44 | def reply(self): |
paul@48 | 45 | |
paul@48 | 46 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 47 | |
paul@48 | 48 | pass |
paul@48 | 49 | |
paul@48 | 50 | def request(self): |
paul@48 | 51 | |
paul@48 | 52 | """ |
paul@48 | 53 | Respond to a request by preparing a reply containing accept/decline |
paul@48 | 54 | information for each indicated attendee. |
paul@48 | 55 | |
paul@48 | 56 | No support for countering requests is implemented. |
paul@48 | 57 | """ |
paul@48 | 58 | |
paul@48 | 59 | oa = self.require_organiser_and_attendees() |
paul@48 | 60 | if not oa: |
paul@48 | 61 | return None |
paul@48 | 62 | |
paul@48 | 63 | (organiser, organiser_attr), attendees = oa |
paul@48 | 64 | |
paul@48 | 65 | # Process each attendee separately. |
paul@48 | 66 | |
paul@48 | 67 | calendar = [] |
paul@48 | 68 | |
paul@48 | 69 | for attendee, attendee_attr in attendees.items(): |
paul@48 | 70 | |
paul@48 | 71 | # Check for event using UID. |
paul@48 | 72 | |
paul@51 | 73 | if not self.have_new_object(attendee, "VEVENT"): |
paul@51 | 74 | continue |
paul@48 | 75 | |
paul@48 | 76 | # If newer than any old version, discard old details from the |
paul@48 | 77 | # free/busy record and check for suitability. |
paul@48 | 78 | |
paul@48 | 79 | dtstart = self.get_utc_datetime("DTSTART") |
paul@48 | 80 | dtend = self.get_utc_datetime("DTEND") |
paul@48 | 81 | |
paul@48 | 82 | # NOTE: Need also DURATION support. |
paul@48 | 83 | |
paul@48 | 84 | duration = dtend - dtstart |
paul@48 | 85 | |
paul@48 | 86 | # Recurrence rules create multiple instances to be checked. |
paul@48 | 87 | # Conflicts may only be assessed within a period defined by policy |
paul@48 | 88 | # for the agent, with instances outside that period being considered |
paul@48 | 89 | # unchecked. |
paul@48 | 90 | |
paul@48 | 91 | # NOTE: Need to expose the 100 day window in the configuration. |
paul@48 | 92 | |
paul@48 | 93 | window_end = datetime.now() + timedelta(100) |
paul@48 | 94 | |
paul@48 | 95 | # NOTE: Need also RDATE and EXDATE support. |
paul@48 | 96 | |
paul@48 | 97 | rrule = self.get_value("RRULE") |
paul@48 | 98 | |
paul@48 | 99 | if rrule: |
paul@48 | 100 | selector = get_rule(dtstart, rrule) |
paul@48 | 101 | parameters = get_parameters(rrule) |
paul@48 | 102 | periods = [] |
paul@48 | 103 | for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")): |
paul@48 | 104 | start = datetime(*start, tzinfo=timezone("UTC")) |
paul@48 | 105 | end = start + duration |
paul@48 | 106 | periods.append((format_datetime(start), format_datetime(end))) |
paul@48 | 107 | else: |
paul@48 | 108 | periods = [(format_datetime(dtstart), format_datetime(dtend))] |
paul@48 | 109 | |
paul@48 | 110 | conflict = False |
paul@48 | 111 | freebusy = self.store.get_freebusy(attendee) |
paul@48 | 112 | |
paul@48 | 113 | if freebusy: |
paul@48 | 114 | remove_period(freebusy, self.uid) |
paul@48 | 115 | conflict = True |
paul@48 | 116 | for start, end in periods: |
paul@48 | 117 | if period_overlaps(freebusy, (start, end)): |
paul@48 | 118 | break |
paul@48 | 119 | else: |
paul@48 | 120 | conflict = False |
paul@48 | 121 | else: |
paul@48 | 122 | freebusy = [] |
paul@48 | 123 | |
paul@48 | 124 | # If the event can be scheduled, it is registered and a reply sent |
paul@48 | 125 | # accepting the event. (The attendee has PARTSTAT=ACCEPTED as an |
paul@48 | 126 | # attribute.) |
paul@48 | 127 | |
paul@48 | 128 | if not conflict: |
paul@48 | 129 | for start, end in periods: |
paul@48 | 130 | insert_period(freebusy, (start, end, self.uid)) |
paul@48 | 131 | |
paul@48 | 132 | if self.get_value("TRANSP") in (None, "OPAQUE"): |
paul@48 | 133 | self.store.set_freebusy(attendee, freebusy) |
paul@48 | 134 | |
paul@48 | 135 | if self.publisher: |
paul@48 | 136 | self.publisher.set_freebusy(attendee, freebusy) |
paul@48 | 137 | |
paul@48 | 138 | self.store.set_event(attendee, self.uid, to_node( |
paul@48 | 139 | {"VEVENT" : [(self.details, {})]} |
paul@48 | 140 | )) |
paul@48 | 141 | attendee_attr["PARTSTAT"] = "ACCEPTED" |
paul@48 | 142 | |
paul@48 | 143 | # If the event cannot be scheduled, it is not registered and a reply |
paul@48 | 144 | # sent declining the event. (The attendee has PARTSTAT=DECLINED as an |
paul@48 | 145 | # attribute.) |
paul@48 | 146 | |
paul@48 | 147 | else: |
paul@48 | 148 | attendee_attr["PARTSTAT"] = "DECLINED" |
paul@48 | 149 | |
paul@48 | 150 | self.details["ATTENDEE"] = [(attendee, attendee_attr)] |
paul@48 | 151 | calendar.append(to_node( |
paul@48 | 152 | {"VEVENT" : [(self.details, {})]} |
paul@48 | 153 | )) |
paul@48 | 154 | |
paul@48 | 155 | return calendar |
paul@48 | 156 | |
paul@48 | 157 | class Freebusy(Handler): |
paul@48 | 158 | |
paul@48 | 159 | "A free/busy handler." |
paul@48 | 160 | |
paul@48 | 161 | def publish(self): |
paul@48 | 162 | pass |
paul@48 | 163 | |
paul@48 | 164 | def reply(self): |
paul@48 | 165 | |
paul@48 | 166 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 167 | |
paul@48 | 168 | pass |
paul@48 | 169 | |
paul@48 | 170 | def request(self): |
paul@48 | 171 | |
paul@48 | 172 | """ |
paul@48 | 173 | Respond to a request by preparing a reply containing free/busy |
paul@48 | 174 | information for each indicated attendee. |
paul@48 | 175 | """ |
paul@48 | 176 | |
paul@48 | 177 | oa = self.require_organiser_and_attendees() |
paul@48 | 178 | if not oa: |
paul@48 | 179 | return None |
paul@48 | 180 | |
paul@48 | 181 | (organiser, organiser_attr), attendees = oa |
paul@48 | 182 | |
paul@48 | 183 | # Construct an appropriate fragment. |
paul@48 | 184 | |
paul@48 | 185 | calendar = [] |
paul@48 | 186 | cwrite = calendar.append |
paul@48 | 187 | |
paul@48 | 188 | # Get the details for each attendee. |
paul@48 | 189 | |
paul@48 | 190 | for attendee, attendee_attr in attendees.items(): |
paul@48 | 191 | freebusy = self.store.get_freebusy(attendee) |
paul@48 | 192 | |
paul@48 | 193 | if freebusy: |
paul@48 | 194 | record = [] |
paul@48 | 195 | rwrite = record.append |
paul@48 | 196 | |
paul@48 | 197 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@48 | 198 | rwrite(("ATTENDEE", attendee_attr, attendee)) |
paul@48 | 199 | rwrite(("UID", {}, self.uid)) |
paul@48 | 200 | |
paul@48 | 201 | for start, end, uid in freebusy: |
paul@48 | 202 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end])) |
paul@48 | 203 | |
paul@48 | 204 | cwrite(("VFREEBUSY", {}, record)) |
paul@48 | 205 | |
paul@48 | 206 | # Return the reply. |
paul@48 | 207 | |
paul@48 | 208 | return calendar |
paul@48 | 209 | |
paul@48 | 210 | class Journal(Handler): |
paul@48 | 211 | |
paul@48 | 212 | "A journal entry handler." |
paul@48 | 213 | |
paul@48 | 214 | def add(self): |
paul@48 | 215 | pass |
paul@48 | 216 | |
paul@48 | 217 | def cancel(self): |
paul@48 | 218 | pass |
paul@48 | 219 | |
paul@48 | 220 | def publish(self): |
paul@48 | 221 | pass |
paul@48 | 222 | |
paul@48 | 223 | class Todo(Handler): |
paul@48 | 224 | |
paul@48 | 225 | "A to-do item handler." |
paul@48 | 226 | |
paul@48 | 227 | def add(self): |
paul@48 | 228 | pass |
paul@48 | 229 | |
paul@48 | 230 | def cancel(self): |
paul@48 | 231 | pass |
paul@48 | 232 | |
paul@48 | 233 | def counter(self): |
paul@48 | 234 | |
paul@48 | 235 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 236 | |
paul@48 | 237 | pass |
paul@48 | 238 | |
paul@48 | 239 | def declinecounter(self): |
paul@48 | 240 | |
paul@48 | 241 | """ |
paul@48 | 242 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 243 | replies to such proposals. |
paul@48 | 244 | """ |
paul@48 | 245 | |
paul@48 | 246 | pass |
paul@48 | 247 | |
paul@48 | 248 | def publish(self): |
paul@48 | 249 | pass |
paul@48 | 250 | |
paul@48 | 251 | def refresh(self): |
paul@48 | 252 | pass |
paul@48 | 253 | |
paul@48 | 254 | def reply(self): |
paul@48 | 255 | |
paul@48 | 256 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 257 | |
paul@48 | 258 | pass |
paul@48 | 259 | |
paul@48 | 260 | def request(self): |
paul@48 | 261 | pass |
paul@48 | 262 | |
paul@48 | 263 | # Handler registry. |
paul@48 | 264 | |
paul@48 | 265 | handlers = [ |
paul@48 | 266 | ("VFREEBUSY", Freebusy), |
paul@48 | 267 | ("VEVENT", Event), |
paul@48 | 268 | ("VTODO", Todo), |
paul@48 | 269 | ("VJOURNAL", Journal), |
paul@48 | 270 | ] |
paul@48 | 271 | |
paul@48 | 272 | # vim: tabstop=4 expandtab shiftwidth=4 |