A couple of speedups, using shell builtins

This commit is contained in:
Neale Pickett 2010-12-21 23:02:20 -06:00
parent a07c3e9f7b
commit adfcd00d66
4 changed files with 39 additions and 37 deletions

6
cobalt
View File

@ -1,7 +1,9 @@
#! /bin/sh #! /bin/sh
exec ./bot \ while true; do
./bot \
-n cobalt \ -n cobalt \
-u cobalt \ -u cobalt \
-a ./cobalt-handler \ -a ./cobalt-handler \
socat STDIO OPENSSL:woozle.org:994,verify=0 socat STDIO OPENSSL:woozle.org:6697,verify=0
done

View File

@ -5,7 +5,10 @@ forum=$1; export forum; shift
prefix=$1; export prefix; shift prefix=$1; export prefix; shift
command=$1; export command; shift command=$1; export command; shift
# $* is now args # $* is now args
text=$(cat)
# Remeber, read discards leading whitespace. If that's not okay, use
# text=$(cat)
read -r text
join () { join () {
printf '\aJOIN %s\n' "$1" printf '\aJOIN %s\n' "$1"

19
firebot
View File

@ -1,24 +1,23 @@
#! /bin/sh #! /bin/sh
firebot () { cmd=${1%% *}
read cmd args args=${1#* }
case $cmd in case $cmd in
calc) calc)
printf "%s = " "$args" printf "%s = " "$args"
echo "$args" | bc -l printf "%s\n" "$args" | bc -l
;; ;;
units) units)
src=$(printf "%s" "$args" | sed 's/ ->.*//') src=${args% ->*}
dst=$(printf "%s" "$args" | sed 's/.*-> //') dst=${args#*-> }
units -1 -v -- "$src" "$dst" units -1 -v -- "$src" "$dst"
;; ;;
*) *)
exit 1 exit 1
;; ;;
esac esac
}
echo "$1" | firebot

22
infobot
View File

@ -2,6 +2,8 @@
db=$1; shift db=$1; shift
text="$1" text="$1"
cmd=${text%% *}
args=${text#* }
lookup () { lookup () {
if ! cdb -q -m $db "$1"; then if ! cdb -q -m $db "$1"; then
@ -16,7 +18,7 @@ lookup () {
fi fi
} }
case "$text" in case "$cmd" in
!h*) !h*)
cat <<EOF cat <<EOF
!stats Show statistics !stats Show statistics
@ -30,23 +32,20 @@ EOF
cdb -s $db | head -n 1 cdb -s $db | head -n 1
;; ;;
!l*) !l*)
args=$(echo "$text" | cut -d\ -f2-)
printf "factoids for \"%s\": " "$args" printf "factoids for \"%s\": " "$args"
cdb -q -m $db "$args" | awk '{printf("|%s", $0);}' cdb -q -m $db "$args" | awk '{printf("|%s", $0);}'
echo echo
;; ;;
!a*) !a*)
args=$(echo "$text" | cut -d\ -f2-) key=${args% +=*}
key=$(printf "%s" "$args" | sed 's/ +=.*//') val=${args#*+= }
val=$(printf "%s" "$args" | sed 's/.*+= //')
(printf "+%d,%d:%s->%s\n" ${#key} ${#val} "$key" "$val"; (printf "+%d,%d:%s->%s\n" ${#key} ${#val} "$key" "$val";
cdb -d $db) | cdb -c $db cdb -d $db) | cdb -c $db
echo "Okay, $sender, I added a factoid to $key" echo "Okay, $sender, I added a factoid to $key"
;; ;;
!r*) !r*)
args=$(echo "$text" | cut -d\ -f2-) key=${args% -=*}
key=$(printf "%s" "$args" | sed 's/ -=.*//') val=${args#*-= }
val=$(printf "%s" "$args" | sed 's/.*-= //')
re=":$key->.*$val" re=":$key->.*$val"
n=$(cdb -d $db | grep -c "$re") n=$(cdb -d $db | grep -c "$re")
if [ $n -gt 0 ]; then if [ $n -gt 0 ]; then
@ -56,8 +55,7 @@ EOF
echo "Nothing matched, $sender." echo "Nothing matched, $sender."
fi fi
;; ;;
!forget\ *) !forget)
args=$(echo "$text" | cut -d\ -f2-)
cdb -d $db | grep -a -F -v ":$args->" | cdb -c $db cdb -d $db | grep -a -F -v ":$args->" | cdb -c $db
echo "I removed all factoids from $args" echo "I removed all factoids from $args"
;; ;;
@ -67,10 +65,10 @@ EOF
"") "")
exit 1 exit 1
;; ;;
\\*) ''*)
echo "Someone's up to no good!" echo "Someone's up to no good!"
;; ;;
\\*) '\'*)
printf "%s" "${resp#\\}" printf "%s" "${resp#\\}"
;; ;;
:*) :*)