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