Files
stormbot/modules/say/say.sh
2025-11-20 12:42:16 -05:00

68 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
[ -f functions.sh ] && source functions.sh
# shellcheck source=/dev/null
[ -f bot.cfg ] && source bot.cfg
userName="$1"
channelName="$2"
shift 2
# Debug logging
echo "DEBUG say.sh: userName='$userName' channelName='$channelName' args='$*'" >> "$log"
# Check if there are any arguments
if [[ -z "$*" ]]; then
msg "$channelName" "$userName: Please provide a message to say."
exit 0
fi
# Ensure channels array is available
if [[ -z "${channels[*]}" ]]; then
msg "$channelName" "$userName: Configuration error - no channels configured."
exit 1
fi
# Check if invoked from a PM (channel name equals username)
if [[ "$channelName" == "$userName" ]]; then
# PM context: check if first argument is a channel name
firstArg="$1"
echo "DEBUG say.sh: PM context detected, firstArg='$firstArg'" >> "$log"
# Check if first argument starts with # (explicit channel)
# IRC channels can contain: alphanumeric, -, _, and some special chars
if [[ "$firstArg" =~ ^#[a-zA-Z0-9_-]+$ ]]; then
# Remove # prefix for comparison with configured channels
targetChannel="${firstArg#\#}"
# Check if the bot is connected to this channel
isConnected=false
for configuredChannel in "${channels[@]}"; do
if [[ "$configuredChannel" == "$targetChannel" ]]; then
isConnected=true
break
fi
done
if [[ "$isConnected" == "true" ]]; then
# Send message to specified channel
shift
if [[ -z "$*" ]]; then
msg "$userName" "Please provide a message to say in $firstArg."
else
msg "$firstArg" "$*"
fi
else
# Not connected to that channel
msg "$userName" "I am not connected to $firstArg."
fi
else
# No channel specified or doesn't start with #, broadcast to all channels
for configuredChannel in "${channels[@]}"; do
msg "#$configuredChannel" "$*"
done
fi
else
# Channel context: just say the message in the current channel
msg "$channelName" "$*"
fi