#! /bin/sh db=$1; shift text="$1" 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 !h*) cat <.*$val" | cdb -c $db echo "Okay, $sender, I removed $n factoids from $key" else echo "Nothing matched, $sender." fi ;; !forget\ *) 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 ;; ''*) echo "Someone's up to no good!" ;; '\'*) printf "%s\n" "${resp#\\}" ;; :*) printf '\001ACTION %s\001\n' "${resp#:}" ;; *) echo "It's been said that $text is $resp" ;; esac ;; esac