Silly little patch.

This commit is contained in:
bʰedoh₂ swé 2025-01-12 02:44:24 +05:00
parent 2ebda02f40
commit d74f7490a0

View File

@ -1,34 +1,75 @@
#!/bin/bash #!/bin/bash
set -e set -e
ip="${1}"
port="${2}" mode="${1}"
ip="${2}"
port="${3}"
nick="${4}"
print_usage() { print_usage() {
echo "${1}" echo "${1}"
echo "${0} <IP> <PORT>" echo "${0} <MODE> <IP> <PORT> [NICK]"
echo "Modes: interactive (intr), send, receive (rec)"
exit exit
} }
[ -z "${ip}" ] && print_usage 'No IP given.' [ -z "${ip}" ] && print_usage 'No IP given.'
[ -z "${port}" ] && print_usage 'No port given.' [ -z "${port}" ] && print_usage 'No port given.'
[ -z "${3}" ] || print_usage 'Too many arguments.' [ -z "${5}" ] || print_usage 'Too many arguments.'
[ -z "${nick}" ] || nick="<${nick}> "
open_socket() {
exec 3<> "/dev/tcp/${ip}/${port}"
}
close_socket() {
exec 3>&-
}
send_message() { send_message() {
exec 3<> "/dev/tcp/${ip}/${port}" open_socket
echo "${1}" >&3 echo "${1}" >&3
exec 3>&- close_socket
} }
get_message() { get_message() {
exec 3<> "/dev/tcp/${ip}/${port}" open_socket
echo "${1}" >&3 echo "${1}" >&3
timeout 1s cat <&3 || true cat <&3 || true
exec 3>&- close_socket
} }
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
while :; do while :; do
read cmd read cmd
case "${cmd}" in case "${cmd}" in
i) I)
b="" b=""
while :; do while :; do
read c read c
@ -39,15 +80,22 @@ while :; do
b="${b}"$'\n'"${c}" b="${b}"$'\n'"${c}"
fi fi
done done
send_message 0"${b}" send_message 0"${nick}${b}"
;; ;;
I) i)
read a read a
send_message 0"${a}" send_message 0"${nick}${a}"
;; ;;
p) p)
send_message 1 length="$(get_message 1)"
get_message 2 | sed -e 's/\x1B/ESC/g' if [ -z "${length}" ]; then
echo !;
else
open_socket
echo -n 2 >&3
head -c "${length}" <&3 | sed -e 's/\x1B/ESC/g'
close_socket
fi
;; ;;
q) q)
exit exit