Dernière activité 1716158530

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

Révision fb312bcdc49129ca4df0d27493f855786d94b0d4

stopforumspam-checker.py Brut
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 datetime import datetime
9import requests
10import re
11
12prompt_data = request.context.get("prompt_data")
13
14if 'email' in prompt_data:
15 req_url = f"http://api.stopforumspam.org/api?email={prompt_data['email']}"
16 if 'username' in prompt_data:
17 req_url += f"&username={prompt_data['username']}"
18
19 resp = requests.get(req_url)
20
21 if resp.status_code != 200:
22 ak_message(f"There was an error creating your account. Please contact {CONTACT} with the following details: SFS HTTP Error {resp.status_code}")
23 return False
24
25 matches = re.search(LAST_SEEN_REGEX, resp.text)
26 if matches is None:
27 return True
28
29 last_seen = datetime.strptime(matches.group(1), '%Y-%m-%d %H:%M:%S')
30 time_elapsed = datetime.now() - last_seen
31 if time_elapsed.days < LAST_SEEN_THRESHOLD:
32 ak_message(f"Sorry, we cannot approve your account at this time. Please come back later or contact {CONTACT} if the issue persists.")
33 return False
34
35return True
36