Остання активність 1716158530

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

elara6331's Avatar elara6331 ревизій цього gist 1716158529. До ревизії

1 file changed, 4 insertions, 3 deletions

stopforumspam-checker.py

@@ -8,6 +8,7 @@ from urllib.parse import urlencode
8 8 from datetime import datetime
9 9 import requests
10 10
11 + context['sfs'] = {}
11 12 prompt_data = request.context.get("prompt_data")
12 13
13 14 user_data = {}
@@ -22,7 +23,7 @@ if len(user_data) == 0:
22 23 ak_logger.warning("Could not find any user data to check against StopForumSpam")
23 24 return True
24 25
25 - context['sfs_user_data'] = user_data
26 + context['sfs']['request_data'] = user_data
26 27 resp = requests.get("http://api.stopforumspam.org/api?json&" + urlencode(user_data))
27 28
28 29 if resp.status_code != 200:
@@ -31,7 +32,7 @@ if resp.status_code != 200:
31 32 return False
32 33
33 34 data = resp.json()
34 - context['sfs_resp_data'] = data
35 + context['sfs']['response_data'] = data
35 36
36 37 if data['success'] == 0 and 'error' in data:
37 38 ak_message(f"There was an error creating your account. Please contact {CONTACT} with the following details: SFS Error '{data['error']['message']}'")
@@ -51,7 +52,7 @@ check_passes('email')
51 52 check_passes('username')
52 53
53 54 if len(rejected_items) > 0:
54 - context['sfs_rejected_for'] = rejected_items
55 + context['sfs']['rejected_for'] = rejected_items
55 56 ak_message(f"Sorry, we cannot create your account at this time. Please come back later or contact {CONTACT} if the issue persists.")
56 57 return False
57 58

elara6331's Avatar elara6331 ревизій цього gist 1716158203. До ревизії

1 file changed, 5 insertions

stopforumspam-checker.py

@@ -33,6 +33,11 @@ if resp.status_code != 200:
33 33 data = resp.json()
34 34 context['sfs_resp_data'] = data
35 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 +
36 41 rejected_items = []
37 42 def check_passes(name):
38 43 if 'lastseen' in data[name]:

elara6331's Avatar elara6331 ревизій цього gist 1716157951. До ревизії

1 file changed, 17 insertions, 10 deletions

stopforumspam-checker.py

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

elara6331's Avatar elara6331 ревизій цього gist 1715543253. До ревизії

1 file changed, 32 insertions, 22 deletions

stopforumspam-checker.py

@@ -5,32 +5,42 @@ LAST_SEEN_REGEX = r"<lastseen>(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})<\/lastseen>"
5 5 # The contact name to use in rejection messages
6 6 CONTACT = "Admin at admin@example.com"
7 7
8 - from urllib.parse import quote_plus
8 + from urllib.parse import urlencode
9 9 from datetime import datetime
10 10 import requests
11 11 import re
12 12
13 13 prompt_data = request.context.get("prompt_data")
14 14
15 + user_data = {}
16 + if ak_client_ip != ip_address('255.255.255.255'):
17 + user_data['ip'] = str(ak_client_ip)
15 18 if 'email' in prompt_data:
16 - req_url = f"http://api.stopforumspam.org/api?email={quote_plus(prompt_data['email'])}"
17 - if 'username' in prompt_data:
18 - req_url += f"&username={quote_plus(prompt_data['username'])}"
19 -
20 - resp = requests.get(req_url)
21 -
22 - if resp.status_code != 200:
23 - ak_message(f"There was an error creating your account. Please contact {CONTACT} with the following details: SFS HTTP Error {resp.status_code}")
24 - return False
25 -
26 - matches = re.search(LAST_SEEN_REGEX, resp.text)
27 - if matches is None:
28 - return True
29 -
30 - last_seen = datetime.strptime(matches.group(1), '%Y-%m-%d %H:%M:%S')
31 - time_elapsed = datetime.now() - last_seen
32 - if time_elapsed.days < LAST_SEEN_THRESHOLD:
33 - ak_message(f"Sorry, we cannot approve your account at this time. Please come back later or contact {CONTACT} if the issue persists.")
34 - return False
35 -
36 - return True
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

elara6331's Avatar elara6331 ревизій цього gist 1715485243. До ревизії

Без змін

elara6331's Avatar elara6331 ревизій цього gist 1715485228. До ревизії

Без змін

elara6331's Avatar elara6331 ревизій цього gist 1715467479. До ревизії

1 file changed, 3 insertions, 2 deletions

stopforumspam-checker.py

@@ -5,6 +5,7 @@ LAST_SEEN_REGEX = r"<lastseen>(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})<\/lastseen>"
5 5 # The contact name to use in rejection messages
6 6 CONTACT = "Admin at admin@example.com"
7 7
8 + from urllib.parse import quote_plus
8 9 from datetime import datetime
9 10 import requests
10 11 import re
@@ -12,9 +13,9 @@ import re
12 13 prompt_data = request.context.get("prompt_data")
13 14
14 15 if 'email' in prompt_data:
15 - req_url = f"http://api.stopforumspam.org/api?email={prompt_data['email']}"
16 + req_url = f"http://api.stopforumspam.org/api?email={quote_plus(prompt_data['email'])}"
16 17 if 'username' in prompt_data:
17 - req_url += f"&username={prompt_data['username']}"
18 + req_url += f"&username={quote_plus(prompt_data['username'])}"
18 19
19 20 resp = requests.get(req_url)
20 21

elara6331's Avatar elara6331 ревизій цього gist 1715456640. До ревизії

1 file changed, 2 insertions, 2 deletions

stopforumspam-checker.py

@@ -19,7 +19,7 @@ if 'email' in prompt_data:
19 19 resp = requests.get(req_url)
20 20
21 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}")
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 23 return False
24 24
25 25 matches = re.search(LAST_SEEN_REGEX, resp.text)
@@ -29,7 +29,7 @@ if 'email' in prompt_data:
29 29 last_seen = datetime.strptime(matches.group(1), '%Y-%m-%d %H:%M:%S')
30 30 time_elapsed = datetime.now() - last_seen
31 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.")
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 33 return False
34 34
35 35 return True

elara6331's Avatar Elara6331 ревизій цього gist 1715456537. До ревизії

1 file changed, 6 insertions, 3 deletions

stopforumspam-checker.py

@@ -12,8 +12,11 @@ import re
12 12 prompt_data = request.context.get("prompt_data")
13 13
14 14 if 'email' in prompt_data:
15 - user_email = prompt_data['email']
16 - resp = requests.get(f"http://api.stopforumspam.org/api?email={user_email}")
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)
17 20
18 21 if resp.status_code != 200:
19 22 ak_message(f"There was an error creating your account. Please contact {contact} with the following details: SFS HTTP Error {resp.status_code}")
@@ -29,4 +32,4 @@ if 'email' in prompt_data:
29 32 ak_message(f"Sorry, we cannot approve your account at this time. Please come back later or contact {contact} if the issue persists.")
30 33 return False
31 34
32 - return True
35 + return True

elara6331's Avatar elara6331 ревизій цього gist 1715454832. До ревизії

Без змін

Новіше Пізніше