Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/submitEmail/web/submitEmail.py

Issue 5177883412660224: Issue 2234 - Add a WSGI controller to collect email addresses for the Adblock Browser iOS launch (Closed)
Patch Set: Got rid of redundant variable Created April 22, 2015, 10:05 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/submitEmail/web/__init__.py ('k') | sitescripts/web.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
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/>.
17
18 import os
19 import errno
20 import fcntl
21 import random
22 import string
23 import wsgiref.util
24 from urlparse import parse_qs, urljoin
25 from urllib import urlencode
26
27 from sitescripts.utils import get_config, sendMail
28 from sitescripts.web import url_handler, form_handler
29
30 VERIFICATION_PATH = '/verifyEmail'
31 VERIFICATION_CODE_PARAM = 'code'
32
33 def generate_verification_code():
34 return ''.join(random.sample(string.letters + string.digits, 32))
35
36 def get_filename_for_verification_code(config, verification_code):
37 directory = config.get('submitEmail', 'pendingVerficationsDirectory')
38 return os.path.join(directory, verification_code)
39
40 def create_file_for_verification_code(config):
41 flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY | getattr(os, 'O_BINARY', 0)
42
43 while True:
44 verification_code = generate_verification_code()
45 filename = get_filename_for_verification_code(config, verification_code)
46
47 try:
48 return (os.open(filename, flags), verification_code)
49 except OSError as e:
50 if e.errno != errno.EEXIST:
51 raise
52
53 def verify_email_address(config, verification_code):
54 if verification_code in ('', os.path.curdir, os.path.pardir):
55 return False
56 if os.path.sep in verification_code:
57 return False
58
59 verification_filename = get_filename_for_verification_code(config, verificatio n_code)
60 try:
61 file = open(verification_filename, 'rb')
62 except IOError as e:
63 if e.errno == errno.ENOENT:
64 return False
65 raise
66
67 with file:
68 email = file.read()
69
70 verified_filename = config.get('submitEmail', 'verifiedEmailAddressesFile')
71 with open(verified_filename, 'ab', 0) as file:
72 fcntl.lockf(file, fcntl.LOCK_EX)
73 try:
74 print >>file, email
75 finally:
76 fcntl.lockf(file, fcntl.LOCK_UN)
77
78 try:
79 os.unlink(verification_filename)
80 except OSError as e:
81 if e.errno != errno.ENOENT:
82 raise
83
84 return True
85
86 @url_handler('/submitEmail')
87 @form_handler
88 def submitEmail(environ, start_response, data):
89 email = data.get('email', '').strip()
90 if not email or '\r' in email or '\n' in email:
91 start_response('400 Bad Request', [('Content-Type', 'text/plain')])
92 if email:
93 return ['No newlines allowed in email address.']
94 return ['No email address given.']
95
96 config = get_config()
97 fd, verification_code = create_file_for_verification_code(config)
98 try:
99 os.write(fd, email.encode('utf-8'))
100 finally:
101 os.close(fd)
102
103 sendMail(
104 config.get('submitEmail', 'verificationEmailTemplate'),
105 {
106 'recipient': email,
107 'verification_url': '%s?%s' % (
108 urljoin(wsgiref.util.application_uri(environ), VERIFICATION_PATH),
109 urlencode([(VERIFICATION_CODE_PARAM, verification_code)])
110 )
111 }
112 )
113
114 start_response('200 OK', [('Content-Type', 'text/plain')])
115 return ["Thanks for your submission! You'll receive a verification email short ly."]
116
117 @url_handler(VERIFICATION_PATH)
118 def verifyEmail(environ, start_response):
119 config = get_config()
120
121 params = parse_qs(environ.get('QUERY_STRING', ''))
122 verification_code = params.get(VERIFICATION_CODE_PARAM, [''])[0]
123
124 if verify_email_address(config, verification_code):
125 option = 'successfulVerificationRedirectLocation'
126 else:
127 option = 'unknownVerificationCodeRedirectLocation'
128
129 start_response('303 See other', [('Location', config.get('submitEmail', option ))])
130 return []
OLDNEW
« no previous file with comments | « sitescripts/submitEmail/web/__init__.py ('k') | sitescripts/web.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld