Plan 9 from Bell Labs’s /usr/web/sources/contrib/sl/rc/mother

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


#!/bin/rc
# Mother wants to talk to you.
# use with nupas/fs
rfork en
ramfs
argv0=$0
if(~ $#editor 0)
	editor=hold
mb=mbox
msg=()
fn d{
	if(test $1 -le $#rposts && test -d $rposts($1)){
		dposts=($dposts $1)
		flag +D $1
	}
	if not
		echo !address
}
fn deldposts{
	ndel=()
	for(i in $dposts){
		if(test $i -le $#rposts && test -d $rposts($i)){
			echo delete $mb $rposts($i) >/mail/fs/ctl
			ndel=($ndel $i)
		}
	}
	echo !$#ndel messages deleted
}
fn e{
	>/tmp/e
	eval $editor /tmp/e
	yn send
	if(~ $yn y)
		/bin/upas/marshal $* </tmp/e
}
fn flag{
	if(~ $1 [\+][aDdfrSs] [-][aDdfrSs]){
		{
			echo $1 >$rposts($2)^/flags
			puth $2
		} || echo !address
	}
}
fn fmtd{
	date=`{cat}
	switch($date(2)){
	case Jan;	mo=1
	case Feb;	mo=2
	case Mar;	mo=3
	case Apr;	mo=4
	case May;	mo=5
	case Jun;	mo=6
	case Jul;	mo=7
	case Aug;	mo=8
	case Sep;	mo=9
	case Oct;	mo=10
	case Nov;	mo=11
	case Dec;	mo=12
	}
	switch($date(3)){
	case [0-9]
		da=0^$date(3)
	case *
		da=$date(3)
	}
	switch($date(6)){
	case `{date | awk '{print $6;}'}
		ti=`{echo $date(4) | awk '{print substr($0,0,5);}'}
	case *
		ti=$date(6)
	}
	echo $mo/$da $ti
}
fn geth{
	for(i in $*){
		fcp $rposts($i)^/info /tmp/
		flags=`{sed -n 18p /tmp/info | sed 's/-//g'}
		mime=`{
			if(~ `{sed -n 7p /tmp/info} multipart*)
				echo H
		}
		size=`{sed -n 17p /tmp/info}
		date=`{sed -n 5p /tmp/info | fmtd}
		from=`{sed -n 1p /tmp/info}
		subject=`{sed -n 6p /tmp/info | awk '{print substr($0,0,50);}'}
		rm /tmp/info
		# unicode 00a0 divides message number from headline.
		# command input ignores everything after unicode 00a0.
		# these lines may be selected and sent to the prompt.
		echo '  '$"i' '$"mime' '$"flags'   '$"size'   '$"date' '$"from'	'$"subject
	}
}
fn getposts{ ls | grep -e '^[0-9]+$' | sort -n }
fn getr{
	switch($*){
	case ,;	echo $posts
	case ,*;	seq 1 `{echo $* | sed 's/,//g'}
	case *,;	seq `{echo $* | sed 's/,//g'} $posts($#posts)
	case *,*;	seq `{echo $* | sed 's/,/ /g'}
	case *;	echo $*
	}
}
fn h{ sed -n $1^p /tmp/h }
fn m{
	if(test $1 -le $#rposts && test -f $rposts($1)^/info){
		subject=`{sed -n 6p $rposts($1)^/info}
		if(! ~ $subject FWD:* Fwd:* fwd:*)
			subject=(Fwd: $subject)
		e -s $"subject -A $rposts($1)^/raw $*(2-)
		flag +a $1
	}
	if not
		echo !address
}
fn printhelp{
echo 'Commands are of the form [<range>] <command> [args]
<range> := <addr> | <addr>','<addr>
<command> :=
a		reply to sender and recipients
b		print the next ten headers
d		mark for deletion
e ...		enter message (args passed to upas/marshal)
g/regexp/cmd	grep headlines for regexp and run cmd on matches
h		print message headline (,h for all)
help		print this help message
m ...		forward mail to address(es)
mb ...		change to specified mailbox
p		print the processed message
P		print the raw message
q		quit
r		reply to message
s ...		store message in specified mailbox
u		remove deletion mark
y		synchronize with mail box
"		print message in quoted form, suitable for reply'
}
fn p{
if(test $1 -le $#rposts && test -f $rposts($1)^/header){
{ # avoid stutter by dumping everything everything into a file first
	cat $rposts($1)^/header
	echo
	if(~ `{sed -n 7p $rposts($1)^/info} multipart*){
		parts=`{ls -p $rposts($1) | grep -e '^[0-9]+'}
		body=1/body
		if(test -f $rposts($1)^/1/1/body)
			body=1/1/body
	}
	if not{
		parts=()
		body=body
	}
	if(~ `{file -m $rposts($1)^/$body} *html*){
		echo !/bin/htmlfmt
		htmlfmt -l60 -cutf8 $rposts($1)^/$body
	}
	if not
		cat $rposts($1)^/$body
	echo
	if(! ~ $#parts 0){
		parts=$parts(2-)
		for(j in $parts){
			type=`{sed -n 7p $rposts($1)^/$j/info}
			disp=`{sed -n 8p $rposts($1)^/$j/info}
			file=`{sed -n 9p $rposts($1)^/$j/info}
			if(! ~ $#file 0 || ~ $disp file inline){
				file='(file,'$"file')'
				fake=`{
					if(~ $file *.*)
						echo body.^`{echo $file | sed 's/^.*\.//g'}
					if not
						echo body
				}
				size=`{du $rposts($1)^/$j/$fake}
				echo !--- $rposts($1)^.$j $type $size(1) $file /mail/fs/$mb/^$rposts($1)^/$j/$fake
			}
		}
		parts=()
	}
} >/tmp/p
	cat /tmp/p
	go=1
	r=$1
	post=$1
	prompt=$1
	flag +s $1
}
if not
	echo !address
}
fn P{
	if(test $1 -le $#rposts && test -f $rposts($1)^/rawunix){
		cat $rposts($1)^/rawunix
		go=1
		r=$1
		post=$1
		prompt=$1
		flag +s $1
	}
	if not
		echo !address
}
fn puth{
	fcp $rposts($1)^/info /tmp/
	flags=`{sed -n 18p /tmp/info | sed 's/-//g'}
	mime=`{
		if(~ `{sed -n 7p /tmp/info} multipart*)
			echo H
	}
	size=`{sed -n 17p /tmp/info}
	date=`{sed -n 5p /tmp/info | fmtd}
	from=`{sed -n 1p /tmp/info}
	subject=`{sed -n 6p /tmp/info | awk '{print substr($0,0,50);}'}
	rm /tmp/info
	{
		echo $1
		echo c
		# remember: unicode 00a0 divides message number from headline.
		echo '  '^$1^' '$"mime' '$"flags'   '$"size'   '$"date' '$"from'	'$"subject
		echo .
		echo w
		echo q
	} | sam -d /tmp/h >/dev/null >[2=1]
}
fn r{
	if(test $1 -le $#rposts && test -f $rposts($1)^/info){
		subject=`{sed -n 6p $rposts($1)^/info}
		if(! ~ $subject RE:* Re:* re:*)
			subject=(Re: $subject)
		e -s $"subject $*(2-) `{sed -n 4p $rposts($1)^/info}
		flag +a $1
	}
	if not
		echo !address
}
fn s{
	if(test $1 -le $#rposts && test -f $rposts($1)^/raw){
		if(! test -d /mail/box/$user/$2)
			echo create $2 >/mail/fs/ctl
		fcp $rposts($1)^/raw /mail/box/$user/$2/^`{cat $rposts($1)^/fileid}
		flag +S $1
		echo !saved in $2
	}
	if not
		echo !address
}
fn u{
	if(test $1 -le $#rposts && test -d $rposts($1))
		flag -D $1
	if not
		echo !address
	dposts=`{grep -v $1 <{for(j in $dposts){ echo $j }}}
}
fn y{
	go=()
	r=$post
	if(! ~ $#dposts 0){
		deldposts
		dposts=()
	}
	if(! ~ $q 1){
		rposts=`{getposts}
		posts=`{seq 1 $#rposts}
		post=$posts(1)
		prompt=$post
		geth $posts >/tmp/h
		if(~ $#msg 0)
			echo $#posts messages
	}
}
fn yn{
	echo
	echo -n $* ' (y, n) '
	yn=`{read}
	switch($yn){
	case y n
		;
	case *
		yn
	}
}
fn '"' { p $1 | sed 1d | sed 's/^/> /g' | sed 's/^> >/>>/g' }
fn usage{
	echo usage: $argv0 [ -d ] [ -f mbox ] >[1=2]
	exit usage
}
while(~ $1 -*){
	switch($1){
	case -d;	debug=1
	case -f;	mb=$2; shift
	case -p;	msg=$2; shift
	case *;	usage
	}
	shift
}
if(! ~ $#* 0)
	usage
if(! test -f /mail/fs/ctl)
	/bin/upas/fs >[2]/dev/null
if(! ~ $mb mbox)
	echo open /mail/box/$user/$mb $mb >/mail/fs/ctl
builtin cd /mail/fs/$mb
y
if(! ~ $#msg 0)
	p $msg
while(){
	echo -n $"prompt': '
	# command input ignores everything after unicode 00a0.
	rcmd=`{read | sed 's/[ ].*$//g' | sed 's/^([0-9]+)?(,)?([0-9]+)?/& /g'}
	switch($rcmd){
	case ,* [0-9]*
		r=`{getr $rcmd(1)}
		cmd=$rcmd(2-)
		if(~ $#cmd 0)
			cmd=p
	case *
		r=$post
		cmd=$rcmd
	}
	switch($cmd){
	case a a' '*
		for(i in $r)
			r $i $cmd(2-) `{sed -n 2,3p $rposts($i)^/info | sort -n | uniq}
		post=$r($#r)
		prompt=$post
	case b
		r=`{seq $r(1) `{echo $r(1)^+10|bc}}
		if(test $r($#r) -gt $posts($#posts))
			r=`{seq $r(1) $posts($#posts)}
		if(! ~ $#r 0 && test $r(1) -le $posts($#posts)){
			sed -n $r(1)^,$r($#r)^p /tmp/h
			post=$r($#r)
			prompt=$post
		}
		if not
			echo !address
	case d
		for(i in $r)
			d $i
		post=$r($#r)
		prompt=$post
	case e' '*
		e $cmd(2-)
	case g/*
		regexp=`{echo $cmd | awk -F '/' '{print $2;}'} # BUG: / is stripped from regexp and cmd
		cmd=`{echo $cmd | awk -F '/' '{$1=""; $2=""; print;}'}
		r=`{</tmp/h grep -e $"regexp | sed 's/ .*$//g'} # strip everything after unicode 00a0.
		if(~ $#cmd 0)
			cmd=p
		if(~ $cmd d h p P u '"'){
			for(i in $r)
				eval $cmd $i
		}
		if not if(~ $cmd a' '* m' '* r' '* s' '*){
			for(i in $r)
				eval $cmd(1) $i $cmd(2-)
		}
		if not
			echo !illegal command
		post=$r($#r)
		prompt=$post
	case h
		for(i in $r)
			h $i
		post=$r($#r)
		prompt=$post
	case help
		printhelp
	case m' '*
		for(i in $r)
			m $i $cmd(2-)
		post=$r($#r)
		prompt=$post
	case mb' '*
		nmb=$cmd(2-)
		if(~ $nmb mbox || test -d /mail/box/$user/$nmb){
			dposts=()
			mb=$nmb
			if(! ~ $mb mbox)
				echo open /mail/box/$user/$mb $mb >/mail/fs/ctl
			builtin cd /mail/fs/$mb
			y
			post=$posts(1)
			prompt=$post
		}
		if not
			echo !/mail/box/$user/$nmb does not exist
	case p
		for(i in $r)
			p $i
	case P
		for(i in $r)
			P $i
	case q
		q=1 y # BUP STOP
		exit
	case r r' '*
		for(i in $r)
			r $i $cmd(2-)
		post=$r($#r)
		prompt=$post
	case s' '*
		for(i in $r)
			s $i $cmd(2-)
		post=$r($#r)
		prompt=$post
	case u
		for(i in $r)
			u $i
		post=$r($#r)
		prompt=$post
	case y
		y
	case '"'
		for(i in $r)
			'"' $i
		post=$r($#r)
		prompt=$post
	case '?'
		echo dposts: $dposts
		echo rposts: $rposts
		echo posts: $posts
		echo post: $post
		echo r: $r
	case *
		if(~ $post $posts(1) && ~ $#go 0)
			p $post
		if not if(! ~ $post $posts($#posts)){
			post=`{echo $post^+1 | bc}
			if(test $post -gt $posts($#posts))
				post=$posts($#posts)
			p $post
		}
	}	
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.