diff options
author | Mark Sapiro <mark@msapiro.net> | 2019-06-19 16:56:49 -0700 |
---|---|---|
committer | Mark Sapiro <mark@msapiro.net> | 2019-06-19 16:56:49 -0700 |
commit | 1799a87556e18776e64df28ff2ac4fee190f2dc1 (patch) | |
tree | 670eea11f001d2273da50af94c9f949b85ded058 /Mailman/Utils.py | |
parent | 56188e427f80ed350b6608ce47124402c90b9d40 (diff) | |
parent | 91203be694e4ca836b862b7921e119b2f55a8307 (diff) | |
download | mailman2-1799a87556e18776e64df28ff2ac4fee190f2dc1.tar.gz mailman2-1799a87556e18776e64df28ff2ac4fee190f2dc1.tar.xz mailman2-1799a87556e18776e64df28ff2ac4fee190f2dc1.zip |
Implement Ralf Jung's captcha feature for the subscribe form.
Diffstat (limited to '')
-rw-r--r-- | Mailman/Utils.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Mailman/Utils.py b/Mailman/Utils.py index 10629fc4..36fbd1f9 100644 --- a/Mailman/Utils.py +++ b/Mailman/Utils.py @@ -1576,3 +1576,33 @@ def banned_domain(email): if not re.search(r'127\.0\.1\.255$', text, re.MULTILINE): return True return False + + +def captcha_display(mlist, lang, captchas): + """Returns a CAPTCHA question, the HTML for the answer box, and + the data to be put into the CSRF token""" + if not lang in captchas: + lang = 'en' + captchas = captchas[lang] + idx = random.randrange(len(captchas)) + question = captchas[idx][0] + box_html = mlist.FormatBox('captcha_answer', size=30) + # Remember to encode the language in the index so that we can get it out + # again! + return (websafe(question), box_html, lang + "-" + str(idx)) + +def captcha_verify(idx, given_answer, captchas): + try: + (lang, idx) = idx.split("-") + idx = int(idx) + except ValueError: + return False + if not lang in captchas: + return False + captchas = captchas[lang] + if not idx in range(len(captchas)): + return False + # Check the given answer. + # We append a `$` to emulate `re.fullmatch`. + correct_answer_pattern = captchas[idx][1] + "$" + return re.match(correct_answer_pattern, given_answer) |