From adfcd00d667d0513586dd512b3fbfd215c20627d Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 21 Dec 2010 23:02:20 -0600 Subject: [PATCH 1/3] A couple of speedups, using shell builtins --- cobalt | 12 +++++++----- cobalt-handler | 5 ++++- firebot | 37 ++++++++++++++++++------------------- infobot | 22 ++++++++++------------ 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/cobalt b/cobalt index 4967985..57cee53 100755 --- a/cobalt +++ b/cobalt @@ -1,7 +1,9 @@ #! /bin/sh -exec ./bot \ - -n cobalt \ - -u cobalt \ - -a ./cobalt-handler \ - socat STDIO OPENSSL:woozle.org:994,verify=0 +while true; do + ./bot \ + -n cobalt \ + -u cobalt \ + -a ./cobalt-handler \ + socat STDIO OPENSSL:woozle.org:6697,verify=0 +done diff --git a/cobalt-handler b/cobalt-handler index a7fe010..5ca66a0 100755 --- a/cobalt-handler +++ b/cobalt-handler @@ -5,7 +5,10 @@ forum=$1; export forum; shift prefix=$1; export prefix; shift command=$1; export command; shift # $* is now args -text=$(cat) + +# Remeber, read discards leading whitespace. If that's not okay, use +# text=$(cat) +read -r text join () { printf '\aJOIN %s\n' "$1" diff --git a/firebot b/firebot index 845ab66..b3d8512 100755 --- a/firebot +++ b/firebot @@ -1,24 +1,23 @@ #! /bin/sh -firebot () { - read cmd args - case $cmd in - calc) - printf "%s = " "$args" - echo "$args" | bc -l - ;; - units) - src=$(printf "%s" "$args" | sed 's/ ->.*//') - dst=$(printf "%s" "$args" | sed 's/.*-> //') - units -1 -v -- "$src" "$dst" - ;; - *) - exit 1 - ;; - esac -} - -echo "$1" | firebot +cmd=${1%% *} +args=${1#* } +case $cmd in + calc) + printf "%s = " "$args" + printf "%s\n" "$args" | bc -l + ;; + units) + src=${args% ->*} + dst=${args#*-> } + units -1 -v -- "$src" "$dst" + ;; + *) + exit 1 + ;; +esac + + diff --git a/infobot b/infobot index 591480f..05c670d 100755 --- a/infobot +++ b/infobot @@ -2,6 +2,8 @@ db=$1; shift text="$1" +cmd=${text%% *} +args=${text#* } lookup () { if ! cdb -q -m $db "$1"; then @@ -16,7 +18,7 @@ lookup () { fi } -case "$text" in +case "$cmd" in !h*) cat <%s\n" ${#key} ${#val} "$key" "$val"; cdb -d $db) | cdb -c $db echo "Okay, $sender, I added a factoid to $key" ;; !r*) - args=$(echo "$text" | cut -d\ -f2-) - key=$(printf "%s" "$args" | sed 's/ -=.*//') - val=$(printf "%s" "$args" | sed 's/.*-= //') + key=${args% -=*} + val=${args#*-= } re=":$key->.*$val" n=$(cdb -d $db | grep -c "$re") if [ $n -gt 0 ]; then @@ -56,8 +55,7 @@ EOF echo "Nothing matched, $sender." fi ;; - !forget\ *) - args=$(echo "$text" | cut -d\ -f2-) + !forget) cdb -d $db | grep -a -F -v ":$args->" | cdb -c $db echo "I removed all factoids from $args" ;; @@ -67,10 +65,10 @@ EOF "") exit 1 ;; - \\*) + ''*) echo "Someone's up to no good!" ;; - \\*) + '\'*) printf "%s" "${resp#\\}" ;; :*) From 1cec77a2ccae69140b932443504c20c8e1644ee1 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 7 Jan 2011 14:03:16 -0600 Subject: [PATCH 2/3] Add `bot` script --- .gitignore | 9 ++++----- bot | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) create mode 100755 bot diff --git a/.gitignore b/.gitignore index ae2fb91..fe334db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ -*.cmi -*.cmo +*.o *~ -bot -.omake* -.depend +dispatch +irc + diff --git a/bot b/bot new file mode 100755 index 0000000..5b97397 --- /dev/null +++ b/bot @@ -0,0 +1,5 @@ +#! /bin/sh + +echo "NICK $1" 1>&6 +echo "USER $1 $1 $1 :I'm a little printf, short and stdout." 1>&6 +exec ./dispatch ./irc ./$1-handler From 3df34ce677a79435c936361d8539102f9d10e9fa Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 7 Jan 2011 14:05:21 -0600 Subject: [PATCH 3/3] Make more portable --- irc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/irc.c b/irc.c index 6889050..24a7a96 100644 --- a/irc.c +++ b/irc.c @@ -108,7 +108,8 @@ main(int argc, char *argv[]) sender = parts[1]; forum = sender; } else if (0 == strcmp(cmd, "PING")) { - dprintf(1, "PONG :%s\r\n", text); + printf("PONG :%s\r\n", text); + fflush(stdout); } {