Change *on-compose-{enter,leave}* environment + fixes..
[s-mailx.git] / cmd-write.c
blobb0b07a5e325db8f0881ab6d01dae499fd335627c
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>.
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 disp = n_empty;
72 shell = NULL;
73 file = NULL;
75 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
76 if (sender_record) {
77 for (cp = str; *cp != '\0' && spacechar(*cp); ++cp)
79 isflag = (*cp != '\0');
80 } else {
81 if ((file = snarf(str, &isflag, convert != SEND_TOFILE)) == NULL)
82 goto jleave;
83 while(spacechar(*file))
84 ++file;
85 if (*file == '|') {
86 ++file;
87 shell = ok_vlook(SHELL);
91 if (!isflag) {
92 *msgvec = first(0, MMNORM);
93 msgvec[1] = 0;
94 } else if (getmsglist(str, msgvec, 0) < 0)
95 goto jleave;
96 if (*msgvec == 0) {
97 if (n_pstate & (n_PS_HOOK_MASK | n_PS_ROBOT)) {
98 success = TRU1;
99 goto jleave;
101 fprintf(n_stdout, _("No messages to %s.\n"), cmd);
102 goto jleave;
105 if (sender_record) {
106 if ((cp = nameof(message + *msgvec - 1, 0)) == NULL) {
107 fprintf(n_stdout, _("Cannot determine message sender to %s.\n"), cmd);
108 goto jleave;
111 for (cq = cp; *cq != '\0' && *cq != '@'; cq++)
113 *cq = '\0';
114 if (ok_blook(outfolder)) {
115 size_t sz = strlen(cp) +1;
116 file = salloc(sz + 1);
117 file[0] = '+';
118 memcpy(file + 1, cp, sz);
119 } else
120 file = cp;
123 /* Pipe target is special TODO hacked in later, normalize flow! */
124 if (shell != NULL) {
125 if ((obuf = Popen(file, "w", shell, NULL, 1)) == NULL) {
126 int esave;
128 esave = n_err_no;
129 n_perr(file, esave);
130 n_err_no = esave;
131 goto jleave;
133 isflag = FAL0;
134 disp = _("[Piped]");
135 goto jsend;
138 if ((file = fexpand(file, FEXP_FULL)) == NULL)
139 goto jleave;
141 /* TODO all this should be URL and Mailbox-"VFS" based, and then finally
142 * TODO end up as Mailbox()->append(). Unless SEND_TOFILE, of course.
143 * TODO However, URL parse because that file:// prefix check is a HACK! */
144 if(convert == SEND_TOFILE && !is_prefix("file://", file))
145 file = savecat("file://", file);
146 if((obuf = n_fopen_any(file, "a+", &fs)) == NULL){
147 n_perr(file, 0);
148 goto jleave;
151 disp = (fs & n_FOPEN_STATE_EXISTS) ? _("[Appended]") : _("[New file]");
153 if((fs & (n_PROTO_MASK | n_FOPEN_STATE_EXISTS)) ==
154 (n_PROTO_FILE | n_FOPEN_STATE_EXISTS)){
155 int xerr;
157 /* TODO RETURN check, but be aware of protocols: v15: Mailbox->lock()!
158 * TODO BETTER yet: should be returned in lock state already! */
159 n_file_lock(fileno(obuf), FLT_WRITE, 0,0, UIZ_MAX);
161 if((xerr = n_folder_mbox_prepare_append(obuf, NULL)) != n_ERR_NONE){
162 n_perr(file, xerr);
163 goto jleave;
167 jsend:
168 success = TRU1;
169 tstats[0] = tstats[1] = 0;
171 srelax_hold();
172 for (ip = msgvec; *ip != 0 && UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount);
173 ++ip) {
174 mp = message + *ip - 1;
175 if (sendmp(mp, obuf, itp, NULL, convert, mstats) < 0) {
176 success = FAL0;
177 goto jferr;
179 srelax();
181 touch(mp);
182 if (domark)
183 mp->m_flag |= MSAVED;
184 if (domove) {
185 mp->m_flag |= MDELETED | MSAVED;
186 last = *ip;
189 tstats[0] += mstats[0];
190 tstats[1] += mp->m_lines;/* TODO won't work, need target! v15!! */
192 srelax_rele();
194 fflush(obuf);
195 if (ferror(obuf)) {
196 jferr:
197 n_perr(file, 0);
198 if (!success)
199 srelax_rele();
200 success = FAL0;
202 if (shell != NULL) {
203 if (!Pclose(obuf, TRU1))
204 success = FAL0;
205 } else if (Fclose(obuf) != 0)
206 success = FAL0;
208 if (success) {
209 fprintf(n_stdout, "%s %s %" /*PRIu64 "/%"*/ PRIu64 " bytes\n",
210 n_shexp_quote_cp(file, FAL0), disp,
211 /*tstats[1], TODO v15: lines written */ tstats[0]);
212 } else if (domark) {
213 for (ip = msgvec; *ip != 0 &&
214 UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount); ++ip) {
215 mp = message + *ip - 1;
216 mp->m_flag &= ~MSAVED;
218 } else if (domove) {
219 for (ip = msgvec; *ip != 0 &&
220 UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount); ++ip) {
221 mp = message + *ip - 1;
222 mp->m_flag &= ~(MSAVED | MDELETED);
226 if (domove && last && success) {
227 setdot(message + last - 1);
228 last = first(0, MDELETED);
229 setdot(message + (last != 0 ? last - 1 : 0));
231 jleave:
232 NYD_LEAVE;
233 return (success == FAL0);
236 static char *
237 snarf(char *linebuf, bool_t *flag, bool_t usembox)
239 char *cp;
240 NYD_ENTER;
242 if ((cp = laststring(linebuf, flag, TRU1)) == NULL) {
243 if (usembox) {
244 *flag = FAL0;
245 cp = fexpand("&", FEXP_FULL);
246 } else
247 n_err(_("No file specified\n"));
249 NYD_LEAVE;
250 return cp;
253 FL int
254 c_save(void *v)
256 char *str = v;
257 int rv;
258 NYD_ENTER;
260 rv = save1(str, 1, "save", n_IGNORE_SAVE, SEND_MBOX, 0, 0);
261 NYD_LEAVE;
262 return rv;
265 FL int
266 c_Save(void *v)
268 char *str = v;
269 int rv;
270 NYD_ENTER;
272 rv = save1(str, 1, "save", n_IGNORE_SAVE, SEND_MBOX, 1, 0);
273 NYD_LEAVE;
274 return rv;
277 FL int
278 c_copy(void *v)
280 char *str = v;
281 int rv;
282 NYD_ENTER;
284 rv = save1(str, 0, "copy", n_IGNORE_SAVE, SEND_MBOX, 0, 0);
285 NYD_LEAVE;
286 return rv;
289 FL int
290 c_Copy(void *v)
292 char *str = v;
293 int rv;
294 NYD_ENTER;
296 rv = save1(str, 0, "copy", n_IGNORE_SAVE, SEND_MBOX, 1, 0);
297 NYD_LEAVE;
298 return rv;
301 FL int
302 c_move(void *v)
304 char *str = v;
305 int rv;
306 NYD_ENTER;
308 rv = save1(str, 0, "move", n_IGNORE_SAVE, SEND_MBOX, 0, 1);
309 NYD_LEAVE;
310 return rv;
313 FL int
314 c_Move(void *v)
316 char *str = v;
317 int rv;
318 NYD_ENTER;
320 rv = save1(str, 0, "move", n_IGNORE_SAVE, SEND_MBOX, 1, 1);
321 NYD_LEAVE;
322 return rv;
325 FL int
326 c_decrypt(void *v)
328 char *str = v;
329 int rv;
330 NYD_ENTER;
332 rv = save1(str, 0, "decrypt", n_IGNORE_SAVE, SEND_DECRYPT, 0, 0);
333 NYD_LEAVE;
334 return rv;
337 FL int
338 c_Decrypt(void *v)
340 char *str = v;
341 int rv;
342 NYD_ENTER;
344 rv = save1(str, 0, "decrypt", n_IGNORE_SAVE, SEND_DECRYPT, 1, 0);
345 NYD_LEAVE;
346 return rv;
349 FL int
350 c_write(void *v)
352 char *str = v;
353 int rv;
354 NYD_ENTER;
356 if (str == NULL || *str == '\0')
357 str = savestr(n_path_devnull);
358 rv = save1(str, 0, "write", n_IGNORE_ALL, SEND_TOFILE, 0, 0);
359 NYD_LEAVE;
360 return rv;
363 /* s-it-mode */