hptoad/examples/chat/answer

57 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# Simple chat bot
# with sending images to an external programme.
random() {
if (( "$1" < 1 )); then
echo '1'
else
shuf -n1 -i "1-$1"
fi
}
cd "$(dirname "$0")"
if (grep -q -- "$1" "ignore"); then
exit 0
fi
printf "%s" "$1: "
from=$(printf "%s\n" "$1" | tr -d '$`|<>')
# Second argument shows whether user is an admin (useless).
shift 2
string=$(echo "$@" | tr -d '$`')
if [[ ${#string} -gt 750 ]]; then
echo "tl;dr"
exit 0
fi
if [[ "$string" == *http*://* ]]; then
amount=$(sed -e '/https*:\/\//!d' ./frs.txt | wc -l)
# Makes it a bit more human (time to find a link?).
sleep 1
sed -e '/https*:\/\//!d' ./frs.txt | sed -n "$(random "$amount")p"
img="$(printf "%s\n" "$string" | sed -ne 's|.*\(https*://[^ \"()<>]*\).*|\1|g;1p')"
ns="$(printf "%s\n" "$string" | sed -e "s|$img||g")"
cl="$(wget --spider -S $(eval printf '%s\n' '$img') 2>&1)"
if [[ $(printf "%s\n" "$cl" | sed -e '/Content-Type/!d;/image\//!d') ]]; then
#echo 'posting'
printf "%s\n" "$img $from" >> ../data/pictures
fi
else
# Exclude last two entries from the amount.
amount="$(($(sed -e '/https*:\/\//d' ./frs.txt | wc -l) - 2))"
answer="$(sed -e '/https*:\/\//d' ./frs.txt | sed -ne "s/;;\\\n/\n/g;$(random "$amount")p")"
# Makes it a bit more human.
sleep "$(echo "${#answer} * 0.15" | bc -l)"
printf "%s\n" "$answer"
fi
if [[ ${#string} -lt 7 ]]; then
exit 0
fi
printf "%s\n" "$string" | sed -e '{:q;N;s/\n/;;\\n/g;t q}' >> ./frs.txt
exit 0