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
= "", *shell
= NULL
;
82 bool_t success
= FAL0
, f
;
85 msgvec
= salloc((msgCount
+ 2) * sizeof *msgvec
);
87 for (cp
= str
; *cp
!= '\0' && spacechar(*cp
); ++cp
)
91 if ((file
= snarf(str
, &f
, convert
!= SEND_TOFILE
)) == NULL
)
93 while(spacechar(*file
))
97 shell
= ok_vlook(SHELL
);
102 *msgvec
= first(0, MMNORM
);
104 } else if (getmsglist(str
, msgvec
, 0) < 0)
107 if (pstate
& (PS_HOOK_MASK
| PS_ROBOT
)) {
111 printf(_("No messages to %s.\n"), cmd
);
116 if ((cp
= nameof(message
+ *msgvec
- 1, 0)) == NULL
) {
117 printf(_("Cannot determine message sender to %s.\n"), cmd
);
121 for (cq
= cp
; *cq
!= '\0' && *cq
!= '@'; cq
++)
124 if (ok_blook(outfolder
)) {
125 size_t sz
= strlen(cp
) +1;
126 file
= salloc(sz
+ 1);
128 memcpy(file
+ 1, cp
, sz
);
133 /* Pipe target is special TODO hacked in later, normalize flow! */
135 if ((obuf
= Popen(file
, "w", shell
, NULL
, 1)) == NULL
) {
145 if ((file
= expand(file
)) == NULL
)
147 if (access(file
, F_OK
) >= 0) {
149 disp
= _("[Appended]");
152 disp
= _("[New file]");
155 obuf
= ((convert
== SEND_TOFILE
) ? Fopen(file
, "a+") : Zopen(file
, "a+"));
157 obuf
= ((convert
== SEND_TOFILE
) ? Fopen(file
, "wx") : Zopen(file
, "wx"));
163 if (!newfile
&& !fstat(fileno(obuf
), &st
) && S_ISREG(st
.st_mode
) &&
164 fseek(obuf
, -2L, SEEK_END
) == 0) {
168 switch (fread(buf
, sizeof *buf
, 2, obuf
)) {
170 if (buf
[1] != '\n') {
197 tstats
[0] = tstats
[1] = 0;
200 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
202 mp
= message
+ *ip
- 1;
203 if (sendmp(mp
, obuf
, ignoret
, NULL
, convert
, mstats
) < 0) {
211 mp
->m_flag
|= MSAVED
;
213 mp
->m_flag
|= MDELETED
| MSAVED
;
217 tstats
[0] += mstats
[0];
218 tstats
[1] += mp
->m_lines
;/* TODO won't work, need target! v15!! */
231 if (!Pclose(obuf
, TRU1
))
233 } else if (Fclose(obuf
) != 0)
237 printf("\"%s\" %s %" /*PRIu64 "/%"*/ PRIu64
" bytes\n",
238 file
, disp
, /*tstats[1], TODO v15: lines written */ tstats
[0]);
243 newfile
= ~(MSAVED
| MDELETED
);
245 for (ip
= msgvec
; *ip
!= 0 &&
246 UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
) {
247 mp
= message
+ *ip
- 1;
248 mp
->m_flag
&= newfile
;
252 if (domove
&& last
&& success
) {
253 setdot(message
+ last
- 1);
254 last
= first(0, MDELETED
);
255 setdot(message
+ (last
!= 0 ? last
- 1 : 0));
259 return (success
== FAL0
);
263 snarf(char *linebuf
, bool_t
*flag
, bool_t usembox
)
268 if ((cp
= laststring(linebuf
, flag
, TRU1
)) == NULL
) {
273 n_err(_("No file specified\n"));
283 int rv
= -1, *ip
, last
;
287 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
288 mp
= message
+ *ip
- 1;
290 mp
->m_flag
|= MDELETED
| MTOUCH
;
291 mp
->m_flag
&= ~(MPRESERVE
| MSAVED
| MBOX
);
295 setdot(message
+ last
- 1);
296 last
= first(0, MDELETED
);
298 setdot(message
+ last
- 1);
309 ignore1(char **list
, struct ignoretab
*tab
, char const *which
)
317 h
= igshow(tab
, which
);
321 for (ap
= list
; *ap
!= 0; ++ap
) {
326 field
= ac_alloc(sz
);
327 i_strcpy(field
, *ap
, sz
);
328 if (member(field
, tab
))
332 igp
= scalloc(1, sizeof *igp
);
333 sz
= strlen(field
) +1;
334 igp
->i_field
= smalloc(sz
);
335 memcpy(igp
->i_field
, field
, sz
);
336 igp
->i_link
= tab
->i_head
[h
];
337 tab
->i_head
[h
] = igp
;
349 igshow(struct ignoretab
*tab
, char const *which
)
356 if (tab
->i_count
== 0) {
357 printf(_("No fields currently being %s.\n"), which
);
361 ring
= salloc((tab
->i_count
+ 1) * sizeof *ring
);
363 for (h
= 0; h
< HSHSIZE
; ++h
)
364 for (igp
= tab
->i_head
[h
]; igp
!= 0; igp
= igp
->i_link
)
365 *ap
++ = igp
->i_field
;
368 qsort(ring
, tab
->i_count
, sizeof *ring
, igcomp
);
370 for (ap
= ring
; *ap
!= NULL
; ++ap
)
378 igcomp(void const *l
, void const *r
)
383 rv
= strcmp(*(char**)UNCONST(l
), *(char**)UNCONST(r
));
389 _unignore(char **list
, struct ignoretab
*tab
, char const *which
)
394 if (tab
->i_count
== 0)
395 printf(_("No fields currently being %s.\n"), which
);
397 while ((field
= *list
++) != NULL
)
398 if (field
[0] == '*' && field
[1] == '\0') {
402 __unign_one(tab
, field
);
408 __unign_all(struct ignoretab
*tab
)
411 struct ignored
*n
, *x
;
414 for (i
= 0; i
< NELEM(tab
->i_head
); ++i
)
415 for (n
= tab
->i_head
[i
]; n
!= NULL
; n
= x
) {
420 memset(tab
, 0, sizeof *tab
);
425 __unign_one(struct ignoretab
*tab
, char const *name
)
427 struct ignored
*ip
, *iq
;
432 for (iq
= NULL
, ip
= tab
->i_head
[h
]; ip
!= NULL
; ip
= ip
->i_link
) {
433 if (!asccasecmp(ip
->i_field
, name
)) {
436 iq
->i_link
= ip
->i_link
;
438 tab
->i_head
[h
] = ip
->i_link
;
451 int list
[2], *ip
, *ip2
, mdot
, *msgvec
= v
, rv
= 1;
456 /* If some messages were supplied, find the first applicable one
457 * following dot using wrap around */
458 mdot
= (int)PTR2SIZE(dot
- message
+ 1);
460 /* Find first message in supplied message list which follows dot */
461 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
462 if ((mb
.mb_threaded
? message
[*ip
- 1].m_threadpos
> dot
->m_threadpos
470 mp
= message
+ *ip2
- 1;
471 if (!(mp
->m_flag
& MMNDEL
)) {
480 printf(_("No messages applicable\n"));
484 /* If this is the first command, select message 1. Note that this must
485 * exist for us to get here at all */
486 if (!(pstate
& PS_SAW_COMMAND
)) {
492 /* Just find the next good message after dot, no wraparound */
493 if (mb
.mb_threaded
== 0) {
494 for (mp
= dot
+ !!(pstate
& PS_DID_PRINT_DOT
);
495 PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
496 if (!(mp
->m_flag
& MMNORM
))
499 /* TODO The threading code had some bugs that caused crashes.
500 * TODO The last thing (before the deep look) happens here,
501 * TODO so let's not trust PS_DID_PRINT_DOT but check & hope it fixes */
502 if ((mp
= dot
) != NULL
&& (pstate
& PS_DID_PRINT_DOT
))
503 mp
= next_in_thread(mp
);
504 while (mp
!= NULL
&& (mp
->m_flag
& MMNORM
))
505 mp
= next_in_thread(mp
);
507 if (mp
== NULL
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
509 printf(_("At EOF\n"));
517 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
532 if (*(args
= v
) == '\0' || args
[1] != '\0') {
534 n_err(_("Synopsis: dotmove: up <-> or down <+> by one message\n"));
536 } else switch (args
[0]) {
540 printf(_("At EOF\n"));
542 } else if (getmsglist(UNCONST(/*TODO*/ args
), msgvec
, 0) > 0) {
543 setdot(message
+ msgvec
[0] - 1);
545 rv
= c_headers(msgvec
);
563 rv
= save1(str
, 1, "save", saveignore
, SEND_MBOX
, 0, 0);
575 rv
= save1(str
, 1, "save", saveignore
, SEND_MBOX
, 1, 0);
587 rv
= save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 0, 0);
599 rv
= save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 1, 0);
611 rv
= save1(str
, 0, "move", saveignore
, SEND_MBOX
, 0, 1);
623 rv
= save1(str
, 0, "move", saveignore
, SEND_MBOX
, 1, 1);
635 rv
= save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 0, 0);
647 rv
= save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 1, 0);
659 if (str
== NULL
|| *str
== '\0')
660 str
= savestr("/dev/null");
661 rv
= save1(str
, 0, "write", allignore
, SEND_TOFILE
, 0, 0);
680 int list
[2], rv
= 0, *msgvec
= v
, lastdot
;
683 lastdot
= (int)PTR2SIZE(dot
- message
+ 1);
684 if (delm(msgvec
) >= 0) {
685 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
686 if (list
[0] > lastdot
) {
692 printf(_("At EOF\n"));
694 printf(_("No more messages\n"));
703 int *msgvec
= v
, *ip
;
707 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
709 mp
= message
+ *ip
- 1;
712 if (mp
->m_flag
& (MDELETED
| MSAVED
))
713 mp
->m_flag
&= ~(MDELETED
| MSAVED
);
715 mp
->m_flag
&= ~MDELETED
;
728 rv
= ignore1(list
, ignore
+ 1, "retained");
740 rv
= ignore1(list
, ignore
, "ignored");
746 c_saveretfield(void *v
)
752 rv
= ignore1(list
, saveignore
+ 1, "retained");
758 c_saveigfield(void *v
)
764 rv
= ignore1(list
, saveignore
, "ignored");
770 c_fwdretfield(void *v
)
776 rv
= ignore1(list
, fwdignore
+ 1, "retained");
782 c_fwdigfield(void *v
)
788 rv
= ignore1(list
, fwdignore
, "ignored");
799 rv
= _unignore((char**)v
, ignore
, "ignored");
810 rv
= _unignore((char**)v
, ignore
+ 1, "retained");
816 c_unsaveignore(void *v
)
821 rv
= _unignore((char**)v
, saveignore
, "ignored");
827 c_unsaveretain(void *v
)
832 rv
= _unignore((char**)v
, saveignore
+ 1, "retained");
838 c_unfwdignore(void *v
)
843 rv
= _unignore((char**)v
, fwdignore
, "ignored");
849 c_unfwdretain(void *v
)
854 rv
= _unignore((char**)v
, fwdignore
+ 1, "retained");