|
""" |
|
MIT License |
|
|
|
Copyright (c) 2022 Aʙɪsʜɴᴏɪ |
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
of this software and associated documentation files (the "Software"), to deal |
|
in the Software without restriction, including without limitation the rights |
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
copies of the Software, and to permit persons to whom the Software is |
|
furnished to do so, subject to the following conditions: |
|
|
|
The above copyright notice and this permission notice shall be included in all |
|
copies or substantial portions of the Software. |
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
SOFTWARE. |
|
""" |
|
|
|
import threading |
|
|
|
from sqlalchemy import Boolean, Column, UnicodeText |
|
|
|
from Database.sql import BASE, SESSION |
|
|
|
|
|
class CleanerBlueTextChatSettings(BASE): |
|
__tablename__ = "cleaner_bluetext_chat_setting" |
|
chat_id = Column(UnicodeText, primary_key=True) |
|
is_enable = Column(Boolean, default=False) |
|
|
|
def __init__(self, chat_id, is_enable): |
|
self.chat_id = chat_id |
|
self.is_enable = is_enable |
|
|
|
def __repr__(self): |
|
return "ᴄʟᴇᴀɴ ʙʟᴜᴇ ᴛᴇxᴛ ғᴏʀ {}".format(self.chat_id) |
|
|
|
|
|
class CleanerBlueTextChat(BASE): |
|
__tablename__ = "cleaner_bluetext_chat_ignore_commands" |
|
chat_id = Column(UnicodeText, primary_key=True) |
|
command = Column(UnicodeText, primary_key=True) |
|
|
|
def __init__(self, chat_id, command): |
|
self.chat_id = chat_id |
|
self.command = command |
|
|
|
|
|
class CleanerBlueTextGlobal(BASE): |
|
__tablename__ = "cleaner_bluetext_global_ignore_commands" |
|
command = Column(UnicodeText, primary_key=True) |
|
|
|
def __init__(self, command): |
|
self.command = command |
|
|
|
|
|
CleanerBlueTextChatSettings.__table__.create(checkfirst=True) |
|
CleanerBlueTextChat.__table__.create(checkfirst=True) |
|
CleanerBlueTextGlobal.__table__.create(checkfirst=True) |
|
|
|
CLEANER_CHAT_SETTINGS = threading.RLock() |
|
CLEANER_CHAT_LOCK = threading.RLock() |
|
CLEANER_GLOBAL_LOCK = threading.RLock() |
|
|
|
CLEANER_CHATS = {} |
|
GLOBAL_IGNORE_COMMANDS = set() |
|
|
|
|
|
def set_cleanbt(chat_id, is_enable): |
|
with CLEANER_CHAT_SETTINGS: |
|
curr = SESSION.query(CleanerBlueTextChatSettings).get(str(chat_id)) |
|
|
|
if not curr: |
|
curr = CleanerBlueTextChatSettings(str(chat_id), is_enable) |
|
else: |
|
curr.is_enabled = is_enable |
|
|
|
if str(chat_id) not in CLEANER_CHATS: |
|
CLEANER_CHATS.setdefault( |
|
str(chat_id), {"setting": False, "commands": set()} |
|
) |
|
|
|
CLEANER_CHATS[str(chat_id)]["setting"] = is_enable |
|
|
|
SESSION.add(curr) |
|
SESSION.commit() |
|
|
|
|
|
def chat_ignore_command(chat_id, ignore): |
|
ignore = ignore.lower() |
|
with CLEANER_CHAT_LOCK: |
|
ignored = SESSION.query(CleanerBlueTextChat).get((str(chat_id), ignore)) |
|
|
|
if not ignored: |
|
if str(chat_id) not in CLEANER_CHATS: |
|
CLEANER_CHATS.setdefault( |
|
str(chat_id), {"setting": False, "commands": set()} |
|
) |
|
|
|
CLEANER_CHATS[str(chat_id)]["commands"].add(ignore) |
|
|
|
ignored = CleanerBlueTextChat(str(chat_id), ignore) |
|
SESSION.add(ignored) |
|
SESSION.commit() |
|
return True |
|
SESSION.close() |
|
return False |
|
|
|
|
|
def chat_unignore_command(chat_id, unignore): |
|
unignore = unignore.lower() |
|
with CLEANER_CHAT_LOCK: |
|
unignored = SESSION.query(CleanerBlueTextChat).get((str(chat_id), unignore)) |
|
|
|
if unignored: |
|
if str(chat_id) not in CLEANER_CHATS: |
|
CLEANER_CHATS.setdefault( |
|
str(chat_id), {"setting": False, "commands": set()} |
|
) |
|
if unignore in CLEANER_CHATS.get(str(chat_id)).get("commands"): |
|
CLEANER_CHATS[str(chat_id)]["commands"].remove(unignore) |
|
|
|
SESSION.delete(unignored) |
|
SESSION.commit() |
|
return True |
|
|
|
SESSION.close() |
|
return False |
|
|
|
|
|
def global_ignore_command(command): |
|
command = command.lower() |
|
with CLEANER_GLOBAL_LOCK: |
|
ignored = SESSION.query(CleanerBlueTextGlobal).get(str(command)) |
|
|
|
if not ignored: |
|
GLOBAL_IGNORE_COMMANDS.add(command) |
|
|
|
ignored = CleanerBlueTextGlobal(str(command)) |
|
SESSION.add(ignored) |
|
SESSION.commit() |
|
return True |
|
|
|
SESSION.close() |
|
return False |
|
|
|
|
|
def global_unignore_command(command): |
|
command = command.lower() |
|
with CLEANER_GLOBAL_LOCK: |
|
unignored = SESSION.query(CleanerBlueTextGlobal).get(str(command)) |
|
|
|
if unignored: |
|
if command in GLOBAL_IGNORE_COMMANDS: |
|
GLOBAL_IGNORE_COMMANDS.remove(command) |
|
|
|
SESSION.delete(command) |
|
SESSION.commit() |
|
return True |
|
|
|
SESSION.close() |
|
return False |
|
|
|
|
|
def is_command_ignored(chat_id, command): |
|
if command.lower() in GLOBAL_IGNORE_COMMANDS: |
|
return True |
|
|
|
if str(chat_id) in CLEANER_CHATS and command.lower() in CLEANER_CHATS.get( |
|
str(chat_id) |
|
).get("commands"): |
|
return True |
|
|
|
return False |
|
|
|
|
|
def is_enabled(chat_id): |
|
if str(chat_id) in CLEANER_CHATS: |
|
return CLEANER_CHATS.get(str(chat_id)).get("setting") |
|
|
|
return False |
|
|
|
|
|
def get_all_ignored(chat_id): |
|
if str(chat_id) in CLEANER_CHATS: |
|
LOCAL_IGNORE_COMMANDS = CLEANER_CHATS.get(str(chat_id)).get("commands") |
|
else: |
|
LOCAL_IGNORE_COMMANDS = set() |
|
|
|
return GLOBAL_IGNORE_COMMANDS, LOCAL_IGNORE_COMMANDS |
|
|
|
|
|
def __load_cleaner_list(): |
|
global GLOBAL_IGNORE_COMMANDS |
|
global CLEANER_CHATS |
|
|
|
try: |
|
GLOBAL_IGNORE_COMMANDS = { |
|
x.command for x in SESSION.query(CleanerBlueTextGlobal).all() |
|
} |
|
finally: |
|
SESSION.close() |
|
|
|
try: |
|
for x in SESSION.query(CleanerBlueTextChatSettings).all(): |
|
CLEANER_CHATS.setdefault(x.chat_id, {"setting": False, "commands": set()}) |
|
CLEANER_CHATS[x.chat_id]["setting"] = x.is_enable |
|
finally: |
|
SESSION.close() |
|
|
|
try: |
|
for x in SESSION.query(CleanerBlueTextChat).all(): |
|
CLEANER_CHATS.setdefault(x.chat_id, {"setting": False, "commands": set()}) |
|
CLEANER_CHATS[x.chat_id]["commands"].add(x.command) |
|
finally: |
|
SESSION.close() |
|
|
|
|
|
__load_cleaner_list() |
|
|