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