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 - 2014 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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
44 /* Edit a message by writing the message into a funnily-named file (which
45 * should not exist) and forking an editor on it */
46 static int edit1(int *msgvec
, int viored
);
49 edit1(int *msgvec
, int viored
)
56 char *line
= NULL
; /* TODO line pool */
60 wb
= ok_blook(writebackedited
);
62 /* Deal with each message to be edited... */
63 for (i
= 0; msgvec
[i
] != 0 && i
< msgCount
; ++i
) {
64 sighandler_type sigint
;
66 if (i
> 0) { /* TODO getapproval(): return APPROV_{YES,NO,QUIT}: USE! */
69 printf(tr(72, "Edit message %d [ynq]? "), msgvec
[i
]);
71 if (readline_restart(stdin
, &line
, &linesize
, 0) < 0)
73 for (p
= line
; blankchar(*p
); ++p
)
80 mp
= message
+ msgvec
[i
] - 1;
85 sigint
= safe_signal(SIGINT
, SIG_IGN
);
87 --mp
->m_size
; /* Strip final NL.. TODO MAILVFS->MESSAGE->length() */
88 fp
= run_editor(fp
, -1/*mp->m_size TODO */, viored
,
89 ((mb
.mb_perm
& MB_EDIT
) == 0 || !wb
), NULL
, mp
,
90 (wb
? SEND_MBOX
: SEND_TODISP_ALL
), sigint
);
91 ++mp
->m_size
; /* And readd it TODO */
94 fseek(mb
.mb_otf
, 0L, SEEK_END
);
95 size
= ftell(mb
.mb_otf
);
96 mp
->m_block
= mailx_blockof(size
);
97 mp
->m_offset
= mailx_offsetof(size
);
103 while ((c
= getc(fp
)) != EOF
) {
104 if ((lastnl
= (c
== '\n')))
106 if (putc(c
, mb
.mb_otf
) == EOF
)
110 if (!lastnl
&& putc('\n', mb
.mb_otf
) != EOF
)
112 if (putc('\n', mb
.mb_otf
) != EOF
)
114 mp
->m_size
= (size_t)size
;
115 if (ferror(mb
.mb_otf
))
120 safe_signal(SIGINT
, sigint
);
135 rv
= edit1(msgvec
, 'e');
146 rv
= edit1(msgvec
, 'v');
152 run_editor(FILE *fp
, off_t size
, int viored
, int readonly
, struct header
*hp
,
153 struct message
*mp
, enum sendaction action
, sighandler_type oldint
)
164 if ((nf
= Ftmp(&tempEdit
, "runed", OF_WRONLY
| OF_REGISTER
,
165 (readonly
? 0400 : 0600))) == NULL
) {
166 perror(tr(73, "temporary mail edit file"));
171 t
= GTO
| GSUBJECT
| GCC
| GBCC
| GNL
| GCOMMA
;
172 if (hp
->h_from
!= NULL
|| hp
->h_replyto
!= NULL
||
173 hp
->h_sender
!= NULL
|| hp
->h_organization
!= NULL
)
175 puthead(hp
, nf
, t
, SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
179 sendmp(mp
, nf
, 0, NULL
, action
, NULL
);
182 while (--size
>= 0 && (t
= getc(fp
)) != EOF
)
185 while ((t
= getc(fp
)) != EOF
)
190 if (fstat(fileno(nf
), &statb
) == -1)
193 modtime
= statb
.st_mtime
;
195 if (Fclose(nf
) < 0 || t
!= 0) {
203 ed
= (viored
== 'e') ? ok_vlook(EDITOR
) : ok_vlook(VISUAL
);
205 ed
= (viored
== 'e') ? "ed" : "vi"; /* XXX no magics, -> nail.h */
208 if (run_command(ed
, (oldint
!= SIG_IGN
? &cset
: NULL
), -1, -1, tempEdit
,
212 /* If in read only mode or file unchanged, just remove the editor temporary
213 * and return. Otherwise switch to new file */
216 if (stat(tempEdit
, &statb
) == -1) {
220 if (modtime
!= statb
.st_mtime
&& (nf
= Fopen(tempEdit
, "a+")) == NULL
)
223 if (tempEdit
!= NULL
) {
225 Ftmp_free(&tempEdit
);
231 /* vim:set fenc=utf-8:s-it-mode */