bbs-telnet/executor.sh

84 lines
2.0 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
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"
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"
echo hi $remote_ip
else
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=''
;;
"home")
tlhome=$(timeline_home "$mesg")
tlhome_len=$(echo "$tlhome" | wc -l)
current_pos=1
while read -r cursor; do
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
toot $full_status | jq '.id, .errors'
;;
*) break ;;
esac
printf 'bbs > '
done
rm access_granted/"$remote_ip"
echo 'session was close'