1 /*@ 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 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
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
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
40 #ifndef HAVE_AMALGAMATION
46 static int save1(char *str
, int domark
, char const *cmd
,
47 struct ignoretab
*ignore
, int convert
,
48 int sender_record
, int domove
);
49 static char * snarf(char *linebuf
, bool_t
*flag
, bool_t usembox
);
50 static int delm(int *msgvec
);
52 static void clob1(int n
);
54 static int ignore1(char **list
, struct ignoretab
*tab
, char const *which
);
55 static int igshow(struct ignoretab
*tab
, char const *which
);
56 static int igcomp(const void *l
, const void *r
);
57 static void unignore_one(const char *name
, struct ignoretab
*tab
);
58 static int unignore1(char **list
, struct ignoretab
*tab
,
62 * If any arguments were given, go to the next applicable argument
63 * following dot, otherwise, go to the next applicable message.
64 * If given as first command with no arguments, print first message.
77 * If some messages were supplied, find the
78 * first applicable one following dot using
82 mdot
= dot
- &message
[0] + 1;
85 * Find the first message in the supplied
86 * message list which follows dot.
89 for (ip
= msgvec
; *ip
!= 0; ip
++) {
92 * Work around an optimizer bug in Cray Standard C Version 4.0.3 (057126).
93 * Otherwise, SIGFPE is received when mb.mb_threaded != 0.
95 #pragma _CRI suppress ip
97 if (mb
.mb_threaded
? message
[*ip
-1].m_threadpos
>
106 mp
= &message
[*ip2
- 1];
107 if ((mp
->m_flag
& (MDELETED
|MHIDDEN
)) == 0) {
116 printf(tr(21, "No messages applicable\n"));
121 * If this is the first command, select message 1.
122 * Note that this must exist for us to get here at all.
132 * Just find the next good message after dot, no
136 if (mb
.mb_threaded
== 0) {
137 for (mp
= dot
+ did_print_dot
; mp
< &message
[msgCount
]; mp
++)
138 if ((mp
->m_flag
& (MDELETED
|MSAVED
|MHIDDEN
)) == 0)
143 mp
= next_in_thread(mp
);
144 while (mp
&& mp
->m_flag
& (MDELETED
|MSAVED
|MHIDDEN
))
145 mp
= next_in_thread(mp
);
147 if (mp
== NULL
|| mp
>= &message
[msgCount
]) {
149 printf(tr(22, "At EOF\n"));
158 list
[0] = dot
- &message
[0] + 1;
164 * Save a message in a file. Mark the message as saved
165 * so we can discard when the user quits.
172 return save1(str
, 1, "save", saveignore
, SEND_MBOX
, 0, 0);
180 return save1(str
, 1, "save", saveignore
, SEND_MBOX
, 1, 0);
184 * Copy a message to a file without affected its saved-ness
191 return save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 0, 0);
199 return save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 1, 0);
203 * Move a message to a file.
210 return save1(str
, 0, "move", saveignore
, SEND_MBOX
, 0, 1);
218 return save1(str
, 0, "move", saveignore
, SEND_MBOX
, 1, 1);
222 * Decrypt and copy a message to a file.
229 return save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 0, 0);
237 return save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 1, 0);
241 * Save/copy the indicated messages at the end of the passed file name.
242 * If mark is true, mark the message "saved."
245 save1(char *str
, int domark
, char const *cmd
, struct ignoretab
*ignoret
,
246 int convert
, int sender_record
, int domove
)
248 off_t mstats
[2], tstats
[2];
250 int newfile
= 0, compressed
= 0, success
= 1, last
= 0, *msgvec
, *ip
;
252 char *file
= NULL
, *cp
, *cq
;
253 char const *disp
= "";
259 msgvec
= (int *)salloc((msgCount
+ 2) * sizeof *msgvec
);
261 for (cp
= str
; *cp
&& blankchar(*cp
); cp
++)
265 if ((file
= snarf(str
, &f
, convert
!= SEND_TOFILE
)) == NULL
)
269 *msgvec
= first(0, MMNORM
);
273 printf(tr(23, "No messages to %s.\n"), cmd
);
277 } else if (getmsglist(str
, msgvec
, 0) < 0)
282 printf("No applicable messages.\n");
286 if ((cp
= nameof(&message
[*msgvec
- 1], 0)) == NULL
) {
288 "Cannot determine message sender to %s.\n"),
292 for (cq
= cp
; *cq
&& *cq
!= '@'; cq
++);
294 if (value("outfolder")) {
295 size_t sz
= strlen(cp
) + 1;
296 file
= salloc(sz
+ 1);
298 memcpy(file
+ 1, cp
, sz
);
302 if ((file
= expand(file
)) == NULL
)
304 prot
= which_protocol(file
);
305 if (prot
!= PROTO_IMAP
) {
306 if (access(file
, 0) >= 0) {
308 disp
= tr(25, "[Appended]");
311 disp
= tr(26, "[New file]");
314 if ((obuf
= convert
== SEND_TOFILE
? Fopen(file
, "a+") :
315 Zopen(file
, "a+", &compressed
)) == NULL
) {
316 if ((obuf
= convert
== SEND_TOFILE
? Fopen(file
, "wx") :
317 Zopen(file
, "wx", &compressed
)) == NULL
) {
324 disp
= tr(25, "[Appended]");
326 if (!newfile
&& fstat(fileno(obuf
), &st
) &&
327 S_ISREG(st
.st_mode
) &&
328 fseek(obuf
, -2L, SEEK_END
) == 0) {
332 switch (fread(buf
, sizeof *buf
, 2, obuf
)) {
334 if (buf
[1] != '\n') {
358 tstats
[0] = tstats
[1] = 0;
359 imap_created_mailbox
= 0;
361 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
362 mp
= &message
[*ip
- 1];
363 if (prot
== PROTO_IMAP
&&
364 ignoret
[0].i_count
== 0 &&
365 ignoret
[1].i_count
== 0
366 #ifdef HAVE_IMAP /* TODO revisit */
367 && imap_thisaccount(file
)
371 if (imap_copy(mp
, *ip
, file
) == STOP
)
376 mstats
[1] = mp
->m_xsize
;
378 } else if (sendmp(mp
, obuf
, ignoret
, NULL
,
379 convert
, mstats
) < 0) {
386 mp
->m_flag
|= MSAVED
;
388 mp
->m_flag
|= MDELETED
|MSAVED
;
391 tstats
[0] += mstats
[0];
392 tstats
[1] += mstats
[1];
400 if (Fclose(obuf
) != 0)
405 if (prot
== PROTO_IMAP
|| prot
== PROTO_MAILDIR
) {
408 ((prot
== PROTO_IMAP
) && disconnected(file
))
411 (imap_created_mailbox
? "[New file]"
414 printf("\"%s\" %s ", file
, disp
);
416 printf("%lu", (long)tstats
[0]);
418 printf(tr(27, "binary"));
419 printf("/%lu\n", (long)tstats
[1]);
421 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
422 mp
= &message
[*ip
- 1];
423 mp
->m_flag
&= ~MSAVED
;
426 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
427 mp
= &message
[*ip
- 1];
428 mp
->m_flag
&= ~(MSAVED
|MDELETED
);
431 if (domove
&& last
&& success
) {
432 setdot(&message
[last
-1]);
433 last
= first(0, MDELETED
);
434 setdot(&message
[last
? last
-1 : 0]);
436 return(success
== 0);
440 * Write the indicated messages at the end of the passed
441 * file name, minus header and trailing blank line.
442 * This is the MIME save function.
449 if (str
== NULL
|| *str
== '\0')
450 str
= savestr("/dev/null");
451 return (save1(str
, 0, "write", allignore
, SEND_TOFILE
, 0, 0));
455 * Snarf the file from the end of the command line and
456 * return a pointer to it. If there is no file attached,
457 * return the mbox file. Put a null in front of the file
458 * name so that the message list processing won't see it,
459 * unless the file name is the only thing on the line, in
460 * which case, return 0 in the reference flag variable.
463 snarf(char *linebuf
, bool_t
*flag
, bool_t usembox
)
467 if ((cp
= laststring(linebuf
, flag
, 0)) == NULL
) {
472 fprintf(stderr
, tr(28, "No file specified.\n"));
489 * Delete messages, then type the new dot.
498 lastdot
= dot
- &message
[0] + 1;
499 if (delm(msgvec
) >= 0) {
500 list
[0] = dot
- &message
[0] + 1;
501 if (list
[0] > lastdot
) {
506 printf(tr(29, "At EOF\n"));
508 printf(tr(30, "No more messages\n"));
513 * Delete the indicated messages.
514 * Set dot to some nice place afterwards.
515 * Internal interface.
525 for (ip
= msgvec
; *ip
!= 0; ip
++) {
526 mp
= &message
[*ip
- 1];
528 mp
->m_flag
|= MDELETED
|MTOUCH
;
529 mp
->m_flag
&= ~(MPRESERVE
|MSAVED
|MBOX
);
533 setdot(&message
[last
-1]);
534 last
= first(0, MDELETED
);
536 setdot(&message
[last
-1]);
546 * Following can't happen -- it keeps lint happy
553 * Undelete the indicated messages.
562 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
563 mp
= &message
[*ip
- 1];
566 if (mp
->m_flag
& (MDELETED
|MSAVED
))
567 mp
->m_flag
&= ~(MDELETED
|MSAVED
);
569 mp
->m_flag
&= ~MDELETED
;
571 if (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)
572 imap_undelete(mp
, *ip
);
580 * Interactively dump core on "core"
588 extern int wait_status
;
592 switch (pid
= fork()) {
600 (void)printf(tr(31, "Okie dokie"));
601 (void)fflush(stdout
);
602 (void)wait_child(pid
);
604 if (WCOREDUMP(wait_status
))
605 (void)printf(tr(32, " -- Core dumped.\n"));
607 (void)printf(tr(33, " -- Can't dump core.\n"));
619 for (cp
= buf
; PTRCMP(cp
, <, buf
+ 512); ++cp
)
625 * Clobber as many bytes of stack as the user requests.
636 times
= (atoi(argv
[0]) + 511) / 512;
640 #endif /* HAVE_DEBUG */
643 * Add the given header fields to the retained list.
644 * If no arguments, print the current list of retained fields.
651 return ignore1(list
, ignore
+ 1, "retained");
655 * Add the given header fields to the ignored list.
656 * If no arguments, print the current list of ignored fields.
663 return ignore1(list
, ignore
, "ignored");
667 saveretfield(void *v
)
671 return ignore1(list
, saveignore
+ 1, "retained");
679 return ignore1(list
, saveignore
, "ignored");
687 return ignore1(list
, fwdignore
+ 1, "retained");
695 return ignore1(list
, fwdignore
, "ignored");
699 ignore1(char **list
, struct ignoretab
*tab
, char const *which
)
706 return igshow(tab
, which
);
707 for (ap
= list
; *ap
!= 0; ap
++) {
712 field
= ac_alloc(sz
+ 1);
713 i_strcpy(field
, *ap
, sz
+ 1);
715 if (member(field
, tab
)) {
720 igp
= (struct ignore
*)scalloc(1, sizeof (struct ignore
));
721 sz
= strlen(field
) + 1;
722 igp
->i_field
= smalloc(sz
);
723 memcpy(igp
->i_field
, field
, sz
);
724 igp
->i_link
= tab
->i_head
[h
];
725 tab
->i_head
[h
] = igp
;
733 * Print out all currently retained fields.
736 igshow(struct ignoretab
*tab
, char const *which
)
742 if (tab
->i_count
== 0) {
743 printf(tr(34, "No fields currently being %s.\n"), which
);
747 ring
= (char **)salloc((tab
->i_count
+ 1) * sizeof (char *));
749 for (h
= 0; h
< HSHSIZE
; h
++)
750 for (igp
= tab
->i_head
[h
]; igp
!= 0; igp
= igp
->i_link
)
751 *ap
++ = igp
->i_field
;
753 qsort(ring
, tab
->i_count
, sizeof (char *), igcomp
);
754 for (ap
= ring
; *ap
!= 0; ap
++)
760 * Compare two names for sorting ignored field list.
763 igcomp(const void *l
, const void *r
)
765 return (strcmp(*(char**)UNCONST(l
), *(char**)UNCONST(r
)));
771 return unignore1((char **)v
, ignore
, "ignored");
777 return unignore1((char **)v
, ignore
+ 1, "retained");
781 unsaveignore(void *v
)
783 return unignore1((char **)v
, saveignore
, "ignored");
787 unsaveretain(void *v
)
789 return unignore1((char **)v
, saveignore
+ 1, "retained");
795 return unignore1((char **)v
, fwdignore
, "ignored");
801 return unignore1((char **)v
, fwdignore
+ 1, "retained");
805 unignore_one(const char *name
, struct ignoretab
*tab
)
807 struct ignore
*ip
, *iq
= NULL
;
810 for (ip
= tab
->i_head
[h
]; ip
; ip
= ip
->i_link
) {
811 if (asccasecmp(ip
->i_field
, name
) == 0) {
814 iq
->i_link
= ip
->i_link
;
816 tab
->i_head
[h
] = ip
->i_link
;
826 unignore1(char **list
, struct ignoretab
*tab
, char const *which
)
828 if (tab
->i_count
== 0) {
829 printf(tr(34, "No fields currently being %s.\n"), which
);
833 unignore_one(*list
++, tab
);