Subject: more funky things to do with sam's pipe

From: Erik Quanstrom <[email-masked]>
Date: Fri, 21 Apr 1995 04:35:48 -0400

this is a little, really unsophisticated mail reader
that i cooked up using rc/sam/9menu. i had 
to do a few ugly things because 9menu doesn't
look at $SHELL. (like create a directory
$h/m with dummy files 0 D d h n p q rest s w)

but what it does is use se's to split 
$MAIL into records. you can delete forward
or delete backward. (FIFO or LIFO).

in retrospect, i should have hacked 9term.

also, running in unix, the sam pipe is a pain
to use. it would be nice if
1] writes to sampipe would block until the command
   actually finished.
2] writes to sampipe would fail if the command
   didn't succede. this would allow one to deal
   correctly with the first and last message in 
   the file.

i just don't know how to do either of those 
with unix.

::$h/m/cmd::
#!/tmp/rc

# get pipe
if (~ $sampipe ()) {
	if (~ $#USER 0) {
		USER=$LOGNAME
	}
	sampipe=/tmp/.sam.$USER
	if (! ~ $#DISPLAY 0) {
		sampipe=$sampipe.$DISPLAY
	}
	if (! test -p $sampipe) {
		sam &
		sleep 1 # this is a timing problem
	}
}
# run command
if (! echo $* >> $sampipe) {
	echo >[1=2] sam command failed
	exit 1
}

::$h/m/^(0 D d h n p q rest s w)::
#!/tmp/rc
$0 $*

::sam_mail::
#!/tmp/rc
. $h/bin/sam_mail_
cd $h/m
sleep 1
cmd '$'
cmd '-/^From /;$' 
9menu delete:d next:n previous:p  delete:D rest:rest top:0 hee:h save:s write:'rc -c ''cmd w''' quit:q exit

::sam_mail-::
#!/tmp/rc
pipe=`{sampipe}
echo fu
if (~ $USER ()){
	USER=$LOGNAME
}
if (~ $MAIL ()) {
	B /usr/spool/mail/$USER
}else{
	B $MAIL
}
fn cmd { echo $* >> $pipe || echo 'write to sam failed' >[1=2]}
fn mail_select {cmd '/^From /;/^From /-1' }
fn mail_select_previous {cmd '-/^From [a-zA-Z]*/;/^From /-1' }
fn rest {cmd '.,$'}
fn debug {}
fn 0 {cmd 0}
fn d {cmd d;n}
fn D {cmd d; cmd k ; cmd '-/^From [a-zA-Z]*/;''' }
fn h {cmd '.,$ x/^From .*\n/ p'}
fn q {cmd w ; cmd q}
fn delay {cat /etc/motd > /dev/null}
fn n {
	if (~ $#* 0) {
		mail_select 
		return
	}
	for (i) {
		switch ($i) {
		case [1-9]*
			if (! echo -n $i | grep '[1-9][0=9]*'>/dev/null) {
				echo 'bad numeric argument' >[1=2]
				return 1
			}
			# evil, i know.
			# if you've got better ideas go for it!
			eval `{yes mail_select';' | sed $i^q}
		case *
			mail_select $i
		}
	}
}
fn p {
	if (~ $#* 0) {
		mail_select_previous 
		return
	}
	for (i) {
		switch ($i) {
		case [1-9]*
			if (! echo -n $i | grep '[1-9][0=9]*'>/dev/null) {
				echo 'bad numeric argument' >[1=2]
				return 1
			}
			# evil, i know.
			# if you've got better ideas go for it!
			eval `{yes mail_select_previous';' | sed $i^q}
		case *
			mail_select_previous $i
		}
	}
}

fn s {
	if (! ~ $#* 0 1) {
		echo 's takes 0/1 argument' >[1=2]
		return 1
	} else if (~ $#* 0) {
		file = /tmp/sammail$pid
	} else {
		file = $1
		if (test -f $file) {
			echo >[1=2] $file 'exists; won''t overwrite.'
			return 1;
		}
	}
	debug $file
	cmd '> cat > ' $file
	delay
	test -f $file || {
		echo >[1=2] could not create temporary file $file^.
		return 1
	}
	if (~ $#* 0) {
		file_sam_mail $file
	}
}

#!/tmp/rc
# sam_mdisperse
# object: file away all email carefully
# erik quanstrom, 14. oct 94
# 
nl = '
'
xlate = ``$nl {cat $home/.mxlate | sed 's/#.*//g'}
fn getdate {
 	awk '/^From ./ {printf "%s-%s-%s\n", $5, $4, substr($7,3,2)}' < $1 | \
		tr '[A-Z]' '[a-z]'
}

fn xl {
	n=() site=() name=() i =() j=() {
		n=``('@' $nl){echo $1}
		site=$n(2) # what's a ``host''?
		name=$n(1)
		for(i in $xlate){
			if(~ $i *^$name^*^$site^*){
				j=`{echo $i}
				echo $j(3)
				break
			}
		}
	}
}
#sed -n 's/^From \([A-Za-z][A-Za-z!]*@[A-Za-z.][A-Za-z.]*\).*/\1/p'
fn parse_addr {
	sed 's/^From \([A-Za-z][A-Za-z]*@[A-Za-z.][A-Za-z.]*\).*/\1/'$nl'q' <$1
}
fn file_sam_mail {folder = () which=() name=() date=() dest=(){
	for (folder in $*) {
		which = from
		name = `{xl `{parse_addr $folder} }
		if (~ $name ()) { 
			echo >[2=1] unrecognized name $name^. $folder not moved.
			return 1
		} else {
			if (test -f $folder) {
				date = `{getdate $folder}
				dest = $home/e-letters/$which-$name/$which-$name-$date
				echo >[2=1]  $folder $dest
				mv $folder $dest
				return $status
			} else {
				echo >[2=1] 'folder does not exist'
				return 1
			}
		}
	}
}}