Index: .hgignore
===================================================================
--- a/.hgignore
+++ b/.hgignore
@@ -5,6 +5,7 @@
.tox
.cache
.sitescripts.test
-.coverage
+.coverage*
htmlcov/
+.pytest_cache/
.DS_Store
Index: .sitescripts.example
===================================================================
--- a/.sitescripts.example
+++ b/.sitescripts.example
@@ -13,7 +13,6 @@
sitescripts.reports.web.showDigest =
sitescripts.reports.web.showUser =
sitescripts.formmail.web.formmail =
-sitescripts.formmail.web.formmail2 =
sitescripts.submit_email.web.submit_email =
sitescripts.send_installation_link.web.send_installation_link =
sitescripts.crawler.web.crawler =
@@ -152,9 +151,6 @@
Adblock-Plus-for-Firefox=adblock-plus(-[\d\.]+)?-for-firefox(-next)?
[formmail]
-template=formmail/template/eyeo.mail
-
-[formmail2]
test.csv_log = /var/log/something.csv_log
test.url=test/apply/submit
test.template=formmail/test/template/test.mail
Index: sitescripts/formmail/README.md
===================================================================
new file mode 100644
--- /dev/null
+++ b/sitescripts/formmail/README.md
@@ -0,0 +1,68 @@
+# formmail
+
+The web handler that extracts form data from a POST request, uses it to
+populate an email template and then sends the produced email to a configured
+list of addresses.
+
+## Dependencies
+
+* [Python 2.7](https://www.python.org/download/releases/2.7/)
+* [Jinja2](http://jinja.pocoo.org/docs/2.10/intro/)
+* Other packages are required for testing, please see the list of 'deps' in
+ [`tox.ini`](../../tox.ini)
+
+## Running the web handler
+
+Normally, the formmail web handler is run by the multiplexer, and configured
+via the sitescripts config file. Please refer to the main
+[README](../../README.md) for more information about the multiplexer and
+configuring `sitescripts.ini`.
+
+In order to activate this handler, add the following line to the multiplexer
+config file:
+
+ [multiplexer]
+ sitescripts.formmail.web.formmail =
+
+## Configuring the web handler
+
+`formmail.py` can handle multiple URLs and forms. Each URL will correspond to a
+group of config variables that all start with the same prefix, for example:
+handler1. These variables are configured in the [formmail] section of the
+config file.
+
+The URL of the form, where the POST request comes from:
+
+ [formmail]
+ handler1.url = formmail/test/apply/submit
+
+The CSV file into which all submissions will be saved (optional):
+
+ handler1.csv_log = /var/log/handler1-log.csv
+
+The Jinja2 template for the email. This is where the recipient email addresses
+are entered.
+[(See an example email template here.)](formmail/test/template/test.mail)
+
+ handler1.template = formmail/handler1/mail-template.tmpl
+
+The `handler1.fields.xxx` subgroup includes the descriptions of the form
+fields, and these must match the fields on the form. These are the fields
+expected in the POST request and then made available to the template. Each
+variable in the group defines a field and its value can be:
+* "mandatory" (which makes the field mandatory)
+* and/or "email" (which makes the field an email)
+* or it can be empty (just a normal optional field).
+
+For mandatory fields we can also set "group-name.fields.field-name.mandatory"
+to override the error message that will be returned by the handler if the field
+was empty. Likewise for email fields we can define
+"group-name.fields.field-name.email" to set the error message that's returned
+if the content of the field doesn't look like an email. See an example:
+
+ handler1.fields.email = mandatory, email
+ handler1.fields.email.mandatory = You failed the email test
+ handler1.fields.email.email = You failed the email validation
+ handler1.fields.non_mandatory_email = email
+ handler1.fields.non_mandatory_message =
+ handler1.fields.mandatory = mandatory
Index: sitescripts/formmail/template/eyeo.mail
===================================================================
deleted file mode 100644
--- a/sitescripts/formmail/template/eyeo.mail
+++ /dev/null
@@ -1,10 +0,0 @@
-To: info@eyeo.com
-From: donotreply@adblockplus.org
-Reply-To: {{(name,email)|emailaddr|mime}}
-Subject: {{subject|mime}} (via Contact form)
-Content-Transfer-Encoding: 8bit
-Content-Type: text/plain; charset="UTF-8"
-
-On {{time.strftime('%d/%m/%y %H:%M')}}, {{(name,email)|emailaddr}} wrote:
-
-{{message}}
Index: sitescripts/formmail/test/test_formmail.py
===================================================================
--- a/sitescripts/formmail/test/test_formmail.py
+++ b/sitescripts/formmail/test/test_formmail.py
@@ -14,13 +14,43 @@
# along with Adblock Plus. If not, see .
from urllib import urlencode
-from urllib2 import urlopen
+from urllib2 import urlopen, HTTPError
+from csv import DictReader
import pytest
from wsgi_intercept import (urllib_intercept, add_wsgi_intercept,
remove_wsgi_intercept)
-from sitescripts.formmail.web.formmail import handleRequest
+from sitescripts.formmail.web import formmail
+
+HOST = 'test.local'
+LOG_PORT = 80
+NO_LOG_PORT = 81
+
+
+@pytest.fixture
+def log_path(tmpdir):
+ return str(tmpdir.join('test.csv_log'))
+
+
+@pytest.fixture
+def log_form_config():
+ return formmail.conf_parse(formmail.get_config_items())['test']
+
+
+@pytest.fixture
+def form_config():
+ config = formmail.conf_parse(formmail.get_config_items())['test']
+ del config['csv_log']
+ return config
+
+
+@pytest.fixture
+def form_handler(log_path, form_config, log_form_config):
+ """ Create two handlers, one that logs and another that doesn't """
+ log_form_config['csv_log'].value = log_path
+ return (formmail.make_handler('test', log_form_config)[1],
+ formmail.make_handler('test', form_config)[1])
# We make this a fixture instead of a constant so we can modify it in each
@@ -28,66 +58,142 @@
@pytest.fixture
def form_data():
return {
- 'name': 'John Doe',
'email': 'john_doe@gmail.com',
- 'subject': 'Hello there!',
- 'message': 'Once upon a time\nthere lived a king.',
+ 'mandatory': 'john_doe@gmail.com',
+ 'non_mandatory_message': 'Once upon a time\nthere lived a king.',
+ 'non_mandatory_email': 'test@test.com',
}
-@pytest.fixture()
-def response_for():
- host, port = 'test.local', 80
+@pytest.fixture
+def response_for(form_handler):
+ """ Registers two intercepts, returns responses for them based on bool """
urllib_intercept.install_opener()
- add_wsgi_intercept(host, port, lambda: handleRequest)
- url = 'http://{}:{}'.format(host, port)
+ add_wsgi_intercept(HOST, LOG_PORT, lambda: form_handler[0])
+ add_wsgi_intercept(HOST, NO_LOG_PORT, lambda: form_handler[1])
- def response_for(data):
+ def response_for(data, log=False):
+ if log:
+ url = 'http://{}:{}'.format(HOST, LOG_PORT)
+ else:
+ url = 'http://{}:{}'.format(HOST, NO_LOG_PORT)
if data is None:
response = urlopen(url)
else:
response = urlopen(url, urlencode(data))
- assert response.getcode() == 200
- return response.read()
+ return response.code, response.read()
yield response_for
remove_wsgi_intercept()
-def test_get_error(response_for):
- assert response_for(None) == 'Unsupported request method'
+@pytest.fixture
+def sm_mock(mocker):
+ return mocker.patch('sitescripts.formmail.web.formmail.sendMail')
-def test_no_name(response_for, form_data):
- del form_data['name']
- assert response_for(form_data) == 'No name entered'
+@pytest.mark.parametrize('key,message', [
+ ('url', 'No URL configured for form handler: test'),
+ ('fields', 'No fields configured for form handler: test'),
+ ('template', 'No template configured for form handler: test'),
+])
+def test_config_errors(key, message, form_config):
+ del form_config[key]
+ with pytest.raises(Exception) as error:
+ formmail.make_handler('test', form_config)[1]
+ assert error.value.message == message
-def test_no_email(response_for, form_data):
+@pytest.mark.parametrize('field,message', [
+ (('new_field', 'foo'), 'Unexpected field/fields: new_field'),
+ (('mandatory', ''), 'No mandatory entered'),
+ (('non_mandatory_email', 'asfaf'), 'Invalid email'),
+ (('email', 'asfaf'), 'You failed the email validation'),
+ (('email', ''), 'You failed the email test'),
+])
+def test_http_errs(field, message, response_for, form_data, sm_mock):
+ key, value = field
+ form_data[key] = value
+ with pytest.raises(HTTPError) as error:
+ response_for(form_data)
+ assert error.value.read() == message
+
+
+@pytest.mark.parametrize('field,expected', [
+ (('non_mandatory_message', '\xc3\xb6'), (200, '')),
+ (('non_mandatory_message', ''), (200, '')),
+])
+def test_success(field, expected, log_path, response_for, form_data, sm_mock):
+ key, value = field
+ form_data[key] = value
+ assert response_for(form_data, log=False) == expected
+ assert sm_mock.call_count == 1
+
+ params = sm_mock.call_args[0][1]['fields']
+ assert set(params.keys()) == set(form_data.keys())
+ for key, value in form_data.items():
+ assert params[key] == value.decode('utf8')
+
+ assert response_for(form_data, log=True) == expected
+ assert sm_mock.call_count == 2
+
+ assert response_for(form_data, log=True) == expected
+ assert sm_mock.call_count == 3
+
+ with open(log_path) as log_file:
+ reader = DictReader(log_file)
+ row = reader.next()
+ # rows should not be equal because the time field
+ # is added by the logging function.
+ assert row != reader.next()
+
+
+def test_config_field_errors(form_config):
+ form_config['fields'] = {}
+ with pytest.raises(Exception) as error:
+ formmail.make_handler('test', form_config)[1]
+ assert error.value.message == 'No fields configured for form handler: test'
+
+
+def test_config_template_errors(form_config):
+ form_config['template'].value = 'no'
+ with pytest.raises(Exception) as error:
+ formmail.make_handler('test', form_config)[1]
+ assert error.value.message == 'Template not found at: no'
+
+
+def test_config_parse(form_config):
+ assert form_config['url'].value == 'test/apply/submit'
+ assert form_config['fields']['email'].value == 'mandatory, email'
+
+
+def test_sendmail_fail(log_path, response_for, form_data, sm_mock):
+ sm_mock.side_effect = Exception('Sendmail Fail')
+ with pytest.raises(HTTPError):
+ response_for(form_data, log=True)
+
+ with open(log_path) as log_file:
+ row = DictReader(log_file).next()
+ assert row != form_data
+
+
+def test_append_field_err(form_config, form_data, log_path):
+ """ Checks that error logs are correctly written and appended
+
+ Submits three forms, the second two have different fields to the first
+ and should be added to the same log file as each other, and be identical
+ """
+ formmail.log_formdata(form_data, log_path)
del form_data['email']
- assert response_for(form_data) == 'No email address entered'
+ # submit two forms with fields that dont match the config
+ # this should append the second form to the error log file
+ with pytest.raises(Exception):
+ formmail.log_formdata(form_data, log_path)
+ with pytest.raises(Exception):
+ formmail.log_formdata(form_data, log_path)
-def test_no_subject(response_for, form_data):
- del form_data['subject']
- assert response_for(form_data) == 'No subject entered'
-
-
-def test_no_message(response_for, form_data):
- del form_data['message']
- assert response_for(form_data) == 'No message entered'
-
-
-def test_bad_email(response_for, form_data):
- form_data['email'] = 'bad_email'
- assert response_for(form_data) == 'Invalid email address'
-
-
-def test_success(response_for, form_data, mocker):
- sm_mock = mocker.patch('sitescripts.formmail.web.formmail.sendMail')
- assert response_for(form_data) == 'Message sent'
- assert sm_mock.call_count == 1
- params = sm_mock.call_args[0][1]
- assert set(params.keys()) == set(form_data.keys()) | {'time'}
- for key, value in form_data.items():
- assert params[key] == value
+ with open(log_path + '_error') as error_log:
+ reader = DictReader(error_log)
+ assert reader.next() == form_data
+ assert reader.next() == form_data
Index: sitescripts/formmail/test/test_formmail2.py
===================================================================
deleted file mode 100644
--- a/sitescripts/formmail/test/test_formmail2.py
+++ /dev/null
@@ -1,199 +0,0 @@
-# This file is part of the Adblock Plus web scripts,
-# Copyright (C) 2006-present eyeo GmbH
-#
-# Adblock Plus is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License version 3 as
-# published by the Free Software Foundation.
-#
-# Adblock Plus is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with Adblock Plus. If not, see .
-
-from urllib import urlencode
-from urllib2 import urlopen, HTTPError
-from csv import DictReader
-
-import pytest
-from wsgi_intercept import (urllib_intercept, add_wsgi_intercept,
- remove_wsgi_intercept)
-
-from sitescripts.formmail.web import formmail2
-
-HOST = 'test.local'
-LOG_PORT = 80
-NO_LOG_PORT = 81
-
-
-@pytest.fixture
-def log_path(tmpdir):
- return str(tmpdir.join('test.csv_log'))
-
-
-@pytest.fixture
-def log_form_config():
- return formmail2.conf_parse(formmail2.get_config_items())['test']
-
-
-@pytest.fixture
-def form_config():
- config = formmail2.conf_parse(formmail2.get_config_items())['test']
- del config['csv_log']
- return config
-
-
-@pytest.fixture
-def form_handler(log_path, form_config, log_form_config):
- """ Create two handlers, one that logs and another that doesn't """
- log_form_config['csv_log'].value = log_path
- return (formmail2.make_handler('test', log_form_config)[1],
- formmail2.make_handler('test', form_config)[1])
-
-
-# We make this a fixture instead of a constant so we can modify it in each
-# test as needed without affecting other tests.
-@pytest.fixture
-def form_data():
- return {
- 'email': 'john_doe@gmail.com',
- 'mandatory': 'john_doe@gmail.com',
- 'non_mandatory_message': 'Once upon a time\nthere lived a king.',
- 'non_mandatory_email': 'test@test.com',
- }
-
-
-@pytest.fixture
-def response_for(form_handler):
- """ Registers two intercepts, returns responses for them based on bool """
- urllib_intercept.install_opener()
- add_wsgi_intercept(HOST, LOG_PORT, lambda: form_handler[0])
- add_wsgi_intercept(HOST, NO_LOG_PORT, lambda: form_handler[1])
-
- def response_for(data, log=False):
- if log:
- url = 'http://{}:{}'.format(HOST, LOG_PORT)
- else:
- url = 'http://{}:{}'.format(HOST, NO_LOG_PORT)
- if data is None:
- response = urlopen(url)
- else:
- response = urlopen(url, urlencode(data))
- return response.code, response.read()
-
- yield response_for
- remove_wsgi_intercept()
-
-
-@pytest.fixture
-def sm_mock(mocker):
- return mocker.patch('sitescripts.formmail.web.formmail2.sendMail')
-
-
-@pytest.mark.parametrize('key,message', [
- ('url', 'No URL configured for form handler: test'),
- ('fields', 'No fields configured for form handler: test'),
- ('template', 'No template configured for form handler: test'),
-])
-def test_config_errors(key, message, form_config):
- del form_config[key]
- with pytest.raises(Exception) as error:
- formmail2.make_handler('test', form_config)[1]
- assert error.value.message == message
-
-
-@pytest.mark.parametrize('field,message', [
- (('new_field', 'foo'), 'Unexpected field/fields: new_field'),
- (('mandatory', ''), 'No mandatory entered'),
- (('non_mandatory_email', 'asfaf'), 'Invalid email'),
- (('email', 'asfaf'), 'You failed the email validation'),
- (('email', ''), 'You failed the email test'),
-])
-def test_http_errs(field, message, response_for, form_data, sm_mock):
- key, value = field
- form_data[key] = value
- with pytest.raises(HTTPError) as error:
- response_for(form_data)
- assert error.value.read() == message
-
-
-@pytest.mark.parametrize('field,expected', [
- (('non_mandatory_message', '\xc3\xb6'), (200, '')),
- (('non_mandatory_message', ''), (200, '')),
-])
-def test_success(field, expected, log_path, response_for, form_data, sm_mock):
- key, value = field
- form_data[key] = value
- assert response_for(form_data, log=False) == expected
- assert sm_mock.call_count == 1
-
- params = sm_mock.call_args[0][1]['fields']
- assert set(params.keys()) == set(form_data.keys())
- for key, value in form_data.items():
- assert params[key] == value.decode('utf8')
-
- assert response_for(form_data, log=True) == expected
- assert sm_mock.call_count == 2
-
- assert response_for(form_data, log=True) == expected
- assert sm_mock.call_count == 3
-
- with open(log_path) as log_file:
- reader = DictReader(log_file)
- row = reader.next()
- # rows should not be equal because the time field
- # is added by the logging function.
- assert row != reader.next()
-
-
-def test_config_field_errors(form_config):
- form_config['fields'] = {}
- with pytest.raises(Exception) as error:
- formmail2.make_handler('test', form_config)[1]
- assert error.value.message == 'No fields configured for form handler: test'
-
-
-def test_config_template_errors(form_config):
- form_config['template'].value = 'no'
- with pytest.raises(Exception) as error:
- formmail2.make_handler('test', form_config)[1]
- assert error.value.message == 'Template not found at: no'
-
-
-def test_config_parse(form_config):
- assert form_config['url'].value == 'test/apply/submit'
- assert form_config['fields']['email'].value == 'mandatory, email'
-
-
-def test_sendmail_fail(log_path, response_for, form_data, sm_mock):
- sm_mock.side_effect = Exception('Sendmail Fail')
- with pytest.raises(HTTPError):
- response_for(form_data, log=True)
-
- with open(log_path) as log_file:
- row = DictReader(log_file).next()
- assert row != form_data
-
-
-def test_append_field_err(form_config, form_data, log_path):
- """ Checks that error logs are correctly written and appended
-
- Submits three forms, the second two have different fields to the first
- and should be added to the same log file as each other, and be identical
- """
- formmail2.log_formdata(form_data, log_path)
- del form_data['email']
-
- # submit two forms with fields that dont match the config
- # this should append the second form to the error log file
- with pytest.raises(Exception):
- formmail2.log_formdata(form_data, log_path)
- with pytest.raises(Exception):
- formmail2.log_formdata(form_data, log_path)
-
- with open(log_path + '_error') as error_log:
- reader = DictReader(error_log)
- assert reader.next() == form_data
- assert reader.next() == form_data
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 .
-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)
Index: sitescripts/formmail/web/formmail2.py
===================================================================
deleted file mode 100644
--- a/sitescripts/formmail/web/formmail2.py
+++ /dev/null
@@ -1,171 +0,0 @@
-# This file is part of the Adblock Plus web scripts,
-# Copyright (C) 2006-present eyeo GmbH
-#
-# Adblock Plus is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License version 3 as
-# published by the Free Software Foundation.
-#
-# Adblock Plus is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with Adblock Plus. If not, see .
-
-from __future__ import print_function
-
-import os
-import sys
-import datetime
-import traceback
-import collections
-from csv import DictWriter, DictReader
-
-import jinja2
-
-from sitescripts.utils import (get_config, sendMail, encode_email_address,
- get_template)
-from sitescripts.web import registerUrlHandler, form_handler
-
-
-def get_config_items():
- config = get_config()
- default_keys = set(config.defaults())
- for name, value in config.items('formmail2'):
- 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:
- 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)
-
- @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 ''
-
- return url, handler
-
-
-conf_dict = conf_parse(get_config_items())
-for name, config in conf_dict.items():
- url, handler = make_handler(name, config)
- registerUrlHandler(url, handler)
Index: tox.ini
===================================================================
--- a/tox.ini
+++ b/tox.ini
@@ -26,9 +26,8 @@
/sitescripts/extensions/utils.py : A107,A206,A301,A302,D200,D202,D204,D205,D208,D400,D401,D403,E501,E711,E722,F401,N802,N803,N805,N806,W504
/sitescripts/extensions/web/adblockbrowserUpdates.py : A107,E501,E722
/sitescripts/extensions/web/downloads.py : E501,E722,F401
- /sitescripts/formmail/web/formmail.py : A301,E501,E713,E722,N802
- /sitescripts/formmail/web/formmail2.py : E722
- /sitescripts/formmail/test/test_formmail2.py : D210,D400,D401
+ /sitescripts/formmail/web/formmail.py : E722
+ /sitescripts/formmail/test/test_formmail.py : D210,D400,D401
/sitescripts/hg/bin/irchook.py : A104
/sitescripts/hg/bin/update_issues.py: W504
/sitescripts/hg/test/update_issues.py : N802