#!/usr/bin/env bash RESET='\033[0m' GRAY='\033[0;90m' BOLD_GRAY='\033[1;90m' GREEN='\033[0;32m' RED='\033[0;31m' port=2826 ############################################################ # Print banner. # ############################################################ Banner() { clear banner="$(ls -1 ./banner/*.sh | sort -R | head -1)" eval "$banner" } ############################################################ # Help # ############################################################ Help() { # Display Help printf "Usage: $(basename $0) user@host.example\n" printf "${RESET}\n" printf "journalNet Client Options:\n" printf -- "-B Show random ASCII Banner from banner folder.\n" printf -- "-b Print the addresses in bookmarks.txt file.\n" printf -- "-m Write a message in the Guestbook.\n" printf -- "-g Show the Guestbook.\n" printf -- "-h Print this help.\n" printf "${RESET}\n" printf "Info: You can read journals with netcat:\n" printf "echo \"user\" | nc -N 127.0.0.1 2826\n" printf "echo \"user\" | nc -q 0 127.0.0.1 2826\n" exit 0 } Usage() { printf "journalNet Client\n" printf "Usage: $(basename $0) user@host.example\n" printf " $(basename $0) -g user@host.example to read the Guestbook.\n" printf " $(basename $0) -m user@host.example to write a message in the Guestbook.\n" printf " $(basename $0) -h for help.\n" exit 0 } ############################################################ # Bookmarks # ############################################################ Bookmarks() { # Remove all leading and trailing spaces from each line, it also removes blank lines. sed -i 's/^[[:blank:]]*//;s/[[:blank:]]*$//;/^\s*$/d' bookmarks.txt # Display Bookmarks BOOKMARKS_ADDR_FILE="bookmarks.txt" # Function to ping hosts and return status. ping_host() { local host=$1 bhostname=$(printf $host | cut -d"@" -f 2) ping -c 1 -W 1 "$bhostname" > /dev/null 2>&1 if [ $? -eq 0 ]; then printf "[${GREEN}Host Online${RESET}]" else printf "[${RED}Ping OFF${RESET}]" fi } # Check if the file exists. if [ ! -f "$BOOKMARKS_ADDR_FILE" ]; then printf "Error: The file $BOOKMARKS_ADDR_FILE does not exist.\n" exit 1 fi # Read journal's addresses from the file into an array. readarray -t bookmark_addrs < "$BOOKMARKS_ADDR_FILE" # Clear the screen. clear # Display menu and ping status. printf "Bookmark List\n" printf "${RESET}\n" for i in "${!bookmark_addrs[@]}"; do printf "$(($i + 1))) ${bookmark_addrs[$i]} - $(ping_host ${bookmark_addrs[$i]})\n" done # Prompt user for a choice. printf "\n" read -p "Enter your choice [1-$((${#bookmark_addrs[@]}))] or 0 to exit: " choice # Process user choice. case $choice in 0) printf "${GRAY}Hasta la vista, ${BOLD_GRAY}Baby!\n" >&2 exit 0 ;; [1-9]|[1-9][0-9]|${#bookmark_addrs[@]}) if [ "$choice" -le "${#bookmark_addrs[@]}" ] && [[ -e /usr/local/bin/journal ]]; then bookmark_addr=${bookmark_addrs[$(($choice - 1))]} timeout 3 journal "${bookmark_addr##* }" else bookmark_addr=${bookmark_addrs[$(($choice - 1))]} timeout 3 ./journal "${bookmark_addr##* }" fi ;; *) printf "Invalid option. Please try again.\n" ;; esac } ############################################################ # Leave a message. # ############################################################ Message() { printf "Messenger\n" read -p "Enter your nickname: " mnickname read -p "Enter your mail/journal/socialnet: " mcontact read -p "Enter your message: " mmessage if [[ -z "$mnickname" || -z "$mmessage" ]]; then printf "Either you have not entered your nickname, or you have not entered the message." exit 1 else read -p "Send it (y/n)? " choice printf "${RESET}\n" fi case "$choice" in y|Y ) ;; n|N ) exit;; * ) printf "Error: Invalid option.\n" && exit 1;; esac } ############################################################ # Guestbook # ############################################################ Guestbook() { guestbook="${guestbook:=journalclient}" } ############################################################ # Process the input options. Add options as needed. # ############################################################ # Get the options while getopts ":Bhbmg" option; do case "$option" in B) # Enable Banner Banner continue;; b) # Display Bookmarks Bookmarks exit;; g) # Display Guestbook. Guestbook continue;; m) # Leave a Message Message continue;; h) # Display Help Help exit;; \?) # Invalid option printf "Error: Invalid option.\n" exit;; esac done ############################################################ # Main program # ############################################################ # Null? [[ -z "$@" ]] && Usage # Arg is valid? user@example.com? re="[A-Za-z0-9]+@[A-Za-z0-9.]+" ! [[ "$@" =~ $re ]] && Usage ############################################################ # 0k. Start here. # ############################################################ # Take user and host. user="$(echo "$@" | cut -d ' ' -f 2| cut -d @ -f 1)" host="$(echo "$@" | cut -d @ -f 2)" printf "Connecting to $user@$host..." printf "${RESET}\n" # Open socket. exec 3<>/dev/tcp/$host/$port printf "$user\n" >&3 printf "$mnickname\n" >&3 printf "$mmessage\n" >&3 printf "$mcontact\n" >&3 printf "$guestbook\n" >&3 cat <&3