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@8 | 77 | d[name].append((attr, get_itip_elements(value))) |
paul@0 | 78 | else: |
paul@2 | 79 | d[name].append((attr, value)) |
paul@0 | 80 | return d |
paul@0 | 81 | |
paul@5 | 82 | def get_attr_value(d, name, single=True): |
paul@5 | 83 | if d.has_key(name): |
paul@5 | 84 | values = d[name] |
paul@8 | 85 | if single 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@2 | 92 | def get_value(d, name, single=True): |
paul@0 | 93 | if d.has_key(name): |
paul@2 | 94 | values = d[name] |
paul@8 | 95 | if single and len(values) == 1: |
paul@2 | 96 | return values[0][1] |
paul@2 | 97 | else: |
paul@2 | 98 | return map(lambda x: x[1], values) |
paul@0 | 99 | else: |
paul@0 | 100 | return None |
paul@0 | 101 | |
paul@9 | 102 | def handle_itip_part(part, recipients): |
paul@0 | 103 | method = part.get_param("method") |
paul@0 | 104 | |
paul@8 | 105 | # Decode the data and parse it. |
paul@8 | 106 | |
paul@0 | 107 | f = StringIO(part.get_payload(decode=True)) |
paul@8 | 108 | |
paul@8 | 109 | try: |
paul@8 | 110 | doctype, attrs, elements = parse(f, encoding=part.get_content_charset()) |
paul@8 | 111 | except (ParseError, ValueError): |
paul@8 | 112 | sys.exit(EX_DATAERR) |
paul@8 | 113 | |
paul@8 | 114 | # Only handle calendar information. |
paul@0 | 115 | |
paul@0 | 116 | if doctype == "VCALENDAR": |
paul@0 | 117 | itip = get_itip_elements(elements) |
paul@0 | 118 | |
paul@8 | 119 | # Require consistency between declared and employed methods. |
paul@8 | 120 | |
paul@0 | 121 | if get_value(itip, "METHOD") == method: |
paul@2 | 122 | |
paul@8 | 123 | # Look for different kinds of sections. |
paul@8 | 124 | |
paul@2 | 125 | for name, cls in handlers: |
paul@8 | 126 | for details in get_value(itip, name, False) or []: |
paul@8 | 127 | |
paul@8 | 128 | # Dispatch to a handler and obtain any response. |
paul@2 | 129 | |
paul@9 | 130 | handler = cls(details, recipients) |
paul@8 | 131 | part = methods[method](handler)() |
paul@8 | 132 | |
paul@8 | 133 | if part: |
paul@8 | 134 | print >>open("/tmp/imip.txt", "a"), part.as_string() |
paul@2 | 135 | |
paul@2 | 136 | class Handler: |
paul@8 | 137 | |
paul@8 | 138 | "General handler support." |
paul@8 | 139 | |
paul@9 | 140 | def __init__(self, details, recipients): |
paul@9 | 141 | |
paul@9 | 142 | """ |
paul@9 | 143 | Initialise the handler with the 'details' of a calendar object and the |
paul@9 | 144 | 'recipients' of the object. |
paul@9 | 145 | """ |
paul@9 | 146 | |
paul@2 | 147 | self.details = details |
paul@9 | 148 | self.recipients = set(recipients) |
paul@9 | 149 | |
paul@2 | 150 | self.uid = get_value(details, "UID") |
paul@2 | 151 | self.sequence = get_value(details, "SEQUENCE") |
paul@2 | 152 | self.store = imip_store.FileStore() |
paul@2 | 153 | |
paul@5 | 154 | def get_attr_value(self, name, single=True): |
paul@5 | 155 | return get_attr_value(self.details, name, single) |
paul@5 | 156 | |
paul@5 | 157 | def get_value(self, name, single=True): |
paul@2 | 158 | return get_value(self.details, name, single) |
paul@2 | 159 | |
paul@9 | 160 | def filter_by_recipients(self, attr_values): |
paul@9 | 161 | return [a for attr, value in attendees if value in self.recipients] |
paul@9 | 162 | |
paul@2 | 163 | class Event(Handler): |
paul@6 | 164 | |
paul@6 | 165 | "An event handler." |
paul@6 | 166 | |
paul@2 | 167 | def add(self): |
paul@2 | 168 | pass |
paul@2 | 169 | |
paul@2 | 170 | def cancel(self): |
paul@2 | 171 | pass |
paul@2 | 172 | |
paul@2 | 173 | def counter(self): |
paul@2 | 174 | pass |
paul@2 | 175 | |
paul@2 | 176 | def declinecounter(self): |
paul@2 | 177 | pass |
paul@2 | 178 | |
paul@6 | 179 | def publish(self): |
paul@6 | 180 | pass |
paul@6 | 181 | |
paul@2 | 182 | def refresh(self): |
paul@2 | 183 | pass |
paul@2 | 184 | |
paul@2 | 185 | def reply(self): |
paul@6 | 186 | |
paul@6 | 187 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 188 | |
paul@2 | 189 | pass |
paul@2 | 190 | |
paul@2 | 191 | def request(self): |
paul@2 | 192 | pass |
paul@2 | 193 | |
paul@2 | 194 | class Freebusy(Handler): |
paul@6 | 195 | |
paul@6 | 196 | "A free/busy handler." |
paul@6 | 197 | |
paul@6 | 198 | def publish(self): |
paul@6 | 199 | pass |
paul@6 | 200 | |
paul@2 | 201 | def reply(self): |
paul@2 | 202 | |
paul@6 | 203 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 204 | |
paul@6 | 205 | pass |
paul@0 | 206 | |
paul@2 | 207 | def request(self): |
paul@6 | 208 | |
paul@6 | 209 | """ |
paul@6 | 210 | Respond to a request by sending a reply containing free/busy information |
paul@6 | 211 | for each indicated attendee. |
paul@6 | 212 | """ |
paul@6 | 213 | |
paul@5 | 214 | attendees = self.get_attr_value("ATTENDEE", False) |
paul@5 | 215 | organiser = self.get_attr_value("ORGANIZER") |
paul@5 | 216 | |
paul@9 | 217 | # Only provide details for recipients who are also attendees. |
paul@9 | 218 | |
paul@9 | 219 | attendees = self.filter_by_recipients(attendees) |
paul@9 | 220 | |
paul@2 | 221 | if not attendees and not organiser: |
paul@2 | 222 | return |
paul@2 | 223 | |
paul@5 | 224 | organiser_attr, organiser = organiser |
paul@5 | 225 | |
paul@2 | 226 | # Get the details for the attendee. |
paul@2 | 227 | |
paul@8 | 228 | out = StringIO() |
paul@5 | 229 | try: |
paul@8 | 230 | w = iterwrite(out, encoding="utf-8") |
paul@5 | 231 | |
paul@5 | 232 | calendar = [] |
paul@5 | 233 | cwrite = calendar.append |
paul@5 | 234 | cwrite(("METHOD", {}, "REPLY")) |
paul@5 | 235 | cwrite(("VERSION", {}, "2.0")) |
paul@2 | 236 | |
paul@5 | 237 | for attendee_attr, attendee in attendees: |
paul@5 | 238 | freebusy = self.store.get_freebusy(attendee) |
paul@5 | 239 | if freebusy: |
paul@5 | 240 | record = [] |
paul@5 | 241 | rwrite = record.append |
paul@5 | 242 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@5 | 243 | rwrite(("ATTENDEE", attendee_attr, attendee)) |
paul@5 | 244 | rwrite(("UID", {}, self.uid)) |
paul@5 | 245 | |
paul@5 | 246 | for start, end in freebusy: |
paul@5 | 247 | rwrite(("FREEBUSY", {}, [start, end])) |
paul@5 | 248 | |
paul@5 | 249 | cwrite(("VFREEBUSY", {}, record)) |
paul@5 | 250 | |
paul@5 | 251 | # Send a reply with the information. |
paul@5 | 252 | |
paul@5 | 253 | w.write("VCALENDAR", {}, calendar) |
paul@5 | 254 | |
paul@8 | 255 | return MIMEText(out.getvalue(), "calendar", "utf-8") |
paul@8 | 256 | |
paul@5 | 257 | finally: |
paul@5 | 258 | out.close() |
paul@0 | 259 | |
paul@2 | 260 | class Journal(Handler): |
paul@6 | 261 | |
paul@6 | 262 | "A journal entry handler." |
paul@6 | 263 | |
paul@2 | 264 | def add(self): |
paul@2 | 265 | pass |
paul@2 | 266 | |
paul@2 | 267 | def cancel(self): |
paul@2 | 268 | pass |
paul@2 | 269 | |
paul@6 | 270 | def publish(self): |
paul@6 | 271 | pass |
paul@6 | 272 | |
paul@2 | 273 | class Todo(Handler): |
paul@6 | 274 | |
paul@6 | 275 | "A to-do item handler." |
paul@6 | 276 | |
paul@2 | 277 | def add(self): |
paul@2 | 278 | pass |
paul@2 | 279 | |
paul@2 | 280 | def cancel(self): |
paul@2 | 281 | pass |
paul@2 | 282 | |
paul@2 | 283 | def counter(self): |
paul@2 | 284 | pass |
paul@0 | 285 | |
paul@2 | 286 | def declinecounter(self): |
paul@2 | 287 | pass |
paul@2 | 288 | |
paul@6 | 289 | def publish(self): |
paul@6 | 290 | pass |
paul@6 | 291 | |
paul@2 | 292 | def refresh(self): |
paul@2 | 293 | pass |
paul@2 | 294 | |
paul@2 | 295 | def reply(self): |
paul@6 | 296 | |
paul@6 | 297 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 298 | |
paul@2 | 299 | pass |
paul@2 | 300 | |
paul@2 | 301 | def request(self): |
paul@2 | 302 | pass |
paul@2 | 303 | |
paul@2 | 304 | # Handler registry. |
paul@0 | 305 | |
paul@2 | 306 | handlers = [ |
paul@2 | 307 | ("VFREEBUSY", Freebusy), |
paul@2 | 308 | ("VEVENT", Event), |
paul@2 | 309 | ("VTODO", Todo), |
paul@2 | 310 | ("VJOURNAL", Journal), |
paul@2 | 311 | ] |
paul@0 | 312 | |
paul@2 | 313 | methods = { |
paul@2 | 314 | "ADD" : lambda handler: handler.add, |
paul@2 | 315 | "CANCEL" : lambda handler: handler.cancel, |
paul@2 | 316 | "COUNTER" : lambda handler: handler.counter, |
paul@2 | 317 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@2 | 318 | "PUBLISH" : lambda handler: handler.publish, |
paul@2 | 319 | "REFRESH" : lambda handler: handler.refresh, |
paul@2 | 320 | "REPLY" : lambda handler: handler.reply, |
paul@2 | 321 | "REQUEST" : lambda handler: handler.request, |
paul@2 | 322 | } |
paul@0 | 323 | |
paul@0 | 324 | if __name__ == "__main__": |
paul@9 | 325 | try: |
paul@9 | 326 | # Obtain the different kinds of recipients. |
paul@9 | 327 | |
paul@9 | 328 | original_recipients = [] |
paul@9 | 329 | recipients = [] |
paul@9 | 330 | |
paul@9 | 331 | l = [] |
paul@9 | 332 | |
paul@9 | 333 | for arg in sys.argv[1:]: |
paul@9 | 334 | if arg == "-o": |
paul@9 | 335 | l = original_recipients |
paul@9 | 336 | elif arg == "-r": |
paul@9 | 337 | l = recipients |
paul@9 | 338 | else: |
paul@9 | 339 | l.append(arg) |
paul@9 | 340 | |
paul@9 | 341 | process(sys.stdin, original_recipients, recipients) |
paul@9 | 342 | except: |
paul@9 | 343 | sys.exit(EX_SOFTWARE) |
paul@9 | 344 | else: |
paul@9 | 345 | sys.exit(0) |
paul@0 | 346 | |
paul@0 | 347 | # vim: tabstop=4 expandtab shiftwidth=4 |