#!/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 # Extract just the first word (bot.sh passes all remaining args as a single string) firstWord="${1%% *}" echo "DEBUG say.sh: PM context detected, firstWord='$firstWord', allArgs='$*'" >> "$log" # Note: bot.sh strips "# " (hash-space) from commands, but "#channel" stays intact # Remove # prefix if present for comparison firstWordNoHash="${firstWord#\#}" # Check if first word looks like a channel name (matches configured channels) isConnected=false targetChannel="" for configuredChannel in "${channels[@]}"; do if [[ "$firstWordNoHash" == "$configuredChannel" ]]; then isConnected=true targetChannel="$configuredChannel" break fi done if [[ "$isConnected" == "true" ]]; then # First word is a known channel, extract the rest as the message restOfMessage="${1#* }" # If firstWord == $1, there was no space, so no message if [[ "$firstWord" == "$1" ]]; then msg "$userName" "Please provide a message to say in #$targetChannel." else msg "#$targetChannel" "$restOfMessage" fi else # Check if first word 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 [[ "$firstWordNoHash" =~ ^[a-zA-Z0-9_-]+$ ]] && [[ ! "$firstWordNoHash" =~ [[:space:]] ]]; then # Looks like a channel name but we're not connected msg "$userName" "I am not connected to #$firstWordNoHash." 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