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@29 | 89 | if not dt: |
paul@29 | 90 | return None |
paul@29 | 91 | elif isinstance(dt, datetime): |
paul@24 | 92 | return dt.astimezone(timezone("UTC")).strftime("%Y%m%dT%H%M%SZ") |
paul@24 | 93 | else: |
paul@24 | 94 | return dt.strftime("%Y%m%d") |
paul@24 | 95 | |
paul@16 | 96 | def get_address(value): |
paul@16 | 97 | return value.startswith("mailto:") and value[7:] or value |
paul@16 | 98 | |
paul@16 | 99 | def get_uri(value): |
paul@16 | 100 | return value.startswith("mailto:") and value or "mailto:%s" % value |
paul@16 | 101 | |
paul@24 | 102 | def get_datetime(value, attr): |
paul@24 | 103 | try: |
paul@24 | 104 | tz = attr.has_key("TZID") and timezone(attr["TZID"]) or None |
paul@24 | 105 | except UnknownTimeZoneError: |
paul@24 | 106 | tz = None |
paul@24 | 107 | |
paul@29 | 108 | if attr.get("VALUE") in (None, "DATE-TIME"): |
paul@29 | 109 | m = match_datetime_icalendar(value) |
paul@29 | 110 | if m: |
paul@29 | 111 | dt = datetime( |
paul@29 | 112 | int(m.group("year")), int(m.group("month")), int(m.group("day")), |
paul@29 | 113 | int(m.group("hour")), int(m.group("minute")), int(m.group("second")) |
paul@29 | 114 | ) |
paul@24 | 115 | |
paul@29 | 116 | # Impose the indicated timezone. |
paul@29 | 117 | # NOTE: This needs an ambiguity policy for DST changes. |
paul@24 | 118 | |
paul@29 | 119 | tz = m.group("utc") and timezone("UTC") or tz or None |
paul@29 | 120 | if tz is not None: |
paul@29 | 121 | return tz.localize(dt) |
paul@29 | 122 | else: |
paul@29 | 123 | return dt |
paul@24 | 124 | |
paul@29 | 125 | if attr.get("VALUE") == "DATE": |
paul@29 | 126 | m = match_date_icalendar(value) |
paul@29 | 127 | if m: |
paul@29 | 128 | return date( |
paul@29 | 129 | int(m.group("year")), int(m.group("month")), int(m.group("day")) |
paul@29 | 130 | ) |
paul@24 | 131 | return None |
paul@24 | 132 | |
paul@16 | 133 | # Time management. |
paul@16 | 134 | |
paul@16 | 135 | def insert_period(freebusy, period): |
paul@16 | 136 | insort_left(freebusy, period) |
paul@16 | 137 | |
paul@16 | 138 | def remove_period(freebusy, uid): |
paul@16 | 139 | i = 0 |
paul@16 | 140 | while i < len(freebusy): |
paul@16 | 141 | t = freebusy[i] |
paul@16 | 142 | if len(t) >= 3 and t[2] == uid: |
paul@16 | 143 | del freebusy[i] |
paul@16 | 144 | else: |
paul@16 | 145 | i += 1 |
paul@16 | 146 | |
paul@16 | 147 | def period_overlaps(freebusy, period): |
paul@16 | 148 | dtstart, dtend = period[:2] |
paul@16 | 149 | i = bisect_left(freebusy, (dtstart, dtend, None)) |
paul@22 | 150 | return ( |
paul@22 | 151 | i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend) |
paul@22 | 152 | or |
paul@22 | 153 | i > 0 and freebusy[i - 1][1] > dtstart |
paul@22 | 154 | ) |
paul@16 | 155 | |
paul@8 | 156 | # Sending of outgoing messages. |
paul@8 | 157 | |
paul@8 | 158 | def sendmail(sender, recipients, data): |
paul@8 | 159 | smtp = SMTP("localhost") |
paul@8 | 160 | smtp.sendmail(sender, recipients, data) |
paul@8 | 161 | smtp.quit() |
paul@8 | 162 | |
paul@8 | 163 | # Processing of incoming messages. |
paul@8 | 164 | |
paul@27 | 165 | def process(f, original_recipients, recipients, this_sender): |
paul@8 | 166 | |
paul@9 | 167 | """ |
paul@9 | 168 | Process content from the stream 'f' accompanied by the given |
paul@27 | 169 | 'original_recipients' and 'recipients', together with details of |
paul@27 | 170 | 'this_sender' (the sender address on messages sent by this service). |
paul@9 | 171 | """ |
paul@8 | 172 | |
paul@0 | 173 | msg = message_from_file(f) |
paul@13 | 174 | senders = msg.get_all("Reply-To") or msg.get_all("From") |
paul@0 | 175 | |
paul@0 | 176 | # Handle messages with iTIP parts. |
paul@0 | 177 | |
paul@12 | 178 | all_parts = [] |
paul@12 | 179 | |
paul@0 | 180 | for part in msg.walk(): |
paul@0 | 181 | if part.get_content_type() in itip_content_types and \ |
paul@0 | 182 | part.get_param("method"): |
paul@0 | 183 | |
paul@12 | 184 | all_parts += handle_itip_part(part, original_recipients) |
paul@12 | 185 | |
paul@12 | 186 | # Pack the parts into a single message. |
paul@12 | 187 | |
paul@12 | 188 | if all_parts: |
paul@19 | 189 | text_part = MIMEText(MESSAGE_TEXT) |
paul@19 | 190 | all_parts.insert(0, text_part) |
paul@19 | 191 | message = MIMEMultipart("alternative", _subparts=all_parts) |
paul@19 | 192 | message.preamble = MESSAGE_TEXT |
paul@12 | 193 | |
paul@27 | 194 | message["From"] = this_sender |
paul@13 | 195 | for sender in senders: |
paul@13 | 196 | message["To"] = sender |
paul@19 | 197 | message["Subject"] = MESSAGE_SUBJECT |
paul@12 | 198 | |
paul@12 | 199 | if "-d" in sys.argv: |
paul@12 | 200 | print message |
paul@13 | 201 | else: |
paul@27 | 202 | sendmail(this_sender, senders, message.as_string()) |
paul@0 | 203 | |
paul@16 | 204 | def to_part(method, calendar): |
paul@11 | 205 | |
paul@16 | 206 | """ |
paul@16 | 207 | Write using the given 'method', the 'calendar' details to a MIME |
paul@16 | 208 | text/calendar part. |
paul@16 | 209 | """ |
paul@10 | 210 | |
paul@16 | 211 | encoding = "utf-8" |
paul@12 | 212 | out = StringIO() |
paul@12 | 213 | try: |
paul@30 | 214 | imip_store.to_stream(out, imip_store.make_calendar(calendar, method), 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@30 | 310 | try: |
paul@30 | 311 | self.publisher = imip_store.FilePublisher() |
paul@30 | 312 | except OSError: |
paul@30 | 313 | self.publisher = None |
paul@30 | 314 | |
paul@11 | 315 | def get_items(self, name, all=True): |
paul@11 | 316 | return get_items(self.details, name, all) |
paul@11 | 317 | |
paul@11 | 318 | def get_item(self, name): |
paul@11 | 319 | return get_item(self.details, name) |
paul@5 | 320 | |
paul@10 | 321 | def get_value_map(self, name): |
paul@10 | 322 | return get_value_map(self.details, name) |
paul@10 | 323 | |
paul@11 | 324 | def get_values(self, name, all=True): |
paul@11 | 325 | return get_values(self.details, name, all) |
paul@11 | 326 | |
paul@11 | 327 | def get_value(self, name): |
paul@11 | 328 | return get_value(self.details, name) |
paul@2 | 329 | |
paul@24 | 330 | def get_utc_datetime(self, name): |
paul@24 | 331 | return get_utc_datetime(self.details, name) |
paul@24 | 332 | |
paul@10 | 333 | def filter_by_recipients(self, values): |
paul@10 | 334 | return self.recipients.intersection(map(get_address, values)) |
paul@9 | 335 | |
paul@14 | 336 | def require_organiser_and_attendees(self): |
paul@14 | 337 | attendee_map = self.get_value_map("ATTENDEE") |
paul@14 | 338 | organiser = self.get_item("ORGANIZER") |
paul@14 | 339 | |
paul@14 | 340 | # Only provide details for recipients who are also attendees. |
paul@14 | 341 | |
paul@14 | 342 | attendees = {} |
paul@14 | 343 | for attendee in map(get_uri, self.filter_by_recipients(attendee_map)): |
paul@14 | 344 | attendees[attendee] = attendee_map[attendee] |
paul@14 | 345 | |
paul@14 | 346 | if not attendees and not organiser: |
paul@14 | 347 | return None |
paul@14 | 348 | |
paul@14 | 349 | return organiser, attendees |
paul@14 | 350 | |
paul@2 | 351 | class Event(Handler): |
paul@6 | 352 | |
paul@6 | 353 | "An event handler." |
paul@6 | 354 | |
paul@2 | 355 | def add(self): |
paul@2 | 356 | pass |
paul@2 | 357 | |
paul@2 | 358 | def cancel(self): |
paul@2 | 359 | pass |
paul@2 | 360 | |
paul@2 | 361 | def counter(self): |
paul@16 | 362 | |
paul@16 | 363 | "Since this handler does not send requests, it will not handle replies." |
paul@16 | 364 | |
paul@2 | 365 | pass |
paul@2 | 366 | |
paul@2 | 367 | def declinecounter(self): |
paul@16 | 368 | |
paul@16 | 369 | """ |
paul@16 | 370 | Since this handler does not send counter proposals, it will not handle |
paul@16 | 371 | replies to such proposals. |
paul@16 | 372 | """ |
paul@16 | 373 | |
paul@2 | 374 | pass |
paul@2 | 375 | |
paul@6 | 376 | def publish(self): |
paul@6 | 377 | pass |
paul@6 | 378 | |
paul@2 | 379 | def refresh(self): |
paul@2 | 380 | pass |
paul@2 | 381 | |
paul@2 | 382 | def reply(self): |
paul@6 | 383 | |
paul@6 | 384 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 385 | |
paul@2 | 386 | pass |
paul@2 | 387 | |
paul@2 | 388 | def request(self): |
paul@14 | 389 | |
paul@14 | 390 | """ |
paul@14 | 391 | Respond to a request by preparing a reply containing accept/decline |
paul@14 | 392 | information for each indicated attendee. |
paul@16 | 393 | |
paul@16 | 394 | No support for countering requests is implemented. |
paul@14 | 395 | """ |
paul@2 | 396 | |
paul@16 | 397 | oa = self.require_organiser_and_attendees() |
paul@16 | 398 | if not oa: |
paul@16 | 399 | return None |
paul@16 | 400 | |
paul@16 | 401 | (organiser, organiser_attr), attendees = oa |
paul@16 | 402 | |
paul@16 | 403 | # Process each attendee separately. |
paul@16 | 404 | |
paul@24 | 405 | calendar = [] |
paul@24 | 406 | |
paul@16 | 407 | for attendee, attendee_attr in attendees.items(): |
paul@16 | 408 | |
paul@16 | 409 | # Check for event using UID. |
paul@16 | 410 | |
paul@16 | 411 | f = self.store.get_event(attendee, self.uid) |
paul@16 | 412 | event = f and parse_object(f, "utf-8", "VEVENT") |
paul@16 | 413 | |
paul@16 | 414 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@16 | 415 | |
paul@16 | 416 | if event: |
paul@16 | 417 | sequence = get_value(event, "SEQUENCE") |
paul@16 | 418 | dtstamp = get_value(event, "DTSTAMP") |
paul@16 | 419 | |
paul@16 | 420 | # If the request refers to an older version of the event, ignore |
paul@16 | 421 | # it. |
paul@16 | 422 | |
paul@18 | 423 | old_dtstamp = self.dtstamp < dtstamp |
paul@16 | 424 | |
paul@16 | 425 | if sequence is not None and ( |
paul@16 | 426 | int(self.sequence) < int(sequence) or |
paul@16 | 427 | int(self.sequence) == int(sequence) and old_dtstamp |
paul@16 | 428 | ) or old_dtstamp: |
paul@16 | 429 | |
paul@16 | 430 | continue |
paul@16 | 431 | |
paul@16 | 432 | # If newer than any old version, discard old details from the |
paul@16 | 433 | # free/busy record and check for suitability. |
paul@16 | 434 | |
paul@24 | 435 | dtstart = self.get_utc_datetime("DTSTART") |
paul@24 | 436 | dtend = self.get_utc_datetime("DTEND") |
paul@16 | 437 | |
paul@16 | 438 | conflict = False |
paul@16 | 439 | freebusy = self.store.get_freebusy(attendee) |
paul@16 | 440 | |
paul@16 | 441 | if freebusy: |
paul@16 | 442 | remove_period(freebusy, self.uid) |
paul@16 | 443 | conflict = period_overlaps(freebusy, (dtstart, dtend)) |
paul@16 | 444 | else: |
paul@16 | 445 | freebusy = [] |
paul@16 | 446 | |
paul@16 | 447 | # If the event can be scheduled, it is registered and a reply sent |
paul@16 | 448 | # accepting the event. (The attendee has PARTSTAT=ACCEPTED as an |
paul@16 | 449 | # attribute.) |
paul@16 | 450 | |
paul@16 | 451 | if not conflict: |
paul@16 | 452 | insert_period(freebusy, (dtstart, dtend, self.uid)) |
paul@29 | 453 | |
paul@30 | 454 | if self.get_value("TRANSP") in (None, "OPAQUE"): |
paul@29 | 455 | self.store.set_freebusy(attendee, freebusy) |
paul@29 | 456 | |
paul@30 | 457 | if self.publisher: |
paul@30 | 458 | self.publisher.set_freebusy(attendee, freebusy) |
paul@30 | 459 | |
paul@26 | 460 | self.store.set_event(attendee, self.uid, to_node( |
paul@26 | 461 | {"VEVENT" : [(self.details, {})]} |
paul@26 | 462 | )) |
paul@16 | 463 | attendee_attr["PARTSTAT"] = "ACCEPTED" |
paul@16 | 464 | |
paul@16 | 465 | # If the event cannot be scheduled, it is not registered and a reply |
paul@16 | 466 | # sent declining the event. (The attendee has PARTSTAT=DECLINED as an |
paul@16 | 467 | # attribute.) |
paul@16 | 468 | |
paul@16 | 469 | else: |
paul@16 | 470 | attendee_attr["PARTSTAT"] = "DECLINED" |
paul@16 | 471 | |
paul@16 | 472 | self.details["ATTENDEE"] = [(attendee, attendee_attr)] |
paul@26 | 473 | calendar.append(to_node( |
paul@26 | 474 | {"VEVENT" : [(self.details, {})]} |
paul@26 | 475 | )) |
paul@24 | 476 | |
paul@24 | 477 | return calendar |
paul@16 | 478 | |
paul@2 | 479 | class Freebusy(Handler): |
paul@6 | 480 | |
paul@6 | 481 | "A free/busy handler." |
paul@6 | 482 | |
paul@6 | 483 | def publish(self): |
paul@6 | 484 | pass |
paul@6 | 485 | |
paul@2 | 486 | def reply(self): |
paul@2 | 487 | |
paul@6 | 488 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 489 | |
paul@6 | 490 | pass |
paul@0 | 491 | |
paul@2 | 492 | def request(self): |
paul@6 | 493 | |
paul@6 | 494 | """ |
paul@12 | 495 | Respond to a request by preparing a reply containing free/busy |
paul@12 | 496 | information for each indicated attendee. |
paul@6 | 497 | """ |
paul@6 | 498 | |
paul@14 | 499 | oa = self.require_organiser_and_attendees() |
paul@14 | 500 | if not oa: |
paul@14 | 501 | return None |
paul@9 | 502 | |
paul@14 | 503 | (organiser, organiser_attr), attendees = oa |
paul@5 | 504 | |
paul@12 | 505 | # Construct an appropriate fragment. |
paul@12 | 506 | |
paul@12 | 507 | calendar = [] |
paul@12 | 508 | cwrite = calendar.append |
paul@2 | 509 | |
paul@12 | 510 | # Get the details for each attendee. |
paul@5 | 511 | |
paul@14 | 512 | for attendee, attendee_attr in attendees.items(): |
paul@12 | 513 | freebusy = self.store.get_freebusy(attendee) |
paul@2 | 514 | |
paul@12 | 515 | if freebusy: |
paul@12 | 516 | record = [] |
paul@12 | 517 | rwrite = record.append |
paul@12 | 518 | |
paul@12 | 519 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@14 | 520 | rwrite(("ATTENDEE", attendee_attr, attendee)) |
paul@12 | 521 | rwrite(("UID", {}, self.uid)) |
paul@5 | 522 | |
paul@16 | 523 | for start, end, uid in freebusy: |
paul@29 | 524 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end])) |
paul@5 | 525 | |
paul@12 | 526 | cwrite(("VFREEBUSY", {}, record)) |
paul@5 | 527 | |
paul@12 | 528 | # Return the reply. |
paul@8 | 529 | |
paul@12 | 530 | return calendar |
paul@0 | 531 | |
paul@2 | 532 | class Journal(Handler): |
paul@6 | 533 | |
paul@6 | 534 | "A journal entry handler." |
paul@6 | 535 | |
paul@2 | 536 | def add(self): |
paul@2 | 537 | pass |
paul@2 | 538 | |
paul@2 | 539 | def cancel(self): |
paul@2 | 540 | pass |
paul@2 | 541 | |
paul@6 | 542 | def publish(self): |
paul@6 | 543 | pass |
paul@6 | 544 | |
paul@2 | 545 | class Todo(Handler): |
paul@6 | 546 | |
paul@6 | 547 | "A to-do item handler." |
paul@6 | 548 | |
paul@2 | 549 | def add(self): |
paul@2 | 550 | pass |
paul@2 | 551 | |
paul@2 | 552 | def cancel(self): |
paul@2 | 553 | pass |
paul@2 | 554 | |
paul@2 | 555 | def counter(self): |
paul@16 | 556 | |
paul@16 | 557 | "Since this handler does not send requests, it will not handle replies." |
paul@16 | 558 | |
paul@2 | 559 | pass |
paul@0 | 560 | |
paul@2 | 561 | def declinecounter(self): |
paul@16 | 562 | |
paul@16 | 563 | """ |
paul@16 | 564 | Since this handler does not send counter proposals, it will not handle |
paul@16 | 565 | replies to such proposals. |
paul@16 | 566 | """ |
paul@16 | 567 | |
paul@2 | 568 | pass |
paul@2 | 569 | |
paul@6 | 570 | def publish(self): |
paul@6 | 571 | pass |
paul@6 | 572 | |
paul@2 | 573 | def refresh(self): |
paul@2 | 574 | pass |
paul@2 | 575 | |
paul@2 | 576 | def reply(self): |
paul@6 | 577 | |
paul@6 | 578 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 579 | |
paul@2 | 580 | pass |
paul@2 | 581 | |
paul@2 | 582 | def request(self): |
paul@2 | 583 | pass |
paul@2 | 584 | |
paul@2 | 585 | # Handler registry. |
paul@0 | 586 | |
paul@2 | 587 | handlers = [ |
paul@2 | 588 | ("VFREEBUSY", Freebusy), |
paul@2 | 589 | ("VEVENT", Event), |
paul@2 | 590 | ("VTODO", Todo), |
paul@2 | 591 | ("VJOURNAL", Journal), |
paul@2 | 592 | ] |
paul@0 | 593 | |
paul@2 | 594 | methods = { |
paul@2 | 595 | "ADD" : lambda handler: handler.add, |
paul@2 | 596 | "CANCEL" : lambda handler: handler.cancel, |
paul@2 | 597 | "COUNTER" : lambda handler: handler.counter, |
paul@2 | 598 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@2 | 599 | "PUBLISH" : lambda handler: handler.publish, |
paul@2 | 600 | "REFRESH" : lambda handler: handler.refresh, |
paul@2 | 601 | "REPLY" : lambda handler: handler.reply, |
paul@2 | 602 | "REQUEST" : lambda handler: handler.request, |
paul@2 | 603 | } |
paul@0 | 604 | |
paul@20 | 605 | response_methods = { |
paul@20 | 606 | "REQUEST" : "REPLY", |
paul@20 | 607 | } |
paul@11 | 608 | |
paul@11 | 609 | def main(): |
paul@11 | 610 | |
paul@27 | 611 | "Interpret program arguments and process input." |
paul@27 | 612 | |
paul@27 | 613 | # Obtain the different kinds of recipients plus sender address. |
paul@9 | 614 | |
paul@11 | 615 | original_recipients = [] |
paul@11 | 616 | recipients = [] |
paul@27 | 617 | senders = [] |
paul@9 | 618 | |
paul@11 | 619 | l = [] |
paul@9 | 620 | |
paul@11 | 621 | for arg in sys.argv[1:]: |
paul@27 | 622 | |
paul@27 | 623 | # Switch to collecting recipients. |
paul@27 | 624 | |
paul@11 | 625 | if arg == "-o": |
paul@11 | 626 | l = original_recipients |
paul@11 | 627 | elif arg == "-r": |
paul@11 | 628 | l = recipients |
paul@27 | 629 | |
paul@27 | 630 | # Switch to collecting senders. |
paul@27 | 631 | |
paul@27 | 632 | elif arg == "-s": |
paul@27 | 633 | l = senders |
paul@27 | 634 | |
paul@27 | 635 | # Ignore debugging options. |
paul@27 | 636 | |
paul@11 | 637 | elif arg == "-d": |
paul@11 | 638 | pass |
paul@11 | 639 | else: |
paul@11 | 640 | l.append(arg) |
paul@11 | 641 | |
paul@27 | 642 | process(sys.stdin, original_recipients, recipients, senders and senders[0] or MESSAGE_SENDER) |
paul@9 | 643 | |
paul@11 | 644 | if __name__ == "__main__": |
paul@11 | 645 | if "-d" in sys.argv[1:]: |
paul@11 | 646 | main() |
paul@9 | 647 | else: |
paul@11 | 648 | try: |
paul@11 | 649 | main() |
paul@11 | 650 | except SystemExit, value: |
paul@11 | 651 | sys.exit(value) |
paul@11 | 652 | except Exception, exc: |
paul@11 | 653 | type, value, tb = sys.exc_info() |
paul@11 | 654 | print >>sys.stderr, "Exception %s at %d" % (exc, tb.tb_lineno) |
paul@11 | 655 | sys.exit(EX_TEMPFAIL) |
paul@11 | 656 | sys.exit(0) |
paul@0 | 657 | |
paul@0 | 658 | # vim: tabstop=4 expandtab shiftwidth=4 |