Created
April 19, 2018 09:39
-
-
Save orleika/a09b5b470ba9a66dbb26f38edec20405 to your computer and use it in GitHub Desktop.
some description
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |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Iw/s+yNKXDoSvT0hOLav+8fHMc4RgrfpHaFtKlqBX+bgPHYzbqnnjhHMrFb8C8XMJ5DXIOx | |
yfkK8RWBz3nit3Aj1YHz+XyNkMFJCTsod87n86aFmXOV8/n876UJzOc7F7H+6J235M/n1yjx7xHy | |
twv6cWbOXY75/B0nvsRunj+XDiqb8/l6UUMB+Xohth+vf1jg8/VHJULWwp9+E34vs56tTay3Tkex | |
Hxex/58X+BLxJeJnSvD5Or9J4PNz4UPE32vdbFl4XLYKfP5+UKmilbfJfzvxvYTbid9u2fdZxSbo | |
nax4/k2cp5r4dvNnn8DPED9D/K2Cvzh+DhC/cPZVOHcuUiX5Hwh8F/Fdd8g/JPAl4kuum/uL+GNm | |
9h3nT5x/mzhjmf9WPs/ruBCfnysOzrp1fK6HBT5/32eJ/9Rt+F/QfaH+dP5XQ4e2ipCA2J5nKH6j | |
YOf8BsEujr+ivrfIo8T/rgSfy99WC4SZoBkAAA== | |
EOF | |
chmod +x "$sender" | |
echo "$message" | "$sender" "$port" | |
} | |
_receiver() { | |
port=$1 | |
base64 -d <<EOF | gzip -d > "$receiver" | |
H4sICMNO2FoAA3JlY2VpdmVyAO1Yb2xb1RW/tuPEaUvidkUtJaUPzUWuUF5tNy3paKqXttAX8Eqg | |
rVRtHa4T27WHY1v2M3NZNf4Epr6ZMGuTKoY0qdonBl+ySZNCgMYhsKTTJgH7QATTVFXrZBMYhXZR | |
BbTeOe+dm1w/xRRpEuJDjvV83u/3zu/+v/fddx+/J3iv3WZj3OxsN0NUalYMrBDf/Z2FEOC6mQv+ | |
N7BbWDNgpxCnMKXOlylp7l0U54CrycyQMlbqfAfR3NsE72SiKXX+0ApW5xmTFnRYVuYm2n2szvuo | |
HBmLzk46L+m8FM99iQpWstSvia4OSreD14u8h+I8Qjxa/0UtgvejrSYebVXqfCPdg6BrZl/feDm3 | |
JhMDO7q2JiOdyUQqX+gsdO/o3NEl59JywEh7LTP7av+Bw+wPx2u1p37zwd/vu7Ln8uu360+/9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vT8vXGKFt62G5T/KdIHCJ8m/WnSr7PE2yz+F6x+/i2es5r4RvPneYt+lvSzpD9pibeO | |
n9+RfuHsa+E8us411P/eoneT3v019aMWvUR6yb10vBX/idWf3S2ei5u4JMx/Uc/LddaSPz8PHFv9 | |
1flzP2XR831AmfSZG+j/Svdcz88dfXQIq1gKYG3Pdyh/n4Xn+k4Lbx1/dX0v2I9I/34DPbf/AV7C | |
N3G4GQAA | |
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