Skip to content

Instantly share code, notes, and snippets.

@maxaudron
Last active February 5, 2020 12:29
Show Gist options
  • Save maxaudron/82ec677ee68252573b4d48a432a706bf to your computer and use it in GitHub Desktop.
Save maxaudron/82ec677ee68252573b4d48a432a706bf to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# shellcheck disable=SC2039
HOST=$1
PORT=$2
NAME=$3
NICK="imabot"
USER="$NICK :$NICK"
exec 3<>/dev/tcp/$HOST/$PORT
echo -e "NICK $NICK\r\nUSER $NICK $NICK $NICK :$NICK\r\n" >&3
# shellcheck disable=SC2039
# PARSE an irc message into a $PREFIX and array of $PARAMS and a $TAIL
# the $TAIL is also the last element of the $PARAMS array
parse_irc_msg() {
__MSG=$1
clean_irc_msg
for (( __i=0; __i<${#__MSG}; __i++ )); do
__CHAR="${__MSG:$__i:1}"
# IF the first char is a : there is a prefix
if [ $__i = 0 ] && [ "$__CHAR" = ":" ]; then
PREFIX="${__MSG:$((__i + 1))}"
PREFIX="${PREFIX%% *}"
__i=$((__i + ${#PREFIX} + 1))
# IF there is any other : in the message
# it is the tail and after that end of message
elif [ "$__CHAR" = ":" ]; then
TAIL="${__MSG:$((__i + 1))}"
PARAMS+=("$TAIL")
break
# Anything else is a normal parameter
# and gets parsed without any spaces
else
__PARAM="${__MSG:$__i}"
__PARAM="${__PARAM%% *}"
__PARAM="${__PARAM##*( )}"
# IF this is the first string after a space
# it is the command
if [ -z "$CMD" ]; then
CMD="$__PARAM"
else
PARAMS+=("$__PARAM")
fi
__i=$((__i + ${#__PARAM}))
fi
unset __PARAM
unset __CHAR
done
unset __i
unset __MSG
}
clean_irc_msg() {
unset PREFIX
unset CMD
unset PARAMS
unset TAIL
}
while IFS="\r\n" read -r msg; do
[ -n "$DEBUG" ] && echo "$msg"
parse_irc_msg "$msg"
if [ "$CMD" = "PING" ]; then
echo -e "PONG $TAIL" >&3 &
elif [ "$CMD" = "001" ]; then
( sleep 60; echo -e "LIST" >&3 ) &
# echo -e "MODE #gnulag +b" >&3
elif [ "$CMD" = "322" ]; then
CHANNELS+=("${PARAMS[1]}")
elif [ "$CMD" = "323" ]; then
( for channel in "${CHANNELS[@]}"; do
if [ "${channel:0:1}" = "#" ]; then
sleep 1
echo -e "MODE $channel +b" >&3
fi
done ) &
elif [ "$CMD" = "367" ]; then
BAN="${PARAMS[2]}"
if [[ "$BAN" == *"$NAME"* ]]; then
echo "${PARAMS[1]}"
BANNED_CHANNELS+=("${PARAMS[2]}")
fi
fi
done <&3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment