This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
273 lines (250 loc) · 9.85 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import logging.handlers
import os
import time
from hashlib import blake2s
from ruamel.yaml import YAML
from telegram import ChatPermissions
from telegram.bot import Bot
FullChatPermissions = ChatPermissions(
can_send_messages=True,
can_send_media_messages=True,
can_send_polls=True,
can_send_other_messages=True,
can_add_web_page_previews=True,
can_change_info=True,
can_invite_users=True,
can_pin_messages=True,
)
yaml = YAML()
logger = logging.getLogger("Telegram_Group_Easyauth")
logger.setLevel(logging.DEBUG)
formater = logging.Formatter(
"%(asctime)s - %(levelname)s - %(funcName)s[%(module)s:%(lineno)d] - %(message)s"
)
# Source: http://code.activestate.com/recipes/325905-memoize-decorator-with-timeout/#c1
class MWT(object):
"""Memoize With Timeout"""
_caches: dict = dict()
_timeouts: dict = dict()
def __init__(self, timeout: int = 2):
self.timeout = timeout
def collect(self):
"""Clear cache of results which have timed out"""
for func in self._caches:
cache = {}
for key in self._caches[func]:
if (time.time() - self._caches[func][key][1]) < self._timeouts[func]:
cache[key] = self._caches[func][key]
self._caches[func] = cache
def __call__(self, f):
self.cache = self._caches[f] = {}
self._timeouts[f] = self.timeout
def func(*args, **kwargs):
kw = sorted(kwargs.items())
key = (args, tuple(kw))
try:
v = self.cache[key]
if (time.time() - v[1]) > self.timeout:
raise KeyError
except KeyError:
v = self.cache[key] = f(*args, **kwargs), time.time()
return v[0]
func.func_name = f.__name__
return func
@MWT(timeout=60 * 60)
def get_chat_admins(bot: Bot, chat_id: int, extra_user=None) -> list:
if extra_user is not None and isinstance(extra_user, int):
users: list = [extra_user]
else:
users: list = extra_user
admins: list = [
admin.user.id
for admin in bot.get_chat_administrators(chat_id)
if admin.user.id != bot.get_me().id
]
if users:
admins.extend(users)
return admins
@MWT(timeout=60 * 60)
def get_chat_admins_name(bot: Bot, chat_id: int, extra_user=None) -> str:
if extra_user is not None and isinstance(extra_user, int):
users: list = [extra_user]
else:
users: list = extra_user
admins: list = [
f"@{admin.user.username}"
for admin in bot.get_chat_administrators(chat_id)
if admin.user.id != bot.get_me().id
]
return " ".join(admins)
def log_to_stream() -> None:
streamhandler = logging.StreamHandler()
streamhandler.setLevel(logging.INFO)
streamhandler.setFormatter(formater)
logger.addHandler(streamhandler)
def log_to_file(filename) -> None:
filehandler = logging.handlers.RotatingFileHandler(
filename, maxBytes=1048576, backupCount=5, encoding="utf-8"
)
filehandler.setLevel(logging.DEBUG)
filehandler.setFormatter(formater)
logger.addHandler(filehandler)
def load_config(config: dict, check_token: bool = True) -> dict:
if check_token:
assert config.get("TOKEN"), "Config: No TOKEN."
if config.get("CHAT"):
assert isinstance(
config.get("CHAT"), int
), "Config: CHAT Must be ID, not username."
else:
logger.warning("Config: CHAT is not set! Use /start to get one in chat.")
if config.get("SUPER_ADMIN"):
assert isinstance(
config.get("SUPER_ADMIN"), int
), "Config: SUPER_ADMIN Must be ID, not username."
if not config.get("TIME"):
config["TIME"] = 120
if not config.get("BANTIME"):
config["BANTIME"] = 120
if config.get("QUIZ"):
assert (
len(config.get("QUIZ", "")) > 2
), "Config: QUIZ command Should be longer than 2 chars"
if not config.get("QUIZTIME"):
config["QUIZTIME"] = 1200
if config.get("ADMIN"):
assert (
len(config.get("ADMIN", "")) > 2
), "Config: ADMIN command Should be longer than 2 chars"
if not config.get("START"):
config["START"] = "CHAT ID: \\`{chat}\\`\nUSER ID: \\`{user}\\`"
if not config.get("GREET"):
config["GREET"] = "Question: {question}\nPlease answer it in {time}s."
if not config.get("SUCCESS"):
config["SUCCESS"] = "Succeed."
if not config.get("RETRY"):
config["RETRY"] = "Failed. Please retry after {time}s."
if not config.get("PASS"):
config[
"PASS"
] = "{user} passed the verification.\nQuestion: {question}\nAnswer: {ans}"
if not config.get("NOT_KICK"):
config[
"NOT_KICK"
] = "{user} didn't pass the verification.\nQuestion: {question}\nAnswer: {ans}"
if not config.get("KICK"):
config["KICK"] = "{user} have been kicked.\nQuestion: {question}\nAnswer: {ans}"
if not config.get("PASS_BTN"):
config["PASS_BTN"] = "Pass"
if not config.get("KICK_BTN"):
config["KICK_BTN"] = "Kick"
if not config.get("ADMIN_PASS"):
config["ADMIN_PASS"] = "{user} is allowed by admin {admin}."
if not config.get("ADMIN_KICK"):
config["ADMIN_KICK"] = "{user} is kicked by admin {admin}."
if not config.get("OTHER"):
config["OTHER"] = "Don't play with buttons."
if not config.get("RELOAD"):
config["RELOAD"] = "Reload finished. Now there are {num} questions."
if not config.get("PENDING"):
config["PENDING"] = "Adding reload task to task list."
if not config.get("CORRUPT"):
config["CORRUPT"] = "The file is corrupted.\n{text}"
if not config.get("BACK"):
config["BACK"] = "<- Back"
if not config.get("ADD_NEW_QUESTION_BTN"):
config["ADD_NEW_QUESTION_BTN"] = "Add new question"
if not config.get("LIST_ALL_QUESTION_BTN"):
config["LIST_ALL_QUESTION_BTN"] = "List all questions"
if not config.get("EDIT_QUESTION_BTN"):
config["EDIT_QUESTION_BTN"] = "Edit question"
if not config.get("DELETE_QUESTION_BTN"):
config["DELETE_QUESTION_BTN"] = "Delete question"
if not config.get("SAVE_QUESTION_BTN"):
config["SAVE_QUESTION_BTN"] = "Save question"
if not config.get("REEDIT_QUESTION_BTN"):
config["REEDIT_QUESTION_BTN"] = "Re-edit question"
if not config.get("START_PRIVATE"):
config[
"START_PRIVATE"
] = "This bot is working for {link}.\n /cancel -- exit\n /config -- pull config\n /reload -- reload config\nOr send config file to me directly."
if not config.get("START_UNAUTHORIZED_PRIVATE"):
config["START_UNAUTHORIZED_PRIVATE"] = "Unauthorized request."
if not config.get("LIST_PRIVATE"):
config["LIST_PRIVATE"] = "Current questions:"
if not config.get("EDIT_PRIVATE"):
config["EDIT_PRIVATE"] = "Editing questions: {text}"
if not config.get("EDIT_QUESTION_PRIVATE"):
config[
"EDIT_QUESTION_PRIVATE"
] = "Editing questions No.{num}, please send your question:"
if not config.get("EDIT_ANSWER_PRIVATE"):
config["EDIT_ANSWER_PRIVATE"] = "Question: {text}\nPlease send your answer:"
if not config.get("EDIT_WRONG_PRIVATE"):
config[
"EDIT_WRONG_PRIVATE"
] = "Answer: {text}\nPlease send your misleading text:"
if not config.get("EDIT_MORE_WRONG_PRIVATE"):
config[
"EDIT_MORE_WRONG_PRIVATE"
] = "Misleading text: {text}\nPlease send more or /finish"
if not config.get("DETAIL_QUESTION_PRIVATE"):
config[
"DETAIL_QUESTION_PRIVATE"
] = "Question: {question}\nAnswer: {ans}\nMisleading text:\n{wrong}"
if not config.get("EDIT_UNFINISH_PRIVATE"):
config["EDIT_UNFINISH_PRIVATE"] = "Need more misleading texts."
if not config.get("EDIT_FINISH_PRIVATE"):
config["EDIT_FINISH_PRIVATE"] = "Questions No.{num} finish."
if not config.get("CANCEL_PRIVATE"):
config["CANCEL_PRIVATE"] = "Process canceled."
if not config.get("SAVING_PRIVATE"):
config["SAVING_PRIVATE"] = "Saving..."
if not config.get("DELETING_PRIVATE"):
config["DELETING_PRIVATE"] = "Deleting..."
for flag in config.get("CHALLENGE"):
assert flag.get("QUESTION"), "Config: No QUESTION tile for question."
assert isinstance(
flag.get("QUESTION"), str
), f"Config: QUESTION {flag.get('QUESTION')} should be string object."
assert flag.get(
"ANSWER"
), f"Config: No ANSWER tile for question: {flag.get('QUESTION')}"
assert isinstance(
flag.get("ANSWER"), str
), f"Config: ANSWER {flag.get('ANSWER')} should be string object for question: {flag.get('QUESTION')}"
assert flag.get(
"WRONG"
), f"Config: No WRONG tile for question: {flag.get('QUESTION')}"
assert (
digest_size := len(flag.get("WRONG"))
) < 20, f"Config: Too many tiles for WRONG for question: {flag.get('QUESTION')}"
assert all(
isinstance(u, str) for u in flag.get("WRONG")
), f"Config: WRONG {flag.get('WRONG')} should all be string object for question: {flag.get('QUESTION')}"
flag.insert(
0,
"answer",
blake2s(
str(flag.get("ANSWER")).encode(),
salt=os.urandom(8),
digest_size=digest_size,
).hexdigest(),
)
flag.insert(
0,
"wrong",
[
blake2s(
str(flag.get("WRONG")[t]).encode(),
salt=os.urandom(8),
digest_size=digest_size,
).hexdigest()
for t in range(digest_size)
],
)
logger.debug(config)
return config