OLD | NEW |
(Empty) | |
| 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2006-2016 Eyeo GmbH |
| 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. |
| 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. |
| 12 # |
| 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
| 16 import datetime |
| 17 import collections |
| 18 |
| 19 from sitescripts.utils import get_config, sendMail, encode_email_address |
| 20 from sitescripts.web import registerUrlHandler, form_handler |
| 21 |
| 22 |
| 23 def get_config_items(): |
| 24 config = get_config() |
| 25 default_keys = set(config.defaults()) |
| 26 for name, value in config.items('formmail2'): |
| 27 if name not in default_keys: |
| 28 yield name, value |
| 29 |
| 30 |
| 31 def store_value(conf_dict, path, value): |
| 32 head, tail = path[0], path[1:] |
| 33 if head not in conf_dict: |
| 34 conf_dict[head] = collections.OrderedDict() |
| 35 if tail: |
| 36 store_value(conf_dict[head], tail, value) |
| 37 else: |
| 38 conf_dict[head].value = value |
| 39 |
| 40 |
| 41 def conf_parse(conf_items): |
| 42 conf_dict = collections.OrderedDict() |
| 43 for key, value in conf_items: |
| 44 path = key.split('.') |
| 45 store_value(conf_dict, path, value) |
| 46 return conf_dict |
| 47 |
| 48 |
| 49 def make_error(spec, check_type, default_message): |
| 50 if check_type in spec: |
| 51 return spec[check_type].value |
| 52 return default_message |
| 53 |
| 54 |
| 55 def make_handler(name, config): |
| 56 try: |
| 57 url = config['url'].value |
| 58 except (KeyError, AttributeError): |
| 59 raise Exception('No URL configured for form handler:' + name) |
| 60 try: |
| 61 template = config['template'].value |
| 62 except (KeyError, AttributeError): |
| 63 raise Exception('No template configured for form handler:' + name) |
| 64 try: |
| 65 fields = config['fields'] |
| 66 for field, spec in fields.items(): |
| 67 spec.value = {s.strip() for s in spec.value.split(',')} |
| 68 except KeyError: |
| 69 raise Exception('No fields configured for form handler:' + name) |
| 70 if len(fields) == 0: |
| 71 raise Exception('No fields configured for form handler:' + name) |
| 72 |
| 73 @form_handler |
| 74 def handler(environ, start_response, params): |
| 75 response_headers = [('Content-Type', 'text/plain; charset=utf-8')] |
| 76 errors = [] |
| 77 for field, spec in fields.items(): |
| 78 if 'mandatory' in spec.value: |
| 79 if field not in params.keys(): |
| 80 errors.append(make_error(spec, 'mandatory', |
| 81 'No {} entered'.format(field))) |
| 82 if 'email' in spec.value and field in params.keys(): |
| 83 try: |
| 84 params[field] = encode_email_address(params[field]) |
| 85 except ValueError: |
| 86 errors.append(make_error(spec, 'email', 'Invalid email')) |
| 87 if errors: |
| 88 start_response('400 Bad Request', response_headers) |
| 89 return '\n'.join(errors) |
| 90 |
| 91 template_args = { |
| 92 'time': datetime.datetime.now(), |
| 93 'fields': {field: params.get(field, '') for field in fields} |
| 94 } |
| 95 sendMail(template, template_args) |
| 96 start_response('200 OK', response_headers) |
| 97 return '' |
| 98 |
| 99 return url, handler |
| 100 |
| 101 |
| 102 conf_dict = conf_parse(get_config_items()) |
| 103 for name, config in conf_dict.items(): |
| 104 url, handler = make_handler(name, config) |
| 105 registerUrlHandler(url, handler) |
OLD | NEW |