simplify hooks code a lot.
[apps/madmutt.git] / compose.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 2004 g10 Code GmbH
5  *
6  * Parts were written/modified by:
7  * Nico Golde <nico@ngolde.de>
8  *
9  * This file is part of mutt-ng, see http://www.muttng.org/.
10  * It's licensed under the GNU General Public License,
11  * please see the file GPL in the top level source directory.
12  */
13
14 #include <lib-lib/lib-lib.h>
15
16 #include <lib-sys/unix.h>
17 #include <lib-mime/mime.h>
18 #include <lib-crypt/crypt.h>
19
20 #include <lib-ui/curses.h>
21 #include <lib-ui/enter.h>
22 #include <lib-ui/menu.h>
23 #include <lib-mx/mx.h>
24
25 #include "mutt.h"
26 #include "alias.h"
27 #include "mutt_idna.h"
28 #include "attach.h"
29 #include "recvattach.h"
30 #include "sort.h"
31 #include "charset.h"
32 #include "buffy.h"
33 #include "remailer.h"
34
35 #ifdef USE_NNTP
36 #include <nntp/nntp.h>
37 #endif
38
39 #define CHECK_COUNT \
40     if (idxlen == 0) {                             \
41         mutt_error _("There are no attachments."); \
42         break;                                     \
43     }
44
45 enum {
46   HDR_FROM = 1,
47   HDR_TO,
48   HDR_CC,
49   HDR_BCC,
50   HDR_SUBJECT,
51   HDR_REPLYTO,
52   HDR_FCC,
53   HDR_MIX,
54
55   HDR_CRYPT,
56   HDR_CRYPTINFO,
57
58 #ifdef USE_NNTP
59   HDR_NEWSGROUPS,
60   HDR_FOLLOWUPTO,
61   HDR_XCOMMENTTO,
62 #endif
63
64 #ifndef USE_NNTP
65   HDR_ATTACH = (HDR_FCC + 5)    /* where to start printing the attachments */
66 #else
67   HDR_ATTACH = (HDR_FCC + 7)
68 #endif
69 };
70
71 #define HDR_XOFFSET     14
72 #define TITLE_FMT       "%14s"        /* Used for Prompts, which are ASCII */
73 #define SW              (option(OPTMBOXPANE)?SidebarWidth:0)
74 #define W               (COLS - HDR_XOFFSET - SW)
75
76 static const char *Prompts[] = {
77     "From: ",
78     "To: ",
79     "Cc: ",
80     "Bcc: ",
81     "Subject: ",
82     "Reply-To: ",
83     "Fcc: ",
84 #ifdef USE_NNTP
85     "", "", "", "Newsgroups: ", "Followup-To: ", "X-Comment-To: "
86 #endif
87 };
88
89 static struct mapping_t ComposeHelp[] = {
90   {N_("Send"), OP_COMPOSE_SEND_MESSAGE},
91   {N_("Abort"), OP_EXIT},
92   {"To", OP_COMPOSE_EDIT_TO},
93   {"CC", OP_COMPOSE_EDIT_CC},
94   {"Subj", OP_COMPOSE_EDIT_SUBJECT},
95   {N_("Attach file"), OP_COMPOSE_ATTACH_FILE},
96   {N_("Descrip"), OP_COMPOSE_EDIT_DESCRIPTION},
97   {N_("Help"), OP_HELP},
98   {NULL, OP_NULL}
99 };
100
101 #ifdef USE_NNTP
102 static struct mapping_t ComposeNewsHelp[] = {
103   {N_("Send"), OP_COMPOSE_SEND_MESSAGE},
104   {N_("Abort"), OP_EXIT},
105   {"Newsgroups", OP_COMPOSE_EDIT_NEWSGROUPS},
106   {"Subj", OP_COMPOSE_EDIT_SUBJECT},
107   {N_("Attach file"), OP_COMPOSE_ATTACH_FILE},
108   {N_("Descrip"), OP_COMPOSE_EDIT_DESCRIPTION},
109   {N_("Help"), OP_HELP},
110   {NULL, OP_NULL}
111 };
112 #endif
113
114
115 static void snd_entry (char *b, ssize_t blen, MUTTMENU * menu, int num) {
116   m_strformat(b, blen, COLS - SW, AttachFormat, mutt_attach_fmt,
117               ((ATTACHPTR **)menu->data)[num],
118               M_FORMAT_STAT_FILE | (option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0));
119 }
120
121 static void redraw_crypt_lines (HEADER * msg)
122 {
123   int off = 0;
124
125   if (!msg->security)
126     mvaddstr (HDR_CRYPT, SW, "    Security: ");
127   else if (msg->security & APPLICATION_SMIME)
128     mvaddstr (HDR_CRYPT, SW, "      S/MIME: ");
129   else if (msg->security & APPLICATION_PGP)
130     mvaddstr (HDR_CRYPT, SW, "         PGP: ");
131
132   if ((msg->security & (ENCRYPT | SIGN)) == (ENCRYPT | SIGN))
133     addstr (_("Sign, Encrypt"));
134   else if (msg->security & ENCRYPT)
135     addstr (_("Encrypt"));
136   else if (msg->security & SIGN)
137     addstr (_("Sign"));
138   else
139     addstr (_("Clear"));
140
141   if ((msg->security & APPLICATION_PGP)
142       && (msg->security & (ENCRYPT | SIGN))) {
143     if ((msg->security & INLINE))
144       addstr (_(" (inline)"));
145     else
146       addstr (_(" (PGP/MIME)"));
147   }
148   clrtoeol ();
149
150   move (HDR_CRYPTINFO, SW);
151   clrtoeol ();
152   if (msg->security & APPLICATION_PGP && msg->security & SIGN)
153     printw ("%s%s", _("     sign as: "),
154             PgpSignAs ? PgpSignAs : _("<default>"));
155
156   if (msg->security & APPLICATION_SMIME && msg->security & SIGN) {
157     printw ("%s%s", _("     sign as: "),
158             SmimeDefaultKey ? SmimeDefaultKey : _("<default>"));
159   }
160
161   if ((msg->security & APPLICATION_SMIME)
162       && (msg->security & ENCRYPT)
163       && SmimeCryptAlg && *SmimeCryptAlg) {
164     mvprintw (HDR_CRYPTINFO, SW + 40, "%s%s", _("Encrypt with: "),
165               NONULL (SmimeCryptAlg));
166     off = 20;
167   }
168 }
169
170 static void redraw_mix_line (string_list_t * chain)
171 {
172   int c;
173   const char *t;
174
175   mvaddstr (HDR_MIX, SW, "         Mix: ");
176
177   if (!chain) {
178     addstr ("<no chain defined>");
179     clrtoeol ();
180     return;
181   }
182
183   for (c = 12; chain; chain = chain->next) {
184     t = chain->data;
185     if (t && t[0] == '0' && t[1] == '\0')
186       t = "<random>";
187
188     if (c + m_strlen(t) + 2 >= COLS - SW)
189       break;
190
191     addstr (NONULL (t));
192     if (chain->next)
193       addstr (", ");
194
195     c += m_strlen(t) + 2;
196   }
197 }
198
199 static int check_attachments (ATTACHPTR ** idx, short idxlen)
200 {
201   int i, r;
202   struct stat st;
203   char pretty[_POSIX_PATH_MAX], msg[_POSIX_PATH_MAX + STRING];
204
205   for (i = 0; i < idxlen; i++) {
206     m_strcpy(pretty, sizeof(pretty), idx[i]->content->filename);
207     if (stat (idx[i]->content->filename, &st) != 0) {
208       mutt_pretty_mailbox (pretty);
209       mutt_error (_("%s [#%d] no longer exists!"), pretty, i + 1);
210       return -1;
211     }
212
213     if (idx[i]->content->stamp < st.st_mtime) {
214       mutt_pretty_mailbox (pretty);
215       snprintf (msg, sizeof (msg), _("%s [#%d] modified. Update encoding?"),
216                 pretty, i + 1);
217
218       if ((r = mutt_yesorno (msg, M_YES)) == M_YES)
219         mutt_update_encoding (idx[i]->content);
220       else if (r == -1)
221         return -1;
222     }
223   }
224
225   return 0;
226 }
227
228 static void draw_envelope_addr (int line, address_t * addr)
229 {
230   char buf[STRING];
231
232   buf[0] = 0;
233   rfc822_addrcat(buf, sizeof (buf), addr, 1);
234   mvprintw (line, SW, TITLE_FMT, Prompts[line - 1]);
235   mutt_paddstr (W, buf);
236 }
237
238 static void draw_envelope (HEADER * msg, char *fcc)
239 {
240   draw_envelope_addr (HDR_FROM, msg->env->from);
241 #ifdef USE_NNTP
242   if (!option (OPTNEWSSEND)) {
243 #endif
244     draw_envelope_addr (HDR_TO, msg->env->to);
245     draw_envelope_addr (HDR_CC, msg->env->cc);
246     draw_envelope_addr (HDR_BCC, msg->env->bcc);
247 #ifdef USE_NNTP
248   } else {
249     mvprintw (HDR_TO, SW, TITLE_FMT, Prompts[HDR_NEWSGROUPS - 1]);
250     mutt_paddstr (W, NONULL (msg->env->newsgroups));
251     mvprintw (HDR_CC, SW, TITLE_FMT, Prompts[HDR_FOLLOWUPTO - 1]);
252     mutt_paddstr (W, NONULL (msg->env->followup_to));
253     if (option (OPTXCOMMENTTO)) {
254       mvprintw (HDR_BCC, 0, TITLE_FMT, Prompts[HDR_XCOMMENTTO - 1]);
255       mutt_paddstr (W, NONULL (msg->env->x_comment_to));
256     }
257   }
258 #endif
259   mvprintw (HDR_SUBJECT, SW, TITLE_FMT, Prompts[HDR_SUBJECT - 1]);
260   mutt_paddstr (W, NONULL (msg->env->subject));
261   draw_envelope_addr (HDR_REPLYTO, msg->env->reply_to);
262   mvprintw (HDR_FCC, SW, TITLE_FMT, Prompts[HDR_FCC - 1]);
263   mutt_paddstr (W, fcc);
264
265   redraw_crypt_lines (msg);
266   redraw_mix_line (msg->chain);
267
268   SETCOLOR (MT_COLOR_STATUS);
269   mvaddstr (HDR_ATTACH - 1, SW, _("-- Attachments"));
270   BKGDSET (MT_COLOR_STATUS);
271   clrtoeol ();
272
273   BKGDSET (MT_COLOR_NORMAL);
274   SETCOLOR (MT_COLOR_NORMAL);
275 }
276
277 static int edit_address_list (int line, address_t ** addr)
278 {
279   char buf[HUGE_STRING] = "";   /* needs to be large for alias expansion */
280   char *err = NULL;
281
282   mutt_addrlist_to_local (*addr);
283   rfc822_addrcat(buf, sizeof (buf), *addr, 0);
284   if (mutt_get_field (Prompts[line - 1], buf, sizeof (buf), M_ALIAS) == 0) {
285     address_list_wipe(addr);
286     *addr = mutt_parse_adrlist (*addr, buf);
287     *addr = mutt_expand_aliases (*addr);
288   }
289
290   if (option (OPTNEEDREDRAW)) {
291     unset_option (OPTNEEDREDRAW);
292     return REDRAW_FULL;
293   }
294
295   if (mutt_addrlist_to_idna (*addr, &err) != 0) {
296     mutt_error (_("Warning: '%s' is a bad IDN."), err);
297     mutt_refresh ();
298     p_delete(&err);
299   }
300
301   /* redraw the expanded list so the user can see the result */
302   buf[0] = 0;
303   rfc822_addrcat(buf, sizeof (buf), *addr, 1);
304   move (line, HDR_XOFFSET + SW);
305   mutt_paddstr (W, buf);
306
307   return 0;
308 }
309
310 static int delete_attachment (MUTTMENU * menu, short *idxlen, int x)
311 {
312   ATTACHPTR **idx = (ATTACHPTR **) menu->data;
313   int y;
314
315   menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
316
317   if (x == 0 && menu->max == 1) {
318     mutt_error _("You may not delete the only attachment.");
319
320     idx[x]->content->tagged = 0;
321     return -1;
322   }
323
324   for (y = 0; y < *idxlen; y++) {
325     if (idx[y]->content->next == idx[x]->content) {
326       idx[y]->content->next = idx[x]->content->next;
327       break;
328     }
329   }
330
331   idx[x]->content->next = NULL;
332   idx[x]->content->parts = NULL;
333   body_list_wipe(&(idx[x]->content));
334   p_delete(&idx[x]->tree);
335   p_delete(&idx[x]);
336   for (; x < *idxlen - 1; x++)
337     idx[x] = idx[x + 1];
338   menu->max = --(*idxlen);
339
340   return (0);
341 }
342
343 static void update_idx (MUTTMENU * menu, ATTACHPTR ** idx, short idxlen)
344 {
345   idx[idxlen]->level = (idxlen > 0) ? idx[idxlen - 1]->level : 0;
346   if (idxlen)
347     idx[idxlen - 1]->content->next = idx[idxlen]->content;
348   idx[idxlen]->content->aptr = idx[idxlen];
349   menu->current = idxlen++;
350   mutt_update_tree (idx, idxlen);
351   menu->max = idxlen;
352   return;
353 }
354
355
356 /*
357  * cum_attachs_size: Cumulative Attachments Size
358  *
359  * Returns the total number of bytes used by the attachments in the
360  * attachment list _after_ content-transfer-encodings have been
361  * applied.
362  *
363  */
364 static unsigned long cum_attachs_size(MUTTMENU * menu)
365 {
366   ssize_t s;
367   unsigned short i;
368   ATTACHPTR **idx = menu->data;
369   CONTENT *info;
370   BODY *b;
371
372   for (i = 0, s = 0; i < menu->max; i++) {
373     b = idx[i]->content;
374
375     if (!b->content)
376       b->content = mutt_get_content_info (b->filename, b);
377
378     if ((info = b->content)) {
379       switch (b->encoding) {
380       case ENCQUOTEDPRINTABLE:
381         s += 3 * (info->lobin + info->hibin) + info->ascii + info->crlf;
382         break;
383       case ENCBASE64:
384         s += (4 * (info->lobin + info->hibin + info->ascii + info->crlf)) / 3;
385         break;
386       default:
387         s += info->lobin + info->hibin + info->ascii + info->crlf;
388         break;
389       }
390     }
391   }
392
393   return s;
394 }
395
396 /*
397  * compose_format_str()
398  *
399  * %a = total number of attachments
400  * %h = hostname  [option]
401  * %l = approx. length of current message (in bytes)
402  * %v = Mutt version
403  *
404  * This function is similar to status_format_str().  Look at that function for
405  * help when modifying this function.
406  */
407 static void compose_status_line (char *buf, ssize_t buflen, MUTTMENU * menu,
408                                  const char *p);
409
410 static const char *compose_format_str (char *buf, ssize_t buflen, char op,
411                                        const char *src, const char *prefix,
412                                        const char *ifstr,
413                                        const char *elstr,
414                                        anytype data, format_flag flags)
415 {
416   char fmt[STRING], tmp[STRING];
417   int optional = (flags & M_FORMAT_OPTIONAL);
418   MUTTMENU *menu = data.ptr;
419
420   *buf = 0;
421   switch (op) {
422   case 'a':                    /* total number of attachments */
423     snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
424     snprintf (buf, buflen, fmt, menu->max);
425     break;
426
427   case 'h':                    /* hostname */
428     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
429     snprintf (buf, buflen, fmt, NONULL (Hostname));
430     break;
431
432   case 'l':                    /* approx length of current message in bytes */
433     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
434     mutt_pretty_size (tmp, sizeof (tmp), menu ? cum_attachs_size (menu) : 0);
435     snprintf (buf, buflen, fmt, tmp);
436     break;
437
438   case 'v':
439     m_strcpy(buf, buflen, mutt_make_version (0));
440     break;
441
442   case 0:
443     *buf = 0;
444     return (src);
445
446   default:
447     *buf = 0;
448     break;
449   }
450
451   if (flags & M_FORMAT_OPTIONAL)
452     compose_status_line(buf, buflen, menu, optional ? ifstr : elstr);
453
454   return (src);
455 }
456
457 static void compose_status_line (char *buf, ssize_t buflen, MUTTMENU * menu,
458                                  const char *p)
459 {
460     m_strformat(buf, buflen, COLS - SW, p, compose_format_str, menu, 0);
461 }
462
463 /* return values:
464  *
465  * 1    message should be postponed
466  * 0    normal exit
467  * -1   abort message
468  */
469 int mutt_compose_menu (HEADER * msg,    /* structure for new message */
470                        char *fcc,       /* where to save a copy of the message */
471                        ssize_t fcclen,
472                        HEADER * cur __attribute__ ((unused)))
473 {                               /* current message */
474   char helpstr[STRING];
475   char buf[LONG_STRING];
476   char fname[_POSIX_PATH_MAX];
477   MUTTMENU *menu;
478   ATTACHPTR **idx = NULL;
479   short idxlen = 0;
480   short idxmax = 0;
481   int i, closed = 0;
482   int r = -1;                   /* return value */
483   int op = 0;
484   int loop = 1;
485   int fccSet = 0;               /* has the user edited the Fcc: field ? */
486   CONTEXT *ctx = NULL, *this = NULL;
487
488   /* Sort, SortAux could be changed in mutt_index_menu() */
489   int oldSort, oldSortAux;
490   struct stat st;
491
492 #ifdef USE_NNTP
493   int news = 0;                 /* is it a news article ? */
494
495   if (option (OPTNEWSSEND))
496     news++;
497 #endif
498
499   mutt_attach_init (msg->content);
500   idx = mutt_gen_attach_list (msg->content, -1, idx, &idxlen, &idxmax, 0, 1);
501
502   menu = mutt_new_menu ();
503   menu->menu = MENU_COMPOSE;
504   menu->offset = HDR_ATTACH;
505   menu->max = idxlen;
506   menu->make_entry = snd_entry;
507   menu->tag = mutt_tag_attach;
508   menu->data = idx;
509 #ifdef USE_NNTP
510   if (news)
511     menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_COMPOSE,
512                                    ComposeNewsHelp);
513   else
514 #endif
515     menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_COMPOSE,
516                                    ComposeHelp);
517
518   if (option (OPTMBOXPANE))
519     buffy_check (0);
520   while (loop) {
521 #ifdef USE_NNTP
522     unset_option (OPTNEWS);     /* for any case */
523 #endif
524     switch (op = mutt_menuLoop (menu)) {
525     case OP_REDRAW:
526       draw_envelope (msg, fcc);
527       menu->offset = HDR_ATTACH;
528       menu->pagelen = LINES - HDR_ATTACH - 2;
529       break;
530     case OP_COMPOSE_EDIT_FROM:
531       menu->redraw = edit_address_list (HDR_FROM, &msg->env->from);
532       mutt_message_hook (NULL, msg, M_SEND2HOOK);
533       break;
534     case OP_COMPOSE_EDIT_TO:
535 #ifdef USE_NNTP
536       if (!news) {
537 #endif
538         menu->redraw = edit_address_list (HDR_TO, &msg->env->to);
539         mutt_message_hook (NULL, msg, M_SEND2HOOK);
540 #ifdef USE_NNTP
541       }
542 #endif
543       break;
544     case OP_COMPOSE_EDIT_BCC:
545 #ifdef USE_NNTP
546       if (!news) {
547 #endif
548         menu->redraw = edit_address_list (HDR_BCC, &msg->env->bcc);
549         mutt_message_hook (NULL, msg, M_SEND2HOOK);
550 #ifdef USE_NNTP
551       }
552 #endif
553       break;
554     case OP_COMPOSE_EDIT_CC:
555 #ifdef USE_NNTP
556       if (!news) {
557 #endif
558         menu->redraw = edit_address_list (HDR_CC, &msg->env->cc);
559         mutt_message_hook (NULL, msg, M_SEND2HOOK);
560 #ifdef USE_NNTP
561       }
562 #endif
563       break;
564 #ifdef USE_NNTP
565     case OP_COMPOSE_EDIT_NEWSGROUPS:
566       if (news) {
567         if (msg->env->newsgroups)
568           m_strcpy(buf, sizeof(buf), msg->env->newsgroups);
569         else
570           buf[0] = 0;
571         if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) == 0
572             && buf[0]) {
573           p_delete(&msg->env->newsgroups);
574           m_strrtrim(buf);
575           msg->env->newsgroups = m_strdup(skipspaces(buf));
576           move (HDR_TO, HDR_XOFFSET);
577           clrtoeol ();
578           if (msg->env->newsgroups)
579             printw ("%-*.*s", W, W, msg->env->newsgroups);
580         }
581       }
582       break;
583
584     case OP_COMPOSE_EDIT_FOLLOWUP_TO:
585       if (news) {
586         buf[0] = 0;
587         if (msg->env->followup_to)
588           m_strcpy(buf, sizeof(buf), msg->env->followup_to);
589         if (mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) == 0
590             && buf[0]) {
591           p_delete(&msg->env->followup_to);
592           m_strrtrim(buf);
593           msg->env->followup_to = m_strdup(skipspaces(buf));
594           move (HDR_CC, HDR_XOFFSET);
595           clrtoeol ();
596           if (msg->env->followup_to)
597             printw ("%-*.*s", W, W, msg->env->followup_to);
598         }
599       }
600       break;
601
602     case OP_COMPOSE_EDIT_X_COMMENT_TO:
603       if (news && option (OPTXCOMMENTTO)) {
604         buf[0] = 0;
605         if (msg->env->x_comment_to)
606           m_strcpy(buf, sizeof(buf), msg->env->x_comment_to);
607         if (mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) == 0
608             && buf[0]) {
609           p_delete(&msg->env->x_comment_to);
610           msg->env->x_comment_to = m_strdup(buf);
611           move (HDR_BCC, HDR_XOFFSET);
612           clrtoeol ();
613           if (msg->env->x_comment_to)
614             printw ("%-*.*s", W, W, msg->env->x_comment_to);
615         }
616       }
617       break;
618 #endif
619     case OP_COMPOSE_EDIT_SUBJECT:
620       if (msg->env->subject)
621         m_strcpy(buf, sizeof(buf), msg->env->subject);
622       else
623         buf[0] = 0;
624       if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) == 0) {
625         m_strreplace(&msg->env->subject, buf);
626         move (HDR_SUBJECT, HDR_XOFFSET + SW);
627         clrtoeol ();
628         if (msg->env->subject)
629           mutt_paddstr (W, msg->env->subject);
630       }
631       mutt_message_hook (NULL, msg, M_SEND2HOOK);
632       break;
633     case OP_COMPOSE_EDIT_REPLY_TO:
634       menu->redraw = edit_address_list (HDR_REPLYTO, &msg->env->reply_to);
635       mutt_message_hook (NULL, msg, M_SEND2HOOK);
636       break;
637     case OP_COMPOSE_EDIT_FCC:
638       m_strcpy(buf, sizeof(buf), fcc);
639       if (mutt_get_field ("Fcc: ", buf, sizeof (buf), M_FILE | M_CLEAR) == 0) {
640         m_strcpy(fcc, _POSIX_PATH_MAX, buf);
641         mutt_pretty_mailbox (fcc);
642         move (HDR_FCC, HDR_XOFFSET + SW);
643         mutt_paddstr (W, fcc);
644         fccSet = 1;
645       }
646       MAYBE_REDRAW (menu->redraw);
647       mutt_message_hook (NULL, msg, M_SEND2HOOK);
648       break;
649     case OP_COMPOSE_EDIT_MESSAGE:
650       if (Editor && !option (OPTEDITHDRS)) {
651         mutt_edit_file (Editor, msg->content->filename);
652         mutt_update_encoding (msg->content);
653         menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
654         mutt_message_hook (NULL, msg, M_SEND2HOOK);
655         break;
656       }
657       /* fall through */
658     case OP_COMPOSE_EDIT_HEADERS:
659       if ((op == OP_COMPOSE_EDIT_HEADERS ||
660            (op == OP_COMPOSE_EDIT_MESSAGE && option (OPTEDITHDRS)))) {
661         const char *tag = NULL;
662         char *err = NULL;
663
664         mutt_env_to_local (msg->env);
665         mutt_edit_headers (NONULL (Editor), msg->content->filename, msg,
666                            fcc, fcclen);
667         if (mutt_env_to_idna (msg->env, &tag, &err)) {
668           mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
669           p_delete(&err);
670         }
671       }
672       mutt_update_encoding (msg->content);
673
674       /* attachments may have been added */
675       if (idxlen && idx[idxlen - 1]->content->next) {
676         for (i = 0; i < idxlen; i++)
677           p_delete(&idx[i]);
678         idxlen = 0;
679         idx =
680           mutt_gen_attach_list (msg->content, -1, idx, &idxlen, &idxmax, 0,
681                                 1);
682         menu->data = idx;
683         menu->max = idxlen;
684       }
685
686       menu->redraw = REDRAW_FULL;
687       mutt_message_hook (NULL, msg, M_SEND2HOOK);
688       break; 
689
690     case OP_COMPOSE_ATTACH_KEY:
691       if (idxlen == idxmax) {
692         p_realloc(&idx, idxmax += 5);
693         menu->data = idx;
694       }
695
696       idx[idxlen] = p_new(ATTACHPTR, 1);
697       if ((idx[idxlen]->content =
698            crypt_pgp_make_key_attachment (NULL)) != NULL) {
699         update_idx (menu, idx, idxlen++);
700         menu->redraw |= REDRAW_INDEX;
701       }
702       else
703         p_delete(&idx[idxlen]);
704
705       menu->redraw |= REDRAW_STATUS;
706
707       if (option (OPTNEEDREDRAW)) {
708         menu->redraw = REDRAW_FULL;
709         unset_option (OPTNEEDREDRAW);
710       }
711
712       mutt_message_hook (NULL, msg, M_SEND2HOOK);
713       break; 
714
715     case OP_COMPOSE_ATTACH_FILE:
716       {
717         char *prompt, **files;
718         int error, numfiles;
719
720         fname[0] = 0;
721         prompt = _("Attach file");
722         numfiles = 0;
723         files = NULL;
724
725         if (_mutt_enter_fname
726             (prompt, fname, sizeof (fname), &menu->redraw, 0, 1, &files,
727              &numfiles) == -1 || *fname == '\0')
728           break;
729
730         if (idxlen + numfiles >= idxmax) {
731           p_realloc(&idx, idxmax += 5 + numfiles);
732           menu->data = idx;
733         }
734
735         error = 0;
736         if (numfiles > 1)
737           mutt_message _("Attaching selected files...");
738
739         for (i = 0; i < numfiles; i++) {
740           char *att = files[i];
741
742           idx[idxlen] = p_new(ATTACHPTR, 1);
743           idx[idxlen]->unowned = 1;
744           idx[idxlen]->content = mutt_make_file_attach (att);
745           if (idx[idxlen]->content != NULL)
746             update_idx (menu, idx, idxlen++);
747           else {
748             error = 1;
749             mutt_error (_("Unable to attach %s!"), att);
750             p_delete(&idx[idxlen]);
751           }
752         }
753
754         p_delete(&files);
755         if (!error)
756           mutt_clear_error ();
757
758         menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
759       }
760       mutt_message_hook (NULL, msg, M_SEND2HOOK);
761       break;
762
763     case OP_COMPOSE_ATTACH_MESSAGE:
764 #ifdef USE_NNTP
765     case OP_COMPOSE_ATTACH_NEWS_MESSAGE:
766 #endif
767       {
768         char *prompt;
769         HEADER *h;
770
771         fname[0] = 0;
772         prompt = _("Open mailbox to attach message from");
773
774 #ifdef USE_NNTP
775         unset_option (OPTNEWS);
776         if (op == OP_COMPOSE_ATTACH_NEWS_MESSAGE) {
777           if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)))
778             break;
779
780           prompt = _("Open newsgroup to attach message from");
781           set_option (OPTNEWS);
782         }
783 #endif
784
785         if (Context)
786 #ifdef USE_NNTP
787           if ((op == OP_COMPOSE_ATTACH_MESSAGE) ^ (Context->magic == M_NNTP))
788 #endif
789           {
790             m_strcpy(fname, sizeof(fname), NONULL(Context->path));
791             mutt_pretty_mailbox (fname);
792           }
793
794         if (mutt_enter_fname (prompt, fname, sizeof (fname), &menu->redraw, 1)
795             == -1 || !fname[0])
796           break;
797
798 #ifdef USE_NNTP
799         if (option (OPTNEWS))
800           nntp_expand_path (fname, sizeof (fname),
801                             &CurrentNewsSrv->conn->account);
802         else
803 #endif
804           mutt_expand_path (fname, sizeof (fname));
805         if (mx_get_magic (fname) != M_IMAP)
806           if (mx_get_magic (fname) != M_POP)
807 #ifdef USE_NNTP
808             if (mx_get_magic (fname) != M_NNTP && !option (OPTNEWS))
809 #endif
810               /* check to make sure the file exists and is readable */
811               if (access (fname, R_OK) == -1) {
812                 mutt_perror (fname);
813                 break;
814               }
815
816         menu->redraw = REDRAW_FULL;
817
818         ctx = mx_open_mailbox (fname, M_READONLY, NULL);
819         if (ctx == NULL) {
820           mutt_perror (fname);
821           break;
822         }
823
824         if (!ctx->msgcount) {
825           mx_close_mailbox (ctx, NULL);
826           p_delete(&ctx);
827           mutt_error _("No messages in that folder.");
828
829           break;
830         }
831
832         this = Context;         /* remember current folder and sort methods */
833         oldSort = Sort;
834         oldSortAux = SortAux;
835
836         Context = ctx;
837         set_option (OPTATTACHMSG);
838         mutt_message _("Tag the messages you want to attach!");
839
840         closed = mutt_index_menu ();
841         unset_option (OPTATTACHMSG);
842
843         if (!Context) {
844           /* go back to the folder we started from */
845           Context = this;
846           /* Restore old $sort and $sort_aux */
847           Sort = oldSort;
848           SortAux = oldSortAux;
849           menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
850           break;
851         }
852
853         if (idxlen + Context->tagged >= idxmax) {
854           p_realloc(&idx, idxmax += 5 + Context->tagged);
855           menu->data = idx;
856         }
857
858         for (i = 0; i < Context->msgcount; i++) {
859           h = Context->hdrs[i];
860           if (h->tagged) {
861             idx[idxlen] = p_new(ATTACHPTR, 1);
862             idx[idxlen]->content = mutt_make_message_attach (Context, h, 1);
863             if (idx[idxlen]->content != NULL)
864               update_idx (menu, idx, idxlen++);
865             else {
866               mutt_error _("Unable to attach!");
867
868               p_delete(&idx[idxlen]);
869             }
870           }
871         }
872         menu->redraw |= REDRAW_FULL;
873
874         if (closed == OP_QUIT)
875           mx_close_mailbox (Context, NULL);
876         else
877           mx_fastclose_mailbox (Context);
878         p_delete(&Context);
879
880         /* go back to the folder we started from */
881         Context = this;
882         /* Restore old $sort and $sort_aux */
883         Sort = oldSort;
884         SortAux = oldSortAux;
885       }
886       mutt_message_hook (NULL, msg, M_SEND2HOOK);
887       break;
888
889     case OP_DELETE:
890       CHECK_COUNT;
891       if (idx[menu->current]->unowned)
892         idx[menu->current]->content->unlink = 0;
893       if (delete_attachment (menu, &idxlen, menu->current) == -1)
894         break;
895       mutt_update_tree (idx, idxlen);
896       if (idxlen) {
897         if (menu->current > idxlen - 1)
898           menu->current = idxlen - 1;
899       }
900       else
901         menu->current = 0;
902
903       if (menu->current == 0)
904         msg->content = idx[0]->content;
905
906       menu->redraw |= REDRAW_STATUS;
907       mutt_message_hook (NULL, msg, M_SEND2HOOK);
908       break;
909
910 #define CURRENT idx[menu->current]->content
911
912     case OP_COMPOSE_TOGGLE_RECODE:
913       {
914         CHECK_COUNT;
915         if (!mutt_is_text_part (CURRENT)) {
916           mutt_error (_("Recoding only affects text attachments."));
917           break;
918         }
919         CURRENT->noconv = !CURRENT->noconv;
920         if (CURRENT->noconv)
921           mutt_message (_("The current attachment won't be converted."));
922         else
923           mutt_message (_("The current attachment will be converted."));
924         menu->redraw = REDRAW_CURRENT;
925         mutt_message_hook (NULL, msg, M_SEND2HOOK);
926         break;
927       }
928 #undef CURRENT
929
930     case OP_COMPOSE_EDIT_DESCRIPTION:
931       CHECK_COUNT;
932       m_strcpy(buf, sizeof(buf),
933                NONULL(idx[menu->current]->content->description));
934       /* header names should not be translated */
935       if (mutt_get_field ("Description: ", buf, sizeof (buf), 0) == 0) {
936         m_strreplace(&idx[menu->current]->content->description, buf);
937         menu->redraw = REDRAW_CURRENT;
938       }
939       mutt_message_hook (NULL, msg, M_SEND2HOOK);
940       break;
941
942     case OP_COMPOSE_UPDATE_ENCODING:
943       CHECK_COUNT;
944       if (menu->tagprefix) {
945         BODY *top;
946
947         for (top = msg->content; top; top = top->next) {
948           if (top->tagged)
949             mutt_update_encoding (top);
950         }
951         menu->redraw = REDRAW_FULL;
952       }
953       else {
954         mutt_update_encoding (idx[menu->current]->content);
955         menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
956       }
957       mutt_message_hook (NULL, msg, M_SEND2HOOK);
958       break;
959
960     case OP_COMPOSE_TOGGLE_DISPOSITION:
961       /* toggle the content-disposition between inline/attachment */
962       idx[menu->current]->content->disposition =
963         (idx[menu->current]->content->disposition ==
964          DISPINLINE) ? DISPATTACH : DISPINLINE;
965       menu->redraw = REDRAW_CURRENT;
966       break;
967
968     case OP_EDIT_TYPE:
969       CHECK_COUNT;
970       {
971         mutt_edit_content_type (NULL, idx[menu->current]->content, NULL);
972
973         /* this may have been a change to text/something */
974         mutt_update_encoding (idx[menu->current]->content);
975
976         menu->redraw = REDRAW_CURRENT;
977       }
978       mutt_message_hook (NULL, msg, M_SEND2HOOK);
979       break;
980
981     case OP_COMPOSE_EDIT_ENCODING:
982       CHECK_COUNT;
983       m_strcpy(buf, sizeof(buf),
984                ENCODING(idx[menu->current]->content->encoding));
985       if (mutt_get_field ("Content-Transfer-Encoding: ", buf,
986                           sizeof (buf), 0) == 0 && buf[0]) {
987         if ((i = mutt_check_encoding (buf)) != ENCOTHER && i != ENCUUENCODED) {
988           idx[menu->current]->content->encoding = i;
989           menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
990           mutt_clear_error ();
991         }
992         else
993           mutt_error _("Invalid encoding.");
994       }
995       mutt_message_hook (NULL, msg, M_SEND2HOOK);
996       break;
997
998     case OP_COMPOSE_SEND_MESSAGE:
999
1000       /* Note: We don't invoke send2-hook here, since we want to leave
1001        * users an opportunity to change settings from the ":" prompt.
1002        */
1003
1004       if (check_attachments (idx, idxlen) != 0) {
1005         menu->redraw = REDRAW_FULL;
1006         break;
1007       }
1008
1009       if (msg->chain && mix_check_message (msg) != 0)
1010         break;
1011
1012       if (!fccSet && *fcc) {
1013         if ((i = query_quadoption (OPT_COPY,
1014                                    _("Save a copy of this message?"))) == -1)
1015           break;
1016         else if (i == M_NO)
1017           *fcc = 0;
1018       }
1019
1020       loop = 0;
1021       r = 0;
1022       break;
1023
1024     case OP_COMPOSE_EDIT_FILE:
1025       CHECK_COUNT;
1026       mutt_edit_file (NONULL (Editor), idx[menu->current]->content->filename);
1027       mutt_update_encoding (idx[menu->current]->content);
1028       menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
1029       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1030       break;
1031
1032     case OP_COMPOSE_TOGGLE_UNLINK:
1033       CHECK_COUNT;
1034       idx[menu->current]->content->unlink =
1035         !idx[menu->current]->content->unlink;
1036
1037       menu->redraw = REDRAW_INDEX;
1038       /* No send2hook since this doesn't change the message. */
1039       break;
1040
1041     case OP_COMPOSE_GET_ATTACHMENT:
1042       CHECK_COUNT;
1043       if (menu->tagprefix) {
1044         BODY *top;
1045
1046         for (top = msg->content; top; top = top->next) {
1047           if (top->tagged)
1048             mutt_get_tmp_attachment (top);
1049         }
1050         menu->redraw = REDRAW_FULL;
1051       }
1052       else if (mutt_get_tmp_attachment (idx[menu->current]->content) == 0)
1053         menu->redraw = REDRAW_CURRENT;
1054
1055       /* No send2hook since this doesn't change the message. */
1056       break;
1057
1058     case OP_COMPOSE_RENAME_FILE:
1059       CHECK_COUNT;
1060       m_strcpy(fname, sizeof(fname), idx[menu->current]->content->filename);
1061       mutt_pretty_mailbox (fname);
1062       if (mutt_get_field (_("Rename to: "), fname, sizeof (fname), M_FILE)
1063           == 0 && fname[0]) {
1064         if (stat (idx[menu->current]->content->filename, &st) == -1) {
1065           mutt_error (_("Can't stat %s: %s"), fname, strerror (errno));
1066           break;
1067         }
1068
1069         mutt_expand_path (fname, sizeof (fname));
1070         if (mutt_rename_file (idx[menu->current]->content->filename, fname))
1071           break;
1072
1073         m_strreplace(&idx[menu->current]->content->filename, fname);
1074         menu->redraw = REDRAW_CURRENT;
1075
1076         if (idx[menu->current]->content->stamp >= st.st_mtime)
1077           mutt_stamp_attachment (idx[menu->current]->content);
1078
1079       }
1080       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1081       break;
1082
1083     case OP_COMPOSE_NEW_MIME:
1084       {
1085         char type[STRING];
1086         char *p;
1087         int itype;
1088         FILE *fp;
1089
1090         CLEARLINE (LINES - 1);
1091         fname[0] = 0;
1092         if (mutt_get_field (_("New file: "), fname, sizeof (fname), M_FILE)
1093             != 0 || !fname[0])
1094           continue;
1095         mutt_expand_path (fname, sizeof (fname));
1096
1097         /* Call to lookup_mime_type () ?  maybe later */
1098         type[0] = 0;
1099         if (mutt_get_field ("Content-Type: ", type, sizeof (type), 0) != 0
1100             || !type[0])
1101           continue;
1102
1103         if (!(p = strchr (type, '/'))) {
1104           mutt_error _("Content-Type is of the form base/sub");
1105
1106           continue;
1107         }
1108         *p++ = 0;
1109         if ((itype = mutt_check_mime_type (type)) == TYPEOTHER) {
1110           mutt_error (_("Unknown Content-Type %s"), type);
1111           continue;
1112         }
1113         if (idxlen == idxmax) {
1114           p_realloc(&idx, idxmax += 5);
1115           menu->data = idx;
1116         }
1117
1118         idx[idxlen] = p_new(ATTACHPTR, 1);
1119         /* Touch the file */
1120         if (!(fp = safe_fopen (fname, "w"))) {
1121           mutt_error (_("Can't create file %s"), fname);
1122           p_delete(&idx[idxlen]);
1123           continue;
1124         }
1125         m_fclose(&fp);
1126
1127         if ((idx[idxlen]->content = mutt_make_file_attach (fname)) == NULL) {
1128           mutt_error
1129             _("What we have here is a failure to make an attachment");
1130           continue;
1131         }
1132         update_idx (menu, idx, idxlen++);
1133
1134         idx[menu->current]->content->type = itype;
1135         m_strreplace(&idx[menu->current]->content->subtype, p);
1136         idx[menu->current]->content->unlink = 1;
1137         menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
1138
1139         if (mutt_compose_attachment (idx[menu->current]->content)) {
1140           mutt_update_encoding (idx[menu->current]->content);
1141           menu->redraw = REDRAW_FULL;
1142         }
1143       }
1144       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1145       break;
1146
1147     case OP_COMPOSE_EDIT_MIME:
1148       CHECK_COUNT;
1149       if (mutt_edit_attachment (idx[menu->current]->content)) {
1150         mutt_update_encoding (idx[menu->current]->content);
1151         menu->redraw = REDRAW_FULL;
1152       }
1153       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1154       break;
1155
1156     case OP_VIEW_ATTACH:
1157     case OP_DISPLAY_HEADERS:
1158       CHECK_COUNT;
1159       mutt_attach_display_loop (menu, op, NULL, NULL, NULL, &idx, &idxlen,
1160                                 NULL, 0);
1161       menu->redraw = REDRAW_FULL;
1162       /* no send2hook, since this doesn't modify the message */
1163       break;
1164
1165     case OP_SAVE:
1166       CHECK_COUNT;
1167       mutt_save_attachment_list (NULL, menu->tagprefix,
1168                                  menu->tagprefix ? msg->content : idx[menu->
1169                                                                       current]->
1170                                  content, NULL, menu);
1171       MAYBE_REDRAW (menu->redraw);
1172       /* no send2hook, since this doesn't modify the message */
1173       break;
1174
1175     case OP_PRINT:
1176       CHECK_COUNT;
1177       mutt_print_attachment_list (NULL, menu->tagprefix,
1178                                   menu->tagprefix ? msg->content : idx[menu->
1179                                                                        current]->
1180                                   content);
1181       /* no send2hook, since this doesn't modify the message */
1182       break;
1183
1184     case OP_PIPE:
1185     case OP_FILTER:
1186       CHECK_COUNT;
1187       mutt_pipe_attachment_list (NULL, menu->tagprefix,
1188                                  menu->tagprefix ? msg->content : idx[menu->
1189                                                                       current]->
1190                                  content, op == OP_FILTER);
1191       if (op == OP_FILTER)      /* cte might have changed */
1192         menu->redraw = menu->tagprefix ? REDRAW_FULL : REDRAW_CURRENT;
1193       menu->redraw |= REDRAW_STATUS;
1194       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1195       break;
1196
1197     case OP_EXIT:
1198       if ((i =
1199            query_quadoption (OPT_POSTPONE,
1200                              _("Postpone this message?"))) == M_NO) {
1201         while (idxlen-- > 0) {
1202           /* avoid freeing other attachments */
1203           idx[idxlen]->content->next = NULL;
1204           idx[idxlen]->content->parts = NULL;
1205           if (idx[idxlen]->unowned)
1206             idx[idxlen]->content->unlink = 0;
1207           body_list_wipe(&idx[idxlen]->content);
1208           p_delete(&idx[idxlen]->tree);
1209           p_delete(&idx[idxlen]);
1210         }
1211         p_delete(&idx);
1212         idxlen = 0;
1213         idxmax = 0;
1214         r = -1;
1215         loop = 0;
1216         break;
1217       }
1218       else if (i == -1)
1219         break;                  /* abort */
1220
1221       /* fall through to postpone! */
1222
1223     case OP_COMPOSE_POSTPONE_MESSAGE:
1224
1225       if (check_attachments (idx, idxlen) != 0) {
1226         menu->redraw = REDRAW_FULL;
1227         break;
1228       }
1229
1230       loop = 0;
1231       r = 1;
1232       break;
1233
1234     case OP_COMPOSE_ISPELL:
1235       endwin ();
1236       snprintf (buf, sizeof (buf), "%s -x %s", NONULL (Ispell),
1237                 msg->content->filename);
1238       if (mutt_system (buf) == -1)
1239         mutt_error (_("Error running \"%s\"!"), buf);
1240       else {
1241         mutt_update_encoding (msg->content);
1242         menu->redraw |= REDRAW_STATUS;
1243       }
1244       break;
1245
1246     case OP_COMPOSE_WRITE_MESSAGE:
1247
1248       fname[0] = '\0';
1249       if (Context) {
1250         m_strcpy(fname, sizeof(fname), NONULL(Context->path));
1251         mutt_pretty_mailbox (fname);
1252       }
1253       if (idxlen)
1254         msg->content = idx[0]->content;
1255       if (mutt_enter_fname
1256           (_("Write message to mailbox"), fname, sizeof (fname),
1257            &menu->redraw, 1) != -1 && fname[0]) {
1258         mutt_message (_("Writing message to %s ..."), fname);
1259         mutt_expand_path (fname, sizeof (fname));
1260
1261         if (msg->content->next)
1262           msg->content = mutt_make_multipart (msg->content);
1263
1264         if (mutt_write_fcc (NONULL (fname), msg, NULL, 1, NULL) < 0)
1265           msg->content = mutt_remove_multipart (msg->content);
1266         else
1267           mutt_message _("Message written.");
1268       }
1269       break;
1270
1271     case OP_COMPOSE_PGP_MENU:
1272       if (msg->security & APPLICATION_SMIME) {
1273         if (mutt_yesorno (_("S/MIME already selected. Clear & continue ? "),
1274                           M_YES) != M_YES) {
1275           mutt_clear_error ();
1276           break;
1277         }
1278         msg->security = 0;
1279       }
1280       msg->security = crypt_pgp_send_menu (msg, &menu->redraw);
1281       redraw_crypt_lines (msg);
1282       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1283       break;
1284
1285     case OP_FORGET_PASSPHRASE:
1286       crypt_forget_passphrase ();
1287       break;
1288
1289     case OP_COMPOSE_SMIME_MENU:
1290       if (msg->security & APPLICATION_PGP) {
1291         if (mutt_yesorno (_("PGP already selected. Clear & continue ? "),
1292                           M_YES) != M_YES) {
1293           mutt_clear_error ();
1294           break;
1295         }
1296         msg->security = 0;
1297       }
1298       msg->security = crypt_smime_send_menu (msg, &menu->redraw);
1299       redraw_crypt_lines (msg);
1300       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1301       break;
1302
1303     case OP_COMPOSE_MIX:
1304       mix_make_chain (&msg->chain, &menu->redraw);
1305       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1306       break;
1307     }
1308
1309     /* Draw formated compose status line */
1310     if (menu->redraw & REDRAW_STATUS) {
1311       compose_status_line (buf, sizeof (buf), menu, NONULL (ComposeFormat));
1312       CLEARLINE (option (OPTSTATUSONTOP) ? 0 : LINES - 2);
1313       SETCOLOR (MT_COLOR_STATUS);
1314       move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, SW);
1315       printw ("%-*.*s", COLS-SW, COLS-SW, buf);
1316       SETCOLOR (MT_COLOR_NORMAL);
1317       menu->redraw &= ~REDRAW_STATUS;
1318     }
1319   }
1320
1321   mutt_menuDestroy (&menu);
1322
1323   if (idxlen) {
1324     msg->content = idx[0]->content;
1325     for (i = 0; i < idxlen; i++) {
1326       idx[i]->content->aptr = NULL;
1327       p_delete(&idx[i]);
1328     }
1329   } else {
1330     msg->content = NULL;
1331   }
1332
1333   p_delete(&idx);
1334
1335   return (r);
1336 }