shithub: gridirc

ref: 034adcbd92e08fa1a2023bf3c0fbe9612f99d6ea
dir: /gridirc.rc/

View raw version
#!/bin/rc

rfork en
srv net!chat.9p.zone!9990 gridirc.$pid /n/chat || {
	echo Fail!
	exit
}
echo -n >/env/gridircpids

fn managedexit{ # exit routine
	pids=`{cat /env/gridircpids}
	for (i in $pids){
		echo killing reader $i
		echo kill >/proc/$i/ctl
	}
	rm /srv/gridirc.$pid
	exit
}

fn sigint{
	managedexit
}

fn sighup{
	managedexit
}

fn startread{
currentdate=`{date -u}
echo JOIN $2 $currentdate >>/n/chat/$1
awk -v 'chan='^$1 -v 'nick='^$2 '
BEGIN { doprint=0 }
$0 ~ ENVIRON["currentdate"] { doprint=1 }
!doprint { next }

doprint && NF >= 2 && $1 ~ nick && $2 ~ /^→$/ {
	next
}

doprint && NF > 1 && $1 ~ /^JOIN$/ {
	printf ":%s JOIN #%s\n", $2, chan
	fflush
	next
}

doprint && NF >= 2 {
	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
	}
	fflush
	next
}

doprint {
	printf ":%s PRIVMSG #%s :%s\n", chan, chan, $0
	fflush
}
' /n/chat/$1 &
echo $apid >>/env/gridircpids
}

# mainloop
awk '
BEGIN {
	nick="user"
	user="user"
	host="9p.zone"
}

NF >= 2 && $1 ~ /^NICK$/ {
	nick=$2
}

NF >= 2 && $1 ~ /^USER$/ {
	user=$2
	printf "001 %s :Welcome %s!%s@%s\n", nick, nick, user, host
	fflush
}

NF >= 2 && $1 ~ /^JOIN$/ {
	sub(/^:?#/, "", $2)
	printf ":%s JOIN #%s\n", nick, $2
	printf "332 #%s :Channel %s\n", $2, $2
	printf "353 #%s :%s %s\n", $2, $2, nick
	printf "366 #%s :End of NAMES list\n", $2
	cmd=sprintf("startread %s %s &", $2, nick)
	system(cmd)
	fflush
}

NF >= 2 && $1 ~ /^PRIVMSG$/ {
	sub(/^#/, "", $2)
	chan=$2
	sub(/^[^:]*:/, "")
	printf "%s → %s\n", nick, $0 >> "/n/chat/chat"
	fflush
}

NF >= 2 && $1 ~ /^PART$/ {
	sub(/^#/, "", $2)
	chan=$2
}

$1 ~ /^PING$/ {
	sub(/^PING/, "PONG")
	printf "%s\n", $0
	fflush
}

$1 ~ /^QUIT$/ {
	exit
}
'

managedexit