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