happybot/happybot/dowork/dowork.sh

119 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env ash
. /home/zgrep/offtopiabday/happybot/common.sh
d="happybot/dowork/people"
delay="300"
pause="7200"
irc | while read -r n m; do
n="$(var "$n" | tr -d './')"
if hreg '^happybot[:,] (?:noslap|(?:autoslap|slap(?:ping|me)) (?:pause|delay|wait|0?\.5|1/2|½))(?: ([0-9]+))?' "$m"; then
pse="$(m 1)"
if [ -z "$pse" ]; then
pse="$pause"
fi
if [ -e "$d/$n.delay" ]; then
dly="$(cat "$d/$n.delay")"
else
dly="$delay"
fi
rpause="$(echo "$pse - $dly" | bc)"
echo "$(dateadd -f '%s' 'now' "${rpause}s")" > "$d/$n.slap";
echo "${zwsp}Automatic slapping for $n paused until about ${pse}s from now." | say
continue
elif hreg '^happybot[:,] (autoslap|slap(ping|me)) (public|loud)(ly)?' "$m"; then
grep -Fvxe "$n" "${d}_private.txt" > "${d}_private.2"
mv "${d}_private.2" "${d}_private.txt"
echo "${zwsp}I will automatically slap $n in public." | say;
continue
elif hreg '^happybot[:,] (autoslap|slap(ping|me)) (priv(msg|ate)?|secret(ive)?|quiet)(ly)?' "$m"; then
echo "$n" >> "${d}_private.txt"
sort -u "${d}_private.txt" > "${d}_private.2"
mv "${d}_private.2" "${d}_private.txt"
echo "${zwsp}I will automatically slap $n privately." | say;
continue
elif hreg '^happybot[:,] (autoslap|slap(ping|me)) (stop|off|disable|no|0)' "$m"; then
grep -Fvxe "$n" "$d.txt" > "$d.2"
mv "$d.2" "$d.txt"
if [ -e "$d/$n.slap" ]; then
rm "$d/$n.slap";
fi
if [ -e "$d/$n.delay" ]; then
rm "$d/$n.delay";
fi
echo "${zwsp}I will no longer automatically slap $n." | say
continue
elif hreg '^happybot[:,] (?:autoslap|slap(?:ping|me)) (?:start|on|enable|yes|1|set)(?: ([0-9]+))?' "$m"; then
dly="$(m 1)"
if [ ! -z "$dly" ]; then
echo "$dly" > "$d/$n.delay";
else
dly="$delay";
fi
echo "$n" >> "$d.txt"
sort -u "$d.txt" > "$d.2"
mv "$d.2" "$d.txt"
echo "${zwsp}I will slap $n every time they speak, at most once every ${dly}s." | say
continue
elif hreg '^happybot[:,] (?:autoslap|slap(?:ping|me)) status(?: (\S+))?' "$m"; then
newn="$(m 1)"
if [ ! -z "$newn" ]; then
n="$newn"
fi
if grep -Fxqe "$n" "${d}_private.txt"; then
slaptype="private";
else
slaptype="public";
fi
if grep -Fxqe "$n" "$d.txt"; then
delta="$(datediff -i '%s' -f '%S' "$(cat "$d/$n.slap")" 'now')"
if [ -e "$d/$n.delay" ]; then
dly="$(cat "$d/$n.delay")"
else
dly="$delay"
fi
if [ "$delta" -le "$dly" ]; then
delta="$(echo "$dly - $delta" | bc)"
pstat="Not slapping for another ${delta}s."
else
pstat="Currently primed to slap."
fi
echo "${zwsp}I'm supposed to ${slaptype}ly slap ${n} at most once every ${dly}s. ${pstat}" | say;
else
echo "${zwsp}No automatic ${slaptype} slapping for ${n} yet." | say;
fi
continue
elif [ -s "$d.txt" ]; then
if grep -Fxqe "$n" "$d.txt"; then
if [ -e "$d/$n.slap" ]; then
delta="$(datediff -i '%s' -f '%S' "$(cat "$d/$n.slap")" 'now')"
if [ -e "$d/$n.delay" ]; then
dly="$(cat "$d/$n.delay")"
else
dly="$delay"
fi
if [ "$delta" -le "$dly" ]; then
continue
fi
fi
echo "$(date +%s)" > "$d/$n.slap"
if [ -e "$d/$n.txt" ]; then
obj="$(rand "$d/$n.txt")"
else
obj="$(rand "${d}_generic.txt")"
fi
if grep -Fxqe "$n" "${d}_private.txt"; then
echo "/PRIVMSG ${n} :${x01}ACTION ${zwsp}slaps you with $obj.${x01}" | say;
else
echo "/m ${zwsp}slaps $n with $obj." | say;
fi
fi;
fi;
done;