Index: sitescripts/formmail/web/formmail.py |
=================================================================== |
--- a/sitescripts/formmail/web/formmail.py |
+++ b/sitescripts/formmail/web/formmail.py |
@@ -13,43 +13,159 @@ |
# You should have received a copy of the GNU General Public License |
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
-import re |
+from __future__ import print_function |
+ |
+import os |
+import sys |
import datetime |
-from urlparse import parse_qsl |
-from sitescripts.utils import get_config, sendMail, setupStderr |
-from sitescripts.web import url_handler |
+import traceback |
+import collections |
+from csv import DictWriter, DictReader |
+import jinja2 |
-@url_handler('/formmail') |
-def handleRequest(environ, start_response): |
- setupStderr(environ['wsgi.errors']) |
+from sitescripts.utils import (get_config, sendMail, encode_email_address, |
+ get_template) |
+from sitescripts.web import registerUrlHandler, form_handler |
- start_response('200 OK', [('Content-Type', 'text/plain; charset=utf-8')]) |
- if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'): |
- return 'Unsupported request method' |
+def get_config_items(): |
+ config = get_config() |
+ default_keys = set(config.defaults()) |
+ for name, value in config.items('formmail'): |
+ if name not in default_keys: |
+ yield name, value |
+ |
+ |
+def store_value(conf_dict, path, value): |
+ head, tail = path[0], path[1:] |
+ if head not in conf_dict: |
+ conf_dict[head] = collections.OrderedDict() |
+ if tail: |
+ store_value(conf_dict[head], tail, value) |
+ else: |
+ conf_dict[head].value = value |
+ |
+ |
+def conf_parse(conf_items): |
+ conf_dict = collections.OrderedDict() |
+ for key, value in conf_items: |
+ path = key.split('.') |
+ store_value(conf_dict, path, value) |
+ return conf_dict |
+ |
+ |
+def make_error(spec, check_type, default_message): |
+ if check_type in spec: |
+ return spec[check_type].value |
+ return default_message |
+ |
+ |
+def log_formfield_error(parameters, log_path): |
+ err_file = os.path.basename(log_path) + '_error' |
+ err_path = os.path.join(os.path.dirname(log_path), err_file) |
+ if os.path.isfile(err_path): |
+ with open(err_path, 'a') as error_log: |
+ writer = DictWriter(error_log, fieldnames=parameters.keys()) |
+ writer.writerow(parameters) |
+ raise Exception('Field names have changed, error log ' |
+ 'appended to ' + err_path) |
+ with open(err_path, 'w') as error_log: |
+ writer = DictWriter(error_log, fieldnames=parameters.keys()) |
+ writer.writeheader() |
+ writer.writerow(parameters) |
+ raise Exception('Field names have changed, error log ' |
+ 'written to ' + err_path) |
+ |
+ |
+def log_formdata(params, path): |
+ if os.path.isfile(path): |
+ with open(path, 'ab+') as formlog: |
+ formlog.seek(0) |
+ reader = DictReader(formlog) |
+ if reader.fieldnames != params.keys(): |
+ log_formfield_error(params, path) |
+ formlog.seek(os.SEEK_END) |
+ writer = DictWriter(formlog, fieldnames=params.keys()) |
+ writer.writerow(params) |
+ return |
+ with open(path, 'w') as new_formlog: |
+ writer = DictWriter(new_formlog, fieldnames=params.keys()) |
+ writer.writeheader() |
+ writer.writerow(params) |
+ return |
+ |
+ |
+def validate_fields(fields, params): |
+ errors = [] |
+ for field, spec in fields.items(): |
+ if 'mandatory' in spec.value and field not in params: |
+ errors.append(make_error(spec, 'mandatory', |
+ 'No {} entered'.format(field))) |
+ if 'email' in spec.value and field in params: |
+ try: |
+ params[field] = encode_email_address(params[field]) |
+ except ValueError: |
+ errors.append(make_error(spec, 'email', 'Invalid email')) |
+ |
+ unexpected_fields = ' '.join(set(params.keys()) - set(fields.keys())) |
+ if unexpected_fields: |
+ errors.append('Unexpected field/fields: ' + str(unexpected_fields)) |
+ return errors |
+ |
+ |
+def make_handler(name, config): |
try: |
- request_body_length = int(environ['CONTENT_LENGTH']) |
- except: |
- return 'Invalid or missing Content-Length header' |
+ url = config['url'].value |
+ except (KeyError, AttributeError): |
+ raise Exception('No URL configured for form handler: ' + name) |
+ try: |
+ template = config['template'].value |
+ get_template(template, autoescape=False) |
+ except (KeyError, AttributeError): |
+ raise Exception('No template configured for form handler: ' + name) |
+ except jinja2.TemplateNotFound: |
+ raise Exception('Template not found at: ' + template) |
+ try: |
+ fields = config['fields'] |
+ for field, spec in fields.items(): |
+ spec.value = {s.strip() for s in spec.value.split(',')} |
+ except KeyError: |
+ raise Exception('No fields configured for form handler: ' + name) |
+ if len(fields) == 0: |
+ raise Exception('No fields configured for form handler: ' + name) |
- request_body = environ['wsgi.input'].read(request_body_length) |
- params = {} |
- for key, value in parse_qsl(request_body): |
- params[key] = value.decode('utf-8').strip() |
+ @form_handler |
+ def handler(environ, start_response, params): |
+ response_headers = [('Content-Type', 'text/plain; charset=utf-8')] |
+ errors = validate_fields(fields, params) |
+ if errors: |
+ start_response('400 Bad Request', response_headers) |
+ return '\n'.join(errors) |
+ time = datetime.datetime.now() |
+ template_args = { |
+ 'time': time, |
+ 'fields': {field: params.get(field, '') for field in fields}, |
+ } |
+ try: |
+ sendMail(template, template_args) |
+ except: |
+ print(traceback.print_exc(), file=sys.stderr) |
+ start_response('500 Server Error', response_headers) |
+ return '' |
+ finally: |
+ if 'csv_log' in config: |
+ params = {field: params.get(field, '').encode('utf8') |
+ for field in fields} |
+ params['time'] = time |
+ log_formdata(params, config['csv_log'].value) |
+ start_response('200 OK', response_headers) |
+ return '' |
- if not 'name' in params or params['name'] == '': |
- return 'No name entered' |
- if not 'email' in params or params['email'] == '': |
- return 'No email address entered' |
- if not 'subject' in params or params['subject'] == '': |
- return 'No subject entered' |
- if not 'message' in params or params['message'] == '': |
- return 'No message entered' |
+ return url, handler |
- if not re.match(r'^\w[\w.+!-]+@\w[\w.-]+\.[a-zA-Z]{2,6}$', params['email']): |
- return 'Invalid email address' |
- params['time'] = datetime.datetime.now() |
- sendMail(get_config().get('formmail', 'template'), params) |
- return 'Message sent' |
+conf_dict = conf_parse(get_config_items()) |
+for name, config in conf_dict.items(): |
+ url, handler = make_handler(name, config) |
+ registerUrlHandler(url, handler) |