# bbs/chat - interactive chat client
# usage: chat [room]
#
# Commands:
#   /read     - show recent messages
#   /users    - show who's online
#   /quit     - exit chat
#   anything else - send as message

bbs=/n/bbs

if (test ! -d $bbs) {
    echo 'BBS not connected. Run: bbs/connect'
    exit 1
}

room=lobby

if (test $#* -gt 0) {
    room=$1
}

echo '--- Chat: '$room' ---'
echo '/read=refresh  /users=who  /quit=exit'
echo ''

# Show recent messages on entry
cat $bbs/chat/$room

while (read line) {
    if (~ $line /quit) { exit 0 }
    if (~ $line /read) { cat $bbs/chat/$room }
    if (~ $line /users) { cat $bbs/chat/users }
    if not { echo $room':'$line > $bbs/chat/post }
}
