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 - 2014 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
);
51 static int ignore1(char **list
, struct ignoretab
*tab
, char const *which
);
52 static int igshow(struct ignoretab
*tab
, char const *which
);
53 static int igcomp(const void *l
, const void *r
);
54 static void unignore_one(const char *name
, struct ignoretab
*tab
);
55 static int unignore1(char **list
, struct ignoretab
*tab
,
59 * If any arguments were given, go to the next applicable argument
60 * following dot, otherwise, go to the next applicable message.
61 * If given as first command with no arguments, print first message.
74 * If some messages were supplied, find the
75 * first applicable one following dot using
79 mdot
= dot
- &message
[0] + 1;
82 * Find the first message in the supplied
83 * message list which follows dot.
86 for (ip
= msgvec
; *ip
!= 0; ip
++) {
89 * Work around an optimizer bug in Cray Standard C Version 4.0.3 (057126).
90 * Otherwise, SIGFPE is received when mb.mb_threaded != 0.
92 #pragma _CRI suppress ip
94 if (mb
.mb_threaded
? message
[*ip
-1].m_threadpos
>
103 mp
= &message
[*ip2
- 1];
104 if ((mp
->m_flag
& (MDELETED
|MHIDDEN
)) == 0) {
113 printf(tr(21, "No messages applicable\n"));
118 * If this is the first command, select message 1.
119 * Note that this must exist for us to get here at all.
129 * Just find the next good message after dot, no
133 if (mb
.mb_threaded
== 0) {
134 for (mp
= dot
+ did_print_dot
; mp
< &message
[msgCount
]; mp
++)
135 if ((mp
->m_flag
& (MDELETED
|MSAVED
|MHIDDEN
)) == 0)
140 mp
= next_in_thread(mp
);
141 while (mp
&& mp
->m_flag
& (MDELETED
|MSAVED
|MHIDDEN
))
142 mp
= next_in_thread(mp
);
144 if (mp
== NULL
|| mp
>= &message
[msgCount
]) {
146 printf(tr(22, "At EOF\n"));
155 list
[0] = dot
- &message
[0] + 1;
161 * Save a message in a file. Mark the message as saved
162 * so we can discard when the user quits.
169 return save1(str
, 1, "save", saveignore
, SEND_MBOX
, 0, 0);
177 return save1(str
, 1, "save", saveignore
, SEND_MBOX
, 1, 0);
181 * Copy a message to a file without affected its saved-ness
188 return save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 0, 0);
196 return save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 1, 0);
200 * Move a message to a file.
207 return save1(str
, 0, "move", saveignore
, SEND_MBOX
, 0, 1);
215 return save1(str
, 0, "move", saveignore
, SEND_MBOX
, 1, 1);
219 * Decrypt and copy a message to a file.
226 return save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 0, 0);
234 return save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 1, 0);
238 * Save/copy the indicated messages at the end of the passed file name.
239 * If mark is true, mark the message "saved."
242 save1(char *str
, int domark
, char const *cmd
, struct ignoretab
*ignoret
,
243 int convert
, int sender_record
, int domove
)
245 off_t mstats
[2], tstats
[2];
247 int newfile
= 0, compressed
= 0, success
= 1, last
= 0, *msgvec
, *ip
;
249 char *file
= NULL
, *cp
, *cq
;
250 char const *disp
= "";
256 msgvec
= (int *)salloc((msgCount
+ 2) * sizeof *msgvec
);
258 for (cp
= str
; *cp
&& blankchar(*cp
); cp
++)
262 if ((file
= snarf(str
, &f
, convert
!= SEND_TOFILE
)) == NULL
)
266 *msgvec
= first(0, MMNORM
);
270 printf(tr(23, "No messages to %s.\n"), cmd
);
274 } else if (getmsglist(str
, msgvec
, 0) < 0)
279 printf("No applicable messages.\n");
283 if ((cp
= nameof(&message
[*msgvec
- 1], 0)) == NULL
) {
285 "Cannot determine message sender to %s.\n"),
289 for (cq
= cp
; *cq
&& *cq
!= '@'; cq
++);
291 if (ok_blook(outfolder
)) {
292 size_t sz
= strlen(cp
) + 1;
293 file
= salloc(sz
+ 1);
295 memcpy(file
+ 1, cp
, sz
);
299 if ((file
= expand(file
)) == NULL
)
301 prot
= which_protocol(file
);
302 if (prot
!= PROTO_IMAP
) {
303 if (access(file
, 0) >= 0) {
305 disp
= tr(25, "[Appended]");
308 disp
= tr(26, "[New file]");
311 if ((obuf
= convert
== SEND_TOFILE
? Fopen(file
, "a+") :
312 Zopen(file
, "a+", &compressed
)) == NULL
) {
313 if ((obuf
= convert
== SEND_TOFILE
? Fopen(file
, "wx") :
314 Zopen(file
, "wx", &compressed
)) == NULL
) {
321 disp
= tr(25, "[Appended]");
323 if (!newfile
&& fstat(fileno(obuf
), &st
) &&
324 S_ISREG(st
.st_mode
) &&
325 fseek(obuf
, -2L, SEEK_END
) == 0) {
329 switch (fread(buf
, sizeof *buf
, 2, obuf
)) {
331 if (buf
[1] != '\n') {
355 tstats
[0] = tstats
[1] = 0;
356 imap_created_mailbox
= 0;
358 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
359 mp
= &message
[*ip
- 1];
360 if (prot
== PROTO_IMAP
&&
361 ignoret
[0].i_count
== 0 &&
362 ignoret
[1].i_count
== 0
363 #ifdef HAVE_IMAP /* TODO revisit */
364 && imap_thisaccount(file
)
368 if (imap_copy(mp
, *ip
, file
) == STOP
)
373 mstats
[1] = mp
->m_xsize
;
375 } else if (sendmp(mp
, obuf
, ignoret
, NULL
,
376 convert
, mstats
) < 0) {
383 mp
->m_flag
|= MSAVED
;
385 mp
->m_flag
|= MDELETED
|MSAVED
;
388 tstats
[0] += mstats
[0];
389 tstats
[1] += mstats
[1];
397 if (Fclose(obuf
) != 0)
402 if (prot
== PROTO_IMAP
|| prot
== PROTO_MAILDIR
) {
405 ((prot
== PROTO_IMAP
) && disconnected(file
))
408 (imap_created_mailbox
? "[New file]"
411 printf("\"%s\" %s ", file
, disp
);
413 printf("%lu", (long)tstats
[0]);
415 printf(tr(27, "binary"));
416 printf("/%lu\n", (long)tstats
[1]);
418 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
419 mp
= &message
[*ip
- 1];
420 mp
->m_flag
&= ~MSAVED
;
423 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
424 mp
= &message
[*ip
- 1];
425 mp
->m_flag
&= ~(MSAVED
|MDELETED
);
428 if (domove
&& last
&& success
) {
429 setdot(&message
[last
-1]);
430 last
= first(0, MDELETED
);
431 setdot(&message
[last
? last
-1 : 0]);
433 return(success
== 0);
437 * Write the indicated messages at the end of the passed
438 * file name, minus header and trailing blank line.
439 * This is the MIME save function.
446 if (str
== NULL
|| *str
== '\0')
447 str
= savestr("/dev/null");
448 return (save1(str
, 0, "write", allignore
, SEND_TOFILE
, 0, 0));
452 * Snarf the file from the end of the command line and
453 * return a pointer to it. If there is no file attached,
454 * return the mbox file. Put a null in front of the file
455 * name so that the message list processing won't see it,
456 * unless the file name is the only thing on the line, in
457 * which case, return 0 in the reference flag variable.
460 snarf(char *linebuf
, bool_t
*flag
, bool_t usembox
)
464 if ((cp
= laststring(linebuf
, flag
, 0)) == NULL
) {
469 fprintf(stderr
, tr(28, "No file specified.\n"));
486 * Delete messages, then type the new dot.
495 lastdot
= dot
- &message
[0] + 1;
496 if (delm(msgvec
) >= 0) {
497 list
[0] = dot
- &message
[0] + 1;
498 if (list
[0] > lastdot
) {
503 printf(tr(29, "At EOF\n"));
505 printf(tr(30, "No more messages\n"));
510 * Delete the indicated messages.
511 * Set dot to some nice place afterwards.
512 * Internal interface.
522 for (ip
= msgvec
; *ip
!= 0; ip
++) {
523 mp
= &message
[*ip
- 1];
525 mp
->m_flag
|= MDELETED
|MTOUCH
;
526 mp
->m_flag
&= ~(MPRESERVE
|MSAVED
|MBOX
);
530 setdot(&message
[last
-1]);
531 last
= first(0, MDELETED
);
533 setdot(&message
[last
-1]);
543 * Following can't happen -- it keeps lint happy
550 * Undelete the indicated messages.
559 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
560 mp
= &message
[*ip
- 1];
563 if (mp
->m_flag
& (MDELETED
|MSAVED
))
564 mp
->m_flag
&= ~(MDELETED
|MSAVED
);
566 mp
->m_flag
&= ~MDELETED
;
568 if (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)
569 imap_undelete(mp
, *ip
);
576 * Add the given header fields to the retained list.
577 * If no arguments, print the current list of retained fields.
584 return ignore1(list
, ignore
+ 1, "retained");
588 * Add the given header fields to the ignored list.
589 * If no arguments, print the current list of ignored fields.
596 return ignore1(list
, ignore
, "ignored");
600 saveretfield(void *v
)
604 return ignore1(list
, saveignore
+ 1, "retained");
612 return ignore1(list
, saveignore
, "ignored");
620 return ignore1(list
, fwdignore
+ 1, "retained");
628 return ignore1(list
, fwdignore
, "ignored");
632 ignore1(char **list
, struct ignoretab
*tab
, char const *which
)
639 return igshow(tab
, which
);
640 for (ap
= list
; *ap
!= 0; ap
++) {
645 field
= ac_alloc(sz
+ 1);
646 i_strcpy(field
, *ap
, sz
+ 1);
648 if (member(field
, tab
)) {
653 igp
= (struct ignore
*)scalloc(1, sizeof (struct ignore
));
654 sz
= strlen(field
) + 1;
655 igp
->i_field
= smalloc(sz
);
656 memcpy(igp
->i_field
, field
, sz
);
657 igp
->i_link
= tab
->i_head
[h
];
658 tab
->i_head
[h
] = igp
;
666 * Print out all currently retained fields.
669 igshow(struct ignoretab
*tab
, char const *which
)
675 if (tab
->i_count
== 0) {
676 printf(tr(34, "No fields currently being %s.\n"), which
);
680 ring
= (char **)salloc((tab
->i_count
+ 1) * sizeof (char *));
682 for (h
= 0; h
< HSHSIZE
; h
++)
683 for (igp
= tab
->i_head
[h
]; igp
!= 0; igp
= igp
->i_link
)
684 *ap
++ = igp
->i_field
;
686 qsort(ring
, tab
->i_count
, sizeof (char *), igcomp
);
687 for (ap
= ring
; *ap
!= 0; ap
++)
693 * Compare two names for sorting ignored field list.
696 igcomp(const void *l
, const void *r
)
698 return (strcmp(*(char**)UNCONST(l
), *(char**)UNCONST(r
)));
704 return unignore1((char **)v
, ignore
, "ignored");
710 return unignore1((char **)v
, ignore
+ 1, "retained");
714 unsaveignore(void *v
)
716 return unignore1((char **)v
, saveignore
, "ignored");
720 unsaveretain(void *v
)
722 return unignore1((char **)v
, saveignore
+ 1, "retained");
728 return unignore1((char **)v
, fwdignore
, "ignored");
734 return unignore1((char **)v
, fwdignore
+ 1, "retained");
738 unignore_one(const char *name
, struct ignoretab
*tab
)
740 struct ignore
*ip
, *iq
= NULL
;
743 for (ip
= tab
->i_head
[h
]; ip
; ip
= ip
->i_link
) {
744 if (asccasecmp(ip
->i_field
, name
) == 0) {
747 iq
->i_link
= ip
->i_link
;
749 tab
->i_head
[h
] = ip
->i_link
;
759 unignore1(char **list
, struct ignoretab
*tab
, char const *which
)
761 if (tab
->i_count
== 0) {
762 printf(tr(34, "No fields currently being %s.\n"), which
);
766 unignore_one(*list
++, tab
);