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. */
19 #include "mutt_curses.h"
20 #include "mutt_idna.h"
35 * SLcurses_waddnstr() can't take a "const char *", so this is only
36 * declared "static" (sigh)
38 static char *EditorHelp = N_("\
39 ~~ insert a line begining with a single ~\n\
40 ~b users add users to the Bcc: field\n\
41 ~c users add users to the Cc: field\n\
42 ~f messages include messages\n\
43 ~F messages same as ~f, except also include headers\n\
44 ~h edit the message header\n\
45 ~m messages include and quote messages\n\
46 ~M messages same as ~m, except include headers\n\
47 ~p print the message\n\
48 ~q write file and quit editor\n\
49 ~r file read a file into the editor\n\
50 ~t users add users to the To: field\n\
51 ~u recall the previous line\n\
52 ~v edit message with the $visual editor\n\
53 ~w file write message to file\n\
54 ~x abort changes and quit editor\n\
56 . on a line by itself ends input\n");
58 static char **be_snarf_data (FILE * f, char **buf, int *bufmax, int *buflen,
59 int offset, int bytes, int prefix)
61 char tmp[HUGE_STRING];
63 int tmplen = sizeof (tmp);
65 tmp[sizeof (tmp) - 1] = 0;
67 strfcpy (tmp, NONULL (Prefix), sizeof (tmp));
68 tmplen = str_len (tmp);
70 tmplen = sizeof (tmp) - tmplen;
75 if (fgets (p, tmplen - 1, f) == NULL)
78 if (*bufmax == *buflen)
79 mem_realloc (&buf, sizeof (char *) * (*bufmax += 25));
80 buf[(*buflen)++] = str_dup (tmp);
82 if (buf && *bufmax == *buflen) { /* Do not smash memory past buf */
83 mem_realloc (&buf, sizeof (char *) * (++*bufmax));
90 static char **be_snarf_file (const char *path, char **buf, int *max, int *len,
94 char tmp[LONG_STRING];
97 if ((f = fopen (path, "r"))) {
98 fstat (fileno (f), &sb);
99 buf = be_snarf_data (f, buf, max, len, 0, sb.st_size, 0);
101 snprintf (tmp, sizeof (tmp), "\"%s\" %lu bytes\n", path,
102 (unsigned long) sb.st_size);
108 snprintf (tmp, sizeof (tmp), "%s: %s\n", path, strerror (errno));
114 static int be_barf_file (const char *path, char **buf, int buflen)
119 if ((f = safe_fopen (path, "w")) == NULL) { /* __FOPEN_CHECKED__ */
120 addstr (strerror (errno));
124 for (i = 0; i < buflen; i++)
128 printw ("fclose: %s\n", strerror (errno));
132 static void be_free_memory (char **buf, int buflen)
135 mem_free (&buf[buflen]);
140 static char **be_include_messages (char *msg, char **buf, int *bufmax,
141 int *buflen, int pfx, int inc_hdrs)
143 int offset, bytes, n;
144 char tmp[LONG_STRING];
146 while ((msg = strtok (msg, " ,")) != NULL) {
148 if (n > 0 && n <= Context->msgcount) {
151 /* add the attribution */
153 mutt_make_string (tmp, sizeof (tmp) - 1, Attribution, Context,
155 strcat (tmp, "\n"); /* __STRCAT_CHECKED__ */
158 if (*bufmax == *buflen)
159 mem_realloc (&buf, sizeof (char *) * (*bufmax += 25));
160 buf[(*buflen)++] = str_dup (tmp);
162 bytes = Context->hdrs[n]->content->length;
164 offset = Context->hdrs[n]->offset;
165 bytes += Context->hdrs[n]->content->offset - offset;
168 offset = Context->hdrs[n]->content->offset;
169 buf = be_snarf_data (Context->fp, buf, bufmax, buflen, offset, bytes,
172 if (*bufmax == *buflen)
173 mem_realloc (&buf, sizeof (char *) * (*bufmax += 25));
174 buf[(*buflen)++] = str_dup ("\n");
177 printw (_("%d: invalid message number.\n"), n);
183 static void be_print_header (ENVELOPE * env)
185 char tmp[HUGE_STRING];
190 rfc822_write_address (tmp, sizeof (tmp), env->to, 1);
197 rfc822_write_address (tmp, sizeof (tmp), env->cc, 1);
204 rfc822_write_address (tmp, sizeof (tmp), env->bcc, 1);
209 addstr ("Subject: ");
210 addstr (env->subject);
217 * force override the $ask* vars (used for the ~h command)
219 static void be_edit_header (ENVELOPE * e, int force)
221 char tmp[HUGE_STRING];
227 mutt_addrlist_to_local (e->to);
228 rfc822_write_address (tmp, sizeof (tmp), e->to, 0);
229 if (!e->to || force) {
230 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 4, 0) == 0) {
231 rfc822_free_address (&e->to);
232 e->to = mutt_parse_adrlist (e->to, tmp);
233 e->to = mutt_expand_aliases (e->to);
234 mutt_addrlist_to_idna (e->to, NULL); /* XXX - IDNA error reporting? */
236 rfc822_write_address (tmp, sizeof (tmp), e->to, 1);
237 mvaddstr (LINES - 1, 4, tmp);
241 mutt_addrlist_to_idna (e->to, NULL); /* XXX - IDNA error reporting? */
246 if (!e->subject || force) {
247 addstr ("Subject: ");
248 strfcpy (tmp, e->subject ? e->subject : "", sizeof (tmp));
249 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 9, 0) == 0)
250 str_replace (&e->subject, tmp);
254 if ((!e->cc && option (OPTASKCC)) || force) {
257 mutt_addrlist_to_local (e->cc);
258 rfc822_write_address (tmp, sizeof (tmp), e->cc, 0);
259 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 4, 0) == 0) {
260 rfc822_free_address (&e->cc);
261 e->cc = mutt_parse_adrlist (e->cc, tmp);
262 e->cc = mutt_expand_aliases (e->cc);
264 mutt_addrlist_to_idna (e->cc, NULL);
265 rfc822_write_address (tmp, sizeof (tmp), e->cc, 1);
266 mvaddstr (LINES - 1, 4, tmp);
269 mutt_addrlist_to_idna (e->cc, NULL);
273 if (option (OPTASKBCC) || force) {
276 mutt_addrlist_to_local (e->bcc);
277 rfc822_write_address (tmp, sizeof (tmp), e->bcc, 0);
278 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 5, 0) == 0) {
279 rfc822_free_address (&e->bcc);
280 e->bcc = mutt_parse_adrlist (e->bcc, tmp);
281 e->bcc = mutt_expand_aliases (e->bcc);
282 mutt_addrlist_to_idna (e->bcc, NULL);
284 rfc822_write_address (tmp, sizeof (tmp), e->bcc, 1);
285 mvaddstr (LINES - 1, 5, tmp);
288 mutt_addrlist_to_idna (e->bcc, NULL);
293 int mutt_builtin_editor (const char *path, HEADER * msg, HEADER * cur)
296 int bufmax = 0, buflen = 0;
297 char tmp[LONG_STRING];
303 scrollok (stdscr, TRUE);
305 be_edit_header (msg->env, 0);
307 addstr (_("(End message with a . on a line by itself)\n"));
309 buf = be_snarf_file (path, buf, &bufmax, &buflen, 0);
313 if (mutt_enter_string (tmp, sizeof (tmp), LINES - 1, 0, 0) == -1) {
319 if (EscChar && tmp[0] == EscChar[0] && tmp[1] != EscChar[0]) {
320 /* remove trailing whitespace from the line */
321 p = tmp + str_len (tmp) - 1;
322 while (p >= tmp && ISSPACE (*p))
330 addstr (_(EditorHelp));
333 msg->env->bcc = mutt_parse_adrlist (msg->env->bcc, p);
334 msg->env->bcc = mutt_expand_aliases (msg->env->bcc);
337 msg->env->cc = mutt_parse_adrlist (msg->env->cc, p);
338 msg->env->cc = mutt_expand_aliases (msg->env->cc);
341 be_edit_header (msg->env, 1);
349 /* include the current message */
350 p = tmp + str_len (tmp) + 1;
351 snprintf (tmp + str_len (tmp),
352 sizeof (tmp) - str_len (tmp), " %d",
355 buf = be_include_messages (p, buf, &bufmax, &buflen,
356 (ascii_tolower (tmp[1]) == 'm'),
358 ((unsigned char) tmp[1])));
361 addstr (_("No mailbox.\n"));
365 addstr (_("Message contains:\n"));
366 be_print_header (msg->env);
367 for (i = 0; i < buflen; i++)
369 addstr (_("(continue)\n"));
376 strncpy (tmp, p, sizeof (tmp));
377 mutt_expand_path (tmp, sizeof (tmp));
378 buf = be_snarf_file (tmp, buf, &bufmax, &buflen, 1);
381 addstr (_("missing filename.\n"));
384 str_replace (&msg->env->subject, p);
387 msg->env->to = rfc822_parse_adrlist (msg->env->to, p);
388 msg->env->to = mutt_expand_aliases (msg->env->to);
393 strfcpy (tmp, buf[buflen], sizeof (tmp));
394 tmp[str_len (tmp) - 1] = 0;
395 mem_free (&buf[buflen]);
400 addstr (_("No lines in message.\n"));
405 if (be_barf_file (path, buf, buflen) == 0) {
408 be_free_memory (buf, buflen);
412 if (option (OPTEDITHDRS)) {
413 mutt_env_to_local (msg->env);
414 mutt_edit_headers (NONULL (Visual), path, msg, NULL, 0);
415 if (mutt_env_to_idna (msg->env, &tag, &err))
416 printw (_("Bad IDN in %s: '%s'\n"), tag, err);
419 mutt_edit_file (NONULL (Visual), path);
421 buf = be_snarf_file (path, buf, &bufmax, &buflen, 0);
423 addstr (_("(continue)\n"));
427 be_barf_file (*p ? p : path, buf, buflen);
434 printw (_("%s: unknown editor command (~? for help)\n"), tmp);
438 else if (str_cmp (".", tmp) == 0)
441 str_cat (tmp, sizeof (tmp), "\n");
442 if (buflen == bufmax)
443 mem_realloc (&buf, sizeof (char *) * (bufmax += 25));
444 buf[buflen++] = str_dup (tmp[1] == '~' ? tmp + 1 : tmp);
451 be_barf_file (path, buf, buflen);
452 be_free_memory (buf, buflen);
454 return (abort ? -1 : 0);