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 method = method and method.upper() 40 41 # Decode the data and parse it. 42 43 f = StringIO(part.get_payload(decode=True)) 44 45 itip = parse_object(f, part.get_content_charset(), "VCALENDAR") 46 47 # Ignore the part if not a calendar object. 48 49 if not itip: 50 return 51 52 # Require consistency between declared and employed methods. 53 54 if get_value(itip, "METHOD") == method: 55 56 # Look for different kinds of sections. 57 58 all_results = [] 59 60 for name, items in itip.items(): 61 62 # Get a handler for the given section. 63 64 handler = handlers.get(name) 65 if not handler: 66 continue 67 68 for item in items: 69 70 # Dispatch to a handler and obtain any response. 71 72 handler.set_object(Object({name : item})) 73 handler.set_identity(method) 74 75 if handler.is_usable(method): 76 77 # Perform the method in a critical section. 78 79 handler.acquire_lock() 80 try: 81 methods[method](handler)() 82 finally: 83 handler.release_lock() 84 85 # Handler registry. 86 87 methods = { 88 "ADD" : lambda handler: handler.add, 89 "CANCEL" : lambda handler: handler.cancel, 90 "COUNTER" : lambda handler: handler.counter, 91 "DECLINECOUNTER" : lambda handler: handler.declinecounter, 92 "PUBLISH" : lambda handler: handler.publish, 93 "REFRESH" : lambda handler: handler.refresh, 94 "REPLY" : lambda handler: handler.reply, 95 "REQUEST" : lambda handler: handler.request, 96 } 97 98 # vim: tabstop=4 expandtab shiftwidth=4