Index: sitescripts/formmail/web/formmail.py |
=================================================================== |
--- a/sitescripts/formmail/web/formmail.py |
+++ b/sitescripts/formmail/web/formmail.py |
@@ -16,39 +16,29 @@ |
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
import re |
-from urlparse import parse_qsl |
from sitescripts.utils import get_config, sendMail, setupStderr |
-from sitescripts.web import url_handler |
+from sitescripts.web import url_handler, form_handler |
@url_handler('/formmail') |
-def handleRequest(environ, start_response): |
+@form_handler |
+def handleRequest(environ, start_response, data): |
setupStderr(environ['wsgi.errors']) |
- 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' |
+ params = {} |
+ for field in ('name', 'email', 'subject', 'message'): |
+ value = data.get(field, '').strip() |
- try: |
- request_body_length = int(environ['CONTENT_LENGTH']) |
- except: |
- return 'Invalid or missing Content-Length header' |
+ if not value: |
+ start_response('400 Bad request') |
+ return ['No "%s" given' % field] |
- 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() |
- |
- 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' |
+ params[field] = value |
if not re.match(r'^\w[\w.+!-]+@\w[\w.-]+\.[a-zA-Z]{2,6}$', params['email']): |
- return 'Invalid email address' |
+ start_response('400 Bad request') |
+ return ['Invalid email address'] |
sendMail(get_config().get('formmail', 'template'), params) |
- return 'Message sent' |
+ |
+ start_response('200 OK') |
+ return ['Message sent'] |