Files
stormbot/modules/reminder/reminder.sh
2025-10-25 02:49:15 -04:00

74 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
[ -f functions.sh ] && source functions.sh
userNick="$1"
chan="$2"
# $3 contains the entire argument string (e.g., "30s hello world")
# We need to split it into time and message
# shellcheck disable=SC2086
set -- $3 # Intentionally unquoted to split into separate words
time="$1"
shift
# Validate time argument is provided
if [[ -z "$time" ]]; then
msg "$chan" "$userNick: Please provide a time (e.g., 30s, 5m, 1h)."
exit 0
fi
# Normalize time format - add 's' suffix if no time unit specified
if ! [[ "$time" =~ ^[0-9]+[HhMmSs]$ ]]; then
# Check if it's just a number without a unit
if [[ "$time" =~ ^[0-9]+$ ]]; then
time="${time}s"
else
msg "$chan" "$userNick: Time must be numeric (e.g., 30s, 5m, 1h)."
exit 0
fi
fi
# Validate time is reasonable (max 24 hours)
timeValue="${time%[HhMmSs]}"
timeUnit="${time: -1}"
case "${timeUnit,,}" in
h) maxTime=24; timeInSeconds=$((timeValue * 3600)) ;;
m) maxTime=1440; timeInSeconds=$((timeValue * 60)) ;;
s) maxTime=86400; timeInSeconds=$timeValue ;;
*) maxTime=86400; timeInSeconds=$timeValue ;;
esac
if [[ $timeValue -gt $maxTime ]]; then
msg "$chan" "$userNick: Maximum time is 24 hours."
exit 0
fi
# Validate reminder message is provided
if [[ -z "$*" ]]; then
msg "$chan" "$userNick: Please provide a reminder message."
exit 0
fi
# Convert message to array for easier parsing
# shellcheck disable=SC2206
msgArray=($*)
targetNick="$userNick" # Default target is the person who set the reminder
# Handle 'tell' syntax for targeting other users
if [[ "${msgArray[0],,}" == "tell" ]]; then
# Extract target nickname (second element, strip trailing punctuation)
extractedNick="${msgArray[1]}"
extractedNick="${extractedNick%:}"
extractedNick="${extractedNick%,}"
# Set target and rebuild message without "tell nickname"
targetNick="$extractedNick"
# Rebuild message from element 2 onward
reminderMessage="${msgArray[*]:2}"
else
reminderMessage="$*"
fi
msg "$chan" "ok, $userNick, reminder in $time."
sleep "$timeInSeconds" && msg "$chan" "$targetNick: $reminderMessage" &