bbs-telnet/executor.sh

92 lines
2.2 KiB
Bash
Raw Normal View History

2023-07-23 01:07:35 +03:00
#!/bin/sh
2023-07-21 20:56:12 +03:00
. ./config
remote_ip=$SOCAT_PEERADDR
die(){
2023-07-21 22:18:04 +03:00
echo 'bye'
pkill -P $PPID
2023-07-21 20:56:12 +03:00
}
mkdir -p -m 700 access_granted
2023-07-24 01:08:09 +03:00
mkdir -p -m 700 access_denied
if [ -f access_denied/"$remote_ip" ]; then exit 0; fi
2023-07-24 02:26:39 +03:00
. ./mastodon_api.sh
2023-07-24 01:08:09 +03:00
session_established=0
2023-07-21 20:56:12 +03:00
generator_session=$(zcat -f $system_dict | shuf -n 2 | tr -d "\n'")
randpadding=$(head -c 8 /dev/urandom | base64)
2023-07-24 15:11:08 +03:00
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
2023-07-24 12:24:40 +03:00
echo "$generator_session" > access_granted/"$remote_ip"
session_established=1
echo hi $remote_ip
else
2023-07-21 20:56:12 +03:00
die
2023-07-21 22:18:04 +03:00
break
2023-07-21 20:56:12 +03:00
fi
else
if [ "$session_established" -eq 0 ]; then die; break; fi
fi
2023-07-24 12:24:40 +03:00
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=''
;;
2023-07-24 16:37:34 +03:00
"notifs") notifications ;;
2023-07-24 11:17:48 +03:00
"home")
tlhome=$(timeline_home "$mesg")
tlhome_len=$(echo "$tlhome" | wc -l)
current_pos=1
2023-07-24 15:11:08 +03:00
echo "-- tl home view ($tlhome_len lines)--"
2023-07-24 11:17:48 +03:00
while read -r cursor; do
2023-07-24 15:11:08 +03:00
rewrite_line
2023-07-24 11:17:48 +03:00
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
;;
2023-07-23 01:07:35 +03:00
"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
;;
2023-07-24 12:24:40 +03:00
*) break ;;
esac
printf 'bbs > '
done
2023-07-21 20:56:12 +03:00
rm access_granted/"$remote_ip"
echo 'session was close'