Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/rc/chugly

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/bin/rc

# this script is a bit basic and was just to get a bit of understanding
# in doing network stuff in rc script
# I lifted some of it directly from Russ Cox's rlogin shells script
# Boyd Roberts inspired me to go with awk
# Jonathan Sergent helped me read $netdir/data a line at a time using read

# you can write messages to it's data file to perform custom irc commands
# eg. 
# echo 'PRIVMSG JOIN #plan9' > net/tcp/21/data
# and chugly will monitor the plan9 channel
# output still goes to $channel though

# bind '#|' /tmp; cd /tmp; window -m sirc < data; cat > data1



# these are the parameters it uses. The are not on the command line
# so that I didn't have to write command parsing code

## network
clonefile = /net/tcp/clone
ircserver = 210.15.254.126!6667
## user info
botnick = chugly
## channel to output commands to and join on connect
channel = '#plan9'

fn do_query {
	hget  'http://sal.iswhatweneed.com/cgi-bin/AskSal.cgi?question=' ^$q | awk 'BEGIN { RS="<"; FS=">" } { gsub("\n", "") } substr($2, 0, 6) == "Sal is" { inanswer = 0} match($2,  /^Google for/) { inanswer=0} match($2,  /^DMOZ for/) { inanswer=0} match($2, /Top Encyclopaedia page for/) { inanswer=0} $2 != "" && inanswer==1 { sub(/^ : /, "", $2) ; print $2} $2 == "Sal says" { inanswer=1} '
}


fn ask {
	oldifs = $ifs

	ifs = ' '
	asn = `{do_query}

	line_cnt = `{echo $asn | wc -l}

	top_5 = `{echo $asn | sed 5q }

	echo $top_5

	if (echo '5 - ' ^$line_cnt | bc | grep -s '^-') { echo ...}

	ifs = $oldifs

}


<[4] $clonefile {
	netdir=`{basename -d $clonefile} ^ / ^  `{cat /fd/4}
	# write connect string to /net/tcp/N/ctl
	echo connect $ircserver to $netdir/ctl
	echo connect $ircserver >$netdir/ctl || exit 'cannot connect'
	inchan = no
	{
		while (~ `{cat $netdir/status} Established*) {
			line=(`{read})

			switch ($line(4)) {
			case Welcome
				echo connecting
				echo USER $botnick plan9.bell-labs.com cuntbubble.com ChuglyV0.3 > $netdir/data
				echo NICK $botnick> $netdir/data
			case :chugly
				oldifs = $ifs
				ifs='
'
				q = `{echo $line | tr -d 
 | awk '{for(i=5; i<=NF; i++) printf "%s ", $i; printf "\n"}' | url_encoder}

				for (a in `{ask}) {
					echo PRIVMSG $channel :$a > $netdir/data
					sleep 1
				}

				ifs = $oldifs
			}
			
			if(~ $line(1) PING) { 
				echo PONG $line(2) > $netdir/data
				if (~ $inchan no) {
					echo JOIN $channel > $netdir/data
					echo PRIVMSG $channel :$botnick ask away > $netdir/data
			
					inchan = yes
				}
			}

			echo $line
		}
	} < $netdir/data
}
	


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.