Skip to content

Commit

Permalink
Trying sudo (base)
Browse files Browse the repository at this point in the history
  • Loading branch information
xditya committed Sep 7, 2020
1 parent f24d82e commit c93afb2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
7 changes: 6 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@
"value": "",
"required": false
},
"CMD_HNDR": {
"CMD_HNDLR": {
"description": "Used to invoke commands, leave blank for default, (dot)",
"value": "",
"required": false
},
"SUDO_HNDLR": {
"description": "Used to invoke sudo commands, leave blank for default",
"value": "",
"required": false
},
"GIT_REPO_NAME": {
"description": "Your repo name Example: xditya/TeleBot, but GITHUB_ACCESS_TOKEN must be setup first.",
Expand Down
7 changes: 4 additions & 3 deletions userbot/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import os
import userbot.utils
from datetime import datetime
from userbot.utils import admin_cmd

DELETE_TIMEOUT = 5
thumb_image_path = "./TeleBot.png"

@command(pattern="^.install", outgoing=True)
@telebot.on(admin_cmd(pattern="install", outgoing=True))
async def install(event):
if event.fwd_from:
return
Expand All @@ -32,7 +33,7 @@ async def install(event):
await asyncio.sleep(DELETE_TIMEOUT)
await event.delete()

@command(pattern="^.unload (?P<shortname>\w+)$", outgoing=True)
@telebot.on(admin_cmd(pattern="unload (?P<shortname>\w+)", outgoing=True))
async def unload(event):
if event.fwd_from:
return
Expand All @@ -43,7 +44,7 @@ async def unload(event):
except Exception as e:
await event.edit("TeleBot has successfully unloaded {shortname}\n{}".format(shortname, str(e)))

@command(pattern="^.load (?P<shortname>\w+)$", outgoing=True)
@telebot.on(admin_cmd(pattern="load (?P<shortname>\w+)", outgoing=True))
async def load(event):
if event.fwd_from:
return
Expand Down
1 change: 1 addition & 0 deletions userbot/uniborgConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Config(object):
# specify command handler that should be used for the plugins
# this should be a valid "regex" pattern
CMD_HNDLR = os.environ.get("CMD_HNDLR", "\.")
SUDO_HNDLR = os.environ.get("SUDO_HNDLR", "\^")
# specify list of users allowed to use bot
# WARNING: be careful who you grant access to your bot.
# malicious users could do ".exec rm -rf /*"
Expand Down
53 changes: 53 additions & 0 deletions userbot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,56 @@ def __init__(self, func=None, **args):
self.Var = Var
bot.add_event_handler(func, events.NewMessage(**args))


def sudo_cmd(pattern=None, **args):
args["func"] = lambda e: e.via_bot_id is None

stack = inspect.stack()
previous_stack_frame = stack[1]
file_test = Path(previous_stack_frame.filename)
file_test = file_test.stem.replace(".py", "")
allow_sudo = args.get("allow_sudo", False)

# get the pattern from the decorator
if pattern is not None:
if pattern.startswith("\#"):
# special fix for snip.py
args["pattern"] = re.compile(pattern)
else:
args["pattern"] = re.compile(sudo_hndlr + pattern)
cmd = sduo_hndlr + pattern
try:
CMD_LIST[file_test].append(cmd)
except:
CMD_LIST.update({file_test: [cmd]})

args["outgoing"] = True
# should this command be available for other users?
if allow_sudo:
args["from_users"] = list(Var.SUDO_USERS)
# Mutually exclusive with outgoing (can only set one of either).
args["incoming"] = True
del args["allow_sudo"]

# error handling condition check
elif "incoming" in args and not args["incoming"]:
args["outgoing"] = True

# add blacklist chats, UB should not respond in these chats
allow_edited_updates = False
if "allow_edited_updates" in args and args["allow_edited_updates"]:
allow_edited_updates = args["allow_edited_updates"]
del args["allow_edited_updates"]

# check if the plugin should listen for outgoing 'messages'
is_message_enabled = True

return events.NewMessage(**args)

async def edit_or_reply(event, text):
if event.from_id in Config.SUDO_USERS:
reply_to = await event.get_reply_message()
if reply_to:
return await reply_to.reply(text)
return await event.reply(text)
return await event.edit(text)

0 comments on commit c93afb2

Please sign in to comment.