forked from SpEcHiDe/UniBorg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocks.py
260 lines (249 loc) · 9.53 KB
/
locks.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
"""Default Permission in Telegram 5.0.1
Available Commands: .lock <option>, .unlock <option>, .locks
API Options: msg, media, sticker, gif, gamee, ainline, gpoll, adduser, cpin, changeinfo
DB Options: bots, commands, email, forward, url"""
from telethon import events, functions, types
@borg.on(slitu.admin_cmd(pattern="lock( (?P<target>\S+)|$)"))
async def _(event):
# Space weirdness in regex required because argument is optional and other
# commands start with ".lock"
if event.fwd_from:
return
input_str = event.pattern_match.group("target")
peer_id = event.chat_id
if input_str in (("bots", "commands", "email", "forward", "url")):
try:
from sql_helpers.locks_sql import update_lock
except Exception as e:
logger.info("DB_URI is not configured.")
logger.info(str(e))
return False
update_lock(peer_id, input_str, True)
await event.edit(
"Locked {}".format(input_str)
)
else:
msg = None
media = None
sticker = None
gif = None
gamee = None
ainline = None
gpoll = None
adduser = None
cpin = None
changeinfo = None
if input_str:
if "msg" in input_str:
msg = True
if "media" in input_str:
media = True
if "sticker" in input_str:
sticker = True
if "gif" in input_str:
gif = True
if "gamee" in input_str:
gamee = True
if "ainline" in input_str:
ainline = True
if "gpoll" in input_str:
gpoll = True
if "adduser" in input_str:
adduser = True
if "cpin" in input_str:
cpin = True
if "changeinfo" in input_str:
changeinfo = True
banned_rights = types.ChatBannedRights(
until_date=None,
# view_messages=None,
send_messages=msg,
send_media=media,
send_stickers=sticker,
send_gifs=gif,
send_games=gamee,
send_inline=ainline,
send_polls=gpoll,
invite_users=adduser,
pin_messages=cpin,
change_info=changeinfo,
)
try:
result = await event.client(
functions.messages.EditChatDefaultBannedRightsRequest(
peer=peer_id,
banned_rights=banned_rights
)
)
except Exception as e: # pylint:disable=C0103,W0703
await event.edit(str(e))
else:
await event.edit(
"Current Chat Default Permissions Changed Successfully, in API"
)
@borg.on(slitu.admin_cmd(pattern="unlock ?(.*)"))
async def _(event):
if event.fwd_from:
return
try:
from sql_helpers.locks_sql import update_lock
except Exception as e:
logger.info("DB_URI is not configured.")
logger.info(str(e))
return False
input_str = event.pattern_match.group(1)
peer_id = event.chat_id
if input_str in (("bots", "commands", "email", "forward", "url")):
update_lock(peer_id, input_str, False)
await event.edit(
"UnLocked {}".format(input_str)
)
else:
await event.edit(
"Use `.lock` without any parameters to unlock API locks"
)
@borg.on(slitu.admin_cmd(pattern="curenabledlocks"))
async def _(event):
if event.fwd_from:
return
try:
from sql_helpers.locks_sql import get_locks
except Exception as e:
logger.info("DB_URI is not configured.")
logger.info(str(e))
return False
res = ""
current_db_locks = get_locks(event.chat_id)
if not current_db_locks:
res = "There are no DataBase locks in this chat"
else:
res = "Following are the DataBase locks in this chat: \n"
res += "👉 `bots`: `{}`\n".format(current_db_locks.bots)
res += "👉 `commands`: `{}`\n".format(current_db_locks.commands)
res += "👉 `email`: `{}`\n".format(current_db_locks.email)
res += "👉 `forward`: `{}`\n".format(current_db_locks.forward)
res += "👉 `url`: `{}`\n".format(current_db_locks.url)
current_chat = await event.get_chat()
try:
current_api_locks = current_chat.default_banned_rights
except AttributeError as e:
logger.info(str(e))
else:
res += "\nFollowing are the API locks in this chat: \n"
res += "👉 `msg`: `{}`\n".format(current_api_locks.send_messages)
res += "👉 `media`: `{}`\n".format(current_api_locks.send_media)
res += "👉 `sticker`: `{}`\n".format(current_api_locks.send_stickers)
res += "👉 `gif`: `{}`\n".format(current_api_locks.send_gifs)
res += "👉 `gamee`: `{}`\n".format(current_api_locks.send_games)
res += "👉 `ainline`: `{}`\n".format(current_api_locks.send_inline)
res += "👉 `gpoll`: `{}`\n".format(current_api_locks.send_polls)
res += "👉 `adduser`: `{}`\n".format(current_api_locks.invite_users)
res += "👉 `cpin`: `{}`\n".format(current_api_locks.pin_messages)
res += "👉 `changeinfo`: `{}`\n".format(current_api_locks.change_info)
await event.edit(res)
@borg.on(events.MessageEdited()) # pylint:disable=E0602
@borg.on(events.NewMessage()) # pylint:disable=E0602
async def check_incoming_messages(event):
try:
from sql_helpers.locks_sql import update_lock, is_locked
except Exception as e:
logger.info("DB_URI is not configured.")
logger.info(str(e))
return False
if await slitu.is_admin(event.client, event.chat_id, event.sender_id):
return
peer_id = event.chat_id
if is_locked(peer_id, "commands"):
entities = event.message.entities
is_command = False
if entities:
for entity in entities:
if isinstance(entity, types.MessageEntityBotCommand):
is_command = True
if is_command:
try:
await event.delete()
except Exception as e:
await event.reply(
"I don't seem to have ADMIN permission here. \n`{}`".format(str(e))
)
update_lock(peer_id, "commands", False)
if is_locked(peer_id, "forward") and event.fwd_from:
try:
await event.delete()
except Exception as e:
await event.reply(
"I don't seem to have ADMIN permission here. \n`{}`".format(str(e))
)
update_lock(peer_id, "forward", False)
if is_locked(peer_id, "email"):
entities = event.message.entities
is_email = False
if entities:
for entity in entities:
if isinstance(entity, types.MessageEntityEmail):
is_email = True
if is_email:
try:
await event.delete()
except Exception as e:
await event.reply(
"I don't seem to have ADMIN permission here. \n`{}`".format(str(e))
)
update_lock(peer_id, "email", False)
if is_locked(peer_id, "url"):
entities = event.message.entities
is_url = False
if entities:
for entity in entities:
if isinstance(entity, (types.MessageEntityTextUrl, types.MessageEntityUrl)):
is_url = True
if is_url:
try:
await event.delete()
except Exception as e:
await event.reply(
"I don't seem to have ADMIN permission here. \n`{}`".format(str(e))
)
update_lock(peer_id, "url", False)
@borg.on(events.ChatAction()) # pylint:disable=E0602
async def _(event):
try:
from sql_helpers.locks_sql import update_lock, is_locked
except Exception as e:
logger.info("DB_URI is not configured.")
logger.info(str(e))
return False
if await slitu.is_admin(event.client, event.chat_id, event.action_message.sender_id):
return
if is_locked(event.chat_id, "bots"):
# bots are limited Telegram accounts,
# and cannot join by themselves
if event.user_added:
users_added_by = event.action_message.sender_id
is_ban_able = False
rights = types.ChatBannedRights(
until_date=None,
view_messages=True
)
added_users = event.action_message.action.users
for user_id in added_users:
user_obj = await event.client.get_entity(user_id)
if user_obj.bot:
is_ban_able = True
try:
await event.client(functions.channels.EditBannedRequest(
event.chat_id,
user_obj,
rights
))
except Exception as e:
await event.reply(
"I don't seem to have ADMIN permission here. \n`{}`".format(str(e))
)
update_lock(event.chat_id, "bots", False)
break
if Config.G_BAN_LOGGER_GROUP is not None and is_ban_able:
ban_reason_msg = await event.reply(
"!warn [user](tg://user?id={}) Please Do Not Add BOTs to this chat.".format(users_added_by)
)