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

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


#!/bin/rc
rfork en
ramfs
if(~ $#editor 0)
	editor=hold
argv0=$0
all=()
debug=()
mfile=/tmp/m.$user.$pid
new=()
quote=()
sort=(sort -nr)
fn delete{
	del=()
	for(i in $*){
		if(test -d /mail/fs/mbox/^$q($i)){
			echo delete mbox $q($i) >/mail/fs/ctl
			del=($del $i)
		}
	}
	echo !$#del messages deleted
}
fn flag{
	for(i in $r)
		if(~ $1 [\+][aDdfrSs] [-][aDdfrSs])
			if(test -d /mail/fs/mbox/^$q($i))
				echo $1 >/mail/fs/mbox/^$q($i)^/flags
}
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 forward{
	for(i in $r){
		subject=`{sed -n '6p' /mail/fs/mbox/^$q($i)^/info}
		subject='Fwd: '$"subject
		if(~ $new 1){
			mffile=/tmp/m.f.$user.$pid
			>$mffile
			eval $editor $mffile
			sendyn
			if(~ $send y)
				/bin/upas/marshal \
					-s $"subject \
					-A /mail/fs/mbox/^$q($i)^/raw \
					$* <$mffile
		}
		if not
			/bin/upas/marshal \
				-s $"subject \
				-t `{file -m /mail/fs/mbox/^$q($i)^/raw} \
				-A /mail/fs/mbox/^$q($i)^/raw \
				-n $*
	}
}
fn getc{
	cc=$*
	switch($1){
	case /*
		while(~ $cc */*)
			cc=`{echo $cc | sed 's/^.*\///g'}
	case %*
		while(~ $cc *%*)
			cc=`{echo $cc | sed 's/^.*%//g'}
	case [0-9]*.[0-9]*
		cc=pb
	case *
		cc=`{echo $* | sed 's/^(,|,[0-9]*|[0-9]*,[0-9]*|[0-9]*,|[0-9]*)//g'}
		ccc=`{echo $1 | sed 's/[^a-zA-Z]//g'}
		if(~ $cc(1) e F m M mb s w)
			cc=$cc
		if not if(~ $#cc 0 && ~ $#ccc 0)
			cc=p
		if not if(~ $#ccc 1)
			cc=$ccc
		if not if(~ $#cc 0)
			cc=NEXT
	}
	echo $cc
}
fn getf{
	if(~ $1 */*)
		echo $1
	if not
		echo /mail/box/$user/$1
}
fn getpnq{
	p=$1
	nq=`{
		for(i in `{seq $p $#q})
			echo $i
	}
}
fn getr{
	switch($1){
	case /*
		regexp=`{echo $1 | sed 's/^\///g; s/\/.*$//g'}
		rr=`{grep -e $"regexp $mfile | awk '{print $1;}'}
	case %*
		regexp=`{echo $1 | sed 's/^%//g; s/%.*$//g'}
		rr=`{grep -e $"regexp /mail/fs/mbox/*/body | awk -F '/' '{print $5;}'}
	case [0-9]*.[0-9]*
		rr=`{echo $1 | sed 's/\.*$//g'}
	case *
		rr=`{echo $1 | sed 's/[^0-9,]//g'}
		rr=`{
			switch($rr){
			case *,*
				start=`{echo $rr | sed 's/,.*$//g'}
				stop=`{echo $rr | sed 's/^.*,//g'}
				if(~ $#start 0 && ~ $#stop 0)
					seq 1 $#q
				if not if(~ $#start 0)
					seq 1 $stop
				if not if(~ $#stop 0)
					seq $start $#q
				if not
					seq $start $stop
			case [0-9]*
				echo $rr
			case *
				if(! ~ $1 [a-zA-Z])
					next
				echo $p
			}
		}
	}
	for(i in $rr)
		if(test -d /mail/fs/mbox/^$q($i))
			echo $i
}
fn mailm{	
	mmfile=/tmp/m.m.$user.$pid
	>$mmfile
	eval $editor $mmfile
	sendyn
	if(~ $send y)
		/bin/upas/marshal $* <$mmfile
}
fn next{
	if(~ $#r 0)
		nq=`{seq 1 $#q}
	if not
		nq=`{nshift $nq}
	p=$nq(1)
	r=$p
}
fn nshift{ shift; echo $* }
fn pb{
	for(i in $*){
		if(test -d /mail/fs/mbox/^$q($r)^/$i){
			type=`{echo `{file -m /mail/fs/mbox/^$q($r)^/$i/body}}
			echo !--- using plumber to type $"type
			plumb /mail/fs/mbox/^$q($r)^/$i/body.^`{echo $"type | sed 's/^.*\///g'}
		}
		if not
			echo !no sub parts
	}
}
fn printb{
	if(~ $#p 0)
		p=0
	r=`{
		for(i in `{seq `{echo $p+1 | bc} `{echo $p+10 | bc}})
			if(~ $i `{echo $nq})
				echo $i
	}
	if(~ $#r 0)
		r=$nq
	getpnq $r($#r)
	printh
}
fn printh{
	for(i in $r)
		grep -e '^'$"i' ' $mfile
}
fn printm{
	for(i in $r){
		cat /mail/fs/mbox/^$q($i)^/header
		echo

		if(~ `{sed -n '7p' /mail/fs/mbox/^$q($i)^/info} *multipart*){
			parts=`{ls -p /mail/fs/mbox/^$q($i) | grep -e '^[0-9]'}
			body=1/body
		}
		if not{
			parts=()
			body=body
		}
		if(~ `{file -m /mail/fs/mbox/^$q($i)^/^$body} *html*){
			echo !/bin/htmlfmt
			htmlfmt -l60 -cutf8 /mail/fs/mbox/^$q($i)^/^$body
		}
		if not
			cat /mail/fs/mbox/^$q($i)^/^$body
		echo
		if(! ~ $#parts 0){
			for(j in $parts){
				type=`{file -m /mail/fs/mbox/^$q($i)^/$j/body}
				if(! ~ $type text*){
					file=`{cat /mail/fs/mbox/^$q($i)^/$j/filename}
					if(! ~ $#file 0)
						file='(file,'$"file')'
					fake=`{
						if(~ $#file 0)
							echo body
						if not
							echo body.^`{echo $type | sed 's/^.*\///g; s/(jpeg|JPEG)/jpg/g'}
					}
					size=`{du /mail/fs/mbox/^$q($i)^/$j/$fake}
					echo !--- $i.$j $type $size $file /mail/fs/mbox/^$q($i)^/$j/$fake
				}
			}
		parts=()
		}
	}
}
fn printmf{
	for(i in $*){
		flags=`{sed -n '18p' /mail/fs/mbox/^$q($i)^/info | sed 's/-//g' }
		mime=`{
			if(~ `{sed -n '7p' /mail/fs/mbox/^$q($i)^/info} multipart*)
				echo H
		}
		size=`{sed -n '17p' /mail/fs/mbox/^$q($i)^/info}
		date=`{sed -n '5p' /mail/fs/mbox/^$q($i)^/info | fmtd}
		from=`{sed 1q /mail/fs/mbox/^$q($i)^/info}
		subject=`{sed -n '6p' /mail/fs/mbox/^$q($i)^/info | awk '{print substr($0,0,50);}'}
		echo $"i' '$"mime' '$"flags'   '$"size'   '$"date' '$"from'	'$"subject
	}
}
fn printq{
	for(i in $r){
		if(~ `{cat /mail/fs/mbox/^$q($i)^/type} multipart*)
			body=1/body
		if not
			body=body
		if(~ `{file /mail/fs/mbox/^$q($i)^/^$body} *HTML*)
			htmlfmt -l60 -cutf8 /mail/fs/mbox/^$q($i)^/^$body | sed 's/^/> /g'
		if not
			sed 's/^/> /g' /mail/fs/mbox/^$q($i)^/^$body
		echo
	}
}
fn printr{
	for(i in $r)
		cat /mail/fs/mbox/^$q($i)^/rawunix
}
fn printv{
	echo q: $q
	echo nq: $nq
	echo d: $d
	echo or: $or
	echo r: $r
	echo op: $op
	echo p: $p
	echo parts: $parts
}
fn reply{
	mrfile=/tmp/m.r.$user.$pid
	for(i in $r){
		>$mrfile
		to=`{cat /mail/fs/mbox/^$q($i)^/replyto}
		if(~ $all 1)
			to=($to `{cat /mail/fs/mbox/^$q($i)^/cc})
		subject=`{cat /mail/fs/mbox/^$q($i)^/subject}
		if(! ~ $subject Re:*)
			subject='Re: '$"subject
		if(~ $quote 1)
			r=$i printq >$mrfile
		eval $editor $mrfile
		sendyn
		if(~ $send y)
			/bin/upas/marshal \
				-s $"subject \
				-R /mail/fs/mbox/^$q($i) \
				$to <$mrfile
	}
	flag +a
}
fn sendyn{
	echo
	echo -n 'send (y, n) '
	send=`{read}
	switch($send){
	case y n
		;
	case *
		sendyn
	}
}
fn store{
	f=()
	sfile=`{echo $* | sed 's/^s //g'}
	if(~ $#sfile 0)
		f=1
	for(i in $r){
		if(~ $f 1)
			sfile=`{sed 's/@.*$//g' /mail/fs/mbox/^$q($i)^/replyto}
		sdir=`{getf $sfile}
		if(! test -d $sdir)
			echo create $sfile >/mail/fs/ctl
		cp /mail/fs/mbox/^$q($i)^/raw $sdir/^`{cat /mail/fs/mbox/^$q($i)^/fileid}
		echo !saved in $sdir
	}
	flag +S
	f=()
}
fn update{
	if(! ~ $#d 0)
		delete $d
	d=()
	q=`{ls -np /mail/fs/mbox | eval $sort | grep -v ctl}
	nq=`{seq 1 $#q}
	p=()
	r=()
	printmf $nq >$mfile
	echo $#q messages
}
fn write{
	wfile=`{echo $* | sed 's/^w //g'}
	for(i in $r){
		cp /mail/fs/mbox/^$q($i)^/body $wfile
		echo !saved in $wfile
	}
}
fn usage {
	echo usage: $argv0 [ -d ] [ -f mbox ] [ -r ] >[1=2]
	exit usage
}
while(~ $1 -*){
	switch($1){
	case -d;	debug=1
	case -f;	/bin/upas/fs -f `{getf $2}; shift
	case -r;	sort=(sort -n)
	case *;	usage
	}
	shift
}
if(! test -f /mail/fs/ctl)
	/bin/upas/fs
update
while(){
	echo -n $"p': '
	raw=`{read}
	or=$r
	op=$p
	r=`{getr $raw}
	c=`{getc $raw}
	if(~ $debug 1){
		echo r: $r
		echo c: $c
	}
	switch($c){
	case a
		getpnq $r($#r)
		all=1 reply
	case A
		getpnq $r($#r)
		all=1 quote=1 reply
	case b
		printb
	case d
		getpnq $r($#r)
		d=`{
			{
				for(i in $d $r)
					echo $i
			} | sort -n | uniq
		}
		r=$d flag +D
	case e' '*
		mailm `{nshift $c}
	case f
		store
	case F' '*
		getpnq $r($#r)
		flag `{nshift $c}
	case h
		getpnq $r($#r)
		printh	
	case H
		# printH - mime structure
	case m' '*
		getpnq $r($#r)
		forward `{nshift $c}
	case M' '*
		getpnq $r($#r)
		new=1 forward `{nshift $c}
	case mb' '*
		if(! ~ $#d 0)
			delete $d
		d=()
		/bin/upas/fs -f `{getf `{nshift $c}}
		update
	case p
		getpnq $r($#r)
		printm
	case P
		getpnq $r($#r)
		printr
	case pb
		parts=`{echo $r | sed 's/^.*\.//g'}
		r=`{echo $r | sed 's/\..*$//g'}
		getpnq $r($#r)
		pb $parts
	case q
		update
		exit
	case r
		getpnq $r($#r)
		reply
	case rf
		getpnq $r($#r)
		store
		reply
	case R
		getpnq $r($#r)
		quote=1 reply
	case s' '*
		getpnq $r($#r)
		store `{nshift $c}
	case u
		getpnq $r($#r)
		r=$d flag -D
		d=`{
			{
				for(i in $r)
					for(j in $d)
						if(! ~ $j $i)
							echo $j
			} | sort -n | uniq
		}
	case w' '*
		getpnq $r($#r)
		write `{nshift $c}
	case x
		exit
	case y
		update
	case '"'
		getpnq $r($#r)
		printq
	case '|'	# cmd w/ body as stdin
	case '||'	# cmd w/ whole msg as stdin
	case '!'	# cmd
	case '='
		echo $p		
	case '?'
		printv
	case *
		echo !illegal command
	}	
}

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].