Skip to content

Instantly share code, notes, and snippets.

@sskras
Forked from wrfly/bash & nc chat
Created October 23, 2022 20:56
Show Gist options
  • Save sskras/b5e9fb20fb09b401b6992e849ece9e83 to your computer and use it in GitHub Desktop.
Save sskras/b5e9fb20fb09b401b6992e849ece9e83 to your computer and use it in GitHub Desktop.

Revisions

  1. @wrfly wrfly revised this gist Jul 10, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions bash & nc chat
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    # A little chat progream via ncat
    # Author:wrfly Date:2015.7.7
    # Author:wrfly Date:2015.7
    # Usage:
    # Server: ncat -e chat.sh -lk &
    # Client: ncat server_ip
    @@ -45,7 +45,7 @@ echo -e "\033[1;36m Now, chose a username first (visitor by default): \033[0m"
    visitor=visitor.$RANDOM
    username=${username:-$visitor}

    check_lenth $username 20
    check_lenth "$username" 20

    if [[ $username =~ ^visitor\. ]]; then
    :
    @@ -61,7 +61,7 @@ echo -e "\033[1;36m Enter a chat room(public by default): \033[0m"
    read -e chat_room
    chat_room=${chat_room:-public}

    check_lenth $chat_room 20
    check_lenth "$chat_room" 20

    if [[ ! -z $(echo $chat_room | grep "\.\./") ]] ;then
    echo "NO way"
    @@ -75,7 +75,7 @@ if [[ -z $(grep ^"$chat_room"$ $db_rooms) ]]; then
    try_times=0
    while true;do
    read -e chat_room_again
    check_lenth $chat_room_again 200
    check_lenth "$chat_room_again" 20
    if [[ $chat_room_again == $chat_room ]]; then
    touch ${rooms_dir}/$chat_room &> /dev/null
    echo $chat_room >> $db_rooms
    @@ -103,7 +103,7 @@ echo -e "\033[1;34m$msg_date\033[0m\033[1;31m User \"$username\" \033[0m\
    \033[1;34m has came into \033[0m\033[1;36m\"$user_room\"\033[0m" >> $chat_room

    while read -e msg; do
    check_lenth $msg
    check_lenth "$msg" 200
    if [[ $msg =~ ^: ]]; then
    case $msg in
    ":help")
  2. @wrfly wrfly revised this gist Jul 9, 2015. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions bash & nc chat
    Original file line number Diff line number Diff line change
    @@ -14,10 +14,16 @@ trap clean_up SIGQUIT EXIT

    function clean_up
    {
    if [[ -z $chat_room ]]; then
    cat bye
    exit
    else
    echo -e "\033[1;34m$msg_date\033[0m\033[1;31m $username \033[0m\
    \033[1;34mleaved room\033[0m \033[1;36m \"$user_room\" \033[0m" >> $chat_room
    # cat bye
    cat bye
    exit
    fi

    }

    function check_lenth {
    @@ -29,7 +35,7 @@ function check_lenth {
    }

    # Welcome
    # cat welcome
    cat welcome

    # Login

    @@ -97,7 +103,7 @@ echo -e "\033[1;34m$msg_date\033[0m\033[1;31m User \"$username\" \033[0m\
    \033[1;34m has came into \033[0m\033[1;36m\"$user_room\"\033[0m" >> $chat_room

    while read -e msg; do
    msg_check_lenth $msg
    check_lenth $msg
    if [[ $msg =~ ^: ]]; then
    case $msg in
    ":help")
  3. @wrfly wrfly revised this gist Jul 9, 2015. 1 changed file with 25 additions and 11 deletions.
    36 changes: 25 additions & 11 deletions bash & nc chat
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    # A little chat progream via ncat
    # Author:wrfly Date:2015.7.8
    # Author:wrfly Date:2015.7.7
    # Usage:
    # Server: ncat -e chat.sh -lk &
    # Client: ncat server_ip
    @@ -10,53 +10,66 @@ db_users=user_lists
    db_rooms=db_rooms
    room_dir=chat_rooms

    trap clean_up SIGINT
    trap clean_up SIGQUIT EXIT

    function clean_up
    {

    echo -e "\033[1;34m$msg_date\033[0m\033[1;31m $username \033[0m\
    \033[1;34mleaved room\033[0m \033[1;36m $user_room \033[0m" >> $chat_room
    \033[1;34mleaved room\033[0m \033[1;36m \"$user_room\" \033[0m" >> $chat_room
    # cat bye
    exit
    }

    function check_lenth {
    count=$(echo $1 | wc -m)
    if [[ $count -gt $2 ]]; then
    echo "Argument too long, 40 limit."
    exit 2
    fi
    }

    # Welcome
    # cat welcome

    # Login

    echo -ne "\033[1;36m Now, chose a username first (visitor by default): \033[0m"
    echo -e "\033[1;36m Now, chose a username first (visitor by default): \033[0m"

    read -n 20 -e username
    read -e username
    visitor=visitor.$RANDOM
    username=${username:-$visitor}

    check_lenth $username 20

    if [[ $username =~ ^visitor\. ]]; then
    :
    else
    echo $username >> $db_users
    fi

    echo -e "\033[1;36m Hi, $username! Which room do you prefer?\033[0m"
    echo -ne "\033[1;36m Enter a chat room(public by default): \033[0m"
    echo -e "\033[1;36m Enter a chat room(public by default): \033[0m"


    # Enter_room
    read -n 20 -e chat_room
    read -e chat_room
    chat_room=${chat_room:-public}

    check_lenth $chat_room 20

    if [[ ! -z $(echo $chat_room | grep "\.\./") ]] ;then
    echo "NO way"
    chat_room=${room_dir}/"bad"
    exit 2
    fi

    if [[ -z $(grep ^"$chat_room"$ $db_rooms) ]]; then
    echo -en "\033[1;36m Sorry, There is no room named \033[1;31m\"$chat_room\"\033[0m.
    echo -e "\033[1;36m Sorry, There is no room named \033[1;31m\"$chat_room\"\033[0m.
    \033[1;36mIf you want to create a chat_room named \"$chat_room\", enter it again: \033[0m"
    try_times=0
    while true;do
    read -n 20 -e chat_room_again
    read -e chat_room_again
    check_lenth $chat_room_again 200
    if [[ $chat_room_again == $chat_room ]]; then
    touch ${rooms_dir}/$chat_room &> /dev/null
    echo $chat_room >> $db_rooms
    @@ -83,7 +96,8 @@ msg_date=$(date +"%R:%S")
    echo -e "\033[1;34m$msg_date\033[0m\033[1;31m User \"$username\" \033[0m\
    \033[1;34m has came into \033[0m\033[1;36m\"$user_room\"\033[0m" >> $chat_room

    while read -n 200 -e msg; do
    while read -e msg; do
    msg_check_lenth $msg
    if [[ $msg =~ ^: ]]; then
    case $msg in
    ":help")
  4. @wrfly wrfly revised this gist Jul 8, 2015. 1 changed file with 9 additions and 7 deletions.
    16 changes: 9 additions & 7 deletions bash & nc chat
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    # A little chat script via netcat
    # Author:wrfly Date:2015.7.7
    # A little chat progream via ncat
    # Author:wrfly Date:2015.7.8
    # Usage:
    # Server: ncat -e chat.sh -lk &
    # Client: ncat server_ip
    @@ -10,7 +10,7 @@ db_users=user_lists
    db_rooms=db_rooms
    room_dir=chat_rooms

    trap clean_up SIGINT EXIT SIGQUIT
    trap clean_up SIGINT

    function clean_up
    {
    @@ -28,7 +28,7 @@ function clean_up

    echo -ne "\033[1;36m Now, chose a username first (visitor by default): \033[0m"

    read -n 20 username
    read -n 20 -e username
    visitor=visitor.$RANDOM
    username=${username:-$visitor}

    @@ -43,18 +43,20 @@ echo -ne "\033[1;36m Enter a chat room(public by default): \033[0m"


    # Enter_room
    read -n 20 chat_room
    read -n 20 -e chat_room
    chat_room=${chat_room:-public}

    if [[ ! -z $(echo $chat_room | grep "\.\./") ]] ;then
    echo "NO way"
    exit 2
    fi

    if [[ -z $(grep ^"$chat_room"$ $db_rooms) ]]; then
    echo -en "\033[1;36m Sorry, There is no room named \033[1;31m\"$chat_room\"\033[0m.
    \033[1;36mIf you want to create a chat_room named \"$chat_room\", enter it again: \033[0m"
    try_times=0
    while true;do
    read -n 20 chat_room_again
    read -n 20 -e chat_room_again
    if [[ $chat_room_again == $chat_room ]]; then
    touch ${rooms_dir}/$chat_room &> /dev/null
    echo $chat_room >> $db_rooms
    @@ -81,7 +83,7 @@ msg_date=$(date +"%R:%S")
    echo -e "\033[1;34m$msg_date\033[0m\033[1;31m User \"$username\" \033[0m\
    \033[1;34m has came into \033[0m\033[1;36m\"$user_room\"\033[0m" >> $chat_room

    while read msg; do
    while read -n 200 -e msg; do
    if [[ $msg =~ ^: ]]; then
    case $msg in
    ":help")
  5. @wrfly wrfly revised this gist Jul 8, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions bash & nc chat
    Original file line number Diff line number Diff line change
    @@ -45,6 +45,10 @@ echo -ne "\033[1;36m Enter a chat room(public by default): \033[0m"
    # Enter_room
    read -n 20 chat_room
    chat_room=${chat_room:-public}
    if [[ ! -z $(echo $chat_room | grep "\.\./") ]] ;then
    echo "NO way"
    exit 2
    fi
    if [[ -z $(grep ^"$chat_room"$ $db_rooms) ]]; then
    echo -en "\033[1;36m Sorry, There is no room named \033[1;31m\"$chat_room\"\033[0m.
    \033[1;36mIf you want to create a chat_room named \"$chat_room\", enter it again: \033[0m"
  6. @wrfly wrfly revised this gist Jul 7, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bash & nc chat
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    #!/bin/bash
    # a little chat progream via netcat
    # A little chat script via netcat
    # Author:wrfly Date:2015.7.7
    # Usage:
    # Server: ncat -e chat.sh -lk &
  7. @wrfly wrfly revised this gist Jul 7, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions bash & nc chat
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    #!/bin/bash
    # a little chat progream via netcat
    # Author:wrfly Date:2015.7.7
    # Usage:
    # Server: ncat -e chat.sh -lk &
    # Client: ncat server_ip

    #config
    db_users=user_lists
  8. @wrfly wrfly created this gist Jul 7, 2015.
    101 changes: 101 additions & 0 deletions bash & nc chat
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    #!/bin/bash
    # a little chat progream via netcat
    # Author:wrfly Date:2015.7.7

    #config
    db_users=user_lists
    db_rooms=db_rooms
    room_dir=chat_rooms

    trap clean_up SIGINT EXIT SIGQUIT

    function clean_up
    {

    echo -e "\033[1;34m$msg_date\033[0m\033[1;31m $username \033[0m\
    \033[1;34mleaved room\033[0m \033[1;36m $user_room \033[0m" >> $chat_room
    # cat bye
    exit
    }

    # Welcome
    # cat welcome

    # Login

    echo -ne "\033[1;36m Now, chose a username first (visitor by default): \033[0m"

    read -n 20 username
    visitor=visitor.$RANDOM
    username=${username:-$visitor}

    if [[ $username =~ ^visitor\. ]]; then
    :
    else
    echo $username >> $db_users
    fi

    echo -e "\033[1;36m Hi, $username! Which room do you prefer?\033[0m"
    echo -ne "\033[1;36m Enter a chat room(public by default): \033[0m"


    # Enter_room
    read -n 20 chat_room
    chat_room=${chat_room:-public}
    if [[ -z $(grep ^"$chat_room"$ $db_rooms) ]]; then
    echo -en "\033[1;36m Sorry, There is no room named \033[1;31m\"$chat_room\"\033[0m.
    \033[1;36mIf you want to create a chat_room named \"$chat_room\", enter it again: \033[0m"
    try_times=0
    while true;do
    read -n 20 chat_room_again
    if [[ $chat_room_again == $chat_room ]]; then
    touch ${rooms_dir}/$chat_room &> /dev/null
    echo $chat_room >> $db_rooms
    break
    else
    echo -e "\033[1;36m Not match. Try again:\033[0m"
    fi
    try_times=$((try_times+1))
    if [[ $try_times -gt 5 ]]; then
    echo -e "\033[1;31mToo many tries.\033[0m"
    exit 1
    fi
    done
    fi

    # Chating......
    user_room=$chat_room
    chat_room=${room_dir}/${chat_room}
    touch $chat_room

    tail -f --pid=$$ $chat_room &

    msg_date=$(date +"%R:%S")
    echo -e "\033[1;34m$msg_date\033[0m\033[1;31m User \"$username\" \033[0m\
    \033[1;34m has came into \033[0m\033[1;36m\"$user_room\"\033[0m" >> $chat_room

    while read msg; do
    if [[ $msg =~ ^: ]]; then
    case $msg in
    ":help")
    echo "Help messages."
    ;;
    ":lists")
    echo "User lists."
    ;;
    ":q")
    exit
    ;;
    *)
    echo "Default"
    ;;
    esac
    elif [[ -z $msg ]]; then
    :
    else
    msg_date=$(date +"%R:%S")
    echo -e "\033[1;34m$msg_date\033[0m\033[1;31m $username \033[0m\
    \033[1;34msaid:\033[0m \033[1;36m $msg \033[0m" >> $chat_room
    fi
    msg= #clear msg for tailf
    done