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 - 2015 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifndef HAVE_AMALGAMATION
42 /* Save/copy the indicated messages at the end of the passed file name.
43 * If mark is true, mark the message "saved" */
44 static int save1(char *str
, int domark
, char const *cmd
,
45 struct ignoretab
*ignoret
, int convert
, int sender_record
,
48 /* Snarf the file from the end of the command line and return a pointer to it.
49 * If there is no file attached, return the mbox file. Put a null in front of
50 * the file name so that the message list processing won't see it, unless the
51 * file name is the only thing on the line, in which case, return 0 in the
52 * reference flag variable */
53 static char * snarf(char *linebuf
, bool_t
*flag
, bool_t usembox
);
55 /* Delete the indicated messages. Set dot to some nice place afterwards */
56 static int delm(int *msgvec
);
58 static int ignore1(char **list
, struct ignoretab
*tab
, char const *which
);
60 /* Print out all currently retained fields */
61 static int igshow(struct ignoretab
*tab
, char const *which
);
63 /* Compare two names for sorting ignored field list */
64 static int igcomp(void const *l
, void const *r
);
67 static int _unignore(char **list
, struct ignoretab
*tab
, char const *which
);
68 static void __unign_all(struct ignoretab
*tab
);
69 static void __unign_one(struct ignoretab
*tab
, char const *name
);
72 save1(char *str
, int domark
, char const *cmd
, struct ignoretab
*ignoret
,
73 int convert
, int sender_record
, int domove
)
75 ui64_t mstats
[1], tstats
[2];
77 int newfile
= 0, last
= 0, *msgvec
, *ip
;
79 char *file
= NULL
, *cp
, *cq
;
80 char const *disp
= "";
83 bool_t success
= FAL0
, f
;
86 msgvec
= salloc((msgCount
+ 2) * sizeof *msgvec
);
88 for (cp
= str
; *cp
!= '\0' && blankchar(*cp
); ++cp
)
92 if ((file
= snarf(str
, &f
, convert
!= SEND_TOFILE
)) == NULL
)
97 *msgvec
= first(0, MMNORM
);
99 } else if (getmsglist(str
, msgvec
, 0) < 0)
102 if (pstate
& PS_HOOK_MASK
) {
106 printf(_("No messages to %s.\n"), cmd
);
111 if ((cp
= nameof(message
+ *msgvec
- 1, 0)) == NULL
) {
112 printf(_("Cannot determine message sender to %s.\n"), cmd
);
116 for (cq
= cp
; *cq
!= '\0' && *cq
!= '@'; cq
++)
119 if (ok_blook(outfolder
)) {
120 size_t sz
= strlen(cp
) +1;
121 file
= salloc(sz
+ 1);
123 memcpy(file
+ 1, cp
, sz
);
128 if ((file
= expand(file
)) == NULL
)
130 prot
= which_protocol(file
);
131 if (prot
!= PROTO_IMAP
) {
132 if (access(file
, F_OK
) >= 0) {
134 disp
= _("[Appended]");
137 disp
= _("[New file]");
141 obuf
= ((convert
== SEND_TOFILE
) ? Fopen(file
, "a+") : Zopen(file
, "a+"));
143 obuf
= ((convert
== SEND_TOFILE
) ? Fopen(file
, "wx") : Zopen(file
, "wx"));
149 if (!newfile
&& !fstat(fileno(obuf
), &st
) && S_ISREG(st
.st_mode
) &&
150 fseek(obuf
, -2L, SEEK_END
) == 0) {
154 switch (fread(buf
, sizeof *buf
, 2, obuf
)) {
156 if (buf
[1] != '\n') {
182 tstats
[0] = tstats
[1] = 0;
183 imap_created_mailbox
= 0;
186 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
188 mp
= message
+ *ip
- 1;
189 if (prot
== PROTO_IMAP
&& ignoret
[0].i_count
== 0 &&
190 ignoret
[1].i_count
== 0
191 #ifdef HAVE_IMAP /* TODO revisit */
192 && imap_thisaccount(file
)
196 if (imap_copy(mp
, *ip
, file
) == STOP
)
202 # elif defined EOPNOTSUPP
212 mstats
[0] = mp
->m_xsize
;
214 } else if (sendmp(mp
, obuf
, ignoret
, NULL
, convert
, mstats
) < 0) {
222 mp
->m_flag
|= MSAVED
;
224 mp
->m_flag
|= MDELETED
| MSAVED
;
228 tstats
[0] += mstats
[0];
229 tstats
[1] += mp
->m_lines
;/* TODO won't work, need target! v15!! */
241 if (Fclose(obuf
) != 0)
245 if (prot
== PROTO_IMAP
|| prot
== PROTO_MAILDIR
) {
248 ((prot
== PROTO_IMAP
) && disconnected(file
)) ? "[Queued]" :
250 (imap_created_mailbox
? "[New file]" : "[Appended]"));
252 printf("\"%s\" %s %" /*PRIu64 "/%"*/ PRIu64
" bytes\n",
253 file
, disp
, /*tstats[1], TODO v15: lines written */ tstats
[0]);
258 newfile
= ~(MSAVED
| MDELETED
);
260 for (ip
= msgvec
; *ip
!= 0 &&
261 UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
) {
262 mp
= message
+ *ip
- 1;
263 mp
->m_flag
&= newfile
;
267 if (domove
&& last
&& success
) {
268 setdot(message
+ last
- 1);
269 last
= first(0, MDELETED
);
270 setdot(message
+ (last
!= 0 ? last
- 1 : 0));
274 return (success
== FAL0
);
278 snarf(char *linebuf
, bool_t
*flag
, bool_t usembox
)
283 if ((cp
= laststring(linebuf
, flag
, FAL0
)) == NULL
) {
288 n_err(_("No file specified\n"));
298 int rv
= -1, *ip
, last
;
302 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
303 mp
= message
+ *ip
- 1;
305 mp
->m_flag
|= MDELETED
| MTOUCH
;
306 mp
->m_flag
&= ~(MPRESERVE
| MSAVED
| MBOX
);
310 setdot(message
+ last
- 1);
311 last
= first(0, MDELETED
);
313 setdot(message
+ last
- 1);
324 ignore1(char **list
, struct ignoretab
*tab
, char const *which
)
332 h
= igshow(tab
, which
);
336 for (ap
= list
; *ap
!= 0; ++ap
) {
341 field
= ac_alloc(sz
);
342 i_strcpy(field
, *ap
, sz
);
343 if (member(field
, tab
))
347 igp
= scalloc(1, sizeof *igp
);
348 sz
= strlen(field
) +1;
349 igp
->i_field
= smalloc(sz
);
350 memcpy(igp
->i_field
, field
, sz
);
351 igp
->i_link
= tab
->i_head
[h
];
352 tab
->i_head
[h
] = igp
;
364 igshow(struct ignoretab
*tab
, char const *which
)
371 if (tab
->i_count
== 0) {
372 printf(_("No fields currently being %s.\n"), which
);
376 ring
= salloc((tab
->i_count
+ 1) * sizeof *ring
);
378 for (h
= 0; h
< HSHSIZE
; ++h
)
379 for (igp
= tab
->i_head
[h
]; igp
!= 0; igp
= igp
->i_link
)
380 *ap
++ = igp
->i_field
;
383 qsort(ring
, tab
->i_count
, sizeof *ring
, igcomp
);
385 for (ap
= ring
; *ap
!= NULL
; ++ap
)
393 igcomp(void const *l
, void const *r
)
398 rv
= strcmp(*(char**)UNCONST(l
), *(char**)UNCONST(r
));
404 _unignore(char **list
, struct ignoretab
*tab
, char const *which
)
409 if (tab
->i_count
== 0)
410 printf(_("No fields currently being %s.\n"), which
);
412 while ((field
= *list
++) != NULL
)
413 if (field
[0] == '*' && field
[1] == '\0') {
417 __unign_one(tab
, field
);
423 __unign_all(struct ignoretab
*tab
)
426 struct ignored
*n
, *x
;
429 for (i
= 0; i
< NELEM(tab
->i_head
); ++i
)
430 for (n
= tab
->i_head
[i
]; n
!= NULL
; n
= x
) {
435 memset(tab
, 0, sizeof *tab
);
440 __unign_one(struct ignoretab
*tab
, char const *name
)
442 struct ignored
*ip
, *iq
;
447 for (iq
= NULL
, ip
= tab
->i_head
[h
]; ip
!= NULL
; ip
= ip
->i_link
) {
448 if (!asccasecmp(ip
->i_field
, name
)) {
451 iq
->i_link
= ip
->i_link
;
453 tab
->i_head
[h
] = ip
->i_link
;
466 int list
[2], *ip
, *ip2
, mdot
, *msgvec
= v
, rv
= 1;
471 /* If some messages were supplied, find the first applicable one
472 * following dot using wrap around */
473 mdot
= (int)PTR2SIZE(dot
- message
+ 1);
475 /* Find first message in supplied message list which follows dot */
476 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
477 if ((mb
.mb_threaded
? message
[*ip
- 1].m_threadpos
> dot
->m_threadpos
485 mp
= message
+ *ip2
- 1;
486 if (!(mp
->m_flag
& MMNDEL
)) {
495 printf(_("No messages applicable\n"));
499 /* If this is the first command, select message 1. Note that this must
500 * exist for us to get here at all */
501 if (!(pstate
& PS_SAW_COMMAND
)) {
507 /* Just find the next good message after dot, no wraparound */
508 if (mb
.mb_threaded
== 0) {
509 for (mp
= dot
+ !!(pstate
& PS_DID_PRINT_DOT
);
510 PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
511 if (!(mp
->m_flag
& MMNORM
))
514 /* TODO The threading code had some bugs that caused crashes.
515 * TODO The last thing (before the deep look) happens here,
516 * TODO so let's not trust PS_DID_PRINT_DOT but check & hope it fixes */
517 if ((mp
= dot
) != NULL
&& (pstate
& PS_DID_PRINT_DOT
))
518 mp
= next_in_thread(mp
);
519 while (mp
!= NULL
&& (mp
->m_flag
& MMNORM
))
520 mp
= next_in_thread(mp
);
522 if (mp
== NULL
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
524 printf(_("At EOF\n"));
532 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
547 rv
= save1(str
, 1, "save", saveignore
, SEND_MBOX
, 0, 0);
559 rv
= save1(str
, 1, "save", saveignore
, SEND_MBOX
, 1, 0);
571 rv
= save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 0, 0);
583 rv
= save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 1, 0);
595 rv
= save1(str
, 0, "move", saveignore
, SEND_MBOX
, 0, 1);
607 rv
= save1(str
, 0, "move", saveignore
, SEND_MBOX
, 1, 1);
619 rv
= save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 0, 0);
631 rv
= save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 1, 0);
643 if (str
== NULL
|| *str
== '\0')
644 str
= savestr("/dev/null");
645 rv
= save1(str
, 0, "write", allignore
, SEND_TOFILE
, 0, 0);
664 int list
[2], rv
= 0, *msgvec
= v
, lastdot
;
667 lastdot
= (int)PTR2SIZE(dot
- message
+ 1);
668 if (delm(msgvec
) >= 0) {
669 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
670 if (list
[0] > lastdot
) {
676 printf(_("At EOF\n"));
678 printf(_("No more messages\n"));
687 int *msgvec
= v
, *ip
;
691 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
693 mp
= message
+ *ip
- 1;
696 if (mp
->m_flag
& (MDELETED
| MSAVED
))
697 mp
->m_flag
&= ~(MDELETED
| MSAVED
);
699 mp
->m_flag
&= ~MDELETED
;
701 if (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)
702 imap_undelete(mp
, *ip
);
716 rv
= ignore1(list
, ignore
+ 1, "retained");
728 rv
= ignore1(list
, ignore
, "ignored");
734 c_saveretfield(void *v
)
740 rv
= ignore1(list
, saveignore
+ 1, "retained");
746 c_saveigfield(void *v
)
752 rv
= ignore1(list
, saveignore
, "ignored");
758 c_fwdretfield(void *v
)
764 rv
= ignore1(list
, fwdignore
+ 1, "retained");
770 c_fwdigfield(void *v
)
776 rv
= ignore1(list
, fwdignore
, "ignored");
787 rv
= _unignore((char**)v
, ignore
, "ignored");
798 rv
= _unignore((char**)v
, ignore
+ 1, "retained");
804 c_unsaveignore(void *v
)
809 rv
= _unignore((char**)v
, saveignore
, "ignored");
815 c_unsaveretain(void *v
)
820 rv
= _unignore((char**)v
, saveignore
+ 1, "retained");
826 c_unfwdignore(void *v
)
831 rv
= _unignore((char**)v
, fwdignore
, "ignored");
837 c_unfwdretain(void *v
)
842 rv
= _unignore((char**)v
, fwdignore
+ 1, "retained");