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");
61 static char **be_snarf_data (FILE * f, char **buf, int *bufmax, int *buflen,
62 int offset, int bytes, int prefix)
64 char tmp[HUGE_STRING];
66 int tmplen = sizeof (tmp);
68 tmp[sizeof (tmp) - 1] = 0;
70 strfcpy (tmp, NONULL (Prefix), sizeof (tmp));
71 tmplen = mutt_strlen (tmp);
73 tmplen = sizeof (tmp) - tmplen;
78 if (fgets (p, tmplen - 1, f) == NULL)
80 bytes -= mutt_strlen (p);
81 if (*bufmax == *buflen)
82 safe_realloc (&buf, sizeof (char *) * (*bufmax += 25));
83 buf[(*buflen)++] = safe_strdup (tmp);
85 if (buf && *bufmax == *buflen) { /* Do not smash memory past buf */
86 safe_realloc (&buf, sizeof (char *) * (++*bufmax));
93 static char **be_snarf_file (const char *path, char **buf, int *max, int *len,
97 char tmp[LONG_STRING];
100 if ((f = fopen (path, "r"))) {
101 fstat (fileno (f), &sb);
102 buf = be_snarf_data (f, buf, max, len, 0, sb.st_size, 0);
104 snprintf (tmp, sizeof (tmp), "\"%s\" %lu bytes\n", path,
105 (unsigned long) sb.st_size);
111 snprintf (tmp, sizeof (tmp), "%s: %s\n", path, strerror (errno));
117 static int be_barf_file (const char *path, char **buf, int buflen)
122 if ((f = safe_fopen (path, "w")) == NULL) { /* __FOPEN_CHECKED__ */
123 addstr (strerror (errno));
127 for (i = 0; i < buflen; i++)
131 printw ("fclose: %s\n", strerror (errno));
135 static void be_free_memory (char **buf, int buflen)
143 static char **be_include_messages (char *msg, char **buf, int *bufmax,
144 int *buflen, int pfx, int inc_hdrs)
146 int offset, bytes, n;
147 char tmp[LONG_STRING];
149 while ((msg = strtok (msg, " ,")) != NULL) {
151 if (n > 0 && n <= Context->msgcount) {
154 /* add the attribution */
156 mutt_make_string (tmp, sizeof (tmp) - 1, Attribution, Context,
158 strcat (tmp, "\n"); /* __STRCAT_CHECKED__ */
161 if (*bufmax == *buflen)
162 safe_realloc (&buf, sizeof (char *) * (*bufmax += 25));
163 buf[(*buflen)++] = safe_strdup (tmp);
165 bytes = Context->hdrs[n]->content->length;
167 offset = Context->hdrs[n]->offset;
168 bytes += Context->hdrs[n]->content->offset - offset;
171 offset = Context->hdrs[n]->content->offset;
172 buf = be_snarf_data (Context->fp, buf, bufmax, buflen, offset, bytes,
175 if (*bufmax == *buflen)
176 safe_realloc (&buf, sizeof (char *) * (*bufmax += 25));
177 buf[(*buflen)++] = safe_strdup ("\n");
180 printw (_("%d: invalid message number.\n"), n);
186 static void be_print_header (ENVELOPE * env)
188 char tmp[HUGE_STRING];
193 rfc822_write_address (tmp, sizeof (tmp), env->to, 1);
200 rfc822_write_address (tmp, sizeof (tmp), env->cc, 1);
207 rfc822_write_address (tmp, sizeof (tmp), env->bcc, 1);
212 addstr ("Subject: ");
213 addstr (env->subject);
220 * force override the $ask* vars (used for the ~h command)
222 static void be_edit_header (ENVELOPE * e, int force)
224 char tmp[HUGE_STRING];
230 mutt_addrlist_to_local (e->to);
231 rfc822_write_address (tmp, sizeof (tmp), e->to, 0);
232 if (!e->to || force) {
233 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 4, 0) == 0) {
234 rfc822_free_address (&e->to);
235 e->to = mutt_parse_adrlist (e->to, tmp);
236 e->to = mutt_expand_aliases (e->to);
237 mutt_addrlist_to_idna (e->to, NULL); /* XXX - IDNA error reporting? */
239 rfc822_write_address (tmp, sizeof (tmp), e->to, 1);
240 mvaddstr (LINES - 1, 4, tmp);
244 mutt_addrlist_to_idna (e->to, NULL); /* XXX - IDNA error reporting? */
249 if (!e->subject || force) {
250 addstr ("Subject: ");
251 strfcpy (tmp, e->subject ? e->subject : "", sizeof (tmp));
252 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 9, 0) == 0)
253 mutt_str_replace (&e->subject, tmp);
257 if ((!e->cc && option (OPTASKCC)) || force) {
260 mutt_addrlist_to_local (e->cc);
261 rfc822_write_address (tmp, sizeof (tmp), e->cc, 0);
262 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 4, 0) == 0) {
263 rfc822_free_address (&e->cc);
264 e->cc = mutt_parse_adrlist (e->cc, tmp);
265 e->cc = mutt_expand_aliases (e->cc);
267 mutt_addrlist_to_idna (e->cc, NULL);
268 rfc822_write_address (tmp, sizeof (tmp), e->cc, 1);
269 mvaddstr (LINES - 1, 4, tmp);
272 mutt_addrlist_to_idna (e->cc, NULL);
276 if (option (OPTASKBCC) || force) {
279 mutt_addrlist_to_local (e->bcc);
280 rfc822_write_address (tmp, sizeof (tmp), e->bcc, 0);
281 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 5, 0) == 0) {
282 rfc822_free_address (&e->bcc);
283 e->bcc = mutt_parse_adrlist (e->bcc, tmp);
284 e->bcc = mutt_expand_aliases (e->bcc);
285 mutt_addrlist_to_idna (e->bcc, NULL);
287 rfc822_write_address (tmp, sizeof (tmp), e->bcc, 1);
288 mvaddstr (LINES - 1, 5, tmp);
291 mutt_addrlist_to_idna (e->bcc, NULL);
296 int mutt_builtin_editor (const char *path, HEADER * msg, HEADER * cur)
299 int bufmax = 0, buflen = 0;
300 char tmp[LONG_STRING];
306 scrollok (stdscr, TRUE);
308 be_edit_header (msg->env, 0);
310 addstr (_("(End message with a . on a line by itself)\n"));
312 buf = be_snarf_file (path, buf, &bufmax, &buflen, 0);
316 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 0, 0) == -1) {
322 if (EscChar && tmp[0] == EscChar[0] && tmp[1] != EscChar[0]) {
323 /* remove trailing whitespace from the line */
324 p = tmp + mutt_strlen (tmp) - 1;
325 while (p >= tmp && ISSPACE (*p))
333 addstr (_(EditorHelp));
336 msg->env->bcc = mutt_parse_adrlist (msg->env->bcc, p);
337 msg->env->bcc = mutt_expand_aliases (msg->env->bcc);
340 msg->env->cc = mutt_parse_adrlist (msg->env->cc, p);
341 msg->env->cc = mutt_expand_aliases (msg->env->cc);
344 be_edit_header (msg->env, 1);
352 /* include the current message */
353 p = tmp + mutt_strlen (tmp) + 1;
354 snprintf (tmp + mutt_strlen (tmp),
355 sizeof (tmp) - mutt_strlen (tmp), " %d",
358 buf = be_include_messages (p, buf, &bufmax, &buflen,
359 (ascii_tolower (tmp[1]) == 'm'),
361 ((unsigned char) tmp[1])));
364 addstr (_("No mailbox.\n"));
368 addstr (_("Message contains:\n"));
369 be_print_header (msg->env);
370 for (i = 0; i < buflen; i++)
372 addstr (_("(continue)\n"));
379 strncpy (tmp, p, sizeof (tmp));
380 mutt_expand_path (tmp, sizeof (tmp));
381 buf = be_snarf_file (tmp, buf, &bufmax, &buflen, 1);
384 addstr (_("missing filename.\n"));
387 mutt_str_replace (&msg->env->subject, p);
390 msg->env->to = rfc822_parse_adrlist (msg->env->to, p);
391 msg->env->to = mutt_expand_aliases (msg->env->to);
396 strfcpy (tmp, buf[buflen], sizeof (tmp));
397 tmp[mutt_strlen (tmp) - 1] = 0;
403 addstr (_("No lines in message.\n"));
408 if (be_barf_file (path, buf, buflen) == 0) {
411 be_free_memory (buf, buflen);
415 if (option (OPTEDITHDRS)) {
416 mutt_env_to_local (msg->env);
417 mutt_edit_headers (NONULL (Visual), path, msg, NULL, 0);
418 if (mutt_env_to_idna (msg->env, &tag, &err))
419 printw (_("Bad IDN in %s: '%s'\n"), tag, err);
422 mutt_edit_file (NONULL (Visual), path);
424 buf = be_snarf_file (path, buf, &bufmax, &buflen, 0);
426 addstr (_("(continue)\n"));
430 be_barf_file (*p ? p : path, buf, buflen);
437 printw (_("%s: unknown editor command (~? for help)\n"), tmp);
441 else if (mutt_strcmp (".", tmp) == 0)
444 safe_strcat (tmp, sizeof (tmp), "\n");
445 if (buflen == bufmax)
446 safe_realloc (&buf, sizeof (char *) * (bufmax += 25));
447 buf[buflen++] = safe_strdup (tmp[1] == '~' ? tmp + 1 : tmp);
454 be_barf_file (path, buf, buflen);
455 be_free_memory (buf, buflen);
457 return (abort ? -1 : 0);