1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ User commands which save, copy, write (parts of) messages.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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
36 #define n_FILE cmd_write
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 n_ignore
const *itp
, 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
);
56 save1(char *str
, int domark
, char const *cmd
, struct n_ignore
const *itp
,
57 int convert
, int sender_record
, int domove
)
59 ui64_t mstats
[1], tstats
[2];
60 enum n_fopen_state fs
;
61 int last
, *msgvec
, *ip
;
64 char const *disp
, *shell
;
66 bool_t success
, isflag
;
74 msgvec
= salloc((msgCount
+ 2) * sizeof *msgvec
);
76 for (cp
= str
; *cp
!= '\0' && spacechar(*cp
); ++cp
)
78 isflag
= (*cp
!= '\0');
80 if ((file
= snarf(str
, &isflag
, convert
!= SEND_TOFILE
)) == NULL
)
82 while(spacechar(*file
))
86 shell
= ok_vlook(SHELL
);
91 *msgvec
= first(0, MMNORM
);
93 } else if (getmsglist(str
, msgvec
, 0) < 0)
96 if (n_pstate
& (n_PS_HOOK_MASK
| n_PS_ROBOT
)) {
100 fprintf(n_stdout
, _("No messages to %s.\n"), cmd
);
105 if ((cp
= nameof(message
+ *msgvec
- 1, 0)) == NULL
) {
106 fprintf(n_stdout
, _("Cannot determine message sender to %s.\n"), cmd
);
110 for (cq
= cp
; *cq
!= '\0' && *cq
!= '@'; cq
++)
113 if (ok_blook(outfolder
)) {
114 size_t sz
= strlen(cp
) +1;
115 file
= salloc(sz
+ 1);
117 memcpy(file
+ 1, cp
, sz
);
122 /* Pipe target is special TODO hacked in later, normalize flow! */
124 if ((obuf
= Popen(file
, "w", shell
, NULL
, 1)) == NULL
) {
137 if ((file
= fexpand(file
, FEXP_FULL
)) == NULL
)
140 /* TODO all this should be URL and Mailbox-"VFS" based, and then finally
141 * TODO end up as Mailbox()->append(). Unless SEND_TOFILE, of course.
142 * TODO However, URL parse because that file:// prefix check is a HACK! */
143 if(convert
== SEND_TOFILE
&& !is_prefix("file://", file
))
144 file
= savecat("file://", file
);
145 if((obuf
= n_fopen_any(file
, "a+", &fs
)) == NULL
){
149 #if defined HAVE_POP3 && defined HAVE_IMAP
150 if(mb
.mb_type
== MB_POP3
&& (fs
& n_PROTO_MASK
) == n_PROTO_IMAP
){
152 n_err(_("Direct copy from POP3 to IMAP not supported before v15\n"));
157 disp
= (fs
& n_FOPEN_STATE_EXISTS
) ? _("[Appended]") : _("[New file]");
159 if((fs
& (n_PROTO_MASK
| n_FOPEN_STATE_EXISTS
)) ==
160 (n_PROTO_FILE
| n_FOPEN_STATE_EXISTS
)){
163 /* TODO RETURN check, but be aware of protocols: v15: Mailbox->lock()!
164 * TODO BETTER yet: should be returned in lock state already! */
165 n_file_lock(fileno(obuf
), FLT_WRITE
, 0,0, UIZ_MAX
);
167 if((xerr
= n_folder_mbox_prepare_append(obuf
, NULL
)) != n_ERR_NONE
){
175 tstats
[0] = tstats
[1] = 0;
177 imap_created_mailbox
= 0;
181 for (ip
= msgvec
; *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
);
183 mp
= &message
[*ip
- 1];
185 if((fs
& n_PROTO_MASK
) == n_PROTO_IMAP
&&
186 !n_ignore_is_any(n_IGNORE_SAVE
) && imap_thisaccount(file
)){
187 if(imap_copy(mp
, PTR2SIZE(mp
- message
+ 1), file
) == STOP
){
191 mstats
[0] = mp
->m_xsize
;
194 if (sendmp(mp
, obuf
, itp
, NULL
, convert
, mstats
) < 0) {
202 mp
->m_flag
|= MSAVED
;
204 mp
->m_flag
|= MDELETED
| MSAVED
;
208 tstats
[0] += mstats
[0];
209 tstats
[1] += mp
->m_lines
;/* TODO won't work, need target! v15!! */
222 if (!Pclose(obuf
, TRU1
))
224 } else if (Fclose(obuf
) != 0)
229 if((fs
& n_PROTO_MASK
) == n_PROTO_IMAP
){
230 if(disconnected(file
))
232 else if(imap_created_mailbox
)
238 fprintf(n_stdout
, "%s %s %" /*PRIu64 "/%"*/ PRIu64
" bytes\n",
239 n_shexp_quote_cp(file
, FAL0
), disp
,
240 /*tstats[1], TODO v15: lines written */ tstats
[0]);
242 for (ip
= msgvec
; *ip
!= 0 &&
243 UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
) {
244 mp
= message
+ *ip
- 1;
245 mp
->m_flag
&= ~MSAVED
;
248 for (ip
= msgvec
; *ip
!= 0 &&
249 UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
) {
250 mp
= message
+ *ip
- 1;
251 mp
->m_flag
&= ~(MSAVED
| MDELETED
);
255 if (domove
&& last
&& success
) {
256 setdot(message
+ last
- 1);
257 last
= first(0, MDELETED
);
258 setdot(message
+ (last
!= 0 ? last
- 1 : 0));
262 return (success
== FAL0
);
266 snarf(char *linebuf
, bool_t
*flag
, bool_t usembox
)
271 if ((cp
= laststring(linebuf
, flag
, TRU1
)) == NULL
) {
274 cp
= fexpand("&", FEXP_FULL
);
276 n_err(_("No file specified\n"));
289 rv
= save1(str
, 1, "save", n_IGNORE_SAVE
, SEND_MBOX
, 0, 0);
301 rv
= save1(str
, 1, "save", n_IGNORE_SAVE
, SEND_MBOX
, 1, 0);
313 rv
= save1(str
, 0, "copy", n_IGNORE_SAVE
, SEND_MBOX
, 0, 0);
325 rv
= save1(str
, 0, "copy", n_IGNORE_SAVE
, SEND_MBOX
, 1, 0);
337 rv
= save1(str
, 0, "move", n_IGNORE_SAVE
, SEND_MBOX
, 0, 1);
349 rv
= save1(str
, 0, "move", n_IGNORE_SAVE
, SEND_MBOX
, 1, 1);
361 rv
= save1(str
, 0, "decrypt", n_IGNORE_SAVE
, SEND_DECRYPT
, 0, 0);
373 rv
= save1(str
, 0, "decrypt", n_IGNORE_SAVE
, SEND_DECRYPT
, 1, 0);
385 if (str
== NULL
|| *str
== '\0')
386 str
= savestr(n_path_devnull
);
387 rv
= save1(str
, 0, "write", n_IGNORE_ALL
, SEND_TOFILE
, 0, 0);