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. */
22 #include "mutt_curses.h"
23 #include "mutt_idna.h"
34 * SLcurses_waddnstr() can't take a "const char *", so this is only
35 * declared "static" (sigh)
37 static char* EditorHelp = N_("\
38 ~~ insert a line begining with a single ~\n\
39 ~b users add users to the Bcc: field\n\
40 ~c users add users to the Cc: field\n\
41 ~f messages include messages\n\
42 ~F messages same as ~f, except also include headers\n\
43 ~h edit the message header\n\
44 ~m messages include and quote messages\n\
45 ~M messages same as ~m, except include headers\n\
46 ~p print the message\n\
47 ~q write file and quit editor\n\
48 ~r file read a file into the editor\n\
49 ~t users add users to the To: field\n\
50 ~u recall the previous line\n\
51 ~v edit message with the $visual editor\n\
52 ~w file write message to file\n\
53 ~x abort changes and quit editor\n\
55 . on a line by itself ends input\n");
58 be_snarf_data (FILE *f, char **buf, int *bufmax, int *buflen, int offset,
59 int bytes, int prefix)
61 char tmp[HUGE_STRING];
63 int tmplen = sizeof (tmp);
65 tmp[sizeof (tmp) - 1] = 0;
68 strfcpy (tmp, NONULL(Prefix), sizeof (tmp));
69 tmplen = mutt_strlen (tmp);
71 tmplen = sizeof (tmp) - tmplen;
77 if (fgets (p, tmplen - 1, f) == NULL) break;
78 bytes -= mutt_strlen (p);
79 if (*bufmax == *buflen)
80 safe_realloc (&buf, sizeof (char *) * (*bufmax += 25));
81 buf[(*buflen)++] = safe_strdup (tmp);
83 if (buf && *bufmax == *buflen) { /* Do not smash memory past buf */
84 safe_realloc (&buf, sizeof (char *) * (++*bufmax));
86 if (buf) buf[*buflen] = NULL;
91 be_snarf_file (const char *path, char **buf, int *max, int *len, int verbose)
94 char tmp[LONG_STRING];
97 if ((f = fopen (path, "r")))
99 fstat (fileno (f), &sb);
100 buf = be_snarf_data (f, buf, max, len, 0, sb.st_size, 0);
103 snprintf(tmp, sizeof(tmp), "\"%s\" %lu bytes\n", path, (unsigned long) sb.st_size);
110 snprintf(tmp, sizeof(tmp), "%s: %s\n", path, strerror(errno));
116 static int be_barf_file (const char *path, char **buf, int buflen)
121 if ((f = fopen (path, "w")) == NULL) /* __FOPEN_CHECKED__ */
123 addstr (strerror (errno));
127 for (i = 0; i < buflen; i++) fputs (buf[i], f);
128 if (fclose (f) == 0) return 0;
129 printw ("fclose: %s\n", strerror (errno));
133 static void be_free_memory (char **buf, int buflen)
142 be_include_messages (char *msg, char **buf, int *bufmax, int *buflen,
143 int pfx, int inc_hdrs)
145 int offset, bytes, n;
146 char tmp[LONG_STRING];
148 while ((msg = strtok (msg, " ,")) != NULL)
151 if (n > 0 && n <= Context->msgcount)
155 /* add the attribution */
158 mutt_make_string (tmp, sizeof (tmp) - 1, Attribution, Context, Context->hdrs[n]);
159 strcat (tmp, "\n"); /* __STRCAT_CHECKED__ */
162 if (*bufmax == *buflen)
163 safe_realloc ( &buf, sizeof (char *) * (*bufmax += 25));
164 buf[(*buflen)++] = safe_strdup (tmp);
166 bytes = Context->hdrs[n]->content->length;
169 offset = Context->hdrs[n]->offset;
170 bytes += Context->hdrs[n]->content->offset - offset;
173 offset = Context->hdrs[n]->content->offset;
174 buf = be_snarf_data (Context->fp, buf, bufmax, buflen, offset, bytes,
177 if (*bufmax == *buflen)
178 safe_realloc (&buf, sizeof (char *) * (*bufmax += 25));
179 buf[(*buflen)++] = safe_strdup ("\n");
182 printw (_("%d: invalid message number.\n"), n);
188 static void be_print_header (ENVELOPE *env)
190 char tmp[HUGE_STRING];
196 rfc822_write_address (tmp, sizeof (tmp), env->to, 1);
204 rfc822_write_address (tmp, sizeof (tmp), env->cc, 1);
212 rfc822_write_address (tmp, sizeof (tmp), env->bcc, 1);
218 addstr ("Subject: ");
219 addstr (env->subject);
226 * force override the $ask* vars (used for the ~h command)
228 static void be_edit_header (ENVELOPE *e, int force)
230 char tmp[HUGE_STRING];
236 mutt_addrlist_to_local (e->to);
237 rfc822_write_address (tmp, sizeof (tmp), e->to, 0);
240 if (mutt_enter_string (tmp, sizeof (tmp), LINES-1, 4, 0) == 0)
242 rfc822_free_address (&e->to);
243 e->to = mutt_parse_adrlist (e->to, tmp);
244 e->to = mutt_expand_aliases (e->to);
245 mutt_addrlist_to_idna (e->to, NULL); /* XXX - IDNA error reporting? */
247 rfc822_write_address (tmp, sizeof (tmp), e->to, 1);
248 mvaddstr (LINES - 1, 4, tmp);
253 mutt_addrlist_to_idna (e->to, NULL); /* XXX - IDNA error reporting? */
258 if (!e->subject || force)
260 addstr ("Subject: ");
261 strfcpy (tmp, e->subject ? e->subject: "", sizeof (tmp));
262 if (mutt_enter_string (tmp, sizeof (tmp), LINES-1, 9, 0) == 0)
263 mutt_str_replace (&e->subject, tmp);
267 if ((!e->cc && option (OPTASKCC)) || force)
271 mutt_addrlist_to_local (e->cc);
272 rfc822_write_address (tmp, sizeof (tmp), e->cc, 0);
273 if (mutt_enter_string (tmp, sizeof (tmp), LINES-1, 4, 0) == 0)
275 rfc822_free_address (&e->cc);
276 e->cc = mutt_parse_adrlist (e->cc, tmp);
277 e->cc = mutt_expand_aliases (e->cc);
279 mutt_addrlist_to_idna (e->cc, NULL);
280 rfc822_write_address (tmp, sizeof (tmp), e->cc, 1);
281 mvaddstr (LINES - 1, 4, tmp);
284 mutt_addrlist_to_idna (e->cc, NULL);
288 if (option (OPTASKBCC) || force)
292 mutt_addrlist_to_local (e->bcc);
293 rfc822_write_address (tmp, sizeof (tmp), e->bcc, 0);
294 if (mutt_enter_string (tmp, sizeof (tmp), LINES-1, 5, 0) == 0)
296 rfc822_free_address (&e->bcc);
297 e->bcc = mutt_parse_adrlist (e->bcc, tmp);
298 e->bcc = mutt_expand_aliases (e->bcc);
299 mutt_addrlist_to_idna (e->bcc, NULL);
301 rfc822_write_address (tmp, sizeof (tmp), e->bcc, 1);
302 mvaddstr (LINES - 1, 5, tmp);
305 mutt_addrlist_to_idna (e->bcc, NULL);
310 int mutt_builtin_editor (const char *path, HEADER *msg, HEADER *cur)
313 int bufmax = 0, buflen = 0;
314 char tmp[LONG_STRING];
320 scrollok (stdscr, TRUE);
322 be_edit_header (msg->env, 0);
324 addstr (_("(End message with a . on a line by itself)\n"));
326 buf = be_snarf_file (path, buf, &bufmax, &buflen, 0);
331 if (mutt_enter_string (tmp, sizeof (tmp), LINES-1, 0, 0) == -1)
338 if (EscChar && tmp[0] == EscChar[0] && tmp[1] != EscChar[0])
340 /* remove trailing whitespace from the line */
341 p = tmp + mutt_strlen (tmp) - 1;
342 while (p >= tmp && ISSPACE (*p))
351 addstr (_(EditorHelp));
354 msg->env->bcc = mutt_parse_adrlist (msg->env->bcc, p);
355 msg->env->bcc = mutt_expand_aliases (msg->env->bcc);
358 msg->env->cc = mutt_parse_adrlist (msg->env->cc, p);
359 msg->env->cc = mutt_expand_aliases (msg->env->cc);
362 be_edit_header (msg->env, 1);
372 /* include the current message */
373 p = tmp + mutt_strlen (tmp) + 1;
374 snprintf (tmp + mutt_strlen (tmp), sizeof (tmp) - mutt_strlen (tmp), " %d",
377 buf = be_include_messages (p, buf, &bufmax, &buflen,
378 (ascii_tolower (tmp[1]) == 'm'),
379 (ascii_isupper ((unsigned char) tmp[1])));
382 addstr (_("No mailbox.\n"));
386 addstr (_("Message contains:\n"));
387 be_print_header (msg->env);
388 for (i = 0; i < buflen; i++)
390 addstr (_("(continue)\n"));
398 strncpy(tmp, p, sizeof(tmp));
399 mutt_expand_path(tmp, sizeof(tmp));
400 buf = be_snarf_file (tmp, buf, &bufmax, &buflen, 1);
403 addstr (_("missing filename.\n"));
406 mutt_str_replace (&msg->env->subject, p);
409 msg->env->to = rfc822_parse_adrlist (msg->env->to, p);
410 msg->env->to = mutt_expand_aliases (msg->env->to);
416 strfcpy (tmp, buf[buflen], sizeof (tmp));
417 tmp[mutt_strlen (tmp)-1] = 0;
423 addstr (_("No lines in message.\n"));
428 if (be_barf_file (path, buf, buflen) == 0)
431 be_free_memory (buf, buflen);
435 if (option (OPTEDITHDRS))
437 mutt_env_to_local (msg->env);
438 mutt_edit_headers (NONULL(Visual), path, msg, NULL, 0);
439 if (mutt_env_to_idna (msg->env, &tag, &err))
440 printw (_("Bad IDN in %s: '%s'\n"), tag, err);
443 mutt_edit_file (NONULL(Visual), path);
445 buf = be_snarf_file (path, buf, &bufmax, &buflen, 0);
447 addstr (_("(continue)\n"));
451 be_barf_file (*p ? p : path, buf, buflen);
458 printw (_("%s: unknown editor command (~? for help)\n"), tmp);
462 else if (mutt_strcmp (".", tmp) == 0)
466 strncat (tmp, "\n", sizeof(tmp)); tmp[sizeof(tmp) - 1] = '\0';
467 if (buflen == bufmax)
468 safe_realloc (&buf, sizeof (char *) * (bufmax += 25));
469 buf[buflen++] = safe_strdup (tmp[1] == '~' ? tmp + 1 : tmp);
475 if (!abort) be_barf_file (path, buf, buflen);
476 be_free_memory (buf, buflen);
478 return (abort ? -1 : 0);