1 #!/usr/bin/env python 2 3 """ 4 Interpretation and preparation of iMIP content, together with a content handling 5 mechanism employed by specific recipients. 6 7 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 from imiptools.data import Object, parse_object, get_value 24 25 try: 26 from cStringIO import StringIO 27 except ImportError: 28 from StringIO import StringIO 29 30 # Handler mechanism objects. 31 32 def handle_itip_part(part, handlers): 33 34 """ 35 Handle the given iTIP 'part' using the given 'handlers' dictionary. 36 37 Return a list of responses, each response being a tuple of the form 38 (outgoing-recipients, message-part). 39 """ 40 41 method = part.get_param("method") 42 43 # Decode the data and parse it. 44 45 f = StringIO(part.get_payload(decode=True)) 46 47 itip = parse_object(f, part.get_content_charset(), "VCALENDAR") 48 49 # Ignore the part if not a calendar object. 50 51 if not itip: 52 return 53 54 # Require consistency between declared and employed methods. 55 56 if get_value(itip, "METHOD") == method: 57 58 # Look for different kinds of sections. 59 60 all_results = [] 61 62 for name, items in itip.items(): 63 64 # Get a handler for the given section. 65 66 handler = handlers.get(name) 67 if not handler: 68 continue 69 70 for item in items: 71 72 # Dispatch to a handler and obtain any response. 73 74 handler.set_object(Object({name : item})) 75 methods[method](handler)() 76 77 # Handler registry. 78 79 methods = { 80 "ADD" : lambda handler: handler.add, 81 "CANCEL" : lambda handler: handler.cancel, 82 "COUNTER" : lambda handler: handler.counter, 83 "DECLINECOUNTER" : lambda handler: handler.declinecounter, 84 "PUBLISH" : lambda handler: handler.publish, 85 "REFRESH" : lambda handler: handler.refresh, 86 "REPLY" : lambda handler: handler.reply, 87 "REQUEST" : lambda handler: handler.request, 88 } 89 90 # vim: tabstop=4 expandtab shiftwidth=4