IMAP: fix and tweak previous [830f948]
[s-mailx.git] / edit.c
blobbb0e1f7fc7400a8a7059992c72a3e99707249066
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Perform message editing functions.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 edit
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 /* Edit a message by writing the message into a funnily-named file (which
43 * should not exist) and forking an editor on it */
44 static int edit1(int *msgvec, int viored);
46 static int
47 edit1(int *msgvec, int viored)
49 int c, i;
50 FILE *fp = NULL;
51 struct message *mp;
52 off_t size;
53 bool_t wb, lastnl;
54 char *line = NULL; /* TODO line pool */
55 size_t linesize = 0;
56 NYD_ENTER;
58 wb = ok_blook(writebackedited);
60 /* Deal with each message to be edited... */
61 for (i = 0; msgvec[i] != 0 && i < msgCount; ++i) {
62 sighandler_type sigint;
64 if (i > 0) { /* TODO getapproval(): return APPROV_{YES,NO,QUIT}: USE! */
65 char *p;
67 printf(_("Edit message %d [ynq]? "), msgvec[i]);
68 fflush(stdout);
69 if (readline_restart(stdin, &line, &linesize, 0) < 0)
70 break;
71 for (p = line; blankchar(*p); ++p)
73 if (*p == 'q')
74 break;
75 if (*p == 'n')
76 continue;
78 mp = message + msgvec[i] - 1;
79 setdot(mp);
80 pstate |= PS_DID_PRINT_DOT;
81 touch(mp);
83 sigint = safe_signal(SIGINT, SIG_IGN);
85 --mp->m_size; /* Strip final NL.. TODO MAILVFS->MESSAGE->length() */
86 fp = run_editor(fp, -1/*mp->m_size TODO */, viored,
87 ((mb.mb_perm & MB_EDIT) == 0 || !wb), NULL, mp,
88 (wb ? SEND_MBOX : SEND_TODISP_ALL), sigint);
89 ++mp->m_size; /* And readd it TODO */
91 if (fp != NULL) {
92 fseek(mb.mb_otf, 0L, SEEK_END);
93 size = ftell(mb.mb_otf);
94 mp->m_block = mailx_blockof(size);
95 mp->m_offset = mailx_offsetof(size);
96 mp->m_lines = 0;
97 mp->m_flag |= MODIFY;
98 rewind(fp);
99 lastnl = 0;
100 size = 0;
101 while ((c = getc(fp)) != EOF) {
102 if ((lastnl = (c == '\n')))
103 ++mp->m_lines;
104 if (putc(c, mb.mb_otf) == EOF)
105 break;
106 ++size;
108 if (!lastnl && putc('\n', mb.mb_otf) != EOF)
109 ++size;
110 if (putc('\n', mb.mb_otf) != EOF)
111 ++size;
112 mp->m_size = (size_t)size;
113 if (ferror(mb.mb_otf))
114 n_perr(_("/tmp"), 0);
115 Fclose(fp);
118 safe_signal(SIGINT, sigint);
121 if (line != NULL)
122 free(line);
123 NYD_LEAVE;
124 return 0;
127 FL int
128 c_editor(void *v)
130 int *msgvec = v, rv;
131 NYD_ENTER;
133 rv = edit1(msgvec, 'e');
134 NYD_LEAVE;
135 return rv;
138 FL int
139 c_visual(void *v)
141 int *msgvec = v, rv;
142 NYD_ENTER;
144 rv = edit1(msgvec, 'v');
145 NYD_LEAVE;
146 return rv;
149 FL FILE *
150 run_editor(FILE *fp, off_t size, int viored, int readonly, struct header *hp,
151 struct message *mp, enum sendaction action, sighandler_type oldint)
153 struct stat statb;
154 sigset_t cset;
155 FILE *nf = NULL;
156 int t;
157 time_t modtime;
158 off_t modsize;
159 char const *ed;
160 char *tempEdit;
161 NYD_ENTER;
163 if ((nf = Ftmp(&tempEdit, "runed", OF_WRONLY | OF_REGISTER,
164 (readonly ? 0400 : 0600))) == NULL) {
165 n_perr(_("temporary mail edit file"), 0);
166 goto jleave;
169 if (hp != NULL) {
170 t = GTO | GSUBJECT | GCC | GBCC | GNL | GCOMMA;
171 if ((hp->h_from != NULL || myaddrs(hp) != NULL) ||
172 (hp->h_sender != NULL || ok_vlook(sender) != NULL) ||
173 (hp->h_replyto != NULL || ok_vlook(replyto) != NULL) ||
174 (hp->h_organization != NULL || ok_vlook(ORGANIZATION) != NULL) ||
175 hp->h_list_post != NULL || (hp->h_flags & HF_LIST_REPLY))
176 t |= GIDENT;
177 puthead(hp, nf, t, SEND_TODISP, CONV_NONE, NULL, NULL);
180 if (mp != NULL) {
181 if (sendmp(mp, nf, 0, NULL, action, NULL) < 0) {
182 n_err(_("Failed to prepare editable message"));
183 goto jleave;
185 } else {
186 if (size >= 0)
187 while (--size >= 0 && (t = getc(fp)) != EOF)
188 putc(t, nf);
189 else
190 while ((t = getc(fp)) != EOF)
191 putc(t, nf);
194 fflush(nf);
195 if (fstat(fileno(nf), &statb) == -1)
196 modtime = 0, modsize = 0;
197 else
198 modtime = statb.st_mtime, modsize = statb.st_size;
199 t = ferror(nf);
200 if (Fclose(nf) < 0 || t != 0) {
201 n_perr(tempEdit, 0);
202 t = 1;
204 nf = NULL;
205 if (t != 0)
206 goto jleave;
208 ed = (viored == 'e') ? ok_vlook(EDITOR) : ok_vlook(VISUAL);
209 if (ed == NULL)
210 ed = (viored == 'e') ? "ed" : "vi"; /* XXX no magics, -> nail.h */
212 sigemptyset(&cset);
213 if (run_command(ed, (oldint != SIG_IGN ? &cset : NULL), -1, -1, tempEdit,
214 NULL, NULL) < 0)
215 goto jleave;
217 /* If in read only mode or file unchanged, just remove the editor temporary
218 * and return. Otherwise switch to new file */
219 if (readonly)
220 goto jleave;
221 if (stat(tempEdit, &statb) == -1) {
222 n_perr(tempEdit, 0);
223 goto jleave;
226 if ((modtime != statb.st_mtime || modsize != statb.st_size) &&
227 (nf = Fopen(tempEdit, "a+")) == NULL)
228 n_perr(tempEdit, 0);
229 jleave:
230 if (tempEdit != NULL) {
231 unlink(tempEdit);
232 Ftmp_free(&tempEdit);
234 NYD_LEAVE;
235 return nf;
238 /* s-it-mode */