Add support for TLS connections

This commit is contained in:
Nick Chambers 2021-06-14 18:15:49 -05:00
parent 3fca870f32
commit 12073a87e7
1 changed files with 41 additions and 4 deletions

45
rowbot
View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash
###
# parse arguments
# argument parser for parsing arguments
##
declare -A opts
@ -38,8 +38,45 @@ done
server=${opts[server]:-irc.libera.chat}
tls=${opts[tls]:-no}
if [[ tls = no ]]; then
port=${opts[port]:-6667}
else
if [[ $tls = yes ]]; then
if ! hash socat 2>/dev/null; then
printf 'please install socat to use tls with rowbot.\n' >&2
exit 1
fi
port=${opts[port]:-6697}
else
port=${opts[port]:-6667}
fi
###
# net code
###
if [[ $tls = yes ]]; then
coproc sock { socat OPENSSL:"$server":"$port" -; }
in_sock=${sock[0]} out_sock=${sock[1]}
else
exec {sock}<>/dev/tcp/"$server"/"$port"
in_sock=$sock out_sock=$sock
fi
send() {
local fmt
printf -v fmt "$1" "${@:2}"
printf '%s\r\n' "$fmt" >&"$out_sock"
}
recv() {
declare -n sock_line=$1
IFS= read -r "$1" <&"$in_sock"
sock_line=${sock_line%$'\r'}
}
###
# driver
###
while recv line; do
declare -p line
done