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