irc-bot/infobot

91 lines
2.2 KiB
Plaintext
Raw Normal View History

2010-12-15 17:23:09 -07:00
#! /bin/sh
2010-12-16 21:13:34 -07:00
db=$1; shift
2010-12-15 17:23:09 -07:00
text="$1"
2011-06-09 21:27:24 -06:00
[ -f $db ] || echo | cdb -c $db
2010-12-15 17:23:09 -07:00
lookup () {
if ! cdb -q -m $db "$1"; then
t="$1"
while [ "$t" != "$n" ]; do
n=$t
t=${n%[?!. ]}
done
if [ "$t" != "$1" ]; then
cdb -q -m $db "$t"
fi
fi
}
db_append () {
(printf "+%d,%d:%s->%s\n" ${#1} ${#2} "$1" "$2"; cdb -d $db) | cdb -c $db
echo "Okay, $sender, I added a factoid to $1."
}
nickname=${nickname:-infobot}
args=${text#* }
case "$text" in
2010-12-15 17:23:09 -07:00
!h*)
cat <<EOF
!stats Show statistics
!list KEY List all factoids stored for KEY
!append KEY += VALUE Add VALUE to KEY's factoids
!remove KEY -= VALUE Remove *VALUE* from KEY's factoids
!forget KEY Remove all factoids for KEY
EOF
;;
!s*)
cdb -s $db | head -n 1
;;
!l*)
2010-12-16 22:02:43 -07:00
printf "factoids for \"%s\": " "$args"
cdb -q -m $db "$args" | awk '{printf("|%s", $0);}'
2010-12-15 17:23:09 -07:00
echo
;;
$nickname:\ *\ is\ *)
s=${text#$nickname: }
db_append "${s%% is *}" "${s#* is }"
;;
2010-12-15 17:23:09 -07:00
!a*)
db_append "${args% +=*}" "${args#*+= }"
2010-12-15 17:23:09 -07:00
;;
!r*)
key=${args% -=*}
val=${args#*-= }
2010-12-15 22:36:16 -07:00
re=":$key->.*$val"
n=$(cdb -d $db | grep -c "$re")
if [ $n -gt 0 ]; then
cdb -d $db | grep -a -v ":$key->.*$val" | cdb -c $db
echo "Okay, $sender, I removed $n factoids from $key"
else
echo "Nothing matched, $sender."
fi
2010-12-15 17:23:09 -07:00
;;
!forget\ *)
2010-12-15 22:36:16 -07:00
cdb -d $db | grep -a -F -v ":$args->" | cdb -c $db
echo "I removed all factoids from $args"
2010-12-15 17:23:09 -07:00
;;
*)
resp=$(lookup "$text" | shuf -n 1 | sed "s/\$sender/$sender/")
case "$resp" in
"")
2010-12-15 22:36:16 -07:00
exit 1
2010-12-15 17:23:09 -07:00
;;
''*)
echo "Someone's up to no good!"
;;
'\'*)
2011-01-06 21:17:16 -07:00
printf "%s\n" "${resp#\\}"
2010-12-15 17:23:09 -07:00
;;
:*)
2010-12-16 22:02:43 -07:00
printf '\001ACTION %s\001\n' "${resp#:}"
2010-12-15 17:23:09 -07:00
;;
*)
echo "It's been said that $text is $resp"
;;
esac
;;
esac