stopforumspam-checker.py
· 1.5 KiB · Python
Raw
# The amount of days within which to check if the user was reported
LAST_SEEN_THRESHOLD = 180
LAST_SEEN_REGEX = r"<lastseen>(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})<\/lastseen>"
# 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
import re
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_data'] = user_data
resp = requests.get("http://api.stopforumspam.org/api?" + 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
matches = re.search(LAST_SEEN_REGEX, resp.text)
if matches is None:
return True
last_seen = datetime.strptime(matches.group(1), '%Y-%m-%d %H:%M:%S')
time_elapsed = datetime.now() - last_seen
if time_elapsed.days < LAST_SEEN_THRESHOLD:
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 | LAST_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 |
6 | CONTACT = "Admin at admin@example.com" |
7 | |
8 | from urllib.parse import urlencode |
9 | from datetime import datetime |
10 | import requests |
11 | import re |
12 | |
13 | prompt_data = request.context.get("prompt_data") |
14 | |
15 | user_data = {} |
16 | if ak_client_ip != ip_address('255.255.255.255'): |
17 | user_data['ip'] = str(ak_client_ip) |
18 | if 'email' in prompt_data: |
19 | user_data['email'] = prompt_data['email'] |
20 | if 'username' in prompt_data: |
21 | user_data['username'] = prompt_data['username'] |
22 | |
23 | if len(user_data) == 0: |
24 | ak_logger.warning("Could not find any user data to check against StopForumSpam") |
25 | return True |
26 | |
27 | context['sfs_data'] = user_data |
28 | resp = requests.get("http://api.stopforumspam.org/api?" + urlencode(user_data)) |
29 | |
30 | if resp.status_code != 200: |
31 | ak_message(f"There was an error creating your account. Please contact {CONTACT} with the following details: SFS HTTP Error {resp.status_code}") |
32 | ak_logger.warning("StopForumSpam HTTP error", status_code=resp.status_code) |
33 | return False |
34 | |
35 | matches = re.search(LAST_SEEN_REGEX, resp.text) |
36 | if matches is None: |
37 | return True |
38 | |
39 | last_seen = datetime.strptime(matches.group(1), '%Y-%m-%d %H:%M:%S') |
40 | time_elapsed = datetime.now() - last_seen |
41 | |
42 | if time_elapsed.days < LAST_SEEN_THRESHOLD: |
43 | ak_message(f"Sorry, we cannot create your account at this time. Please come back later or contact {CONTACT} if the issue persists.") |
44 | return False |
45 | |
46 | return True |