2020-02-19 15:31:39 +00:00
|
|
|
""" System related utilities for PagerMaid to integrate into the system. """
|
|
|
|
|
|
|
|
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
|
2020-04-05 03:27:29 +00:00
|
|
|
from pagermaid import log, bot
|
2020-02-19 15:31:39 +00:00
|
|
|
from pagermaid.listener import listener
|
2021-04-12 16:25:32 +00:00
|
|
|
from pagermaid.utils import attach_log, execute, lang
|
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
|
|
|
|
|
|
|
|
2020-08-09 15:17:55 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, 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
|
|
|
|
|
|
|
|
2020-08-09 15:17:55 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, 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()
|
|
|
|
|
|
|
|
|
2020-08-09 15:17:55 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, 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
|
|
|
|
|
|
|
|
2020-08-09 15:17:55 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, 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-04-12 16:25:32 +00:00
|
|
|
await context.edit(f'{lang("chat_already_join1")} [Pagermaid-Modify](https://github.com/xtaodada/PagerMaid-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-04-12 16:25:32 +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
|