Break the welcominator with improvements

This commit is contained in:
Nick Chambers 2021-11-13 00:27:17 -06:00
parent e8c7c0f26e
commit fa17f37485
1 changed files with 76 additions and 28 deletions

104
rowbot
View File

@ -181,6 +181,38 @@ url() {
fi
}
b64_encode() {
local idx=0 numerics table_idxs table_idx encoded
local table=( {A..Z} {a..z} {0..9} + / )
for (( ; idx < ${#1}; idx+=3 )); do
read -ra numerics < <(
printf '%d %d %d\n' "'${1:idx:1}" "'${1:idx+1:1}" "'${1:idx+2:1}"
)
(( table_idxs[0] = numerics[0] >> 2 ))
(( table_idxs[1] = (((numerics[0] & 0x03) << 6) | (numerics[1] & 0xF0) >> 2) >> 2 ))
(( table_idxs[2] = (((numerics[1] & 0x0F) << 4) | (numerics[2] & 0xC0) >> 4) >> 2 ))
(( table_idxs[3] = numerics[2] & 0x3F ))
for table_idx in "${table_idxs[@]}"; do
encoded+=${table[$table_idx]}
done
done
if (( ${#1} % 3 == 1 )); then
encoded=${encoded::-2}==
elif (( ${#1} % 3 == 2 )); then
encoded=${encoded::-1}=
fi
printf %s "$encoded"
if [[ -t 1 ]]; then
printf \\n
fi
}
###
# configure rowbot's environment
###
@ -499,78 +531,90 @@ on_sys_exit_997_annoyatron900() {
###
# register with the server
###
/###
# not a doomsday device™
welcomeinator() {
local wait_for_server=0
local synergizing=0
log_trace "beginning welcomeinator state is %s" "$reg_state"
while (( ! wait_for_server )); do
case $reg_state in
begin)
while (( ! synergizing )); do
case $welcome_phase in
alpha)
if [[ -v config[caps] ]]; then
net_send "CAP LS 302"
wait_for_server=1
cap ls
synergizing=1
else
reg_state=auth
welcome_phase=beta
fi
;;
auth)
beta)
# This is a a false positive.
# shellcheck disable=SC2102
if [[ -v config[pass] ]]; then
irc_pass "${config[pass]}"
fi
reg_state=identify
welcome_phase=gamma
;;
identify)
gamma)
irc_nick "${config[nick]}"
irc_user "${config[ident]}" "${config[realname]}"
# This is a a false positive.
# shellcheck disable=SC2102
if [[ -v config[caps] ]]; then
reg_state=negotiate
# This is not a command. Shellcheck is drunk.
# shellcheck disable=SC2209
welcome_phase=delta
elif [[ -v config[sasl-method] ]]; then
reg_state=sasl
welcome_phase=sasl
else
reg_state=registered
wait_for_server=1
welcome_phase=registered
synergizing=1
fi
;;
negotiate)
local avail_cap cap caps=( ) req_caps=( )
delta)
# FIXME: caps should be parsed into associative arrays
local avail_cap cap caps=( ) req_caps=( ) found_sasl=0
IFS=, read -ra caps <<< "${config[caps]}"
for cap in "${caps[@]}"; do
for avail_cap in "${avail_caps[@]}"; do
if [[ $cap = "$avail_cap" ]]; then
if [[ $cap = "$avail_cap" || $cap = "$avail_cap"=* ]]; then
if [[ $cap = sasl ]]; then
found_sasl=1
fi
req_caps+=( "$cap" )
break
fi
done
done
# this should have logic similar to privmsg or notify for splitting up
# long lines
# This is a a false positive.
# shellcheck disable=SC2102
if [[ -v config[sasl-method] ]] && (( ! found_sasl )); then
req_caps+=( sasl )
fi
if (( ${#req_caps[@]} )); then
log_debug "requesting the following capabilities: %s" "${req_caps[*]}"
net_send "CAP REQ :%s" "${req_caps[*]}"
wait_for_server=1
synergizing=1
else
log_debug "no desired capabilities are available."
reg_state=registered
fi
;;
sasl)
false
;;
registered)
if [[ -v config[caps] ]]; then
net_send "CAP END"
fi
wait_for_server=1
synergizing=1
esac
done
@ -591,10 +635,6 @@ on_sys_first_003_welcome() {
welcomeinator
}
# Capability Negotiation
# SASL (if negotiated)
# CAP END
on_sys_init_999_welcome() {
get_option chan ""
declare -gA isupport
@ -866,6 +906,14 @@ irc_accept() {
net_send "ACCEPT $1"
}
irc_cap() {
case $1 in
ls)
net_send "CAP LS %d" "${2-302}"
;;
esac
}
irc_join() {
local chans
printf -v chans %s, "$@"