Skip to content

Instantly share code, notes, and snippets.

@maxaudron
Created February 5, 2020 09:44
Show Gist options
  • Save maxaudron/5550012326bb6c519e9467c5816f4a77 to your computer and use it in GitHub Desktop.
Save maxaudron/5550012326bb6c519e9467c5816f4a77 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment