grimoire/bash/irc

48 lines
1.2 KiB
Bash
Executable file

#!/usr/bin/env bash
exec {sock}<>/dev/tcp/irc.libera.chat/6667
while IFS= read -ru "$sock" line; do
line=${line%$'\r'}
declare -A msg=( [words]=no [original]="$line" )
if [[ ${line:0:1} = : ]]; then
prefix=${line%% *} line=${line#"$prefix"} line=${line# } prefix=${prefix#:}
msg[host]=${prefix#*@} prefix=${prefix%"${msg[host]}"} prefix=${prefix%@}
msg[from]=${msg[host]}
if [[ $prefix ]]; then
msg[ident]=${prefix#*!}
msg[from]=${msg[ident]}
if [[ ${msg[ident]} != "$prefix" ]]; then
msg[nick]=${prefix%!*}
msg[from]=${msg[nick]}
fi
fi
fi
msg[cmd]=${line%% *} msg[cmd]=${msg[cmd]^^}
line=${line#"${msg[cmd]}"} line=${line# }
msg_args=( )
while [[ $line ]]; do
if [[ ${line:0:1} = : ]]; then
msg_args+=( "${line:1}" ) msg[words]=yes line=
read -ra msg_words <<< "${msg_args[-1]}"
else
arg=${line%% *} msg_args+=( "$arg" )
line=${line#"$arg"} line=${line# }
fi
done
if [[ ${msg[cmd]} = @(PRIVMSG|NOTICE) ]]; then
if [[ ${msg_args[0]:0:1} = \# ]]; then
msg[to]=${msg_args[0]}
else
msg[to]=${msg[from]}
fi
fi
printf '%s\n' "${msg[original]}"
done