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.
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
47 * Mail -- a mail program
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
);
57 static void clob1(int n
);
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.
81 * If some messages were supplied, find the
82 * first applicable one following dot using
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
++) {
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
101 if (mb
.mb_threaded
? message
[*ip
-1].m_threadpos
>
110 mp
= &message
[*ip2
- 1];
111 if ((mp
->m_flag
& (MDELETED
|MHIDDEN
)) == 0) {
120 printf(catgets(catd
, CATSET
, 21, "No messages applicable\n"));
125 * If this is the first command, select message 1.
126 * Note that this must exist for us to get here at all.
136 * Just find the next good message after dot, no
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)
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
]) {
153 printf(catgets(catd
, CATSET
, 22, "At EOF\n"));
162 list
[0] = dot
- &message
[0] + 1;
168 * Save a message in a file. Mark the message as saved
169 * so we can discard when the user quits.
176 return save1(str
, 1, "save", saveignore
, SEND_MBOX
, 0, 0);
184 return save1(str
, 1, "save", saveignore
, SEND_MBOX
, 1, 0);
188 * Copy a message to a file without affected its saved-ness
195 return save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 0, 0);
203 return save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 1, 0);
207 * Move a message to a file.
214 return save1(str
, 0, "move", saveignore
, SEND_MBOX
, 0, 1);
222 return save1(str
, 0, "move", saveignore
, SEND_MBOX
, 1, 1);
226 * Decrypt and copy a message to a file.
233 return save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 0, 0);
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."
249 save1(char *str
, int mark
, char *cmd
, struct ignoretab
*ignore
,
250 int convert
, int sender_record
, int domove
)
255 char *file
= NULL
, *disp
= "";
260 off_t mstats
[2], tstats
[2];
263 int success
= 1, last
= 0;
266 msgvec
= (int *)salloc((msgCount
+ 2) * sizeof *msgvec
);
268 for (cp
= str
; *cp
&& blankchar(*cp
& 0377); cp
++);
271 if ((file
= snarf(str
, &f
, convert
!= SEND_TOFILE
)) == NULL
)
275 *msgvec
= first(0, MMNORM
);
279 printf(catgets(catd
, CATSET
, 23,
280 "No messages to %s.\n"), cmd
);
285 if (f
&& getmsglist(str
, msgvec
, 0) < 0)
290 printf("No applicable messages.\n");
294 if ((cp
= nameof(&message
[*msgvec
- 1], 0)) == NULL
) {
295 printf(catgets(catd
, CATSET
, 24,
296 "Cannot determine message sender to %s.\n"),
300 for (cq
= cp
; *cq
&& *cq
!= '@'; cq
++);
302 if (value("outfolder")) {
303 file
= salloc(strlen(cp
) + 2);
305 strcpy(&file
[1], cp
);
309 if ((file
= expand(file
)) == NULL
)
311 prot
= which_protocol(file
);
312 if (prot
!= PROTO_IMAP
) {
313 if (access(file
, 0) >= 0) {
315 disp
= catgets(catd
, CATSET
, 25, "[Appended]");
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
) {
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) {
339 switch (fread(buf
, sizeof *buf
, 2, obuf
)) {
341 if (buf
[1] != '\n') {
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
)
375 mstats
[1] = mp
->m_xsize
;
376 } else if (send(mp
, obuf
, ignore
, NULL
,
377 convert
, mstats
) < 0) {
383 mp
->m_flag
|= MSAVED
;
385 mp
->m_flag
|= MDELETED
|MSAVED
;
388 tstats
[0] += mstats
[0];
389 tstats
[1] += mstats
[1];
396 if (Fclose(obuf
) != 0)
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
);
405 printf("%lu", (long)tstats
[0]);
407 printf(catgets(catd
, CATSET
, 27, "binary"));
408 printf("/%lu\n", (long)tstats
[1]);
410 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
411 mp
= &message
[*ip
- 1];
412 mp
->m_flag
&= ~MSAVED
;
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.
438 if (str
== NULL
|| *str
== '\0')
439 str
= savestr("/dev/null");
440 return (save1(str
, 0, "write", allignore
, SEND_TOFILE
, 0, 0));
444 * Snarf the file from the end of the command line and
445 * return a pointer to it. If there is no file attached,
446 * return the mbox file. Put a null in front of the file
447 * name so that the message list processing won't see it,
448 * unless the file name is the only thing on the line, in
449 * which case, return 0 in the reference flag variable.
453 snarf(char *linebuf
, int *flag
, int usembox
)
458 if ((cp
= laststring(linebuf
, flag
, 0)) == NULL
) {
463 printf(tr(28, "No file specified.\n"));
480 * Delete messages, then type the new dot.
489 lastdot
= dot
- &message
[0] + 1;
490 if (delm(msgvec
) >= 0) {
491 list
[0] = dot
- &message
[0] + 1;
492 if (list
[0] > lastdot
) {
497 printf(catgets(catd
, CATSET
, 29, "At EOF\n"));
499 printf(catgets(catd
, CATSET
, 30, "No more messages\n"));
504 * Delete the indicated messages.
505 * Set dot to some nice place afterwards.
506 * Internal interface.
516 for (ip
= msgvec
; *ip
!= 0; ip
++) {
517 mp
= &message
[*ip
- 1];
519 mp
->m_flag
|= MDELETED
|MTOUCH
;
520 mp
->m_flag
&= ~(MPRESERVE
|MSAVED
|MBOX
);
524 setdot(&message
[last
-1]);
525 last
= first(0, MDELETED
);
527 setdot(&message
[last
-1]);
537 * Following can't happen -- it keeps lint happy
544 * Undelete the indicated messages.
553 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
554 mp
= &message
[*ip
- 1];
557 if (mp
->m_flag
& (MDELETED
|MSAVED
))
558 mp
->m_flag
&= ~(MDELETED
|MSAVED
);
560 mp
->m_flag
&= ~MDELETED
;
561 if (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)
562 imap_undelete(mp
, *ip
);
569 * Interactively dump core on "core"
577 extern int wait_status
;
581 switch (pid
= fork()) {
589 (void)printf(tr(31, "Okie dokie"));
590 (void)fflush(stdout
);
591 (void)wait_child(pid
);
593 if (WCOREDUMP(wait_status
))
594 (void)printf(tr(32, " -- Core dumped.\n"));
596 (void)printf(tr(33, " -- Can't dump core.\n"));
608 for (cp
= buf
; cp
< &buf
[512]; *cp
++ = (char)0xFF)
614 * Clobber as many bytes of stack as the user requests.
625 times
= (atoi(argv
[0]) + 511) / 512;
629 #endif /* HAVE_ASSERTS */
632 * Add the given header fields to the retained list.
633 * If no arguments, print the current list of retained fields.
640 return ignore1(list
, ignore
+ 1, "retained");
644 * Add the given header fields to the ignored list.
645 * If no arguments, print the current list of ignored fields.
652 return ignore1(list
, ignore
, "ignored");
656 saveretfield(void *v
)
660 return ignore1(list
, saveignore
+ 1, "retained");
668 return ignore1(list
, saveignore
, "ignored");
676 return ignore1(list
, fwdignore
+ 1, "retained");
684 return ignore1(list
, fwdignore
, "ignored");
688 ignore1(char **list
, struct ignoretab
*tab
, char *which
)
695 return igshow(tab
, which
);
696 for (ap
= list
; *ap
!= 0; ap
++) {
701 field
= ac_alloc(sz
+ 1);
702 i_strcpy(field
, *ap
, sz
+ 1);
704 if (member(field
, tab
)) {
709 igp
= (struct ignore
*)scalloc(1, sizeof (struct ignore
));
710 igp
->i_field
= smalloc(strlen(field
) + 1);
711 strcpy(igp
->i_field
, field
);
712 igp
->i_link
= tab
->i_head
[h
];
713 tab
->i_head
[h
] = igp
;
721 * Print out all currently retained fields.
724 igshow(struct ignoretab
*tab
, char *which
)
730 if (tab
->i_count
== 0) {
731 printf(catgets(catd
, CATSET
, 34,
732 "No fields currently being %s.\n"), which
);
736 ring
= (char **)salloc((tab
->i_count
+ 1) * sizeof (char *));
738 for (h
= 0; h
< HSHSIZE
; h
++)
739 for (igp
= tab
->i_head
[h
]; igp
!= 0; igp
= igp
->i_link
)
740 *ap
++ = igp
->i_field
;
742 qsort(ring
, tab
->i_count
, sizeof (char *), igcomp
);
743 for (ap
= ring
; *ap
!= 0; ap
++)
749 * Compare two names for sorting ignored field list.
752 igcomp(const void *l
, const void *r
)
754 return (strcmp(*(char **)l
, *(char **)r
));
760 return unignore1((char **)v
, ignore
, "ignored");
766 return unignore1((char **)v
, ignore
+ 1, "retained");
770 unsaveignore(void *v
)
772 return unignore1((char **)v
, saveignore
, "ignored");
776 unsaveretain(void *v
)
778 return unignore1((char **)v
, saveignore
+ 1, "retained");
784 return unignore1((char **)v
, fwdignore
, "ignored");
790 return unignore1((char **)v
, fwdignore
+ 1, "retained");
794 unignore_one(const char *name
, struct ignoretab
*tab
)
796 struct ignore
*ip
, *iq
= NULL
;
799 for (ip
= tab
->i_head
[h
]; ip
; ip
= ip
->i_link
) {
800 if (asccasecmp(ip
->i_field
, name
) == 0) {
803 iq
->i_link
= ip
->i_link
;
805 tab
->i_head
[h
] = ip
->i_link
;
815 unignore1(char **list
, struct ignoretab
*tab
, char *which
)
817 if (tab
->i_count
== 0) {
818 printf(catgets(catd
, CATSET
, 34,
819 "No fields currently being %s.\n"), which
);
823 unignore_one(*list
++, tab
);