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@108 | 8 | from imiptools.handlers.common import CommonFreebusy |
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@94 | 61 | organiser_item, attendees = oa |
paul@94 | 62 | |
paul@94 | 63 | # Validate the organiser, ignoring spoofed requests. |
paul@94 | 64 | |
paul@94 | 65 | if not self.validate_identities([organiser_item]): |
paul@94 | 66 | return None |
paul@48 | 67 | |
paul@48 | 68 | # Process each attendee separately. |
paul@48 | 69 | |
paul@48 | 70 | calendar = [] |
paul@48 | 71 | |
paul@48 | 72 | for attendee, attendee_attr in attendees.items(): |
paul@48 | 73 | |
paul@48 | 74 | # Check for event using UID. |
paul@48 | 75 | |
paul@51 | 76 | if not self.have_new_object(attendee, "VEVENT"): |
paul@51 | 77 | continue |
paul@48 | 78 | |
paul@48 | 79 | # If newer than any old version, discard old details from the |
paul@48 | 80 | # free/busy record and check for suitability. |
paul@48 | 81 | |
paul@75 | 82 | periods = self.get_periods() |
paul@77 | 83 | freebusy = self.store.get_freebusy(attendee) or [] |
paul@77 | 84 | scheduled = self.can_schedule(freebusy, periods) |
paul@48 | 85 | |
paul@76 | 86 | attendee_attr["PARTSTAT"] = scheduled and "ACCEPTED" or "DECLINED" |
paul@89 | 87 | if self.messenger and self.messenger.sender != get_address(attendee): |
paul@89 | 88 | attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@76 | 89 | self.details["ATTENDEE"] = [(attendee, attendee_attr)] |
paul@48 | 90 | |
paul@77 | 91 | event = to_node({"VEVENT" : [(self.details, {})]}) |
paul@77 | 92 | calendar.append(event) |
paul@77 | 93 | self.store.set_event(attendee, self.uid, event) |
paul@77 | 94 | |
paul@77 | 95 | # Only update free/busy details if the event is scheduled. |
paul@48 | 96 | |
paul@76 | 97 | if scheduled: |
paul@77 | 98 | self.update_freebusy(freebusy, attendee, periods) |
paul@76 | 99 | if self.publisher: |
paul@76 | 100 | self.publisher.set_freebusy(attendee, freebusy) |
paul@48 | 101 | |
paul@60 | 102 | return "REPLY", to_part("REPLY", calendar) |
paul@48 | 103 | |
paul@108 | 104 | class Freebusy(Handler, CommonFreebusy): |
paul@48 | 105 | |
paul@48 | 106 | "A free/busy handler." |
paul@48 | 107 | |
paul@48 | 108 | def publish(self): |
paul@48 | 109 | pass |
paul@48 | 110 | |
paul@48 | 111 | def reply(self): |
paul@48 | 112 | |
paul@48 | 113 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 114 | |
paul@48 | 115 | pass |
paul@48 | 116 | |
paul@108 | 117 | # request provided by CommonFreeBusy.request |
paul@48 | 118 | |
paul@48 | 119 | class Journal(Handler): |
paul@48 | 120 | |
paul@48 | 121 | "A journal entry handler." |
paul@48 | 122 | |
paul@48 | 123 | def add(self): |
paul@48 | 124 | pass |
paul@48 | 125 | |
paul@48 | 126 | def cancel(self): |
paul@48 | 127 | pass |
paul@48 | 128 | |
paul@48 | 129 | def publish(self): |
paul@48 | 130 | pass |
paul@48 | 131 | |
paul@48 | 132 | class Todo(Handler): |
paul@48 | 133 | |
paul@48 | 134 | "A to-do item handler." |
paul@48 | 135 | |
paul@48 | 136 | def add(self): |
paul@48 | 137 | pass |
paul@48 | 138 | |
paul@48 | 139 | def cancel(self): |
paul@48 | 140 | pass |
paul@48 | 141 | |
paul@48 | 142 | def counter(self): |
paul@48 | 143 | |
paul@48 | 144 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 145 | |
paul@48 | 146 | pass |
paul@48 | 147 | |
paul@48 | 148 | def declinecounter(self): |
paul@48 | 149 | |
paul@48 | 150 | """ |
paul@48 | 151 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 152 | replies to such proposals. |
paul@48 | 153 | """ |
paul@48 | 154 | |
paul@48 | 155 | pass |
paul@48 | 156 | |
paul@48 | 157 | def publish(self): |
paul@48 | 158 | pass |
paul@48 | 159 | |
paul@48 | 160 | def refresh(self): |
paul@48 | 161 | pass |
paul@48 | 162 | |
paul@48 | 163 | def reply(self): |
paul@48 | 164 | |
paul@48 | 165 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 166 | |
paul@48 | 167 | pass |
paul@48 | 168 | |
paul@48 | 169 | def request(self): |
paul@48 | 170 | pass |
paul@48 | 171 | |
paul@48 | 172 | # Handler registry. |
paul@48 | 173 | |
paul@48 | 174 | handlers = [ |
paul@48 | 175 | ("VFREEBUSY", Freebusy), |
paul@48 | 176 | ("VEVENT", Event), |
paul@48 | 177 | ("VTODO", Todo), |
paul@48 | 178 | ("VJOURNAL", Journal), |
paul@48 | 179 | ] |
paul@48 | 180 | |
paul@48 | 181 | # vim: tabstop=4 expandtab shiftwidth=4 |