bbs-telnet/executor.sh

97 lines
2.3 KiB
Bash
Executable File

#!/bin/sh
. ./config
remote_ip=$SOCAT_PEERADDR
die(){
echo 'bye'
pkill -P $PPID
}
mkdir -p -m 700 access_granted
mkdir -p -m 700 access_denied
if [ -f access_denied/"$remote_ip" ]; then exit 0; fi
. ./mastodon_api.sh
session_established=0
generator_session=$(zcat -f $system_dict | shuf -n 2 | tr -d "\n'")
randpadding=$(head -c 8 /dev/urandom | base64)
printf "$randpadding\t$generator_session\n"
rewrite_line() { printf "\r\033[1A\033[0K%s" "$@"; }
while read -r cmd mesg; do
if [ "$cmd" = "reg" ]; then
key=$(echo "$mesg" | cut -d' ' -f 2)
session=$(echo "$mesg" | cut -d' ' -f 1)
if [ "$session" = "$generator_session" ] && [ "$key" = "$pass_for_bbs" ]; then
echo "$generator_session" > access_granted/"$remote_ip"
session_established=1
echo hi $remote_ip
else
die
break
fi
else
if [ "$session_established" -eq 0 ]; then die; break; fi
fi
if [ ! -n "$(grep -F "$generator_session" access_granted/"$remote_ip")" ]; then # Drop unregistred handshake
die
break
fi
# ---
case $cmd in
"post") toot "$mesg" | jq '.id, .errors' ;;
"reply")
reply_id=$(echo "$mesg" | cut -d' ' -f 1)
status=$(echo "$mesg" | cut -d' ' -f 2-)
reply=$reply_id toot "$status" | jq '.id, .errors'
reply=''
;;
"notifs") notifications ;;
"home")
tlhome=$(timeline_home "$mesg")
tlhome_len=$(echo "$tlhome" | wc -l)
current_pos=1
echo "-- tl home view ($tlhome_len lines)--"
while read -r cursor; do
rewrite_line
if [ ':q' = "$cursor" ]; then break; fi
if [ ':all' = "$cursor" ]; then echo "$tlhome"; break; fi
echo "$tlhome" | awk "NR==$current_pos"
current_pos=$(($current_pos + 1))
if [ $current_pos -gt $tlhome_len ]; then break; fi
done
;;
"reg") printf 'bbs > '; continue ;;
"x")
case $mesg in
"http"*) w3m -dump -T text/html -cols 34 -o 'display_link_number=1' "$mesg" ;;
*) echo 'only http(s)' ;;
esac
;;
"testio")
printf 'test io > '
while read -r testinput; do
if [ ':q' = "$testinput" ]; then break; fi
if [ '1M' = "$testinput" ]; then
dd if=/dev/zero bs=1K count=1024 2>&1
else
echo "$testinput" | dd 2>&1
fi
printf 'test io > '
done
;;
"updatebbs")
git pull
echo 'reconnect pls'
break
;;
*) break ;;
esac
printf 'bbs > '
done
rm access_granted/"$remote_ip"
echo 'session was close'