2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 /* Close approximation of the mailx(1) builtin editor for sending mail. */
17 #include "mutt_curses.h"
18 #include "mutt_idna.h"
29 * SLcurses_waddnstr() can't take a "const char *", so this is only
30 * declared "static" (sigh)
32 static char *EditorHelp = N_("\
33 ~~ insert a line begining with a single ~\n\
34 ~b users add users to the Bcc: field\n\
35 ~c users add users to the Cc: field\n\
36 ~f messages include messages\n\
37 ~F messages same as ~f, except also include headers\n\
38 ~h edit the message header\n\
39 ~m messages include and quote messages\n\
40 ~M messages same as ~m, except include headers\n\
41 ~p print the message\n\
42 ~q write file and quit editor\n\
43 ~r file read a file into the editor\n\
44 ~t users add users to the To: field\n\
45 ~u recall the previous line\n\
46 ~v edit message with the $visual editor\n\
47 ~w file write message to file\n\
48 ~x abort changes and quit editor\n\
50 . on a line by itself ends input\n");
52 static char **be_snarf_data (FILE * f, char **buf, int *bufmax, int *buflen,
53 int offset, int bytes, int prefix)
55 char tmp[HUGE_STRING];
57 int tmplen = sizeof (tmp);
59 tmp[sizeof (tmp) - 1] = 0;
61 strfcpy (tmp, NONULL (Prefix), sizeof (tmp));
62 tmplen = mutt_strlen (tmp);
64 tmplen = sizeof (tmp) - tmplen;
69 if (fgets (p, tmplen - 1, f) == NULL)
71 bytes -= mutt_strlen (p);
72 if (*bufmax == *buflen)
73 safe_realloc (&buf, sizeof (char *) * (*bufmax += 25));
74 buf[(*buflen)++] = safe_strdup (tmp);
76 if (buf && *bufmax == *buflen) { /* Do not smash memory past buf */
77 safe_realloc (&buf, sizeof (char *) * (++*bufmax));
84 static char **be_snarf_file (const char *path, char **buf, int *max, int *len,
88 char tmp[LONG_STRING];
91 if ((f = fopen (path, "r"))) {
92 fstat (fileno (f), &sb);
93 buf = be_snarf_data (f, buf, max, len, 0, sb.st_size, 0);
95 snprintf (tmp, sizeof (tmp), "\"%s\" %lu bytes\n", path,
96 (unsigned long) sb.st_size);
102 snprintf (tmp, sizeof (tmp), "%s: %s\n", path, strerror (errno));
108 static int be_barf_file (const char *path, char **buf, int buflen)
113 if ((f = safe_fopen (path, "w")) == NULL) { /* __FOPEN_CHECKED__ */
114 addstr (strerror (errno));
118 for (i = 0; i < buflen; i++)
122 printw ("fclose: %s\n", strerror (errno));
126 static void be_free_memory (char **buf, int buflen)
134 static char **be_include_messages (char *msg, char **buf, int *bufmax,
135 int *buflen, int pfx, int inc_hdrs)
137 int offset, bytes, n;
138 char tmp[LONG_STRING];
140 while ((msg = strtok (msg, " ,")) != NULL) {
142 if (n > 0 && n <= Context->msgcount) {
145 /* add the attribution */
147 mutt_make_string (tmp, sizeof (tmp) - 1, Attribution, Context,
149 strcat (tmp, "\n"); /* __STRCAT_CHECKED__ */
152 if (*bufmax == *buflen)
153 safe_realloc (&buf, sizeof (char *) * (*bufmax += 25));
154 buf[(*buflen)++] = safe_strdup (tmp);
156 bytes = Context->hdrs[n]->content->length;
158 offset = Context->hdrs[n]->offset;
159 bytes += Context->hdrs[n]->content->offset - offset;
162 offset = Context->hdrs[n]->content->offset;
163 buf = be_snarf_data (Context->fp, buf, bufmax, buflen, offset, bytes,
166 if (*bufmax == *buflen)
167 safe_realloc (&buf, sizeof (char *) * (*bufmax += 25));
168 buf[(*buflen)++] = safe_strdup ("\n");
171 printw (_("%d: invalid message number.\n"), n);
177 static void be_print_header (ENVELOPE * env)
179 char tmp[HUGE_STRING];
184 rfc822_write_address (tmp, sizeof (tmp), env->to, 1);
191 rfc822_write_address (tmp, sizeof (tmp), env->cc, 1);
198 rfc822_write_address (tmp, sizeof (tmp), env->bcc, 1);
203 addstr ("Subject: ");
204 addstr (env->subject);
211 * force override the $ask* vars (used for the ~h command)
213 static void be_edit_header (ENVELOPE * e, int force)
215 char tmp[HUGE_STRING];
221 mutt_addrlist_to_local (e->to);
222 rfc822_write_address (tmp, sizeof (tmp), e->to, 0);
223 if (!e->to || force) {
224 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 4, 0) == 0) {
225 rfc822_free_address (&e->to);
226 e->to = mutt_parse_adrlist (e->to, tmp);
227 e->to = mutt_expand_aliases (e->to);
228 mutt_addrlist_to_idna (e->to, NULL); /* XXX - IDNA error reporting? */
230 rfc822_write_address (tmp, sizeof (tmp), e->to, 1);
231 mvaddstr (LINES - 1, 4, tmp);
235 mutt_addrlist_to_idna (e->to, NULL); /* XXX - IDNA error reporting? */
240 if (!e->subject || force) {
241 addstr ("Subject: ");
242 strfcpy (tmp, e->subject ? e->subject : "", sizeof (tmp));
243 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 9, 0) == 0)
244 mutt_str_replace (&e->subject, tmp);
248 if ((!e->cc && option (OPTASKCC)) || force) {
251 mutt_addrlist_to_local (e->cc);
252 rfc822_write_address (tmp, sizeof (tmp), e->cc, 0);
253 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 4, 0) == 0) {
254 rfc822_free_address (&e->cc);
255 e->cc = mutt_parse_adrlist (e->cc, tmp);
256 e->cc = mutt_expand_aliases (e->cc);
258 mutt_addrlist_to_idna (e->cc, NULL);
259 rfc822_write_address (tmp, sizeof (tmp), e->cc, 1);
260 mvaddstr (LINES - 1, 4, tmp);
263 mutt_addrlist_to_idna (e->cc, NULL);
267 if (option (OPTASKBCC) || force) {
270 mutt_addrlist_to_local (e->bcc);
271 rfc822_write_address (tmp, sizeof (tmp), e->bcc, 0);
272 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 5, 0) == 0) {
273 rfc822_free_address (&e->bcc);
274 e->bcc = mutt_parse_adrlist (e->bcc, tmp);
275 e->bcc = mutt_expand_aliases (e->bcc);
276 mutt_addrlist_to_idna (e->bcc, NULL);
278 rfc822_write_address (tmp, sizeof (tmp), e->bcc, 1);
279 mvaddstr (LINES - 1, 5, tmp);
282 mutt_addrlist_to_idna (e->bcc, NULL);
287 int mutt_builtin_editor (const char *path, HEADER * msg, HEADER * cur)
290 int bufmax = 0, buflen = 0;
291 char tmp[LONG_STRING];
297 scrollok (stdscr, TRUE);
299 be_edit_header (msg->env, 0);
301 addstr (_("(End message with a . on a line by itself)\n"));
303 buf = be_snarf_file (path, buf, &bufmax, &buflen, 0);
307 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 0, 0) == -1) {
313 if (EscChar && tmp[0] == EscChar[0] && tmp[1] != EscChar[0]) {
314 /* remove trailing whitespace from the line */
315 p = tmp + mutt_strlen (tmp) - 1;
316 while (p >= tmp && ISSPACE (*p))
324 addstr (_(EditorHelp));
327 msg->env->bcc = mutt_parse_adrlist (msg->env->bcc, p);
328 msg->env->bcc = mutt_expand_aliases (msg->env->bcc);
331 msg->env->cc = mutt_parse_adrlist (msg->env->cc, p);
332 msg->env->cc = mutt_expand_aliases (msg->env->cc);
335 be_edit_header (msg->env, 1);
343 /* include the current message */
344 p = tmp + mutt_strlen (tmp) + 1;
345 snprintf (tmp + mutt_strlen (tmp),
346 sizeof (tmp) - mutt_strlen (tmp), " %d",
349 buf = be_include_messages (p, buf, &bufmax, &buflen,
350 (ascii_tolower (tmp[1]) == 'm'),
352 ((unsigned char) tmp[1])));
355 addstr (_("No mailbox.\n"));
359 addstr (_("Message contains:\n"));
360 be_print_header (msg->env);
361 for (i = 0; i < buflen; i++)
363 addstr (_("(continue)\n"));
370 strncpy (tmp, p, sizeof (tmp));
371 mutt_expand_path (tmp, sizeof (tmp));
372 buf = be_snarf_file (tmp, buf, &bufmax, &buflen, 1);
375 addstr (_("missing filename.\n"));
378 mutt_str_replace (&msg->env->subject, p);
381 msg->env->to = rfc822_parse_adrlist (msg->env->to, p);
382 msg->env->to = mutt_expand_aliases (msg->env->to);
387 strfcpy (tmp, buf[buflen], sizeof (tmp));
388 tmp[mutt_strlen (tmp) - 1] = 0;
394 addstr (_("No lines in message.\n"));
399 if (be_barf_file (path, buf, buflen) == 0) {
402 be_free_memory (buf, buflen);
406 if (option (OPTEDITHDRS)) {
407 mutt_env_to_local (msg->env);
408 mutt_edit_headers (NONULL (Visual), path, msg, NULL, 0);
409 if (mutt_env_to_idna (msg->env, &tag, &err))
410 printw (_("Bad IDN in %s: '%s'\n"), tag, err);
413 mutt_edit_file (NONULL (Visual), path);
415 buf = be_snarf_file (path, buf, &bufmax, &buflen, 0);
417 addstr (_("(continue)\n"));
421 be_barf_file (*p ? p : path, buf, buflen);
428 printw (_("%s: unknown editor command (~? for help)\n"), tmp);
432 else if (mutt_strcmp (".", tmp) == 0)
435 safe_strcat (tmp, sizeof (tmp), "\n");
436 if (buflen == bufmax)
437 safe_realloc (&buf, sizeof (char *) * (bufmax += 25));
438 buf[buflen++] = safe_strdup (tmp[1] == '~' ? tmp + 1 : tmp);
445 be_barf_file (path, buf, buflen);
446 be_free_memory (buf, buflen);
448 return (abort ? -1 : 0);