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