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