shithub: gridirc

ref: 0491d35b5d1db1b0193e4103716a734f6fd68f93
dir: /gridirc.rc/

View raw version
#!/bin/rc

rfork en

echo Beginning gridirc session $pid >[1=2]

srv net!chat.9p.zone!9990 gridirc.$pid /n/chat || {
	echo Unable to srv chat >[1=2]
	exit
}
echo -n >/env/gridircpids

fn managedexit{ # exit routine
	unmount /n/chat
	rm /srv/gridirc.$pid
	echo Attempting to exit >[1=2]
	pids=`{cat /env/gridircpids}
	for (i in $pids) {
        echo Killing process $i >[1=2]
        chmod 777 /proc/$i/ctl
		echo kill >>/proc/$i/ctl
	}
	exit
}

fn sigint{
	managedexit
}

fn sighup{
	managedexit
}

fn startread{
currentdate=`{date -u}
host=$1
chan=$2
nick=$3

# commented out to prevent spam
# echo JOIN $nick $currentdate >>/n/chat/$chan

awk '
BEGIN {
	host=ENVIRON["host"]
	chan=ENVIRON["chan"]
	nick=ENVIRON["nick"]
	doprint=0
}
$0 ~ ENVIRON["currentdate"] {
	doprint=1
	next
}
!doprint { next }

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

function sendMsg(origin, recip, msg) {
	printf ":%s PRIVMSG %s :%s\r\n", origin, recip, msg
	fflush
}

doprint {
	if (NF >= 2 && length($2) == 1){
		sender=$1
		$1=""
		$2=""
		sub(/^ +/, "")
		sendMsg(sender, sprintf("#%s", chan), $0)
	} else {
		sendMsg(host, sprintf("#%s", chan), $0)
	}
	next
}
' /n/chat/$chan &
echo $apid >>/env/gridircpids
}

# mainloop
tr -d 
 | awk -mr 512 '
BEGIN {
	RS="\n"
	ORS="\r\n"
	nick="nonick"
	user="nouser"
	host="9p.zone"
}

function handleJoin(chan) {
	printf ":%s JOIN #%s\r\n", nick, chan
	printf ":%s 332 #%s :Channel %s\r\n", host, chan, chan
	printf ":%s 353 = #%s :%s\r\n", host, chan, nick
	printf ":%s 366 #%s :End of NAMES list\r\n", host, chan
	joined[chan]=1
	cmd=sprintf("startread %s %s %s &", host, chan, nick)
	system(cmd)
	fflush
}

$1 ~ /^:/ {
	$1=""
	sub(/^ */, "")
	fflush
}

$1 ~ /^NICK$/ {
	nick=$2
}

$1 ~ /^USER$/ {
	user=$2
	printf ":%s 001 %s :Welcome %s!%s@%s\r\n", host, nick, nick, user, host
	printf ":%s 002 %s :Your host is %s\r\n", host, nick, host
	printf ":%s 003 %s :This server was created some time ago\r\n", host, nick
	printf ":%s 004 %s gridirc 0.1-alpha i ov\r\n", host, nick
	printf ":%s 375 %s :- %s Message of the day -\r\n", host, nick, host
	printf ":%s 372 %s :- Welcome to the grid! -\r\n", host, nick
	printf ":%s 372 %s :- Thanks to all contributors to 9p.zone! -\r\n", host, nick
	printf ":%s 376 %s :End of /MOTD command\r\n", host, nick
	handleJoin("chat")
	fflush
}

$1 ~ /^JOIN$/ {
	sub(/^:?#/, "", $2)
    handleJoin($2)
}

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

$1 ~ /^PART$/ {
	sub(/^#/, "", $2)
	chan=$2
	$1=""
	sub(/^[^:]*:/, "")
	file=sprintf("/n/chat/%s", chan)
	printf "IRC user %s parted: %s\n", nick, $0 >> file
	printf ":%s PART #%s\r\n", nick, chan
	fflush
}

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

$1 ~ /^QUIT$/ {
	exit
}

#$1 ~ /^VERSION/ || $1 ~ /^INFO/ {
#	printf ":%s 351 %s :gridirc - bridge to IRC\r\n", host, nick
#	fflush
#}
'

managedexit