2020-02-19 15:31:39 +00:00
|
|
|
""" System related utilities for PagerMaid to integrate into the system. """
|
|
|
|
|
2021-06-15 16:51:34 +00:00
|
|
|
import io, sys, traceback
|
2020-02-19 15:31:39 +00:00
|
|
|
from platform import node
|
|
|
|
from getpass import getuser
|
|
|
|
from os import geteuid
|
|
|
|
from requests import head
|
2020-04-05 04:00:28 +00:00
|
|
|
from asyncio import sleep
|
2020-02-19 15:31:39 +00:00
|
|
|
from requests.exceptions import MissingSchema, InvalidURL, ConnectionError
|
2021-06-15 16:51:34 +00:00
|
|
|
from pagermaid import log, bot, redis_status, redis
|
2020-02-19 15:31:39 +00:00
|
|
|
from pagermaid.listener import listener
|
2021-06-15 04:31:05 +00:00
|
|
|
from pagermaid.utils import attach_log, execute, lang, alias_command
|
2020-08-09 05:32:50 +00:00
|
|
|
from telethon.errors.rpcerrorlist import UserAlreadyParticipantError
|
2020-04-05 03:27:29 +00:00
|
|
|
from telethon.tl.functions.messages import ImportChatInviteRequest
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("sh"),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('sh_des'),
|
|
|
|
parameters=lang('sh_parameters'))
|
2020-02-19 15:31:39 +00:00
|
|
|
async def sh(context):
|
|
|
|
""" Use the command-line from Telegram. """
|
|
|
|
user = getuser()
|
|
|
|
command = context.arguments
|
|
|
|
hostname = node()
|
|
|
|
if context.is_channel and not context.is_group:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('sh_channel'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if not command:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if geteuid() == 0:
|
|
|
|
await context.edit(
|
|
|
|
f"`{user}`@{hostname} ~"
|
|
|
|
f"\n> `#` {command}"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await context.edit(
|
|
|
|
f"`{user}`@{hostname} ~"
|
|
|
|
f"\n> `$` {command}"
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await execute(command)
|
|
|
|
|
|
|
|
if result:
|
|
|
|
if len(result) > 4096:
|
|
|
|
await attach_log(result, context.chat_id, "output.log", context.id)
|
|
|
|
return
|
|
|
|
|
|
|
|
if geteuid() == 0:
|
|
|
|
await context.edit(
|
|
|
|
f"`{user}`@{hostname} ~"
|
|
|
|
f"\n> `#` {command}"
|
|
|
|
f"\n`{result}`"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await context.edit(
|
|
|
|
f"`{user}`@{hostname} ~"
|
|
|
|
f"\n> `$` {command}"
|
|
|
|
f"\n`{result}`"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return
|
2021-04-12 16:25:32 +00:00
|
|
|
await log(f"{lang('sh_success')}: `{command}`")
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
2021-06-15 16:51:34 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command="eval",
|
|
|
|
description=lang('eval_des'),
|
|
|
|
parameters=lang('eval_parameters'))
|
|
|
|
async def sh(context):
|
|
|
|
""" Run python commands from Telegram. """
|
|
|
|
if not redis_status():
|
|
|
|
await context.edit(f"{lang('error_prefix')}{lang('redis_dis')}")
|
|
|
|
return
|
|
|
|
if not redis.get("dev"):
|
|
|
|
await context.edit(lang('eval_need_dev'))
|
|
|
|
return
|
|
|
|
if context.is_channel and not context.is_group:
|
|
|
|
await context.edit(lang('eval_channel'))
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
cmd = context.text.split(" ", maxsplit=1)[1]
|
|
|
|
except IndexError:
|
|
|
|
await context.edit(lang('arg_error'))
|
|
|
|
return
|
|
|
|
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, context)
|
|
|
|
except Exception:
|
|
|
|
exc = traceback.format_exc()
|
|
|
|
stdout = redirected_output.getvalue()
|
|
|
|
stderr = redirected_error.getvalue()
|
|
|
|
sys.stdout = old_stdout
|
|
|
|
sys.stderr = old_stderr
|
|
|
|
if exc:
|
|
|
|
evaluation = exc
|
|
|
|
elif stderr:
|
|
|
|
evaluation = stderr
|
|
|
|
elif stdout:
|
|
|
|
evaluation = stdout
|
|
|
|
else:
|
|
|
|
evaluation = "Success"
|
|
|
|
final_output = (
|
|
|
|
"**>>>** ```{}``` \n```{}```".format(
|
|
|
|
cmd,
|
|
|
|
evaluation,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if len(final_output) > 4096:
|
|
|
|
await context.edit("**>>>** ```{}```".format(cmd))
|
|
|
|
await attach_log(evaluation, context.chat_id, "output.log", context.id)
|
|
|
|
else:
|
|
|
|
await context.edit(final_output)
|
|
|
|
await log(f"{lang('eval_success')}: `{cmd}`")
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("restart"), diagnostics=False,
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('restart_des'))
|
2020-02-19 15:31:39 +00:00
|
|
|
async def restart(context):
|
|
|
|
""" To re-execute PagerMaid. """
|
|
|
|
if not context.text[0].isalpha():
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('restart_processing'))
|
|
|
|
await log(lang('restart_log'))
|
2020-02-19 15:31:39 +00:00
|
|
|
await context.client.disconnect()
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("trace"),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('trace_des'),
|
2020-02-19 15:31:39 +00:00
|
|
|
parameters="<url>")
|
|
|
|
async def trace(context):
|
|
|
|
""" Trace URL redirects. """
|
|
|
|
url = context.arguments
|
|
|
|
reply = await context.get_reply_message()
|
|
|
|
if reply:
|
|
|
|
url = reply.text
|
|
|
|
if url:
|
|
|
|
if url.startswith("https://") or url.startswith("http://"):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
url = "https://" + url
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('trace_processing'))
|
2020-02-19 15:31:39 +00:00
|
|
|
result = str("")
|
|
|
|
for url in url_tracer(url):
|
|
|
|
count = 0
|
|
|
|
if result:
|
|
|
|
result += " ↴\n" + url
|
|
|
|
else:
|
|
|
|
result = url
|
|
|
|
if count == 128:
|
2021-04-12 16:25:32 +00:00
|
|
|
result += f"\n\n{lang('trace_over128')}"
|
2020-02-19 15:31:39 +00:00
|
|
|
break
|
|
|
|
if result:
|
|
|
|
if len(result) > 4096:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('translate_tg_limit_uploading_file'))
|
2020-02-19 15:31:39 +00:00
|
|
|
await attach_log(result, context.chat_id, "output.log", context.id)
|
|
|
|
return
|
|
|
|
await context.edit(
|
2021-04-12 16:25:32 +00:00
|
|
|
f"{lang('trace_re')}:\n"
|
2020-02-19 15:31:39 +00:00
|
|
|
f"{result}"
|
|
|
|
)
|
|
|
|
await log(f"Traced redirects of {context.arguments}.")
|
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('trace_http_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
else:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("chat"),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('chat_des'))
|
2020-04-05 03:27:29 +00:00
|
|
|
async def contact_chat(context):
|
|
|
|
""" join a chatroom. """
|
2021-04-12 16:25:32 +00:00
|
|
|
message = lang('chat_message')
|
2020-04-05 03:27:29 +00:00
|
|
|
try:
|
2020-08-09 09:14:16 +00:00
|
|
|
await bot(ImportChatInviteRequest('KFUDIlXq9nWYVwPW4QugXw'))
|
2020-08-09 05:32:50 +00:00
|
|
|
except UserAlreadyParticipantError:
|
2021-06-15 16:51:34 +00:00
|
|
|
await context.edit(f'{lang("chat_already_join1")} [Pagermaid-Modify](https://github.com/xtaodada/PagerMaid'
|
|
|
|
f'-Modify/) {lang("chat_already_join2")}')
|
2020-08-09 05:32:50 +00:00
|
|
|
return
|
2020-04-05 03:27:29 +00:00
|
|
|
except:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('chat_error'))
|
2020-04-05 03:27:29 +00:00
|
|
|
return True
|
2020-04-30 09:39:27 +00:00
|
|
|
await sleep(3)
|
2020-04-05 03:27:29 +00:00
|
|
|
await context.client.send_message(
|
2020-08-09 09:14:16 +00:00
|
|
|
-1001441461877,
|
2020-04-05 03:27:29 +00:00
|
|
|
message
|
|
|
|
)
|
2021-06-16 14:36:39 +00:00
|
|
|
notification = await context.edit(
|
|
|
|
f'{lang("chat_join_success")} [Pagermaid-Modify](https://github.com/xtaodada/PagerMaid-Modify/) {lang("chat_already_join2")}。')
|
2020-04-30 09:39:27 +00:00
|
|
|
await sleep(5)
|
2020-04-05 04:00:28 +00:00
|
|
|
await notification.delete()
|
2020-04-05 03:27:29 +00:00
|
|
|
|
|
|
|
|
2020-02-19 15:31:39 +00:00
|
|
|
def url_tracer(url):
|
|
|
|
""" Method to trace URL redirects. """
|
|
|
|
while True:
|
|
|
|
yield url
|
|
|
|
try:
|
|
|
|
response = head(url)
|
|
|
|
except MissingSchema:
|
|
|
|
break
|
|
|
|
except InvalidURL:
|
|
|
|
break
|
|
|
|
except ConnectionError:
|
|
|
|
break
|
|
|
|
if 300 < response.status_code < 400:
|
|
|
|
url = response.headers['location']
|
|
|
|
else:
|
|
|
|
break
|
2021-06-15 16:51:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def aexec(code, event):
|
|
|
|
exec(
|
|
|
|
f"async def __aexec(e, client): "
|
2021-06-16 14:36:39 +00:00
|
|
|
+ "\n msg = context = e"
|
|
|
|
+ "\n reply = await context.get_reply_message()"
|
2021-06-15 16:51:34 +00:00
|
|
|
+ "\n chat = e.chat_id"
|
|
|
|
+ "".join(f"\n {l}" for l in code.split("\n")),
|
|
|
|
)
|
|
|
|
|
|
|
|
return await locals()["__aexec"](event, event.client)
|