1.1 --- a/imip_agent.py Sun Sep 21 23:50:20 2014 +0200
1.2 +++ b/imip_agent.py Mon Sep 22 16:34:36 2014 +0200
1.3 @@ -2,6 +2,7 @@
1.4
1.5 from email import message_from_file
1.6 from vCalendar import parse
1.7 +import imip_store
1.8 import sys
1.9
1.10 try:
1.11 @@ -56,13 +57,20 @@
1.12 if isinstance(value, list):
1.13 d[name] = attr, get_itip_elements(value)
1.14 else:
1.15 - d[name] = attr, value
1.16 + if not d.has_key(name):
1.17 + d[name] = []
1.18 + d[name].append((attr, value))
1.19 return d
1.20
1.21 -def get_value(d, name):
1.22 +def get_value(d, name, single=True):
1.23 if d.has_key(name):
1.24 - attr, value = d[name]
1.25 - return value
1.26 + values = d[name]
1.27 + if isinstance(values, tuple):
1.28 + return values[1]
1.29 + elif single and len(values) == 1:
1.30 + return values[0][1]
1.31 + else:
1.32 + return map(lambda x: x[1], values)
1.33 else:
1.34 return None
1.35
1.36 @@ -76,30 +84,123 @@
1.37 itip = get_itip_elements(elements)
1.38
1.39 if get_value(itip, "METHOD") == method:
1.40 - for name, handler in [
1.41 - ("VFREEBUSY", handle_itip_freebusy),
1.42 - ("VEVENT", handle_itip_event),
1.43 - ("VTODO", handle_itip_todo),
1.44 - ("VJOURNAL", handle_itip_journal),
1.45 - ]:
1.46 +
1.47 + for name, cls in handlers:
1.48 + details = get_value(itip, name)
1.49 +
1.50 + if details:
1.51 + print >>open("/tmp/imip.txt", "a"), details
1.52 + handler = cls(details)
1.53 + print >>open("/tmp/imip.txt", "a"), "Handling", method, "with", handler, "->", methods[method](handler)
1.54 + methods[method](handler)()
1.55 +
1.56 +class Handler:
1.57 + def __init__(self, details):
1.58 + self.details = details
1.59 + self.uid = get_value(details, "UID")
1.60 + self.sequence = get_value(details, "SEQUENCE")
1.61 + self.store = imip_store.FileStore()
1.62 +
1.63 + def publish(self):
1.64 + pass
1.65 +
1.66 + def get_value(self, name, single=False):
1.67 + return get_value(self.details, name, single)
1.68 +
1.69 +class Event(Handler):
1.70 + def add(self):
1.71 + pass
1.72 +
1.73 + def cancel(self):
1.74 + pass
1.75 +
1.76 + def counter(self):
1.77 + pass
1.78 +
1.79 + def declinecounter(self):
1.80 + pass
1.81 +
1.82 + def refresh(self):
1.83 + pass
1.84 +
1.85 + def reply(self):
1.86 + pass
1.87 +
1.88 + def request(self):
1.89 + pass
1.90 +
1.91 +class Freebusy(Handler):
1.92 + def reply(self):
1.93 + attendees = self.get_value("ATTENDEE", False)
1.94 + organiser = self.get_value("ORGANIZER")
1.95 + if not attendees and not organiser:
1.96 + return
1.97 +
1.98 + # Store the reply details for the attendee.
1.99
1.100 - obj = get_value(itip, name)
1.101 - if obj:
1.102 - uid = get_value(obj, "UID")
1.103 - if uid:
1.104 - handler(obj, method)
1.105 + def request(self):
1.106 + attendees = self.get_value("ATTENDEE", False)
1.107 + organiser = self.get_value("ORGANIZER")
1.108 + print >>open("/tmp/imip.txt", "a"), attendees, organiser
1.109 + if not attendees and not organiser:
1.110 + return
1.111 +
1.112 + # Get the details for the attendee.
1.113 +
1.114 + for attendee in attendees:
1.115 + freebusy = self.store.get_freebusy(attendee)
1.116 + print >>open("/tmp/imip.txt", "a"), freebusy
1.117 +
1.118 + # Send a reply with the information.
1.119
1.120 -def handle_itip_freebusy(freebusy, method):
1.121 - print >>open("/tmp/imip.txt", "a"), freebusy
1.122 +class Journal(Handler):
1.123 + def add(self):
1.124 + pass
1.125 +
1.126 + def cancel(self):
1.127 + pass
1.128 +
1.129 +class Todo(Handler):
1.130 + def add(self):
1.131 + pass
1.132 +
1.133 + def cancel(self):
1.134 + pass
1.135 +
1.136 + def counter(self):
1.137 + pass
1.138
1.139 -def handle_itip_event(event, method):
1.140 - print >>open("/tmp/imip.txt", "a"), event
1.141 + def declinecounter(self):
1.142 + pass
1.143 +
1.144 + def refresh(self):
1.145 + pass
1.146 +
1.147 + def reply(self):
1.148 + pass
1.149 +
1.150 + def request(self):
1.151 + pass
1.152 +
1.153 +# Handler registry.
1.154
1.155 -def handle_itip_todo(todo, method):
1.156 - print >>open("/tmp/imip.txt", "a"), todo
1.157 +handlers = [
1.158 + ("VFREEBUSY", Freebusy),
1.159 + ("VEVENT", Event),
1.160 + ("VTODO", Todo),
1.161 + ("VJOURNAL", Journal),
1.162 + ]
1.163
1.164 -def handle_itip_journal(journal, method):
1.165 - print >>open("/tmp/imip.txt", "a"), journal
1.166 +methods = {
1.167 + "ADD" : lambda handler: handler.add,
1.168 + "CANCEL" : lambda handler: handler.cancel,
1.169 + "COUNTER" : lambda handler: handler.counter,
1.170 + "DECLINECOUNTER" : lambda handler: handler.declinecounter,
1.171 + "PUBLISH" : lambda handler: handler.publish,
1.172 + "REFRESH" : lambda handler: handler.refresh,
1.173 + "REPLY" : lambda handler: handler.reply,
1.174 + "REQUEST" : lambda handler: handler.request,
1.175 + }
1.176
1.177 if __name__ == "__main__":
1.178 process(sys.stdin, sys.argv[1:])
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/imip_store.py Mon Sep 22 16:34:36 2014 +0200
2.3 @@ -0,0 +1,41 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +from os.path import abspath, commonprefix, exists, join
2.7 +from os import makedirs
2.8 +
2.9 +STORE_DIR = "/tmp/imip"
2.10 +
2.11 +def check_dir(base, dir):
2.12 + return commonprefix([base, abspath(dir)]) == base
2.13 +
2.14 +class FileStore:
2.15 +
2.16 + "A file store of tabular data."
2.17 +
2.18 + def __init__(self):
2.19 + self.store_dir = STORE_DIR
2.20 + if not exists(self.store_dir):
2.21 + makedirs(self.store_dir)
2.22 +
2.23 + def get_freebusy(self, calendar):
2.24 +
2.25 + "Get free/busy details from the given 'calendar'."
2.26 +
2.27 + dir = join(self.store_dir, calendar)
2.28 + if not check_dir(self.store_dir, dir):
2.29 + return None
2.30 +
2.31 + filename = join(dir, "freebusy")
2.32 + if not exists(filename):
2.33 + return None
2.34 +
2.35 + f = open(filename)
2.36 + try:
2.37 + l = []
2.38 + for line in f.readlines():
2.39 + l.append(line.strip().split("\t"))
2.40 + return l
2.41 + finally:
2.42 + f.close()
2.43 +
2.44 +# vim: tabstop=4 expandtab shiftwidth=4