#!/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" # Note: bot.sh strips "# " from commands, so #channel becomes just channel name # Check if first argument looks like a channel name (matches configured channels) isConnected=false targetChannel="" for configuredChannel in "${channels[@]}"; do if [[ "$firstArg" == "$configuredChannel" ]]; then isConnected=true targetChannel="$configuredChannel" break fi done if [[ "$isConnected" == "true" ]]; then # First arg is a known channel, send message there shift if [[ -z "$*" ]]; then msg "$userName" "Please provide a message to say in #$targetChannel." else msg "#$targetChannel" "$*" fi else # Check if first arg looks like it could be a channel name (not in our list) # If it contains no spaces and looks channel-ish, assume user specified wrong channel if [[ "$firstArg" =~ ^[a-zA-Z0-9_-]+$ ]] && [[ ! "$firstArg" =~ [[:space:]] ]]; then # Looks like a channel name but we're not connected msg "$userName" "I am not connected to #$firstArg." else # No channel specified, broadcast to all channels for configuredChannel in "${channels[@]}"; do msg "#$configuredChannel" "$*" done fi fi else # Channel context: just say the message in the current channel msg "$channelName" "$*" fi