Skip to content

Commit

Permalink
stolen plugins from @spechides
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Mar 16, 2020
1 parent 603750c commit 9db6e6c
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 4 deletions.
2 changes: 1 addition & 1 deletion userge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .utils import logging
from .utils import Config, logging

log = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion userge/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def on_cmd(self, command: str):
self.log.info(f"setting new command => {command}")

def decorator(func):
dec = self.on_message(Filters.command(command, ".") & Filters.me)
dec = self.on_message(Filters.regex(pattern=f"^.{command}") & Filters.me)
return dec(func)

return decorator
Expand Down
2 changes: 1 addition & 1 deletion userge/plugins/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async def getplugins(_, message: Message):
all_plugins = await get_all_plugins()
out_str = ""
for plugin in all_plugins:
out_str += f"{plugin}.py\n"
out_str += f"**{plugin}**\n"
await message.edit(out_str)


Expand Down
78 changes: 78 additions & 0 deletions userge/plugins/eval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import io
import os
import sys
import traceback
from pyrogram import Message
from userge import userge, Config

log = userge.getLogger(__name__)


@userge.on_cmd("eval")
async def eval_(_, message: Message):
await message.edit("Processing ...")
cmd = message.text.split(" ", maxsplit=1)[1]
reply_to_id = message.message_id

if message.reply_to_message:
reply_to_id = message.reply_to_message.message_id

old_stderr = sys.stderr
old_stdout = sys.stdout
redirected_output = sys.stdout = io.StringIO()
redirected_error = sys.stderr = io.StringIO()
stdout, stderr, exc = None, None, None

try:
await aexec(cmd, userge, 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 = "**EVAL**: ```{}```\n\n**OUTPUT**:\n```{}``` \n".format(cmd, evaluation.strip())

if len(final_output) > Config.MAX_MESSAGE_LENGTH:
with open("eval.text", "w+", encoding="utf8") as out_file:
out_file.write(str(final_output))

await userge.send_document(
chat_id=message.chat.id,
document="eval.text",
caption=cmd,
disable_notification=True,
reply_to_message_id=reply_to_id
)

os.remove("eval.text")
await message.delete()

else:
await message.edit(final_output)


async def aexec(code, userge, message):
exec(
f'async def __aexec(userge, message): ' +
''.join(f'\n {l}' for l in code.split('\n'))
)
return await locals()['__aexec'](userge, message)


userge.add_help(
command="eval",
about="run eval"
)
58 changes: 58 additions & 0 deletions userge/plugins/exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import asyncio
import os
import time
from pyrogram import Message
from userge import userge, Config

log = userge.getLogger(__name__)


@userge.on_cmd("exec")
async def exec_(_, message: Message):
cmd = message.text.split(" ", maxsplit=1)[1]
reply_to_id = message.message_id

if message.reply_to_message:
reply_to_id = message.reply_to_message.message_id

process = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()

e = stderr.decode() or "No Error"
o = stdout.decode()

if o:
_o = o.split("\n")
o = "`\n".join(_o)
else:
o = "**Tip**: \n`If you want to see the results of your code, I suggest printing them to stdout.`"

OUTPUT = f"**QUERY:**\n__Command:__\n`{cmd}` \n__PID:__\n`{process.pid}`\n\n**stderr:** \n`{e}`\n**Output:**\n{o}"

if len(OUTPUT) > Config.MAX_MESSAGE_LENGTH:
with open("exec.text", "w+", encoding="utf8") as out_file:
out_file.write(str(OUTPUT))

await userge.send_document(
chat_id=message.chat.id,
document="exec.text",
caption=cmd,
disable_notification=True,
reply_to_message_id=reply_to_id
)

os.remove("exec.text")
await message.delete()

else:
await message.edit(OUTPUT)


userge.add_help(
command="exec",
about="run exec"
)
2 changes: 1 addition & 1 deletion userge/plugins/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
async def helpme(_, message: Message):
out_str = ""
for cmd in userge.get_help():
out_str += f".{cmd} : {userge.get_help(cmd)}\n"
out_str += f"**.{cmd}** : __{userge.get_help(cmd)}__\n"
await message.edit(out_str)


Expand Down
39 changes: 39 additions & 0 deletions userge/plugins/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
from pyrogram import Message
from userge import userge

log = userge.getLogger(__name__)


@userge.on_cmd("json")
async def jsonify(_, message: Message):
the_real_message = None
reply_to_id = None

if message.reply_to_message:
reply_to_id = message.reply_to_message.message_id
the_real_message = message.reply_to_message
else:
the_real_message = message
reply_to_id = message.message_id

try:
await message.edit(the_real_message)
except Exception as e:
with open("json.text", "w+", encoding="utf8") as out_file:
out_file.write(str(the_real_message))
await userge.send_document(
chat_id=message.chat.id,
document="json.text",
caption=str(e),
disable_notification=True,
reply_to_message_id=reply_to_id
)
os.remove("json.text")
await message.delete()


userge.add_help(
command="json",
about="replied msg to json"
)
48 changes: 48 additions & 0 deletions userge/plugins/whois.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
from pyrogram import Message
from userge import userge

log = userge.getLogger(__name__)


@userge.on_cmd("whois")
async def who_is(_, message: Message):
from_user = None
if " " in message.text:
recvd_command, user_id = message.text.split(" ")
try:
user_id = int(user_id)
from_user = await userge.get_users(user_id)
except Exception as e:
await message.edit(str(e))
return
elif message.reply_to_message:
from_user = message.reply_to_message.from_user
else:
await message.edit("no valid user_id / message specified")
return
if from_user is not None:
message_out_str = ""
message_out_str += f"ID: `{from_user.id}`\n"
message_out_str += f"First Name: <a href='tg://user?id={from_user.id}'>{from_user.first_name}</a>\n"
message_out_str += f"Last Name: {from_user.last_name}"
chat_photo = from_user.photo
local_user_photo = await userge.download_media(
message=chat_photo.big_file_id
)
await message.reply_photo(
photo=local_user_photo,
quote=True,
caption=message_out_str,
parse_mode="html",
# ttl_seconds=,
disable_notification=True
)
os.remove(local_user_photo)
await message.delete()


userge.add_help(
command="whois",
about="to get user details"
)
2 changes: 2 additions & 0 deletions userge/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ class Config:
HU_STRING_SESSION = os.environ.get("HU_STRING_SESSION", None)

DB_URI = os.environ.get("DATABASE_URL", None)

MAX_MESSAGE_LENGTH = 4096

0 comments on commit 9db6e6c

Please sign in to comment.