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 59e12108fe83ded7d11fe9209e8b2fdd2504b7cc

stopforumspam-checker.py Raw
1# The amount of days within which to check if the user was reported
2LAST_SEEN_THRESHOLD = 180
3
4# The contact name to use in rejection messages
5CONTACT = "Admin at admin@example.com"
6
7from urllib.parse import urlencode
8from datetime import datetime
9import requests
10
11prompt_data = request.context.get("prompt_data")
12
13user_data = {}
14if ak_client_ip != ip_address('255.255.255.255'):
15 user_data['ip'] = str(ak_client_ip)
16if 'email' in prompt_data:
17 user_data['email'] = prompt_data['email']
18if 'username' in prompt_data:
19 user_data['username'] = prompt_data['username']
20
21if len(user_data) == 0:
22 ak_logger.warning("Could not find any user data to check against StopForumSpam")
23 return True
24
25context['sfs_user_data'] = user_data
26resp = requests.get("http://api.stopforumspam.org/api?json&" + urlencode(user_data))
27
28if resp.status_code != 200:
29 ak_message(f"There was an error creating your account. Please contact {CONTACT} with the following details: SFS HTTP Error {resp.status_code}")
30 ak_logger.warning("StopForumSpam HTTP error", status_code=resp.status_code)
31 return False
32
33data = resp.json()
34context['sfs_resp_data'] = data
35
36rejected_items = []
37def check_passes(name):
38 if 'lastseen' in data[name]:
39 last_seen = datetime.strptime(data[name]['lastseen'], '%Y-%m-%d %H:%M:%S')
40 time_elapsed = datetime.now() - last_seen
41 if time_elapsed.days < LAST_SEEN_THRESHOLD:
42 rejected_items.append(name)
43
44check_passes('ip')
45check_passes('email')
46check_passes('username')
47
48if len(rejected_items) > 0:
49 context['sfs_rejected_for'] = rejected_items
50 ak_message(f"Sorry, we cannot create your account at this time. Please come back later or contact {CONTACT} if the issue persists.")
51 return False
52
53return True