dobroho_vechora/dobroho_vechora.bash

125 lines
1.7 KiB
Bash
Raw Normal View History

2024-12-16 17:42:18 +00:00
#!/bin/bash
set -e
2025-01-11 21:44:24 +00:00
mode="${1}"
ip="${2}"
port="${3}"
nick="${4}"
2024-12-16 17:46:50 +00:00
2024-12-16 17:42:18 +00:00
print_usage() {
echo "${1}"
2025-01-11 21:44:24 +00:00
echo "${0} <MODE> <IP> <PORT> [NICK]"
echo "Modes: interactive (intr), send, receive (rec)"
2024-12-16 17:42:18 +00:00
exit
}
2024-12-16 17:46:50 +00:00
[ -z "${ip}" ] && print_usage 'No IP given.'
[ -z "${port}" ] && print_usage 'No port given.'
2025-01-11 21:44:24 +00:00
[ -z "${5}" ] || print_usage 'Too many arguments.'
2024-12-16 17:42:18 +00:00
2025-01-11 21:44:24 +00:00
[ -z "${nick}" ] || nick="<${nick}> "
open_socket() {
2024-12-16 17:42:18 +00:00
exec 3<> "/dev/tcp/${ip}/${port}"
2025-01-11 21:44:24 +00:00
}
close_socket() {
2024-12-16 17:42:18 +00:00
exec 3>&-
}
2025-01-11 21:44:24 +00:00
send_message() {
open_socket
echo "${1}" >&3
close_socket
}
2024-12-16 17:42:18 +00:00
get_message() {
2025-01-11 21:44:24 +00:00
open_socket
2024-12-16 17:42:18 +00:00
echo "${1}" >&3
2025-01-11 21:44:24 +00:00
cat <&3 || true
close_socket
2024-12-16 17:42:18 +00:00
}
2025-01-11 21:44:24 +00:00
case "${mode}" in
intr)
;&
interactive)
;;
send)
open_socket
text="${nick}$(cat)"
send_message "0${text}"
close_socket
exit
;;
rec)
;&
receive)
length="$(get_message 1)"
open_socket
echo -n 2 >&3
head -c "${length}" <&3
close_socket
exit
;;
*)
echo Unknown mode
exit
esac
2025-01-11 22:57:45 +00:00
print_messages() {
open_socket
echo -n 2 >&3
head -c "${1}" <&3 | sed -e 's/\x1B/ESC/g'
close_socket
}
length=0
2024-12-16 17:42:18 +00:00
while :; do
read cmd
case "${cmd}" in
2025-01-11 21:44:24 +00:00
I)
2024-12-16 17:42:18 +00:00
b=""
while :; do
read c
[[ "${c}" != "." ]] || break
if [ -z ${b} ]; then
b="${c}"
else
b="${b}"$'\n'"${c}"
fi
done
2025-01-11 21:44:24 +00:00
send_message 0"${nick}${b}"
2024-12-16 17:42:18 +00:00
;;
2025-01-11 21:44:24 +00:00
i)
2024-12-16 17:42:18 +00:00
read a
2025-01-11 21:44:24 +00:00
send_message 0"${nick}${a}"
2024-12-16 17:42:18 +00:00
;;
2025-01-11 22:57:45 +00:00
l)
echo "${length}"
;;
w)
oldlength="${length}"
while :; do
length="$(get_message 1)"
if [ "${oldlength}" -lt "${length}" ]; then
break
fi
echo -n .
sleep 1
done
echo
print_messages "${length}"
;;
2024-12-16 17:42:18 +00:00
p)
2025-01-11 21:44:24 +00:00
length="$(get_message 1)"
2025-01-11 22:57:45 +00:00
print_messages "${length}"
2024-12-16 17:42:18 +00:00
;;
q)
exit
;;
*)
echo ?
;;
esac
done