paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | from email import message_from_file |
paul@8 | 4 | from email.mime.text import MIMEText |
paul@8 | 5 | from smtplib import SMTP |
paul@8 | 6 | from vCalendar import parse, iterwrite, ParseError, SECTION_TYPES |
paul@2 | 7 | import imip_store |
paul@0 | 8 | import sys |
paul@0 | 9 | |
paul@0 | 10 | try: |
paul@0 | 11 | from cStringIO import StringIO |
paul@0 | 12 | except ImportError: |
paul@0 | 13 | from StringIO import StringIO |
paul@0 | 14 | |
paul@0 | 15 | # Postfix exit codes. |
paul@0 | 16 | |
paul@0 | 17 | EX_USAGE = 64 |
paul@0 | 18 | EX_DATAERR = 65 |
paul@0 | 19 | EX_NOINPUT = 66 |
paul@0 | 20 | EX_NOUSER = 67 |
paul@0 | 21 | EX_NOHOST = 68 |
paul@0 | 22 | EX_UNAVAILABLE = 69 |
paul@0 | 23 | EX_SOFTWARE = 70 |
paul@0 | 24 | EX_OSERR = 71 |
paul@0 | 25 | EX_OSFILE = 72 |
paul@0 | 26 | EX_CANTCREAT = 73 |
paul@0 | 27 | EX_IOERR = 74 |
paul@0 | 28 | EX_TEMPFAIL = 75 |
paul@0 | 29 | EX_PROTOCOL = 76 |
paul@0 | 30 | EX_NOPERM = 77 |
paul@0 | 31 | EX_CONFIG = 78 |
paul@0 | 32 | |
paul@0 | 33 | # Permitted iTIP content types. |
paul@0 | 34 | |
paul@0 | 35 | itip_content_types = [ |
paul@0 | 36 | "text/calendar", # from RFC 6047 |
paul@0 | 37 | "text/x-vcalendar", "application/ics", # other possibilities |
paul@0 | 38 | ] |
paul@0 | 39 | |
paul@8 | 40 | # Sending of outgoing messages. |
paul@8 | 41 | |
paul@8 | 42 | def sendmail(sender, recipients, data): |
paul@8 | 43 | smtp = SMTP("localhost") |
paul@8 | 44 | smtp.sendmail(sender, recipients, data) |
paul@8 | 45 | smtp.quit() |
paul@8 | 46 | |
paul@8 | 47 | # Processing of incoming messages. |
paul@8 | 48 | |
paul@9 | 49 | def process(f, original_recipients, recipients): |
paul@8 | 50 | |
paul@9 | 51 | """ |
paul@9 | 52 | Process content from the stream 'f' accompanied by the given |
paul@9 | 53 | 'original_recipients' and 'recipients'. |
paul@9 | 54 | """ |
paul@8 | 55 | |
paul@0 | 56 | msg = message_from_file(f) |
paul@1 | 57 | print >>open("/tmp/imip.txt", "a"), "----" |
paul@9 | 58 | print >>open("/tmp/imip.txt", "a"), original_recipients, recipients |
paul@1 | 59 | print >>open("/tmp/imip.txt", "a"), "----" |
paul@1 | 60 | print >>open("/tmp/imip.txt", "a"), msg.as_string() |
paul@1 | 61 | print >>open("/tmp/imip.txt", "a") |
paul@0 | 62 | |
paul@0 | 63 | # Handle messages with iTIP parts. |
paul@0 | 64 | |
paul@0 | 65 | for part in msg.walk(): |
paul@0 | 66 | if part.get_content_type() in itip_content_types and \ |
paul@0 | 67 | part.get_param("method"): |
paul@0 | 68 | |
paul@9 | 69 | handle_itip_part(part, original_recipients) |
paul@0 | 70 | |
paul@0 | 71 | def get_itip_elements(elements): |
paul@0 | 72 | d = {} |
paul@0 | 73 | for name, attr, value in elements: |
paul@8 | 74 | if not d.has_key(name): |
paul@8 | 75 | d[name] = [] |
paul@8 | 76 | if name in SECTION_TYPES: |
paul@11 | 77 | d[name].append((get_itip_elements(value), attr)) |
paul@0 | 78 | else: |
paul@11 | 79 | d[name].append((value, attr)) |
paul@0 | 80 | return d |
paul@0 | 81 | |
paul@11 | 82 | def get_items(d, name, all=True): |
paul@5 | 83 | if d.has_key(name): |
paul@5 | 84 | values = d[name] |
paul@11 | 85 | if not all and len(values) == 1: |
paul@5 | 86 | return values[0] |
paul@5 | 87 | else: |
paul@5 | 88 | return values |
paul@5 | 89 | else: |
paul@5 | 90 | return None |
paul@5 | 91 | |
paul@11 | 92 | def get_item(d, name): |
paul@11 | 93 | return get_items(d, name, False) |
paul@11 | 94 | |
paul@10 | 95 | def get_value_map(d, name): |
paul@11 | 96 | items = get_items(d, name) |
paul@10 | 97 | if items: |
paul@11 | 98 | return dict(items) |
paul@10 | 99 | else: |
paul@10 | 100 | return {} |
paul@10 | 101 | |
paul@11 | 102 | def get_values(d, name, all=True): |
paul@0 | 103 | if d.has_key(name): |
paul@2 | 104 | values = d[name] |
paul@11 | 105 | if not all and len(values) == 1: |
paul@11 | 106 | return values[0][0] |
paul@2 | 107 | else: |
paul@11 | 108 | return map(lambda x: x[0], values) |
paul@0 | 109 | else: |
paul@0 | 110 | return None |
paul@0 | 111 | |
paul@11 | 112 | def get_value(d, name): |
paul@11 | 113 | return get_values(d, name, False) |
paul@10 | 114 | |
paul@10 | 115 | def get_address(value): |
paul@10 | 116 | return value.startswith("mailto:") and value[7:] or value |
paul@10 | 117 | |
paul@10 | 118 | def get_uri(value): |
paul@10 | 119 | return value.startswith("mailto:") and value or "mailto:%s" % value |
paul@10 | 120 | |
paul@9 | 121 | def handle_itip_part(part, recipients): |
paul@0 | 122 | method = part.get_param("method") |
paul@0 | 123 | |
paul@8 | 124 | # Decode the data and parse it. |
paul@8 | 125 | |
paul@0 | 126 | f = StringIO(part.get_payload(decode=True)) |
paul@8 | 127 | |
paul@8 | 128 | try: |
paul@8 | 129 | doctype, attrs, elements = parse(f, encoding=part.get_content_charset()) |
paul@8 | 130 | except (ParseError, ValueError): |
paul@8 | 131 | sys.exit(EX_DATAERR) |
paul@8 | 132 | |
paul@8 | 133 | # Only handle calendar information. |
paul@0 | 134 | |
paul@0 | 135 | if doctype == "VCALENDAR": |
paul@0 | 136 | itip = get_itip_elements(elements) |
paul@0 | 137 | |
paul@8 | 138 | # Require consistency between declared and employed methods. |
paul@8 | 139 | |
paul@0 | 140 | if get_value(itip, "METHOD") == method: |
paul@2 | 141 | |
paul@8 | 142 | # Look for different kinds of sections. |
paul@8 | 143 | |
paul@2 | 144 | for name, cls in handlers: |
paul@11 | 145 | for details in get_values(itip, name) or []: |
paul@8 | 146 | |
paul@8 | 147 | # Dispatch to a handler and obtain any response. |
paul@2 | 148 | |
paul@9 | 149 | handler = cls(details, recipients) |
paul@8 | 150 | part = methods[method](handler)() |
paul@8 | 151 | |
paul@8 | 152 | if part: |
paul@8 | 153 | print >>open("/tmp/imip.txt", "a"), part.as_string() |
paul@2 | 154 | |
paul@2 | 155 | class Handler: |
paul@8 | 156 | |
paul@8 | 157 | "General handler support." |
paul@8 | 158 | |
paul@9 | 159 | def __init__(self, details, recipients): |
paul@9 | 160 | |
paul@9 | 161 | """ |
paul@9 | 162 | Initialise the handler with the 'details' of a calendar object and the |
paul@9 | 163 | 'recipients' of the object. |
paul@9 | 164 | """ |
paul@9 | 165 | |
paul@2 | 166 | self.details = details |
paul@9 | 167 | self.recipients = set(recipients) |
paul@9 | 168 | |
paul@2 | 169 | self.uid = get_value(details, "UID") |
paul@2 | 170 | self.sequence = get_value(details, "SEQUENCE") |
paul@2 | 171 | self.store = imip_store.FileStore() |
paul@2 | 172 | |
paul@11 | 173 | def get_items(self, name, all=True): |
paul@11 | 174 | return get_items(self.details, name, all) |
paul@11 | 175 | |
paul@11 | 176 | def get_item(self, name): |
paul@11 | 177 | return get_item(self.details, name) |
paul@5 | 178 | |
paul@10 | 179 | def get_value_map(self, name): |
paul@10 | 180 | return get_value_map(self.details, name) |
paul@10 | 181 | |
paul@11 | 182 | def get_values(self, name, all=True): |
paul@11 | 183 | return get_values(self.details, name, all) |
paul@11 | 184 | |
paul@11 | 185 | def get_value(self, name): |
paul@11 | 186 | return get_value(self.details, name) |
paul@2 | 187 | |
paul@10 | 188 | def filter_by_recipients(self, values): |
paul@10 | 189 | return self.recipients.intersection(map(get_address, values)) |
paul@9 | 190 | |
paul@2 | 191 | class Event(Handler): |
paul@6 | 192 | |
paul@6 | 193 | "An event handler." |
paul@6 | 194 | |
paul@2 | 195 | def add(self): |
paul@2 | 196 | pass |
paul@2 | 197 | |
paul@2 | 198 | def cancel(self): |
paul@2 | 199 | pass |
paul@2 | 200 | |
paul@2 | 201 | def counter(self): |
paul@2 | 202 | pass |
paul@2 | 203 | |
paul@2 | 204 | def declinecounter(self): |
paul@2 | 205 | pass |
paul@2 | 206 | |
paul@6 | 207 | def publish(self): |
paul@6 | 208 | pass |
paul@6 | 209 | |
paul@2 | 210 | def refresh(self): |
paul@2 | 211 | pass |
paul@2 | 212 | |
paul@2 | 213 | def reply(self): |
paul@6 | 214 | |
paul@6 | 215 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 216 | |
paul@2 | 217 | pass |
paul@2 | 218 | |
paul@2 | 219 | def request(self): |
paul@2 | 220 | pass |
paul@2 | 221 | |
paul@2 | 222 | class Freebusy(Handler): |
paul@6 | 223 | |
paul@6 | 224 | "A free/busy handler." |
paul@6 | 225 | |
paul@6 | 226 | def publish(self): |
paul@6 | 227 | pass |
paul@6 | 228 | |
paul@2 | 229 | def reply(self): |
paul@2 | 230 | |
paul@6 | 231 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 232 | |
paul@6 | 233 | pass |
paul@0 | 234 | |
paul@2 | 235 | def request(self): |
paul@6 | 236 | |
paul@6 | 237 | """ |
paul@6 | 238 | Respond to a request by sending a reply containing free/busy information |
paul@6 | 239 | for each indicated attendee. |
paul@6 | 240 | """ |
paul@6 | 241 | |
paul@10 | 242 | attendee_map = self.get_value_map("ATTENDEE") |
paul@11 | 243 | organiser = self.get_item("ORGANIZER") |
paul@5 | 244 | |
paul@9 | 245 | # Only provide details for recipients who are also attendees. |
paul@9 | 246 | |
paul@10 | 247 | attendees = map(get_uri, self.filter_by_recipients(attendee_map)) |
paul@9 | 248 | |
paul@2 | 249 | if not attendees and not organiser: |
paul@2 | 250 | return |
paul@2 | 251 | |
paul@11 | 252 | organiser, organiser_attr = organiser |
paul@5 | 253 | |
paul@2 | 254 | # Get the details for the attendee. |
paul@2 | 255 | |
paul@8 | 256 | out = StringIO() |
paul@5 | 257 | try: |
paul@8 | 258 | w = iterwrite(out, encoding="utf-8") |
paul@5 | 259 | |
paul@5 | 260 | calendar = [] |
paul@5 | 261 | cwrite = calendar.append |
paul@5 | 262 | cwrite(("METHOD", {}, "REPLY")) |
paul@5 | 263 | cwrite(("VERSION", {}, "2.0")) |
paul@2 | 264 | |
paul@10 | 265 | for attendee in attendees: |
paul@5 | 266 | freebusy = self.store.get_freebusy(attendee) |
paul@5 | 267 | if freebusy: |
paul@5 | 268 | record = [] |
paul@5 | 269 | rwrite = record.append |
paul@5 | 270 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@10 | 271 | rwrite(("ATTENDEE", attendee_map[attendee], attendee)) |
paul@5 | 272 | rwrite(("UID", {}, self.uid)) |
paul@5 | 273 | |
paul@5 | 274 | for start, end in freebusy: |
paul@5 | 275 | rwrite(("FREEBUSY", {}, [start, end])) |
paul@5 | 276 | |
paul@5 | 277 | cwrite(("VFREEBUSY", {}, record)) |
paul@5 | 278 | |
paul@5 | 279 | # Send a reply with the information. |
paul@5 | 280 | |
paul@5 | 281 | w.write("VCALENDAR", {}, calendar) |
paul@5 | 282 | |
paul@8 | 283 | return MIMEText(out.getvalue(), "calendar", "utf-8") |
paul@8 | 284 | |
paul@5 | 285 | finally: |
paul@5 | 286 | out.close() |
paul@0 | 287 | |
paul@2 | 288 | class Journal(Handler): |
paul@6 | 289 | |
paul@6 | 290 | "A journal entry handler." |
paul@6 | 291 | |
paul@2 | 292 | def add(self): |
paul@2 | 293 | pass |
paul@2 | 294 | |
paul@2 | 295 | def cancel(self): |
paul@2 | 296 | pass |
paul@2 | 297 | |
paul@6 | 298 | def publish(self): |
paul@6 | 299 | pass |
paul@6 | 300 | |
paul@2 | 301 | class Todo(Handler): |
paul@6 | 302 | |
paul@6 | 303 | "A to-do item handler." |
paul@6 | 304 | |
paul@2 | 305 | def add(self): |
paul@2 | 306 | pass |
paul@2 | 307 | |
paul@2 | 308 | def cancel(self): |
paul@2 | 309 | pass |
paul@2 | 310 | |
paul@2 | 311 | def counter(self): |
paul@2 | 312 | pass |
paul@0 | 313 | |
paul@2 | 314 | def declinecounter(self): |
paul@2 | 315 | pass |
paul@2 | 316 | |
paul@6 | 317 | def publish(self): |
paul@6 | 318 | pass |
paul@6 | 319 | |
paul@2 | 320 | def refresh(self): |
paul@2 | 321 | pass |
paul@2 | 322 | |
paul@2 | 323 | def reply(self): |
paul@6 | 324 | |
paul@6 | 325 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 326 | |
paul@2 | 327 | pass |
paul@2 | 328 | |
paul@2 | 329 | def request(self): |
paul@2 | 330 | pass |
paul@2 | 331 | |
paul@2 | 332 | # Handler registry. |
paul@0 | 333 | |
paul@2 | 334 | handlers = [ |
paul@2 | 335 | ("VFREEBUSY", Freebusy), |
paul@2 | 336 | ("VEVENT", Event), |
paul@2 | 337 | ("VTODO", Todo), |
paul@2 | 338 | ("VJOURNAL", Journal), |
paul@2 | 339 | ] |
paul@0 | 340 | |
paul@2 | 341 | methods = { |
paul@2 | 342 | "ADD" : lambda handler: handler.add, |
paul@2 | 343 | "CANCEL" : lambda handler: handler.cancel, |
paul@2 | 344 | "COUNTER" : lambda handler: handler.counter, |
paul@2 | 345 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@2 | 346 | "PUBLISH" : lambda handler: handler.publish, |
paul@2 | 347 | "REFRESH" : lambda handler: handler.refresh, |
paul@2 | 348 | "REPLY" : lambda handler: handler.reply, |
paul@2 | 349 | "REQUEST" : lambda handler: handler.request, |
paul@2 | 350 | } |
paul@0 | 351 | |
paul@11 | 352 | |
paul@11 | 353 | def main(): |
paul@11 | 354 | |
paul@11 | 355 | # Obtain the different kinds of recipients. |
paul@9 | 356 | |
paul@11 | 357 | original_recipients = [] |
paul@11 | 358 | recipients = [] |
paul@9 | 359 | |
paul@11 | 360 | l = [] |
paul@9 | 361 | |
paul@11 | 362 | for arg in sys.argv[1:]: |
paul@11 | 363 | if arg == "-o": |
paul@11 | 364 | l = original_recipients |
paul@11 | 365 | elif arg == "-r": |
paul@11 | 366 | l = recipients |
paul@11 | 367 | elif arg == "-d": |
paul@11 | 368 | pass |
paul@11 | 369 | else: |
paul@11 | 370 | l.append(arg) |
paul@11 | 371 | |
paul@11 | 372 | process(sys.stdin, original_recipients, recipients) |
paul@9 | 373 | |
paul@11 | 374 | if __name__ == "__main__": |
paul@11 | 375 | if "-d" in sys.argv[1:]: |
paul@11 | 376 | main() |
paul@9 | 377 | else: |
paul@11 | 378 | try: |
paul@11 | 379 | main() |
paul@11 | 380 | except SystemExit, value: |
paul@11 | 381 | sys.exit(value) |
paul@11 | 382 | except Exception, exc: |
paul@11 | 383 | type, value, tb = sys.exc_info() |
paul@11 | 384 | print >>sys.stderr, "Exception %s at %d" % (exc, tb.tb_lineno) |
paul@11 | 385 | sys.exit(EX_TEMPFAIL) |
paul@11 | 386 | sys.exit(0) |
paul@0 | 387 | |
paul@0 | 388 | # vim: tabstop=4 expandtab shiftwidth=4 |