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 last
= 0, *msgvec
, *ip
;
79 char *file
= NULL
, *cp
, *cq
;
80 char const *disp
= "", *shell
= NULL
;
82 bool_t success
= FAL0
, isflag
;
85 msgvec
= salloc((msgCount
+ 2) * sizeof *msgvec
);
87 for (cp
= str
; *cp
!= '\0' && spacechar(*cp
); ++cp
)
89 isflag
= (*cp
!= '\0');
91 if ((file
= snarf(str
, &isflag
, 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
) {
147 if ((file
= expand(file
)) == NULL
)
150 obuf
= ((convert
== SEND_TOFILE
) ? Fopen(file
, "a+") : Zopen(file
, "a+"));
152 obuf
= ((convert
== SEND_TOFILE
) ? Fopen(file
, "wx") : Zopen(file
, "wx"));
158 disp
= _("[New file]");
161 disp
= _("[Appended]");
164 /* TODO RETURN check, but be aware of protocols: v15: Mailbox->lock()! */
165 n_file_lock(fileno(obuf
), FLT_WRITE
, 0,0, UIZ_MAX
);
167 if (!isflag
&& !fstat(fileno(obuf
), &st
) && S_ISREG(st
.st_mode
) &&
168 fseek(obuf
, -2L, SEEK_END
) == 0) {
172 switch (fread(buf
, sizeof *buf
, 2, obuf
)) {
174 if (buf
[1] != '\n') {
200 tstats
[0] = tstats
[1] = 0;
203 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
205 mp
= message
+ *ip
- 1;
206 if (sendmp(mp
, obuf
, ignoret
, NULL
, convert
, mstats
) < 0) {
214 mp
->m_flag
|= MSAVED
;
216 mp
->m_flag
|= MDELETED
| MSAVED
;
220 tstats
[0] += mstats
[0];
221 tstats
[1] += mp
->m_lines
;/* TODO won't work, need target! v15!! */
234 if (!Pclose(obuf
, TRU1
))
236 } else if (Fclose(obuf
) != 0)
240 printf("\"%s\" %s %" /*PRIu64 "/%"*/ PRIu64
" bytes\n",
241 file
, disp
, /*tstats[1], TODO v15: lines written */ tstats
[0]);
243 for (ip
= msgvec
; *ip
!= 0 &&
244 UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
) {
245 mp
= message
+ *ip
- 1;
246 mp
->m_flag
&= ~MSAVED
;
249 for (ip
= msgvec
; *ip
!= 0 &&
250 UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
) {
251 mp
= message
+ *ip
- 1;
252 mp
->m_flag
&= ~(MSAVED
| MDELETED
);
256 if (domove
&& last
&& success
) {
257 setdot(message
+ last
- 1);
258 last
= first(0, MDELETED
);
259 setdot(message
+ (last
!= 0 ? last
- 1 : 0));
263 return (success
== FAL0
);
267 snarf(char *linebuf
, bool_t
*flag
, bool_t usembox
)
272 if ((cp
= laststring(linebuf
, flag
, TRU1
)) == NULL
) {
277 n_err(_("No file specified\n"));
287 int rv
= -1, *ip
, last
;
291 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
292 mp
= message
+ *ip
- 1;
294 mp
->m_flag
|= MDELETED
| MTOUCH
;
295 mp
->m_flag
&= ~(MPRESERVE
| MSAVED
| MBOX
);
299 setdot(message
+ last
- 1);
300 last
= first(0, MDELETED
);
302 setdot(message
+ last
- 1);
313 ignore1(char **list
, struct ignoretab
*tab
, char const *which
)
321 h
= igshow(tab
, which
);
325 for (ap
= list
; *ap
!= 0; ++ap
) {
330 field
= ac_alloc(sz
);
331 i_strcpy(field
, *ap
, sz
);
332 if (member(field
, tab
))
336 igp
= scalloc(1, sizeof *igp
);
337 sz
= strlen(field
) +1;
338 igp
->i_field
= smalloc(sz
);
339 memcpy(igp
->i_field
, field
, sz
);
340 igp
->i_link
= tab
->i_head
[h
];
341 tab
->i_head
[h
] = igp
;
353 igshow(struct ignoretab
*tab
, char const *which
)
360 if (tab
->i_count
== 0) {
361 printf(_("No fields currently being %s.\n"), which
);
365 ring
= salloc((tab
->i_count
+ 1) * sizeof *ring
);
367 for (h
= 0; h
< HSHSIZE
; ++h
)
368 for (igp
= tab
->i_head
[h
]; igp
!= 0; igp
= igp
->i_link
)
369 *ap
++ = igp
->i_field
;
372 qsort(ring
, tab
->i_count
, sizeof *ring
, igcomp
);
374 for (ap
= ring
; *ap
!= NULL
; ++ap
)
382 igcomp(void const *l
, void const *r
)
387 rv
= strcmp(*(char**)UNCONST(l
), *(char**)UNCONST(r
));
393 _unignore(char **list
, struct ignoretab
*tab
, char const *which
)
398 if (tab
->i_count
== 0)
399 printf(_("No fields currently being %s.\n"), which
);
401 while ((field
= *list
++) != NULL
)
402 if (field
[0] == '*' && field
[1] == '\0') {
406 __unign_one(tab
, field
);
412 __unign_all(struct ignoretab
*tab
)
415 struct ignored
*n
, *x
;
418 for (i
= 0; i
< NELEM(tab
->i_head
); ++i
)
419 for (n
= tab
->i_head
[i
]; n
!= NULL
; n
= x
) {
424 memset(tab
, 0, sizeof *tab
);
429 __unign_one(struct ignoretab
*tab
, char const *name
)
431 struct ignored
*ip
, *iq
;
436 for (iq
= NULL
, ip
= tab
->i_head
[h
]; ip
!= NULL
; ip
= ip
->i_link
) {
437 if (!asccasecmp(ip
->i_field
, name
)) {
440 iq
->i_link
= ip
->i_link
;
442 tab
->i_head
[h
] = ip
->i_link
;
455 int list
[2], *ip
, *ip2
, mdot
, *msgvec
= v
, rv
= 1;
460 /* If some messages were supplied, find the first applicable one
461 * following dot using wrap around */
462 mdot
= (int)PTR2SIZE(dot
- message
+ 1);
464 /* Find first message in supplied message list which follows dot */
465 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
466 if ((mb
.mb_threaded
? message
[*ip
- 1].m_threadpos
> dot
->m_threadpos
474 mp
= message
+ *ip2
- 1;
475 if (!(mp
->m_flag
& MMNDEL
)) {
484 printf(_("No messages applicable\n"));
488 /* If this is the first command, select message 1. Note that this must
489 * exist for us to get here at all */
490 if (!(pstate
& PS_SAW_COMMAND
)) {
496 /* Just find the next good message after dot, no wraparound */
497 if (mb
.mb_threaded
== 0) {
498 for (mp
= dot
+ !!(pstate
& PS_DID_PRINT_DOT
);
499 PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
500 if (!(mp
->m_flag
& MMNORM
))
503 /* TODO The threading code had some bugs that caused crashes.
504 * TODO The last thing (before the deep look) happens here,
505 * TODO so let's not trust PS_DID_PRINT_DOT but check & hope it fixes */
506 if ((mp
= dot
) != NULL
&& (pstate
& PS_DID_PRINT_DOT
))
507 mp
= next_in_thread(mp
);
508 while (mp
!= NULL
&& (mp
->m_flag
& MMNORM
))
509 mp
= next_in_thread(mp
);
511 if (mp
== NULL
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
513 printf(_("At EOF\n"));
521 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
536 if (*(args
= v
) == '\0' || args
[1] != '\0') {
538 n_err(_("Synopsis: dotmove: up <-> or down <+> by one message\n"));
540 } else switch (args
[0]) {
544 printf(_("At EOF\n"));
546 } else if (getmsglist(UNCONST(/*TODO*/ args
), msgvec
, 0) > 0) {
547 setdot(message
+ msgvec
[0] - 1);
549 rv
= c_headers(msgvec
);
567 rv
= save1(str
, 1, "save", saveignore
, SEND_MBOX
, 0, 0);
579 rv
= save1(str
, 1, "save", saveignore
, SEND_MBOX
, 1, 0);
591 rv
= save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 0, 0);
603 rv
= save1(str
, 0, "copy", saveignore
, SEND_MBOX
, 1, 0);
615 rv
= save1(str
, 0, "move", saveignore
, SEND_MBOX
, 0, 1);
627 rv
= save1(str
, 0, "move", saveignore
, SEND_MBOX
, 1, 1);
639 rv
= save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 0, 0);
651 rv
= save1(str
, 0, "decrypt", saveignore
, SEND_DECRYPT
, 1, 0);
663 if (str
== NULL
|| *str
== '\0')
664 str
= savestr("/dev/null");
665 rv
= save1(str
, 0, "write", allignore
, SEND_TOFILE
, 0, 0);
684 int list
[2], rv
= 0, *msgvec
= v
, lastdot
;
687 lastdot
= (int)PTR2SIZE(dot
- message
+ 1);
688 if (delm(msgvec
) >= 0) {
689 list
[0] = (int)PTR2SIZE(dot
- message
+ 1);
690 if (list
[0] > lastdot
) {
696 printf(_("At EOF\n"));
698 printf(_("No more messages\n"));
707 int *msgvec
= v
, *ip
;
711 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
713 mp
= message
+ *ip
- 1;
716 if (mp
->m_flag
& (MDELETED
| MSAVED
))
717 mp
->m_flag
&= ~(MDELETED
| MSAVED
);
719 mp
->m_flag
&= ~MDELETED
;
732 rv
= ignore1(list
, ignore
+ 1, "retained");
744 rv
= ignore1(list
, ignore
, "ignored");
750 c_saveretfield(void *v
)
756 rv
= ignore1(list
, saveignore
+ 1, "retained");
762 c_saveigfield(void *v
)
768 rv
= ignore1(list
, saveignore
, "ignored");
774 c_fwdretfield(void *v
)
780 rv
= ignore1(list
, fwdignore
+ 1, "retained");
786 c_fwdigfield(void *v
)
792 rv
= ignore1(list
, fwdignore
, "ignored");
803 rv
= _unignore((char**)v
, ignore
, "ignored");
814 rv
= _unignore((char**)v
, ignore
+ 1, "retained");
820 c_unsaveignore(void *v
)
825 rv
= _unignore((char**)v
, saveignore
, "ignored");
831 c_unsaveretain(void *v
)
836 rv
= _unignore((char**)v
, saveignore
+ 1, "retained");
842 c_unfwdignore(void *v
)
847 rv
= _unignore((char**)v
, fwdignore
, "ignored");
853 c_unfwdretain(void *v
)
858 rv
= _unignore((char**)v
, fwdignore
+ 1, "retained");