2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /* Close approximation of the mailx(1) builtin editor for sending mail. */
26 #include "mutt_curses.h"
27 #include "mutt_idna.h"
38 * SLcurses_waddnstr() can't take a "const char *", so this is only
39 * declared "static" (sigh)
41 static char* EditorHelp = N_("\
42 ~~ insert a line begining with a single ~\n\
43 ~b users add users to the Bcc: field\n\
44 ~c users add users to the Cc: field\n\
45 ~f messages include messages\n\
46 ~F messages same as ~f, except also include headers\n\
47 ~h edit the message header\n\
48 ~m messages include and quote messages\n\
49 ~M messages same as ~m, except include headers\n\
50 ~p print the message\n\
51 ~q write file and quit editor\n\
52 ~r file read a file into the editor\n\
53 ~t users add users to the To: field\n\
54 ~u recall the previous line\n\
55 ~v edit message with the $visual editor\n\
56 ~w file write message to file\n\
57 ~x abort changes and quit editor\n\
59 . on a line by itself ends input\n");
62 be_snarf_data (FILE *f, char **buf, int *bufmax, int *buflen, int offset,
63 int bytes, int prefix)
65 char tmp[HUGE_STRING];
67 int tmplen = sizeof (tmp);
69 tmp[sizeof (tmp) - 1] = 0;
72 strfcpy (tmp, NONULL(Prefix), sizeof (tmp));
73 tmplen = mutt_strlen (tmp);
75 tmplen = sizeof (tmp) - tmplen;
81 if (fgets (p, tmplen - 1, f) == NULL) break;
82 bytes -= mutt_strlen (p);
83 if (*bufmax == *buflen)
84 safe_realloc (&buf, sizeof (char *) * (*bufmax += 25));
85 buf[(*buflen)++] = safe_strdup (tmp);
87 if (buf && *bufmax == *buflen) { /* Do not smash memory past buf */
88 safe_realloc (&buf, sizeof (char *) * (++*bufmax));
90 if (buf) buf[*buflen] = NULL;
95 be_snarf_file (const char *path, char **buf, int *max, int *len, int verbose)
98 char tmp[LONG_STRING];
101 if ((f = fopen (path, "r")))
103 fstat (fileno (f), &sb);
104 buf = be_snarf_data (f, buf, max, len, 0, sb.st_size, 0);
107 snprintf(tmp, sizeof(tmp), "\"%s\" %lu bytes\n", path, (unsigned long) sb.st_size);
114 snprintf(tmp, sizeof(tmp), "%s: %s\n", path, strerror(errno));
120 static int be_barf_file (const char *path, char **buf, int buflen)
125 if ((f = safe_fopen (path, "w")) == NULL) /* __FOPEN_CHECKED__ */
127 addstr (strerror (errno));
131 for (i = 0; i < buflen; i++) fputs (buf[i], f);
132 if (fclose (f) == 0) return 0;
133 printw ("fclose: %s\n", strerror (errno));
137 static void be_free_memory (char **buf, int buflen)
146 be_include_messages (char *msg, char **buf, int *bufmax, int *buflen,
147 int pfx, int inc_hdrs)
149 int offset, bytes, n;
150 char tmp[LONG_STRING];
152 while ((msg = strtok (msg, " ,")) != NULL)
155 if (n > 0 && n <= Context->msgcount)
159 /* add the attribution */
162 mutt_make_string (tmp, sizeof (tmp) - 1, Attribution, Context, Context->hdrs[n]);
163 strcat (tmp, "\n"); /* __STRCAT_CHECKED__ */
166 if (*bufmax == *buflen)
167 safe_realloc ( &buf, sizeof (char *) * (*bufmax += 25));
168 buf[(*buflen)++] = safe_strdup (tmp);
170 bytes = Context->hdrs[n]->content->length;
173 offset = Context->hdrs[n]->offset;
174 bytes += Context->hdrs[n]->content->offset - offset;
177 offset = Context->hdrs[n]->content->offset;
178 buf = be_snarf_data (Context->fp, buf, bufmax, buflen, offset, bytes,
181 if (*bufmax == *buflen)
182 safe_realloc (&buf, sizeof (char *) * (*bufmax += 25));
183 buf[(*buflen)++] = safe_strdup ("\n");
186 printw (_("%d: invalid message number.\n"), n);
192 static void be_print_header (ENVELOPE *env)
194 char tmp[HUGE_STRING];
200 rfc822_write_address (tmp, sizeof (tmp), env->to, 1);
208 rfc822_write_address (tmp, sizeof (tmp), env->cc, 1);
216 rfc822_write_address (tmp, sizeof (tmp), env->bcc, 1);
222 addstr ("Subject: ");
223 addstr (env->subject);
230 * force override the $ask* vars (used for the ~h command)
232 static void be_edit_header (ENVELOPE *e, int force)
234 char tmp[HUGE_STRING];
240 mutt_addrlist_to_local (e->to);
241 rfc822_write_address (tmp, sizeof (tmp), e->to, 0);
244 if (mutt_enter_string (tmp, sizeof (tmp), LINES-1, 4, 0) == 0)
246 rfc822_free_address (&e->to);
247 e->to = mutt_parse_adrlist (e->to, tmp);
248 e->to = mutt_expand_aliases (e->to);
249 mutt_addrlist_to_idna (e->to, NULL); /* XXX - IDNA error reporting? */
251 rfc822_write_address (tmp, sizeof (tmp), e->to, 1);
252 mvaddstr (LINES - 1, 4, tmp);
257 mutt_addrlist_to_idna (e->to, NULL); /* XXX - IDNA error reporting? */
262 if (!e->subject || force)
264 addstr ("Subject: ");
265 strfcpy (tmp, e->subject ? e->subject: "", sizeof (tmp));
266 if (mutt_enter_string (tmp, sizeof (tmp), LINES-1, 9, 0) == 0)
267 mutt_str_replace (&e->subject, tmp);
271 if ((!e->cc && option (OPTASKCC)) || force)
275 mutt_addrlist_to_local (e->cc);
276 rfc822_write_address (tmp, sizeof (tmp), e->cc, 0);
277 if (mutt_enter_string (tmp, sizeof (tmp), LINES-1, 4, 0) == 0)
279 rfc822_free_address (&e->cc);
280 e->cc = mutt_parse_adrlist (e->cc, tmp);
281 e->cc = mutt_expand_aliases (e->cc);
283 mutt_addrlist_to_idna (e->cc, NULL);
284 rfc822_write_address (tmp, sizeof (tmp), e->cc, 1);
285 mvaddstr (LINES - 1, 4, tmp);
288 mutt_addrlist_to_idna (e->cc, NULL);
292 if (option (OPTASKBCC) || force)
296 mutt_addrlist_to_local (e->bcc);
297 rfc822_write_address (tmp, sizeof (tmp), e->bcc, 0);
298 if (mutt_enter_string (tmp, sizeof (tmp), LINES-1, 5, 0) == 0)
300 rfc822_free_address (&e->bcc);
301 e->bcc = mutt_parse_adrlist (e->bcc, tmp);
302 e->bcc = mutt_expand_aliases (e->bcc);
303 mutt_addrlist_to_idna (e->bcc, NULL);
305 rfc822_write_address (tmp, sizeof (tmp), e->bcc, 1);
306 mvaddstr (LINES - 1, 5, tmp);
309 mutt_addrlist_to_idna (e->bcc, NULL);
314 int mutt_builtin_editor (const char *path, HEADER *msg, HEADER *cur)
317 int bufmax = 0, buflen = 0;
318 char tmp[LONG_STRING];
324 scrollok (stdscr, TRUE);
326 be_edit_header (msg->env, 0);
328 addstr (_("(End message with a . on a line by itself)\n"));
330 buf = be_snarf_file (path, buf, &bufmax, &buflen, 0);
335 if (mutt_enter_string (tmp, sizeof (tmp), LINES-1, 0, 0) == -1)
342 if (EscChar && tmp[0] == EscChar[0] && tmp[1] != EscChar[0])
344 /* remove trailing whitespace from the line */
345 p = tmp + mutt_strlen (tmp) - 1;
346 while (p >= tmp && ISSPACE (*p))
355 addstr (_(EditorHelp));
358 msg->env->bcc = mutt_parse_adrlist (msg->env->bcc, p);
359 msg->env->bcc = mutt_expand_aliases (msg->env->bcc);
362 msg->env->cc = mutt_parse_adrlist (msg->env->cc, p);
363 msg->env->cc = mutt_expand_aliases (msg->env->cc);
366 be_edit_header (msg->env, 1);
376 /* include the current message */
377 p = tmp + mutt_strlen (tmp) + 1;
378 snprintf (tmp + mutt_strlen (tmp), sizeof (tmp) - mutt_strlen (tmp), " %d",
381 buf = be_include_messages (p, buf, &bufmax, &buflen,
382 (ascii_tolower (tmp[1]) == 'm'),
383 (ascii_isupper ((unsigned char) tmp[1])));
386 addstr (_("No mailbox.\n"));
390 addstr (_("Message contains:\n"));
391 be_print_header (msg->env);
392 for (i = 0; i < buflen; i++)
394 addstr (_("(continue)\n"));
402 strncpy(tmp, p, sizeof(tmp));
403 mutt_expand_path(tmp, sizeof(tmp));
404 buf = be_snarf_file (tmp, buf, &bufmax, &buflen, 1);
407 addstr (_("missing filename.\n"));
410 mutt_str_replace (&msg->env->subject, p);
413 msg->env->to = rfc822_parse_adrlist (msg->env->to, p);
414 msg->env->to = mutt_expand_aliases (msg->env->to);
420 strfcpy (tmp, buf[buflen], sizeof (tmp));
421 tmp[mutt_strlen (tmp)-1] = 0;
427 addstr (_("No lines in message.\n"));
432 if (be_barf_file (path, buf, buflen) == 0)
435 be_free_memory (buf, buflen);
439 if (option (OPTEDITHDRS))
441 mutt_env_to_local (msg->env);
442 mutt_edit_headers (NONULL(Visual), path, msg, NULL, 0);
443 if (mutt_env_to_idna (msg->env, &tag, &err))
444 printw (_("Bad IDN in %s: '%s'\n"), tag, err);
447 mutt_edit_file (NONULL(Visual), path);
449 buf = be_snarf_file (path, buf, &bufmax, &buflen, 0);
451 addstr (_("(continue)\n"));
455 be_barf_file (*p ? p : path, buf, buflen);
462 printw (_("%s: unknown editor command (~? for help)\n"), tmp);
466 else if (mutt_strcmp (".", tmp) == 0)
470 safe_strcat (tmp, sizeof (tmp), "\n");
471 if (buflen == bufmax)
472 safe_realloc (&buf, sizeof (char *) * (bufmax += 25));
473 buf[buflen++] = safe_strdup (tmp[1] == '~' ? tmp + 1 : tmp);
479 if (!abort) be_barf_file (path, buf, buflen);
480 be_free_memory (buf, buflen);
482 return (abort ? -1 : 0);