shithub: gridirc

ref: 5bba44eae97153d0cc3ee5a08d014c9a38a850e4
dir: /gridirc.rc/

View raw version
#!/bin/rc

rfork en
test -r /srv/gridircmq || {
	/usr/sirjofri/src/gridirc/gridircprep
}
mount /srv/gridircmq /n/chat

nick=user
user=user
prefix=9p.zone
joined=false
pids=()

fn sigint{
	for (i in $pids)
		echo kill>/proc/$i/ctl
	exit
}

fn sighup{
	for (i in $pids)
		echo kill>/proc/$i/ctl
	exit
}

fn parsed{
echo $* | awk 'NF >= 3 {
	chan=ENVIRON["currentchannel"]
	if (length($2) == 1){
		sender=$1
		$1=""
		$2=""
		sub(/^ +/, "")
		printf ":%s PRIVMSG #%s :%s\n", sender, chan, $0
	} else {
		printf ":%s PRIVMSG #%s :%s\n", chan, chan, $0
	}
	next
}
{
	printf ":%s PRIVMSG #%s :%s\n", chan, chan, $0
}'
}

fn loopread{
currentchannel=$1
while(c=`{read})
	parsed $c
	#echo :$1 PRIVMSG '#'$1 :$"c
}

fn startread{
	{ cat /n/chat/$1 | loopread $1 } &
	pids=($pids $apid)
}

fn loop{
cmd=`{read}
switch($cmd(1)){
case NICK
	nick=$cmd(2)
	#echo nick $cmd(2)
case USER
	user=$cmd(2)
	echo 001 $nick :Welcome $nick!$user@$prefix
case JOIN
	ch=`{echo $cmd(2) | sed 's/^:?#//g'}
	echo :$nick JOIN '#'^$ch
	echo 332 '#'^$ch :Channel $ch
	echo 353 '#'^$ch :$ch $nick
	echo 366 '#'^$ch :End of NAMES list
	startread $ch
case PRIVMSG
	ch=`{echo $cmd(2) | sed 's/^#//g'}
	msg=`{echo $cmd | sed 's/^[^:]*://'}
	echo $nick → $msg >>/n/chat/$ch^WRITE
	#echo msg: $msg
case PING
	echo $cmd | sed 's/PING/PONG/'
case QUIT
	for (i in $pids)
		echo kill>/proc/$i/ctl
	exit
case *
	#echo ignored
}
loop
}

loop