Plan 9 from Bell Labs’s /usr/web/sources/contrib/akumar/α/irc/irc-nofmt

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/bin/rc

rfork

. acme.rc
mainpid=$pid
nl='
'
imfs=/n/irc/freenode
ipipe=/n/ircpipe

bind '#|' $ipipe

fn ircwin {
	datafd=''
	winid=$1
	shift
	echo -n scroll > /mnt/acme/$winid/ctl
	echo -n NoScroll Join > /mnt/acme/$winid/tag
	if(~ $1 main){
		winname irc
		{cat $imfs/event | winwrite data}&
	}
	if(~ $1 sub){
		datafd=$3/data
		winname $2

		dpipe=/n/dpipe
		spipe=/n/spipe
		bind '#|' $dpipe
		bind '#|' $spipe

		{
			awk '{print "IRC", $0; fflush}' < $datafd >$dpipe/data &
#			(FIX: this proc does not get killed ^)

			awk '
				/^IRC/{
					if(lock != 1) {
						printf("length: %s in '^$winid^'\n", length($0)) > "/fd/2";
						sub("^IRC", "");
						print $0;
						fflush;
					} else {
						printf("locked\n") > "/fd/2";
					}
				}

				/^hold/{
					lock = 1;
					printf("ok\n") > "'^$spipe/data^'";
					close("'^$spipe/data^'");
				}
				/^unhold/{
					lock = 0;
					fflush;
				}
			' < $dpipe/data1 > /mnt/acme/$winid/data <[5]/mnt/acme/$winid/addr
		}&
	}
	catpid=$apid
	wineventloop
	echo DEBUG killing: $apid
	echo kill > /proc/$catpid/notepg
}

fn event {
	switch($1$2){
	case KI	# keyboard: char insterted in body
		if(! ~ $datafd '' && ~ $9 $nl){
			pos=`{cat /mnt/acme/$winid/addr}
			p1=$pos(1)
			if(test $3 -gt $p1){
				{echo hold > $dpipe/data; read}<$spipe/data1
				echo -n 'dot=addr' >/mnt/acme/$winid/ctl
				echo -n '.,' > /mnt/acme/$winid/addr
				pos=`{cat /mnt/acme/$winid/addr}
				p1=$pos(1)
				line = `{cat /mnt/acme/$winid/data}
				echo -n '#'$p1,'#'$pos(2) > /mnt/acme/$winid/addr
				echo -n '' > /mnt/acme/$winid/data
				echo unhold > $dpipe/data
				echo -n $line > $datafd
			}
			if not echo -n '$' > /mnt/acme/$winid/addr
		}
	case Mx	# button 2 in tag
		args=`{echo $9} # turn string into list
		if(~ $args(1) Join)
			echo join ''^$args(2-)^'' > $imfs/ctl
#			(TODO: auto-open window on new join)

		if(~ $9 Reconn && ~ $winid $topwin)
			echo -n reconnect > $imfs/0/ctl
		if(~ $9 NoScroll) echo -n noscroll > /mnt/acme/$winid/ctl
		if(~ $9 Scroll) echo -n scroll > /mnt/acme/$winid/ctl
		if(~ $9 Del){
			if(~ $winid $topwin)
				echo Del > $ipipe/data
			if not{
				echo -n 'Del '^$winid > $ipipe/data
				winwriteevent $*
			}
		}
		if(! ~ $9 Del && ! ~ $9 Reconn && ! ~ $9 NoScroll && ! ~ $9 Scroll && ! ~ $args(1) Join)
			winwriteevent $*
	case ML	# button 3 in body
		args=`{echo $9}
		echo Open $args > $ipipe/data
#		(TODO: handle new user?)
		found=`{read $ipipe/data}
		if(~ $found none) winwriteevent $*
	}
}


#TODO
fn mainproc {
	while() {
		m=`{read $ipipe/data1}
		switch($m(1)) {
		case Open
			for(j in $m(2-))		# O(n^2) but data is small
			for(i in $imfs/[0-9]*){
				n=`{cat $i/name}
				if(~ $n $j){
					match=1
					show=`{awk '
						$6 == "irc/'$n'"{ print $1 }
						' /mnt/acme/index}
					if(! ~ $show ''){
						echo show > /mnt/acme/$show/ctl
						show=''
					}
					if not{
						newwindow
						winds=($winid $winds)
						ircwin $winid sub irc/$n $i &
					}
				}
			}
			if(~ $match '0') echo none > $ipipe/data1
			if(~ $match '1') echo -n '' > $ipipe/data1
			match=0
		case Del
			if(~ $m(2) ''){
				for(i in $winds)
					echo delete > /mnt/acme/$i/ctl
				echo DEBUG exiting...
				exit
			}
			if not winds=`{echo $winds | sed 's/( |^)'^$m(2)^'( |$)/ /g'}
		} #dostuff
	}
}

# startup: create main
# window
newwindow
winds=($winid)
topwin=$winid
ircwin $winid main &

# begin the main proc
mainproc

exit

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].