-
Notifications
You must be signed in to change notification settings - Fork 570
/
userbot.py
232 lines (214 loc) · 6.96 KB
/
userbot.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
"""
CREDITS:
EVAL AND SH FUNCTION IN THIS MODULE IS WRITTEN BY @Pokurt.
SOURCE:
https://github.com/pokurt/Nana-Remix/blob/master/nana/plugins/devs.py
"""
import os
import re
import subprocess
import sys
import traceback
from html import escape
from inspect import getfullargspec
from io import StringIO
import aiofiles
from pyrogram import filters
from pyrogram.types import Message
from handlers.play import arq
from config import SUDO_USERS
from helpers.misc import exec_time
from callsmusic.callsmusic import client as tede
# Eval and Sh module from nana-remix
m = None
p = print
r = None
exec_time = exec_time
arq = arq
async def aexec(code, client, message):
exec(
"async def __aexec(client, message): "
+ "".join(f"\n {a}" for a in code.split("\n"))
)
return await locals()["__aexec"](client, message)
async def edit_or_reply(msg: Message, **kwargs):
func = msg.edit_text if msg.from_user.is_self else msg.reply
spec = getfullargspec(func.__wrapped__).args
await func(**{k: v for k, v in kwargs.items() if k in spec})
@tede.on_message(
filters.user(SUDO_USERS)
& ~filters.forwarded
& ~filters.via_bot
& ~filters.edited
& filters.command("py", prefixes=".")
)
async def executor(client, message: Message):
global m, p, r
try:
cmd = message.text.split(" ", maxsplit=1)[1]
except IndexError:
return await message.delete()
m = message
p = print
if message.reply_to_message:
r = message.reply_to_message
old_stderr = sys.stderr
old_stdout = sys.stdout
redirected_output = sys.stdout = StringIO()
redirected_error = sys.stderr = StringIO()
stdout, stderr, exc = None, None, None
try:
await aexec(cmd, client, message)
except Exception:
exc = traceback.format_exc()
stdout = redirected_output.getvalue()
stderr = redirected_error.getvalue()
sys.stdout = old_stdout
sys.stderr = old_stderr
evaluation = ""
if exc:
evaluation = exc
elif stderr:
evaluation = stderr
elif stdout:
evaluation = stdout
else:
evaluation = "Success"
final_output = f"**INPUT:**\n```{escape(cmd)}```\n\n**OUTPUT**:\n```{escape(evaluation.strip())}```"
if len(final_output) > 4096:
filename = "output.txt"
with open(filename, "w+", encoding="utf8") as out_file:
out_file.write(str(evaluation.strip()))
await message.reply_document(
document=filename,
caption=f"**INPUT:**\n`{escape(cmd[0:980])}`\n\n**OUTPUT:**\n`Attached Document`",
quote=False,
)
await message.delete()
os.remove(filename)
else:
await edit_or_reply(message, text=final_output)
@tede.on_message(
filters.user(SUDO_USERS)
& ~filters.forwarded
& ~filters.via_bot
& ~filters.edited
& filters.command("sh", prefixes="."),
)
async def shellrunner(client, message: Message):
if len(message.command) < 2:
return await edit_or_reply(
message, text="**Usage:**\n/sh git pull"
)
text = message.text.split(None, 1)[1]
if "\n" in text:
code = text.split("\n")
output = ""
for x in code:
shell = re.split(
""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x
)
try:
process = subprocess.Popen(
shell,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except Exception as err:
print(err)
await edit_or_reply(
message,
text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{escape(err)}```",
)
output += f"**{code}**\n"
output += process.stdout.read()[:-1].decode("utf-8")
output += "\n"
else:
shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
for a in range(len(shell)):
shell[a] = shell[a].replace('"', "")
try:
process = subprocess.Popen(
shell,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except Exception as err:
print(err)
exc_type, exc_obj, exc_tb = sys.exc_info()
errors = traceback.format_exception(
etype=exc_type,
value=exc_obj,
tb=exc_tb,
)
return await edit_or_reply(
message,
text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{''.join(errors)}```",
)
output = process.stdout.read()[:-1].decode("utf-8")
if str(output) == "\n":
output = None
if output:
if len(output) > 4096:
with open("output.txt", "w+") as file:
file.write(output)
await tede.send_document(
message.chat.id,
"output.txt",
reply_to_message_id=message.message_id,
caption=escape(text),
)
return os.remove("output.txt")
await edit_or_reply(
message,
text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT:**\n```{escape(output)}```",
)
else:
await edit_or_reply(
message,
text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT: **\n`No output`",
)
""" C and CPP Eval """
async def sendFile(message: Message, text: str):
file = "output.txt"
async with aiofiles.open(file, mode="w+") as f:
await f.write(text)
await message.reply_document(file)
os.remove(file)
@tede.on_message(
filters.command(["c", "cpp"], prefixes=".")
& ~filters.edited
& ~filters.via_bot
& filters.user(SUDO_USERS)
)
async def c_cpp_eval(_, message: Message):
if len(message.command) < 2:
return await message.edit("Write Some Code..")
code = message.text.split(None, 1)[1]
file = "exec.c"
compiler = "g++"
out = "exec"
cmdCompile = [compiler, "-g", file, "-o", out]
cmdRun = [f"./{out}"]
async with aiofiles.open(file, mode="w+") as f:
await f.write(code)
pCompile = subprocess.run(cmdCompile, capture_output=True)
os.remove(file)
err = pCompile.stderr.decode()
if err:
text = f"**INPUT:**\n```{escape(code)}```\n\n**COMPILE-TIME ERROR:**```{escape(err)}```"
if len(text) > 4090:
return await sendFile(message, text)
return await edit_or_reply(message, text=text)
pRun = subprocess.run(cmdRun, capture_output=True)
os.remove(out)
err = pRun.stderr.decode()
out = pRun.stdout.decode()
err = f"**RUNTIME ERROR:**\n```{escape(err)}```" if err else None
out = f"**OUTPUT:**\n```{escape(out)}```" if out else None
text = (
f"**INPUT:**\n```{escape(code)}```\n\n{err if err else out}"
)
if len(text) > 4090:
return await sendFile(message, text)
await edit_or_reply(message, text=text)