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