makeconfig: small tweaks
[s-mailx.git] / cmd2.c
blobae140de9b8f72aed647ad82ba2364799a29e75ac
1 /*
2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #include "rcv.h"
41 #include "extern.h"
42 #include <sys/wait.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
47 * Mail -- a mail program
49 * More user commands.
52 static int save1(char *str, int mark, char *cmd, struct ignoretab *ignore,
53 int convert, int sender_record, int domove);
54 static char * snarf(char *linebuf, int *flag, int usembox);
55 static int delm(int *msgvec);
56 #ifdef HAVE_ASSERTS
57 static void clob1(int n);
58 #endif
59 static int ignore1(char **list, struct ignoretab *tab, char *which);
60 static int igshow(struct ignoretab *tab, char *which);
61 static int igcomp(const void *l, const void *r);
62 static void unignore_one(const char *name, struct ignoretab *tab);
63 static int unignore1(char **list, struct ignoretab *tab, char *which);
66 * If any arguments were given, go to the next applicable argument
67 * following dot, otherwise, go to the next applicable message.
68 * If given as first command with no arguments, print first message.
70 int
71 next(void *v)
73 int *msgvec = v;
74 struct message *mp;
75 int *ip, *ip2;
76 int list[2], mdot;
78 if (*msgvec != 0) {
81 * If some messages were supplied, find the
82 * first applicable one following dot using
83 * wrap around.
86 mdot = dot - &message[0] + 1;
89 * Find the first message in the supplied
90 * message list which follows dot.
93 for (ip = msgvec; *ip != 0; ip++) {
94 #ifdef _CRAY
96 * Work around an optimizer bug in Cray Standard C Version 4.0.3 (057126).
97 * Otherwise, SIGFPE is received when mb.mb_threaded != 0.
99 #pragma _CRI suppress ip
100 #endif /* _CRAY */
101 if (mb.mb_threaded ? message[*ip-1].m_threadpos >
102 dot->m_threadpos :
103 *ip > mdot)
104 break;
106 if (*ip == 0)
107 ip = msgvec;
108 ip2 = ip;
109 do {
110 mp = &message[*ip2 - 1];
111 if ((mp->m_flag & (MDELETED|MHIDDEN)) == 0) {
112 setdot(mp);
113 goto hitit;
115 if (*ip2 != 0)
116 ip2++;
117 if (*ip2 == 0)
118 ip2 = msgvec;
119 } while (ip2 != ip);
120 printf(catgets(catd, CATSET, 21, "No messages applicable\n"));
121 return(1);
125 * If this is the first command, select message 1.
126 * Note that this must exist for us to get here at all.
129 if (!sawcom) {
130 if (msgCount == 0)
131 goto ateof;
132 goto hitit;
136 * Just find the next good message after dot, no
137 * wraparound.
140 if (mb.mb_threaded == 0) {
141 for (mp = dot + did_print_dot; mp < &message[msgCount]; mp++)
142 if ((mp->m_flag & (MDELETED|MSAVED|MHIDDEN|MKILL)) == 0)
143 break;
144 } else {
145 mp = dot;
146 if (did_print_dot)
147 mp = next_in_thread(mp);
148 while (mp && mp->m_flag & (MDELETED|MSAVED|MHIDDEN|MKILL))
149 mp = next_in_thread(mp);
151 if (mp == NULL || mp >= &message[msgCount]) {
152 ateof:
153 printf(catgets(catd, CATSET, 22, "At EOF\n"));
154 return(0);
156 setdot(mp);
157 hitit:
159 * Print dot.
162 list[0] = dot - &message[0] + 1;
163 list[1] = 0;
164 return(type(list));
168 * Save a message in a file. Mark the message as saved
169 * so we can discard when the user quits.
171 int
172 save(void *v)
174 char *str = v;
176 return save1(str, 1, "save", saveignore, SEND_MBOX, 0, 0);
179 int
180 Save(void *v)
182 char *str = v;
184 return save1(str, 1, "save", saveignore, SEND_MBOX, 1, 0);
188 * Copy a message to a file without affected its saved-ness
190 int
191 copycmd(void *v)
193 char *str = v;
195 return save1(str, 0, "copy", saveignore, SEND_MBOX, 0, 0);
198 int
199 Copycmd(void *v)
201 char *str = v;
203 return save1(str, 0, "copy", saveignore, SEND_MBOX, 1, 0);
207 * Move a message to a file.
209 int
210 cmove(void *v)
212 char *str = v;
214 return save1(str, 0, "move", saveignore, SEND_MBOX, 0, 1);
217 int
218 cMove(void *v)
220 char *str = v;
222 return save1(str, 0, "move", saveignore, SEND_MBOX, 1, 1);
226 * Decrypt and copy a message to a file.
228 int
229 cdecrypt(void *v)
231 char *str = v;
233 return save1(str, 0, "decrypt", saveignore, SEND_DECRYPT, 0, 0);
236 int
237 cDecrypt(void *v)
239 char *str = v;
241 return save1(str, 0, "decrypt", saveignore, SEND_DECRYPT, 1, 0);
245 * Save/copy the indicated messages at the end of the passed file name.
246 * If mark is true, mark the message "saved."
248 static int
249 save1(char *str, int mark, char *cmd, struct ignoretab *ignore,
250 int convert, int sender_record, int domove)
252 struct stat st;
253 int *ip;
254 struct message *mp;
255 char *file = NULL, *disp = "";
256 int f, *msgvec;
257 FILE *obuf;
258 int newfile = 0;
259 char *cp, *cq;
260 off_t mstats[2], tstats[2];
261 int compressed = 0;
262 enum protocol prot;
263 int success = 1, last = 0;
265 /*LINTED*/
266 msgvec = (int *)salloc((msgCount + 2) * sizeof *msgvec);
267 if (sender_record) {
268 for (cp = str; *cp && blankchar(*cp & 0377); cp++);
269 f = (*cp != '\0');
270 } else {
271 if ((file = snarf(str, &f, convert != SEND_TOFILE)) == NULL)
272 return(1);
274 if (!f) {
275 *msgvec = first(0, MMNORM);
276 if (*msgvec == 0) {
277 if (inhook)
278 return 0;
279 printf(catgets(catd, CATSET, 23,
280 "No messages to %s.\n"), cmd);
281 return(1);
283 msgvec[1] = 0;
285 if (f && getmsglist(str, msgvec, 0) < 0)
286 return(1);
287 if (*msgvec == 0) {
288 if (inhook)
289 return 0;
290 printf("No applicable messages.\n");
291 return 1;
293 if (sender_record) {
294 if ((cp = nameof(&message[*msgvec - 1], 0)) == NULL) {
295 printf(catgets(catd, CATSET, 24,
296 "Cannot determine message sender to %s.\n"),
297 cmd);
298 return 1;
300 for (cq = cp; *cq && *cq != '@'; cq++);
301 *cq = '\0';
302 if (value("outfolder")) {
303 file = salloc(strlen(cp) + 2);
304 file[0] = '+';
305 strcpy(&file[1], cp);
306 } else
307 file = cp;
309 if ((file = expand(file)) == NULL)
310 return(1);
311 prot = which_protocol(file);
312 if (prot != PROTO_IMAP) {
313 if (access(file, 0) >= 0) {
314 newfile = 0;
315 disp = catgets(catd, CATSET, 25, "[Appended]");
316 } else {
317 newfile = 1;
318 disp = catgets(catd, CATSET, 26, "[New file]");
321 if ((obuf = convert == SEND_TOFILE ? Fopen(file, "a+") :
322 Zopen(file, "a+", &compressed)) == NULL) {
323 if ((obuf = convert == SEND_TOFILE ? Fopen(file, "wx") :
324 Zopen(file, "wx", &compressed)) == NULL) {
325 perror(file);
326 return(1);
328 } else {
329 if (compressed) {
330 newfile = 0;
331 disp = catgets(catd, CATSET, 25, "[Appended]");
333 if (!newfile && fstat(fileno(obuf), &st) &&
334 S_ISREG(st.st_mode) &&
335 fseek(obuf, -2L, SEEK_END) == 0) {
336 char buf[2];
337 int prependnl = 0;
339 switch (fread(buf, sizeof *buf, 2, obuf)) {
340 case 2:
341 if (buf[1] != '\n') {
342 prependnl = 1;
343 break;
345 /*FALLTHRU*/
346 case 1:
347 if (buf[0] != '\n')
348 prependnl = 1;
349 break;
350 default:
351 if (ferror(obuf)) {
352 perror(file);
353 return(1);
355 prependnl = 0;
357 fflush(obuf);
358 if (prependnl) {
359 putc('\n', obuf);
360 fflush(obuf);
364 tstats[0] = tstats[1] = 0;
365 imap_created_mailbox = 0;
366 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
367 mp = &message[*ip - 1];
368 if (prot == PROTO_IMAP &&
369 ignore[0].i_count == 0 &&
370 ignore[1].i_count == 0 &&
371 imap_thisaccount(file)) {
372 if (imap_copy(mp, *ip, file) == STOP)
373 goto ferr;
374 mstats[0] = -1;
375 mstats[1] = mp->m_xsize;
376 } else if (send(mp, obuf, ignore, NULL,
377 convert, mstats) < 0) {
378 perror(file);
379 goto ferr;
381 touch(mp);
382 if (mark)
383 mp->m_flag |= MSAVED;
384 if (domove) {
385 mp->m_flag |= MDELETED|MSAVED;
386 last = *ip;
388 tstats[0] += mstats[0];
389 tstats[1] += mstats[1];
391 fflush(obuf);
392 if (ferror(obuf)) {
393 perror(file);
394 ferr: success = 0;
396 if (Fclose(obuf) != 0)
397 success = 0;
398 if (success) {
399 if (prot == PROTO_IMAP || prot == PROTO_MAILDIR)
400 disp = prot == PROTO_IMAP && disconnected(file) ?
401 "[Queued]" : imap_created_mailbox ?
402 "[New file]" : "[Appended]";
403 printf("\"%s\" %s ", file, disp);
404 if (tstats[0] >= 0)
405 printf("%lu", (long)tstats[0]);
406 else
407 printf(catgets(catd, CATSET, 27, "binary"));
408 printf("/%lu\n", (long)tstats[1]);
409 } else if (mark) {
410 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
411 mp = &message[*ip - 1];
412 mp->m_flag &= ~MSAVED;
414 } else if (domove) {
415 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
416 mp = &message[*ip - 1];
417 mp->m_flag &= ~(MSAVED|MDELETED);
420 if (domove && last && success) {
421 setdot(&message[last-1]);
422 last = first(0, MDELETED);
423 setdot(&message[last ? last-1 : 0]);
425 return(success == 0);
429 * Write the indicated messages at the end of the passed
430 * file name, minus header and trailing blank line.
431 * This is the MIME save function.
433 int
434 cwrite(void *v)
436 char *str = v;
438 return save1(str, 0, "write", allignore, SEND_TOFILE, 0, 0);
442 * Snarf the file from the end of the command line and
443 * return a pointer to it. If there is no file attached,
444 * return the mbox file. Put a null in front of the file
445 * name so that the message list processing won't see it,
446 * unless the file name is the only thing on the line, in
447 * which case, return 0 in the reference flag variable.
450 static char *
451 snarf(char *linebuf, int *flag, int usembox)
453 char *cp;
455 *flag = 1;
456 if ((cp = laststring(linebuf, flag, 0)) == NULL) {
457 if (usembox) {
458 *flag = 0;
459 return expand("&");
460 } else {
461 printf(catgets(catd, CATSET, 28,
462 "No file specified.\n"));
463 return NULL;
466 return(cp);
470 * Delete messages.
472 int
473 delete(void *v)
475 int *msgvec = v;
476 delm(msgvec);
477 return 0;
481 * Delete messages, then type the new dot.
483 int
484 deltype(void *v)
486 int *msgvec = v;
487 int list[2];
488 int lastdot;
490 lastdot = dot - &message[0] + 1;
491 if (delm(msgvec) >= 0) {
492 list[0] = dot - &message[0] + 1;
493 if (list[0] > lastdot) {
494 touch(dot);
495 list[1] = 0;
496 return(type(list));
498 printf(catgets(catd, CATSET, 29, "At EOF\n"));
499 } else
500 printf(catgets(catd, CATSET, 30, "No more messages\n"));
501 return(0);
505 * Delete the indicated messages.
506 * Set dot to some nice place afterwards.
507 * Internal interface.
509 static int
510 delm(int *msgvec)
512 struct message *mp;
513 int *ip;
514 int last;
516 last = 0;
517 for (ip = msgvec; *ip != 0; ip++) {
518 mp = &message[*ip - 1];
519 touch(mp);
520 mp->m_flag |= MDELETED|MTOUCH;
521 mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
522 last = *ip;
524 if (last != 0) {
525 setdot(&message[last-1]);
526 last = first(0, MDELETED);
527 if (last != 0) {
528 setdot(&message[last-1]);
529 return(0);
531 else {
532 setdot(&message[0]);
533 return(-1);
538 * Following can't happen -- it keeps lint happy
541 return(-1);
545 * Undelete the indicated messages.
547 int
548 undeletecmd(void *v)
550 int *msgvec = v;
551 struct message *mp;
552 int *ip;
554 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
555 mp = &message[*ip - 1];
556 touch(mp);
557 setdot(mp);
558 if (mp->m_flag & (MDELETED|MSAVED))
559 mp->m_flag &= ~(MDELETED|MSAVED);
560 else
561 mp->m_flag &= ~MDELETED;
562 if (mb.mb_type == MB_IMAP || mb.mb_type == MB_CACHE)
563 imap_undelete(mp, *ip);
565 return 0;
568 #ifdef HAVE_ASSERTS
570 * Interactively dump core on "core"
572 /*ARGSUSED*/
573 int
574 core(void *v)
576 int pid;
577 # ifdef WCOREDUMP
578 extern int wait_status;
579 # endif
580 (void)v;
582 switch (pid = fork()) {
583 case -1:
584 perror("fork");
585 return (1);
586 case 0:
587 abort();
588 _exit(1);
590 (void)printf(tr(31, "Okie dokie"));
591 (void)fflush(stdout);
592 (void)wait_child(pid);
593 # ifdef WCOREDUMP
594 if (WCOREDUMP(wait_status))
595 (void)printf(tr(32, " -- Core dumped.\n"));
596 else
597 (void)printf(tr(33, " -- Can't dump core.\n"));
598 # endif
599 return (0);
602 static void
603 clob1(int n)
605 char buf[512], *cp;
607 if (n <= 0)
608 return;
609 for (cp = buf; cp < &buf[512]; *cp++ = (char)0xFF)
611 clob1(n - 1);
615 * Clobber as many bytes of stack as the user requests.
617 int
618 clobber(void *v)
620 char **argv = v;
621 int times;
623 if (argv[0] == 0)
624 times = 1;
625 else
626 times = (atoi(argv[0]) + 511) / 512;
627 clob1(times);
628 return (0);
630 #endif /* HAVE_ASSERTS */
633 * Add the given header fields to the retained list.
634 * If no arguments, print the current list of retained fields.
636 int
637 retfield(void *v)
639 char **list = v;
641 return ignore1(list, ignore + 1, "retained");
645 * Add the given header fields to the ignored list.
646 * If no arguments, print the current list of ignored fields.
648 int
649 igfield(void *v)
651 char **list = v;
653 return ignore1(list, ignore, "ignored");
656 int
657 saveretfield(void *v)
659 char **list = v;
661 return ignore1(list, saveignore + 1, "retained");
664 int
665 saveigfield(void *v)
667 char **list = v;
669 return ignore1(list, saveignore, "ignored");
672 int
673 fwdretfield(void *v)
675 char **list = v;
677 return ignore1(list, fwdignore + 1, "retained");
680 int
681 fwdigfield(void *v)
683 char **list = v;
685 return ignore1(list, fwdignore, "ignored");
688 static int
689 ignore1(char **list, struct ignoretab *tab, char *which)
691 int h;
692 struct ignore *igp;
693 char **ap;
695 if (*list == NULL)
696 return igshow(tab, which);
697 for (ap = list; *ap != 0; ap++) {
698 char *field;
699 size_t sz;
701 sz = strlen(*ap);
702 field = ac_alloc(sz + 1);
703 i_strcpy(field, *ap, sz + 1);
704 field[sz]='\0';
705 if (member(field, tab)) {
706 ac_free(field);
707 continue;
709 h = hash(field);
710 igp = (struct ignore *)scalloc(1, sizeof (struct ignore));
711 igp->i_field = smalloc(strlen(field) + 1);
712 strcpy(igp->i_field, field);
713 igp->i_link = tab->i_head[h];
714 tab->i_head[h] = igp;
715 tab->i_count++;
716 ac_free(field);
718 return 0;
722 * Print out all currently retained fields.
724 static int
725 igshow(struct ignoretab *tab, char *which)
727 int h;
728 struct ignore *igp;
729 char **ap, **ring;
731 if (tab->i_count == 0) {
732 printf(catgets(catd, CATSET, 34,
733 "No fields currently being %s.\n"), which);
734 return 0;
736 /*LINTED*/
737 ring = (char **)salloc((tab->i_count + 1) * sizeof (char *));
738 ap = ring;
739 for (h = 0; h < HSHSIZE; h++)
740 for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
741 *ap++ = igp->i_field;
742 *ap = 0;
743 qsort(ring, tab->i_count, sizeof (char *), igcomp);
744 for (ap = ring; *ap != 0; ap++)
745 printf("%s\n", *ap);
746 return 0;
750 * Compare two names for sorting ignored field list.
752 static int
753 igcomp(const void *l, const void *r)
755 return (strcmp(*(char **)l, *(char **)r));
758 int
759 unignore(void *v)
761 return unignore1((char **)v, ignore, "ignored");
764 int
765 unretain(void *v)
767 return unignore1((char **)v, ignore + 1, "retained");
770 int
771 unsaveignore(void *v)
773 return unignore1((char **)v, saveignore, "ignored");
776 int
777 unsaveretain(void *v)
779 return unignore1((char **)v, saveignore + 1, "retained");
782 int
783 unfwdignore(void *v)
785 return unignore1((char **)v, fwdignore, "ignored");
788 int
789 unfwdretain(void *v)
791 return unignore1((char **)v, fwdignore + 1, "retained");
794 static void
795 unignore_one(const char *name, struct ignoretab *tab)
797 struct ignore *ip, *iq = NULL;
798 int h = hash(name);
800 for (ip = tab->i_head[h]; ip; ip = ip->i_link) {
801 if (asccasecmp(ip->i_field, name) == 0) {
802 free(ip->i_field);
803 if (iq != NULL)
804 iq->i_link = ip->i_link;
805 else
806 tab->i_head[h] = ip->i_link;
807 free(ip);
808 tab->i_count--;
809 break;
811 iq = ip;
815 static int
816 unignore1(char **list, struct ignoretab *tab, char *which)
818 if (tab->i_count == 0) {
819 printf(catgets(catd, CATSET, 34,
820 "No fields currently being %s.\n"), which);
821 return 0;
823 while (*list)
824 unignore_one(*list++, tab);
825 return 0;