Última actividad 1716158530

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

stopforumspam-checker.py Sin formato
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
11context['sfs'] = {}
12prompt_data = request.context.get("prompt_data")
13
14user_data = {}
15if ak_client_ip != ip_address('255.255.255.255'):
16 user_data['ip'] = str(ak_client_ip)
17if 'email' in prompt_data:
18 user_data['email'] = prompt_data['email']
19if 'username' in prompt_data:
20 user_data['username'] = prompt_data['username']
21
22if len(user_data) == 0:
23 ak_logger.warning("Could not find any user data to check against StopForumSpam")
24 return True
25
26context['sfs']['request_data'] = user_data
27resp = requests.get("http://api.stopforumspam.org/api?json&" + urlencode(user_data))
28
29if resp.status_code != 200:
30 ak_message(f"There was an error creating your account. Please contact {CONTACT} with the following details: SFS HTTP Error {resp.status_code}")
31 ak_logger.warning("StopForumSpam HTTP error", status_code=resp.status_code)
32 return False
33
34data = resp.json()
35context['sfs']['response_data'] = data
36
37if data['success'] == 0 and 'error' in data:
38 ak_message(f"There was an error creating your account. Please contact {CONTACT} with the following details: SFS Error '{data['error']['message']}'")
39 ak_logger.warning("StopForumSpam error", msg=data['error']['message'])
40 return False
41
42rejected_items = []
43def check_passes(name):
44 if 'lastseen' in data[name]:
45 last_seen = datetime.strptime(data[name]['lastseen'], '%Y-%m-%d %H:%M:%S')
46 time_elapsed = datetime.now() - last_seen
47 if time_elapsed.days < LAST_SEEN_THRESHOLD:
48 rejected_items.append(name)
49
50check_passes('ip')
51check_passes('email')
52check_passes('username')
53
54if len(rejected_items) > 0:
55 context['sfs']['rejected_for'] = rejected_items
56 ak_message(f"Sorry, we cannot create your account at this time. Please come back later or contact {CONTACT} if the issue persists.")
57 return False
58
59return True