shithub: rc

ref: c1a707427fbbb5129e467516fc671e976ee3e398
dir: /wircrc/

View raw version
#!/bin/rc
# by cinap_lenrek

wins=()
cons=()
chans=()

server=irc.oftc.net
realname='<nil>'
nick=$user

bind '#|' /n/wircrc

rfork en

fn wmk {
	id=$1
	>/n/wircrc/data1 {
		@{
			rfork s
			d=/n/wircrc.$id
			p=`{cat /dev/ppid}
			mount $wsys $d 'new -scroll -pid '^$p || exit
			echo -n $id >$d/label
			echo wnew $id $d $p
			<$d/cons {
				while(line=`{read}){
					~ $line '' || echo wtype $id $line
				}
			}
		}
		echo wclose $id
	}
	exit
}

fn wio {
	if(~ $1 $wins){
		d=wd$1
		d=$$d
		shift
		@{echo $* >$d/cons}
	}
}

fn sio {
	d=$1
	if(~ $d *!*)
		d=`{echo $d | awk -F'!' '{print $1}'}
	if(~ $d $cons){
		d=cd$d
		d=$$d
		shift
		@{echo $* >$d/data}
	}
}

fn wctl {
	if(~ $1 $wins){
		d=wd$1
		d=$$d
		shift
		@{echo $* >$d/wctl}
	}
}

fn hangup {
	if(~ $1 $cons){
		d=cd$1
		d=$$d
		@{echo hangup >$d/ctl} &
	}
}

fn netdial {	# insert tlsclient here
#cinap_lenrek → netdial gets called with some program args
#cinap_lenrek → that it expects to be run with the network connection on stdin/stdout
#cinap_lenrek → which is basically what tlsclient already does :)
#cinap_lenrek → so instead of manually open tcp files and shit and writing connect and then redirecting data file
#cinap_lenrek → you could just run tlsclient in there
#cinap_lenrek → or do it in csdial
#cinap_lenrek → its the same thing
#cinap_lenrek → basically, csdial does the name resolution shit
#cinap_lenrek → calling netdial with each address resolved
#cinap_lenrek → it shouldnt be too hard to understand
	<$1 {
		conn=`{basename -d $1}^/^`{read}
		echo -n connect $2 >$conn/ctl >[2]/dev/null || exit 'connect failed'
		<$conn/data >$conn/data {
			shift
			shift
			$*
			exit
		}
	}
}
fn csdial {
	echo $2 | ndb/csquery $1/cs | sed 's/> //g;/^$/d' | 
		while(l=`{read} && ~ $#l 2 && ! @{netdial $l $*(3-)}){
		status=failed
	}
}
fn dial {
	csdial /net $* || csdial /net.alt $*
}

fn irc {
	>[3]/n/wircrc/data1 {
		echo cnew $1 $conn >[1=3]
		tr -d '
' | @{
			while(line=`{read}){
				#echo xxx $line >/dev/cons
				if(~ $line(1) PING)
					echo PONG $line(2-)
				if not {
					line=`{echo $line | sed '
s/^:([^!]+)[^\ ]+\ PRIVMSG\ (#[^\ ]+)[^:]+:(.*)$/cmsg '$1'!\2 \1 \3/g;
s/^:([^!]+)[^\ ]+\ PRIVMSG\ :?([^\ ]+)[^:]+:(.*)$/cmsg '$1'!\1 \1 \3/g;
s/^:([^!]+)[^\ ]+\ NOTICE\ \*[^:]+:(.*)$/cmsg '$1' \1 \2/g;
s/^:([^!]+)[^\ ]+\ NOTICE\ ([^\ ]+)[^:]+:(.*)$/cmsg '$1'!\1 \1 \3/g;
s/^:([^!]+)[^\ ]+\ ([0123456789]+)[^:]+:?(.*)$/xmsg '$1' \2 \3/g;
s/^:([^!]+)[^\ ]+\ (MODE|TOPIC)\ (#[^\ ]+)\ :?(.*)$/xmsg '$1'!\3 \2 \4/g;
s/^:([^!]+)[^\ ]+\ (QUIT|NICK)\ :(.*)$/xmsg '$1' \2 \1 \3/g;
s/^:([^!]+)[^\ ]+\ (JOIN|PART)\ (#[^\ ]+).*$/xmsg '$1'!\3 \2 \1/g;
'}
					~ $line '' || echo $line >[1=3]
				}
			}
		}
		echo cclose $1 >[1=3]
	}
}

fn shutdown {
	for(i in $cons)
		hangup $i
	for(i in $wins){
		d=wp$i
		d=$$d
		@{echo hangup >/proc/$d/notepg} &
	}
	exec cat /n/wircrc/data >/dev/null
}

fn sighup {
	shutdown
}

fn sigint {
	shutdown
}

wmk $server &
dial tcp!$server!6667 irc $server &

</n/wircrc/data {
	while(a=`{read}){
		id=$a(2)
		switch($a(1)){
		case cnew
			cd$id=$a(3)
			cons=($id $cons)
			sio $id USER $user foo bar :$realname
			sio $id NICK $nick
			for(i in $chans){
				if(~ $i $id!'#'*)
					sio $id JOIN `{echo $i | awk -F'!' '{print $2}'}
			}
		case cclose
			d=cd$id
			$d=()
			oids=$cons
			cons=()
			for(i in $oids){
				if(! ~ $i $id)
					cons=($i $cons)
			}
			for(i in $wins){
				if(~ $i $id || ~ $i $id!*)
					wio $i HUNGUP $id
			}
		case wnew
			wd$id=$a(3)
			wp$id=$a(4)
			wins=($id $wins)
			wio $id '---' $id '---'
		case wclose
			d=wd$id
			p=wp$id
			unmount $$d
			$d=()
			$p=()
			oids=$wins
			wins=()
			for(i in $oids){
				if(! ~ $i $id)
					wins=($i $wins)
			}
			if(~ $id $chans){
				oids=$chans
				chans=()
				for(i in $oids){
					if(! ~ $i $id)
						chans=($i $chans)
				}
				sio $id PART `{echo $id | awk -F'!' '{print $2}'}
			}
		case xmsg
			if(~ $id $wins)
				wio $id $a(3-)
			if not {
				for(i in $wins){
					if(~ $i $id!*)
						wio $i $a(3-)
				}
			}
		case cmsg
			if(~ $id $wins)
				wio $id $a(3) '→' $a(4-)
			if not if(! ~ $id *!'#'*){
				id=`{echo $id | awk -F'!' '{print $1}'}
				for(i in $wins)
					if(~ $i $id || ~ $i $id!*)
						wio $i /t $a(3) '→' $a(4-)
			}
		case wtype
			switch($a(3)){
			case /x
				shutdown
			case /s
				server=$a(4)
				if(! ~ $server '' && ! ~ $server $cons){
					if(! ~ $server $wins)
						wmk $server &
					dial tcp!$server!6667 irc $server &
				}
			case /j /t
				target=$a(4)
				server=`{echo $id | awk -F'!' '{print $1}'}
				if(! ~ $target '' && ~ $server $cons){
					id=$server!$target
					if(~ $id $wins){
						wctl $id unhide
						wctl $id current
					}
					if not {
						wmk $id &
					}
					if(~ $a(3) /j && ~ $target '#'*){
						if(! ~ $id $chans){
							chans=($id $chans)
							sio $server JOIN $target
						}
					}
				}
			case /h
				if(~ $a(3) $cons)
					id=$a(3)
				hangup `{echo $id | awk -F'!' '{print $1}'}
			case *
				target=`{echo $id | awk -F'!' '{print $2}'}
				if(~ $target '')
					sio $id $a(3-)
				if not {
					a=$a(3-)
					sio $id PRIVMSG $target ':'^$"a
				}
			}
		}
	}
}
shutdown