| LEFT | RIGHT |
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 | 2 |
| 3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
| 5 # | 5 # |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
| 7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
| 8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
| 9 # | 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
| 14 # | 14 # |
| 15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
| 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 17 | 17 |
| 18 import re | 18 import datetime |
| 19 from sitescripts.utils import get_config, sendMail, setupStderr | 19 |
| 20 from sitescripts.web import url_handler, form_handler | 20 from sitescripts.utils import get_config, sendMail, encode_email_address |
| 21 from sitescripts.web import url_handler, form_handler, send_simple_response |
| 21 | 22 |
| 22 @url_handler('/formmail') | 23 @url_handler('/formmail') |
| 23 @form_handler | 24 @form_handler |
| 24 def handleRequest(environ, start_response, data): | 25 def handleRequest(environ, start_response, data): |
| 25 setupStderr(environ['wsgi.errors']) | 26 params = {name: data.get(name, '').strip() for name in ('name', 'email', 'subj
ect', 'message')} |
| 27 missing = [k for k, v in params.iteritems() if not v] |
| 26 | 28 |
| 27 params = {} | 29 if missing: |
| 28 for field in ('name', 'email', 'subject', 'message'): | 30 text = 'Missing fields: ' + ', '.join(missing) |
| 29 value = data.get(field, '').strip() | 31 return send_simple_response(start_response, 400, text) |
| 30 | 32 |
| 31 if not value: | 33 try: |
| 32 start_response('400 Bad request') | 34 params['email'] = encode_email_address(params['email']) |
| 33 return ['No "%s" given' % field] | 35 except ValueError: |
| 36 return send_simple_response(start_response, 400, 'Invalid email address') |
| 34 | 37 |
| 35 params[field] = value | 38 params['time'] = datetime.datetime.now() |
| 36 | |
| 37 if not re.match(r'^\w[\w.+!-]+@\w[\w.-]+\.[a-zA-Z]{2,6}$', params['email']): | |
| 38 start_response('400 Bad request') | |
| 39 return ['Invalid email address'] | |
| 40 | |
| 41 sendMail(get_config().get('formmail', 'template'), params) | 39 sendMail(get_config().get('formmail', 'template'), params) |
| 42 | 40 |
| 43 start_response('200 OK') | 41 return send_simple_response(start_response, 200, 'Message sent') |
| 44 return ['Message sent'] | |
| LEFT | RIGHT |