shell script for file-based bbs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bbsh/bbsh

173 lines
3.5 KiB

#!/bin/sh
# (c) 2019 Vincenzo 'KatolaZ' Nicosia <katolaz@freaknet.org>
# MIT License
BDIR="/var/bbs/boards"
######
NUMW='3'
BRDW='18'
TITW='50'
TRDW='12'
TOTW="$(echo "$NUMW+$BRDW+$TITW+$TRDW" | bc)"
HR="------------------------------------------------------------------------------\n"
#set -x
######
PROMPT='COMMAND :> '
DFLTBOARD=""
DFLTPAGER=${PAGER:-"less"}
CURBOARD=${1:-"$DFLTBOARD"}
##function
__prompt(){
printf '[%s] %s' "$CURBOARD" "$PROMPT"
}
##function
__board_prompt(){
printf "%s" "Go to which board? (use index) "
}
##function
__thread_prompt(){
printf "%s" "Read which thread? "
}
##function
select_board(){
__board_prompt
read -r board
NEW_BOARD="$(ls "$BDIR" | tail -n +"$board" | head -1)"
[ -z "${NEW_BOARD}" ] && echo "invalid board number" && return 1
return 0
}
##function
get_boards(){
echo $(ls "${BDIR}")
}
##function
show_boards(){
printf -- "$HR"
for b in $(get_boards); do
t="$(head -1 "$BDIR/$b"/topic | sed -E 's/^(.{50}).*/\1/g')"
n="$(ls "$BDIR/$b" | grep -E "^[0-9]+" | wc -l | sed -E 's/ //g')"
printf "%-"$BRDW"s %-"$TITW"s [ %2s boards ]\n" "$b" "$t" "$n"
done | sort | nl -v1 -w3 -s " "
printf -- "$HR"
}
##function
show_help(){
printf '%s\n' \
' [Bulletin Boards Commands]'\
' ----------------------------------------------------------'\
' (l)ist ......... List all available boards'\
' (g)oto ......... Goto a board by name or number'\
' (h)elp ......... Show this help message'\
' (m)essages...... List all messages in current board'\
' (t)ype ......... Display the contents of a message thread'\
' (q)uit ......... Quit bbsh'
}
##function
print_thread_title(){
date="$(echo "$(basename $1)" | cut -d "-" -f 1 )"
author="$(echo "$(basename $1)" | cut -d "-" -f 2)"
subject="$(head -1 $1/subject | sed -E 's/^(.{50}).*/\1/g')"
posts="$(ls $1 | grep -v '^subject$' | wc -l | sed -E 's/\ +//g')"
printf "%-10s %-16s %-"$TITW"s [ %2s posts ]\n" "$date" "$author" "$subject" "$posts"
}
##function
list_board_messages(){
[ -z "$CURBOARD" ] && {
printf "%s\n" "Not at any board! Hit `l` to list boards, `g` to go to one."
return
}
printf -- "$HR"
for i in $(ls $BDIR/$CURBOARD | sort -rn | grep -v "^topic") ; do
print_thread_title "$BDIR/$CURBOARD/$i"
done | sort -rk1 | nl -w3 -v1 -s" "
printf -- "$HR"
}
##function
type_message(){
subject="$2"
author="$(echo $(basename $1) | cut -d "-" -f 2)"
date="$(echo $(basename $1) | cut -d "-" -f 1)"
printf -- "--------------------------\n"
printf "SUBJECT: %s\n" "$subject"
printf "AUTHOR: %s\n" "$author"
printf "POSTED: %s\n" "$date"
printf -- "--------------------------\n"
cat $1
}
##function
type_thread(){
[ -z "$CURBOARD" ] && {
printf "%s\n" "Not at any board! Hit `l` to list boards, `g` to go to one."
return
}
__thread_prompt
read -r thread
THREAD="$(ls "$BDIR/$CURBOARD" | sort -rn | grep -v "^topic$" | tail -n +"$thread" | head -1)"
[ -z "${THREAD}" ] && echo "Invalid thread index!" && return 1
subject="$(head -1 "$BDIR/$CURBOARD/$THREAD/subject")"
for i in $(ls "$BDIR/$CURBOARD/$THREAD" | sort -n | grep -v "^subject$"); do
type_message "$BDIR/$CURBOARD/$THREAD/$i" "$subject"
done | ${DFLTPAGER}
} ##function
read_cmd(){
__prompt
while read -r cmd; do
case $cmd in
g)
select_board
[ $? -eq 0 ] && exec $0 ${NEW_BOARD}
;;
h)
show_help
;;
l)
show_boards
;;
q)
exit 0
;;
m)
list_board_messages
;;
t)
type_thread
;;
*)
show_boards
;;
esac
__prompt
done
}
show_boards
read_cmd