*quote-fold*: add optional third argument to improve break algo
[s-mailx.git] / cmd-write.c
blob91947dd3039702eadc62e1c0a52957be8cc9c2ca
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 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
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
13 * are met:
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
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE cmd_write
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
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,
46 int domove);
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 static int
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;
62 struct message *mp;
63 char *file, *cp, *cq;
64 char const *disp, *shell;
65 FILE *obuf;
66 bool_t success, isflag;
67 NYD_ENTER;
69 success = FAL0;
70 last = 0;
71 shell = NULL;
72 file = NULL;
74 msgvec = n_autorec_alloc((msgCount + 2) * sizeof *msgvec);
75 if (sender_record) {
76 for (cp = str; *cp != '\0' && spacechar(*cp); ++cp)
78 isflag = (*cp != '\0');
79 } else {
80 if ((file = snarf(str, &isflag, convert != SEND_TOFILE)) == NULL)
81 goto jleave;
82 while(spacechar(*file))
83 ++file;
84 if (*file == '|') {
85 ++file;
86 shell = ok_vlook(SHELL);
90 if (!isflag) {
91 *msgvec = first(0, MMNORM);
92 msgvec[1] = 0;
93 } else if (getmsglist(str, msgvec, 0) < 0)
94 goto jleave;
95 if (*msgvec == 0) {
96 if (n_pstate & (n_PS_HOOK_MASK | n_PS_ROBOT)) {
97 success = TRU1;
98 goto jleave;
100 fprintf(n_stdout, _("No messages to %s.\n"), cmd);
101 goto jleave;
104 if (sender_record) {
105 struct name *np;
107 if((cp = n_header_senderfield_of(message + *msgvec - 1)) == NULL ||
108 (np = lextract(cp, GTO | GSKIN)) == NULL){
109 n_err(_("Cannot determine message sender to %s.\n"), cmd);
110 goto jleave;
112 cp = np->n_name;
114 for (cq = cp; *cq != '\0' && *cq != '@'; cq++)
116 *cq = '\0';
117 if (ok_blook(outfolder)) {
118 size_t sz = strlen(cp) +1;
119 file = n_autorec_alloc(sz + 1);
120 file[0] = '+';
121 memcpy(file + 1, cp, sz);
122 } else
123 file = cp;
126 /* Pipe target is special TODO hacked in later, normalize flow! */
127 if (shell != NULL) {
128 if ((obuf = Popen(file, "w", shell, NULL, 1)) == NULL) {
129 int esave;
131 esave = n_err_no;
132 n_perr(file, esave);
133 n_err_no = esave;
134 goto jleave;
136 isflag = FAL0;
137 disp = A_("[Piped]");
138 goto jsend;
141 if ((file = fexpand(file, FEXP_FULL)) == NULL)
142 goto jleave;
144 /* TODO all this should be URL and Mailbox-"VFS" based, and then finally
145 * TODO end up as Mailbox()->append(). Unless SEND_TOFILE, of course.
146 * TODO However, URL parse because that file:// prefix check is a HACK! */
147 if(convert == SEND_TOFILE && !is_prefix("file://", file))
148 file = savecat("file://", file);
149 if((obuf = n_fopen_any(file, "a+", &fs)) == NULL){
150 n_perr(file, 0);
151 goto jleave;
154 #if defined HAVE_POP3 && defined HAVE_IMAP
155 if(mb.mb_type == MB_POP3 && (fs & n_PROTO_MASK) == n_PROTO_IMAP){
156 Fclose(obuf);
157 n_err(_("Direct copy from POP3 to IMAP not supported before v15\n"));
158 goto jleave;
160 #endif
162 disp = (fs & n_FOPEN_STATE_EXISTS) ? A_("[Appended]") : A_("[New file]");
164 if((fs & (n_PROTO_MASK | n_FOPEN_STATE_EXISTS)) ==
165 (n_PROTO_FILE | n_FOPEN_STATE_EXISTS)){
166 int xerr;
168 /* TODO RETURN check, but be aware of protocols: v15: Mailbox->lock()!
169 * TODO BETTER yet: should be returned in lock state already! */
170 n_file_lock(fileno(obuf), FLT_WRITE, 0,0, UIZ_MAX);
172 if((xerr = n_folder_mbox_prepare_append(obuf, NULL)) != n_ERR_NONE){
173 n_perr(file, xerr);
174 goto jleave;
178 jsend:
179 success = TRU1;
180 tstats[0] = tstats[1] = 0;
181 #ifdef HAVE_IMAP
182 imap_created_mailbox = 0;
183 #endif
185 srelax_hold();
186 for (ip = msgvec; *ip != 0 && UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount);
187 ++ip) {
188 mp = &message[*ip - 1];
189 #ifdef HAVE_IMAP
190 if((fs & n_PROTO_MASK) == n_PROTO_IMAP &&
191 !n_ignore_is_any(n_IGNORE_SAVE) && imap_thisaccount(file)){
192 if(imap_copy(mp, PTR2SIZE(mp - message + 1), file) == STOP){
193 success = FAL0;
194 goto jferr;
196 mstats[0] = mp->m_xsize;
197 }else
198 #endif
199 if (sendmp(mp, obuf, itp, NULL, convert, mstats) < 0) {
200 success = FAL0;
201 goto jferr;
203 srelax();
205 touch(mp);
206 if (domark)
207 mp->m_flag |= MSAVED;
208 if (domove) {
209 mp->m_flag |= MDELETED | MSAVED;
210 last = *ip;
213 tstats[0] += mstats[0];
214 tstats[1] += mp->m_lines;/* TODO won't work, need target! v15!! */
216 srelax_rele();
218 fflush(obuf);
220 /* TODO Should be a VFS, then n_MBOX knows what to do upon .close()! */
221 if((fs & n_PROTO_MASK) == n_PROTO_FILE && convert == SEND_MBOX)
222 n_folder_mbox_prepare_append(obuf, NULL);
224 if (ferror(obuf)) {
225 jferr:
226 n_perr(file, 0);
227 if (!success)
228 srelax_rele();
229 success = FAL0;
231 if (shell != NULL) {
232 if (!Pclose(obuf, TRU1))
233 success = FAL0;
234 } else if (Fclose(obuf) != 0)
235 success = FAL0;
237 if (success) {
238 #ifdef HAVE_IMAP
239 if((fs & n_PROTO_MASK) == n_PROTO_IMAP){
240 if(disconnected(file))
241 disp = A_("[Queued]");
242 else if(imap_created_mailbox)
243 disp = A_("[New file]");
244 else
245 disp = A_("[Appended]");
247 #endif
248 fprintf(n_stdout, "%s %s %" /*PRIu64 "/%"*/ PRIu64 " bytes\n",
249 n_shexp_quote_cp(file, FAL0), disp,
250 /*tstats[1], TODO v15: lines written */ tstats[0]);
251 } else if (domark) {
252 for (ip = msgvec; *ip != 0 &&
253 UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount); ++ip) {
254 mp = message + *ip - 1;
255 mp->m_flag &= ~MSAVED;
257 } else if (domove) {
258 for (ip = msgvec; *ip != 0 &&
259 UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount); ++ip) {
260 mp = message + *ip - 1;
261 mp->m_flag &= ~(MSAVED | MDELETED);
265 if (domove && last && success) {
266 setdot(message + last - 1);
267 last = first(0, MDELETED);
268 setdot(message + (last != 0 ? last - 1 : 0));
270 jleave:
271 NYD_LEAVE;
272 return (success == FAL0);
275 static char *
276 snarf(char *linebuf, bool_t *flag, bool_t usembox)
278 char *cp;
279 NYD_ENTER;
281 if ((cp = laststring(linebuf, flag, TRU1)) == NULL) {
282 if (usembox) {
283 *flag = FAL0;
284 cp = fexpand("&", FEXP_NVAR);
285 } else
286 n_err(_("No file specified\n"));
288 NYD_LEAVE;
289 return cp;
292 FL int
293 c_save(void *v)
295 char *str = v;
296 int rv;
297 NYD_ENTER;
299 rv = save1(str, 1, "save", n_IGNORE_SAVE, SEND_MBOX, 0, 0);
300 NYD_LEAVE;
301 return rv;
304 FL int
305 c_Save(void *v)
307 char *str = v;
308 int rv;
309 NYD_ENTER;
311 rv = save1(str, 1, "save", n_IGNORE_SAVE, SEND_MBOX, 1, 0);
312 NYD_LEAVE;
313 return rv;
316 FL int
317 c_copy(void *v)
319 char *str = v;
320 int rv;
321 NYD_ENTER;
323 rv = save1(str, 0, "copy", n_IGNORE_SAVE, SEND_MBOX, 0, 0);
324 NYD_LEAVE;
325 return rv;
328 FL int
329 c_Copy(void *v)
331 char *str = v;
332 int rv;
333 NYD_ENTER;
335 rv = save1(str, 0, "copy", n_IGNORE_SAVE, SEND_MBOX, 1, 0);
336 NYD_LEAVE;
337 return rv;
340 FL int
341 c_move(void *v)
343 char *str = v;
344 int rv;
345 NYD_ENTER;
347 rv = save1(str, 0, "move", n_IGNORE_SAVE, SEND_MBOX, 0, 1);
348 NYD_LEAVE;
349 return rv;
352 FL int
353 c_Move(void *v)
355 char *str = v;
356 int rv;
357 NYD_ENTER;
359 rv = save1(str, 0, "move", n_IGNORE_SAVE, SEND_MBOX, 1, 1);
360 NYD_LEAVE;
361 return rv;
364 FL int
365 c_decrypt(void *v)
367 char *str = v;
368 int rv;
369 NYD_ENTER;
371 rv = save1(str, 0, "decrypt", n_IGNORE_SAVE, SEND_DECRYPT, 0, 0);
372 NYD_LEAVE;
373 return rv;
376 FL int
377 c_Decrypt(void *v)
379 char *str = v;
380 int rv;
381 NYD_ENTER;
383 rv = save1(str, 0, "decrypt", n_IGNORE_SAVE, SEND_DECRYPT, 1, 0);
384 NYD_LEAVE;
385 return rv;
388 FL int
389 c_write(void *v)
391 char *str = v;
392 int rv;
393 NYD_ENTER;
395 if (str == NULL || *str == '\0')
396 str = savestr(n_path_devnull);
397 rv = save1(str, 0, "write", n_IGNORE_ALL, SEND_TOFILE, 0, 0);
398 NYD_LEAVE;
399 return rv;
402 /* s-it-mode */