summaryrefslogtreecommitdiff
path: root/rc/bin/newt
blob: 0a899bf517ddc33d1d8ea96e8900ec6a649b5f62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/rc
# NEWT, their six-year-old daughter, yells from the back...
# use with nntpfs
rfork en
ramfs
argv0=$0
if(~ $#editor 0)
	editor=hold
if(~ $#pager 0)
	pager=cat
group=alt/test
maxposts=30
mnt=/mnt/news
if(~ $#newtname 0)
	newtname=newt@dont-email.me
fn e{
	{
		echo From: $"newtname
		echo Newsgroups: `{echo $group | sed 's/\//\./g'}
		echo Subject: $"subject
		echo
	} >/tmp/post
	eval $editor /tmp/post
	yn send
	if(~ $yn y)
		cat /tmp/post >$mnt/$group/post
}
fn fmtd{
	date=`{cat}
	if(! ~ $date(1) [0-9]*)
		date=`{nshift $date}
	da=$date(1)
	if(~ $da [0-9])
		da=0^$da
	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
	}
	if(! ~ $date(3) `{date | awk '{print $6;}'})
		ti=$date(3)
	if not
		ti=`{echo $date(4) | awk '{print substr($0,0,5);}'}
	if(! ~ $#mo 0 && ! ~ $#da 0 && ! ~ $#ti 0)
		echo $mo/$da $ti
	if not
		echo nil
}
fn geth{
	for(i in $*){
		from=`{awk -F '	' '{print $3;}' $rposts($i)^/xover >[2]/dev/null}
		if(! ~ $#from 0 && ! ~ $#from 1){
			nfrom=`{
				for(i in $from){
					if(~ $i *@*)
						echo $i | sed 's/(<|>)//g'
				}
			}
			if(! ~ $#nfrom 0)
				from=$nfrom
		}
		if(~ $#from 0)
			from=nil
		date=`{awk -F '	' '{print $4;}' $rposts($i)^/xover >[2]/dev/null | fmtd}
		if(~ $#date 0)
			date=nil
		subject=`{awk -F '	' '{print substr($2,0,50);}' $rposts($i)^/xover >[2]/dev/null}
		if(~ $#subject 0)
			subject=nil
		# 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' '$"date' '$"from'		'$"subject
	}
}
fn getposts{ ls | grep -e '^[0-9]+$' | sort -n | tail -$maxposts }
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 k{
	kmnt=`{echo $mnt | sed 's/\//\\\//g'}
	walk $mnt/$* |
		grep -v -e '\/([0-9]+|article|body|header|post|xover)$' |
		sed 's/^'$"kmnt'\//  g	/g' |
		sort |
		eval $pager
}
fn nshift{ shift; echo $* }
fn printhelp{
echo 'Commands are of the form [<range>] <command> [args]
<range> := <addr> | <addr>','<addr>
<command> :=
b		print the next ten headers
e		enter message
g ...		go to specified group
h		print message headline (,h for all)
help		print this help message
k ...		list sub-groups under specified group
kf ...		grep $home/lib/newsgroups for regexp
p		print message with minimal headers
P		print message with full headers
q		quit
r		reply to message
y		synchronize message list with server
"		print message in quoted form, suitable for reply
|cmd		pipe message body to a command
||cmd		pipe raw message to a command
!cmd		run a command
?		print debug information'
}
fn np{
	if(test $1 -le $#rposts && test -f $mnt/$group/$rposts($1)^/body){
		{ # don't stutter
			grep -e '(^From|^Newsgroups|^Subject|^Date)' $rposts($1)^/header
			echo
			cat $rposts($1)^/body
		} >/tmp/p
		eval $pager /tmp/p
	}
	echo
	go=1
	r=$1
	post=$1
	prompt=$group/$1
}
fn P{
	if(test $1 -le $#rposts && test -f $mnt/$group/$rposts($1)^/article)
		eval $pager $rposts($1)^/article
	echo
	go=1
	r=$1
	post=$1
	prompt=$group/$1
}
fn r{
	if(test $1 -le $#rposts && test -f $mnt/$group/$rposts($1)^/header){
		subject=`{grep -e '^Subject: ' $mnt/$group/$rposts($1)^/header | sed 's/^Subject: //g'}
		if(! ~ $subject RE:* Re:* re:*)
			subject=(Re: $subject)
		e
	}
	if not
		echo !message missing
}
fn y{
	go=()
	r=$post
	if(! ~ $q 1){
		rposts=`{getposts}
		posts=`{seq 1 $#rposts}
		post=$posts(1)
		prompt=$post
		geth $posts >/tmp/h
		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' | sed '$d'; echo}}
fn usage{
	echo usage: $argv0 '[ -f newsgroup ] [ -m mountpoint ] [ -p maxposts ]' >[1=2]
	exit usage
}
while(~ $1 -*){
	switch($1){
	case -f
		group=`{echo $2 | sed 's/\./\//g'}
		shift
	case -m
		mnt=$2
		shift
	case -p
		maxposts=$2
		shift
	case *
		usage
	}
	shift
}
if(! ~ $#* 0)
	usage
prompt=$group
if(! test -d $mnt/$group){
	echo !$mnt/$group does not exist >[1=2]
	exit
}
builtin cd $mnt/$group
y
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=`{nshift $rcmd}
		if(~ $#cmd 0)
			cmd=p
	case *
		r=$post
		cmd=$rcmd
	}
	switch($cmd){
	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 e
		subject=() e
	case g' '*
		ngroup=`{nshift $cmd | sed 's/\./\//g'}
		if(test -d $mnt/$ngroup){
			if(grep -s -e '^[0-9]+$' <{ls -p $mnt/$ngroup}){
				group=$ngroup
				builtin cd $mnt/$group
				y
			}
			if not
				echo !$ngroup contains no messages
		}
		if not
			echo !$ngroup does not exist
	case h
		{
			for(i in $r)
				h $i
		} | eval $pager
		post=$r($#r)
		prompt=$post
	case help
		printhelp
	case k
		k $group
	case k' '*
		k `{nshift $cmd | sed 's/\./\//g'}
	case kf' '*
		grep -e `{nshift $cmd} $home/lib/newsgroups |
			sed 's/^/  g	/g' |
			eval $pager
	case p
		for(i in $r)
			np $i
	case P
		for(i in $r)
			P $i
	case q
		q=1 y # BUP STOP
		exit
	case r
		for(i in $r)
			r $i
		post=$r($#r)
		prompt=$post
	case y
		y
	case '"'
		for(i in $r)
			'"' $i
		post=$r($#r)
		prompt=$post
	case '||'*
		cmd=`{echo $"cmd | sed 's/^\|\|//g'}
		cat $mnt/$group/^$rposts($post)^/article | eval $cmd
	case '|'*
		cmd=`{echo $"cmd | sed 's/^\|//g'}
		cat $mnt/$group/^$rposts($post)^/body | eval $cmd
	case '!'*
		eval `{echo $"cmd | sed 's/^\!//g'}
	case '?'
		echo editor: $editor
		echo pager: $pager
		echo mnt: $mnt
		echo group: $group
		echo maxposts: $maxposts
		echo '#go': $#go
		echo rposts: $rposts
		echo 'rposts(r):' $rposts($r)
		echo posts: $posts
		echo post: $post
		echo r: $r
	case *
		if(~ $post $posts(1) && ~ $#go 0)
			np $post
		if not if(! ~ $post $posts($#posts)){
			post=`{echo $post^+1 | bc}
			if(test $post -gt $posts($#posts))
				post=$posts($#posts)
			np $post
		}
	}	
}