spamguard/spamguard.py

108 líneas
2.3 KiB
Python

from app.libraries.setup import Setup
from app.libraries.database import Database
from mastodon import Mastodon
import subprocess
import os
import sys
import os.path
def switch_reg_mode():
result = False
rvm_ruby = os.environ['HOME'] + "/.rbenv/shims/ruby"
os.environ['RAILS_ENV'] = 'production'
try:
switch_mode = subprocess.run([
rvm_ruby,
"bin/tootctl",
"settings",
"registrations",
"approved"
],
capture_output=True,
cwd=setup.mastodon_path,
env={'RAILS_ENV': 'production'},
check=True
)
if switch_mode.returncode == 0:
result = True
except subprocess.CalledProcessError as err:
pass
return result
def get_data(notification, signups):
notification_id = notification.id
if notification.type != 'admin.sign_up':
print(f'dismissing notification {notification_id}')
mastodon.notifications_dismiss(notification_id)
return signups
account_id = notification.account.id
username = notification.account.acct
db.write_user(username)
users = db.read_evo()
db.update_evo(users + 1)
signups = signups + 1
print(f'Dismissing notification {notification_id}')
mastodon.notifications_dismiss(notification_id)
return signups
###############################################################################
# main
if __name__ == '__main__':
setup = Setup()
db = Database()
mastodon = Mastodon(
access_token = setup.mastodon_app_token,
api_base_url= setup.mastodon_hostname
)
bot_notifications = mastodon.notifications()
signups = 0
for notif in bot_notifications:
signups = get_data(notif, signups)
if signups >= int(setup.threshold):
switched = switch_reg_mode()
else:
switched = False
if switched:
mastodon.status_post(f"This server is under spam attack (reached sign up threshold of {setup.threshold} per minute)\n,registration is now in approval mode", in_reply_to_id=None, )
sys.exit("This server is under spam attack (reached sign up threshold of {setup.threshold} per minute)\n,registration is now in approval mode")