paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@16 | 3 | from bisect import bisect_left, insort_left |
paul@24 | 4 | from datetime import date, datetime |
paul@0 | 5 | from email import message_from_file |
paul@12 | 6 | from email.mime.multipart import MIMEMultipart |
paul@8 | 7 | from email.mime.text import MIMEText |
paul@24 | 8 | from pytz import timezone, UnknownTimeZoneError |
paul@8 | 9 | from smtplib import SMTP |
paul@26 | 10 | from vCalendar import parse, ParseError, to_dict, to_node |
paul@2 | 11 | import imip_store |
paul@24 | 12 | import re |
paul@0 | 13 | import sys |
paul@0 | 14 | |
paul@0 | 15 | try: |
paul@0 | 16 | from cStringIO import StringIO |
paul@0 | 17 | except ImportError: |
paul@0 | 18 | from StringIO import StringIO |
paul@0 | 19 | |
paul@27 | 20 | MESSAGE_SENDER = "resources+agent@example.com" |
paul@12 | 21 | |
paul@19 | 22 | MESSAGE_SUBJECT = "Calendar system message" |
paul@19 | 23 | |
paul@19 | 24 | MESSAGE_TEXT = """\ |
paul@19 | 25 | This is a response to a calendar message sent by your calendar program. |
paul@19 | 26 | """ |
paul@19 | 27 | |
paul@0 | 28 | # Postfix exit codes. |
paul@0 | 29 | |
paul@0 | 30 | EX_TEMPFAIL = 75 |
paul@0 | 31 | |
paul@0 | 32 | # Permitted iTIP content types. |
paul@0 | 33 | |
paul@0 | 34 | itip_content_types = [ |
paul@0 | 35 | "text/calendar", # from RFC 6047 |
paul@0 | 36 | "text/x-vcalendar", "application/ics", # other possibilities |
paul@0 | 37 | ] |
paul@0 | 38 | |
paul@24 | 39 | # iCalendar date and datetime parsing (from DateSupport in MoinSupport). |
paul@24 | 40 | |
paul@24 | 41 | date_icalendar_regexp_str = ur'(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})' |
paul@24 | 42 | datetime_icalendar_regexp_str = date_icalendar_regexp_str + \ |
paul@24 | 43 | ur'(?:' \ |
paul@24 | 44 | ur'T(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-6][0-9])' \ |
paul@24 | 45 | ur'(?P<utc>Z)?' \ |
paul@24 | 46 | ur')?' |
paul@24 | 47 | |
paul@24 | 48 | match_date_icalendar = re.compile(date_icalendar_regexp_str, re.UNICODE).match |
paul@24 | 49 | match_datetime_icalendar = re.compile(datetime_icalendar_regexp_str, re.UNICODE).match |
paul@24 | 50 | |
paul@16 | 51 | # Content interpretation. |
paul@16 | 52 | |
paul@16 | 53 | def get_items(d, name, all=True): |
paul@16 | 54 | if d.has_key(name): |
paul@16 | 55 | values = d[name] |
paul@16 | 56 | if not all and len(values) == 1: |
paul@16 | 57 | return values[0] |
paul@16 | 58 | else: |
paul@16 | 59 | return values |
paul@16 | 60 | else: |
paul@16 | 61 | return None |
paul@16 | 62 | |
paul@16 | 63 | def get_item(d, name): |
paul@16 | 64 | return get_items(d, name, False) |
paul@16 | 65 | |
paul@16 | 66 | def get_value_map(d, name): |
paul@16 | 67 | items = get_items(d, name) |
paul@16 | 68 | if items: |
paul@16 | 69 | return dict(items) |
paul@16 | 70 | else: |
paul@16 | 71 | return {} |
paul@16 | 72 | |
paul@16 | 73 | def get_values(d, name, all=True): |
paul@16 | 74 | if d.has_key(name): |
paul@16 | 75 | values = d[name] |
paul@16 | 76 | if not all and len(values) == 1: |
paul@16 | 77 | return values[0][0] |
paul@16 | 78 | else: |
paul@16 | 79 | return map(lambda x: x[0], values) |
paul@16 | 80 | else: |
paul@16 | 81 | return None |
paul@16 | 82 | |
paul@16 | 83 | def get_value(d, name): |
paul@16 | 84 | return get_values(d, name, False) |
paul@16 | 85 | |
paul@24 | 86 | def get_utc_datetime(d, name): |
paul@24 | 87 | value, attr = get_item(d, name) |
paul@24 | 88 | dt = get_datetime(value, attr) |
paul@24 | 89 | if isinstance(dt, datetime): |
paul@24 | 90 | return dt.astimezone(timezone("UTC")).strftime("%Y%m%dT%H%M%SZ") |
paul@24 | 91 | else: |
paul@24 | 92 | return dt.strftime("%Y%m%d") |
paul@24 | 93 | |
paul@16 | 94 | def get_address(value): |
paul@16 | 95 | return value.startswith("mailto:") and value[7:] or value |
paul@16 | 96 | |
paul@16 | 97 | def get_uri(value): |
paul@16 | 98 | return value.startswith("mailto:") and value or "mailto:%s" % value |
paul@16 | 99 | |
paul@24 | 100 | def get_datetime(value, attr): |
paul@24 | 101 | try: |
paul@24 | 102 | tz = attr.has_key("TZID") and timezone(attr["TZID"]) or None |
paul@24 | 103 | except UnknownTimeZoneError: |
paul@24 | 104 | tz = None |
paul@24 | 105 | |
paul@24 | 106 | m = match_datetime_icalendar(value) |
paul@24 | 107 | if m: |
paul@24 | 108 | dt = datetime( |
paul@24 | 109 | int(m.group("year")), int(m.group("month")), int(m.group("day")), |
paul@24 | 110 | int(m.group("hour")), int(m.group("minute")), int(m.group("second")) |
paul@24 | 111 | ) |
paul@24 | 112 | |
paul@24 | 113 | # Impose the indicated timezone. |
paul@24 | 114 | # NOTE: This needs an ambiguity policy for DST changes. |
paul@24 | 115 | |
paul@24 | 116 | tz = m.group("utc") and timezone("UTC") or tz or None |
paul@24 | 117 | if tz is not None: |
paul@24 | 118 | return tz.localize(dt) |
paul@24 | 119 | else: |
paul@24 | 120 | return dt |
paul@24 | 121 | |
paul@24 | 122 | m = match_date_icalendar(value) |
paul@24 | 123 | if m: |
paul@24 | 124 | return date( |
paul@24 | 125 | int(m.group("year")), int(m.group("month")), int(m.group("day")) |
paul@24 | 126 | ) |
paul@24 | 127 | return None |
paul@24 | 128 | |
paul@16 | 129 | # Time management. |
paul@16 | 130 | |
paul@16 | 131 | def insert_period(freebusy, period): |
paul@16 | 132 | insort_left(freebusy, period) |
paul@16 | 133 | |
paul@16 | 134 | def remove_period(freebusy, uid): |
paul@16 | 135 | i = 0 |
paul@16 | 136 | while i < len(freebusy): |
paul@16 | 137 | t = freebusy[i] |
paul@16 | 138 | if len(t) >= 3 and t[2] == uid: |
paul@16 | 139 | del freebusy[i] |
paul@16 | 140 | else: |
paul@16 | 141 | i += 1 |
paul@16 | 142 | |
paul@16 | 143 | def period_overlaps(freebusy, period): |
paul@16 | 144 | dtstart, dtend = period[:2] |
paul@16 | 145 | i = bisect_left(freebusy, (dtstart, dtend, None)) |
paul@22 | 146 | return ( |
paul@22 | 147 | i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend) |
paul@22 | 148 | or |
paul@22 | 149 | i > 0 and freebusy[i - 1][1] > dtstart |
paul@22 | 150 | ) |
paul@16 | 151 | |
paul@8 | 152 | # Sending of outgoing messages. |
paul@8 | 153 | |
paul@8 | 154 | def sendmail(sender, recipients, data): |
paul@8 | 155 | smtp = SMTP("localhost") |
paul@8 | 156 | smtp.sendmail(sender, recipients, data) |
paul@8 | 157 | smtp.quit() |
paul@8 | 158 | |
paul@8 | 159 | # Processing of incoming messages. |
paul@8 | 160 | |
paul@27 | 161 | def process(f, original_recipients, recipients, this_sender): |
paul@8 | 162 | |
paul@9 | 163 | """ |
paul@9 | 164 | Process content from the stream 'f' accompanied by the given |
paul@27 | 165 | 'original_recipients' and 'recipients', together with details of |
paul@27 | 166 | 'this_sender' (the sender address on messages sent by this service). |
paul@9 | 167 | """ |
paul@8 | 168 | |
paul@0 | 169 | msg = message_from_file(f) |
paul@13 | 170 | senders = msg.get_all("Reply-To") or msg.get_all("From") |
paul@0 | 171 | |
paul@0 | 172 | # Handle messages with iTIP parts. |
paul@0 | 173 | |
paul@12 | 174 | all_parts = [] |
paul@12 | 175 | |
paul@0 | 176 | for part in msg.walk(): |
paul@0 | 177 | if part.get_content_type() in itip_content_types and \ |
paul@0 | 178 | part.get_param("method"): |
paul@0 | 179 | |
paul@12 | 180 | all_parts += handle_itip_part(part, original_recipients) |
paul@12 | 181 | |
paul@12 | 182 | # Pack the parts into a single message. |
paul@12 | 183 | |
paul@12 | 184 | if all_parts: |
paul@19 | 185 | text_part = MIMEText(MESSAGE_TEXT) |
paul@19 | 186 | all_parts.insert(0, text_part) |
paul@19 | 187 | message = MIMEMultipart("alternative", _subparts=all_parts) |
paul@19 | 188 | message.preamble = MESSAGE_TEXT |
paul@12 | 189 | |
paul@27 | 190 | message["From"] = this_sender |
paul@13 | 191 | for sender in senders: |
paul@13 | 192 | message["To"] = sender |
paul@19 | 193 | message["Subject"] = MESSAGE_SUBJECT |
paul@12 | 194 | |
paul@12 | 195 | if "-d" in sys.argv: |
paul@12 | 196 | print message |
paul@13 | 197 | else: |
paul@27 | 198 | sendmail(this_sender, senders, message.as_string()) |
paul@0 | 199 | |
paul@16 | 200 | def to_part(method, calendar): |
paul@11 | 201 | |
paul@16 | 202 | """ |
paul@16 | 203 | Write using the given 'method', the 'calendar' details to a MIME |
paul@16 | 204 | text/calendar part. |
paul@16 | 205 | """ |
paul@10 | 206 | |
paul@16 | 207 | encoding = "utf-8" |
paul@12 | 208 | out = StringIO() |
paul@12 | 209 | try: |
paul@12 | 210 | calendar[:0] = [ |
paul@12 | 211 | ("METHOD", {}, method), |
paul@12 | 212 | ("VERSION", {}, "2.0") |
paul@12 | 213 | ] |
paul@26 | 214 | imip_store.to_stream(out, ("VCALENDAR", {}, calendar), encoding) |
paul@20 | 215 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@20 | 216 | part.set_param("method", method) |
paul@20 | 217 | return part |
paul@20 | 218 | |
paul@12 | 219 | finally: |
paul@12 | 220 | out.close() |
paul@12 | 221 | |
paul@16 | 222 | def parse_object(f, encoding, objtype): |
paul@16 | 223 | |
paul@16 | 224 | """ |
paul@16 | 225 | Parse the iTIP content from 'f' having the given 'encoding'. Return None if |
paul@16 | 226 | the content was not readable or suitable. |
paul@16 | 227 | """ |
paul@16 | 228 | |
paul@16 | 229 | try: |
paul@16 | 230 | try: |
paul@26 | 231 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@16 | 232 | if doctype == objtype: |
paul@26 | 233 | return to_dict(obj)[objtype][0] |
paul@16 | 234 | finally: |
paul@16 | 235 | f.close() |
paul@16 | 236 | except (ParseError, ValueError): |
paul@16 | 237 | pass |
paul@16 | 238 | |
paul@16 | 239 | return None |
paul@16 | 240 | |
paul@9 | 241 | def handle_itip_part(part, recipients): |
paul@12 | 242 | |
paul@12 | 243 | "Handle the given iTIP 'part' for the given 'recipients'." |
paul@12 | 244 | |
paul@0 | 245 | method = part.get_param("method") |
paul@0 | 246 | |
paul@8 | 247 | # Decode the data and parse it. |
paul@8 | 248 | |
paul@0 | 249 | f = StringIO(part.get_payload(decode=True)) |
paul@8 | 250 | |
paul@16 | 251 | itip = parse_object(f, part.get_content_charset(), "VCALENDAR") |
paul@28 | 252 | |
paul@28 | 253 | # Ignore the part if not a calendar object. |
paul@28 | 254 | |
paul@16 | 255 | if not itip: |
paul@28 | 256 | return [] |
paul@8 | 257 | |
paul@8 | 258 | # Only handle calendar information. |
paul@0 | 259 | |
paul@12 | 260 | all_parts = [] |
paul@12 | 261 | |
paul@16 | 262 | # Require consistency between declared and employed methods. |
paul@0 | 263 | |
paul@16 | 264 | if get_value(itip, "METHOD") == method: |
paul@8 | 265 | |
paul@16 | 266 | # Look for different kinds of sections. |
paul@2 | 267 | |
paul@16 | 268 | all_objects = [] |
paul@8 | 269 | |
paul@16 | 270 | for name, cls in handlers: |
paul@16 | 271 | for details in get_values(itip, name) or []: |
paul@12 | 272 | |
paul@16 | 273 | # Dispatch to a handler and obtain any response. |
paul@8 | 274 | |
paul@16 | 275 | handler = cls(details, recipients) |
paul@16 | 276 | object = methods[method](handler)() |
paul@2 | 277 | |
paul@16 | 278 | # Concatenate responses for a single calendar object. |
paul@12 | 279 | |
paul@16 | 280 | if object: |
paul@16 | 281 | all_objects += object |
paul@8 | 282 | |
paul@16 | 283 | # Obtain a message part for the objects. |
paul@12 | 284 | |
paul@16 | 285 | if all_objects: |
paul@20 | 286 | all_parts.append(to_part(response_methods[method], all_objects)) |
paul@12 | 287 | |
paul@12 | 288 | return all_parts |
paul@2 | 289 | |
paul@2 | 290 | class Handler: |
paul@8 | 291 | |
paul@8 | 292 | "General handler support." |
paul@8 | 293 | |
paul@9 | 294 | def __init__(self, details, recipients): |
paul@9 | 295 | |
paul@9 | 296 | """ |
paul@9 | 297 | Initialise the handler with the 'details' of a calendar object and the |
paul@9 | 298 | 'recipients' of the object. |
paul@9 | 299 | """ |
paul@9 | 300 | |
paul@2 | 301 | self.details = details |
paul@9 | 302 | self.recipients = set(recipients) |
paul@9 | 303 | |
paul@2 | 304 | self.uid = get_value(details, "UID") |
paul@2 | 305 | self.sequence = get_value(details, "SEQUENCE") |
paul@16 | 306 | self.dtstamp = get_value(details, "DTSTAMP") |
paul@16 | 307 | |
paul@2 | 308 | self.store = imip_store.FileStore() |
paul@2 | 309 | |
paul@11 | 310 | def get_items(self, name, all=True): |
paul@11 | 311 | return get_items(self.details, name, all) |
paul@11 | 312 | |
paul@11 | 313 | def get_item(self, name): |
paul@11 | 314 | return get_item(self.details, name) |
paul@5 | 315 | |
paul@10 | 316 | def get_value_map(self, name): |
paul@10 | 317 | return get_value_map(self.details, name) |
paul@10 | 318 | |
paul@11 | 319 | def get_values(self, name, all=True): |
paul@11 | 320 | return get_values(self.details, name, all) |
paul@11 | 321 | |
paul@11 | 322 | def get_value(self, name): |
paul@11 | 323 | return get_value(self.details, name) |
paul@2 | 324 | |
paul@24 | 325 | def get_utc_datetime(self, name): |
paul@24 | 326 | return get_utc_datetime(self.details, name) |
paul@24 | 327 | |
paul@10 | 328 | def filter_by_recipients(self, values): |
paul@10 | 329 | return self.recipients.intersection(map(get_address, values)) |
paul@9 | 330 | |
paul@14 | 331 | def require_organiser_and_attendees(self): |
paul@14 | 332 | attendee_map = self.get_value_map("ATTENDEE") |
paul@14 | 333 | organiser = self.get_item("ORGANIZER") |
paul@14 | 334 | |
paul@14 | 335 | # Only provide details for recipients who are also attendees. |
paul@14 | 336 | |
paul@14 | 337 | attendees = {} |
paul@14 | 338 | for attendee in map(get_uri, self.filter_by_recipients(attendee_map)): |
paul@14 | 339 | attendees[attendee] = attendee_map[attendee] |
paul@14 | 340 | |
paul@14 | 341 | if not attendees and not organiser: |
paul@14 | 342 | return None |
paul@14 | 343 | |
paul@14 | 344 | return organiser, attendees |
paul@14 | 345 | |
paul@2 | 346 | class Event(Handler): |
paul@6 | 347 | |
paul@6 | 348 | "An event handler." |
paul@6 | 349 | |
paul@2 | 350 | def add(self): |
paul@2 | 351 | pass |
paul@2 | 352 | |
paul@2 | 353 | def cancel(self): |
paul@2 | 354 | pass |
paul@2 | 355 | |
paul@2 | 356 | def counter(self): |
paul@16 | 357 | |
paul@16 | 358 | "Since this handler does not send requests, it will not handle replies." |
paul@16 | 359 | |
paul@2 | 360 | pass |
paul@2 | 361 | |
paul@2 | 362 | def declinecounter(self): |
paul@16 | 363 | |
paul@16 | 364 | """ |
paul@16 | 365 | Since this handler does not send counter proposals, it will not handle |
paul@16 | 366 | replies to such proposals. |
paul@16 | 367 | """ |
paul@16 | 368 | |
paul@2 | 369 | pass |
paul@2 | 370 | |
paul@6 | 371 | def publish(self): |
paul@6 | 372 | pass |
paul@6 | 373 | |
paul@2 | 374 | def refresh(self): |
paul@2 | 375 | pass |
paul@2 | 376 | |
paul@2 | 377 | def reply(self): |
paul@6 | 378 | |
paul@6 | 379 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 380 | |
paul@2 | 381 | pass |
paul@2 | 382 | |
paul@2 | 383 | def request(self): |
paul@14 | 384 | |
paul@14 | 385 | """ |
paul@14 | 386 | Respond to a request by preparing a reply containing accept/decline |
paul@14 | 387 | information for each indicated attendee. |
paul@16 | 388 | |
paul@16 | 389 | No support for countering requests is implemented. |
paul@14 | 390 | """ |
paul@2 | 391 | |
paul@16 | 392 | oa = self.require_organiser_and_attendees() |
paul@16 | 393 | if not oa: |
paul@16 | 394 | return None |
paul@16 | 395 | |
paul@16 | 396 | (organiser, organiser_attr), attendees = oa |
paul@16 | 397 | |
paul@16 | 398 | # Process each attendee separately. |
paul@16 | 399 | |
paul@24 | 400 | calendar = [] |
paul@24 | 401 | |
paul@16 | 402 | for attendee, attendee_attr in attendees.items(): |
paul@16 | 403 | |
paul@16 | 404 | # Check for event using UID. |
paul@16 | 405 | |
paul@16 | 406 | f = self.store.get_event(attendee, self.uid) |
paul@16 | 407 | event = f and parse_object(f, "utf-8", "VEVENT") |
paul@16 | 408 | |
paul@16 | 409 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@16 | 410 | |
paul@16 | 411 | if event: |
paul@16 | 412 | sequence = get_value(event, "SEQUENCE") |
paul@16 | 413 | dtstamp = get_value(event, "DTSTAMP") |
paul@16 | 414 | |
paul@16 | 415 | # If the request refers to an older version of the event, ignore |
paul@16 | 416 | # it. |
paul@16 | 417 | |
paul@18 | 418 | old_dtstamp = self.dtstamp < dtstamp |
paul@16 | 419 | |
paul@16 | 420 | if sequence is not None and ( |
paul@16 | 421 | int(self.sequence) < int(sequence) or |
paul@16 | 422 | int(self.sequence) == int(sequence) and old_dtstamp |
paul@16 | 423 | ) or old_dtstamp: |
paul@16 | 424 | |
paul@16 | 425 | continue |
paul@16 | 426 | |
paul@16 | 427 | # If newer than any old version, discard old details from the |
paul@16 | 428 | # free/busy record and check for suitability. |
paul@16 | 429 | |
paul@24 | 430 | dtstart = self.get_utc_datetime("DTSTART") |
paul@24 | 431 | dtend = self.get_utc_datetime("DTEND") |
paul@16 | 432 | |
paul@16 | 433 | conflict = False |
paul@16 | 434 | freebusy = self.store.get_freebusy(attendee) |
paul@16 | 435 | |
paul@16 | 436 | if freebusy: |
paul@16 | 437 | remove_period(freebusy, self.uid) |
paul@16 | 438 | conflict = period_overlaps(freebusy, (dtstart, dtend)) |
paul@16 | 439 | else: |
paul@16 | 440 | freebusy = [] |
paul@16 | 441 | |
paul@16 | 442 | # If the event can be scheduled, it is registered and a reply sent |
paul@16 | 443 | # accepting the event. (The attendee has PARTSTAT=ACCEPTED as an |
paul@16 | 444 | # attribute.) |
paul@16 | 445 | |
paul@16 | 446 | if not conflict: |
paul@16 | 447 | insert_period(freebusy, (dtstart, dtend, self.uid)) |
paul@16 | 448 | self.store.set_freebusy(attendee, freebusy) |
paul@26 | 449 | self.store.set_event(attendee, self.uid, to_node( |
paul@26 | 450 | {"VEVENT" : [(self.details, {})]} |
paul@26 | 451 | )) |
paul@16 | 452 | attendee_attr["PARTSTAT"] = "ACCEPTED" |
paul@16 | 453 | |
paul@16 | 454 | # If the event cannot be scheduled, it is not registered and a reply |
paul@16 | 455 | # sent declining the event. (The attendee has PARTSTAT=DECLINED as an |
paul@16 | 456 | # attribute.) |
paul@16 | 457 | |
paul@16 | 458 | else: |
paul@16 | 459 | attendee_attr["PARTSTAT"] = "DECLINED" |
paul@16 | 460 | |
paul@16 | 461 | self.details["ATTENDEE"] = [(attendee, attendee_attr)] |
paul@26 | 462 | calendar.append(to_node( |
paul@26 | 463 | {"VEVENT" : [(self.details, {})]} |
paul@26 | 464 | )) |
paul@24 | 465 | |
paul@24 | 466 | return calendar |
paul@16 | 467 | |
paul@2 | 468 | class Freebusy(Handler): |
paul@6 | 469 | |
paul@6 | 470 | "A free/busy handler." |
paul@6 | 471 | |
paul@6 | 472 | def publish(self): |
paul@6 | 473 | pass |
paul@6 | 474 | |
paul@2 | 475 | def reply(self): |
paul@2 | 476 | |
paul@6 | 477 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 478 | |
paul@6 | 479 | pass |
paul@0 | 480 | |
paul@2 | 481 | def request(self): |
paul@6 | 482 | |
paul@6 | 483 | """ |
paul@12 | 484 | Respond to a request by preparing a reply containing free/busy |
paul@12 | 485 | information for each indicated attendee. |
paul@6 | 486 | """ |
paul@6 | 487 | |
paul@14 | 488 | oa = self.require_organiser_and_attendees() |
paul@14 | 489 | if not oa: |
paul@14 | 490 | return None |
paul@9 | 491 | |
paul@14 | 492 | (organiser, organiser_attr), attendees = oa |
paul@5 | 493 | |
paul@12 | 494 | # Construct an appropriate fragment. |
paul@12 | 495 | |
paul@12 | 496 | calendar = [] |
paul@12 | 497 | cwrite = calendar.append |
paul@2 | 498 | |
paul@12 | 499 | # Get the details for each attendee. |
paul@5 | 500 | |
paul@14 | 501 | for attendee, attendee_attr in attendees.items(): |
paul@12 | 502 | freebusy = self.store.get_freebusy(attendee) |
paul@2 | 503 | |
paul@12 | 504 | if freebusy: |
paul@12 | 505 | record = [] |
paul@12 | 506 | rwrite = record.append |
paul@12 | 507 | |
paul@12 | 508 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@14 | 509 | rwrite(("ATTENDEE", attendee_attr, attendee)) |
paul@12 | 510 | rwrite(("UID", {}, self.uid)) |
paul@5 | 511 | |
paul@16 | 512 | for start, end, uid in freebusy: |
paul@12 | 513 | rwrite(("FREEBUSY", {}, [start, end])) |
paul@5 | 514 | |
paul@12 | 515 | cwrite(("VFREEBUSY", {}, record)) |
paul@5 | 516 | |
paul@12 | 517 | # Return the reply. |
paul@8 | 518 | |
paul@12 | 519 | return calendar |
paul@0 | 520 | |
paul@2 | 521 | class Journal(Handler): |
paul@6 | 522 | |
paul@6 | 523 | "A journal entry handler." |
paul@6 | 524 | |
paul@2 | 525 | def add(self): |
paul@2 | 526 | pass |
paul@2 | 527 | |
paul@2 | 528 | def cancel(self): |
paul@2 | 529 | pass |
paul@2 | 530 | |
paul@6 | 531 | def publish(self): |
paul@6 | 532 | pass |
paul@6 | 533 | |
paul@2 | 534 | class Todo(Handler): |
paul@6 | 535 | |
paul@6 | 536 | "A to-do item handler." |
paul@6 | 537 | |
paul@2 | 538 | def add(self): |
paul@2 | 539 | pass |
paul@2 | 540 | |
paul@2 | 541 | def cancel(self): |
paul@2 | 542 | pass |
paul@2 | 543 | |
paul@2 | 544 | def counter(self): |
paul@16 | 545 | |
paul@16 | 546 | "Since this handler does not send requests, it will not handle replies." |
paul@16 | 547 | |
paul@2 | 548 | pass |
paul@0 | 549 | |
paul@2 | 550 | def declinecounter(self): |
paul@16 | 551 | |
paul@16 | 552 | """ |
paul@16 | 553 | Since this handler does not send counter proposals, it will not handle |
paul@16 | 554 | replies to such proposals. |
paul@16 | 555 | """ |
paul@16 | 556 | |
paul@2 | 557 | pass |
paul@2 | 558 | |
paul@6 | 559 | def publish(self): |
paul@6 | 560 | pass |
paul@6 | 561 | |
paul@2 | 562 | def refresh(self): |
paul@2 | 563 | pass |
paul@2 | 564 | |
paul@2 | 565 | def reply(self): |
paul@6 | 566 | |
paul@6 | 567 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 568 | |
paul@2 | 569 | pass |
paul@2 | 570 | |
paul@2 | 571 | def request(self): |
paul@2 | 572 | pass |
paul@2 | 573 | |
paul@2 | 574 | # Handler registry. |
paul@0 | 575 | |
paul@2 | 576 | handlers = [ |
paul@2 | 577 | ("VFREEBUSY", Freebusy), |
paul@2 | 578 | ("VEVENT", Event), |
paul@2 | 579 | ("VTODO", Todo), |
paul@2 | 580 | ("VJOURNAL", Journal), |
paul@2 | 581 | ] |
paul@0 | 582 | |
paul@2 | 583 | methods = { |
paul@2 | 584 | "ADD" : lambda handler: handler.add, |
paul@2 | 585 | "CANCEL" : lambda handler: handler.cancel, |
paul@2 | 586 | "COUNTER" : lambda handler: handler.counter, |
paul@2 | 587 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@2 | 588 | "PUBLISH" : lambda handler: handler.publish, |
paul@2 | 589 | "REFRESH" : lambda handler: handler.refresh, |
paul@2 | 590 | "REPLY" : lambda handler: handler.reply, |
paul@2 | 591 | "REQUEST" : lambda handler: handler.request, |
paul@2 | 592 | } |
paul@0 | 593 | |
paul@20 | 594 | response_methods = { |
paul@20 | 595 | "REQUEST" : "REPLY", |
paul@20 | 596 | } |
paul@11 | 597 | |
paul@11 | 598 | def main(): |
paul@11 | 599 | |
paul@27 | 600 | "Interpret program arguments and process input." |
paul@27 | 601 | |
paul@27 | 602 | # Obtain the different kinds of recipients plus sender address. |
paul@9 | 603 | |
paul@11 | 604 | original_recipients = [] |
paul@11 | 605 | recipients = [] |
paul@27 | 606 | senders = [] |
paul@9 | 607 | |
paul@11 | 608 | l = [] |
paul@9 | 609 | |
paul@11 | 610 | for arg in sys.argv[1:]: |
paul@27 | 611 | |
paul@27 | 612 | # Switch to collecting recipients. |
paul@27 | 613 | |
paul@11 | 614 | if arg == "-o": |
paul@11 | 615 | l = original_recipients |
paul@11 | 616 | elif arg == "-r": |
paul@11 | 617 | l = recipients |
paul@27 | 618 | |
paul@27 | 619 | # Switch to collecting senders. |
paul@27 | 620 | |
paul@27 | 621 | elif arg == "-s": |
paul@27 | 622 | l = senders |
paul@27 | 623 | |
paul@27 | 624 | # Ignore debugging options. |
paul@27 | 625 | |
paul@11 | 626 | elif arg == "-d": |
paul@11 | 627 | pass |
paul@11 | 628 | else: |
paul@11 | 629 | l.append(arg) |
paul@11 | 630 | |
paul@27 | 631 | process(sys.stdin, original_recipients, recipients, senders and senders[0] or MESSAGE_SENDER) |
paul@9 | 632 | |
paul@11 | 633 | if __name__ == "__main__": |
paul@11 | 634 | if "-d" in sys.argv[1:]: |
paul@11 | 635 | main() |
paul@9 | 636 | else: |
paul@11 | 637 | try: |
paul@11 | 638 | main() |
paul@11 | 639 | except SystemExit, value: |
paul@11 | 640 | sys.exit(value) |
paul@11 | 641 | except Exception, exc: |
paul@11 | 642 | type, value, tb = sys.exc_info() |
paul@11 | 643 | print >>sys.stderr, "Exception %s at %d" % (exc, tb.tb_lineno) |
paul@11 | 644 | sys.exit(EX_TEMPFAIL) |
paul@11 | 645 | sys.exit(0) |
paul@0 | 646 | |
paul@0 | 647 | # vim: tabstop=4 expandtab shiftwidth=4 |