From c8481dcc56c983e119e7f340f847cf8599df1129 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 15 Dec 2010 17:23:09 -0700 Subject: [PATCH] Infobot functionality --- infobot | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 infobot diff --git a/infobot b/infobot new file mode 100755 index 0000000..099bc7c --- /dev/null +++ b/infobot @@ -0,0 +1,89 @@ +#! /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 <%s\n" ${#key} ${#val} "$key" "$val"; + cdb -d $db) | cdb -c $db + ;; + !r*) + key=$(printf "%s" "$args" | sed 's/ -=.*//') + val=$(printf "%s" "$args" | sed 's/.*-= //') + gs=":$key->.*$val" + case $(cdb -d $db | grep -c "$gs") in + 0) + echo 'No matches' + ;; + 1) + cdb -d $db | grep -v "$gs" | cdb -c $db + echo "I removed $val from $key" + ;; + *) + echo 'That maches more than one entry; please refine your request' + ;; + esac + ;; + !f*) + cdb -d $db | fgrep -v ":$args->" | cdb -c $db + echo "I forgot $args" + ;; + *) + resp=$(lookup "$text" | shuf -n 1 | sed "s/\$sender/$sender/") + case "$resp" in + "") + ;; + \\*) + echo "$resp" | cut -b2- + ;; + :*) + echo -ne '\001ACTION ' + echo -n $(echo "$resp" | cut -b2-) + echo -e '\001' + ;; + *) + echo "It's been said that $text is $resp" + ;; + esac + ;; +esac