Skip to content

Commit

Permalink
redesigned init + some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Sep 7, 2020
1 parent 6de0a4b commit 4cb39da
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 137 deletions.
61 changes: 1 addition & 60 deletions init/logbot/core/api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ declare -a _allMessages=()

_getResponse() {
if [[ -n $BOT_TOKEN && -n $LOG_CHANNEL_ID ]]; then
local reqType=$1 parse=false; shift
local reqType=${1#api.} parse=false; shift
test ${reqType::4} = "send" && parse=true
local params=$(sed 's/ /\&/g' <<< $*)
test -n $params && params="?$params"
Expand All @@ -39,62 +39,3 @@ _getResponse() {
sleep 0.6
fi
}

_urlEncode() {
echo "<code>$(echo "${1#\~}" | sed -E 's/(\\t)|(\\n)/ /g' |
curl -Gso /dev/null -w %{url_effective} --data-urlencode @- "" | cut -c 3-)</code>"
}

getMessageCount() {
return ${#_allMessages[@]}
}

getAllMessages() {
echo ${_allMessages[@]}
}

getLastMessage() {
if test ${#_allMessages[@]} -gt 0; then
${_allMessages[-1]}.$1 "$2"
elif [[ -n $BOT_TOKEN && -n $LOG_CHANNEL_ID ]]; then
log "first sendMessage ! (caused by \"core.api.$FUNCNAME\")\n"$2""
else
log "$2"
fi
}

getUpdates() {
local params=($*)
_getResponse $FUNCNAME ${params[*]}
}

rawsendMessage() {
local params=(
chat_id=$1
text=$(_urlEncode "$2")
parse_mode=HTML
)
test -n $3 && params+=(reply_to_message_id=$3)
log "$2"
_getResponse ${FUNCNAME#raw} ${params[*]}
}

raweditMessageText() {
local params=(
chat_id=$1
message_id=$2
text=$(_urlEncode "$3")
parse_mode=HTML
)
log "$3"
_getResponse ${FUNCNAME#raw} ${params[*]}
}

rawdeleteMessage() {
local params=(
chat_id=$1
message_id=$2
)
unset _allMessages[$3]
_getResponse ${FUNCNAME#raw} ${params[*]}
}
47 changes: 47 additions & 0 deletions init/logbot/core/apiMethods.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
#
# Copyright (C) 2020 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/uaudith/Userge/blob/master/LICENSE >
#
# All rights reserved.

. init/logbot/core/utils.sh

api.getUpdates() {
local params=($*)
_getResponse $FUNCNAME ${params[*]}
}

api.sendMessage() {
local params=(
chat_id=$1
text=$(urlEncode "$2")
parse_mode=HTML
)
test -n $3 && params+=(reply_to_message_id=$3)
log "$2"
_getResponse $FUNCNAME ${params[*]}
}

api.editMessageText() {
local params=(
chat_id=$1
message_id=$2
text=$(urlEncode "$3")
parse_mode=HTML
)
log "$3"
_getResponse $FUNCNAME ${params[*]}
}

api.deleteMessage() {
local params=(
chat_id=$1
message_id=$2
)
unset _allMessages[$3]
_getResponse $FUNCNAME ${params[*]}
}
14 changes: 14 additions & 0 deletions init/logbot/core/core.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
#
# Copyright (C) 2020 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/uaudith/Userge/blob/master/LICENSE >
#
# All rights reserved.

. init/logbot/core/types/types.sh
. init/logbot/core/api.sh
. init/logbot/core/rawMethods.sh
. init/logbot/core/apiMethods.sh
27 changes: 27 additions & 0 deletions init/logbot/core/rawMethods.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
#
# Copyright (C) 2020 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/uaudith/Userge/blob/master/LICENSE >
#
# All rights reserved.

raw.getMessageCount() {
return ${#_allMessages[@]}
}

raw.getAllMessages() {
echo ${_allMessages[@]}
}

raw.getLastMessage() {
if test ${#_allMessages[@]} -gt 0; then
${_allMessages[-1]}.$1 "$2"
elif [[ -n $BOT_TOKEN && -n $LOG_CHANNEL_ID ]]; then
log "first sendMessage ! (caused by \"core.api.$FUNCNAME\")\n"$2""
else
log "$2"
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
# All rights reserved.

Message() {
. <(sed "s/_Message/$1/g" init/logbot/core/messageClass.sh)
. <(sed "s/_Message/$1/g" init/logbot/core/types/messageClass.sh)
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ $(_Message.id) $(_Message.chat_id) $(_Message.message_id) "$(_Message.text)"
}

_Message.edit() {
raweditMessageText $(_Message.chat_id) $(_Message.message_id) "$1"
api.editMessageText $(_Message.chat_id) $(_Message.message_id) "$1"
_Message.text = "$1"
}

_Message.reply() {
rawsendMessage $(_Message.chat_id) "$1" $(_Message.message_id)
api.sendMessage $(_Message.chat_id) "$1" $(_Message.message_id)
}

_Message.delete() {
rawdeleteMessage $(_Message.chat_id) $(_Message.message_id) $(_Message.id)
api.deleteMessage $(_Message.chat_id) $(_Message.message_id) $(_Message.id)
}
3 changes: 1 addition & 2 deletions init/logbot/core/init.sh → init/logbot/core/types/types.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
#
# All rights reserved.

. init/logbot/core/api.sh
. init/logbot/core/message.sh
. init/logbot/core/types/message.sh
14 changes: 14 additions & 0 deletions init/logbot/core/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
#
# Copyright (C) 2020 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/uaudith/Userge/blob/master/LICENSE >
#
# All rights reserved.

urlEncode() {
echo "<code>$(echo "${1#\~}" | sed -E 's/(\\t)|(\\n)/ /g' |
curl -Gso /dev/null -w %{url_effective} --data-urlencode @- "" | cut -c 3-)</code>"
}
4 changes: 2 additions & 2 deletions init/logbot/logbot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
#
# All rights reserved.

. init/logbot/core/init.sh
. init/logbot/methods/init.sh
. init/logbot/core/core.sh
. init/logbot/methods/methods.sh
12 changes: 6 additions & 6 deletions init/logbot/methods/basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@
# All rights reserved.

sendMessage() {
test -z "$1" || rawsendMessage $LOG_CHANNEL_ID "$1"
test -z "$1" || api.sendMessage $LOG_CHANNEL_ID "$1"
}

replyLastMessage() {
test -z "$1" || getLastMessage reply "$1"
test -z "$1" || raw.getLastMessage reply "$1"
}

editLastMessage() {
test -z "$1" || getLastMessage edit "$1"
test -z "$1" || raw.getLastMessage edit "$1"
}

deleteLastMessage() {
getLastMessage delete
raw.getLastMessage delete
}

deleteMessages() {
getMessageCount
raw.getMessageCount
local count=$(($?))
for ((i=0; i<$count; i++)); do
deleteLastMessage
done
}

printMessages() {
for msg in $(getAllMessages); do
for msg in $(raw.getAllMessages); do
printf "{%s: %s}\n" $msg "$($msg.print)"
done
}
File renamed without changes.
19 changes: 12 additions & 7 deletions init/logbot/methods/polling.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
#
# All rights reserved.

declare -i _to=1
declare -r _input=logs/logbot.stdin

startLogBotPolling() {
test -z $BOT_TOKEN || _polling &
}

endLogBotPolling() {
test -z $BOT_TOKEN || echo quit >> logs/logbot.stdin
test -z $BOT_TOKEN || echo quit >> $_input
}

_polling() {
local cmd func args input=logs/logbot.stdin
rm -f $input
local cmd func args
_resetConnection
log "LogBot Polling Started !"
while true; do
cmd="$(head -n 1 $input 2> /dev/null && sed -i '1d' $input)"
cmd="$(head -n 1 $_input 2> /dev/null && sed -i '1d' $_input)"
test -z "$cmd" && _pollsleep && continue
test $_to -gt 3 && let _to-=3
case $cmd in
Expand All @@ -39,12 +42,14 @@ _polling() {
esac
sleep 1
done
log "LogBot Polling Ended with SIGTERM !"
rm -f $input
log "LogBot Polling Ended !"
_resetConnection
exit 0
}

declare -i _to=1
_resetConnection() {
rm -f $_input
}

_pollsleep() {
let _to+=1
Expand Down
14 changes: 7 additions & 7 deletions init/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ installReq() {
}

printLine() {
echo ========================================================
echo '-<- -<- -<- -<- -<- -<- -<- -<- -<- -<- -<- -<- -<- -<- -<-'
}

printLogo() {
printLine
echo '
_ _ ____ _____ ____ ____ _____ ____ _ _ _____
| | | / ___|| ____| _ \ / ___| ____|/ __ \| | | |_ _|
| | | \___ \| _| | |_) | | _| _| / / _` | | | | | |
| |_| |___) | |___| _ <| |_| | |__| | (_| | |_| | | |
\___/|____/|_____|_| \_\\____|_____\ \__,_|\___/ |_|
\____/
________ __ __ ______
/_ __/ /_ ___ / / / /_______ _____/ ____/__
/ / / __ \/ _ \ / / / / ___/ _ \/ ___/ / __/ _ \
/ / / / / / __/ / /_/ (__ ) __/ / / /_/ / __/
/_/ /_/ /_/\___/ \____/____/\___/_/ \____/\___/
'
printLine
}
4 changes: 2 additions & 2 deletions userge/core/methods/utils/restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ async def restart(self, update_req: bool = False) -> None: # pylint: disable=ar
for handler in c_p.open_files() + c_p.connections():
os.close(handler.fd)
except Exception as c_e: # pylint: disable=broad-except
_LOG.error(_LOG_STR, c_e)
print(_LOG_STR % c_e)
if update_req:
_LOG.info(_LOG_STR, "Installing Requirements...")
print(_LOG_STR % "Installing Requirements...")
os.system("pip3 install -U pip && pip3 install -r requirements.txt") # nosec
os.execl(sys.executable, sys.executable, '-m', 'userge') # nosec
sys.exit()
2 changes: 1 addition & 1 deletion userge/plugins/tools/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def aexec(code):
if '\n' in code:
rest_code = '\n '.join(line for line in code.split('\n'))
elif any(True for k_ in keyword.kwlist
if k_ not in ('True', 'False', 'None') and code.startswith(k_)):
if k_ not in ('True', 'False', 'None') and code.startswith(f"{k_} ")):
rest_code = f"\n {code}"
else:
rest_code = f"\n return {code}"
Expand Down
Loading

0 comments on commit 4cb39da

Please sign in to comment.