2020-12-28 03:34:15 +01:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Hard requirement for lighttpd
|
2020-12-28 14:42:08 +01:00
|
|
|
|
printf "Content-Type: text/plain\n\n"
|
2020-12-25 20:57:07 +01:00
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Initialize basic variables
|
2020-12-28 14:42:08 +01:00
|
|
|
|
. /etc/konata.conf
|
2020-12-25 20:57:07 +01:00
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Parse incoming messages
|
2021-01-04 16:39:50 +01:00
|
|
|
|
input=$(cat /dev/stdin)
|
2021-01-04 19:09:09 +01:00
|
|
|
|
msg=$(jq -r '.message.text' <<< $input)
|
2021-01-09 06:08:33 +01:00
|
|
|
|
cmd=$(cut -d" " -f1 <<< $msg)
|
2021-01-04 19:09:09 +01:00
|
|
|
|
args=$(cut -d" " -f2- <<< $msg)
|
2020-12-25 20:57:07 +01:00
|
|
|
|
chat=$(jq -r '.message.chat.id' <<< $input)
|
2020-12-26 01:08:36 +01:00
|
|
|
|
user=$(jq -r '.message.from.username' <<< $input)
|
2020-12-25 20:57:07 +01:00
|
|
|
|
|
2021-01-04 19:09:09 +01:00
|
|
|
|
# Append bot's tag to any command without one
|
2021-01-09 06:08:33 +01:00
|
|
|
|
if ! grep -q "/.*@" <<< $cmd
|
2021-01-04 19:09:09 +01:00
|
|
|
|
then
|
2021-01-09 06:08:33 +01:00
|
|
|
|
cmd=${cmd}@$bot
|
|
|
|
|
fi
|
|
|
|
|
# Convert k.command to /command
|
|
|
|
|
if grep -q "k\." <<< $cmd
|
|
|
|
|
then
|
|
|
|
|
cmd=$(sed "s/k\./\//g" <<< $cmd)
|
2021-01-04 19:09:09 +01:00
|
|
|
|
fi
|
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Functions for outgoing messages
|
2020-12-25 20:57:07 +01:00
|
|
|
|
sendmsg(){
|
2020-12-26 12:32:10 +01:00
|
|
|
|
# Escape any special characters first, see https://0x0.st/NO7J.
|
2021-01-06 19:41:18 +01:00
|
|
|
|
fmt=$(sed 's/[>#+-=|{}.!]/\\&/g' <<< $1 | sed 's/\t//g')
|
2020-12-31 17:53:29 +01:00
|
|
|
|
sendraw "$fmt"
|
|
|
|
|
}
|
2021-01-07 17:00:52 +01:00
|
|
|
|
escape(){
|
|
|
|
|
sed "s/[$1]/\\\&/g" <<< $2
|
|
|
|
|
}
|
2020-12-31 17:53:29 +01:00
|
|
|
|
sendraw(){
|
2021-01-18 15:43:12 +01:00
|
|
|
|
curl -sX POST "https://api.telegram.org/bot$token/sendMessage" \
|
2020-12-31 17:53:29 +01:00
|
|
|
|
-d "chat_id=$chat" -d "parse_mode=markdownv2" \
|
|
|
|
|
-d "disable_web_page_preview=true" -d "text=$1"
|
2020-12-25 20:57:07 +01:00
|
|
|
|
}
|
2021-01-13 18:23:15 +01:00
|
|
|
|
sendlog(){
|
|
|
|
|
chat=$logchat
|
|
|
|
|
sendmsg "$1"
|
|
|
|
|
}
|
2020-12-31 22:16:44 +01:00
|
|
|
|
sendfile(){
|
|
|
|
|
curl -sF document=@"$1" "https://api.telegram.org/bot$token/sendDocument?chat_id=$chat"
|
|
|
|
|
}
|
2021-01-09 08:46:22 +01:00
|
|
|
|
sendpic(){
|
2021-01-18 15:43:12 +01:00
|
|
|
|
curl -sX POST "https://api.telegram.org/bot$token/sendPhoto" \
|
2021-01-09 08:46:22 +01:00
|
|
|
|
-d "chat_id=$chat" -d "parse_mode=markdownv2" \
|
|
|
|
|
-d "photo=$1" -d "caption=$2"
|
|
|
|
|
}
|
2021-01-24 08:42:06 +01:00
|
|
|
|
|
|
|
|
|
# Debugging commands
|
2021-01-18 16:03:26 +01:00
|
|
|
|
debug(){
|
|
|
|
|
[ ! -z $debug ] && echo "[DEBUG] $1" >&2
|
|
|
|
|
}
|
2020-12-25 20:57:07 +01:00
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Public commands
|
2020-12-27 13:17:31 +01:00
|
|
|
|
help(){
|
2021-01-23 17:02:18 +01:00
|
|
|
|
sendmsg "Hiya! I'm [ghnou](https://t.me/ghnou)'s personal bot, Konata.
|
2020-12-27 13:17:31 +01:00
|
|
|
|
Currently I have the following features:
|
2020-12-31 22:16:44 +01:00
|
|
|
|
- /help - Show this help message.
|
2020-12-27 13:17:31 +01:00
|
|
|
|
- /alive - Check whether I'm running.
|
2021-01-06 20:11:22 +01:00
|
|
|
|
- /cv \[country\] - Check COVID-19 stats.
|
2020-12-27 13:17:31 +01:00
|
|
|
|
- /id - Get the current chat's ID.
|
2021-01-06 20:11:22 +01:00
|
|
|
|
- /ud \[term\] - Get a definition from Urban Dictionary.
|
2021-01-09 08:46:22 +01:00
|
|
|
|
- /konata - Get one of my selfies :3
|
2021-01-23 17:00:47 +01:00
|
|
|
|
- /repo - Get my source code.
|
2020-12-27 13:17:31 +01:00
|
|
|
|
Admin only commands:
|
2021-01-06 20:11:22 +01:00
|
|
|
|
- /mp3 \[link\] - Download an mp3 file from YouTube.
|
2020-12-27 13:17:31 +01:00
|
|
|
|
- /stat - Uptime, memory, storage stats.
|
|
|
|
|
- /term - Execute a command on the system.
|
|
|
|
|
|
|
|
|
|
And a lot more to come!"
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 20:57:07 +01:00
|
|
|
|
alive(){
|
2020-12-26 01:08:36 +01:00
|
|
|
|
sendmsg "Alive and well!"
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
cv(){
|
2021-01-04 16:26:59 +01:00
|
|
|
|
stats=$(curl -s "https://corona.lmao.ninja/v2/countries/$args")
|
2020-12-31 21:23:34 +01:00
|
|
|
|
country=$(jq -r '.country' <<< $stats)
|
|
|
|
|
cases=$(jq -r '.todayCases' <<< $stats)
|
2021-01-13 17:48:40 +01:00
|
|
|
|
pm=$(jq -r '.testsPerOneMillion' <<< $stats)
|
|
|
|
|
pct=$(echo "scale = 2; $pm / 10000" | bc)
|
2021-01-04 16:26:59 +01:00
|
|
|
|
if jq -re '.country' <<< $stats
|
2021-01-01 00:03:16 +01:00
|
|
|
|
then
|
2021-01-13 17:48:40 +01:00
|
|
|
|
sendmsg "Today's COVID-19 cases for $country: $cases \($pct% of the population tested\). Stay safe!"
|
2021-01-04 16:26:59 +01:00
|
|
|
|
else
|
|
|
|
|
sendmsg "This country doesn't have any stats!"
|
2021-01-01 00:03:16 +01:00
|
|
|
|
fi
|
2020-12-25 20:57:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
id(){
|
|
|
|
|
sendmsg "Chat ID: $chat"
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 15:51:53 +01:00
|
|
|
|
ud(){
|
|
|
|
|
enc=$(echo "$args" | sed "s/\ /%20/g")
|
2021-01-18 15:43:12 +01:00
|
|
|
|
res=$(curl -s "https://api.urbandictionary.com/v0/define?term=$enc")
|
2021-01-04 16:26:59 +01:00
|
|
|
|
if jq -re '.list[0].definition' <<< $res
|
|
|
|
|
then
|
|
|
|
|
def=$(jq -r '.list[0].definition' <<< $res | sed "s/[][]//g")
|
|
|
|
|
sam=$(jq -r '.list[0].example' <<< $res | sed "s/[][]//g")
|
2021-01-06 19:52:20 +01:00
|
|
|
|
sendmsg "*Definition for __${args}__:*
|
2021-01-07 17:00:52 +01:00
|
|
|
|
$(escape "()" "$def")
|
2021-01-06 19:52:20 +01:00
|
|
|
|
*Example:*
|
2021-01-07 17:00:52 +01:00
|
|
|
|
$(escape "()" "$sam")"
|
2021-01-04 16:26:59 +01:00
|
|
|
|
else
|
|
|
|
|
sendmsg "No definition found for $args."
|
|
|
|
|
fi
|
2021-01-04 15:51:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 08:46:22 +01:00
|
|
|
|
konata(){
|
2021-01-18 15:43:12 +01:00
|
|
|
|
data=$(curl -s "https://konachan.net/post.json?limit=1&page=1&tags=order:random%20izumi_konata")
|
2021-01-09 08:46:22 +01:00
|
|
|
|
jpeg=$(jq -r '.[0].jpeg_url' <<< $data)
|
|
|
|
|
file=$(jq -r '.[0].file_url' <<< $data)
|
|
|
|
|
sfw=$(jq -r '.[0].rating' <<< $data)
|
|
|
|
|
if [ $sfw == "s" ]
|
|
|
|
|
then
|
|
|
|
|
sendpic "$jpeg" "[sauce]($file)"
|
|
|
|
|
else
|
|
|
|
|
sendmsg "This picture appears to be NSFW! Here's the [sauce]($file)."
|
|
|
|
|
fi
|
|
|
|
|
}
|
2021-01-19 16:07:01 +01:00
|
|
|
|
repo(){
|
|
|
|
|
sendmsg "You can find my source code [here](https://git.ghnou.su/ghnou/konata)."
|
|
|
|
|
}
|
2021-01-09 08:46:22 +01:00
|
|
|
|
|
2021-01-14 22:14:57 +01:00
|
|
|
|
# Administrative
|
2020-12-31 22:16:44 +01:00
|
|
|
|
mp3(){
|
2021-01-01 00:32:33 +01:00
|
|
|
|
admin
|
2020-12-31 22:16:44 +01:00
|
|
|
|
tmp="/tmp/mp3"
|
|
|
|
|
[ -d $tmp ] && mkdir $tmp
|
|
|
|
|
sendmsg "Downloading, this might take a while..."
|
2021-01-02 17:42:10 +01:00
|
|
|
|
youtube-dl -f 140 --max-filesize 50M -o "$tmp/%(title)s.%(ext)s" "$args"
|
|
|
|
|
rm $tmp/*.part
|
|
|
|
|
if [ -f $tmp/* ]
|
|
|
|
|
then
|
|
|
|
|
sendfile "$tmp/$(ls $tmp)"
|
|
|
|
|
rm $tmp/*
|
|
|
|
|
else
|
|
|
|
|
sendmsg "I couldn't download this file. Perhaps it's too large?"
|
|
|
|
|
fi
|
2020-12-31 22:16:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 20:57:07 +01:00
|
|
|
|
stat(){
|
2020-12-26 12:32:10 +01:00
|
|
|
|
admin
|
2020-12-25 20:57:07 +01:00
|
|
|
|
total=$(grep MemTotal /proc/meminfo | awk '{print $2}')
|
|
|
|
|
avail=$(grep MemAvailable /proc/meminfo | awk '{print $2}')
|
|
|
|
|
used=$(bc <<< "$total-$avail")
|
2020-12-26 12:32:10 +01:00
|
|
|
|
sendmsg "$(uptime | cut -c2-)
|
|
|
|
|
I am using ${used}K of memory and \
|
|
|
|
|
$(df -h / | tail -n1 | awk '{print $3}') of storage."
|
2020-12-26 01:08:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-27 13:17:31 +01:00
|
|
|
|
term(){
|
|
|
|
|
admin
|
2021-01-08 02:50:16 +01:00
|
|
|
|
sendmsg "\`$(whoami)@${HOSTNAME}:${PWD}$\` \`$args\`
|
|
|
|
|
\`$(echo $args | bash - 2>&1)\`"
|
2020-12-27 13:17:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
fail(){
|
2021-01-18 16:03:26 +01:00
|
|
|
|
debug "User @$user tried to execute $cmd and was rejected."
|
2020-12-31 21:23:34 +01:00
|
|
|
|
sendmsg "Sorry, you are not allowed to use this command!"
|
2021-01-01 00:37:39 +01:00
|
|
|
|
exit 1
|
2020-12-26 01:08:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
admin(){
|
|
|
|
|
case $user in
|
2021-01-18 16:03:26 +01:00
|
|
|
|
ghnou) true ;;
|
2020-12-31 21:23:34 +01:00
|
|
|
|
*) fail ;;
|
|
|
|
|
esac
|
2021-01-18 16:03:26 +01:00
|
|
|
|
debug "Admin @$user tried to execute $cmd and was approved."
|
2020-12-25 20:57:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Notes and such
|
2020-12-31 17:53:29 +01:00
|
|
|
|
anyone(){
|
2021-01-06 20:00:01 +01:00
|
|
|
|
sendmsg "Quite possibly. [Why do you ask?](https://dontasktoask.com)"
|
2020-12-31 17:53:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Miscellaneous
|
2020-12-27 13:26:26 +01:00
|
|
|
|
tag(){
|
|
|
|
|
sendmsg "Please stand by, he'll get to your message soon™️..."
|
2021-01-13 18:29:02 +01:00
|
|
|
|
sendlog "You've been tagged: $msg"
|
2020-12-27 13:26:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Match incoming messages
|
2020-12-25 20:57:07 +01:00
|
|
|
|
case "$cmd" in
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Public commands
|
2021-01-04 19:09:09 +01:00
|
|
|
|
/help@$bot) help ;;
|
|
|
|
|
/start@$bot) help ;;
|
|
|
|
|
/alive@$bot) alive ;;
|
|
|
|
|
/cv@$bot*) cv ;;
|
|
|
|
|
/id@$bot) id ;;
|
|
|
|
|
/ud@$bot*) ud ;;
|
2021-01-09 08:46:22 +01:00
|
|
|
|
/konata@$bot*) konata ;;
|
2021-01-19 16:07:01 +01:00
|
|
|
|
/repo@$bot) repo ;;
|
2021-01-14 22:14:57 +01:00
|
|
|
|
# Administrative
|
2021-01-04 19:09:09 +01:00
|
|
|
|
/mp3@$bot*) mp3 ;;
|
|
|
|
|
/stat@$bot) stat ;;
|
|
|
|
|
/term@$bot*) term ;;
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Notes and such
|
2021-01-06 20:00:01 +01:00
|
|
|
|
!anyone*) anyone ;;
|
2021-02-04 19:36:56 +01:00
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
case "$msg" in
|
2020-12-31 21:23:34 +01:00
|
|
|
|
# Miscellaneous
|
2020-12-27 13:26:26 +01:00
|
|
|
|
*@ghnou*) tag ;;
|
2020-12-25 20:57:07 +01:00
|
|
|
|
esac
|