Karma
commited on
Commit
·
247097f
1
Parent(s):
b9c2de7
Delete Database directory
Browse files- Database/mongodb/db.py +0 -6
- Database/mongodb/fsub_db.py +0 -18
- Database/mongodb/karma_mongo.py +0 -122
- Database/mongodb/mongodb.py +0 -75
- Database/mongodb/toggle_mongo.py +0 -50
- Database/mongodb/users_db.py +0 -102
- Database/mongodb/whispers.py +0 -19
- Database/sql/__init__.py +0 -27
- Database/sql/afk_sql.py +0 -99
- Database/sql/antiflood_sql.py +0 -145
- Database/sql/approve_sql.py +0 -61
- Database/sql/blacklist_sql.py +0 -200
- Database/sql/blacklistusers_sql.py +0 -69
- Database/sql/blsticker_sql.py +0 -200
- Database/sql/connection_sql.py +0 -204
- Database/sql/cust_filters_sql.py +0 -299
- Database/sql/disable_sql.py +0 -105
- Database/sql/feds_sql.py +0 -931
- Database/sql/global_bans_sql.py +0 -167
- Database/sql/kuki_sql.py +0 -42
- Database/sql/log_channel_sql.py +0 -156
- Database/sql/notes_sql.py +0 -187
- Database/sql/rules_sql.py +0 -58
- Database/sql/userinfo_sql.py +0 -76
- Database/sql/users_sql.py +0 -235
- Database/sql/warns_sql.py +0 -327
- Database/sql/welcome_sql.py +0 -428
Database/mongodb/db.py
DELETED
@@ -1,6 +0,0 @@
|
|
1 |
-
from motor.motor_asyncio import AsyncIOMotorClient as MongoClient
|
2 |
-
|
3 |
-
from Mikobot import DB_NAME, MONGO_DB_URI
|
4 |
-
|
5 |
-
mongo = MongoClient(MONGO_DB_URI)
|
6 |
-
dbname = mongo[DB_NAME]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/mongodb/fsub_db.py
DELETED
@@ -1,18 +0,0 @@
|
|
1 |
-
from Infamous.temp import dbname
|
2 |
-
|
3 |
-
fsub = dbname.force_sub
|
4 |
-
|
5 |
-
|
6 |
-
def fs_settings(chat_id: int):
|
7 |
-
_x = fsub.find_one({"chat_id": chat_id})
|
8 |
-
if _x:
|
9 |
-
return _x
|
10 |
-
return None
|
11 |
-
|
12 |
-
|
13 |
-
def add_channel(chat_id: int, channel):
|
14 |
-
fsub.update_one({"chat_id": chat_id}, {"$set": {"channel": channel}}, upsert=True)
|
15 |
-
|
16 |
-
|
17 |
-
def disapprove(chat_id: int):
|
18 |
-
fsub.delete_one({"chat_id": chat_id})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/mongodb/karma_mongo.py
DELETED
@@ -1,122 +0,0 @@
|
|
1 |
-
from typing import Dict, Union
|
2 |
-
|
3 |
-
from pymongo import MongoClient
|
4 |
-
|
5 |
-
from Mikobot import DB_NAME, MONGO_DB_URI
|
6 |
-
|
7 |
-
client = MongoClient(MONGO_DB_URI)
|
8 |
-
db = client[DB_NAME]
|
9 |
-
|
10 |
-
coupledb = db.couple
|
11 |
-
karmadb = db.karma
|
12 |
-
|
13 |
-
|
14 |
-
async def _get_lovers(chat_id: int):
|
15 |
-
lovers = coupledb.find_one({"chat_id": chat_id})
|
16 |
-
if lovers:
|
17 |
-
lovers = lovers["couple"]
|
18 |
-
else:
|
19 |
-
lovers = {}
|
20 |
-
return lovers
|
21 |
-
|
22 |
-
|
23 |
-
async def get_couple(chat_id: int, date: str):
|
24 |
-
lovers = await _get_lovers(chat_id)
|
25 |
-
if date in lovers:
|
26 |
-
return lovers[date]
|
27 |
-
else:
|
28 |
-
return False
|
29 |
-
|
30 |
-
|
31 |
-
async def save_couple(chat_id: int, date: str, couple: dict):
|
32 |
-
lovers = await _get_lovers(chat_id)
|
33 |
-
lovers[date] = couple
|
34 |
-
coupledb.update_one({"chat_id": chat_id}, {"$set": {"couple": lovers}}, upsert=True)
|
35 |
-
|
36 |
-
|
37 |
-
async def get_karmas_count() -> dict:
|
38 |
-
chats = karmadb.find({"chat_id": {"$lt": 0}})
|
39 |
-
if not chats:
|
40 |
-
return {}
|
41 |
-
chats_count = 0
|
42 |
-
karmas_count = 0
|
43 |
-
for chat in await chats.to_list(length=1000000):
|
44 |
-
for i in chat["karma"]:
|
45 |
-
karma_ = chat["karma"][i]["karma"]
|
46 |
-
if karma_ > 0:
|
47 |
-
karmas_count += karma_
|
48 |
-
chats_count += 1
|
49 |
-
return {"chats_count": chats_count, "karmas_count": karmas_count}
|
50 |
-
|
51 |
-
|
52 |
-
async def user_global_karma(user_id) -> int:
|
53 |
-
chats = karmadb.find({"chat_id": {"$lt": 0}})
|
54 |
-
if not chats:
|
55 |
-
return 0
|
56 |
-
total_karma = 0
|
57 |
-
for chat in await chats.to_list(length=1000000):
|
58 |
-
karma = await get_karma(chat["chat_id"], await int_to_alpha(user_id))
|
59 |
-
if karma and (int(karma["karma"]) > 0):
|
60 |
-
total_karma += int(karma["karma"])
|
61 |
-
return total_karma
|
62 |
-
|
63 |
-
|
64 |
-
async def get_karmas(chat_id: int) -> Dict[str, int]:
|
65 |
-
karma = karmadb.find_one({"chat_id": chat_id})
|
66 |
-
if not karma:
|
67 |
-
return {}
|
68 |
-
return karma["karma"]
|
69 |
-
|
70 |
-
|
71 |
-
async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
|
72 |
-
name = name.lower().strip()
|
73 |
-
karmas = await get_karmas(chat_id)
|
74 |
-
if name in karmas:
|
75 |
-
return karmas[name]
|
76 |
-
|
77 |
-
|
78 |
-
async def update_karma(chat_id: int, name: str, karma: dict):
|
79 |
-
name = name.lower().strip()
|
80 |
-
karmas = await get_karmas(chat_id)
|
81 |
-
karmas[name] = karma
|
82 |
-
karmadb.update_one({"chat_id": chat_id}, {"$set": {"karma": karmas}}, upsert=True)
|
83 |
-
|
84 |
-
|
85 |
-
async def is_karma_on(chat_id: int) -> bool:
|
86 |
-
chat = karmadb.find_one({"chat_id_toggle": chat_id})
|
87 |
-
if not chat:
|
88 |
-
return True
|
89 |
-
return False
|
90 |
-
|
91 |
-
|
92 |
-
async def karma_on(chat_id: int):
|
93 |
-
is_karma = await is_karma_on(chat_id)
|
94 |
-
if is_karma:
|
95 |
-
return
|
96 |
-
return karmadb.delete_one({"chat_id_toggle": chat_id})
|
97 |
-
|
98 |
-
|
99 |
-
async def karma_off(chat_id: int):
|
100 |
-
is_karma = await is_karma_on(chat_id)
|
101 |
-
if not is_karma:
|
102 |
-
return
|
103 |
-
return karmadb.insert_one({"chat_id_toggle": chat_id})
|
104 |
-
|
105 |
-
|
106 |
-
async def int_to_alpha(user_id: int) -> str:
|
107 |
-
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
|
108 |
-
text = ""
|
109 |
-
user_id = str(user_id)
|
110 |
-
for i in user_id:
|
111 |
-
text += alphabet[int(i)]
|
112 |
-
return text
|
113 |
-
|
114 |
-
|
115 |
-
async def alpha_to_int(user_id_alphabet: str) -> int:
|
116 |
-
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
|
117 |
-
user_id = ""
|
118 |
-
for i in user_id_alphabet:
|
119 |
-
index = alphabet.index(i)
|
120 |
-
user_id += str(index)
|
121 |
-
user_id = int(user_id)
|
122 |
-
return user_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/mongodb/mongodb.py
DELETED
@@ -1,75 +0,0 @@
|
|
1 |
-
from sys import exit as exiter
|
2 |
-
|
3 |
-
from pymongo import MongoClient
|
4 |
-
from pymongo.errors import PyMongoError
|
5 |
-
|
6 |
-
from Mikobot import DB_NAME, LOGGER, MONGO_DB_URI
|
7 |
-
|
8 |
-
try:
|
9 |
-
Mikobot_db_client = MongoClient(MONGO_DB_URI)
|
10 |
-
except PyMongoError as f:
|
11 |
-
LOGGER.error(f"Error in Mongodb: {f}")
|
12 |
-
exiter(1)
|
13 |
-
Mikobot_main_db = Mikobot_db_client[DB_NAME]
|
14 |
-
|
15 |
-
|
16 |
-
class MongoDB:
|
17 |
-
"""Class for interacting with Bot database."""
|
18 |
-
|
19 |
-
def __init__(self, collection) -> None:
|
20 |
-
self.collection = Mikobot_main_db[collection]
|
21 |
-
|
22 |
-
# Insert one entry into collection
|
23 |
-
def insert_one(self, document):
|
24 |
-
result = self.collection.insert_one(document)
|
25 |
-
return repr(result.inserted_id)
|
26 |
-
|
27 |
-
# Find one entry from collection
|
28 |
-
def find_one(self, query):
|
29 |
-
result = self.collection.find_one(query)
|
30 |
-
if result:
|
31 |
-
return result
|
32 |
-
return False
|
33 |
-
|
34 |
-
# Find entries from collection
|
35 |
-
def find_all(self, query=None):
|
36 |
-
if query is None:
|
37 |
-
query = {}
|
38 |
-
return list(self.collection.find(query))
|
39 |
-
|
40 |
-
# Count entries from collection
|
41 |
-
def count(self, query=None):
|
42 |
-
if query is None:
|
43 |
-
query = {}
|
44 |
-
return self.collection.count_documents(query)
|
45 |
-
|
46 |
-
# Delete entry/entries from collection
|
47 |
-
def delete_one(self, query):
|
48 |
-
self.collection.delete_many(query)
|
49 |
-
return self.collection.count_documents({})
|
50 |
-
|
51 |
-
# Replace one entry in collection
|
52 |
-
def replace(self, query, new_data):
|
53 |
-
old = self.collection.find_one(query)
|
54 |
-
_id = old["_id"]
|
55 |
-
self.collection.replace_one({"_id": _id}, new_data)
|
56 |
-
new = self.collection.find_one({"_id": _id})
|
57 |
-
return old, new
|
58 |
-
|
59 |
-
# Update one entry from collection
|
60 |
-
def update(self, query, update):
|
61 |
-
result = self.collection.update_one(query, {"$set": update})
|
62 |
-
new_document = self.collection.find_one(query)
|
63 |
-
return result.modified_count, new_document
|
64 |
-
|
65 |
-
@staticmethod
|
66 |
-
def close():
|
67 |
-
return Mikobot_db_client.close()
|
68 |
-
|
69 |
-
|
70 |
-
def __connect_first():
|
71 |
-
_ = MongoDB("test")
|
72 |
-
LOGGER.info("Initialized Mongo Database!\n")
|
73 |
-
|
74 |
-
|
75 |
-
__connect_first()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/mongodb/toggle_mongo.py
DELETED
@@ -1,50 +0,0 @@
|
|
1 |
-
from Database.mongodb.db import *
|
2 |
-
|
3 |
-
dwelcomedb = dbname.dwelcome
|
4 |
-
nsfwdb = dbname.nsfw
|
5 |
-
nekomodedb = dbname.nekomode
|
6 |
-
|
7 |
-
|
8 |
-
async def is_dwelcome_on(chat_id: int) -> bool:
|
9 |
-
chat = await dwelcomedb.find_one({"chat_id_toggle": chat_id})
|
10 |
-
return not bool(chat)
|
11 |
-
|
12 |
-
|
13 |
-
async def dwelcome_on(chat_id: int):
|
14 |
-
await dwelcomedb.delete_one({"chat_id_toggle": chat_id})
|
15 |
-
|
16 |
-
|
17 |
-
async def dwelcome_off(chat_id: int):
|
18 |
-
await dwelcomedb.insert_one({"chat_id_toggle": chat_id})
|
19 |
-
|
20 |
-
|
21 |
-
async def is_nsfw_on(chat_id: int) -> bool:
|
22 |
-
chat = await nsfwdb.find_one({"chat_id": chat_id})
|
23 |
-
return chat
|
24 |
-
|
25 |
-
|
26 |
-
async def nsfw_on(chat_id: int):
|
27 |
-
is_nsfw = await is_nsfw_on(chat_id)
|
28 |
-
if is_nsfw:
|
29 |
-
return
|
30 |
-
return await nsfwdb.insert_one({"chat_id": chat_id})
|
31 |
-
|
32 |
-
|
33 |
-
async def nsfw_off(chat_id: int):
|
34 |
-
is_nsfw = await is_nsfw_on(chat_id)
|
35 |
-
if not is_nsfw:
|
36 |
-
return
|
37 |
-
return await nsfwdb.delete_one({"chat_id": chat_id})
|
38 |
-
|
39 |
-
|
40 |
-
async def is_nekomode_on(chat_id: int) -> bool:
|
41 |
-
chat = await nekomodedb.find_one({"chat_id_toggle": chat_id})
|
42 |
-
return not bool(chat)
|
43 |
-
|
44 |
-
|
45 |
-
async def nekomode_on(chat_id: int):
|
46 |
-
await nekomodedb.delete_one({"chat_id_toggle": chat_id})
|
47 |
-
|
48 |
-
|
49 |
-
async def nekomode_off(chat_id: int):
|
50 |
-
await nekomodedb.insert_one({"chat_id_toggle": chat_id})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/mongodb/users_db.py
DELETED
@@ -1,102 +0,0 @@
|
|
1 |
-
from threading import RLock
|
2 |
-
from time import time
|
3 |
-
|
4 |
-
from Database.mongodb.mongodb import MongoDB
|
5 |
-
from Mikobot import LOGGER
|
6 |
-
|
7 |
-
INSERTION_LOCK = RLock()
|
8 |
-
|
9 |
-
|
10 |
-
class Users(MongoDB):
|
11 |
-
"""Class to manage users for bot."""
|
12 |
-
|
13 |
-
db_name = "users"
|
14 |
-
|
15 |
-
def __init__(self, user_id: int) -> None:
|
16 |
-
super().__init__(self.db_name)
|
17 |
-
self.user_id = user_id
|
18 |
-
self.user_info = self.__ensure_in_db()
|
19 |
-
|
20 |
-
def update_user(self, name: str, username: str = None):
|
21 |
-
with INSERTION_LOCK:
|
22 |
-
if name != self.user_info["name"] or username != self.user_info["username"]:
|
23 |
-
return self.update(
|
24 |
-
{"_id": self.user_id},
|
25 |
-
{"username": username, "name": name},
|
26 |
-
)
|
27 |
-
return True
|
28 |
-
|
29 |
-
def delete_user(self):
|
30 |
-
with INSERTION_LOCK:
|
31 |
-
return self.delete_one({"_id": self.user_id})
|
32 |
-
|
33 |
-
@staticmethod
|
34 |
-
def count_users():
|
35 |
-
with INSERTION_LOCK:
|
36 |
-
collection = MongoDB(Users.db_name)
|
37 |
-
return collection.count()
|
38 |
-
|
39 |
-
def get_my_info(self):
|
40 |
-
with INSERTION_LOCK:
|
41 |
-
return self.user_info
|
42 |
-
|
43 |
-
@staticmethod
|
44 |
-
def list_users():
|
45 |
-
with INSERTION_LOCK:
|
46 |
-
collection = MongoDB(Users.db_name)
|
47 |
-
return collection.find_all()
|
48 |
-
|
49 |
-
@staticmethod
|
50 |
-
def get_user_info(user_id: int or str):
|
51 |
-
with INSERTION_LOCK:
|
52 |
-
collection = MongoDB(Users.db_name)
|
53 |
-
if isinstance(user_id, int):
|
54 |
-
curr = collection.find_one({"_id": user_id})
|
55 |
-
elif isinstance(user_id, str):
|
56 |
-
# user_id[1:] because we don't want the '@' in the username
|
57 |
-
# search!
|
58 |
-
curr = collection.find_one({"username": user_id[1:]})
|
59 |
-
else:
|
60 |
-
curr = None
|
61 |
-
|
62 |
-
if curr:
|
63 |
-
return curr
|
64 |
-
|
65 |
-
return {}
|
66 |
-
|
67 |
-
def __ensure_in_db(self):
|
68 |
-
chat_data = self.find_one({"_id": self.user_id})
|
69 |
-
if not chat_data:
|
70 |
-
new_data = {"_id": self.user_id, "username": "", "name": "unknown_till_now"}
|
71 |
-
self.insert_one(new_data)
|
72 |
-
LOGGER.info(f"Initialized User Document for {self.user_id}")
|
73 |
-
return new_data
|
74 |
-
return chat_data
|
75 |
-
|
76 |
-
@staticmethod
|
77 |
-
def load_from_db():
|
78 |
-
with INSERTION_LOCK:
|
79 |
-
collection = MongoDB(Users.db_name)
|
80 |
-
return collection.find_all()
|
81 |
-
|
82 |
-
@staticmethod
|
83 |
-
def repair_db(collection):
|
84 |
-
all_data = collection.find_all()
|
85 |
-
keys = {"username": "", "name": "unknown_till_now"}
|
86 |
-
for data in all_data:
|
87 |
-
for key, val in keys.items():
|
88 |
-
try:
|
89 |
-
_ = data[key]
|
90 |
-
except KeyError:
|
91 |
-
LOGGER.warning(
|
92 |
-
f"Repairing Users Database - setting '{key}:{val}' for {data['_id']}",
|
93 |
-
)
|
94 |
-
collection.update({"_id": data["_id"]}, {key: val})
|
95 |
-
|
96 |
-
|
97 |
-
def __pre_req_users():
|
98 |
-
start = time()
|
99 |
-
LOGGER.info("Starting Users Database Repair...")
|
100 |
-
collection = MongoDB(Users.db_name)
|
101 |
-
Users.repair_db(collection)
|
102 |
-
LOGGER.info(f"Done in {round((time() - start), 3)}s!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/mongodb/whispers.py
DELETED
@@ -1,19 +0,0 @@
|
|
1 |
-
from Database.mongodb.db import dbname
|
2 |
-
|
3 |
-
collection = dbname["whisper"]
|
4 |
-
|
5 |
-
|
6 |
-
class Whispers:
|
7 |
-
@staticmethod
|
8 |
-
async def add_whisper(WhisperId, WhisperData):
|
9 |
-
whisper = {"WhisperId": WhisperId, "whisperData": WhisperData}
|
10 |
-
await collection.insert_one(whisper)
|
11 |
-
|
12 |
-
@staticmethod
|
13 |
-
async def del_whisper(WhisperId):
|
14 |
-
await collection.delete_one({"WhisperId": WhisperId})
|
15 |
-
|
16 |
-
@staticmethod
|
17 |
-
async def get_whisper(WhisperId):
|
18 |
-
whisper = await collection.find_one({"WhisperId": WhisperId})
|
19 |
-
return whisper["whisperData"] if whisper else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/__init__.py
DELETED
@@ -1,27 +0,0 @@
|
|
1 |
-
from sqlalchemy import create_engine
|
2 |
-
from sqlalchemy.ext.declarative import declarative_base
|
3 |
-
from sqlalchemy.orm import scoped_session, sessionmaker
|
4 |
-
|
5 |
-
from Mikobot import DB_URI
|
6 |
-
from Mikobot import LOGGER as log
|
7 |
-
|
8 |
-
if DB_URI and DB_URI.startswith("postgres://"):
|
9 |
-
DB_URI = DB_URI.replace("postgres://", "postgresql://", 1)
|
10 |
-
|
11 |
-
|
12 |
-
def start() -> scoped_session:
|
13 |
-
engine = create_engine(DB_URI, client_encoding="utf8")
|
14 |
-
log.info("[PostgreSQL] Connecting to database......")
|
15 |
-
BASE.metadata.bind = engine
|
16 |
-
BASE.metadata.create_all(engine)
|
17 |
-
return scoped_session(sessionmaker(bind=engine, autoflush=False))
|
18 |
-
|
19 |
-
|
20 |
-
BASE = declarative_base()
|
21 |
-
try:
|
22 |
-
SESSION = start()
|
23 |
-
except Exception as e:
|
24 |
-
log.exception(f"[PostgreSQL] Failed to connect due to {e}")
|
25 |
-
exit()
|
26 |
-
|
27 |
-
log.info("[PostgreSQL] Connection successful, session started.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/afk_sql.py
DELETED
@@ -1,99 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
from datetime import datetime
|
3 |
-
|
4 |
-
from sqlalchemy import BigInteger, Boolean, Column, DateTime, UnicodeText
|
5 |
-
|
6 |
-
from Database.sql import BASE, SESSION
|
7 |
-
|
8 |
-
|
9 |
-
class AFK(BASE):
|
10 |
-
__tablename__ = "afk_users"
|
11 |
-
|
12 |
-
user_id = Column(BigInteger, primary_key=True)
|
13 |
-
is_afk = Column(Boolean)
|
14 |
-
reason = Column(UnicodeText)
|
15 |
-
time = Column(DateTime)
|
16 |
-
|
17 |
-
def __init__(self, user_id: int, reason: str = "", is_afk: bool = True):
|
18 |
-
self.user_id = user_id
|
19 |
-
self.reason = reason
|
20 |
-
self.is_afk = is_afk
|
21 |
-
self.time = datetime.now()
|
22 |
-
|
23 |
-
def __repr__(self):
|
24 |
-
return "afk_status for {}".format(self.user_id)
|
25 |
-
|
26 |
-
|
27 |
-
AFK.__table__.create(checkfirst=True)
|
28 |
-
INSERTION_LOCK = threading.RLock()
|
29 |
-
|
30 |
-
AFK_USERS = {}
|
31 |
-
|
32 |
-
|
33 |
-
def is_afk(user_id):
|
34 |
-
return user_id in AFK_USERS
|
35 |
-
|
36 |
-
|
37 |
-
def check_afk_status(user_id):
|
38 |
-
try:
|
39 |
-
return SESSION.query(AFK).get(user_id)
|
40 |
-
finally:
|
41 |
-
SESSION.close()
|
42 |
-
|
43 |
-
|
44 |
-
def set_afk(user_id, reason=""):
|
45 |
-
with INSERTION_LOCK:
|
46 |
-
curr = SESSION.query(AFK).get(user_id)
|
47 |
-
if not curr:
|
48 |
-
curr = AFK(user_id, reason, True)
|
49 |
-
else:
|
50 |
-
curr.is_afk = True
|
51 |
-
|
52 |
-
AFK_USERS[user_id] = {"reason": reason, "time": curr.time}
|
53 |
-
|
54 |
-
SESSION.add(curr)
|
55 |
-
SESSION.commit()
|
56 |
-
|
57 |
-
|
58 |
-
def rm_afk(user_id):
|
59 |
-
with INSERTION_LOCK:
|
60 |
-
curr = SESSION.query(AFK).get(user_id)
|
61 |
-
if curr:
|
62 |
-
if user_id in AFK_USERS: # sanity check
|
63 |
-
del AFK_USERS[user_id]
|
64 |
-
|
65 |
-
SESSION.delete(curr)
|
66 |
-
SESSION.commit()
|
67 |
-
return True
|
68 |
-
|
69 |
-
SESSION.close()
|
70 |
-
return False
|
71 |
-
|
72 |
-
|
73 |
-
def toggle_afk(user_id, reason=""):
|
74 |
-
with INSERTION_LOCK:
|
75 |
-
curr = SESSION.query(AFK).get(user_id)
|
76 |
-
if not curr:
|
77 |
-
curr = AFK(user_id, reason, True)
|
78 |
-
elif curr.is_afk:
|
79 |
-
curr.is_afk = False
|
80 |
-
elif not curr.is_afk:
|
81 |
-
curr.is_afk = True
|
82 |
-
SESSION.add(curr)
|
83 |
-
SESSION.commit()
|
84 |
-
|
85 |
-
|
86 |
-
def __load_afk_users():
|
87 |
-
global AFK_USERS
|
88 |
-
try:
|
89 |
-
all_afk = SESSION.query(AFK).all()
|
90 |
-
AFK_USERS = {
|
91 |
-
user.user_id: {"reason": user.reason, "time": user.time}
|
92 |
-
for user in all_afk
|
93 |
-
if user.is_afk
|
94 |
-
}
|
95 |
-
finally:
|
96 |
-
SESSION.close()
|
97 |
-
|
98 |
-
|
99 |
-
__load_afk_users()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/antiflood_sql.py
DELETED
@@ -1,145 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import BigInteger, Column, Integer, String, UnicodeText
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
|
7 |
-
DEF_COUNT = 1
|
8 |
-
DEF_LIMIT = 0
|
9 |
-
DEF_OBJ = (None, DEF_COUNT, DEF_LIMIT)
|
10 |
-
|
11 |
-
|
12 |
-
class FloodControl(BASE):
|
13 |
-
__tablename__ = "antiflood"
|
14 |
-
chat_id = Column(String(14), primary_key=True)
|
15 |
-
user_id = Column(BigInteger)
|
16 |
-
count = Column(Integer, default=DEF_COUNT)
|
17 |
-
limit = Column(Integer, default=DEF_LIMIT)
|
18 |
-
|
19 |
-
def __init__(self, chat_id):
|
20 |
-
self.chat_id = str(chat_id) # ensure string
|
21 |
-
|
22 |
-
def __repr__(self):
|
23 |
-
return "<flood control for %s>" % self.chat_id
|
24 |
-
|
25 |
-
|
26 |
-
class FloodSettings(BASE):
|
27 |
-
__tablename__ = "antiflood_settings"
|
28 |
-
chat_id = Column(String(14), primary_key=True)
|
29 |
-
flood_type = Column(Integer, default=1)
|
30 |
-
value = Column(UnicodeText, default="0")
|
31 |
-
|
32 |
-
def __init__(self, chat_id, flood_type=1, value="0"):
|
33 |
-
self.chat_id = str(chat_id)
|
34 |
-
self.flood_type = flood_type
|
35 |
-
self.value = value
|
36 |
-
|
37 |
-
def __repr__(self):
|
38 |
-
return "<{} will executing {} for flood.>".format(self.chat_id, self.flood_type)
|
39 |
-
|
40 |
-
|
41 |
-
FloodControl.__table__.create(checkfirst=True)
|
42 |
-
FloodSettings.__table__.create(checkfirst=True)
|
43 |
-
|
44 |
-
INSERTION_FLOOD_LOCK = threading.RLock()
|
45 |
-
INSERTION_FLOOD_SETTINGS_LOCK = threading.RLock()
|
46 |
-
|
47 |
-
CHAT_FLOOD = {}
|
48 |
-
|
49 |
-
|
50 |
-
def set_flood(chat_id, amount):
|
51 |
-
with INSERTION_FLOOD_LOCK:
|
52 |
-
flood = SESSION.query(FloodControl).get(str(chat_id))
|
53 |
-
if not flood:
|
54 |
-
flood = FloodControl(str(chat_id))
|
55 |
-
|
56 |
-
flood.user_id = None
|
57 |
-
flood.limit = amount
|
58 |
-
|
59 |
-
CHAT_FLOOD[str(chat_id)] = (None, DEF_COUNT, amount)
|
60 |
-
|
61 |
-
SESSION.add(flood)
|
62 |
-
SESSION.commit()
|
63 |
-
|
64 |
-
|
65 |
-
def update_flood(chat_id: str, user_id) -> bool:
|
66 |
-
if str(chat_id) in CHAT_FLOOD:
|
67 |
-
curr_user_id, count, limit = CHAT_FLOOD.get(str(chat_id), DEF_OBJ)
|
68 |
-
|
69 |
-
if limit == 0: # no antiflood
|
70 |
-
return False
|
71 |
-
|
72 |
-
if user_id != curr_user_id or user_id is None: # other user
|
73 |
-
CHAT_FLOOD[str(chat_id)] = (user_id, DEF_COUNT, limit)
|
74 |
-
return False
|
75 |
-
|
76 |
-
count += 1
|
77 |
-
if count > limit: # too many msgs, kick
|
78 |
-
CHAT_FLOOD[str(chat_id)] = (None, DEF_COUNT, limit)
|
79 |
-
return True
|
80 |
-
|
81 |
-
# default -> update
|
82 |
-
CHAT_FLOOD[str(chat_id)] = (user_id, count, limit)
|
83 |
-
return False
|
84 |
-
|
85 |
-
|
86 |
-
def get_flood_limit(chat_id):
|
87 |
-
return CHAT_FLOOD.get(str(chat_id), DEF_OBJ)[2]
|
88 |
-
|
89 |
-
|
90 |
-
def set_flood_strength(chat_id, flood_type, value):
|
91 |
-
# for flood_type
|
92 |
-
# 1 = ban
|
93 |
-
# 2 = kick
|
94 |
-
# 3 = mute
|
95 |
-
# 4 = tban
|
96 |
-
# 5 = tmute
|
97 |
-
with INSERTION_FLOOD_SETTINGS_LOCK:
|
98 |
-
curr_setting = SESSION.query(FloodSettings).get(str(chat_id))
|
99 |
-
if not curr_setting:
|
100 |
-
curr_setting = FloodSettings(
|
101 |
-
chat_id,
|
102 |
-
flood_type=int(flood_type),
|
103 |
-
value=value,
|
104 |
-
)
|
105 |
-
|
106 |
-
curr_setting.flood_type = int(flood_type)
|
107 |
-
curr_setting.value = str(value)
|
108 |
-
|
109 |
-
SESSION.add(curr_setting)
|
110 |
-
SESSION.commit()
|
111 |
-
|
112 |
-
|
113 |
-
def get_flood_setting(chat_id):
|
114 |
-
try:
|
115 |
-
setting = SESSION.query(FloodSettings).get(str(chat_id))
|
116 |
-
if setting:
|
117 |
-
return setting.flood_type, setting.value
|
118 |
-
else:
|
119 |
-
return 1, "0"
|
120 |
-
|
121 |
-
finally:
|
122 |
-
SESSION.close()
|
123 |
-
|
124 |
-
|
125 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
126 |
-
with INSERTION_FLOOD_LOCK:
|
127 |
-
flood = SESSION.query(FloodControl).get(str(old_chat_id))
|
128 |
-
if flood:
|
129 |
-
CHAT_FLOOD[str(new_chat_id)] = CHAT_FLOOD.get(str(old_chat_id), DEF_OBJ)
|
130 |
-
flood.chat_id = str(new_chat_id)
|
131 |
-
SESSION.commit()
|
132 |
-
|
133 |
-
SESSION.close()
|
134 |
-
|
135 |
-
|
136 |
-
def __load_flood_settings():
|
137 |
-
global CHAT_FLOOD
|
138 |
-
try:
|
139 |
-
all_chats = SESSION.query(FloodControl).all()
|
140 |
-
CHAT_FLOOD = {chat.chat_id: (None, DEF_COUNT, chat.limit) for chat in all_chats}
|
141 |
-
finally:
|
142 |
-
SESSION.close()
|
143 |
-
|
144 |
-
|
145 |
-
__load_flood_settings()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/approve_sql.py
DELETED
@@ -1,61 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import BigInteger, Column, String
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
|
7 |
-
|
8 |
-
class Approvals(BASE):
|
9 |
-
__tablename__ = "approval"
|
10 |
-
chat_id = Column(String(14), primary_key=True)
|
11 |
-
user_id = Column(BigInteger, primary_key=True)
|
12 |
-
|
13 |
-
def __init__(self, chat_id, user_id):
|
14 |
-
self.chat_id = str(chat_id) # ensure string
|
15 |
-
self.user_id = user_id
|
16 |
-
|
17 |
-
def __repr__(self):
|
18 |
-
return "<Approve %s>" % self.user_id
|
19 |
-
|
20 |
-
|
21 |
-
Approvals.__table__.create(checkfirst=True)
|
22 |
-
|
23 |
-
APPROVE_INSERTION_LOCK = threading.RLock()
|
24 |
-
|
25 |
-
|
26 |
-
def approve(chat_id, user_id):
|
27 |
-
with APPROVE_INSERTION_LOCK:
|
28 |
-
approve_user = Approvals(str(chat_id), user_id)
|
29 |
-
SESSION.add(approve_user)
|
30 |
-
SESSION.commit()
|
31 |
-
|
32 |
-
|
33 |
-
def is_approved(chat_id, user_id):
|
34 |
-
try:
|
35 |
-
return SESSION.query(Approvals).get((str(chat_id), user_id))
|
36 |
-
finally:
|
37 |
-
SESSION.close()
|
38 |
-
|
39 |
-
|
40 |
-
def disapprove(chat_id, user_id):
|
41 |
-
with APPROVE_INSERTION_LOCK:
|
42 |
-
disapprove_user = SESSION.query(Approvals).get((str(chat_id), user_id))
|
43 |
-
if disapprove_user:
|
44 |
-
SESSION.delete(disapprove_user)
|
45 |
-
SESSION.commit()
|
46 |
-
return True
|
47 |
-
else:
|
48 |
-
SESSION.close()
|
49 |
-
return False
|
50 |
-
|
51 |
-
|
52 |
-
def list_approved(chat_id):
|
53 |
-
try:
|
54 |
-
return (
|
55 |
-
SESSION.query(Approvals)
|
56 |
-
.filter(Approvals.chat_id == str(chat_id))
|
57 |
-
.order_by(Approvals.user_id.asc())
|
58 |
-
.all()
|
59 |
-
)
|
60 |
-
finally:
|
61 |
-
SESSION.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/blacklist_sql.py
DELETED
@@ -1,200 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import Column, Integer, String, UnicodeText, distinct, func
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
|
7 |
-
|
8 |
-
class BlackListFilters(BASE):
|
9 |
-
__tablename__ = "blacklist"
|
10 |
-
chat_id = Column(String(14), primary_key=True)
|
11 |
-
trigger = Column(UnicodeText, primary_key=True, nullable=False)
|
12 |
-
|
13 |
-
def __init__(self, chat_id, trigger):
|
14 |
-
self.chat_id = str(chat_id) # ensure string
|
15 |
-
self.trigger = trigger
|
16 |
-
|
17 |
-
def __repr__(self):
|
18 |
-
return "<Blacklist filter '%s' for %s>" % (self.trigger, self.chat_id)
|
19 |
-
|
20 |
-
def __eq__(self, other):
|
21 |
-
return bool(
|
22 |
-
isinstance(other, BlackListFilters)
|
23 |
-
and self.chat_id == other.chat_id
|
24 |
-
and self.trigger == other.trigger,
|
25 |
-
)
|
26 |
-
|
27 |
-
|
28 |
-
class BlacklistSettings(BASE):
|
29 |
-
__tablename__ = "blacklist_settings"
|
30 |
-
chat_id = Column(String(14), primary_key=True)
|
31 |
-
blacklist_type = Column(Integer, default=1)
|
32 |
-
value = Column(UnicodeText, default="0")
|
33 |
-
|
34 |
-
def __init__(self, chat_id, blacklist_type=1, value="0"):
|
35 |
-
self.chat_id = str(chat_id)
|
36 |
-
self.blacklist_type = blacklist_type
|
37 |
-
self.value = value
|
38 |
-
|
39 |
-
def __repr__(self):
|
40 |
-
return "<{} will executing {} for blacklist trigger.>".format(
|
41 |
-
self.chat_id,
|
42 |
-
self.blacklist_type,
|
43 |
-
)
|
44 |
-
|
45 |
-
|
46 |
-
BlackListFilters.__table__.create(checkfirst=True)
|
47 |
-
BlacklistSettings.__table__.create(checkfirst=True)
|
48 |
-
|
49 |
-
BLACKLIST_FILTER_INSERTION_LOCK = threading.RLock()
|
50 |
-
BLACKLIST_SETTINGS_INSERTION_LOCK = threading.RLock()
|
51 |
-
|
52 |
-
CHAT_BLACKLISTS = {}
|
53 |
-
CHAT_SETTINGS_BLACKLISTS = {}
|
54 |
-
|
55 |
-
|
56 |
-
def add_to_blacklist(chat_id, trigger):
|
57 |
-
with BLACKLIST_FILTER_INSERTION_LOCK:
|
58 |
-
blacklist_filt = BlackListFilters(str(chat_id), trigger)
|
59 |
-
|
60 |
-
SESSION.merge(blacklist_filt) # merge to avoid duplicate key issues
|
61 |
-
SESSION.commit()
|
62 |
-
global CHAT_BLACKLISTS
|
63 |
-
if CHAT_BLACKLISTS.get(str(chat_id), set()) == set():
|
64 |
-
CHAT_BLACKLISTS[str(chat_id)] = {trigger}
|
65 |
-
else:
|
66 |
-
CHAT_BLACKLISTS.get(str(chat_id), set()).add(trigger)
|
67 |
-
|
68 |
-
|
69 |
-
def rm_from_blacklist(chat_id, trigger):
|
70 |
-
with BLACKLIST_FILTER_INSERTION_LOCK:
|
71 |
-
blacklist_filt = SESSION.query(BlackListFilters).get((str(chat_id), trigger))
|
72 |
-
if blacklist_filt:
|
73 |
-
if trigger in CHAT_BLACKLISTS.get(str(chat_id), set()): # sanity check
|
74 |
-
CHAT_BLACKLISTS.get(str(chat_id), set()).remove(trigger)
|
75 |
-
|
76 |
-
SESSION.delete(blacklist_filt)
|
77 |
-
SESSION.commit()
|
78 |
-
return True
|
79 |
-
|
80 |
-
SESSION.close()
|
81 |
-
return False
|
82 |
-
|
83 |
-
|
84 |
-
def get_chat_blacklist(chat_id):
|
85 |
-
return CHAT_BLACKLISTS.get(str(chat_id), set())
|
86 |
-
|
87 |
-
|
88 |
-
def num_blacklist_filters():
|
89 |
-
try:
|
90 |
-
return SESSION.query(BlackListFilters).count()
|
91 |
-
finally:
|
92 |
-
SESSION.close()
|
93 |
-
|
94 |
-
|
95 |
-
def num_blacklist_chat_filters(chat_id):
|
96 |
-
try:
|
97 |
-
return (
|
98 |
-
SESSION.query(BlackListFilters.chat_id)
|
99 |
-
.filter(BlackListFilters.chat_id == str(chat_id))
|
100 |
-
.count()
|
101 |
-
)
|
102 |
-
finally:
|
103 |
-
SESSION.close()
|
104 |
-
|
105 |
-
|
106 |
-
def num_blacklist_filter_chats():
|
107 |
-
try:
|
108 |
-
return SESSION.query(func.count(distinct(BlackListFilters.chat_id))).scalar()
|
109 |
-
finally:
|
110 |
-
SESSION.close()
|
111 |
-
|
112 |
-
|
113 |
-
def set_blacklist_strength(chat_id, blacklist_type, value):
|
114 |
-
# for blacklist_type
|
115 |
-
# 0 = nothing
|
116 |
-
# 1 = delete
|
117 |
-
# 2 = warn
|
118 |
-
# 3 = mute
|
119 |
-
# 4 = kick
|
120 |
-
# 5 = ban
|
121 |
-
# 6 = tban
|
122 |
-
# 7 = tmute
|
123 |
-
with BLACKLIST_SETTINGS_INSERTION_LOCK:
|
124 |
-
global CHAT_SETTINGS_BLACKLISTS
|
125 |
-
curr_setting = SESSION.query(BlacklistSettings).get(str(chat_id))
|
126 |
-
if not curr_setting:
|
127 |
-
curr_setting = BlacklistSettings(
|
128 |
-
chat_id,
|
129 |
-
blacklist_type=int(blacklist_type),
|
130 |
-
value=value,
|
131 |
-
)
|
132 |
-
|
133 |
-
curr_setting.blacklist_type = int(blacklist_type)
|
134 |
-
curr_setting.value = str(value)
|
135 |
-
CHAT_SETTINGS_BLACKLISTS[str(chat_id)] = {
|
136 |
-
"blacklist_type": int(blacklist_type),
|
137 |
-
"value": value,
|
138 |
-
}
|
139 |
-
|
140 |
-
SESSION.add(curr_setting)
|
141 |
-
SESSION.commit()
|
142 |
-
|
143 |
-
|
144 |
-
def get_blacklist_setting(chat_id):
|
145 |
-
try:
|
146 |
-
setting = CHAT_SETTINGS_BLACKLISTS.get(str(chat_id))
|
147 |
-
if setting:
|
148 |
-
return setting["blacklist_type"], setting["value"]
|
149 |
-
else:
|
150 |
-
return 1, "0"
|
151 |
-
|
152 |
-
finally:
|
153 |
-
SESSION.close()
|
154 |
-
|
155 |
-
|
156 |
-
def __load_chat_blacklists():
|
157 |
-
global CHAT_BLACKLISTS
|
158 |
-
try:
|
159 |
-
chats = SESSION.query(BlackListFilters.chat_id).distinct().all()
|
160 |
-
for (chat_id,) in chats: # remove tuple by ( ,)
|
161 |
-
CHAT_BLACKLISTS[chat_id] = []
|
162 |
-
|
163 |
-
all_filters = SESSION.query(BlackListFilters).all()
|
164 |
-
for x in all_filters:
|
165 |
-
CHAT_BLACKLISTS[x.chat_id] += [x.trigger]
|
166 |
-
|
167 |
-
CHAT_BLACKLISTS = {x: set(y) for x, y in CHAT_BLACKLISTS.items()}
|
168 |
-
|
169 |
-
finally:
|
170 |
-
SESSION.close()
|
171 |
-
|
172 |
-
|
173 |
-
def __load_chat_settings_blacklists():
|
174 |
-
global CHAT_SETTINGS_BLACKLISTS
|
175 |
-
try:
|
176 |
-
chats_settings = SESSION.query(BlacklistSettings).all()
|
177 |
-
for x in chats_settings: # remove tuple by ( ,)
|
178 |
-
CHAT_SETTINGS_BLACKLISTS[x.chat_id] = {
|
179 |
-
"blacklist_type": x.blacklist_type,
|
180 |
-
"value": x.value,
|
181 |
-
}
|
182 |
-
|
183 |
-
finally:
|
184 |
-
SESSION.close()
|
185 |
-
|
186 |
-
|
187 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
188 |
-
with BLACKLIST_FILTER_INSERTION_LOCK:
|
189 |
-
chat_filters = (
|
190 |
-
SESSION.query(BlackListFilters)
|
191 |
-
.filter(BlackListFilters.chat_id == str(old_chat_id))
|
192 |
-
.all()
|
193 |
-
)
|
194 |
-
for filt in chat_filters:
|
195 |
-
filt.chat_id = str(new_chat_id)
|
196 |
-
SESSION.commit()
|
197 |
-
|
198 |
-
|
199 |
-
__load_chat_blacklists()
|
200 |
-
__load_chat_settings_blacklists()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/blacklistusers_sql.py
DELETED
@@ -1,69 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import Column, String, UnicodeText
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
|
7 |
-
|
8 |
-
class BlacklistUsers(BASE):
|
9 |
-
__tablename__ = "blacklistusers"
|
10 |
-
user_id = Column(String(14), primary_key=True)
|
11 |
-
reason = Column(UnicodeText)
|
12 |
-
|
13 |
-
def __init__(self, user_id, reason=None):
|
14 |
-
self.user_id = user_id
|
15 |
-
self.reason = reason
|
16 |
-
|
17 |
-
|
18 |
-
BlacklistUsers.__table__.create(checkfirst=True)
|
19 |
-
|
20 |
-
BLACKLIST_LOCK = threading.RLock()
|
21 |
-
BLACKLIST_USERS = set()
|
22 |
-
|
23 |
-
|
24 |
-
def blacklist_user(user_id, reason=None):
|
25 |
-
with BLACKLIST_LOCK:
|
26 |
-
user = SESSION.query(BlacklistUsers).get(str(user_id))
|
27 |
-
if not user:
|
28 |
-
user = BlacklistUsers(str(user_id), reason)
|
29 |
-
else:
|
30 |
-
user.reason = reason
|
31 |
-
|
32 |
-
SESSION.add(user)
|
33 |
-
SESSION.commit()
|
34 |
-
__load_blacklist_userid_list()
|
35 |
-
|
36 |
-
|
37 |
-
def unblacklist_user(user_id):
|
38 |
-
with BLACKLIST_LOCK:
|
39 |
-
user = SESSION.query(BlacklistUsers).get(str(user_id))
|
40 |
-
if user:
|
41 |
-
SESSION.delete(user)
|
42 |
-
|
43 |
-
SESSION.commit()
|
44 |
-
__load_blacklist_userid_list()
|
45 |
-
|
46 |
-
|
47 |
-
def get_reason(user_id):
|
48 |
-
user = SESSION.query(BlacklistUsers).get(str(user_id))
|
49 |
-
rep = ""
|
50 |
-
if user:
|
51 |
-
rep = user.reason
|
52 |
-
|
53 |
-
SESSION.close()
|
54 |
-
return rep
|
55 |
-
|
56 |
-
|
57 |
-
def is_user_blacklisted(user_id):
|
58 |
-
return user_id in BLACKLIST_USERS
|
59 |
-
|
60 |
-
|
61 |
-
def __load_blacklist_userid_list():
|
62 |
-
global BLACKLIST_USERS
|
63 |
-
try:
|
64 |
-
BLACKLIST_USERS = {int(x.user_id) for x in SESSION.query(BlacklistUsers).all()}
|
65 |
-
finally:
|
66 |
-
SESSION.close()
|
67 |
-
|
68 |
-
|
69 |
-
__load_blacklist_userid_list()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/blsticker_sql.py
DELETED
@@ -1,200 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import Column, Integer, String, UnicodeText, distinct, func
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
|
7 |
-
|
8 |
-
class StickersFilters(BASE):
|
9 |
-
__tablename__ = "blacklist_stickers"
|
10 |
-
chat_id = Column(String(14), primary_key=True)
|
11 |
-
trigger = Column(UnicodeText, primary_key=True, nullable=False)
|
12 |
-
|
13 |
-
def __init__(self, chat_id, trigger):
|
14 |
-
self.chat_id = str(chat_id) # ensure string
|
15 |
-
self.trigger = trigger
|
16 |
-
|
17 |
-
def __repr__(self):
|
18 |
-
return "<Stickers filter '%s' for %s>" % (self.trigger, self.chat_id)
|
19 |
-
|
20 |
-
def __eq__(self, other):
|
21 |
-
return bool(
|
22 |
-
isinstance(other, StickersFilters)
|
23 |
-
and self.chat_id == other.chat_id
|
24 |
-
and self.trigger == other.trigger,
|
25 |
-
)
|
26 |
-
|
27 |
-
|
28 |
-
class StickerSettings(BASE):
|
29 |
-
__tablename__ = "blsticker_settings"
|
30 |
-
chat_id = Column(String(14), primary_key=True)
|
31 |
-
blacklist_type = Column(Integer, default=1)
|
32 |
-
value = Column(UnicodeText, default="0")
|
33 |
-
|
34 |
-
def __init__(self, chat_id, blacklist_type=1, value="0"):
|
35 |
-
self.chat_id = str(chat_id)
|
36 |
-
self.blacklist_type = blacklist_type
|
37 |
-
self.value = value
|
38 |
-
|
39 |
-
def __repr__(self):
|
40 |
-
return "<{} will executing {} for blacklist trigger.>".format(
|
41 |
-
self.chat_id,
|
42 |
-
self.blacklist_type,
|
43 |
-
)
|
44 |
-
|
45 |
-
|
46 |
-
StickersFilters.__table__.create(checkfirst=True)
|
47 |
-
StickerSettings.__table__.create(checkfirst=True)
|
48 |
-
|
49 |
-
STICKERS_FILTER_INSERTION_LOCK = threading.RLock()
|
50 |
-
STICKSET_FILTER_INSERTION_LOCK = threading.RLock()
|
51 |
-
|
52 |
-
CHAT_STICKERS = {}
|
53 |
-
CHAT_BLSTICK_BLACKLISTS = {}
|
54 |
-
|
55 |
-
|
56 |
-
def add_to_stickers(chat_id, trigger):
|
57 |
-
with STICKERS_FILTER_INSERTION_LOCK:
|
58 |
-
stickers_filt = StickersFilters(str(chat_id), trigger)
|
59 |
-
|
60 |
-
SESSION.merge(stickers_filt) # merge to avoid duplicate key issues
|
61 |
-
SESSION.commit()
|
62 |
-
global CHAT_STICKERS
|
63 |
-
if CHAT_STICKERS.get(str(chat_id), set()) == set():
|
64 |
-
CHAT_STICKERS[str(chat_id)] = {trigger}
|
65 |
-
else:
|
66 |
-
CHAT_STICKERS.get(str(chat_id), set()).add(trigger)
|
67 |
-
|
68 |
-
|
69 |
-
def rm_from_stickers(chat_id, trigger):
|
70 |
-
with STICKERS_FILTER_INSERTION_LOCK:
|
71 |
-
stickers_filt = SESSION.query(StickersFilters).get((str(chat_id), trigger))
|
72 |
-
if stickers_filt:
|
73 |
-
if trigger in CHAT_STICKERS.get(str(chat_id), set()): # sanity check
|
74 |
-
CHAT_STICKERS.get(str(chat_id), set()).remove(trigger)
|
75 |
-
|
76 |
-
SESSION.delete(stickers_filt)
|
77 |
-
SESSION.commit()
|
78 |
-
return True
|
79 |
-
|
80 |
-
SESSION.close()
|
81 |
-
return False
|
82 |
-
|
83 |
-
|
84 |
-
def get_chat_stickers(chat_id):
|
85 |
-
return CHAT_STICKERS.get(str(chat_id), set())
|
86 |
-
|
87 |
-
|
88 |
-
def num_stickers_filters():
|
89 |
-
try:
|
90 |
-
return SESSION.query(StickersFilters).count()
|
91 |
-
finally:
|
92 |
-
SESSION.close()
|
93 |
-
|
94 |
-
|
95 |
-
def num_stickers_chat_filters(chat_id):
|
96 |
-
try:
|
97 |
-
return (
|
98 |
-
SESSION.query(StickersFilters.chat_id)
|
99 |
-
.filter(StickersFilters.chat_id == str(chat_id))
|
100 |
-
.count()
|
101 |
-
)
|
102 |
-
finally:
|
103 |
-
SESSION.close()
|
104 |
-
|
105 |
-
|
106 |
-
def num_stickers_filter_chats():
|
107 |
-
try:
|
108 |
-
return SESSION.query(func.count(distinct(StickersFilters.chat_id))).scalar()
|
109 |
-
finally:
|
110 |
-
SESSION.close()
|
111 |
-
|
112 |
-
|
113 |
-
def set_blacklist_strength(chat_id, blacklist_type, value):
|
114 |
-
# for blacklist_type
|
115 |
-
# 0 = nothing
|
116 |
-
# 1 = delete
|
117 |
-
# 2 = warn
|
118 |
-
# 3 = mute
|
119 |
-
# 4 = kick
|
120 |
-
# 5 = ban
|
121 |
-
# 6 = tban
|
122 |
-
# 7 = tmute
|
123 |
-
with STICKSET_FILTER_INSERTION_LOCK:
|
124 |
-
global CHAT_BLSTICK_BLACKLISTS
|
125 |
-
curr_setting = SESSION.query(StickerSettings).get(str(chat_id))
|
126 |
-
if not curr_setting:
|
127 |
-
curr_setting = StickerSettings(
|
128 |
-
chat_id,
|
129 |
-
blacklist_type=int(blacklist_type),
|
130 |
-
value=value,
|
131 |
-
)
|
132 |
-
|
133 |
-
curr_setting.blacklist_type = int(blacklist_type)
|
134 |
-
curr_setting.value = str(value)
|
135 |
-
CHAT_BLSTICK_BLACKLISTS[str(chat_id)] = {
|
136 |
-
"blacklist_type": int(blacklist_type),
|
137 |
-
"value": value,
|
138 |
-
}
|
139 |
-
|
140 |
-
SESSION.add(curr_setting)
|
141 |
-
SESSION.commit()
|
142 |
-
|
143 |
-
|
144 |
-
def get_blacklist_setting(chat_id):
|
145 |
-
try:
|
146 |
-
setting = CHAT_BLSTICK_BLACKLISTS.get(str(chat_id))
|
147 |
-
if setting:
|
148 |
-
return setting["blacklist_type"], setting["value"]
|
149 |
-
else:
|
150 |
-
return 1, "0"
|
151 |
-
|
152 |
-
finally:
|
153 |
-
SESSION.close()
|
154 |
-
|
155 |
-
|
156 |
-
def __load_CHAT_STICKERS():
|
157 |
-
global CHAT_STICKERS
|
158 |
-
try:
|
159 |
-
chats = SESSION.query(StickersFilters.chat_id).distinct().all()
|
160 |
-
for (chat_id,) in chats: # remove tuple by ( ,)
|
161 |
-
CHAT_STICKERS[chat_id] = []
|
162 |
-
|
163 |
-
all_filters = SESSION.query(StickersFilters).all()
|
164 |
-
for x in all_filters:
|
165 |
-
CHAT_STICKERS[x.chat_id] += [x.trigger]
|
166 |
-
|
167 |
-
CHAT_STICKERS = {x: set(y) for x, y in CHAT_STICKERS.items()}
|
168 |
-
|
169 |
-
finally:
|
170 |
-
SESSION.close()
|
171 |
-
|
172 |
-
|
173 |
-
def __load_chat_stickerset_blacklists():
|
174 |
-
global CHAT_BLSTICK_BLACKLISTS
|
175 |
-
try:
|
176 |
-
chats_settings = SESSION.query(StickerSettings).all()
|
177 |
-
for x in chats_settings: # remove tuple by ( ,)
|
178 |
-
CHAT_BLSTICK_BLACKLISTS[x.chat_id] = {
|
179 |
-
"blacklist_type": x.blacklist_type,
|
180 |
-
"value": x.value,
|
181 |
-
}
|
182 |
-
|
183 |
-
finally:
|
184 |
-
SESSION.close()
|
185 |
-
|
186 |
-
|
187 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
188 |
-
with STICKERS_FILTER_INSERTION_LOCK:
|
189 |
-
chat_filters = (
|
190 |
-
SESSION.query(StickersFilters)
|
191 |
-
.filter(StickersFilters.chat_id == str(old_chat_id))
|
192 |
-
.all()
|
193 |
-
)
|
194 |
-
for filt in chat_filters:
|
195 |
-
filt.chat_id = str(new_chat_id)
|
196 |
-
SESSION.commit()
|
197 |
-
|
198 |
-
|
199 |
-
__load_CHAT_STICKERS()
|
200 |
-
__load_chat_stickerset_blacklists()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/connection_sql.py
DELETED
@@ -1,204 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
import time
|
3 |
-
from typing import Union
|
4 |
-
|
5 |
-
from sqlalchemy import BigInteger, Boolean, Column, String, UnicodeText
|
6 |
-
|
7 |
-
from Database.sql import BASE, SESSION
|
8 |
-
|
9 |
-
|
10 |
-
class ChatAccessConnectionSettings(BASE):
|
11 |
-
__tablename__ = "access_connection"
|
12 |
-
chat_id = Column(String(14), primary_key=True)
|
13 |
-
allow_connect_to_chat = Column(Boolean, default=True)
|
14 |
-
|
15 |
-
def __init__(self, chat_id, allow_connect_to_chat):
|
16 |
-
self.chat_id = str(chat_id)
|
17 |
-
self.allow_connect_to_chat = str(allow_connect_to_chat)
|
18 |
-
|
19 |
-
def __repr__(self):
|
20 |
-
return "<Chat access settings ({}) is {}>".format(
|
21 |
-
self.chat_id, self.allow_connect_to_chat
|
22 |
-
)
|
23 |
-
|
24 |
-
|
25 |
-
class Connection(BASE):
|
26 |
-
__tablename__ = "connection"
|
27 |
-
user_id = Column(BigInteger, primary_key=True)
|
28 |
-
chat_id = Column(String(14))
|
29 |
-
|
30 |
-
def __init__(self, user_id, chat_id):
|
31 |
-
self.user_id = user_id
|
32 |
-
self.chat_id = str(chat_id) # Ensure String
|
33 |
-
|
34 |
-
|
35 |
-
class ConnectionHistory(BASE):
|
36 |
-
__tablename__ = "connection_history"
|
37 |
-
user_id = Column(BigInteger, primary_key=True)
|
38 |
-
chat_id = Column(String(14), primary_key=True)
|
39 |
-
chat_name = Column(UnicodeText)
|
40 |
-
conn_time = Column(BigInteger)
|
41 |
-
|
42 |
-
def __init__(self, user_id, chat_id, chat_name, conn_time):
|
43 |
-
self.user_id = user_id
|
44 |
-
self.chat_id = str(chat_id)
|
45 |
-
self.chat_name = str(chat_name)
|
46 |
-
self.conn_time = int(conn_time)
|
47 |
-
|
48 |
-
def __repr__(self):
|
49 |
-
return "<connection user {} history {}>".format(self.user_id, self.chat_id)
|
50 |
-
|
51 |
-
|
52 |
-
ChatAccessConnectionSettings.__table__.create(checkfirst=True)
|
53 |
-
Connection.__table__.create(checkfirst=True)
|
54 |
-
ConnectionHistory.__table__.create(checkfirst=True)
|
55 |
-
|
56 |
-
CHAT_ACCESS_LOCK = threading.RLock()
|
57 |
-
CONNECTION_INSERTION_LOCK = threading.RLock()
|
58 |
-
CONNECTION_HISTORY_LOCK = threading.RLock()
|
59 |
-
|
60 |
-
HISTORY_CONNECT = {}
|
61 |
-
|
62 |
-
|
63 |
-
def allow_connect_to_chat(chat_id: Union[str, int]) -> bool:
|
64 |
-
try:
|
65 |
-
chat_setting = SESSION.query(ChatAccessConnectionSettings).get(str(chat_id))
|
66 |
-
if chat_setting:
|
67 |
-
return chat_setting.allow_connect_to_chat
|
68 |
-
return False
|
69 |
-
finally:
|
70 |
-
SESSION.close()
|
71 |
-
|
72 |
-
|
73 |
-
def set_allow_connect_to_chat(chat_id: Union[int, str], setting: bool):
|
74 |
-
with CHAT_ACCESS_LOCK:
|
75 |
-
chat_setting = SESSION.query(ChatAccessConnectionSettings).get(str(chat_id))
|
76 |
-
if not chat_setting:
|
77 |
-
chat_setting = ChatAccessConnectionSettings(chat_id, setting)
|
78 |
-
|
79 |
-
chat_setting.allow_connect_to_chat = setting
|
80 |
-
SESSION.add(chat_setting)
|
81 |
-
SESSION.commit()
|
82 |
-
|
83 |
-
|
84 |
-
def connect(user_id, chat_id):
|
85 |
-
with CONNECTION_INSERTION_LOCK:
|
86 |
-
prev = SESSION.query(Connection).get((int(user_id)))
|
87 |
-
if prev:
|
88 |
-
SESSION.delete(prev)
|
89 |
-
connect_to_chat = Connection(int(user_id), chat_id)
|
90 |
-
SESSION.add(connect_to_chat)
|
91 |
-
SESSION.commit()
|
92 |
-
return True
|
93 |
-
|
94 |
-
|
95 |
-
def get_connected_chat(user_id):
|
96 |
-
try:
|
97 |
-
return SESSION.query(Connection).get((int(user_id)))
|
98 |
-
finally:
|
99 |
-
SESSION.close()
|
100 |
-
|
101 |
-
|
102 |
-
def curr_connection(chat_id):
|
103 |
-
try:
|
104 |
-
return SESSION.query(Connection).get((str(chat_id)))
|
105 |
-
finally:
|
106 |
-
SESSION.close()
|
107 |
-
|
108 |
-
|
109 |
-
def disconnect(user_id):
|
110 |
-
with CONNECTION_INSERTION_LOCK:
|
111 |
-
disconnect = SESSION.query(Connection).get((int(user_id)))
|
112 |
-
if disconnect:
|
113 |
-
SESSION.delete(disconnect)
|
114 |
-
SESSION.commit()
|
115 |
-
return True
|
116 |
-
else:
|
117 |
-
SESSION.close()
|
118 |
-
return False
|
119 |
-
|
120 |
-
|
121 |
-
def add_history_conn(user_id, chat_id, chat_name):
|
122 |
-
global HISTORY_CONNECT
|
123 |
-
with CONNECTION_HISTORY_LOCK:
|
124 |
-
conn_time = int(time.time())
|
125 |
-
if HISTORY_CONNECT.get(int(user_id)):
|
126 |
-
counting = (
|
127 |
-
SESSION.query(ConnectionHistory.user_id)
|
128 |
-
.filter(ConnectionHistory.user_id == str(user_id))
|
129 |
-
.count()
|
130 |
-
)
|
131 |
-
getchat_id = {}
|
132 |
-
for x in HISTORY_CONNECT[int(user_id)]:
|
133 |
-
getchat_id[HISTORY_CONNECT[int(user_id)][x]["chat_id"]] = x
|
134 |
-
if chat_id in getchat_id:
|
135 |
-
todeltime = getchat_id[str(chat_id)]
|
136 |
-
delold = SESSION.query(ConnectionHistory).get(
|
137 |
-
(int(user_id), str(chat_id))
|
138 |
-
)
|
139 |
-
if delold:
|
140 |
-
SESSION.delete(delold)
|
141 |
-
HISTORY_CONNECT[int(user_id)].pop(todeltime)
|
142 |
-
elif counting >= 5:
|
143 |
-
todel = list(HISTORY_CONNECT[int(user_id)])
|
144 |
-
todel.reverse()
|
145 |
-
todel = todel[4:]
|
146 |
-
for x in todel:
|
147 |
-
chat_old = HISTORY_CONNECT[int(user_id)][x]["chat_id"]
|
148 |
-
delold = SESSION.query(ConnectionHistory).get(
|
149 |
-
(int(user_id), str(chat_old))
|
150 |
-
)
|
151 |
-
if delold:
|
152 |
-
SESSION.delete(delold)
|
153 |
-
HISTORY_CONNECT[int(user_id)].pop(x)
|
154 |
-
else:
|
155 |
-
HISTORY_CONNECT[int(user_id)] = {}
|
156 |
-
delold = SESSION.query(ConnectionHistory).get((int(user_id), str(chat_id)))
|
157 |
-
if delold:
|
158 |
-
SESSION.delete(delold)
|
159 |
-
history = ConnectionHistory(int(user_id), str(chat_id), chat_name, conn_time)
|
160 |
-
SESSION.add(history)
|
161 |
-
SESSION.commit()
|
162 |
-
HISTORY_CONNECT[int(user_id)][conn_time] = {
|
163 |
-
"chat_name": chat_name,
|
164 |
-
"chat_id": str(chat_id),
|
165 |
-
}
|
166 |
-
|
167 |
-
|
168 |
-
def get_history_conn(user_id):
|
169 |
-
if not HISTORY_CONNECT.get(int(user_id)):
|
170 |
-
HISTORY_CONNECT[int(user_id)] = {}
|
171 |
-
return HISTORY_CONNECT[int(user_id)]
|
172 |
-
|
173 |
-
|
174 |
-
def clear_history_conn(user_id):
|
175 |
-
global HISTORY_CONNECT
|
176 |
-
todel = list(HISTORY_CONNECT[int(user_id)])
|
177 |
-
for x in todel:
|
178 |
-
chat_old = HISTORY_CONNECT[int(user_id)][x]["chat_id"]
|
179 |
-
delold = SESSION.query(ConnectionHistory).get((int(user_id), str(chat_old)))
|
180 |
-
if delold:
|
181 |
-
SESSION.delete(delold)
|
182 |
-
HISTORY_CONNECT[int(user_id)].pop(x)
|
183 |
-
SESSION.commit()
|
184 |
-
return True
|
185 |
-
|
186 |
-
|
187 |
-
def __load_user_history():
|
188 |
-
global HISTORY_CONNECT
|
189 |
-
try:
|
190 |
-
qall = SESSION.query(ConnectionHistory).all()
|
191 |
-
HISTORY_CONNECT = {}
|
192 |
-
for x in qall:
|
193 |
-
check = HISTORY_CONNECT.get(x.user_id)
|
194 |
-
if check is None:
|
195 |
-
HISTORY_CONNECT[x.user_id] = {}
|
196 |
-
HISTORY_CONNECT[x.user_id][x.conn_time] = {
|
197 |
-
"chat_name": x.chat_name,
|
198 |
-
"chat_id": x.chat_id,
|
199 |
-
}
|
200 |
-
finally:
|
201 |
-
SESSION.close()
|
202 |
-
|
203 |
-
|
204 |
-
__load_user_history()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/cust_filters_sql.py
DELETED
@@ -1,299 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import Boolean, Column, Integer, String, UnicodeText, distinct, func
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
from Mikobot.plugins.helper_funcs.msg_types import Types
|
7 |
-
|
8 |
-
|
9 |
-
class CustomFilters(BASE):
|
10 |
-
__tablename__ = "cust_filters"
|
11 |
-
chat_id = Column(String(14), primary_key=True)
|
12 |
-
keyword = Column(UnicodeText, primary_key=True, nullable=False)
|
13 |
-
reply = Column(UnicodeText, nullable=False)
|
14 |
-
reply_text = Column(UnicodeText)
|
15 |
-
file_type = Column(Integer, nullable=False, default=1)
|
16 |
-
file_id = Column(UnicodeText, default=None)
|
17 |
-
has_buttons = Column(Boolean, nullable=False, default=False)
|
18 |
-
# NOTE: Here for legacy purposes, to ensure older filters don't mess up.
|
19 |
-
has_media_spoiler = Column(Boolean, nullable=False, default=False)
|
20 |
-
|
21 |
-
def __init__(
|
22 |
-
self,
|
23 |
-
chat_id: int | str,
|
24 |
-
keyword: str,
|
25 |
-
reply: str,
|
26 |
-
reply_text: str,
|
27 |
-
has_buttons: bool,
|
28 |
-
has_media_spoiler: bool,
|
29 |
-
file_type: int,
|
30 |
-
file_id: str,
|
31 |
-
):
|
32 |
-
self.chat_id = str(chat_id) # ensure string
|
33 |
-
self.keyword = keyword
|
34 |
-
self.reply = reply
|
35 |
-
self.reply_text = reply_text
|
36 |
-
self.has_buttons = has_buttons
|
37 |
-
self.has_media_spoiler = has_media_spoiler
|
38 |
-
self.file_type = file_type
|
39 |
-
self.file_id = file_id
|
40 |
-
|
41 |
-
def __repr__(self):
|
42 |
-
return "<Filter for %s>" % self.chat_id
|
43 |
-
|
44 |
-
def __eq__(self, other):
|
45 |
-
return bool(
|
46 |
-
isinstance(other, CustomFilters)
|
47 |
-
and self.chat_id == other.chat_id
|
48 |
-
and self.keyword == other.keyword,
|
49 |
-
)
|
50 |
-
|
51 |
-
|
52 |
-
class Buttons(BASE):
|
53 |
-
__tablename__ = "cust_filter_urls"
|
54 |
-
id = Column(Integer, primary_key=True, autoincrement=True)
|
55 |
-
chat_id = Column(String(14), primary_key=True)
|
56 |
-
keyword = Column(UnicodeText, primary_key=True)
|
57 |
-
name = Column(UnicodeText, nullable=False)
|
58 |
-
url = Column(UnicodeText, nullable=False)
|
59 |
-
same_line = Column(Boolean, default=False)
|
60 |
-
|
61 |
-
def __init__(self, chat_id, keyword, name, url, same_line=False):
|
62 |
-
self.chat_id = str(chat_id)
|
63 |
-
self.keyword = keyword
|
64 |
-
self.name = name
|
65 |
-
self.url = url
|
66 |
-
self.same_line = same_line
|
67 |
-
|
68 |
-
|
69 |
-
CustomFilters.__table__.create(checkfirst=True)
|
70 |
-
Buttons.__table__.create(checkfirst=True)
|
71 |
-
|
72 |
-
CUST_FILT_LOCK = threading.RLock()
|
73 |
-
BUTTON_LOCK = threading.RLock()
|
74 |
-
CHAT_FILTERS = {}
|
75 |
-
|
76 |
-
|
77 |
-
def get_all_filters():
|
78 |
-
try:
|
79 |
-
return SESSION.query(CustomFilters).all()
|
80 |
-
finally:
|
81 |
-
SESSION.close()
|
82 |
-
|
83 |
-
|
84 |
-
def new_add_filter(
|
85 |
-
chat_id, keyword, reply_text, file_type, file_id, buttons, media_spoiler
|
86 |
-
):
|
87 |
-
global CHAT_FILTERS
|
88 |
-
|
89 |
-
if buttons is None:
|
90 |
-
buttons = []
|
91 |
-
|
92 |
-
with CUST_FILT_LOCK:
|
93 |
-
prev = SESSION.query(CustomFilters).get((str(chat_id), keyword))
|
94 |
-
if prev:
|
95 |
-
with BUTTON_LOCK:
|
96 |
-
prev_buttons = (
|
97 |
-
SESSION.query(Buttons)
|
98 |
-
.filter(Buttons.chat_id == str(chat_id), Buttons.keyword == keyword)
|
99 |
-
.all()
|
100 |
-
)
|
101 |
-
for btn in prev_buttons:
|
102 |
-
SESSION.delete(btn)
|
103 |
-
SESSION.delete(prev)
|
104 |
-
|
105 |
-
filt = CustomFilters(
|
106 |
-
str(chat_id),
|
107 |
-
keyword,
|
108 |
-
reply="there is should be a new reply",
|
109 |
-
has_buttons=bool(buttons),
|
110 |
-
has_media_spoiler=media_spoiler,
|
111 |
-
reply_text=reply_text,
|
112 |
-
file_type=file_type.value,
|
113 |
-
file_id=file_id,
|
114 |
-
)
|
115 |
-
|
116 |
-
if keyword not in CHAT_FILTERS.get(str(chat_id), []):
|
117 |
-
CHAT_FILTERS[str(chat_id)] = sorted(
|
118 |
-
CHAT_FILTERS.get(str(chat_id), []) + [keyword],
|
119 |
-
key=lambda x: (-len(x), x),
|
120 |
-
)
|
121 |
-
|
122 |
-
SESSION.add(filt)
|
123 |
-
SESSION.commit()
|
124 |
-
|
125 |
-
for b_name, url, same_line in buttons:
|
126 |
-
add_note_button_to_db(chat_id, keyword, b_name, url, same_line)
|
127 |
-
|
128 |
-
|
129 |
-
def remove_filter(chat_id, keyword):
|
130 |
-
global CHAT_FILTERS
|
131 |
-
with CUST_FILT_LOCK:
|
132 |
-
filt = SESSION.query(CustomFilters).get((str(chat_id), keyword))
|
133 |
-
if filt:
|
134 |
-
if keyword in CHAT_FILTERS.get(str(chat_id), []): # Sanity check
|
135 |
-
CHAT_FILTERS.get(str(chat_id), []).remove(keyword)
|
136 |
-
|
137 |
-
with BUTTON_LOCK:
|
138 |
-
prev_buttons = (
|
139 |
-
SESSION.query(Buttons)
|
140 |
-
.filter(Buttons.chat_id == str(chat_id), Buttons.keyword == keyword)
|
141 |
-
.all()
|
142 |
-
)
|
143 |
-
for btn in prev_buttons:
|
144 |
-
SESSION.delete(btn)
|
145 |
-
|
146 |
-
SESSION.delete(filt)
|
147 |
-
SESSION.commit()
|
148 |
-
return True
|
149 |
-
|
150 |
-
SESSION.close()
|
151 |
-
return False
|
152 |
-
|
153 |
-
|
154 |
-
def get_chat_triggers(chat_id):
|
155 |
-
return CHAT_FILTERS.get(str(chat_id), set())
|
156 |
-
|
157 |
-
|
158 |
-
def get_chat_filters(chat_id):
|
159 |
-
try:
|
160 |
-
return (
|
161 |
-
SESSION.query(CustomFilters)
|
162 |
-
.filter(CustomFilters.chat_id == str(chat_id))
|
163 |
-
.order_by(func.length(CustomFilters.keyword).desc())
|
164 |
-
.order_by(CustomFilters.keyword.asc())
|
165 |
-
.all()
|
166 |
-
)
|
167 |
-
finally:
|
168 |
-
SESSION.close()
|
169 |
-
|
170 |
-
|
171 |
-
def get_filter(chat_id, keyword) -> CustomFilters:
|
172 |
-
try:
|
173 |
-
return SESSION.query(CustomFilters).get((str(chat_id), keyword))
|
174 |
-
finally:
|
175 |
-
SESSION.close()
|
176 |
-
|
177 |
-
|
178 |
-
def add_note_button_to_db(chat_id, keyword, b_name, url, same_line):
|
179 |
-
with BUTTON_LOCK:
|
180 |
-
button = Buttons(chat_id, keyword, b_name, url, same_line)
|
181 |
-
SESSION.add(button)
|
182 |
-
SESSION.commit()
|
183 |
-
|
184 |
-
|
185 |
-
def get_buttons(chat_id, keyword):
|
186 |
-
try:
|
187 |
-
return (
|
188 |
-
SESSION.query(Buttons)
|
189 |
-
.filter(Buttons.chat_id == str(chat_id), Buttons.keyword == keyword)
|
190 |
-
.order_by(Buttons.id)
|
191 |
-
.all()
|
192 |
-
)
|
193 |
-
finally:
|
194 |
-
SESSION.close()
|
195 |
-
|
196 |
-
|
197 |
-
def num_filters():
|
198 |
-
try:
|
199 |
-
return SESSION.query(CustomFilters).count()
|
200 |
-
finally:
|
201 |
-
SESSION.close()
|
202 |
-
|
203 |
-
|
204 |
-
def num_chats():
|
205 |
-
try:
|
206 |
-
return SESSION.query(func.count(distinct(CustomFilters.chat_id))).scalar()
|
207 |
-
finally:
|
208 |
-
SESSION.close()
|
209 |
-
|
210 |
-
|
211 |
-
def __load_chat_filters():
|
212 |
-
global CHAT_FILTERS
|
213 |
-
try:
|
214 |
-
chats = SESSION.query(CustomFilters.chat_id).distinct().all()
|
215 |
-
for (chat_id,) in chats: # remove tuple by ( ,)
|
216 |
-
CHAT_FILTERS[chat_id] = []
|
217 |
-
|
218 |
-
all_filters = SESSION.query(CustomFilters).all()
|
219 |
-
for x in all_filters:
|
220 |
-
CHAT_FILTERS[x.chat_id] += [x.keyword]
|
221 |
-
|
222 |
-
CHAT_FILTERS = {
|
223 |
-
x: sorted(set(y), key=lambda i: (-len(i), i))
|
224 |
-
for x, y in CHAT_FILTERS.items()
|
225 |
-
}
|
226 |
-
|
227 |
-
finally:
|
228 |
-
SESSION.close()
|
229 |
-
|
230 |
-
|
231 |
-
# ONLY USE FOR MIGRATE OLD FILTERS TO NEW FILTERS
|
232 |
-
def __migrate_filters():
|
233 |
-
try:
|
234 |
-
all_filters = SESSION.query(CustomFilters).distinct().all()
|
235 |
-
for x in all_filters:
|
236 |
-
if x.is_document:
|
237 |
-
file_type = Types.DOCUMENT
|
238 |
-
elif x.is_image:
|
239 |
-
file_type = Types.PHOTO
|
240 |
-
elif x.is_video:
|
241 |
-
file_type = Types.VIDEO
|
242 |
-
elif x.is_sticker:
|
243 |
-
file_type = Types.STICKER
|
244 |
-
elif x.is_audio:
|
245 |
-
file_type = Types.AUDIO
|
246 |
-
elif x.is_voice:
|
247 |
-
file_type = Types.VOICE
|
248 |
-
else:
|
249 |
-
file_type = Types.TEXT
|
250 |
-
|
251 |
-
if file_type == Types.TEXT:
|
252 |
-
filt = CustomFilters(
|
253 |
-
str(x.chat_id),
|
254 |
-
x.keyword,
|
255 |
-
x.reply,
|
256 |
-
file_type.value,
|
257 |
-
None,
|
258 |
-
)
|
259 |
-
else:
|
260 |
-
filt = CustomFilters(
|
261 |
-
str(x.chat_id),
|
262 |
-
x.keyword,
|
263 |
-
None,
|
264 |
-
file_type.value,
|
265 |
-
x.reply,
|
266 |
-
)
|
267 |
-
|
268 |
-
SESSION.add(filt)
|
269 |
-
SESSION.commit()
|
270 |
-
|
271 |
-
finally:
|
272 |
-
SESSION.close()
|
273 |
-
|
274 |
-
|
275 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
276 |
-
with CUST_FILT_LOCK:
|
277 |
-
chat_filters = (
|
278 |
-
SESSION.query(CustomFilters)
|
279 |
-
.filter(CustomFilters.chat_id == str(old_chat_id))
|
280 |
-
.all()
|
281 |
-
)
|
282 |
-
for filt in chat_filters:
|
283 |
-
filt.chat_id = str(new_chat_id)
|
284 |
-
SESSION.commit()
|
285 |
-
old_filt = CHAT_FILTERS.get(str(old_chat_id))
|
286 |
-
if old_filt:
|
287 |
-
CHAT_FILTERS[str(new_chat_id)] = old_filt
|
288 |
-
del CHAT_FILTERS[str(old_chat_id)]
|
289 |
-
|
290 |
-
with BUTTON_LOCK:
|
291 |
-
chat_buttons = (
|
292 |
-
SESSION.query(Buttons).filter(Buttons.chat_id == str(old_chat_id)).all()
|
293 |
-
)
|
294 |
-
for btn in chat_buttons:
|
295 |
-
btn.chat_id = str(new_chat_id)
|
296 |
-
SESSION.commit()
|
297 |
-
|
298 |
-
|
299 |
-
__load_chat_filters()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/disable_sql.py
DELETED
@@ -1,105 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import Column, String, UnicodeText, distinct, func
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
|
7 |
-
|
8 |
-
class Disable(BASE):
|
9 |
-
__tablename__ = "disabled_commands"
|
10 |
-
chat_id = Column(String(14), primary_key=True)
|
11 |
-
command = Column(UnicodeText, primary_key=True)
|
12 |
-
|
13 |
-
def __init__(self, chat_id, command):
|
14 |
-
self.chat_id = chat_id
|
15 |
-
self.command = command
|
16 |
-
|
17 |
-
def __repr__(self):
|
18 |
-
return "Disabled cmd {} in {}".format(self.command, self.chat_id)
|
19 |
-
|
20 |
-
|
21 |
-
Disable.__table__.create(checkfirst=True)
|
22 |
-
DISABLE_INSERTION_LOCK = threading.RLock()
|
23 |
-
|
24 |
-
DISABLED = {}
|
25 |
-
|
26 |
-
|
27 |
-
def disable_command(chat_id, disable):
|
28 |
-
with DISABLE_INSERTION_LOCK:
|
29 |
-
disabled = SESSION.query(Disable).get((str(chat_id), disable))
|
30 |
-
|
31 |
-
if not disabled:
|
32 |
-
DISABLED.setdefault(str(chat_id), set()).add(disable)
|
33 |
-
|
34 |
-
disabled = Disable(str(chat_id), disable)
|
35 |
-
SESSION.add(disabled)
|
36 |
-
SESSION.commit()
|
37 |
-
return True
|
38 |
-
|
39 |
-
SESSION.close()
|
40 |
-
return False
|
41 |
-
|
42 |
-
|
43 |
-
def enable_command(chat_id, enable):
|
44 |
-
with DISABLE_INSERTION_LOCK:
|
45 |
-
disabled = SESSION.query(Disable).get((str(chat_id), enable))
|
46 |
-
|
47 |
-
if disabled:
|
48 |
-
if enable in DISABLED.get(str(chat_id)): # sanity check
|
49 |
-
DISABLED.setdefault(str(chat_id), set()).remove(enable)
|
50 |
-
|
51 |
-
SESSION.delete(disabled)
|
52 |
-
SESSION.commit()
|
53 |
-
return True
|
54 |
-
|
55 |
-
SESSION.close()
|
56 |
-
return False
|
57 |
-
|
58 |
-
|
59 |
-
def is_command_disabled(chat_id, cmd):
|
60 |
-
return str(cmd).lower() in DISABLED.get(str(chat_id), set())
|
61 |
-
|
62 |
-
|
63 |
-
def get_all_disabled(chat_id):
|
64 |
-
return DISABLED.get(str(chat_id), set())
|
65 |
-
|
66 |
-
|
67 |
-
def num_chats():
|
68 |
-
try:
|
69 |
-
return SESSION.query(func.count(distinct(Disable.chat_id))).scalar()
|
70 |
-
finally:
|
71 |
-
SESSION.close()
|
72 |
-
|
73 |
-
|
74 |
-
def num_disabled():
|
75 |
-
try:
|
76 |
-
return SESSION.query(Disable).count()
|
77 |
-
finally:
|
78 |
-
SESSION.close()
|
79 |
-
|
80 |
-
|
81 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
82 |
-
with DISABLE_INSERTION_LOCK:
|
83 |
-
chats = SESSION.query(Disable).filter(Disable.chat_id == str(old_chat_id)).all()
|
84 |
-
for chat in chats:
|
85 |
-
chat.chat_id = str(new_chat_id)
|
86 |
-
SESSION.add(chat)
|
87 |
-
|
88 |
-
if str(old_chat_id) in DISABLED:
|
89 |
-
DISABLED[str(new_chat_id)] = DISABLED.get(str(old_chat_id), set())
|
90 |
-
|
91 |
-
SESSION.commit()
|
92 |
-
|
93 |
-
|
94 |
-
def __load_disabled_commands():
|
95 |
-
global DISABLED
|
96 |
-
try:
|
97 |
-
all_chats = SESSION.query(Disable).all()
|
98 |
-
for chat in all_chats:
|
99 |
-
DISABLED.setdefault(chat.chat_id, set()).add(chat.command)
|
100 |
-
|
101 |
-
finally:
|
102 |
-
SESSION.close()
|
103 |
-
|
104 |
-
|
105 |
-
__load_disabled_commands()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/feds_sql.py
DELETED
@@ -1,931 +0,0 @@
|
|
1 |
-
import ast
|
2 |
-
import threading
|
3 |
-
|
4 |
-
from sqlalchemy import BigInteger, Boolean, Column, Integer, String, UnicodeText
|
5 |
-
from telegram.error import BadRequest, Forbidden
|
6 |
-
|
7 |
-
from Database.sql import BASE, SESSION
|
8 |
-
from Mikobot import dispatcher
|
9 |
-
|
10 |
-
|
11 |
-
class Federations(BASE):
|
12 |
-
__tablename__ = "feds"
|
13 |
-
owner_id = Column(String(14))
|
14 |
-
fed_name = Column(UnicodeText)
|
15 |
-
fed_id = Column(UnicodeText, primary_key=True)
|
16 |
-
fed_rules = Column(UnicodeText)
|
17 |
-
fed_log = Column(UnicodeText)
|
18 |
-
fed_users = Column(UnicodeText)
|
19 |
-
|
20 |
-
def __init__(self, owner_id, fed_name, fed_id, fed_rules, fed_log, fed_users):
|
21 |
-
self.owner_id = owner_id
|
22 |
-
self.fed_name = fed_name
|
23 |
-
self.fed_id = fed_id
|
24 |
-
self.fed_rules = fed_rules
|
25 |
-
self.fed_log = fed_log
|
26 |
-
self.fed_users = fed_users
|
27 |
-
|
28 |
-
|
29 |
-
class ChatF(BASE):
|
30 |
-
__tablename__ = "chat_feds"
|
31 |
-
chat_id = Column(String(14), primary_key=True)
|
32 |
-
chat_name = Column(UnicodeText)
|
33 |
-
fed_id = Column(UnicodeText)
|
34 |
-
|
35 |
-
def __init__(self, chat_id, chat_name, fed_id):
|
36 |
-
self.chat_id = chat_id
|
37 |
-
self.chat_name = chat_name
|
38 |
-
self.fed_id = fed_id
|
39 |
-
|
40 |
-
|
41 |
-
class BansF(BASE):
|
42 |
-
__tablename__ = "bans_feds"
|
43 |
-
fed_id = Column(UnicodeText, primary_key=True)
|
44 |
-
user_id = Column(String(14), primary_key=True)
|
45 |
-
first_name = Column(UnicodeText, nullable=False)
|
46 |
-
last_name = Column(UnicodeText)
|
47 |
-
user_name = Column(UnicodeText)
|
48 |
-
reason = Column(UnicodeText, default="")
|
49 |
-
time = Column(Integer, default=0)
|
50 |
-
|
51 |
-
def __init__(self, fed_id, user_id, first_name, last_name, user_name, reason, time):
|
52 |
-
self.fed_id = fed_id
|
53 |
-
self.user_id = user_id
|
54 |
-
self.first_name = first_name
|
55 |
-
self.last_name = last_name
|
56 |
-
self.user_name = user_name
|
57 |
-
self.reason = reason
|
58 |
-
self.time = time
|
59 |
-
|
60 |
-
|
61 |
-
class FedsUserSettings(BASE):
|
62 |
-
__tablename__ = "feds_settings"
|
63 |
-
user_id = Column(BigInteger, primary_key=True)
|
64 |
-
should_report = Column(Boolean, default=True)
|
65 |
-
|
66 |
-
def __init__(self, user_id):
|
67 |
-
self.user_id = user_id
|
68 |
-
|
69 |
-
def __repr__(self):
|
70 |
-
return "<Feds report settings ({})>".format(self.user_id)
|
71 |
-
|
72 |
-
|
73 |
-
class FedSubs(BASE):
|
74 |
-
__tablename__ = "feds_subs"
|
75 |
-
fed_id = Column(UnicodeText, primary_key=True)
|
76 |
-
fed_subs = Column(UnicodeText, primary_key=True, nullable=False)
|
77 |
-
|
78 |
-
def __init__(self, fed_id, fed_subs):
|
79 |
-
self.fed_id = fed_id
|
80 |
-
self.fed_subs = fed_subs
|
81 |
-
|
82 |
-
def __repr__(self):
|
83 |
-
return "<Fed {} subscribes for {}>".format(self.fed_id, self.fed_subs)
|
84 |
-
|
85 |
-
|
86 |
-
# Dropping db
|
87 |
-
# Federations.__table__.drop()
|
88 |
-
# ChatF.__table__.drop()
|
89 |
-
# BansF.__table__.drop()
|
90 |
-
# FedSubs.__table__.drop()
|
91 |
-
|
92 |
-
Federations.__table__.create(checkfirst=True)
|
93 |
-
ChatF.__table__.create(checkfirst=True)
|
94 |
-
BansF.__table__.create(checkfirst=True)
|
95 |
-
FedsUserSettings.__table__.create(checkfirst=True)
|
96 |
-
FedSubs.__table__.create(checkfirst=True)
|
97 |
-
|
98 |
-
FEDS_LOCK = threading.RLock()
|
99 |
-
CHAT_FEDS_LOCK = threading.RLock()
|
100 |
-
FEDS_SETTINGS_LOCK = threading.RLock()
|
101 |
-
FEDS_SUBSCRIBER_LOCK = threading.RLock()
|
102 |
-
|
103 |
-
FEDERATION_BYNAME = {}
|
104 |
-
FEDERATION_BYOWNER = {}
|
105 |
-
FEDERATION_BYFEDID = {}
|
106 |
-
|
107 |
-
FEDERATION_CHATS = {}
|
108 |
-
FEDERATION_CHATS_BYID = {}
|
109 |
-
|
110 |
-
FEDERATION_BANNED_FULL = {}
|
111 |
-
FEDERATION_BANNED_USERID = {}
|
112 |
-
|
113 |
-
FEDERATION_NOTIFICATION = {}
|
114 |
-
FEDS_SUBSCRIBER = {}
|
115 |
-
MYFEDS_SUBSCRIBER = {}
|
116 |
-
|
117 |
-
|
118 |
-
def get_fed_info(fed_id):
|
119 |
-
get = FEDERATION_BYFEDID.get(str(fed_id))
|
120 |
-
if get is None:
|
121 |
-
return False
|
122 |
-
return get
|
123 |
-
|
124 |
-
|
125 |
-
def get_fed_id(chat_id):
|
126 |
-
get = FEDERATION_CHATS.get(str(chat_id))
|
127 |
-
if get is None:
|
128 |
-
return False
|
129 |
-
else:
|
130 |
-
return get["fid"]
|
131 |
-
|
132 |
-
|
133 |
-
def get_fed_name(chat_id):
|
134 |
-
get = FEDERATION_CHATS.get(str(chat_id))
|
135 |
-
if get is None:
|
136 |
-
return False
|
137 |
-
else:
|
138 |
-
return get["chat_name"]
|
139 |
-
|
140 |
-
|
141 |
-
def get_user_fban(fed_id, user_id):
|
142 |
-
if not FEDERATION_BANNED_FULL.get(fed_id):
|
143 |
-
return False, False, False
|
144 |
-
user_info = FEDERATION_BANNED_FULL[fed_id].get(user_id)
|
145 |
-
if not user_info:
|
146 |
-
return None, None, None
|
147 |
-
return user_info["first_name"], user_info["reason"], user_info["time"]
|
148 |
-
|
149 |
-
|
150 |
-
def get_user_admin_fed_name(user_id):
|
151 |
-
user_feds = []
|
152 |
-
for f in FEDERATION_BYFEDID:
|
153 |
-
if int(user_id) in ast.literal_eval(
|
154 |
-
ast.literal_eval(FEDERATION_BYFEDID[f]["fusers"])["members"]
|
155 |
-
):
|
156 |
-
user_feds.append(FEDERATION_BYFEDID[f]["fname"])
|
157 |
-
return user_feds
|
158 |
-
|
159 |
-
|
160 |
-
def get_user_owner_fed_name(user_id):
|
161 |
-
user_feds = []
|
162 |
-
for f in FEDERATION_BYFEDID:
|
163 |
-
if int(user_id) == int(
|
164 |
-
ast.literal_eval(FEDERATION_BYFEDID[f]["fusers"])["owner"]
|
165 |
-
):
|
166 |
-
user_feds.append(FEDERATION_BYFEDID[f]["fname"])
|
167 |
-
return user_feds
|
168 |
-
|
169 |
-
|
170 |
-
def get_user_admin_fed_full(user_id):
|
171 |
-
user_feds = []
|
172 |
-
for f in FEDERATION_BYFEDID:
|
173 |
-
if int(user_id) in ast.literal_eval(
|
174 |
-
ast.literal_eval(FEDERATION_BYFEDID[f]["fusers"])["members"]
|
175 |
-
):
|
176 |
-
user_feds.append({"fed_id": f, "fed": FEDERATION_BYFEDID[f]})
|
177 |
-
return user_feds
|
178 |
-
|
179 |
-
|
180 |
-
def get_user_owner_fed_full(user_id):
|
181 |
-
user_feds = []
|
182 |
-
for f in FEDERATION_BYFEDID:
|
183 |
-
if int(user_id) == int(
|
184 |
-
ast.literal_eval(FEDERATION_BYFEDID[f]["fusers"])["owner"]
|
185 |
-
):
|
186 |
-
user_feds.append({"fed_id": f, "fed": FEDERATION_BYFEDID[f]})
|
187 |
-
return user_feds
|
188 |
-
|
189 |
-
|
190 |
-
def get_user_fbanlist(user_id):
|
191 |
-
banlist = FEDERATION_BANNED_FULL
|
192 |
-
user_name = ""
|
193 |
-
fedname = []
|
194 |
-
for x in banlist:
|
195 |
-
if banlist[x].get(user_id):
|
196 |
-
if user_name == "":
|
197 |
-
user_name = banlist[x][user_id].get("first_name")
|
198 |
-
fedname.append([x, banlist[x][user_id].get("reason")])
|
199 |
-
return user_name, fedname
|
200 |
-
|
201 |
-
|
202 |
-
def new_fed(owner_id, fed_name, fed_id):
|
203 |
-
with FEDS_LOCK:
|
204 |
-
global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME
|
205 |
-
fed = Federations(
|
206 |
-
str(owner_id),
|
207 |
-
fed_name,
|
208 |
-
str(fed_id),
|
209 |
-
"Rules is not set in this federation.",
|
210 |
-
None,
|
211 |
-
str({"owner": str(owner_id), "members": "[]"}),
|
212 |
-
)
|
213 |
-
SESSION.add(fed)
|
214 |
-
SESSION.commit()
|
215 |
-
FEDERATION_BYOWNER[str(owner_id)] = {
|
216 |
-
"fid": str(fed_id),
|
217 |
-
"fname": fed_name,
|
218 |
-
"frules": "Rules is not set in this federation.",
|
219 |
-
"flog": None,
|
220 |
-
"fusers": str({"owner": str(owner_id), "members": "[]"}),
|
221 |
-
}
|
222 |
-
FEDERATION_BYFEDID[str(fed_id)] = {
|
223 |
-
"owner": str(owner_id),
|
224 |
-
"fname": fed_name,
|
225 |
-
"frules": "Rules is not set in this federation.",
|
226 |
-
"flog": None,
|
227 |
-
"fusers": str({"owner": str(owner_id), "members": "[]"}),
|
228 |
-
}
|
229 |
-
FEDERATION_BYNAME[fed_name] = {
|
230 |
-
"fid": str(fed_id),
|
231 |
-
"owner": str(owner_id),
|
232 |
-
"frules": "Rules is not set in this federation.",
|
233 |
-
"flog": None,
|
234 |
-
"fusers": str({"owner": str(owner_id), "members": "[]"}),
|
235 |
-
}
|
236 |
-
return fed
|
237 |
-
|
238 |
-
|
239 |
-
def del_fed(fed_id):
|
240 |
-
with FEDS_LOCK:
|
241 |
-
global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME, FEDERATION_CHATS, FEDERATION_CHATS_BYID, FEDERATION_BANNED_USERID, FEDERATION_BANNED_FULL
|
242 |
-
getcache = FEDERATION_BYFEDID.get(fed_id)
|
243 |
-
if getcache is None:
|
244 |
-
return False
|
245 |
-
# Variables
|
246 |
-
getfed = FEDERATION_BYFEDID.get(fed_id)
|
247 |
-
owner_id = getfed["owner"]
|
248 |
-
fed_name = getfed["fname"]
|
249 |
-
# Delete from cache
|
250 |
-
FEDERATION_BYOWNER.pop(owner_id)
|
251 |
-
FEDERATION_BYFEDID.pop(fed_id)
|
252 |
-
FEDERATION_BYNAME.pop(fed_name)
|
253 |
-
if FEDERATION_CHATS_BYID.get(fed_id):
|
254 |
-
for x in FEDERATION_CHATS_BYID[fed_id]:
|
255 |
-
delchats = SESSION.query(ChatF).get(str(x))
|
256 |
-
if delchats:
|
257 |
-
SESSION.delete(delchats)
|
258 |
-
SESSION.commit()
|
259 |
-
FEDERATION_CHATS.pop(x)
|
260 |
-
FEDERATION_CHATS_BYID.pop(fed_id)
|
261 |
-
# Delete fedban users
|
262 |
-
getall = FEDERATION_BANNED_USERID.get(fed_id)
|
263 |
-
if getall:
|
264 |
-
for x in getall:
|
265 |
-
banlist = SESSION.query(BansF).get((fed_id, str(x)))
|
266 |
-
if banlist:
|
267 |
-
SESSION.delete(banlist)
|
268 |
-
SESSION.commit()
|
269 |
-
if FEDERATION_BANNED_USERID.get(fed_id):
|
270 |
-
FEDERATION_BANNED_USERID.pop(fed_id)
|
271 |
-
if FEDERATION_BANNED_FULL.get(fed_id):
|
272 |
-
FEDERATION_BANNED_FULL.pop(fed_id)
|
273 |
-
# Delete fedsubs
|
274 |
-
getall = MYFEDS_SUBSCRIBER.get(fed_id)
|
275 |
-
if getall:
|
276 |
-
for x in getall:
|
277 |
-
getsubs = SESSION.query(FedSubs).get((fed_id, str(x)))
|
278 |
-
if getsubs:
|
279 |
-
SESSION.delete(getsubs)
|
280 |
-
SESSION.commit()
|
281 |
-
if FEDS_SUBSCRIBER.get(fed_id):
|
282 |
-
FEDS_SUBSCRIBER.pop(fed_id)
|
283 |
-
if MYFEDS_SUBSCRIBER.get(fed_id):
|
284 |
-
MYFEDS_SUBSCRIBER.pop(fed_id)
|
285 |
-
# Delete from database
|
286 |
-
curr = SESSION.query(Federations).get(fed_id)
|
287 |
-
if curr:
|
288 |
-
SESSION.delete(curr)
|
289 |
-
SESSION.commit()
|
290 |
-
return True
|
291 |
-
|
292 |
-
|
293 |
-
def rename_fed(fed_id, owner_id, newname):
|
294 |
-
with FEDS_LOCK:
|
295 |
-
global FEDERATION_BYFEDID, FEDERATION_BYOWNER, FEDERATION_BYNAME
|
296 |
-
fed = SESSION.query(Federations).get(fed_id)
|
297 |
-
if not fed:
|
298 |
-
return False
|
299 |
-
fed.fed_name = newname
|
300 |
-
SESSION.commit()
|
301 |
-
|
302 |
-
# Update the dicts
|
303 |
-
oldname = FEDERATION_BYFEDID[str(fed_id)]["fname"]
|
304 |
-
tempdata = FEDERATION_BYNAME[oldname]
|
305 |
-
FEDERATION_BYNAME.pop(oldname)
|
306 |
-
|
307 |
-
FEDERATION_BYOWNER[str(owner_id)]["fname"] = newname
|
308 |
-
FEDERATION_BYFEDID[str(fed_id)]["fname"] = newname
|
309 |
-
FEDERATION_BYNAME[newname] = tempdata
|
310 |
-
return True
|
311 |
-
|
312 |
-
|
313 |
-
def chat_join_fed(fed_id, chat_name, chat_id):
|
314 |
-
with FEDS_LOCK:
|
315 |
-
global FEDERATION_CHATS, FEDERATION_CHATS_BYID
|
316 |
-
r = ChatF(chat_id, chat_name, fed_id)
|
317 |
-
SESSION.add(r)
|
318 |
-
FEDERATION_CHATS[str(chat_id)] = {"chat_name": chat_name, "fid": fed_id}
|
319 |
-
checkid = FEDERATION_CHATS_BYID.get(fed_id)
|
320 |
-
if checkid is None:
|
321 |
-
FEDERATION_CHATS_BYID[fed_id] = []
|
322 |
-
FEDERATION_CHATS_BYID[fed_id].append(str(chat_id))
|
323 |
-
SESSION.commit()
|
324 |
-
return r
|
325 |
-
|
326 |
-
|
327 |
-
def search_fed_by_name(fed_name):
|
328 |
-
allfed = FEDERATION_BYNAME.get(fed_name)
|
329 |
-
if allfed is None:
|
330 |
-
return False
|
331 |
-
return allfed
|
332 |
-
|
333 |
-
|
334 |
-
def search_user_in_fed(fed_id, user_id):
|
335 |
-
getfed = FEDERATION_BYFEDID.get(fed_id)
|
336 |
-
if getfed is None:
|
337 |
-
return False
|
338 |
-
getfed = ast.literal_eval(getfed["fusers"])["members"]
|
339 |
-
if user_id in ast.literal_eval(getfed):
|
340 |
-
return True
|
341 |
-
else:
|
342 |
-
return False
|
343 |
-
|
344 |
-
|
345 |
-
def user_demote_fed(fed_id, user_id):
|
346 |
-
with FEDS_LOCK:
|
347 |
-
global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME
|
348 |
-
# Variables
|
349 |
-
getfed = FEDERATION_BYFEDID.get(str(fed_id))
|
350 |
-
owner_id = getfed["owner"]
|
351 |
-
fed_name = getfed["fname"]
|
352 |
-
fed_rules = getfed["frules"]
|
353 |
-
fed_log = getfed["flog"]
|
354 |
-
# Temp set
|
355 |
-
try:
|
356 |
-
members = ast.literal_eval(ast.literal_eval(getfed["fusers"])["members"])
|
357 |
-
except ValueError:
|
358 |
-
return False
|
359 |
-
members.remove(user_id)
|
360 |
-
# Set user
|
361 |
-
FEDERATION_BYOWNER[str(owner_id)]["fusers"] = str(
|
362 |
-
{"owner": str(owner_id), "members": str(members)},
|
363 |
-
)
|
364 |
-
FEDERATION_BYFEDID[str(fed_id)]["fusers"] = str(
|
365 |
-
{"owner": str(owner_id), "members": str(members)},
|
366 |
-
)
|
367 |
-
FEDERATION_BYNAME[fed_name]["fusers"] = str(
|
368 |
-
{"owner": str(owner_id), "members": str(members)},
|
369 |
-
)
|
370 |
-
# Set on database
|
371 |
-
fed = Federations(
|
372 |
-
str(owner_id),
|
373 |
-
fed_name,
|
374 |
-
str(fed_id),
|
375 |
-
fed_rules,
|
376 |
-
fed_log,
|
377 |
-
str({"owner": str(owner_id), "members": str(members)}),
|
378 |
-
)
|
379 |
-
SESSION.merge(fed)
|
380 |
-
SESSION.commit()
|
381 |
-
return True
|
382 |
-
|
383 |
-
curr = SESSION.query(UserF).all()
|
384 |
-
result = False
|
385 |
-
for r in curr:
|
386 |
-
if int(r.user_id) == int(user_id):
|
387 |
-
if r.fed_id == fed_id:
|
388 |
-
SESSION.delete(r)
|
389 |
-
SESSION.commit()
|
390 |
-
result = True
|
391 |
-
|
392 |
-
SESSION.close()
|
393 |
-
return result
|
394 |
-
|
395 |
-
|
396 |
-
def user_join_fed(fed_id, user_id):
|
397 |
-
with FEDS_LOCK:
|
398 |
-
global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME
|
399 |
-
# Variables
|
400 |
-
getfed = FEDERATION_BYFEDID.get(str(fed_id))
|
401 |
-
owner_id = getfed["owner"]
|
402 |
-
fed_name = getfed["fname"]
|
403 |
-
fed_rules = getfed["frules"]
|
404 |
-
fed_log = getfed["flog"]
|
405 |
-
# Temp set
|
406 |
-
members = ast.literal_eval(ast.literal_eval(getfed["fusers"])["members"])
|
407 |
-
members.append(user_id)
|
408 |
-
# Set user
|
409 |
-
FEDERATION_BYOWNER[str(owner_id)]["fusers"] = str(
|
410 |
-
{"owner": str(owner_id), "members": str(members)},
|
411 |
-
)
|
412 |
-
FEDERATION_BYFEDID[str(fed_id)]["fusers"] = str(
|
413 |
-
{"owner": str(owner_id), "members": str(members)},
|
414 |
-
)
|
415 |
-
FEDERATION_BYNAME[fed_name]["fusers"] = str(
|
416 |
-
{"owner": str(owner_id), "members": str(members)},
|
417 |
-
)
|
418 |
-
# Set on database
|
419 |
-
fed = Federations(
|
420 |
-
str(owner_id),
|
421 |
-
fed_name,
|
422 |
-
str(fed_id),
|
423 |
-
fed_rules,
|
424 |
-
fed_log,
|
425 |
-
str({"owner": str(owner_id), "members": str(members)}),
|
426 |
-
)
|
427 |
-
SESSION.merge(fed)
|
428 |
-
SESSION.commit()
|
429 |
-
__load_all_feds_chats()
|
430 |
-
return True
|
431 |
-
|
432 |
-
|
433 |
-
def chat_leave_fed(chat_id):
|
434 |
-
with FEDS_LOCK:
|
435 |
-
global FEDERATION_CHATS, FEDERATION_CHATS_BYID
|
436 |
-
# Set variables
|
437 |
-
fed_info = FEDERATION_CHATS.get(str(chat_id))
|
438 |
-
if fed_info is None:
|
439 |
-
return False
|
440 |
-
fed_id = fed_info["fid"]
|
441 |
-
# Delete from cache
|
442 |
-
FEDERATION_CHATS.pop(str(chat_id))
|
443 |
-
FEDERATION_CHATS_BYID[str(fed_id)].remove(str(chat_id))
|
444 |
-
# Delete from db
|
445 |
-
curr = SESSION.query(ChatF).all()
|
446 |
-
for U in curr:
|
447 |
-
if int(U.chat_id) == int(chat_id):
|
448 |
-
SESSION.delete(U)
|
449 |
-
SESSION.commit()
|
450 |
-
return True
|
451 |
-
|
452 |
-
|
453 |
-
def all_fed_chats(fed_id):
|
454 |
-
with FEDS_LOCK:
|
455 |
-
getfed = FEDERATION_CHATS_BYID.get(fed_id)
|
456 |
-
if getfed is None:
|
457 |
-
return []
|
458 |
-
else:
|
459 |
-
return getfed
|
460 |
-
|
461 |
-
|
462 |
-
def all_fed_users(fed_id):
|
463 |
-
with FEDS_LOCK:
|
464 |
-
getfed = FEDERATION_BYFEDID.get(str(fed_id))
|
465 |
-
if getfed is None:
|
466 |
-
return False
|
467 |
-
fed_owner = ast.literal_eval(ast.literal_eval(getfed["fusers"])["owner"])
|
468 |
-
fed_admins = ast.literal_eval(ast.literal_eval(getfed["fusers"])["members"])
|
469 |
-
fed_admins.append(fed_owner)
|
470 |
-
return fed_admins
|
471 |
-
|
472 |
-
|
473 |
-
def all_fed_members(fed_id):
|
474 |
-
with FEDS_LOCK:
|
475 |
-
getfed = FEDERATION_BYFEDID.get(str(fed_id))
|
476 |
-
fed_admins = ast.literal_eval(ast.literal_eval(getfed["fusers"])["members"])
|
477 |
-
return fed_admins
|
478 |
-
|
479 |
-
|
480 |
-
def set_frules(fed_id, rules):
|
481 |
-
with FEDS_LOCK:
|
482 |
-
global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME
|
483 |
-
# Variables
|
484 |
-
getfed = FEDERATION_BYFEDID.get(str(fed_id))
|
485 |
-
owner_id = getfed["owner"]
|
486 |
-
fed_name = getfed["fname"]
|
487 |
-
fed_members = getfed["fusers"]
|
488 |
-
fed_rules = str(rules)
|
489 |
-
fed_log = getfed["flog"]
|
490 |
-
# Set user
|
491 |
-
FEDERATION_BYOWNER[str(owner_id)]["frules"] = fed_rules
|
492 |
-
FEDERATION_BYFEDID[str(fed_id)]["frules"] = fed_rules
|
493 |
-
FEDERATION_BYNAME[fed_name]["frules"] = fed_rules
|
494 |
-
# Set on database
|
495 |
-
fed = Federations(
|
496 |
-
str(owner_id),
|
497 |
-
fed_name,
|
498 |
-
str(fed_id),
|
499 |
-
fed_rules,
|
500 |
-
fed_log,
|
501 |
-
str(fed_members),
|
502 |
-
)
|
503 |
-
SESSION.merge(fed)
|
504 |
-
SESSION.commit()
|
505 |
-
return True
|
506 |
-
|
507 |
-
|
508 |
-
def get_frules(fed_id):
|
509 |
-
with FEDS_LOCK:
|
510 |
-
rules = FEDERATION_BYFEDID[str(fed_id)]["frules"]
|
511 |
-
return rules
|
512 |
-
|
513 |
-
|
514 |
-
def fban_user(fed_id, user_id, first_name, last_name, user_name, reason, time):
|
515 |
-
with FEDS_LOCK:
|
516 |
-
r = SESSION.query(BansF).all()
|
517 |
-
for I in r:
|
518 |
-
if I.fed_id == fed_id:
|
519 |
-
if int(I.user_id) == int(user_id):
|
520 |
-
SESSION.delete(I)
|
521 |
-
|
522 |
-
r = BansF(
|
523 |
-
str(fed_id),
|
524 |
-
str(user_id),
|
525 |
-
first_name,
|
526 |
-
last_name,
|
527 |
-
user_name,
|
528 |
-
reason,
|
529 |
-
time,
|
530 |
-
)
|
531 |
-
|
532 |
-
SESSION.add(r)
|
533 |
-
try:
|
534 |
-
SESSION.commit()
|
535 |
-
except:
|
536 |
-
SESSION.rollback()
|
537 |
-
return False
|
538 |
-
finally:
|
539 |
-
SESSION.commit()
|
540 |
-
__load_all_feds_banned()
|
541 |
-
return r
|
542 |
-
|
543 |
-
|
544 |
-
def multi_fban_user(
|
545 |
-
multi_fed_id,
|
546 |
-
multi_user_id,
|
547 |
-
multi_first_name,
|
548 |
-
multi_last_name,
|
549 |
-
multi_user_name,
|
550 |
-
multi_reason,
|
551 |
-
):
|
552 |
-
if True: # with FEDS_LOCK:
|
553 |
-
counter = 0
|
554 |
-
time = 0
|
555 |
-
for x in range(len(multi_fed_id)):
|
556 |
-
fed_id = multi_fed_id[x]
|
557 |
-
user_id = multi_user_id[x]
|
558 |
-
first_name = multi_first_name[x]
|
559 |
-
last_name = multi_last_name[x]
|
560 |
-
user_name = multi_user_name[x]
|
561 |
-
reason = multi_reason[x]
|
562 |
-
r = SESSION.query(BansF).all()
|
563 |
-
for I in r:
|
564 |
-
if I.fed_id == fed_id:
|
565 |
-
if int(I.user_id) == int(user_id):
|
566 |
-
SESSION.delete(I)
|
567 |
-
|
568 |
-
r = BansF(
|
569 |
-
str(fed_id),
|
570 |
-
str(user_id),
|
571 |
-
first_name,
|
572 |
-
last_name,
|
573 |
-
user_name,
|
574 |
-
reason,
|
575 |
-
time,
|
576 |
-
)
|
577 |
-
|
578 |
-
SESSION.add(r)
|
579 |
-
counter += 1
|
580 |
-
try:
|
581 |
-
SESSION.commit()
|
582 |
-
except:
|
583 |
-
SESSION.rollback()
|
584 |
-
return False
|
585 |
-
finally:
|
586 |
-
SESSION.commit()
|
587 |
-
__load_all_feds_banned()
|
588 |
-
return counter
|
589 |
-
|
590 |
-
|
591 |
-
def un_fban_user(fed_id, user_id):
|
592 |
-
with FEDS_LOCK:
|
593 |
-
r = SESSION.query(BansF).all()
|
594 |
-
for I in r:
|
595 |
-
if I.fed_id == fed_id:
|
596 |
-
if int(I.user_id) == int(user_id):
|
597 |
-
SESSION.delete(I)
|
598 |
-
try:
|
599 |
-
SESSION.commit()
|
600 |
-
except:
|
601 |
-
SESSION.rollback()
|
602 |
-
return False
|
603 |
-
finally:
|
604 |
-
SESSION.commit()
|
605 |
-
__load_all_feds_banned()
|
606 |
-
return I
|
607 |
-
|
608 |
-
|
609 |
-
def get_fban_user(fed_id, user_id):
|
610 |
-
list_fbanned = FEDERATION_BANNED_USERID.get(fed_id)
|
611 |
-
if list_fbanned is None:
|
612 |
-
FEDERATION_BANNED_USERID[fed_id] = []
|
613 |
-
if user_id in FEDERATION_BANNED_USERID[fed_id]:
|
614 |
-
r = SESSION.query(BansF).all()
|
615 |
-
reason = None
|
616 |
-
for I in r:
|
617 |
-
if I.fed_id == fed_id:
|
618 |
-
if int(I.user_id) == int(user_id):
|
619 |
-
reason = I.reason
|
620 |
-
time = I.time
|
621 |
-
return True, reason, time
|
622 |
-
else:
|
623 |
-
return False, None, None
|
624 |
-
|
625 |
-
|
626 |
-
def get_all_fban_users(fed_id):
|
627 |
-
list_fbanned = FEDERATION_BANNED_USERID.get(fed_id)
|
628 |
-
if list_fbanned is None:
|
629 |
-
FEDERATION_BANNED_USERID[fed_id] = []
|
630 |
-
return FEDERATION_BANNED_USERID[fed_id]
|
631 |
-
|
632 |
-
|
633 |
-
def get_all_fban_users_target(fed_id, user_id):
|
634 |
-
list_fbanned = FEDERATION_BANNED_FULL.get(fed_id)
|
635 |
-
if list_fbanned is None:
|
636 |
-
FEDERATION_BANNED_FULL[fed_id] = []
|
637 |
-
return False
|
638 |
-
getuser = list_fbanned[str(user_id)]
|
639 |
-
return getuser
|
640 |
-
|
641 |
-
|
642 |
-
def get_all_fban_users_global():
|
643 |
-
list_fbanned = FEDERATION_BANNED_USERID
|
644 |
-
total = []
|
645 |
-
for x in list(FEDERATION_BANNED_USERID):
|
646 |
-
for y in FEDERATION_BANNED_USERID[x]:
|
647 |
-
total.append(y)
|
648 |
-
return total
|
649 |
-
|
650 |
-
|
651 |
-
def get_all_feds_users_global():
|
652 |
-
list_fed = FEDERATION_BYFEDID
|
653 |
-
total = []
|
654 |
-
for x in list(FEDERATION_BYFEDID):
|
655 |
-
total.append(FEDERATION_BYFEDID[x])
|
656 |
-
return total
|
657 |
-
|
658 |
-
|
659 |
-
def search_fed_by_id(fed_id):
|
660 |
-
get = FEDERATION_BYFEDID.get(fed_id)
|
661 |
-
if get is None:
|
662 |
-
return False
|
663 |
-
else:
|
664 |
-
return get
|
665 |
-
result = False
|
666 |
-
for Q in curr:
|
667 |
-
if Q.fed_id == fed_id:
|
668 |
-
result = Q.fed_id
|
669 |
-
|
670 |
-
return result
|
671 |
-
|
672 |
-
|
673 |
-
def user_feds_report(user_id: int) -> bool:
|
674 |
-
user_setting = FEDERATION_NOTIFICATION.get(str(user_id))
|
675 |
-
if user_setting is None:
|
676 |
-
user_setting = True
|
677 |
-
return user_setting
|
678 |
-
|
679 |
-
|
680 |
-
def set_feds_setting(user_id: int, setting: bool):
|
681 |
-
with FEDS_SETTINGS_LOCK:
|
682 |
-
global FEDERATION_NOTIFICATION
|
683 |
-
user_setting = SESSION.query(FedsUserSettings).get(user_id)
|
684 |
-
if not user_setting:
|
685 |
-
user_setting = FedsUserSettings(user_id)
|
686 |
-
|
687 |
-
user_setting.should_report = setting
|
688 |
-
FEDERATION_NOTIFICATION[str(user_id)] = setting
|
689 |
-
SESSION.add(user_setting)
|
690 |
-
SESSION.commit()
|
691 |
-
|
692 |
-
|
693 |
-
async def get_fed_log(fed_id):
|
694 |
-
fed_setting = FEDERATION_BYFEDID.get(str(fed_id))
|
695 |
-
if fed_setting is None:
|
696 |
-
fed_setting = False
|
697 |
-
return fed_setting
|
698 |
-
if fed_setting.get("flog") is None:
|
699 |
-
return False
|
700 |
-
elif fed_setting.get("flog"):
|
701 |
-
try:
|
702 |
-
await dispatcher.bot.get_chat(fed_setting.get("flog"))
|
703 |
-
except BadRequest:
|
704 |
-
set_fed_log(fed_id, None)
|
705 |
-
return False
|
706 |
-
except Forbidden:
|
707 |
-
set_fed_log(fed_id, None)
|
708 |
-
return False
|
709 |
-
return fed_setting.get("flog")
|
710 |
-
else:
|
711 |
-
return False
|
712 |
-
|
713 |
-
|
714 |
-
def set_fed_log(fed_id, chat_id):
|
715 |
-
with FEDS_LOCK:
|
716 |
-
global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME
|
717 |
-
# Variables
|
718 |
-
getfed = FEDERATION_BYFEDID.get(str(fed_id))
|
719 |
-
owner_id = getfed["owner"]
|
720 |
-
fed_name = getfed["fname"]
|
721 |
-
fed_members = getfed["fusers"]
|
722 |
-
fed_rules = getfed["frules"]
|
723 |
-
fed_log = str(chat_id)
|
724 |
-
# Set user
|
725 |
-
FEDERATION_BYOWNER[str(owner_id)]["flog"] = fed_log
|
726 |
-
FEDERATION_BYFEDID[str(fed_id)]["flog"] = fed_log
|
727 |
-
FEDERATION_BYNAME[fed_name]["flog"] = fed_log
|
728 |
-
# Set on database
|
729 |
-
fed = Federations(
|
730 |
-
str(owner_id),
|
731 |
-
fed_name,
|
732 |
-
str(fed_id),
|
733 |
-
fed_rules,
|
734 |
-
fed_log,
|
735 |
-
str(fed_members),
|
736 |
-
)
|
737 |
-
SESSION.merge(fed)
|
738 |
-
SESSION.commit()
|
739 |
-
return True
|
740 |
-
|
741 |
-
|
742 |
-
def subs_fed(fed_id, my_fed):
|
743 |
-
check = get_spec_subs(fed_id, my_fed)
|
744 |
-
if check:
|
745 |
-
return False
|
746 |
-
with FEDS_SUBSCRIBER_LOCK:
|
747 |
-
subsfed = FedSubs(fed_id, my_fed)
|
748 |
-
|
749 |
-
SESSION.merge(subsfed) # merge to avoid duplicate key issues
|
750 |
-
SESSION.commit()
|
751 |
-
global FEDS_SUBSCRIBER, MYFEDS_SUBSCRIBER
|
752 |
-
# Temporary Data For Subbed Feds
|
753 |
-
if FEDS_SUBSCRIBER.get(fed_id, set()) == set():
|
754 |
-
FEDS_SUBSCRIBER[fed_id] = {my_fed}
|
755 |
-
else:
|
756 |
-
FEDS_SUBSCRIBER.get(fed_id, set()).add(my_fed)
|
757 |
-
# Temporary data for Fed Subs
|
758 |
-
if MYFEDS_SUBSCRIBER.get(my_fed, set()) == set():
|
759 |
-
MYFEDS_SUBSCRIBER[my_fed] = {fed_id}
|
760 |
-
else:
|
761 |
-
MYFEDS_SUBSCRIBER.get(my_fed, set()).add(fed_id)
|
762 |
-
return True
|
763 |
-
|
764 |
-
|
765 |
-
def unsubs_fed(fed_id, my_fed):
|
766 |
-
with FEDS_SUBSCRIBER_LOCK:
|
767 |
-
getsubs = SESSION.query(FedSubs).get((fed_id, my_fed))
|
768 |
-
if getsubs:
|
769 |
-
if my_fed in FEDS_SUBSCRIBER.get(fed_id, set()): # sanity check
|
770 |
-
FEDS_SUBSCRIBER.get(fed_id, set()).remove(my_fed)
|
771 |
-
if fed_id in MYFEDS_SUBSCRIBER.get(my_fed, set()): # sanity check
|
772 |
-
MYFEDS_SUBSCRIBER.get(my_fed, set()).remove(fed_id)
|
773 |
-
|
774 |
-
SESSION.delete(getsubs)
|
775 |
-
SESSION.commit()
|
776 |
-
return True
|
777 |
-
|
778 |
-
SESSION.close()
|
779 |
-
return False
|
780 |
-
|
781 |
-
|
782 |
-
def get_all_subs(fed_id):
|
783 |
-
return FEDS_SUBSCRIBER.get(fed_id, set())
|
784 |
-
|
785 |
-
|
786 |
-
def get_spec_subs(fed_id, fed_target):
|
787 |
-
if FEDS_SUBSCRIBER.get(fed_id, set()) == set():
|
788 |
-
return {}
|
789 |
-
else:
|
790 |
-
return FEDS_SUBSCRIBER.get(fed_id, fed_target)
|
791 |
-
|
792 |
-
|
793 |
-
def get_mysubs(my_fed):
|
794 |
-
return list(MYFEDS_SUBSCRIBER.get(my_fed))
|
795 |
-
|
796 |
-
|
797 |
-
def get_subscriber(fed_id):
|
798 |
-
return FEDS_SUBSCRIBER.get(fed_id, set())
|
799 |
-
|
800 |
-
|
801 |
-
def __load_all_feds():
|
802 |
-
global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME
|
803 |
-
try:
|
804 |
-
feds = SESSION.query(Federations).all()
|
805 |
-
for x in feds: # remove tuple by ( ,)
|
806 |
-
# Fed by Owner
|
807 |
-
check = FEDERATION_BYOWNER.get(x.owner_id)
|
808 |
-
if check is None:
|
809 |
-
FEDERATION_BYOWNER[x.owner_id] = []
|
810 |
-
FEDERATION_BYOWNER[str(x.owner_id)] = {
|
811 |
-
"fid": str(x.fed_id),
|
812 |
-
"fname": x.fed_name,
|
813 |
-
"frules": x.fed_rules,
|
814 |
-
"flog": x.fed_log,
|
815 |
-
"fusers": str(x.fed_users),
|
816 |
-
}
|
817 |
-
# Fed By FedId
|
818 |
-
check = FEDERATION_BYFEDID.get(x.fed_id)
|
819 |
-
if check is None:
|
820 |
-
FEDERATION_BYFEDID[x.fed_id] = []
|
821 |
-
FEDERATION_BYFEDID[str(x.fed_id)] = {
|
822 |
-
"owner": str(x.owner_id),
|
823 |
-
"fname": x.fed_name,
|
824 |
-
"frules": x.fed_rules,
|
825 |
-
"flog": x.fed_log,
|
826 |
-
"fusers": str(x.fed_users),
|
827 |
-
}
|
828 |
-
# Fed By Name
|
829 |
-
check = FEDERATION_BYNAME.get(x.fed_name)
|
830 |
-
if check is None:
|
831 |
-
FEDERATION_BYNAME[x.fed_name] = []
|
832 |
-
FEDERATION_BYNAME[x.fed_name] = {
|
833 |
-
"fid": str(x.fed_id),
|
834 |
-
"owner": str(x.owner_id),
|
835 |
-
"frules": x.fed_rules,
|
836 |
-
"flog": x.fed_log,
|
837 |
-
"fusers": str(x.fed_users),
|
838 |
-
}
|
839 |
-
finally:
|
840 |
-
SESSION.close()
|
841 |
-
|
842 |
-
|
843 |
-
def __load_all_feds_chats():
|
844 |
-
global FEDERATION_CHATS, FEDERATION_CHATS_BYID
|
845 |
-
try:
|
846 |
-
qall = SESSION.query(ChatF).all()
|
847 |
-
FEDERATION_CHATS = {}
|
848 |
-
FEDERATION_CHATS_BYID = {}
|
849 |
-
for x in qall:
|
850 |
-
# Federation Chats
|
851 |
-
check = FEDERATION_CHATS.get(x.chat_id)
|
852 |
-
if check is None:
|
853 |
-
FEDERATION_CHATS[x.chat_id] = {}
|
854 |
-
FEDERATION_CHATS[x.chat_id] = {"chat_name": x.chat_name, "fid": x.fed_id}
|
855 |
-
# Federation Chats By ID
|
856 |
-
check = FEDERATION_CHATS_BYID.get(x.fed_id)
|
857 |
-
if check is None:
|
858 |
-
FEDERATION_CHATS_BYID[x.fed_id] = []
|
859 |
-
FEDERATION_CHATS_BYID[x.fed_id].append(x.chat_id)
|
860 |
-
finally:
|
861 |
-
SESSION.close()
|
862 |
-
|
863 |
-
|
864 |
-
def __load_all_feds_banned():
|
865 |
-
global FEDERATION_BANNED_USERID, FEDERATION_BANNED_FULL
|
866 |
-
try:
|
867 |
-
FEDERATION_BANNED_USERID = {}
|
868 |
-
FEDERATION_BANNED_FULL = {}
|
869 |
-
qall = SESSION.query(BansF).all()
|
870 |
-
for x in qall:
|
871 |
-
check = FEDERATION_BANNED_USERID.get(x.fed_id)
|
872 |
-
if check is None:
|
873 |
-
FEDERATION_BANNED_USERID[x.fed_id] = []
|
874 |
-
if int(x.user_id) not in FEDERATION_BANNED_USERID[x.fed_id]:
|
875 |
-
FEDERATION_BANNED_USERID[x.fed_id].append(int(x.user_id))
|
876 |
-
check = FEDERATION_BANNED_FULL.get(x.fed_id)
|
877 |
-
if check is None:
|
878 |
-
FEDERATION_BANNED_FULL[x.fed_id] = {}
|
879 |
-
FEDERATION_BANNED_FULL[x.fed_id][x.user_id] = {
|
880 |
-
"first_name": x.first_name,
|
881 |
-
"last_name": x.last_name,
|
882 |
-
"user_name": x.user_name,
|
883 |
-
"reason": x.reason,
|
884 |
-
"time": x.time,
|
885 |
-
}
|
886 |
-
finally:
|
887 |
-
SESSION.close()
|
888 |
-
|
889 |
-
|
890 |
-
def __load_all_feds_settings():
|
891 |
-
global FEDERATION_NOTIFICATION
|
892 |
-
try:
|
893 |
-
getuser = SESSION.query(FedsUserSettings).all()
|
894 |
-
for x in getuser:
|
895 |
-
FEDERATION_NOTIFICATION[str(x.user_id)] = x.should_report
|
896 |
-
finally:
|
897 |
-
SESSION.close()
|
898 |
-
|
899 |
-
|
900 |
-
def __load_feds_subscriber():
|
901 |
-
global FEDS_SUBSCRIBER
|
902 |
-
global MYFEDS_SUBSCRIBER
|
903 |
-
try:
|
904 |
-
feds = SESSION.query(FedSubs.fed_id).distinct().all()
|
905 |
-
for (fed_id,) in feds: # remove tuple by ( ,)
|
906 |
-
FEDS_SUBSCRIBER[fed_id] = []
|
907 |
-
MYFEDS_SUBSCRIBER[fed_id] = []
|
908 |
-
|
909 |
-
all_fedsubs = SESSION.query(FedSubs).all()
|
910 |
-
for x in all_fedsubs:
|
911 |
-
FEDS_SUBSCRIBER[x.fed_id] += [x.fed_subs]
|
912 |
-
try:
|
913 |
-
MYFEDS_SUBSCRIBER[x.fed_subs] += [x.fed_id]
|
914 |
-
except KeyError:
|
915 |
-
getsubs = SESSION.query(FedSubs).get((x.fed_id, x.fed_subs))
|
916 |
-
if getsubs:
|
917 |
-
SESSION.delete(getsubs)
|
918 |
-
SESSION.commit()
|
919 |
-
|
920 |
-
FEDS_SUBSCRIBER = {x: set(y) for x, y in FEDS_SUBSCRIBER.items()}
|
921 |
-
MYFEDS_SUBSCRIBER = {x: set(y) for x, y in MYFEDS_SUBSCRIBER.items()}
|
922 |
-
|
923 |
-
finally:
|
924 |
-
SESSION.close()
|
925 |
-
|
926 |
-
|
927 |
-
__load_all_feds()
|
928 |
-
__load_all_feds_chats()
|
929 |
-
__load_all_feds_banned()
|
930 |
-
__load_all_feds_settings()
|
931 |
-
__load_feds_subscriber()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/global_bans_sql.py
DELETED
@@ -1,167 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import BigInteger, Boolean, Column, String, UnicodeText
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
|
7 |
-
|
8 |
-
class GloballyBannedUsers(BASE):
|
9 |
-
__tablename__ = "gbans"
|
10 |
-
user_id = Column(BigInteger, primary_key=True)
|
11 |
-
name = Column(UnicodeText, nullable=False)
|
12 |
-
reason = Column(UnicodeText)
|
13 |
-
|
14 |
-
def __init__(self, user_id, name, reason=None):
|
15 |
-
self.user_id = user_id
|
16 |
-
self.name = name
|
17 |
-
self.reason = reason
|
18 |
-
|
19 |
-
def __repr__(self):
|
20 |
-
return "<GBanned User {} ({})>".format(self.name, self.user_id)
|
21 |
-
|
22 |
-
def to_dict(self):
|
23 |
-
return {"user_id": self.user_id, "name": self.name, "reason": self.reason}
|
24 |
-
|
25 |
-
|
26 |
-
class GbanSettings(BASE):
|
27 |
-
__tablename__ = "gban_settings"
|
28 |
-
chat_id = Column(String(14), primary_key=True)
|
29 |
-
setting = Column(Boolean, default=True, nullable=False)
|
30 |
-
|
31 |
-
def __init__(self, chat_id, enabled):
|
32 |
-
self.chat_id = str(chat_id)
|
33 |
-
self.setting = enabled
|
34 |
-
|
35 |
-
def __repr__(self):
|
36 |
-
return "<Gban setting {} ({})>".format(self.chat_id, self.setting)
|
37 |
-
|
38 |
-
|
39 |
-
GloballyBannedUsers.__table__.create(checkfirst=True)
|
40 |
-
GbanSettings.__table__.create(checkfirst=True)
|
41 |
-
|
42 |
-
GBANNED_USERS_LOCK = threading.RLock()
|
43 |
-
GBAN_SETTING_LOCK = threading.RLock()
|
44 |
-
GBANNED_LIST = set()
|
45 |
-
GBANSTAT_LIST = set()
|
46 |
-
|
47 |
-
|
48 |
-
def gban_user(user_id, name, reason=None):
|
49 |
-
with GBANNED_USERS_LOCK:
|
50 |
-
user = SESSION.query(GloballyBannedUsers).get(user_id)
|
51 |
-
if not user:
|
52 |
-
user = GloballyBannedUsers(user_id, name, reason)
|
53 |
-
else:
|
54 |
-
user.name = name
|
55 |
-
user.reason = reason
|
56 |
-
|
57 |
-
SESSION.merge(user)
|
58 |
-
SESSION.commit()
|
59 |
-
__load_gbanned_userid_list()
|
60 |
-
|
61 |
-
|
62 |
-
def update_gban_reason(user_id, name, reason=None):
|
63 |
-
with GBANNED_USERS_LOCK:
|
64 |
-
user = SESSION.query(GloballyBannedUsers).get(user_id)
|
65 |
-
if not user:
|
66 |
-
return None
|
67 |
-
old_reason = user.reason
|
68 |
-
user.name = name
|
69 |
-
user.reason = reason
|
70 |
-
|
71 |
-
SESSION.merge(user)
|
72 |
-
SESSION.commit()
|
73 |
-
return old_reason
|
74 |
-
|
75 |
-
|
76 |
-
def ungban_user(user_id):
|
77 |
-
with GBANNED_USERS_LOCK:
|
78 |
-
user = SESSION.query(GloballyBannedUsers).get(user_id)
|
79 |
-
if user:
|
80 |
-
SESSION.delete(user)
|
81 |
-
|
82 |
-
SESSION.commit()
|
83 |
-
__load_gbanned_userid_list()
|
84 |
-
|
85 |
-
|
86 |
-
def is_user_gbanned(user_id):
|
87 |
-
return user_id in GBANNED_LIST
|
88 |
-
|
89 |
-
|
90 |
-
def get_gbanned_user(user_id):
|
91 |
-
try:
|
92 |
-
return SESSION.query(GloballyBannedUsers).get(user_id)
|
93 |
-
finally:
|
94 |
-
SESSION.close()
|
95 |
-
|
96 |
-
|
97 |
-
def get_gban_list():
|
98 |
-
try:
|
99 |
-
return [x.to_dict() for x in SESSION.query(GloballyBannedUsers).all()]
|
100 |
-
finally:
|
101 |
-
SESSION.close()
|
102 |
-
|
103 |
-
|
104 |
-
def enable_gbans(chat_id):
|
105 |
-
with GBAN_SETTING_LOCK:
|
106 |
-
chat = SESSION.query(GbanSettings).get(str(chat_id))
|
107 |
-
if not chat:
|
108 |
-
chat = GbanSettings(chat_id, True)
|
109 |
-
|
110 |
-
chat.setting = True
|
111 |
-
SESSION.add(chat)
|
112 |
-
SESSION.commit()
|
113 |
-
if str(chat_id) in GBANSTAT_LIST:
|
114 |
-
GBANSTAT_LIST.remove(str(chat_id))
|
115 |
-
|
116 |
-
|
117 |
-
def disable_gbans(chat_id):
|
118 |
-
with GBAN_SETTING_LOCK:
|
119 |
-
chat = SESSION.query(GbanSettings).get(str(chat_id))
|
120 |
-
if not chat:
|
121 |
-
chat = GbanSettings(chat_id, False)
|
122 |
-
|
123 |
-
chat.setting = False
|
124 |
-
SESSION.add(chat)
|
125 |
-
SESSION.commit()
|
126 |
-
GBANSTAT_LIST.add(str(chat_id))
|
127 |
-
|
128 |
-
|
129 |
-
def does_chat_gban(chat_id):
|
130 |
-
return str(chat_id) not in GBANSTAT_LIST
|
131 |
-
|
132 |
-
|
133 |
-
def num_gbanned_users():
|
134 |
-
return len(GBANNED_LIST)
|
135 |
-
|
136 |
-
|
137 |
-
def __load_gbanned_userid_list():
|
138 |
-
global GBANNED_LIST
|
139 |
-
try:
|
140 |
-
GBANNED_LIST = {x.user_id for x in SESSION.query(GloballyBannedUsers).all()}
|
141 |
-
finally:
|
142 |
-
SESSION.close()
|
143 |
-
|
144 |
-
|
145 |
-
def __load_gban_stat_list():
|
146 |
-
global GBANSTAT_LIST
|
147 |
-
try:
|
148 |
-
GBANSTAT_LIST = {
|
149 |
-
x.chat_id for x in SESSION.query(GbanSettings).all() if not x.setting
|
150 |
-
}
|
151 |
-
finally:
|
152 |
-
SESSION.close()
|
153 |
-
|
154 |
-
|
155 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
156 |
-
with GBAN_SETTING_LOCK:
|
157 |
-
chat = SESSION.query(GbanSettings).get(str(old_chat_id))
|
158 |
-
if chat:
|
159 |
-
chat.chat_id = new_chat_id
|
160 |
-
SESSION.add(chat)
|
161 |
-
|
162 |
-
SESSION.commit()
|
163 |
-
|
164 |
-
|
165 |
-
# Create in memory userid to avoid disk access
|
166 |
-
__load_gbanned_userid_list()
|
167 |
-
__load_gban_stat_list()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/kuki_sql.py
DELETED
@@ -1,42 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import Column, String
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
|
7 |
-
|
8 |
-
class KukiChats(BASE):
|
9 |
-
__tablename__ = "kuki_chats"
|
10 |
-
chat_id = Column(String(14), primary_key=True)
|
11 |
-
|
12 |
-
def __init__(self, chat_id):
|
13 |
-
self.chat_id = chat_id
|
14 |
-
|
15 |
-
|
16 |
-
KukiChats.__table__.create(checkfirst=True)
|
17 |
-
INSERTION_LOCK = threading.RLock()
|
18 |
-
|
19 |
-
|
20 |
-
def is_kuki(chat_id):
|
21 |
-
try:
|
22 |
-
chat = SESSION.query(KukiChats).get(str(chat_id))
|
23 |
-
return bool(chat)
|
24 |
-
finally:
|
25 |
-
SESSION.close()
|
26 |
-
|
27 |
-
|
28 |
-
def set_kuki(chat_id):
|
29 |
-
with INSERTION_LOCK:
|
30 |
-
kukichat = SESSION.query(KukiChats).get(str(chat_id))
|
31 |
-
if not kukichat:
|
32 |
-
kukichat = KukiChats(str(chat_id))
|
33 |
-
SESSION.add(kukichat)
|
34 |
-
SESSION.commit()
|
35 |
-
|
36 |
-
|
37 |
-
def rem_kuki(chat_id):
|
38 |
-
with INSERTION_LOCK:
|
39 |
-
kukichat = SESSION.query(KukiChats).get(str(chat_id))
|
40 |
-
if kukichat:
|
41 |
-
SESSION.delete(kukichat)
|
42 |
-
SESSION.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/log_channel_sql.py
DELETED
@@ -1,156 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
import typing
|
3 |
-
|
4 |
-
from sqlalchemy import BigInteger, Boolean, Column, String, distinct, func
|
5 |
-
|
6 |
-
from Database.sql import BASE, SESSION
|
7 |
-
|
8 |
-
|
9 |
-
class GroupLogs(BASE):
|
10 |
-
__tablename__ = "log_channels"
|
11 |
-
chat_id = Column(String(14), primary_key=True)
|
12 |
-
log_channel = Column(String(14), nullable=False)
|
13 |
-
|
14 |
-
def __init__(self, chat_id, log_channel):
|
15 |
-
self.chat_id = str(chat_id)
|
16 |
-
self.log_channel = str(log_channel)
|
17 |
-
|
18 |
-
|
19 |
-
class LogChannelSettings(BASE):
|
20 |
-
__tablename__ = "log_channel_setting"
|
21 |
-
chat_id = Column(BigInteger, primary_key=True)
|
22 |
-
log_joins = Column(Boolean, default=True)
|
23 |
-
log_leave = Column(Boolean, default=True)
|
24 |
-
log_warn = Column(Boolean, default=True)
|
25 |
-
log_action = Column(Boolean, default=True)
|
26 |
-
# log_media = Column(Boolean)
|
27 |
-
log_report = Column(Boolean, default=True)
|
28 |
-
|
29 |
-
def __init__(
|
30 |
-
self,
|
31 |
-
chat_id: int,
|
32 |
-
log_join: bool,
|
33 |
-
log_leave: bool,
|
34 |
-
log_warn: bool,
|
35 |
-
log_action: bool,
|
36 |
-
log_report: bool,
|
37 |
-
):
|
38 |
-
self.chat_id = chat_id
|
39 |
-
self.log_warn = log_warn
|
40 |
-
self.log_joins = log_join
|
41 |
-
self.log_leave = log_leave
|
42 |
-
self.log_report = log_report
|
43 |
-
self.log_action = log_action
|
44 |
-
|
45 |
-
def toggle_warn(self) -> bool:
|
46 |
-
self.log_warn = not self.log_warn
|
47 |
-
SESSION.commit()
|
48 |
-
return self.log_warn
|
49 |
-
|
50 |
-
def toggle_joins(self) -> bool:
|
51 |
-
self.log_joins = not self.log_joins
|
52 |
-
SESSION.commit()
|
53 |
-
return self.log_joins
|
54 |
-
|
55 |
-
def toggle_leave(self) -> bool:
|
56 |
-
self.log_leave = not self.log_leave
|
57 |
-
SESSION.commit()
|
58 |
-
return self.log_leave
|
59 |
-
|
60 |
-
def toggle_report(self) -> bool:
|
61 |
-
self.log_report = not self.log_report
|
62 |
-
SESSION.commit()
|
63 |
-
return self.log_report
|
64 |
-
|
65 |
-
def toggle_action(self) -> bool:
|
66 |
-
self.log_action = not self.log_action
|
67 |
-
SESSION.commit()
|
68 |
-
return self.log_action
|
69 |
-
|
70 |
-
|
71 |
-
GroupLogs.__table__.create(checkfirst=True)
|
72 |
-
LogChannelSettings.__table__.create(checkfirst=True)
|
73 |
-
|
74 |
-
LOGS_INSERTION_LOCK = threading.RLock()
|
75 |
-
LOG_SETTING_LOCK = threading.RLock()
|
76 |
-
CHANNELS = {}
|
77 |
-
|
78 |
-
|
79 |
-
def get_chat_setting(chat_id: int) -> typing.Optional[LogChannelSettings]:
|
80 |
-
with LOG_SETTING_LOCK:
|
81 |
-
return SESSION.query(LogChannelSettings).get(chat_id)
|
82 |
-
|
83 |
-
|
84 |
-
def set_chat_setting(setting: LogChannelSettings):
|
85 |
-
with LOGS_INSERTION_LOCK:
|
86 |
-
res: LogChannelSettings = SESSION.query(LogChannelSettings).get(setting.chat_id)
|
87 |
-
if res:
|
88 |
-
res.log_warn = setting.log_warn
|
89 |
-
res.log_action = setting.log_action
|
90 |
-
res.log_report = setting.log_report
|
91 |
-
res.log_joins = setting.log_joins
|
92 |
-
res.log_leave = setting.log_leave
|
93 |
-
else:
|
94 |
-
SESSION.add(setting)
|
95 |
-
SESSION.commit()
|
96 |
-
|
97 |
-
|
98 |
-
def set_chat_log_channel(chat_id, log_channel):
|
99 |
-
with LOGS_INSERTION_LOCK:
|
100 |
-
res = SESSION.query(GroupLogs).get(str(chat_id))
|
101 |
-
if res:
|
102 |
-
res.log_channel = log_channel
|
103 |
-
else:
|
104 |
-
res = GroupLogs(chat_id, log_channel)
|
105 |
-
SESSION.add(res)
|
106 |
-
|
107 |
-
CHANNELS[str(chat_id)] = log_channel
|
108 |
-
SESSION.commit()
|
109 |
-
|
110 |
-
|
111 |
-
def get_chat_log_channel(chat_id):
|
112 |
-
return CHANNELS.get(str(chat_id))
|
113 |
-
|
114 |
-
|
115 |
-
def stop_chat_logging(chat_id):
|
116 |
-
with LOGS_INSERTION_LOCK:
|
117 |
-
res = SESSION.query(GroupLogs).get(str(chat_id))
|
118 |
-
if res:
|
119 |
-
if str(chat_id) in CHANNELS:
|
120 |
-
del CHANNELS[str(chat_id)]
|
121 |
-
|
122 |
-
log_channel = res.log_channel
|
123 |
-
SESSION.delete(res)
|
124 |
-
SESSION.commit()
|
125 |
-
return log_channel
|
126 |
-
|
127 |
-
|
128 |
-
def num_logchannels():
|
129 |
-
try:
|
130 |
-
return SESSION.query(func.count(distinct(GroupLogs.chat_id))).scalar()
|
131 |
-
finally:
|
132 |
-
SESSION.close()
|
133 |
-
|
134 |
-
|
135 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
136 |
-
with LOGS_INSERTION_LOCK:
|
137 |
-
chat = SESSION.query(GroupLogs).get(str(old_chat_id))
|
138 |
-
if chat:
|
139 |
-
chat.chat_id = str(new_chat_id)
|
140 |
-
SESSION.add(chat)
|
141 |
-
if str(old_chat_id) in CHANNELS:
|
142 |
-
CHANNELS[str(new_chat_id)] = CHANNELS.get(str(old_chat_id))
|
143 |
-
|
144 |
-
SESSION.commit()
|
145 |
-
|
146 |
-
|
147 |
-
def __load_log_channels():
|
148 |
-
global CHANNELS
|
149 |
-
try:
|
150 |
-
all_chats = SESSION.query(GroupLogs).all()
|
151 |
-
CHANNELS = {chat.chat_id: chat.log_channel for chat in all_chats}
|
152 |
-
finally:
|
153 |
-
SESSION.close()
|
154 |
-
|
155 |
-
|
156 |
-
__load_log_channels()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/notes_sql.py
DELETED
@@ -1,187 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import Boolean, Column, Integer, String, UnicodeText, distinct, func
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
from Mikobot.plugins.helper_funcs.msg_types import Types
|
7 |
-
|
8 |
-
|
9 |
-
class Notes(BASE):
|
10 |
-
__tablename__ = "notes"
|
11 |
-
chat_id = Column(String(14), primary_key=True)
|
12 |
-
name = Column(UnicodeText, primary_key=True)
|
13 |
-
value = Column(UnicodeText, nullable=False)
|
14 |
-
file = Column(UnicodeText)
|
15 |
-
is_reply = Column(Boolean, default=False)
|
16 |
-
has_buttons = Column(Boolean, default=False)
|
17 |
-
msgtype = Column(Integer, default=Types.BUTTON_TEXT.value)
|
18 |
-
|
19 |
-
def __init__(self, chat_id, name, value, msgtype, file=None):
|
20 |
-
self.chat_id = str(chat_id) # ensure string
|
21 |
-
self.name = name
|
22 |
-
self.value = value
|
23 |
-
self.msgtype = msgtype
|
24 |
-
self.file = file
|
25 |
-
|
26 |
-
def __repr__(self):
|
27 |
-
return "<Note %s>" % self.name
|
28 |
-
|
29 |
-
|
30 |
-
class Buttons(BASE):
|
31 |
-
__tablename__ = "note_urls"
|
32 |
-
id = Column(Integer, primary_key=True, autoincrement=True)
|
33 |
-
chat_id = Column(String(14), primary_key=True)
|
34 |
-
note_name = Column(UnicodeText, primary_key=True)
|
35 |
-
name = Column(UnicodeText, nullable=False)
|
36 |
-
url = Column(UnicodeText, nullable=False)
|
37 |
-
same_line = Column(Boolean, default=False)
|
38 |
-
|
39 |
-
def __init__(self, chat_id, note_name, name, url, same_line=False):
|
40 |
-
self.chat_id = str(chat_id)
|
41 |
-
self.note_name = note_name
|
42 |
-
self.name = name
|
43 |
-
self.url = url
|
44 |
-
self.same_line = same_line
|
45 |
-
|
46 |
-
|
47 |
-
Notes.__table__.create(checkfirst=True)
|
48 |
-
Buttons.__table__.create(checkfirst=True)
|
49 |
-
|
50 |
-
NOTES_INSERTION_LOCK = threading.RLock()
|
51 |
-
BUTTONS_INSERTION_LOCK = threading.RLock()
|
52 |
-
|
53 |
-
|
54 |
-
def add_note_to_db(chat_id, note_name, note_data, msgtype, buttons=None, file=None):
|
55 |
-
if not buttons:
|
56 |
-
buttons = []
|
57 |
-
|
58 |
-
with NOTES_INSERTION_LOCK:
|
59 |
-
prev = SESSION.query(Notes).get((str(chat_id), note_name))
|
60 |
-
if prev:
|
61 |
-
with BUTTONS_INSERTION_LOCK:
|
62 |
-
prev_buttons = (
|
63 |
-
SESSION.query(Buttons)
|
64 |
-
.filter(
|
65 |
-
Buttons.chat_id == str(chat_id),
|
66 |
-
Buttons.note_name == note_name,
|
67 |
-
)
|
68 |
-
.all()
|
69 |
-
)
|
70 |
-
for btn in prev_buttons:
|
71 |
-
SESSION.delete(btn)
|
72 |
-
SESSION.delete(prev)
|
73 |
-
note = Notes(
|
74 |
-
str(chat_id),
|
75 |
-
note_name,
|
76 |
-
note_data or "",
|
77 |
-
msgtype=msgtype.value,
|
78 |
-
file=file,
|
79 |
-
)
|
80 |
-
SESSION.add(note)
|
81 |
-
SESSION.commit()
|
82 |
-
|
83 |
-
for b_name, url, same_line in buttons:
|
84 |
-
add_note_button_to_db(chat_id, note_name, b_name, url, same_line)
|
85 |
-
|
86 |
-
|
87 |
-
def get_note(chat_id, note_name):
|
88 |
-
try:
|
89 |
-
return (
|
90 |
-
SESSION.query(Notes)
|
91 |
-
.filter(func.lower(Notes.name) == note_name, Notes.chat_id == str(chat_id))
|
92 |
-
.first()
|
93 |
-
)
|
94 |
-
finally:
|
95 |
-
SESSION.close()
|
96 |
-
|
97 |
-
|
98 |
-
def rm_note(chat_id, note_name):
|
99 |
-
with NOTES_INSERTION_LOCK:
|
100 |
-
note = (
|
101 |
-
SESSION.query(Notes)
|
102 |
-
.filter(func.lower(Notes.name) == note_name, Notes.chat_id == str(chat_id))
|
103 |
-
.first()
|
104 |
-
)
|
105 |
-
if note:
|
106 |
-
with BUTTONS_INSERTION_LOCK:
|
107 |
-
buttons = (
|
108 |
-
SESSION.query(Buttons)
|
109 |
-
.filter(
|
110 |
-
Buttons.chat_id == str(chat_id),
|
111 |
-
Buttons.note_name == note_name,
|
112 |
-
)
|
113 |
-
.all()
|
114 |
-
)
|
115 |
-
for btn in buttons:
|
116 |
-
SESSION.delete(btn)
|
117 |
-
|
118 |
-
SESSION.delete(note)
|
119 |
-
SESSION.commit()
|
120 |
-
return True
|
121 |
-
|
122 |
-
else:
|
123 |
-
SESSION.close()
|
124 |
-
return False
|
125 |
-
|
126 |
-
|
127 |
-
def get_all_chat_notes(chat_id):
|
128 |
-
try:
|
129 |
-
return (
|
130 |
-
SESSION.query(Notes)
|
131 |
-
.filter(Notes.chat_id == str(chat_id))
|
132 |
-
.order_by(Notes.name.asc())
|
133 |
-
.all()
|
134 |
-
)
|
135 |
-
finally:
|
136 |
-
SESSION.close()
|
137 |
-
|
138 |
-
|
139 |
-
def add_note_button_to_db(chat_id, note_name, b_name, url, same_line):
|
140 |
-
with BUTTONS_INSERTION_LOCK:
|
141 |
-
button = Buttons(chat_id, note_name, b_name, url, same_line)
|
142 |
-
SESSION.add(button)
|
143 |
-
SESSION.commit()
|
144 |
-
|
145 |
-
|
146 |
-
def get_buttons(chat_id, note_name):
|
147 |
-
try:
|
148 |
-
return (
|
149 |
-
SESSION.query(Buttons)
|
150 |
-
.filter(Buttons.chat_id == str(chat_id), Buttons.note_name == note_name)
|
151 |
-
.order_by(Buttons.id)
|
152 |
-
.all()
|
153 |
-
)
|
154 |
-
finally:
|
155 |
-
SESSION.close()
|
156 |
-
|
157 |
-
|
158 |
-
def num_notes():
|
159 |
-
try:
|
160 |
-
return SESSION.query(Notes).count()
|
161 |
-
finally:
|
162 |
-
SESSION.close()
|
163 |
-
|
164 |
-
|
165 |
-
def num_chats():
|
166 |
-
try:
|
167 |
-
return SESSION.query(func.count(distinct(Notes.chat_id))).scalar()
|
168 |
-
finally:
|
169 |
-
SESSION.close()
|
170 |
-
|
171 |
-
|
172 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
173 |
-
with NOTES_INSERTION_LOCK:
|
174 |
-
chat_notes = (
|
175 |
-
SESSION.query(Notes).filter(Notes.chat_id == str(old_chat_id)).all()
|
176 |
-
)
|
177 |
-
for note in chat_notes:
|
178 |
-
note.chat_id = str(new_chat_id)
|
179 |
-
|
180 |
-
with BUTTONS_INSERTION_LOCK:
|
181 |
-
chat_buttons = (
|
182 |
-
SESSION.query(Buttons).filter(Buttons.chat_id == str(old_chat_id)).all()
|
183 |
-
)
|
184 |
-
for btn in chat_buttons:
|
185 |
-
btn.chat_id = str(new_chat_id)
|
186 |
-
|
187 |
-
SESSION.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/rules_sql.py
DELETED
@@ -1,58 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import Column, String, UnicodeText, distinct, func
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
|
7 |
-
|
8 |
-
class Rules(BASE):
|
9 |
-
__tablename__ = "rules"
|
10 |
-
chat_id = Column(String(14), primary_key=True)
|
11 |
-
rules = Column(UnicodeText, default="")
|
12 |
-
|
13 |
-
def __init__(self, chat_id):
|
14 |
-
self.chat_id = chat_id
|
15 |
-
|
16 |
-
def __repr__(self):
|
17 |
-
return "<Chat {} rules: {}>".format(self.chat_id, self.rules)
|
18 |
-
|
19 |
-
|
20 |
-
Rules.__table__.create(checkfirst=True)
|
21 |
-
|
22 |
-
INSERTION_LOCK = threading.RLock()
|
23 |
-
|
24 |
-
|
25 |
-
def set_rules(chat_id, rules_text):
|
26 |
-
with INSERTION_LOCK:
|
27 |
-
rules = SESSION.query(Rules).get(str(chat_id))
|
28 |
-
if not rules:
|
29 |
-
rules = Rules(str(chat_id))
|
30 |
-
rules.rules = rules_text
|
31 |
-
|
32 |
-
SESSION.add(rules)
|
33 |
-
SESSION.commit()
|
34 |
-
|
35 |
-
|
36 |
-
def get_rules(chat_id):
|
37 |
-
rules = SESSION.query(Rules).get(str(chat_id))
|
38 |
-
ret = ""
|
39 |
-
if rules:
|
40 |
-
ret = rules.rules
|
41 |
-
|
42 |
-
SESSION.close()
|
43 |
-
return ret
|
44 |
-
|
45 |
-
|
46 |
-
def num_chats():
|
47 |
-
try:
|
48 |
-
return SESSION.query(func.count(distinct(Rules.chat_id))).scalar()
|
49 |
-
finally:
|
50 |
-
SESSION.close()
|
51 |
-
|
52 |
-
|
53 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
54 |
-
with INSERTION_LOCK:
|
55 |
-
chat = SESSION.query(Rules).get(str(old_chat_id))
|
56 |
-
if chat:
|
57 |
-
chat.chat_id = str(new_chat_id)
|
58 |
-
SESSION.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/userinfo_sql.py
DELETED
@@ -1,76 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import BigInteger, Column, UnicodeText
|
4 |
-
|
5 |
-
from Database.sql import BASE, SESSION
|
6 |
-
|
7 |
-
|
8 |
-
class UserInfo(BASE):
|
9 |
-
__tablename__ = "userinfo"
|
10 |
-
user_id = Column(BigInteger, primary_key=True)
|
11 |
-
info = Column(UnicodeText)
|
12 |
-
|
13 |
-
def __init__(self, user_id, info):
|
14 |
-
self.user_id = user_id
|
15 |
-
self.info = info
|
16 |
-
|
17 |
-
def __repr__(self):
|
18 |
-
return "<User info %d>" % self.user_id
|
19 |
-
|
20 |
-
|
21 |
-
class UserBio(BASE):
|
22 |
-
__tablename__ = "userbio"
|
23 |
-
user_id = Column(BigInteger, primary_key=True)
|
24 |
-
bio = Column(UnicodeText)
|
25 |
-
|
26 |
-
def __init__(self, user_id, bio):
|
27 |
-
self.user_id = user_id
|
28 |
-
self.bio = bio
|
29 |
-
|
30 |
-
def __repr__(self):
|
31 |
-
return "<User info %d>" % self.user_id
|
32 |
-
|
33 |
-
|
34 |
-
UserInfo.__table__.create(checkfirst=True)
|
35 |
-
UserBio.__table__.create(checkfirst=True)
|
36 |
-
|
37 |
-
INSERTION_LOCK = threading.RLock()
|
38 |
-
|
39 |
-
|
40 |
-
def get_user_me_info(user_id):
|
41 |
-
userinfo = SESSION.query(UserInfo).get(user_id)
|
42 |
-
SESSION.close()
|
43 |
-
if userinfo:
|
44 |
-
return userinfo.info
|
45 |
-
return None
|
46 |
-
|
47 |
-
|
48 |
-
def set_user_me_info(user_id, info):
|
49 |
-
with INSERTION_LOCK:
|
50 |
-
userinfo = SESSION.query(UserInfo).get(user_id)
|
51 |
-
if userinfo:
|
52 |
-
userinfo.info = info
|
53 |
-
else:
|
54 |
-
userinfo = UserInfo(user_id, info)
|
55 |
-
SESSION.add(userinfo)
|
56 |
-
SESSION.commit()
|
57 |
-
|
58 |
-
|
59 |
-
def get_user_bio(user_id):
|
60 |
-
userbio = SESSION.query(UserBio).get(user_id)
|
61 |
-
SESSION.close()
|
62 |
-
if userbio:
|
63 |
-
return userbio.bio
|
64 |
-
return None
|
65 |
-
|
66 |
-
|
67 |
-
def set_user_bio(user_id, bio):
|
68 |
-
with INSERTION_LOCK:
|
69 |
-
userbio = SESSION.query(UserBio).get(user_id)
|
70 |
-
if userbio:
|
71 |
-
userbio.bio = bio
|
72 |
-
else:
|
73 |
-
userbio = UserBio(user_id, bio)
|
74 |
-
|
75 |
-
SESSION.add(userbio)
|
76 |
-
SESSION.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/users_sql.py
DELETED
@@ -1,235 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import (
|
4 |
-
BigInteger,
|
5 |
-
Column,
|
6 |
-
ForeignKey,
|
7 |
-
Integer,
|
8 |
-
String,
|
9 |
-
UnicodeText,
|
10 |
-
UniqueConstraint,
|
11 |
-
func,
|
12 |
-
)
|
13 |
-
|
14 |
-
from Database.sql import BASE, SESSION
|
15 |
-
from Mikobot import dispatcher
|
16 |
-
|
17 |
-
|
18 |
-
class Users(BASE):
|
19 |
-
__tablename__ = "users"
|
20 |
-
user_id = Column(BigInteger, primary_key=True)
|
21 |
-
username = Column(UnicodeText)
|
22 |
-
|
23 |
-
def __init__(self, user_id, username=None):
|
24 |
-
self.user_id = user_id
|
25 |
-
self.username = username
|
26 |
-
|
27 |
-
def __repr__(self):
|
28 |
-
return "<User {} ({})>".format(self.username, self.user_id)
|
29 |
-
|
30 |
-
|
31 |
-
class Chats(BASE):
|
32 |
-
__tablename__ = "chats"
|
33 |
-
chat_id = Column(String(14), primary_key=True)
|
34 |
-
chat_name = Column(UnicodeText, nullable=False)
|
35 |
-
|
36 |
-
def __init__(self, chat_id, chat_name):
|
37 |
-
self.chat_id = str(chat_id)
|
38 |
-
self.chat_name = chat_name
|
39 |
-
|
40 |
-
def __repr__(self):
|
41 |
-
return "<Chat {} ({})>".format(self.chat_name, self.chat_id)
|
42 |
-
|
43 |
-
|
44 |
-
class ChatMembers(BASE):
|
45 |
-
__tablename__ = "chat_members"
|
46 |
-
priv_chat_id = Column(Integer, primary_key=True)
|
47 |
-
# NOTE: Use dual primary key instead of private primary key?
|
48 |
-
chat = Column(
|
49 |
-
String(14),
|
50 |
-
ForeignKey("chats.chat_id", onupdate="CASCADE", ondelete="CASCADE"),
|
51 |
-
nullable=False,
|
52 |
-
)
|
53 |
-
user = Column(
|
54 |
-
BigInteger,
|
55 |
-
ForeignKey("users.user_id", onupdate="CASCADE", ondelete="CASCADE"),
|
56 |
-
nullable=False,
|
57 |
-
)
|
58 |
-
__table_args__ = (UniqueConstraint("chat", "user", name="_chat_members_uc"),)
|
59 |
-
|
60 |
-
def __init__(self, chat, user):
|
61 |
-
self.chat = chat
|
62 |
-
self.user = user
|
63 |
-
|
64 |
-
def __repr__(self):
|
65 |
-
return "<Chat user {} ({}) in chat {} ({})>".format(
|
66 |
-
self.user.username,
|
67 |
-
self.user.user_id,
|
68 |
-
self.chat.chat_name,
|
69 |
-
self.chat.chat_id,
|
70 |
-
)
|
71 |
-
|
72 |
-
|
73 |
-
Users.__table__.create(checkfirst=True)
|
74 |
-
Chats.__table__.create(checkfirst=True)
|
75 |
-
ChatMembers.__table__.create(checkfirst=True)
|
76 |
-
|
77 |
-
INSERTION_LOCK = threading.RLock()
|
78 |
-
|
79 |
-
|
80 |
-
def ensure_bot_in_db():
|
81 |
-
with INSERTION_LOCK:
|
82 |
-
bot = Users(dispatcher.bot.id, dispatcher.bot.username)
|
83 |
-
SESSION.merge(bot)
|
84 |
-
SESSION.commit()
|
85 |
-
|
86 |
-
|
87 |
-
def update_user(user_id, username, chat_id=None, chat_name=None):
|
88 |
-
with INSERTION_LOCK:
|
89 |
-
user = SESSION.query(Users).get(user_id)
|
90 |
-
if not user:
|
91 |
-
user = Users(user_id, username)
|
92 |
-
SESSION.add(user)
|
93 |
-
SESSION.flush()
|
94 |
-
else:
|
95 |
-
user.username = username
|
96 |
-
|
97 |
-
if not chat_id or not chat_name:
|
98 |
-
SESSION.commit()
|
99 |
-
return
|
100 |
-
|
101 |
-
chat = SESSION.query(Chats).get(str(chat_id))
|
102 |
-
if not chat:
|
103 |
-
chat = Chats(str(chat_id), chat_name)
|
104 |
-
SESSION.add(chat)
|
105 |
-
SESSION.flush()
|
106 |
-
|
107 |
-
else:
|
108 |
-
chat.chat_name = chat_name
|
109 |
-
|
110 |
-
member = (
|
111 |
-
SESSION.query(ChatMembers)
|
112 |
-
.filter(ChatMembers.chat == chat.chat_id, ChatMembers.user == user.user_id)
|
113 |
-
.first()
|
114 |
-
)
|
115 |
-
if not member:
|
116 |
-
chat_member = ChatMembers(chat.chat_id, user.user_id)
|
117 |
-
SESSION.add(chat_member)
|
118 |
-
|
119 |
-
SESSION.commit()
|
120 |
-
|
121 |
-
|
122 |
-
def get_userid_by_name(username):
|
123 |
-
try:
|
124 |
-
return (
|
125 |
-
SESSION.query(Users)
|
126 |
-
.filter(func.lower(Users.username) == username.lower())
|
127 |
-
.all()
|
128 |
-
)
|
129 |
-
finally:
|
130 |
-
SESSION.close()
|
131 |
-
|
132 |
-
|
133 |
-
def get_name_by_userid(user_id):
|
134 |
-
try:
|
135 |
-
return SESSION.query(Users).get(Users.user_id == int(user_id)).first()
|
136 |
-
finally:
|
137 |
-
SESSION.close()
|
138 |
-
|
139 |
-
|
140 |
-
def get_chat_members(chat_id):
|
141 |
-
try:
|
142 |
-
return SESSION.query(ChatMembers).filter(ChatMembers.chat == str(chat_id)).all()
|
143 |
-
finally:
|
144 |
-
SESSION.close()
|
145 |
-
|
146 |
-
|
147 |
-
def get_all_chats():
|
148 |
-
try:
|
149 |
-
return SESSION.query(Chats).all()
|
150 |
-
finally:
|
151 |
-
SESSION.close()
|
152 |
-
|
153 |
-
|
154 |
-
def get_all_users():
|
155 |
-
try:
|
156 |
-
return SESSION.query(Users).all()
|
157 |
-
finally:
|
158 |
-
SESSION.close()
|
159 |
-
|
160 |
-
|
161 |
-
def get_user_num_chats(user_id):
|
162 |
-
try:
|
163 |
-
return (
|
164 |
-
SESSION.query(ChatMembers).filter(ChatMembers.user == int(user_id)).count()
|
165 |
-
)
|
166 |
-
finally:
|
167 |
-
SESSION.close()
|
168 |
-
|
169 |
-
|
170 |
-
def get_user_com_chats(user_id):
|
171 |
-
try:
|
172 |
-
chat_members = (
|
173 |
-
SESSION.query(ChatMembers).filter(ChatMembers.user == int(user_id)).all()
|
174 |
-
)
|
175 |
-
return [i.chat for i in chat_members]
|
176 |
-
finally:
|
177 |
-
SESSION.close()
|
178 |
-
|
179 |
-
|
180 |
-
def num_chats():
|
181 |
-
try:
|
182 |
-
return SESSION.query(Chats).count()
|
183 |
-
finally:
|
184 |
-
SESSION.close()
|
185 |
-
|
186 |
-
|
187 |
-
def num_users():
|
188 |
-
try:
|
189 |
-
return SESSION.query(Users).count()
|
190 |
-
finally:
|
191 |
-
SESSION.close()
|
192 |
-
|
193 |
-
|
194 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
195 |
-
with INSERTION_LOCK:
|
196 |
-
chat = SESSION.query(Chats).get(str(old_chat_id))
|
197 |
-
if chat:
|
198 |
-
chat.chat_id = str(new_chat_id)
|
199 |
-
SESSION.commit()
|
200 |
-
|
201 |
-
chat_members = (
|
202 |
-
SESSION.query(ChatMembers)
|
203 |
-
.filter(ChatMembers.chat == str(old_chat_id))
|
204 |
-
.all()
|
205 |
-
)
|
206 |
-
for member in chat_members:
|
207 |
-
member.chat = str(new_chat_id)
|
208 |
-
SESSION.commit()
|
209 |
-
|
210 |
-
|
211 |
-
ensure_bot_in_db()
|
212 |
-
|
213 |
-
|
214 |
-
def del_user(user_id):
|
215 |
-
with INSERTION_LOCK:
|
216 |
-
curr = SESSION.query(Users).get(user_id)
|
217 |
-
if curr:
|
218 |
-
SESSION.delete(curr)
|
219 |
-
SESSION.commit()
|
220 |
-
return True
|
221 |
-
|
222 |
-
ChatMembers.query.filter(ChatMembers.user == user_id).delete()
|
223 |
-
SESSION.commit()
|
224 |
-
SESSION.close()
|
225 |
-
return False
|
226 |
-
|
227 |
-
|
228 |
-
def rem_chat(chat_id):
|
229 |
-
with INSERTION_LOCK:
|
230 |
-
chat = SESSION.query(Chats).get(str(chat_id))
|
231 |
-
if chat:
|
232 |
-
SESSION.delete(chat)
|
233 |
-
SESSION.commit()
|
234 |
-
else:
|
235 |
-
SESSION.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/warns_sql.py
DELETED
@@ -1,327 +0,0 @@
|
|
1 |
-
import threading
|
2 |
-
|
3 |
-
from sqlalchemy import (
|
4 |
-
BigInteger,
|
5 |
-
Boolean,
|
6 |
-
Column,
|
7 |
-
Integer,
|
8 |
-
String,
|
9 |
-
UnicodeText,
|
10 |
-
distinct,
|
11 |
-
func,
|
12 |
-
)
|
13 |
-
from sqlalchemy.dialects import postgresql
|
14 |
-
|
15 |
-
from Database.sql import BASE, SESSION
|
16 |
-
|
17 |
-
|
18 |
-
class Warns(BASE):
|
19 |
-
__tablename__ = "warns"
|
20 |
-
|
21 |
-
user_id = Column(BigInteger, primary_key=True)
|
22 |
-
chat_id = Column(String(14), primary_key=True)
|
23 |
-
num_warns = Column(Integer, default=0)
|
24 |
-
reasons = Column(postgresql.ARRAY(UnicodeText))
|
25 |
-
|
26 |
-
def __init__(self, user_id, chat_id):
|
27 |
-
self.user_id = user_id
|
28 |
-
self.chat_id = str(chat_id)
|
29 |
-
self.num_warns = 0
|
30 |
-
self.reasons = []
|
31 |
-
|
32 |
-
def __repr__(self):
|
33 |
-
return "<{} warns for {} in {} for reasons {}>".format(
|
34 |
-
self.num_warns,
|
35 |
-
self.user_id,
|
36 |
-
self.chat_id,
|
37 |
-
self.reasons,
|
38 |
-
)
|
39 |
-
|
40 |
-
|
41 |
-
class WarnFilters(BASE):
|
42 |
-
__tablename__ = "warn_filters"
|
43 |
-
chat_id = Column(String(14), primary_key=True)
|
44 |
-
keyword = Column(UnicodeText, primary_key=True, nullable=False)
|
45 |
-
reply = Column(UnicodeText, nullable=False)
|
46 |
-
|
47 |
-
def __init__(self, chat_id, keyword, reply):
|
48 |
-
self.chat_id = str(chat_id) # ensure string
|
49 |
-
self.keyword = keyword
|
50 |
-
self.reply = reply
|
51 |
-
|
52 |
-
def __repr__(self):
|
53 |
-
return "<Permissions for %s>" % self.chat_id
|
54 |
-
|
55 |
-
def __eq__(self, other):
|
56 |
-
return bool(
|
57 |
-
isinstance(other, WarnFilters)
|
58 |
-
and self.chat_id == other.chat_id
|
59 |
-
and self.keyword == other.keyword,
|
60 |
-
)
|
61 |
-
|
62 |
-
|
63 |
-
class WarnSettings(BASE):
|
64 |
-
__tablename__ = "warn_settings"
|
65 |
-
chat_id = Column(String(14), primary_key=True)
|
66 |
-
warn_limit = Column(Integer, default=3)
|
67 |
-
soft_warn = Column(Boolean, default=False)
|
68 |
-
|
69 |
-
def __init__(self, chat_id, warn_limit=3, soft_warn=False):
|
70 |
-
self.chat_id = str(chat_id)
|
71 |
-
self.warn_limit = warn_limit
|
72 |
-
self.soft_warn = soft_warn
|
73 |
-
|
74 |
-
def __repr__(self):
|
75 |
-
return "<{} has {} possible warns.>".format(self.chat_id, self.warn_limit)
|
76 |
-
|
77 |
-
|
78 |
-
Warns.__table__.create(checkfirst=True)
|
79 |
-
WarnFilters.__table__.create(checkfirst=True)
|
80 |
-
WarnSettings.__table__.create(checkfirst=True)
|
81 |
-
|
82 |
-
WARN_INSERTION_LOCK = threading.RLock()
|
83 |
-
WARN_FILTER_INSERTION_LOCK = threading.RLock()
|
84 |
-
WARN_SETTINGS_LOCK = threading.RLock()
|
85 |
-
|
86 |
-
WARN_FILTERS = {}
|
87 |
-
|
88 |
-
|
89 |
-
def warn_user(user_id, chat_id, reason=None):
|
90 |
-
with WARN_INSERTION_LOCK:
|
91 |
-
warned_user = SESSION.query(Warns).get((user_id, str(chat_id)))
|
92 |
-
if not warned_user:
|
93 |
-
warned_user = Warns(user_id, str(chat_id))
|
94 |
-
|
95 |
-
warned_user.num_warns += 1
|
96 |
-
if reason:
|
97 |
-
warned_user.reasons = warned_user.reasons + [
|
98 |
-
reason,
|
99 |
-
] # TODO:: double check this wizardry
|
100 |
-
|
101 |
-
reasons = warned_user.reasons
|
102 |
-
num = warned_user.num_warns
|
103 |
-
|
104 |
-
SESSION.add(warned_user)
|
105 |
-
SESSION.commit()
|
106 |
-
|
107 |
-
return num, reasons
|
108 |
-
|
109 |
-
|
110 |
-
def remove_warn(user_id, chat_id):
|
111 |
-
with WARN_INSERTION_LOCK:
|
112 |
-
removed = False
|
113 |
-
warned_user = SESSION.query(Warns).get((user_id, str(chat_id)))
|
114 |
-
|
115 |
-
if warned_user and warned_user.num_warns > 0:
|
116 |
-
warned_user.num_warns -= 1
|
117 |
-
warned_user.reasons = warned_user.reasons[:-1]
|
118 |
-
SESSION.add(warned_user)
|
119 |
-
SESSION.commit()
|
120 |
-
removed = True
|
121 |
-
|
122 |
-
SESSION.close()
|
123 |
-
return removed
|
124 |
-
|
125 |
-
|
126 |
-
def reset_warns(user_id, chat_id):
|
127 |
-
with WARN_INSERTION_LOCK:
|
128 |
-
warned_user = SESSION.query(Warns).get((user_id, str(chat_id)))
|
129 |
-
if warned_user:
|
130 |
-
warned_user.num_warns = 0
|
131 |
-
warned_user.reasons = []
|
132 |
-
|
133 |
-
SESSION.add(warned_user)
|
134 |
-
SESSION.commit()
|
135 |
-
SESSION.close()
|
136 |
-
|
137 |
-
|
138 |
-
def get_warns(user_id, chat_id):
|
139 |
-
try:
|
140 |
-
user = SESSION.query(Warns).get((user_id, str(chat_id)))
|
141 |
-
if not user:
|
142 |
-
return None
|
143 |
-
reasons = user.reasons
|
144 |
-
num = user.num_warns
|
145 |
-
return num, reasons
|
146 |
-
finally:
|
147 |
-
SESSION.close()
|
148 |
-
|
149 |
-
|
150 |
-
def add_warn_filter(chat_id, keyword, reply):
|
151 |
-
with WARN_FILTER_INSERTION_LOCK:
|
152 |
-
warn_filt = WarnFilters(str(chat_id), keyword, reply)
|
153 |
-
|
154 |
-
if keyword not in WARN_FILTERS.get(str(chat_id), []):
|
155 |
-
WARN_FILTERS[str(chat_id)] = sorted(
|
156 |
-
WARN_FILTERS.get(str(chat_id), []) + [keyword],
|
157 |
-
key=lambda x: (-len(x), x),
|
158 |
-
)
|
159 |
-
|
160 |
-
SESSION.merge(warn_filt) # merge to avoid duplicate key issues
|
161 |
-
SESSION.commit()
|
162 |
-
|
163 |
-
|
164 |
-
def remove_warn_filter(chat_id, keyword):
|
165 |
-
with WARN_FILTER_INSERTION_LOCK:
|
166 |
-
warn_filt = SESSION.query(WarnFilters).get((str(chat_id), keyword))
|
167 |
-
if warn_filt:
|
168 |
-
if keyword in WARN_FILTERS.get(str(chat_id), []): # sanity check
|
169 |
-
WARN_FILTERS.get(str(chat_id), []).remove(keyword)
|
170 |
-
|
171 |
-
SESSION.delete(warn_filt)
|
172 |
-
SESSION.commit()
|
173 |
-
return True
|
174 |
-
SESSION.close()
|
175 |
-
return False
|
176 |
-
|
177 |
-
|
178 |
-
def get_chat_warn_triggers(chat_id):
|
179 |
-
return WARN_FILTERS.get(str(chat_id), set())
|
180 |
-
|
181 |
-
|
182 |
-
def get_chat_warn_filters(chat_id):
|
183 |
-
try:
|
184 |
-
return (
|
185 |
-
SESSION.query(WarnFilters).filter(WarnFilters.chat_id == str(chat_id)).all()
|
186 |
-
)
|
187 |
-
finally:
|
188 |
-
SESSION.close()
|
189 |
-
|
190 |
-
|
191 |
-
def get_warn_filter(chat_id, keyword):
|
192 |
-
try:
|
193 |
-
return SESSION.query(WarnFilters).get((str(chat_id), keyword))
|
194 |
-
finally:
|
195 |
-
SESSION.close()
|
196 |
-
|
197 |
-
|
198 |
-
def set_warn_limit(chat_id, warn_limit):
|
199 |
-
with WARN_SETTINGS_LOCK:
|
200 |
-
curr_setting = SESSION.query(WarnSettings).get(str(chat_id))
|
201 |
-
if not curr_setting:
|
202 |
-
curr_setting = WarnSettings(chat_id, warn_limit=warn_limit)
|
203 |
-
|
204 |
-
curr_setting.warn_limit = warn_limit
|
205 |
-
|
206 |
-
SESSION.add(curr_setting)
|
207 |
-
SESSION.commit()
|
208 |
-
|
209 |
-
|
210 |
-
def set_warn_strength(chat_id, soft_warn):
|
211 |
-
with WARN_SETTINGS_LOCK:
|
212 |
-
curr_setting = SESSION.query(WarnSettings).get(str(chat_id))
|
213 |
-
if not curr_setting:
|
214 |
-
curr_setting = WarnSettings(chat_id, soft_warn=soft_warn)
|
215 |
-
|
216 |
-
curr_setting.soft_warn = soft_warn
|
217 |
-
|
218 |
-
SESSION.add(curr_setting)
|
219 |
-
SESSION.commit()
|
220 |
-
|
221 |
-
|
222 |
-
def get_warn_setting(chat_id):
|
223 |
-
try:
|
224 |
-
setting = SESSION.query(WarnSettings).get(str(chat_id))
|
225 |
-
if setting:
|
226 |
-
return setting.warn_limit, setting.soft_warn
|
227 |
-
else:
|
228 |
-
return 3, False
|
229 |
-
|
230 |
-
finally:
|
231 |
-
SESSION.close()
|
232 |
-
|
233 |
-
|
234 |
-
def num_warns():
|
235 |
-
try:
|
236 |
-
return SESSION.query(func.sum(Warns.num_warns)).scalar() or 0
|
237 |
-
finally:
|
238 |
-
SESSION.close()
|
239 |
-
|
240 |
-
|
241 |
-
def num_warn_chats():
|
242 |
-
try:
|
243 |
-
return SESSION.query(func.count(distinct(Warns.chat_id))).scalar()
|
244 |
-
finally:
|
245 |
-
SESSION.close()
|
246 |
-
|
247 |
-
|
248 |
-
def num_warn_filters():
|
249 |
-
try:
|
250 |
-
return SESSION.query(WarnFilters).count()
|
251 |
-
finally:
|
252 |
-
SESSION.close()
|
253 |
-
|
254 |
-
|
255 |
-
def num_warn_chat_filters(chat_id):
|
256 |
-
try:
|
257 |
-
return (
|
258 |
-
SESSION.query(WarnFilters.chat_id)
|
259 |
-
.filter(WarnFilters.chat_id == str(chat_id))
|
260 |
-
.count()
|
261 |
-
)
|
262 |
-
finally:
|
263 |
-
SESSION.close()
|
264 |
-
|
265 |
-
|
266 |
-
def num_warn_filter_chats():
|
267 |
-
try:
|
268 |
-
return SESSION.query(func.count(distinct(WarnFilters.chat_id))).scalar()
|
269 |
-
finally:
|
270 |
-
SESSION.close()
|
271 |
-
|
272 |
-
|
273 |
-
def __load_chat_warn_filters():
|
274 |
-
global WARN_FILTERS
|
275 |
-
try:
|
276 |
-
chats = SESSION.query(WarnFilters.chat_id).distinct().all()
|
277 |
-
for (chat_id,) in chats: # remove tuple by ( ,)
|
278 |
-
WARN_FILTERS[chat_id] = []
|
279 |
-
|
280 |
-
all_filters = SESSION.query(WarnFilters).all()
|
281 |
-
for x in all_filters:
|
282 |
-
WARN_FILTERS[x.chat_id] += [x.keyword]
|
283 |
-
|
284 |
-
WARN_FILTERS = {
|
285 |
-
x: sorted(set(y), key=lambda i: (-len(i), i))
|
286 |
-
for x, y in WARN_FILTERS.items()
|
287 |
-
}
|
288 |
-
|
289 |
-
finally:
|
290 |
-
SESSION.close()
|
291 |
-
|
292 |
-
|
293 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
294 |
-
with WARN_INSERTION_LOCK:
|
295 |
-
chat_notes = (
|
296 |
-
SESSION.query(Warns).filter(Warns.chat_id == str(old_chat_id)).all()
|
297 |
-
)
|
298 |
-
for note in chat_notes:
|
299 |
-
note.chat_id = str(new_chat_id)
|
300 |
-
SESSION.commit()
|
301 |
-
|
302 |
-
with WARN_FILTER_INSERTION_LOCK:
|
303 |
-
chat_filters = (
|
304 |
-
SESSION.query(WarnFilters)
|
305 |
-
.filter(WarnFilters.chat_id == str(old_chat_id))
|
306 |
-
.all()
|
307 |
-
)
|
308 |
-
for filt in chat_filters:
|
309 |
-
filt.chat_id = str(new_chat_id)
|
310 |
-
SESSION.commit()
|
311 |
-
old_warn_filt = WARN_FILTERS.get(str(old_chat_id))
|
312 |
-
if old_warn_filt is not None:
|
313 |
-
WARN_FILTERS[str(new_chat_id)] = old_warn_filt
|
314 |
-
del WARN_FILTERS[str(old_chat_id)]
|
315 |
-
|
316 |
-
with WARN_SETTINGS_LOCK:
|
317 |
-
chat_settings = (
|
318 |
-
SESSION.query(WarnSettings)
|
319 |
-
.filter(WarnSettings.chat_id == str(old_chat_id))
|
320 |
-
.all()
|
321 |
-
)
|
322 |
-
for setting in chat_settings:
|
323 |
-
setting.chat_id = str(new_chat_id)
|
324 |
-
SESSION.commit()
|
325 |
-
|
326 |
-
|
327 |
-
__load_chat_warn_filters()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database/sql/welcome_sql.py
DELETED
@@ -1,428 +0,0 @@
|
|
1 |
-
import random
|
2 |
-
import threading
|
3 |
-
from typing import Union
|
4 |
-
|
5 |
-
from sqlalchemy import BigInteger, Boolean, Column, Integer, String, UnicodeText
|
6 |
-
|
7 |
-
from Database.sql import BASE, SESSION
|
8 |
-
from Mikobot.plugins.helper_funcs.msg_types import Types
|
9 |
-
|
10 |
-
DEFAULT_WELCOME = "Hey {first}, how are you?"
|
11 |
-
DEFAULT_GOODBYE = "Nice knowing you!"
|
12 |
-
|
13 |
-
DEFAULT_WELCOME_MESSAGES = [
|
14 |
-
"{first} is here!", # Discord welcome messages copied
|
15 |
-
"Ready player {first}",
|
16 |
-
"Welcome bro {first}",
|
17 |
-
]
|
18 |
-
|
19 |
-
DEFAULT_GOODBYE_MESSAGES = [
|
20 |
-
"{first} will be missed.",
|
21 |
-
"{first} when back ?",
|
22 |
-
]
|
23 |
-
# Line 111 to 152 are references from https://bindingofisaac.fandom.com/wiki/Fortune_Telling_Machine
|
24 |
-
|
25 |
-
|
26 |
-
class Welcome(BASE):
|
27 |
-
__tablename__ = "welcome_pref"
|
28 |
-
chat_id = Column(String(14), primary_key=True)
|
29 |
-
should_welcome = Column(Boolean, default=True)
|
30 |
-
should_goodbye = Column(Boolean, default=True)
|
31 |
-
custom_content = Column(UnicodeText, default=None)
|
32 |
-
|
33 |
-
custom_welcome = Column(
|
34 |
-
UnicodeText,
|
35 |
-
default=random.choice(DEFAULT_WELCOME_MESSAGES),
|
36 |
-
)
|
37 |
-
welcome_type = Column(Integer, default=Types.TEXT.value)
|
38 |
-
|
39 |
-
custom_leave = Column(UnicodeText, default=random.choice(DEFAULT_GOODBYE_MESSAGES))
|
40 |
-
leave_type = Column(Integer, default=Types.TEXT.value)
|
41 |
-
|
42 |
-
clean_welcome = Column(BigInteger)
|
43 |
-
|
44 |
-
def __init__(self, chat_id, should_welcome=True, should_goodbye=True):
|
45 |
-
self.chat_id = chat_id
|
46 |
-
self.should_welcome = should_welcome
|
47 |
-
self.should_goodbye = should_goodbye
|
48 |
-
|
49 |
-
def __repr__(self):
|
50 |
-
return "<Chat {} should Welcome new users: {}>".format(
|
51 |
-
self.chat_id,
|
52 |
-
self.should_welcome,
|
53 |
-
)
|
54 |
-
|
55 |
-
|
56 |
-
class WelcomeButtons(BASE):
|
57 |
-
__tablename__ = "welcome_urls"
|
58 |
-
id = Column(Integer, primary_key=True, autoincrement=True)
|
59 |
-
chat_id = Column(String(14), primary_key=True)
|
60 |
-
name = Column(UnicodeText, nullable=False)
|
61 |
-
url = Column(UnicodeText, nullable=False)
|
62 |
-
same_line = Column(Boolean, default=False)
|
63 |
-
|
64 |
-
def __init__(self, chat_id, name, url, same_line=False):
|
65 |
-
self.chat_id = str(chat_id)
|
66 |
-
self.name = name
|
67 |
-
self.url = url
|
68 |
-
self.same_line = same_line
|
69 |
-
|
70 |
-
|
71 |
-
class GoodbyeButtons(BASE):
|
72 |
-
__tablename__ = "leave_urls"
|
73 |
-
id = Column(Integer, primary_key=True, autoincrement=True)
|
74 |
-
chat_id = Column(String(14), primary_key=True)
|
75 |
-
name = Column(UnicodeText, nullable=False)
|
76 |
-
url = Column(UnicodeText, nullable=False)
|
77 |
-
same_line = Column(Boolean, default=False)
|
78 |
-
|
79 |
-
def __init__(self, chat_id, name, url, same_line=False):
|
80 |
-
self.chat_id = str(chat_id)
|
81 |
-
self.name = name
|
82 |
-
self.url = url
|
83 |
-
self.same_line = same_line
|
84 |
-
|
85 |
-
|
86 |
-
class WelcomeMute(BASE):
|
87 |
-
__tablename__ = "welcome_mutes"
|
88 |
-
chat_id = Column(String(14), primary_key=True)
|
89 |
-
welcomemutes = Column(UnicodeText, default=False)
|
90 |
-
|
91 |
-
def __init__(self, chat_id, welcomemutes):
|
92 |
-
self.chat_id = str(chat_id) # ensure string
|
93 |
-
self.welcomemutes = welcomemutes
|
94 |
-
|
95 |
-
|
96 |
-
class WelcomeMuteUsers(BASE):
|
97 |
-
__tablename__ = "human_checks"
|
98 |
-
user_id = Column(BigInteger, primary_key=True)
|
99 |
-
chat_id = Column(String(14), primary_key=True)
|
100 |
-
human_check = Column(Boolean)
|
101 |
-
|
102 |
-
def __init__(self, user_id, chat_id, human_check):
|
103 |
-
self.user_id = user_id # ensure string
|
104 |
-
self.chat_id = str(chat_id)
|
105 |
-
self.human_check = human_check
|
106 |
-
|
107 |
-
|
108 |
-
class CleanServiceSetting(BASE):
|
109 |
-
__tablename__ = "clean_service"
|
110 |
-
chat_id = Column(String(14), primary_key=True)
|
111 |
-
clean_service = Column(Boolean, default=True)
|
112 |
-
|
113 |
-
def __init__(self, chat_id):
|
114 |
-
self.chat_id = str(chat_id)
|
115 |
-
|
116 |
-
def __repr__(self):
|
117 |
-
return "<Chat used clean service ({})>".format(self.chat_id)
|
118 |
-
|
119 |
-
|
120 |
-
Welcome.__table__.create(checkfirst=True)
|
121 |
-
WelcomeButtons.__table__.create(checkfirst=True)
|
122 |
-
GoodbyeButtons.__table__.create(checkfirst=True)
|
123 |
-
WelcomeMute.__table__.create(checkfirst=True)
|
124 |
-
WelcomeMuteUsers.__table__.create(checkfirst=True)
|
125 |
-
CleanServiceSetting.__table__.create(checkfirst=True)
|
126 |
-
|
127 |
-
INSERTION_LOCK = threading.RLock()
|
128 |
-
WELC_BTN_LOCK = threading.RLock()
|
129 |
-
LEAVE_BTN_LOCK = threading.RLock()
|
130 |
-
WM_LOCK = threading.RLock()
|
131 |
-
CS_LOCK = threading.RLock()
|
132 |
-
|
133 |
-
|
134 |
-
def welcome_mutes(chat_id):
|
135 |
-
try:
|
136 |
-
welcomemutes = SESSION.query(WelcomeMute).get(str(chat_id))
|
137 |
-
if welcomemutes:
|
138 |
-
return welcomemutes.welcomemutes
|
139 |
-
return False
|
140 |
-
finally:
|
141 |
-
SESSION.close()
|
142 |
-
|
143 |
-
|
144 |
-
def set_welcome_mutes(chat_id, welcomemutes):
|
145 |
-
with WM_LOCK:
|
146 |
-
prev = SESSION.query(WelcomeMute).get((str(chat_id)))
|
147 |
-
if prev:
|
148 |
-
SESSION.delete(prev)
|
149 |
-
welcome_m = WelcomeMute(str(chat_id), welcomemutes)
|
150 |
-
SESSION.add(welcome_m)
|
151 |
-
SESSION.commit()
|
152 |
-
|
153 |
-
|
154 |
-
def set_human_checks(user_id, chat_id):
|
155 |
-
with INSERTION_LOCK:
|
156 |
-
human_check = SESSION.query(WelcomeMuteUsers).get((user_id, str(chat_id)))
|
157 |
-
if not human_check:
|
158 |
-
human_check = WelcomeMuteUsers(user_id, str(chat_id), True)
|
159 |
-
|
160 |
-
else:
|
161 |
-
human_check.human_check = True
|
162 |
-
|
163 |
-
SESSION.add(human_check)
|
164 |
-
SESSION.commit()
|
165 |
-
|
166 |
-
return human_check
|
167 |
-
|
168 |
-
|
169 |
-
def get_human_checks(user_id, chat_id):
|
170 |
-
try:
|
171 |
-
human_check = SESSION.query(WelcomeMuteUsers).get((user_id, str(chat_id)))
|
172 |
-
if not human_check:
|
173 |
-
return None
|
174 |
-
human_check = human_check.human_check
|
175 |
-
return human_check
|
176 |
-
finally:
|
177 |
-
SESSION.close()
|
178 |
-
|
179 |
-
|
180 |
-
def get_welc_mutes_pref(chat_id):
|
181 |
-
welcomemutes = SESSION.query(WelcomeMute).get(str(chat_id))
|
182 |
-
SESSION.close()
|
183 |
-
|
184 |
-
if welcomemutes:
|
185 |
-
return welcomemutes.welcomemutes
|
186 |
-
|
187 |
-
return False
|
188 |
-
|
189 |
-
|
190 |
-
def get_welc_pref(chat_id):
|
191 |
-
welc = SESSION.query(Welcome).get(str(chat_id))
|
192 |
-
SESSION.close()
|
193 |
-
if welc:
|
194 |
-
return (
|
195 |
-
welc.should_welcome,
|
196 |
-
welc.custom_welcome,
|
197 |
-
welc.custom_content,
|
198 |
-
welc.welcome_type,
|
199 |
-
)
|
200 |
-
|
201 |
-
else:
|
202 |
-
# Welcome by default.
|
203 |
-
return True, DEFAULT_WELCOME, None, Types.TEXT
|
204 |
-
|
205 |
-
|
206 |
-
def get_gdbye_pref(chat_id):
|
207 |
-
welc = SESSION.query(Welcome).get(str(chat_id))
|
208 |
-
SESSION.close()
|
209 |
-
if welc:
|
210 |
-
return welc.should_goodbye, welc.custom_leave, welc.leave_type
|
211 |
-
else:
|
212 |
-
# Welcome by default.
|
213 |
-
return True, DEFAULT_GOODBYE, Types.TEXT
|
214 |
-
|
215 |
-
|
216 |
-
def set_clean_welcome(chat_id, clean_welcome):
|
217 |
-
with INSERTION_LOCK:
|
218 |
-
curr = SESSION.query(Welcome).get(str(chat_id))
|
219 |
-
if not curr:
|
220 |
-
curr = Welcome(str(chat_id))
|
221 |
-
|
222 |
-
curr.clean_welcome = int(clean_welcome)
|
223 |
-
|
224 |
-
SESSION.add(curr)
|
225 |
-
SESSION.commit()
|
226 |
-
|
227 |
-
|
228 |
-
def get_clean_pref(chat_id):
|
229 |
-
welc = SESSION.query(Welcome).get(str(chat_id))
|
230 |
-
SESSION.close()
|
231 |
-
|
232 |
-
if welc:
|
233 |
-
return welc.clean_welcome
|
234 |
-
|
235 |
-
return False
|
236 |
-
|
237 |
-
|
238 |
-
def set_welc_preference(chat_id, should_welcome):
|
239 |
-
with INSERTION_LOCK:
|
240 |
-
curr = SESSION.query(Welcome).get(str(chat_id))
|
241 |
-
if not curr:
|
242 |
-
curr = Welcome(str(chat_id), should_welcome=should_welcome)
|
243 |
-
else:
|
244 |
-
curr.should_welcome = should_welcome
|
245 |
-
|
246 |
-
SESSION.add(curr)
|
247 |
-
SESSION.commit()
|
248 |
-
|
249 |
-
|
250 |
-
def set_gdbye_preference(chat_id, should_goodbye):
|
251 |
-
with INSERTION_LOCK:
|
252 |
-
curr = SESSION.query(Welcome).get(str(chat_id))
|
253 |
-
if not curr:
|
254 |
-
curr = Welcome(str(chat_id), should_goodbye=should_goodbye)
|
255 |
-
else:
|
256 |
-
curr.should_goodbye = should_goodbye
|
257 |
-
|
258 |
-
SESSION.add(curr)
|
259 |
-
SESSION.commit()
|
260 |
-
|
261 |
-
|
262 |
-
def set_custom_welcome(
|
263 |
-
chat_id,
|
264 |
-
custom_content,
|
265 |
-
custom_welcome,
|
266 |
-
welcome_type,
|
267 |
-
buttons=None,
|
268 |
-
):
|
269 |
-
if buttons is None:
|
270 |
-
buttons = []
|
271 |
-
|
272 |
-
with INSERTION_LOCK:
|
273 |
-
welcome_settings = SESSION.query(Welcome).get(str(chat_id))
|
274 |
-
if not welcome_settings:
|
275 |
-
welcome_settings = Welcome(str(chat_id), True)
|
276 |
-
|
277 |
-
if custom_welcome or custom_content:
|
278 |
-
welcome_settings.custom_content = custom_content
|
279 |
-
welcome_settings.custom_welcome = custom_welcome
|
280 |
-
welcome_settings.welcome_type = welcome_type.value
|
281 |
-
|
282 |
-
else:
|
283 |
-
welcome_settings.custom_welcome = DEFAULT_WELCOME
|
284 |
-
welcome_settings.welcome_type = Types.TEXT.value
|
285 |
-
|
286 |
-
SESSION.add(welcome_settings)
|
287 |
-
|
288 |
-
with WELC_BTN_LOCK:
|
289 |
-
prev_buttons = (
|
290 |
-
SESSION.query(WelcomeButtons)
|
291 |
-
.filter(WelcomeButtons.chat_id == str(chat_id))
|
292 |
-
.all()
|
293 |
-
)
|
294 |
-
for btn in prev_buttons:
|
295 |
-
SESSION.delete(btn)
|
296 |
-
|
297 |
-
for b_name, url, same_line in buttons:
|
298 |
-
button = WelcomeButtons(chat_id, b_name, url, same_line)
|
299 |
-
SESSION.add(button)
|
300 |
-
|
301 |
-
SESSION.commit()
|
302 |
-
|
303 |
-
|
304 |
-
def get_custom_welcome(chat_id):
|
305 |
-
welcome_settings = SESSION.query(Welcome).get(str(chat_id))
|
306 |
-
ret = DEFAULT_WELCOME
|
307 |
-
if welcome_settings and welcome_settings.custom_welcome:
|
308 |
-
ret = welcome_settings.custom_welcome
|
309 |
-
|
310 |
-
SESSION.close()
|
311 |
-
return ret
|
312 |
-
|
313 |
-
|
314 |
-
def set_custom_gdbye(chat_id, custom_goodbye, goodbye_type, buttons=None):
|
315 |
-
if buttons is None:
|
316 |
-
buttons = []
|
317 |
-
|
318 |
-
with INSERTION_LOCK:
|
319 |
-
welcome_settings = SESSION.query(Welcome).get(str(chat_id))
|
320 |
-
if not welcome_settings:
|
321 |
-
welcome_settings = Welcome(str(chat_id), True)
|
322 |
-
|
323 |
-
if custom_goodbye:
|
324 |
-
welcome_settings.custom_leave = custom_goodbye
|
325 |
-
welcome_settings.leave_type = goodbye_type.value
|
326 |
-
|
327 |
-
else:
|
328 |
-
welcome_settings.custom_leave = DEFAULT_GOODBYE
|
329 |
-
welcome_settings.leave_type = Types.TEXT.value
|
330 |
-
|
331 |
-
SESSION.add(welcome_settings)
|
332 |
-
|
333 |
-
with LEAVE_BTN_LOCK:
|
334 |
-
prev_buttons = (
|
335 |
-
SESSION.query(GoodbyeButtons)
|
336 |
-
.filter(GoodbyeButtons.chat_id == str(chat_id))
|
337 |
-
.all()
|
338 |
-
)
|
339 |
-
for btn in prev_buttons:
|
340 |
-
SESSION.delete(btn)
|
341 |
-
|
342 |
-
for b_name, url, same_line in buttons:
|
343 |
-
button = GoodbyeButtons(chat_id, b_name, url, same_line)
|
344 |
-
SESSION.add(button)
|
345 |
-
|
346 |
-
SESSION.commit()
|
347 |
-
|
348 |
-
|
349 |
-
def get_custom_gdbye(chat_id):
|
350 |
-
welcome_settings = SESSION.query(Welcome).get(str(chat_id))
|
351 |
-
ret = DEFAULT_GOODBYE
|
352 |
-
if welcome_settings and welcome_settings.custom_leave:
|
353 |
-
ret = welcome_settings.custom_leave
|
354 |
-
|
355 |
-
SESSION.close()
|
356 |
-
return ret
|
357 |
-
|
358 |
-
|
359 |
-
def get_welc_buttons(chat_id):
|
360 |
-
try:
|
361 |
-
return (
|
362 |
-
SESSION.query(WelcomeButtons)
|
363 |
-
.filter(WelcomeButtons.chat_id == str(chat_id))
|
364 |
-
.order_by(WelcomeButtons.id)
|
365 |
-
.all()
|
366 |
-
)
|
367 |
-
finally:
|
368 |
-
SESSION.close()
|
369 |
-
|
370 |
-
|
371 |
-
def get_gdbye_buttons(chat_id):
|
372 |
-
try:
|
373 |
-
return (
|
374 |
-
SESSION.query(GoodbyeButtons)
|
375 |
-
.filter(GoodbyeButtons.chat_id == str(chat_id))
|
376 |
-
.order_by(GoodbyeButtons.id)
|
377 |
-
.all()
|
378 |
-
)
|
379 |
-
finally:
|
380 |
-
SESSION.close()
|
381 |
-
|
382 |
-
|
383 |
-
def clean_service(chat_id: Union[str, int]) -> bool:
|
384 |
-
try:
|
385 |
-
chat_setting = SESSION.query(CleanServiceSetting).get(str(chat_id))
|
386 |
-
if chat_setting:
|
387 |
-
return chat_setting.clean_service
|
388 |
-
return False
|
389 |
-
finally:
|
390 |
-
SESSION.close()
|
391 |
-
|
392 |
-
|
393 |
-
def set_clean_service(chat_id: Union[int, str], setting: bool):
|
394 |
-
with CS_LOCK:
|
395 |
-
chat_setting = SESSION.query(CleanServiceSetting).get(str(chat_id))
|
396 |
-
if not chat_setting:
|
397 |
-
chat_setting = CleanServiceSetting(chat_id)
|
398 |
-
|
399 |
-
chat_setting.clean_service = setting
|
400 |
-
SESSION.add(chat_setting)
|
401 |
-
SESSION.commit()
|
402 |
-
|
403 |
-
|
404 |
-
def migrate_chat(old_chat_id, new_chat_id):
|
405 |
-
with INSERTION_LOCK:
|
406 |
-
chat = SESSION.query(Welcome).get(str(old_chat_id))
|
407 |
-
if chat:
|
408 |
-
chat.chat_id = str(new_chat_id)
|
409 |
-
|
410 |
-
with WELC_BTN_LOCK:
|
411 |
-
chat_buttons = (
|
412 |
-
SESSION.query(WelcomeButtons)
|
413 |
-
.filter(WelcomeButtons.chat_id == str(old_chat_id))
|
414 |
-
.all()
|
415 |
-
)
|
416 |
-
for btn in chat_buttons:
|
417 |
-
btn.chat_id = str(new_chat_id)
|
418 |
-
|
419 |
-
with LEAVE_BTN_LOCK:
|
420 |
-
chat_buttons = (
|
421 |
-
SESSION.query(GoodbyeButtons)
|
422 |
-
.filter(GoodbyeButtons.chat_id == str(old_chat_id))
|
423 |
-
.all()
|
424 |
-
)
|
425 |
-
for btn in chat_buttons:
|
426 |
-
btn.chat_id = str(new_chat_id)
|
427 |
-
|
428 |
-
SESSION.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|