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