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 handler.set_identity(method) 73 74 if handler.is_usable(method): 75 76 # Perform the method in a critical section. 77 78 handler.acquire_lock() 79 try: 80 methods[method](handler)() 81 finally: 82 handler.release_lock() 83 84 # Handler registry. 85 86 methods = { 87 "ADD" : lambda handler: handler.add, 88 "CANCEL" : lambda handler: handler.cancel, 89 "COUNTER" : lambda handler: handler.counter, 90 "DECLINECOUNTER" : lambda handler: handler.declinecounter, 91 "PUBLISH" : lambda handler: handler.publish, 92 "REFRESH" : lambda handler: handler.refresh, 93 "REPLY" : lambda handler: handler.reply, 94 "REQUEST" : lambda handler: handler.request, 95 } 96 97 # vim: tabstop=4 expandtab shiftwidth=4