irc-bot/infobot

84 lines
2.2 KiB
Bash
Executable File

#! /bin/sh
if [ $# -gt 1 ]; then
db=$1; shift
else
db=info.cdb
fi
text="$1"
args=$(echo "$text" | cut -d\ -f2-)
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
}
case "$text" in
!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*)
printf "%s" "$args"
cdb -q -m $db "$args" | \
awk '{printf("¦\"%s\"", $0);}'
echo
;;
!a*)
key=$(printf "%s" "$args" | sed 's/ +=.*//')
val=$(printf "%s" "$args" | sed 's/.*+= //')
(printf "+%d,%d:%s->%s\n" ${#key} ${#val} "$key" "$val";
cdb -d $db) | cdb -c $db
echo "Okay, $sender, I added a factoid to $key"
;;
!r*)
key=$(printf "%s" "$args" | sed 's/ -=.*//')
val=$(printf "%s" "$args" | sed 's/.*-= //')
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
;;
!f*)
cdb -d $db | grep -a -F -v ":$args->" | cdb -c $db
echo "I removed all factoids from $args"
;;
*)
resp=$(lookup "$text" | shuf -n 1 | sed "s/\$sender/$sender/")
case "$resp" in
"")
exit 1
;;
\\*)
printf "%s" "$resp" | cut -b2-
;;
:*)
printf '\001ACTION %s\001\n' "$(echo "$resp" | cut -b2-)"
;;
*)
echo "It's been said that $text is $resp"
;;
esac
;;
esac