Reorganization to hopefully prevent git conflicts.
This commit is contained in:
@@ -4,67 +4,142 @@
|
||||
who="${1%!*}"
|
||||
who="${who//:}"
|
||||
shift
|
||||
# shellcheck disable=SC2034 # Used in action strings via execute_action
|
||||
chan="$1"
|
||||
shift
|
||||
|
||||
# each word is stored in an associative array, with the actions to be taken as the array's contents.
|
||||
# the variable $chan contains the channel that caused the trigger.
|
||||
# the variable $who contains the nick that caused the trigger.
|
||||
# Optional: Add a percentage (e.g., "50%") as the last element to respond only that percent of the time.
|
||||
declare -A keywords
|
||||
keywords[linux]="msg \"$chan\" \"Linux is $(shuf -n1 -e awesome God great lovely fantastic amazing wonderful)!\" 25%"
|
||||
keywords[windows]="msg \"$chan\" \"$(shuf -n1 -e\
|
||||
"Failure is not an option, it comes bundled with Windows!"\
|
||||
"Apple got all pissed off because I farted in their store. It's not my falt they don't have Windows..."\
|
||||
"Windows is dumb!"\
|
||||
"Did you know that Micro Soft is Linda's pet name for Bill Gates?"\
|
||||
"A computer without Windows is like a chocolate cake without the mustard."\
|
||||
"Windows is stupid"\
|
||||
"In a world without walls and fences - who needs windows and gates?"\
|
||||
"Windows, plug and pray."\
|
||||
"Windows - Just another pain in the glass."\
|
||||
"Windows, it's not pretty, it's not ugly, but it's pretty ugly.")!\" 25%"
|
||||
keywords[emacs]="msg \"$chan\" \"$who, Real men of genius use vim!\" 50%"
|
||||
keywords[jaws]="msg \"$chan\" \"${who}: watch out for sharks!\""
|
||||
keywords[emacspeak]="msg \"$chan\" \"$who, Real men of genius use vim!\""
|
||||
keywords[nano]="msg \"$chan\" \"$who, Real men of genius use vim!\""
|
||||
keywords[pidgin]="msg \"$chan\" \"$who, Real men of genius use irssi!\""
|
||||
keywords[weechat]="msg \"$chan\" \"$who, Real men of genius use irssi!\""
|
||||
keywords[thunderbird]="msg \"$chan\" \"$who, Real dogs use mutt, real men of genius use cat on a mailbox file!\""
|
||||
keywords[gedit]="msg \"$chan\" \"$who, Real men of genius use vim!\""
|
||||
keywords[pluma]="msg \"$chan\" \"$who, Real men of genius use vim!\""
|
||||
keywords[dragonforce]="msg \"$chan\" \"$who: I love DragonForce!!!\""
|
||||
keywords[vim]="msg \"$chan\" \"$(shuf -n1 -e \
|
||||
"Praise vim! HA"\
|
||||
"In times of trouble, just ask yourself, 'What would Bram Moolenaar do?'."\
|
||||
"Vim is like a Ferrari, if you're a beginner, it handles like a bitch, but once you get the hang of it, it's small, powerful and FAST!"\
|
||||
"VIM is like a new model Ferrari, and sounds like one too - 'VIIIIIIMMM!'"\
|
||||
"Only through vim can you be saved! HA")\""
|
||||
keywordsFile="triggers/keywords/keywords.cfg"
|
||||
|
||||
# Function to process random selection syntax: {random:opt1|opt2|opt3}
|
||||
process_random() {
|
||||
local text="$1"
|
||||
while [[ "$text" =~ \{random:([^}]+)\} ]]; do
|
||||
local options="${BASH_REMATCH[1]}"
|
||||
IFS='|' read -ra optArray <<< "$options"
|
||||
local selected="${optArray[$((RANDOM % ${#optArray[@]}))]}"
|
||||
text="${text/\{random:$options\}/$selected}"
|
||||
done
|
||||
echo "$text"
|
||||
}
|
||||
|
||||
# Safe execution function - only allows predefined IRC functions
|
||||
execute_action() {
|
||||
local action="$1"
|
||||
|
||||
# Parse the action to extract function name and arguments
|
||||
if [[ "$action" =~ ^msg[[:space:]]+(\"[^\"]+\"|[^[:space:]]+)[[:space:]]+(.+)$ ]]; then
|
||||
local target="${BASH_REMATCH[1]}"
|
||||
local message="${BASH_REMATCH[2]}"
|
||||
# Remove quotes from target and message if present
|
||||
target="${target//\"/}"
|
||||
# Safely expand only $chan and $who variables - NO EVAL
|
||||
message="${message//\"\$chan\"/$chan}"
|
||||
message="${message//\$chan/$chan}"
|
||||
message="${message//\"\$who\"/$who}"
|
||||
message="${message//\$who/$who}"
|
||||
message="${message//\$\{chan\}/$chan}"
|
||||
message="${message//\$\{who\}/$who}"
|
||||
message="$(process_random "$message")"
|
||||
msg "$target" "$message"
|
||||
elif [[ "$action" =~ ^act[[:space:]]+(\"[^\"]+\"|[^[:space:]]+)[[:space:]]+(.+)$ ]]; then
|
||||
local target="${BASH_REMATCH[1]}"
|
||||
local message="${BASH_REMATCH[2]}"
|
||||
# Remove quotes from target and message if present
|
||||
target="${target//\"/}"
|
||||
# Safely expand only $chan and $who variables - NO EVAL
|
||||
message="${message//\"\$chan\"/$chan}"
|
||||
message="${message//\$chan/$chan}"
|
||||
message="${message//\"\$who\"/$who}"
|
||||
message="${message//\$who/$who}"
|
||||
message="${message//\$\{chan\}/$chan}"
|
||||
message="${message//\$\{who\}/$who}"
|
||||
message="$(process_random "$message")"
|
||||
act "$target" "$message"
|
||||
elif [[ "$action" =~ ^reply[[:space:]]+(\"[^\"]+\"|[^[:space:]]+)[[:space:]]+(.+)$ ]]; then
|
||||
local target="${BASH_REMATCH[1]}"
|
||||
local message="${BASH_REMATCH[2]}"
|
||||
# Remove quotes from target and message if present
|
||||
target="${target//\"/}"
|
||||
# Safely expand only $chan and $who variables - NO EVAL
|
||||
message="${message//\"\$chan\"/$chan}"
|
||||
message="${message//\$chan/$chan}"
|
||||
message="${message//\"\$who\"/$who}"
|
||||
message="${message//\$who/$who}"
|
||||
message="${message//\$\{chan\}/$chan}"
|
||||
message="${message//\$\{who\}/$who}"
|
||||
message="$(process_random "$message")"
|
||||
reply "$target" "$message"
|
||||
fi
|
||||
}
|
||||
|
||||
# Load keywords from config file into associative array
|
||||
declare -A keywords
|
||||
if [[ -f "$keywordsFile" ]]; then
|
||||
while IFS='|' read -r keyword action percentage || [[ -n "$keyword" ]]; do
|
||||
# Skip comments and empty lines
|
||||
[[ "$keyword" =~ ^[[:space:]]*# ]] && continue
|
||||
[[ -z "$keyword" ]] && continue
|
||||
|
||||
# Trim whitespace
|
||||
keyword="${keyword#"${keyword%%[![:space:]]*}"}"
|
||||
keyword="${keyword%"${keyword##*[![:space:]]}"}"
|
||||
action="${action#"${action%%[![:space:]]*}"}"
|
||||
action="${action%"${action##*[![:space:]]}"}"
|
||||
percentage="${percentage#"${percentage%%[![:space:]]*}"}"
|
||||
percentage="${percentage%"${percentage##*[![:space:]]}"}"
|
||||
|
||||
# Store in array (key includes ~ prefix for multi-word triggers)
|
||||
if [[ -n "$percentage" ]]; then
|
||||
keywords["$keyword"]="$action $percentage"
|
||||
else
|
||||
keywords["$keyword"]="$action"
|
||||
fi
|
||||
done < "$keywordsFile"
|
||||
fi
|
||||
|
||||
# Process single-word triggers
|
||||
wordList="$(echo "${@,,}" | tr '[:space:]' $'\n' | sort -u)"
|
||||
for w in ${wordList//[[:punct:]]/} ; do
|
||||
if [[ -n "${keywords[${w,,}]}" && "$lastWordMatch" != "${keywords[${w,,}]}" ]]; then
|
||||
keywordAction="${keywords[${w,,}]}"
|
||||
# Check if the last element is a percentage
|
||||
if [[ "$keywordAction" =~ (.*)\ ([0-9]+)%$ ]]; then
|
||||
command="${BASH_REMATCH[1]}"
|
||||
percentage="${BASH_REMATCH[2]}"
|
||||
# Generate random number between 1-100 and only respond if within percentage
|
||||
randomNum=$((RANDOM % 100 + 1))
|
||||
if [[ $randomNum -le $percentage ]]; then
|
||||
eval "$command"
|
||||
fi
|
||||
else
|
||||
# No percentage specified, always respond
|
||||
eval "$keywordAction"
|
||||
fi
|
||||
lastWordMatch="${keywords[${w,,}]}"
|
||||
fi
|
||||
if [[ -n "${keywords[${w,,}]}" && "$lastWordMatch" != "${keywords[${w,,}]}" ]]; then
|
||||
keywordAction="${keywords[${w,,}]}"
|
||||
# Check if the last element is a percentage
|
||||
if [[ "$keywordAction" =~ (.*)\ ([0-9]+)%$ ]]; then
|
||||
command="${BASH_REMATCH[1]}"
|
||||
percentage="${BASH_REMATCH[2]}"
|
||||
# Generate random number between 1-100 and only respond if within percentage
|
||||
randomNum=$((RANDOM % 100 + 1))
|
||||
if [[ $randomNum -le $percentage ]]; then
|
||||
execute_action "$command"
|
||||
fi
|
||||
else
|
||||
# No percentage specified, always respond
|
||||
execute_action "$keywordAction"
|
||||
fi
|
||||
lastWordMatch="${keywords[${w,,}]}"
|
||||
fi
|
||||
done
|
||||
|
||||
# Example of dealing with multi word triggers.
|
||||
# Reset wordList without sorting it and with spaces removed.
|
||||
# Process multi-word triggers (those starting with ~)
|
||||
wordList="$(echo "${@,,}" | tr -d '[:space:]')"
|
||||
if [[ "${wordList,,}" =~ .*nowplaying:.* ]]; then
|
||||
act "$chan" "$(shuf -n1 -e "cranks the volume up to 11" "got soooo high at that show" "boogies down to the sound of the band")!"
|
||||
fi
|
||||
for trigger in "${!keywords[@]}"; do
|
||||
if [[ "$trigger" =~ ^~ ]]; then
|
||||
# Remove the ~ prefix for matching
|
||||
triggerPattern="${trigger#\~}"
|
||||
if [[ "${wordList,,}" =~ .*${triggerPattern}.* ]]; then
|
||||
keywordAction="${keywords[$trigger]}"
|
||||
# Check if the last element is a percentage
|
||||
if [[ "$keywordAction" =~ (.*)\ ([0-9]+)%$ ]]; then
|
||||
command="${BASH_REMATCH[1]}"
|
||||
percentage="${BASH_REMATCH[2]}"
|
||||
# Generate random number between 1-100 and only respond if within percentage
|
||||
randomNum=$((RANDOM % 100 + 1))
|
||||
if [[ $randomNum -le $percentage ]]; then
|
||||
execute_action "$command"
|
||||
fi
|
||||
else
|
||||
# No percentage specified, always respond
|
||||
execute_action "$keywordAction"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user