1 /** $MirOS: src/usr.bin/mail/cmd1.c,v 1.2 2005/03/13 18:33:14 tg Exp $ */
2 /* $OpenBSD: cmd1.c,v 1.25 2003/12/03 20:59:45 millert Exp $ */
3 /* $NetBSD: cmd1.c,v 1.9 1997/07/09 05:29:48 mikel Exp $ */
4
5 /*-
6 * Copyright (c) 1980, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 #if 0
36 static const char sccsid[] = "@(#)cmd1.c 8.2 (Berkeley) 4/20/95";
37 #else
38 static const char rcsid[] = "$OpenBSD: cmd1.c,v 1.25 2003/12/03 20:59:45 millert Exp $";
39 #endif
40 #endif /* not lint */
41
42 #include "rcv.h"
43 #include "extern.h"
44
45 /*
46 * Mail -- a mail program
47 *
48 * User commands.
49 */
50
51 /*
52 * Print the current active headings.
53 * Don't change dot if invoker didn't give an argument.
54 */
55
56 static int screen;
57 static volatile sig_atomic_t gothdrint;
58
59 int
headers(void * v)60 headers(void *v)
61 {
62 int *msgvec = v;
63 int n, mesg, flag, size;
64 struct message *mp;
65 struct sigaction act, oact;
66 sigset_t oset;
67
68 size = screensize();
69 n = msgvec[0];
70 if (n != 0)
71 screen = (n-1)/size;
72 if (screen < 0)
73 screen = 0;
74 mp = &message[screen * size];
75 if (mp >= &message[msgCount])
76 mp = &message[msgCount - size];
77 if (mp < &message[0])
78 mp = &message[0];
79 flag = 0;
80 mesg = mp - &message[0];
81 if (dot != &message[n-1])
82 dot = mp;
83 sigemptyset(&act.sa_mask);
84 act.sa_flags = SA_RESTART;
85 act.sa_handler = hdrint;
86 if (sigaction(SIGINT, NULL, &oact) == 0 &&
87 oact.sa_handler != SIG_IGN) {
88 (void)sigaction(SIGINT, &act, &oact);
89 (void)sigprocmask(SIG_UNBLOCK, &intset, &oset);
90 }
91 for (gothdrint = 0; !gothdrint && mp < &message[msgCount]; mp++) {
92 mesg++;
93 if (mp->m_flag & MDELETED)
94 continue;
95 if (flag++ >= size)
96 break;
97 printhead(mesg);
98 }
99 if (gothdrint) {
100 fflush(stdout);
101 fputs("\nInterrupt\n", stderr);
102 }
103 if (oact.sa_handler != SIG_IGN) {
104 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
105 (void)sigaction(SIGINT, &oact, NULL);
106 }
107 if (flag == 0) {
108 puts("No more mail.");
109 return(1);
110 }
111 return(0);
112 }
113
114 /*
115 * Scroll to the next/previous screen
116 */
117 int
scroll(void * v)118 scroll(void *v)
119 {
120 char *arg = v;
121 int size, maxscreen;
122 int cur[1];
123
124 cur[0] = 0;
125 size = screensize();
126 maxscreen = (msgCount - 1) / size;
127 switch (*arg) {
128 case 0:
129 case '+':
130 if (screen >= maxscreen) {
131 puts("On last screenful of messages");
132 return(0);
133 }
134 screen++;
135 break;
136
137 case '-':
138 if (screen <= 0) {
139 puts("On first screenful of messages");
140 return(0);
141 }
142 screen--;
143 break;
144
145 default:
146 printf("Unrecognized scrolling command \"%s\"\n", arg);
147 return(1);
148 }
149 return(headers(cur));
150 }
151
152 /*
153 * Compute screen size.
154 */
155 int
screensize(void)156 screensize(void)
157 {
158 int s;
159 char *cp;
160
161 if ((cp = value("screen")) != NULL && (s = atoi(cp)) > 0)
162 return(s);
163 return(screenheight - 4);
164 }
165
166 /*
167 * Print out the headlines for each message
168 * in the passed message list.
169 */
170 int
from(void * v)171 from(void *v)
172 {
173 int *msgvec = v;
174 int *ip;
175
176 for (ip = msgvec; *ip; ip++)
177 printhead(*ip);
178 if (--ip >= msgvec)
179 dot = &message[*ip - 1];
180 return(0);
181 }
182
183 /*
184 * Print out the header of a specific message.
185 * This is a slight improvement to the standard one.
186 */
187 void
printhead(int mesg)188 printhead(int mesg)
189 {
190 struct message *mp;
191 char headline[LINESIZE], *subjline, dispc, curind;
192 char visname[LINESIZE], vissub[LINESIZE];
193 char pbuf[LINESIZE];
194 char fmtline[LINESIZE];
195 const char *fmt;
196 struct headline hl;
197 char *name;
198 char *to, *from;
199 struct name *np;
200 char **ap;
201
202 mp = &message[mesg-1];
203 (void)readline(setinput(mp), headline, LINESIZE, NULL);
204 if ((subjline = hfield("subject", mp)) == NULL &&
205 (subjline = hfield("subj", mp)) == NULL)
206 subjline = "";
207 /*
208 * Bletch!
209 */
210 curind = dot == mp ? '>' : ' ';
211 dispc = ' ';
212 if (mp->m_flag & MSAVED)
213 dispc = '*';
214 if (mp->m_flag & MPRESERVE)
215 dispc = 'P';
216 if ((mp->m_flag & (MREAD|MNEW)) == MNEW)
217 dispc = 'N';
218 if ((mp->m_flag & (MREAD|MNEW)) == 0)
219 dispc = 'U';
220 if (mp->m_flag & MBOX)
221 dispc = 'M';
222 parse(headline, &hl, pbuf);
223 from = nameof(mp, 0);
224 to = skin(hfield("to", mp));
225 np = extract(from, GTO);
226 np = delname(np, myname);
227 if (altnames)
228 for (ap = altnames; *ap; ap++)
229 np = delname(np, *ap);
230 if (np)
231 /* not from me */
232 name = value("show-rcpt") != NULL && to ? to : from;
233 else
234 /* from me - show TO */
235 name = value("showto") != NULL && to ? to : from;
236 strnvis(visname, name, sizeof(visname), VIS_SAFE|VIS_NOSLASH);
237 if (name == to)
238 fmt = "%c%c%3d TO %-14.14s %16.16s %4d/%-5d %s";
239 else
240 fmt = "%c%c%3d %-17.17s %16.16s %4d/%-5d %s";
241 strnvis(vissub, subjline, sizeof(vissub), VIS_SAFE|VIS_NOSLASH);
242 /* hl.l_date was sanity-checked when read in. */
243 snprintf(fmtline, sizeof(fmtline), fmt, curind, dispc, mesg, visname,
244 hl.l_date, mp->m_lines, mp->m_size, vissub);
245 printf("%.*s\n", screenwidth, fmtline);
246 }
247
248 /*
249 * Print out the value of dot.
250 */
251 int
pdot(void * v)252 pdot(void *v)
253 {
254 printf("%d\n", (int)(dot - &message[0] + 1));
255 return(0);
256 }
257
258 /*
259 * Print out all the possible commands.
260 */
261 int
pcmdlist(void * v)262 pcmdlist(void *v)
263 {
264 extern const struct cmd cmdtab[];
265 const struct cmd *cp;
266 int cc;
267
268 puts("Commands are:");
269 for (cc = 0, cp = cmdtab; cp->c_name != NULL; cp++) {
270 cc += strlen(cp->c_name) + 2;
271 if (cc > 72) {
272 putchar('\n');
273 cc = strlen(cp->c_name) + 2;
274 }
275 if ((cp+1)->c_name != NULL)
276 printf("%s, ", cp->c_name);
277 else
278 puts(cp->c_name);
279 }
280 return(0);
281 }
282
283 /*
284 * Pipe message to command
285 */
286 int
pipeit(void * ml,void * sl)287 pipeit(void *ml, void *sl)
288 {
289 int *msgvec = ml;
290 char *cmd = sl;
291
292 return(type1(msgvec, cmd, 0, 0));
293 }
294
295 /*
296 * Paginate messages, honor ignored fields.
297 */
298 int
more(void * v)299 more(void *v)
300 {
301 int *msgvec = v;
302 return(type1(msgvec, NULL, 1, 1));
303 }
304
305 /*
306 * Paginate messages, even printing ignored fields.
307 */
308 int
More(void * v)309 More(void *v)
310 {
311 int *msgvec = v;
312
313 return(type1(msgvec, NULL, 0, 1));
314 }
315
316 /*
317 * Type out messages, honor ignored fields.
318 */
319 int
type(void * v)320 type(void *v)
321 {
322 int *msgvec = v;
323
324 return(type1(msgvec, NULL, 1, 0));
325 }
326
327 /*
328 * Type out messages, even printing ignored fields.
329 */
330 int
Type(void * v)331 Type(void *v)
332 {
333 int *msgvec = v;
334
335 return(type1(msgvec, NULL, 0, 0));
336 }
337
338 /*
339 * Type out the messages requested.
340 */
341 int
type1(int * msgvec,char * cmd,int doign,int page)342 type1(int *msgvec, char *cmd, int doign, int page)
343 {
344 int nlines, *ip, restoreterm;
345 struct message *mp;
346 struct termios tbuf;
347 char *cp;
348 FILE *obuf;
349
350 obuf = stdout;
351 restoreterm = 0;
352
353 /*
354 * start a pipe if needed.
355 */
356 if (cmd) {
357 restoreterm = (tcgetattr(fileno(stdin), &tbuf) == 0);
358 obuf = Popen(cmd, "w");
359 if (obuf == NULL) {
360 warn("%s", cmd);
361 obuf = stdout;
362 }
363 } else if (value("interactive") != NULL &&
364 (page || (cp = value("crt")) != NULL)) {
365 nlines = 0;
366 if (!page) {
367 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
368 nlines += message[*ip - 1].m_lines;
369 }
370 if (page || nlines > (*cp ? atoi(cp) : realscreenheight)) {
371 restoreterm = (tcgetattr(fileno(stdin), &tbuf) == 0);
372 obuf = Popen(value("PAGER"), "w");
373 if (obuf == NULL) {
374 warn("%s", cp);
375 obuf = stdout;
376 }
377 }
378 }
379
380 /*
381 * Send messages to the output.
382 */
383 for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
384 mp = &message[*ip - 1];
385 touch(mp);
386 dot = mp;
387 if (cmd == NULL && value("quiet") == NULL)
388 fprintf(obuf, "Message %d:\n", *ip);
389 if (sendmessage(mp, obuf, doign ? ignore : 0, NULL) == -1)
390 break;
391 }
392
393 if (obuf != stdout) {
394 (void)Pclose(obuf);
395 if (restoreterm)
396 (void)tcsetattr(fileno(stdin), TCSADRAIN, &tbuf);
397 }
398 return(0);
399 }
400
401 /*
402 * Print the top so many lines of each desired message.
403 * The number of lines is taken from the variable "toplines"
404 * and defaults to 5.
405 */
406 int
top(void * v)407 top(void * v)
408 {
409 int *msgvec = v;
410 int *ip;
411 struct message *mp;
412 int c, topl, lines, lineb;
413 char *valtop, linebuf[LINESIZE];
414 FILE *ibuf;
415
416 topl = 5;
417 valtop = value("toplines");
418 if (valtop != NULL) {
419 topl = atoi(valtop);
420 if (topl < 0 || topl > 10000)
421 topl = 5;
422 }
423 lineb = 1;
424 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
425 mp = &message[*ip - 1];
426 touch(mp);
427 dot = mp;
428 if (value("quiet") == NULL)
429 printf("Message %d:\n", *ip);
430 ibuf = setinput(mp);
431 c = mp->m_lines;
432 if (!lineb)
433 putchar('\n');
434 for (lines = 0; lines < c && lines <= topl; lines++) {
435 if (readline(ibuf, linebuf, sizeof(linebuf), NULL) < 0)
436 break;
437 puts(linebuf);
438 lineb = blankline(linebuf);
439 }
440 }
441 return(0);
442 }
443
444 /*
445 * Touch all the given messages so that they will
446 * get mboxed.
447 */
448 int
stouch(void * v)449 stouch(void *v)
450 {
451 int *msgvec = v;
452 int *ip;
453
454 for (ip = msgvec; *ip != 0; ip++) {
455 dot = &message[*ip-1];
456 dot->m_flag |= MTOUCH;
457 dot->m_flag &= ~MPRESERVE;
458 }
459 return(0);
460 }
461
462 /*
463 * Make sure all passed messages get mboxed.
464 */
465 int
mboxit(void * v)466 mboxit(void *v)
467 {
468 int *msgvec = v;
469 int *ip;
470
471 for (ip = msgvec; *ip != 0; ip++) {
472 dot = &message[*ip-1];
473 dot->m_flag |= MTOUCH|MBOX;
474 dot->m_flag &= ~MPRESERVE;
475 }
476 return(0);
477 }
478
479 /*
480 * List the folders the user currently has.
481 */
482 int
folders(void * v)483 folders(void *v)
484 {
485 char *files = (char *)v;
486 char dirname[PATHSIZE];
487 char cmd[BUFSIZ];
488
489 if (getfold(dirname, sizeof(dirname)) < 0)
490 strlcpy(dirname, "$HOME", sizeof(dirname));
491
492 snprintf(cmd, sizeof(cmd), "cd %s; %s %s", dirname, value("LISTER"),
493 files && *files ? files : "");
494
495 (void)run_command(value("SHELL"), 0, -1, -1, "-c", cmd, NULL);
496 return(0);
497 }
498
499 /*
500 * Update the mail file with any new messages that have
501 * come in since we started reading mail.
502 */
503 int
inc(void * v)504 inc(void *v)
505 {
506 int nmsg, mdot;
507
508 nmsg = incfile();
509
510 if (nmsg == 0) {
511 puts("No new mail.");
512 } else if (nmsg > 0) {
513 mdot = newfileinfo(msgCount - nmsg);
514 dot = &message[mdot - 1];
515 } else {
516 puts("\"inc\" command failed...");
517 }
518
519 return(0);
520 }
521
522 /*
523 * User hit ^C while printing the headers.
524 */
525 void
hdrint(int s)526 hdrint(int s)
527 {
528
529 gothdrint = 1;
530 }
531