Last active 1716158530

This expression policy checks users against the StopForumSpam API during enrollment and rejects them if they've been reported within a specified threshold

Revision 7bc946a8d7939d3f8272553dcfd8035fb228bc0b

stopforumspam-checker.py Raw
1# The amount of days within which to check if the user was reported
2LAST_SEEN_THRESHOLD = 180
3LAST_SEEN_REGEX = r"<lastseen>(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})<\/lastseen>"
4
5# The contact name to use in rejection messages
6CONTACT = "Admin at admin@example.com"
7
8from urllib.parse import quote_plus
9from datetime import datetime
10import requests
11import re
12
13prompt_data = request.context.get("prompt_data")
14
15if 'email' in prompt_data:
16 req_url = f"http://api.stopforumspam.org/api?email={quote_plus(prompt_data['email'])}"
17 if 'username' in prompt_data:
18 req_url += f"&username={quote_plus(prompt_data['username'])}"
19
20 resp = requests.get(req_url)
21
22 if resp.status_code != 200:
23 ak_message(f"There was an error creating your account. Please contact {CONTACT} with the following details: SFS HTTP Error {resp.status_code}")
24 return False
25
26 matches = re.search(LAST_SEEN_REGEX, resp.text)
27 if matches is None:
28 return True
29
30 last_seen = datetime.strptime(matches.group(1), '%Y-%m-%d %H:%M:%S')
31 time_elapsed = datetime.now() - last_seen
32 if time_elapsed.days < LAST_SEEN_THRESHOLD:
33 ak_message(f"Sorry, we cannot approve your account at this time. Please come back later or contact {CONTACT} if the issue persists.")
34 return False
35
36return True
37