Add argument checks

This commit is contained in:
Michael De Roover 2021-03-01 11:16:26 +01:00
parent 80bb3deceb
commit 072e2b51a0
Signed by: vim
GPG Key ID: 075496E232CE04CB

View File

@ -31,28 +31,56 @@ sendmsg(){
fmt=$(sed 's/[>#+-=|{}.!]/\\&/g' <<< $1 | sed 's/\t//g') fmt=$(sed 's/[>#+-=|{}.!]/\\&/g' <<< $1 | sed 's/\t//g')
sendraw "$fmt" sendraw "$fmt"
} }
escape(){ escape(){
sed "s/[$1]/\\\&/g" <<< $2 sed "s/[$1]/\\\&/g" <<< $2
} }
sendraw(){ sendraw(){
curl -sX POST "https://api.telegram.org/bot$token/sendMessage" \ curl -sX POST "https://api.telegram.org/bot$token/sendMessage" \
-d "chat_id=$chat" -d "parse_mode=markdownv2" \ -d "chat_id=$chat" -d "parse_mode=markdownv2" \
-d "disable_web_page_preview=true" \ -d "disable_web_page_preview=true" \
--data-urlencode "text=$1" --data-urlencode "text=$1"
} }
sendlog(){ sendlog(){
chat=$logchat chat=$logchat
sendmsg "$1" sendmsg "$1"
} }
sendfile(){ sendfile(){
curl -sF document=@"$1" "https://api.telegram.org/bot$token/sendDocument?chat_id=$chat" curl -sF document=@"$1" "https://api.telegram.org/bot$token/sendDocument?chat_id=$chat"
} }
sendpic(){ sendpic(){
curl -sX POST "https://api.telegram.org/bot$token/sendPhoto" \ curl -sX POST "https://api.telegram.org/bot$token/sendPhoto" \
-d "chat_id=$chat" -d "parse_mode=markdownv2" \ -d "chat_id=$chat" -d "parse_mode=markdownv2" \
-d "photo=$1" -d "caption=$2" -d "photo=$1" -d "caption=$2"
} }
# Internal checks
args(){
if [ -z ${args[*]} ]
then
sendmsg "Don't just click me like that. See /help@$bot for more info on how to use this bot."
exit 1
fi
}
admin(){
case $user in
ghnou) true ;;
*) fail ;;
esac
debug "Admin @$user tried to execute $cmd and was approved."
}
fail(){
debug "User @$user tried to execute $cmd and was rejected."
sendmsg "Sorry, you are not allowed to use this command!"
exit 1
}
# Debugging commands # Debugging commands
debug(){ debug(){
[ ! -z $debug ] && echo "[DEBUG] $1" >&2 [ ! -z $debug ] && echo "[DEBUG] $1" >&2
@ -86,6 +114,7 @@ alive(){
} }
cv(){ cv(){
args
stats=$(curl -s "https://corona.lmao.ninja/v2/countries/${args[*]}") stats=$(curl -s "https://corona.lmao.ninja/v2/countries/${args[*]}")
mapfile -t cv <<< $(jq -cr '.[]' <<< $stats) mapfile -t cv <<< $(jq -cr '.[]' <<< $stats)
if [ "${cv[4]}" == "0" ] if [ "${cv[4]}" == "0" ]
@ -114,6 +143,7 @@ id(){
} }
ud(){ ud(){
args
enc=$(echo "${args[*]}" | sed "s/\ /%20/g") enc=$(echo "${args[*]}" | sed "s/\ /%20/g")
res=$(curl -s "https://api.urbandictionary.com/v0/define?term=$enc") res=$(curl -s "https://api.urbandictionary.com/v0/define?term=$enc")
if jq -re '.list[0].definition' <<< $res if jq -re '.list[0].definition' <<< $res
@ -135,6 +165,7 @@ konata(){
} }
konachan(){ konachan(){
args
query=$(sed "s/\ /_/g" <<< ${args[*]}) query=$(sed "s/\ /_/g" <<< ${args[*]})
data=$(curl -s "https://konachan.net/post.json?limit=1&page=1&tags=order:random%20$query") data=$(curl -s "https://konachan.net/post.json?limit=1&page=1&tags=order:random%20$query")
jpeg=$(jq -r '.[0].jpeg_url' <<< $data) jpeg=$(jq -r '.[0].jpeg_url' <<< $data)
@ -152,6 +183,7 @@ konachan(){
} }
4c(){ 4c(){
args
tmp="/tmp/4chan" tmp="/tmp/4chan"
json="$tmp/${args[0]}.json" json="$tmp/${args[0]}.json"
[ ! -d $tmp ] && mkdir $tmp [ ! -d $tmp ] && mkdir $tmp
@ -166,25 +198,24 @@ konachan(){
} }
lfy(){ lfy(){
args
query=$(sed "s/\ /+/g" <<< ${args[*]}) query=$(sed "s/\ /+/g" <<< ${args[*]})
sendmsg "[Let me look that up for you](https://lmgtfy.app/?s=d&iee=1&q=$query)" sendmsg "[Let me look that up for you](https://lmgtfy.app/?s=d&iee=1&q=$query)"
} }
ip(){ ip(){
if [ ! -z ${args[0]} ] args
then ip=${args[0]}
ip=${args[0]} res=$(curl -s http://ip-api.com/json/$ip)
res=$(curl -s http://ip-api.com/json/$ip) # See https://0x0.st/Nlc0 for reference.
# See https://0x0.st/Nlc0 for reference. mapfile -t data <<< $(jq -cr '.[]' <<< $res)
mapfile -t data <<< $(jq -cr '.[]' <<< $res) sendmsg "*IP information for $ip:*
sendmsg "*IP information for $ip:* *Country:* ${data[1]}
*Country:* ${data[1]} *Region:* ${data[4]}
*Region:* ${data[4]} *City:* ${data[6]} ${data[5]}
*City:* ${data[6]} ${data[5]} *ISP:* ${data[10]}
*ISP:* ${data[10]} *ASN:* ${data[12]}
*ASN:* ${data[12]} *Time:* $(TZ=${data[9]} date +'%H:%M %Z')"
*Time:* $(TZ=${data[9]} date +'%H:%M %Z')"
fi
} }
repo(){ repo(){
@ -194,6 +225,7 @@ repo(){
# Administrative # Administrative
mp3(){ mp3(){
admin admin
args
tmp="/tmp/mp3" tmp="/tmp/mp3"
[ ! -d $tmp ] && mkdir $tmp [ ! -d $tmp ] && mkdir $tmp
sendmsg "Downloading, this might take a while..." sendmsg "Downloading, this might take a while..."
@ -224,20 +256,6 @@ term(){
\`$(echo ${args[*]} | bash - 2>&1)\`" \`$(echo ${args[*]} | bash - 2>&1)\`"
} }
fail(){
debug "User @$user tried to execute $cmd and was rejected."
sendmsg "Sorry, you are not allowed to use this command!"
exit 1
}
admin(){
case $user in
ghnou) true ;;
*) fail ;;
esac
debug "Admin @$user tried to execute $cmd and was approved."
}
# Notes and such # Notes and such
anyone(){ anyone(){
sendmsg "Quite possibly. [Why do you ask?](https://dontasktoask.com)" sendmsg "Quite possibly. [Why do you ask?](https://dontasktoask.com)"