ref: 5d361d64d599b0d4d87f990e3e2a1b806d1bbb37
dir: /gridirc.rc/
#!/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\r\n", $2, chan
fflush
next
}
doprint && NF >= 2 {
if (length($2) == 1){
sender=$1
$1=""
$2=""
sub(/^ +/, "")
printf ":%s PRIVMSG #%s :%s\r\n", sender, chan, $0
} else {
printf ":%s PRIVMSG #%s :%s\r\n", chan, chan, $0
}
fflush
next
}
doprint {
printf ":%s PRIVMSG #%s :%s\r\n", chan, chan, $0
fflush
}
' /n/chat/$1 &
echo $apid >>/env/gridircpids
}
# mainloop
awk '
BEGIN {
nick="user"
user="user"
host="9p.zone"
}
$1 ~ /^:/ {
$1=""
sub(/^ */, "")
}
NF >= 2 && $1 ~ /^NICK$/ {
nick=$2
}
NF >= 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\r\n", host, nick
fflush
}
NF >= 2 && $1 ~ /^JOIN$/ {
sub(/^:?#/, "", $2)
printf ":%s JOIN #%s\r\n", nick, $2
printf ":%s 332 #%s :Channel %s\r\n", host, $2, $2
if (joined[$2] == 1)
next
printf ":%s 353 #%s :%s %s\r\n", host, $2, $2, nick
printf ":%s 366 #%s :End of NAMES list\r\n", host, $2
joined[$2]=1
cmd=sprintf("startread %s %s &", $2, nick)
system(cmd)
fflush
}
NF >= 2 && $1 ~ /^PRIVMSG$/ {
sub(/^#/, "", $2)
chan=$2
sub(/^[^:]*:/, "")
file=sprintf("/n/chat/%s", chan)
printf "%s → %s\n", nick, $0 >> file
fflush
}
NF >= 2 && $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\r\n", $0
fflush
}
$1 ~ /^QUIT$/ {
exit
}
$1 ~ /^VERSION/ || $1 ~ /^INFO/ {
printf ":%s 351 %s :gridirc - bridge to IRC\r\n", host, nick
fflush
}
'
managedexit