2022-11-15 09:19:08 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
import gevent
|
|
|
|
import gevent_inotifyx as inotify
|
|
|
|
|
2023-03-12 13:03:58 +00:00
|
|
|
from files.helpers.config.const import *
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
_SETTINGS = {
|
2023-04-08 22:38:26 +00:00
|
|
|
"login_required": False,
|
|
|
|
"under_attack": False,
|
2022-11-30 19:03:46 +00:00
|
|
|
"bots": True,
|
|
|
|
"fart_mode": False,
|
|
|
|
"read_only_mode": False,
|
2023-03-25 21:35:13 +00:00
|
|
|
"offline_mode": False,
|
2022-11-30 19:03:46 +00:00
|
|
|
"signups": True,
|
2022-11-30 17:37:35 +00:00
|
|
|
"under_siege": False,
|
2023-05-12 15:27:46 +00:00
|
|
|
"dm_media": True,
|
2023-04-08 22:38:26 +00:00
|
|
|
"ddos_detected": False
|
2022-11-15 09:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_setting(setting:str):
|
|
|
|
if not setting or not isinstance(setting, str): raise TypeError()
|
|
|
|
return _SETTINGS[setting]
|
|
|
|
|
|
|
|
def get_settings() -> dict[str, bool]:
|
|
|
|
return _SETTINGS
|
|
|
|
|
|
|
|
def toggle_setting(setting:str):
|
|
|
|
val = not _SETTINGS[setting]
|
|
|
|
_SETTINGS[setting] = val
|
|
|
|
_save_settings()
|
|
|
|
return val
|
|
|
|
|
|
|
|
def reload_settings():
|
|
|
|
global _SETTINGS
|
|
|
|
if not os.path.isfile(SETTINGS_FILENAME):
|
|
|
|
_save_settings()
|
|
|
|
with open(SETTINGS_FILENAME, 'r', encoding='utf_8') as f:
|
2023-03-17 14:48:38 +00:00
|
|
|
x = f.read()
|
|
|
|
if x:
|
|
|
|
_SETTINGS = json.loads(x)
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
def _save_settings():
|
|
|
|
with open(SETTINGS_FILENAME, "w", encoding='utf_8') as f:
|
|
|
|
json.dump(_SETTINGS, f)
|
|
|
|
|
|
|
|
def start_watching_settings():
|
|
|
|
gevent.spawn(_settings_watcher, SETTINGS_FILENAME)
|
|
|
|
|
|
|
|
def _settings_watcher(filename):
|
|
|
|
fd = inotify.init()
|
|
|
|
try:
|
|
|
|
inotify.add_watch(fd, filename, inotify.IN_CLOSE_WRITE)
|
|
|
|
while True:
|
|
|
|
for event in inotify.get_events(fd, 0):
|
|
|
|
reload_settings()
|
|
|
|
break
|
|
|
|
gevent.sleep(0.5)
|
|
|
|
finally:
|
|
|
|
os.close(fd)
|