Files
stormbot/triggers/link/link.sh
2025-10-25 01:30:02 -04:00

37 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
[ -f functions.sh ] && source functions.sh
# Dependencies required by this trigger
dependencies=("curl" "w3m" "sed")
# Silently skip if dependencies are missing (triggers shouldn't error in channel)
if ! check_dependencies "${dependencies[@]}" &> /dev/null; then
exit 0
fi
for l in $3 ; do
text="${l#:}"
if [[ "${text}" =~ http://|https://|www\..* ]]; then
# Security: Only allow http:// and https:// protocols
if [[ ! "$text" =~ ^https?:// ]]; then
# Convert www. to http://www.
if [[ "$text" =~ ^www\. ]]; then
text="http://$text"
else
# Skip unknown protocols
continue
fi
fi
# Remove potentially dangerous shell metacharacters from URL
text="${text//[;&|]/}"
# Fetch page title with timeout and security limits
pageTitle="$(curl -L -s --connect-timeout 5 --max-time 10 "$text" | sed -n -e 'H;${x;s!.*<head[^>]*>\(.*\)</head>.*!\1!;T;s!.*<title>\(.*\)</title>.*!\1!p}' | w3m -dump -T text/html | tr '[:space:]' ' ')"
pageTitle="$(echo "$pageTitle" | tr -cd '[:print:]')"
if [[ ${#pageTitle} -gt 1 ]]; then
msg "$2" "$pageTitle"
fi
fi
done