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 if ((shell
= ok_vlook(SHELL
)) == NULL
)
103 *msgvec
= first(0, MMNORM
);
105 } else if (getmsglist(str
, msgvec
, 0) < 0)
108 if (pstate
& PS_HOOK_MASK
) {
112 printf(_("No messages to %s.\n"), cmd
);
117 if ((cp
= nameof(message
+ *msgvec
- 1, 0)) == NULL
) {
118 printf(_("Cannot determine message sender to %s.\n"), cmd
);
122 for (cq
= cp
; *cq
!= '\0' && *cq
!= '@'; cq
++)
125 if (ok_blook(outfolder
)) {
126 size_t sz
= strlen(cp
) +1;
127 file
= salloc(sz
+ 1);
129 memcpy(file
+ 1, cp
, sz
);
134 /* Pipe target is special TODO hacked in later, normalize flow! */
136 if ((obuf
= Popen(file
, "w", shell
, NULL
, 1)) == NULL
) {
146 if ((file
= expand(file
)) == NULL
)
148 if (access(file
, F_OK
) >= 0) {
150 disp
= _("[Appended]");
153 disp
= _("[New file]");
156 obuf
= ((convert
== SEND_TOFILE
) ? Fopen(file
, "a+") : Zopen(file
, "a+"));
158 obuf
= ((convert
== SEND_TOFILE
) ? Fopen(file
, "wx") : Zopen(file
, "wx"));
164 if (!newfile
&& !fstat(fileno(obuf
), &st
) && S_ISREG(st
.st_mode
) &&
165 fseek(obuf
, -2L, SEEK_END
) == 0) {
169 switch (fread(buf
, sizeof *buf
, 2, obuf
)) {
171 if (buf
[1] != '\n') {
198 tstats
[0] = tstats
[1] = 0;
201 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
203 mp
= message
+ *ip
- 1;
204 if (sendmp(mp
, obuf
, ignoret
, NULL
, convert
, mstats
) < 0) {
212 mp
->m_flag
|= MSAVED
;
214 mp
->m_flag
|= MDELETED
| MSAVED
;
218 tstats
[0] += mstats
[0];
219 tstats
[1] += mp
->m_lines
;/* TODO won't work, need target! v15!! */
232 if (!Pclose(obuf
, TRU1
))
234 } else if (Fclose(obuf
) != 0)
238 printf("\"%s\" %s %" /*PRIu64 "/%"*/ PRIu64
" bytes\n",
239 file
, disp
, /*tstats[1], TODO v15: lines written */ tstats
[0]);
244 newfile
= ~(MSAVED
| MDELETED
);
246 for (ip
= msgvec
; *ip
!= 0 &&
247 UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
) {
248 mp
= message
+ *ip
- 1;
249 mp
->m_flag
&= newfile
;
253 if (domove
&& last
&& success
) {
254 setdot(message
+ last
- 1);
255 last
= first(0, MDELETED
);
256 setdot(message
+ (last
!= 0 ? last
- 1 : 0));
260 return (success
== FAL0
);
264 snarf(char *linebuf
, bool_t
*flag
, bool_t usembox
)
269 if ((cp
= laststring(linebuf
, flag
, TRU1
)) == NULL
) {
274 n_err(_("No file specified\n"));
284 int rv
= -1, *ip
, last
;
288 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
289 mp
= message
+ *ip
- 1;
291 mp
->m_flag
|= MDELETED
| MTOUCH
;
292 mp
->m_flag
&= ~(MPRESERVE
| MSAVED
| MBOX
);
296 setdot(message
+ last
- 1);
297 last
= first(0, MDELETED
);
299 setdot(message
+ last
- 1);
310 ignore1(char **list
, struct ignoretab
*tab
, char const *which
)
318 h
= igshow(tab
, which
);
322 for (ap
= list
; *ap
!= 0; ++ap
) {
327 field
= ac_alloc(sz
);
328 i_strcpy(field
, *ap
, sz
);
329 if (member(field
, tab
))
333 igp
= scalloc(1, sizeof *igp
);
334 sz
= strlen(field
) +1;
335 igp
->i_field
= smalloc(sz
);
336 memcpy(igp
->i_field
, field
, sz
);
337 igp
->i_link
= tab
->i_head
[h
];
338 tab
->i_head
[h
] = igp
;
350 igshow(struct ignoretab
*tab
, char const *which
)
357 if (tab
->i_count
== 0) {
358 printf(_("No fields currently being %s.\n"), which
);
362 ring
= salloc((tab
->i_count
+ 1) * sizeof *ring
);
364 for (h
= 0; h
< HSHSIZE
; ++h
)
365 for (igp
= tab
->i_head
[h
]; igp
!= 0; igp
= igp
->i_link
)
366 *ap
++ = igp
->i_field
;
369 qsort(ring
, tab
->i_count
, sizeof *ring
, igcomp
);
371 for (ap
= ring
; *ap
!= NULL
; ++ap
)
379 igcomp(void const *l
, void const *r
)
384 rv
= strcmp(*(char**)UNCONST(l
), *(char**)UNCONST(r
));
390 _unignore(char **list
, struct ignoretab
*tab
, char const *which
)
395 if (tab
->i_count
== 0)
396 printf(_("No fields currently being %s.\n"), which
);
398 while ((field
= *list
++) != NULL
)
399 if (field
[0] == '*' && field
[1] == '\0') {
403 __unign_one(tab
, field
);
409 __unign_all(struct ignoretab
*tab
)
412 struct ignored
*n
, *x
;
415 for (i
= 0; i
< NELEM(tab
->i_head
); ++i
)
416 for (n
= tab
->i_head
[i
]; n
!= NULL
; n
= x
) {
421 memset(tab
, 0, sizeof *tab
);
426 __unign_one(struct ignoretab
*tab
, char const *name
)
428 struct ignored
*ip
, *iq
;
433 for (iq
= NULL
, ip
= tab
->i_head
[h
]; ip
!= NULL
; ip
= ip
->i_link
) {
434 if (!asccasecmp(ip
->i_field
, name
)) {
437 iq
->i_link
= ip
->i_link
;
439 tab
->i_head
[h
] = ip
->i_link
;
452 int list
[2], *ip
, *ip2
, mdot
, *msgvec
= v
, rv
= 1;
457 /* If some messages were supplied, find the first applicable one
458 * following dot using wrap around */
459 mdot
= (int)PTR2SIZE(dot
- message
+ 1);
461 /* Find first message in supplied message list which follows dot */
462 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
463 if ((mb
.mb_threaded
? message
[*ip
- 1].m_threadpos
> dot
->m_threadpos
471 mp
= message
+ *ip2
- 1;
472 if (!(mp
->m_flag
& MMNDEL
)) {
481 printf(_("No messages applicable\n"));
485 /* If this is the first command, select message 1. Note that this must
486 * exist for us to get here at all */
487 if (!(pstate
& PS_SAW_COMMAND
)) {
493 /* Just find the next good message after dot, no wraparound */
494 if (mb
.mb_threaded
== 0) {
495 for (mp
= dot
+ !!(pstate
& PS_DID_PRINT_DOT
);
496 PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
497 if (!(mp
->m_flag
& MMNORM
))
500 /* TODO The threading code had some bugs that caused crashes.
501 * TODO The last thing (before the deep look) happens here,
502 * TODO so let's not trust PS_DID_PRINT_DOT but check & hope it fixes */
503 if ((mp
= dot
) != NULL
&& (pstate
& PS_DID_PRINT_DOT
))
504 mp
= next_in_thread(mp
);
505 while (mp
!= NULL
&& (mp
->m_flag
& MMNORM
))
506 mp
= next_in_thread(mp
);
508 if (mp
== NULL
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
510 printf(_("At EOF\n"));
518 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
533 if (*(args
= v
) == '\0' || args
[1] != '\0') {
535 n_err(_("Synopsis: dotmove: up <-> or down <+> by one message\n"));
537 } else switch (args
[0]) {
541 printf(_("At EOF\n"));
543 } else if (getmsglist(UNCONST(/*TODO*/ args
), msgvec
, 0) > 0) {
544 setdot(message
+ msgvec
[0] - 1);
546 rv
= c_headers(msgvec
);
564 rv
= save1(str
, 1, "save", saveignore
, SEND_MBOX
, 0, 0);
576 rv
= save1(str
, 1, "save", saveignore
, SEND_MBOX
, 1, 0);
588 rv
= save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 0, 0);
600 rv
= save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 1, 0);
612 rv
= save1(str
, 0, "move", saveignore
, SEND_MBOX
, 0, 1);
624 rv
= save1(str
, 0, "move", saveignore
, SEND_MBOX
, 1, 1);
636 rv
= save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 0, 0);
648 rv
= save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 1, 0);
660 if (str
== NULL
|| *str
== '\0')
661 str
= savestr("/dev/null");
662 rv
= save1(str
, 0, "write", allignore
, SEND_TOFILE
, 0, 0);
681 int list
[2], rv
= 0, *msgvec
= v
, lastdot
;
684 lastdot
= (int)PTR2SIZE(dot
- message
+ 1);
685 if (delm(msgvec
) >= 0) {
686 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
687 if (list
[0] > lastdot
) {
693 printf(_("At EOF\n"));
695 printf(_("No more messages\n"));
704 int *msgvec
= v
, *ip
;
708 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
710 mp
= message
+ *ip
- 1;
713 if (mp
->m_flag
& (MDELETED
| MSAVED
))
714 mp
->m_flag
&= ~(MDELETED
| MSAVED
);
716 mp
->m_flag
&= ~MDELETED
;
729 rv
= ignore1(list
, ignore
+ 1, "retained");
741 rv
= ignore1(list
, ignore
, "ignored");
747 c_saveretfield(void *v
)
753 rv
= ignore1(list
, saveignore
+ 1, "retained");
759 c_saveigfield(void *v
)
765 rv
= ignore1(list
, saveignore
, "ignored");
771 c_fwdretfield(void *v
)
777 rv
= ignore1(list
, fwdignore
+ 1, "retained");
783 c_fwdigfield(void *v
)
789 rv
= ignore1(list
, fwdignore
, "ignored");
800 rv
= _unignore((char**)v
, ignore
, "ignored");
811 rv
= _unignore((char**)v
, ignore
+ 1, "retained");
817 c_unsaveignore(void *v
)
822 rv
= _unignore((char**)v
, saveignore
, "ignored");
828 c_unsaveretain(void *v
)
833 rv
= _unignore((char**)v
, saveignore
+ 1, "retained");
839 c_unfwdignore(void *v
)
844 rv
= _unignore((char**)v
, fwdignore
, "ignored");
850 c_unfwdretain(void *v
)
855 rv
= _unignore((char**)v
, fwdignore
+ 1, "retained");