Files
stormbot/modules/reminder/reminder.sh
2025-10-25 02:14:29 -04:00

64 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
[ -f functions.sh ] && source functions.sh
userNick="$1"
shift
chan="$1"
shift
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
msg "$chan" "ok, $userNick, reminder in $time."
reminderMessage="$*"
# Handle 'tell' syntax for targeting other users
if [[ "$reminderMessage" =~ ^[Tt]ell[[:space:]]+ ]]; then
targetNick="$(echo "${reminderMessage#[T|t]ell }" | cut -d ' ' -f1)"
# Validate nick doesn't contain invalid characters
if [[ "$targetNick" =~ ^[a-zA-Z0-9_\[\]\{\}\\|\`\^\-]+$ ]]; then
userNick="$targetNick"
reminderMessage="${reminderMessage#[Tt]ell }"
reminderMessage="${reminderMessage#* }"
fi
fi
sleep "$timeInSeconds" && msg "$chan" "$userNick: $reminderMessage" &