Skip to content

Instantly share code, notes, and snippets.

@orleika
Created April 25, 2018 05:55
Show Gist options
  • Save orleika/aa25745222c53b08a7d00d6c714a827e to your computer and use it in GitHub Desktop.
Save orleika/aa25745222c53b08a7d00d6c714a827e to your computer and use it in GitHub Desktop.
some description
#!/bin/bash
#
# ychat
# simple multicast chat tools on the shell (only supported bash)
# copyrights: koki yasuda aka orleika <[email protected]>
# Licensed under MIT
#
# Usage:
# ychat
#
##############################################################################
# Strict mode
##############################################################################
set -o nounset
set -o errexit
trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR
set -o errtrace
set -o pipefail
IFS=$'
'
###############################################################################
# Environment
###############################################################################
_ME=$(basename "${0}")
###############################################################################
# Help
###############################################################################
_print_logo() {
tput clear
echo -e "\e[1;31m
## ## ###### ## ## ### ########
## ## ## ## ## ## ## ## ##
#### ## ## ## ## ## ##
## ## ######### ## ## ##
## ## ## ## ######### ##
## ## ## ## ## ## ## ##
## ###### ## ## ## ## ## \e[m
"
}
_print_help() {
_print_logo
echo -e "
Usage:
NAME=[your name] PORT=[udp port] ${_ME} [OPTION]...
Options:
-h, --help Show this screen."
}
###############################################################################
# Program Functions
###############################################################################
sender=$(mktemp "/tmp/${0##*/}.tmp.XXXXXX")
receiver=$(mktemp "/tmp/${0##*/}.tmp.XXXXXX")
log=$(mktemp "/tmp/${0##*/}.tmp.XXXXXX")
atexit() {
[[ -n ${sender-} ]] && rm -f "$sender"
[[ -n ${receiver-} ]] && { pkill "${0##*/}"; rm -f "$receiver"; }
[[ -n ${log-} ]] && rm -f "$log"
}
trap atexit EXIT
trap 'trap - EXIT; atexit; exit -1' INT PIPE TERM
_sender() {
port=$1
message=$2
base64 -d <<EOF | gzip -d > "$sender"
H4sICGYW4FoAA3NlbmRlcgDtWG9oW9cVv5IsW0lcW/bSzk0MeQOlUyB+tmTHddp5POdP85yJ4ubP
OmhWRdYfS6ssCel5OF3GWrwOv2peXNiXfRn+1n5ooYwVvBRiZd5mPFjpyli9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5M547/vYUdwPvAzFFvc1C/8fEny
d0kYj+9aBP7WeZSDFcm/ScJ4ruIV+BrxNTLoEs8n4S9THTifz/tZMgxRwl7KnfP5/OtlzpznfL6O
VGgh4OsGF7n+DzNnznI+n6enKcF9Ur5uScOngb0GcMzXgTQRFLZ9/lzOUtmb9Sf+LPH5uiO3H6//
NyU+X8cWicDXPYR7tuFPMOEMlG2t26946/24yP0/LvErxK8Q//UGfP6+mJT4/Hy5QIbF+s+pTR6X
ksTn75kgLdRt98j/EqufP0PEHyL+Mclf7r/nJf7WuayDA5K/HP+KxF8g/gI/571H/J8Qf/NMa/P8
uk415C9IfD/x/Z+T/7LEV4iv+Lf3l/GrzOk7zt86R3fwvDD/RT7P6w0pPj8vXOz47PhcX5X4fN9Q
IX76HvxliR+kc7ogHcrK/S3jVSqL8/m55mHia1IF5P54i+L3SXbOf1qyu7bRHvZpeZH47zbgc/kP
rWCJJCgaAAA=
EOF
chmod +x "$sender"
echo "$message" | "$sender" "$port"
}
_receiver() {
port=$1
base64 -d <<EOF | gzip -d > "$receiver"
H4sICGYW4FoAA3JlY2VpdmVyAO1YX2xbVxk/tuPESUvidkWENtALcyB9yI3jZplXGrhpG3ozrLZr
VpjYqOPEduLVscP1zZR2E3SEoV7cbn7ggT+CTbywB2B9AZVOalxlEOWlChXSIoGmaqLSNemgaFMV
YJ35vnu/kxyfxWPigRf6Wdff/f3u9zvn3PP/nm8Oxb7o9XgYNy/7PEMUbNIcrBE/e996CHBRFoD/
XWwnawTsF+I0ptX4MiXNfYDifHA1uBlSxlqN7yCae4/g/Uw0rcY3bGE1njFlXYdlZUGig6M1fj+V
42xLrc5Lui7SdVE89yUqWEl6vwa6OijdDv5e5JWaXKguwI7dNJN4n6VyZFu0Gh+iuJCkewR0jezD
Gy9nTzYz1t/Xk012ZzO5mdnu2Wh/d3+fWsirESdtfG18tcNHTjjxvD62kx7bsQ2uVriwyFtYrfHm
xbT8lJaP0miGq4nKshWuj3xAebuFPEWbrsNjPsomf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BWkhapXi5/KdZ7fhp
J3076UekeLn9npX0G+e0Lg5J8XL+L0j6EulLpJcnCjn/H7CNs1TH1s+za1xd/UuSPkj64IfU/0zS
K6RXgpvHy/jnrPaMbuNc3cUlYfyLel6uX0n583O/S9s+OH/uL0t6vo8ok372P+gXJH2Uzs+idNgq
t7eMlygtrufnk/tJr0kvILfHNco/LPFcf1LiPZt4H3u//ZD0r9fRc/s3tMkidDgaAAA=
EOF
chmod +x "$receiver"
"$receiver" "$port" >> "$log" &
}
_ychat() {
name=$1
port=$2
_print_logo
_receiver "$port"
while :
do
echo -en "\nsay: "; IFS="" read -r message
date=$(date '+%H:%M:%S')
if [[ -n "$message" ]]
then
_sender "$port" "$name ($date)> $message"
fi
_print_logo
tail -n15 "$log"
done
}
###############################################################################
# Main
###############################################################################
ip="[$(hostname -i)]"
name=${NAME:-"$ip"}
port=${PORT:-8900}
_main() {
if [[ "${1:-}" =~ ^-h|--help$ ]]
then
_print_help
else
_ychat "$name" "$port"
fi
}
_main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment