more hash code simplifications.
[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());
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 (!option (OPTEDITHDRS)) {
651         mutt_edit_file(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(msg->content->filename, msg, fcc, fcclen);
666         if (mutt_env_to_idna (msg->env, &tag, &err)) {
667           mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
668           p_delete(&err);
669         }
670       }
671       mutt_update_encoding (msg->content);
672
673       /* attachments may have been added */
674       if (idxlen && idx[idxlen - 1]->content->next) {
675         for (i = 0; i < idxlen; i++)
676           p_delete(&idx[i]);
677         idxlen = 0;
678         idx =
679           mutt_gen_attach_list (msg->content, -1, idx, &idxlen, &idxmax, 0,
680                                 1);
681         menu->data = idx;
682         menu->max = idxlen;
683       }
684
685       menu->redraw = REDRAW_FULL;
686       mutt_message_hook (NULL, msg, M_SEND2HOOK);
687       break; 
688
689     case OP_COMPOSE_ATTACH_KEY:
690       if (idxlen == idxmax) {
691         p_realloc(&idx, idxmax += 5);
692         menu->data = idx;
693       }
694
695       idx[idxlen] = p_new(ATTACHPTR, 1);
696       if ((idx[idxlen]->content =
697            crypt_pgp_make_key_attachment (NULL)) != NULL) {
698         update_idx (menu, idx, idxlen++);
699         menu->redraw |= REDRAW_INDEX;
700       }
701       else
702         p_delete(&idx[idxlen]);
703
704       menu->redraw |= REDRAW_STATUS;
705
706       if (option (OPTNEEDREDRAW)) {
707         menu->redraw = REDRAW_FULL;
708         unset_option (OPTNEEDREDRAW);
709       }
710
711       mutt_message_hook (NULL, msg, M_SEND2HOOK);
712       break; 
713
714     case OP_COMPOSE_ATTACH_FILE:
715       {
716         char *prompt, **files;
717         int error, numfiles;
718
719         fname[0] = 0;
720         prompt = _("Attach file");
721         numfiles = 0;
722         files = NULL;
723
724         if (_mutt_enter_fname
725             (prompt, fname, sizeof (fname), &menu->redraw, 0, 1, &files,
726              &numfiles) == -1 || *fname == '\0')
727           break;
728
729         if (idxlen + numfiles >= idxmax) {
730           p_realloc(&idx, idxmax += 5 + numfiles);
731           menu->data = idx;
732         }
733
734         error = 0;
735         if (numfiles > 1)
736           mutt_message _("Attaching selected files...");
737
738         for (i = 0; i < numfiles; i++) {
739           char *att = files[i];
740
741           idx[idxlen] = p_new(ATTACHPTR, 1);
742           idx[idxlen]->unowned = 1;
743           idx[idxlen]->content = mutt_make_file_attach (att);
744           if (idx[idxlen]->content != NULL)
745             update_idx (menu, idx, idxlen++);
746           else {
747             error = 1;
748             mutt_error (_("Unable to attach %s!"), att);
749             p_delete(&idx[idxlen]);
750           }
751         }
752
753         p_delete(&files);
754         if (!error)
755           mutt_clear_error ();
756
757         menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
758       }
759       mutt_message_hook (NULL, msg, M_SEND2HOOK);
760       break;
761
762     case OP_COMPOSE_ATTACH_MESSAGE:
763 #ifdef USE_NNTP
764     case OP_COMPOSE_ATTACH_NEWS_MESSAGE:
765 #endif
766       {
767         char *prompt;
768         HEADER *h;
769
770         fname[0] = 0;
771         prompt = _("Open mailbox to attach message from");
772
773 #ifdef USE_NNTP
774         unset_option (OPTNEWS);
775         if (op == OP_COMPOSE_ATTACH_NEWS_MESSAGE) {
776           if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)))
777             break;
778
779           prompt = _("Open newsgroup to attach message from");
780           set_option (OPTNEWS);
781         }
782 #endif
783
784         if (Context)
785 #ifdef USE_NNTP
786           if ((op == OP_COMPOSE_ATTACH_MESSAGE) ^ (Context->magic == M_NNTP))
787 #endif
788           {
789             m_strcpy(fname, sizeof(fname), NONULL(Context->path));
790             mutt_pretty_mailbox (fname);
791           }
792
793         if (mutt_enter_fname (prompt, fname, sizeof (fname), &menu->redraw, 1)
794             == -1 || !fname[0])
795           break;
796
797 #ifdef USE_NNTP
798         if (option (OPTNEWS))
799           nntp_expand_path (fname, sizeof (fname),
800                             &CurrentNewsSrv->conn->account);
801         else
802 #endif
803           mutt_expand_path (fname, sizeof (fname));
804         if (mx_get_magic (fname) != M_IMAP)
805           if (mx_get_magic (fname) != M_POP)
806 #ifdef USE_NNTP
807             if (mx_get_magic (fname) != M_NNTP && !option (OPTNEWS))
808 #endif
809               /* check to make sure the file exists and is readable */
810               if (access (fname, R_OK) == -1) {
811                 mutt_perror (fname);
812                 break;
813               }
814
815         menu->redraw = REDRAW_FULL;
816
817         ctx = mx_open_mailbox (fname, M_READONLY, NULL);
818         if (ctx == NULL) {
819           mutt_perror (fname);
820           break;
821         }
822
823         if (!ctx->msgcount) {
824           mx_close_mailbox (ctx, NULL);
825           p_delete(&ctx);
826           mutt_error _("No messages in that folder.");
827
828           break;
829         }
830
831         this = Context;         /* remember current folder and sort methods */
832         oldSort = Sort;
833         oldSortAux = SortAux;
834
835         Context = ctx;
836         set_option (OPTATTACHMSG);
837         mutt_message _("Tag the messages you want to attach!");
838
839         closed = mutt_index_menu ();
840         unset_option (OPTATTACHMSG);
841
842         if (!Context) {
843           /* go back to the folder we started from */
844           Context = this;
845           /* Restore old $sort and $sort_aux */
846           Sort = oldSort;
847           SortAux = oldSortAux;
848           menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
849           break;
850         }
851
852         if (idxlen + Context->tagged >= idxmax) {
853           p_realloc(&idx, idxmax += 5 + Context->tagged);
854           menu->data = idx;
855         }
856
857         for (i = 0; i < Context->msgcount; i++) {
858           h = Context->hdrs[i];
859           if (h->tagged) {
860             idx[idxlen] = p_new(ATTACHPTR, 1);
861             idx[idxlen]->content = mutt_make_message_attach (Context, h, 1);
862             if (idx[idxlen]->content != NULL)
863               update_idx (menu, idx, idxlen++);
864             else {
865               mutt_error _("Unable to attach!");
866
867               p_delete(&idx[idxlen]);
868             }
869           }
870         }
871         menu->redraw |= REDRAW_FULL;
872
873         if (closed == OP_QUIT)
874           mx_close_mailbox (Context, NULL);
875         else
876           mx_fastclose_mailbox (Context);
877         p_delete(&Context);
878
879         /* go back to the folder we started from */
880         Context = this;
881         /* Restore old $sort and $sort_aux */
882         Sort = oldSort;
883         SortAux = oldSortAux;
884       }
885       mutt_message_hook (NULL, msg, M_SEND2HOOK);
886       break;
887
888     case OP_DELETE:
889       CHECK_COUNT;
890       if (idx[menu->current]->unowned)
891         idx[menu->current]->content->unlink = 0;
892       if (delete_attachment (menu, &idxlen, menu->current) == -1)
893         break;
894       mutt_update_tree (idx, idxlen);
895       if (idxlen) {
896         if (menu->current > idxlen - 1)
897           menu->current = idxlen - 1;
898       }
899       else
900         menu->current = 0;
901
902       if (menu->current == 0)
903         msg->content = idx[0]->content;
904
905       menu->redraw |= REDRAW_STATUS;
906       mutt_message_hook (NULL, msg, M_SEND2HOOK);
907       break;
908
909 #define CURRENT idx[menu->current]->content
910
911     case OP_COMPOSE_TOGGLE_RECODE:
912       {
913         CHECK_COUNT;
914         if (!mutt_is_text_part (CURRENT)) {
915           mutt_error (_("Recoding only affects text attachments."));
916           break;
917         }
918         CURRENT->noconv = !CURRENT->noconv;
919         if (CURRENT->noconv)
920           mutt_message (_("The current attachment won't be converted."));
921         else
922           mutt_message (_("The current attachment will be converted."));
923         menu->redraw = REDRAW_CURRENT;
924         mutt_message_hook (NULL, msg, M_SEND2HOOK);
925         break;
926       }
927 #undef CURRENT
928
929     case OP_COMPOSE_EDIT_DESCRIPTION:
930       CHECK_COUNT;
931       m_strcpy(buf, sizeof(buf),
932                NONULL(idx[menu->current]->content->description));
933       /* header names should not be translated */
934       if (mutt_get_field ("Description: ", buf, sizeof (buf), 0) == 0) {
935         m_strreplace(&idx[menu->current]->content->description, buf);
936         menu->redraw = REDRAW_CURRENT;
937       }
938       mutt_message_hook (NULL, msg, M_SEND2HOOK);
939       break;
940
941     case OP_COMPOSE_UPDATE_ENCODING:
942       CHECK_COUNT;
943       if (menu->tagprefix) {
944         BODY *top;
945
946         for (top = msg->content; top; top = top->next) {
947           if (top->tagged)
948             mutt_update_encoding (top);
949         }
950         menu->redraw = REDRAW_FULL;
951       }
952       else {
953         mutt_update_encoding (idx[menu->current]->content);
954         menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
955       }
956       mutt_message_hook (NULL, msg, M_SEND2HOOK);
957       break;
958
959     case OP_COMPOSE_TOGGLE_DISPOSITION:
960       /* toggle the content-disposition between inline/attachment */
961       idx[menu->current]->content->disposition =
962         (idx[menu->current]->content->disposition ==
963          DISPINLINE) ? DISPATTACH : DISPINLINE;
964       menu->redraw = REDRAW_CURRENT;
965       break;
966
967     case OP_EDIT_TYPE:
968       CHECK_COUNT;
969       {
970         mutt_edit_content_type (NULL, idx[menu->current]->content, NULL);
971
972         /* this may have been a change to text/something */
973         mutt_update_encoding (idx[menu->current]->content);
974
975         menu->redraw = REDRAW_CURRENT;
976       }
977       mutt_message_hook (NULL, msg, M_SEND2HOOK);
978       break;
979
980     case OP_COMPOSE_EDIT_ENCODING:
981       CHECK_COUNT;
982       m_strcpy(buf, sizeof(buf),
983                ENCODING(idx[menu->current]->content->encoding));
984       if (mutt_get_field ("Content-Transfer-Encoding: ", buf,
985                           sizeof (buf), 0) == 0 && buf[0]) {
986         if ((i = mutt_check_encoding (buf)) != ENCOTHER && i != ENCUUENCODED) {
987           idx[menu->current]->content->encoding = i;
988           menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
989           mutt_clear_error ();
990         }
991         else
992           mutt_error _("Invalid encoding.");
993       }
994       mutt_message_hook (NULL, msg, M_SEND2HOOK);
995       break;
996
997     case OP_COMPOSE_SEND_MESSAGE:
998
999       /* Note: We don't invoke send2-hook here, since we want to leave
1000        * users an opportunity to change settings from the ":" prompt.
1001        */
1002
1003       if (check_attachments (idx, idxlen) != 0) {
1004         menu->redraw = REDRAW_FULL;
1005         break;
1006       }
1007
1008       if (msg->chain && mix_check_message (msg) != 0)
1009         break;
1010
1011       if (!fccSet && *fcc) {
1012         if ((i = query_quadoption (OPT_COPY,
1013                                    _("Save a copy of this message?"))) == -1)
1014           break;
1015         else if (i == M_NO)
1016           *fcc = 0;
1017       }
1018
1019       loop = 0;
1020       r = 0;
1021       break;
1022
1023     case OP_COMPOSE_EDIT_FILE:
1024       CHECK_COUNT;
1025       mutt_edit_file(idx[menu->current]->content->filename);
1026       mutt_update_encoding (idx[menu->current]->content);
1027       menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
1028       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1029       break;
1030
1031     case OP_COMPOSE_TOGGLE_UNLINK:
1032       CHECK_COUNT;
1033       idx[menu->current]->content->unlink =
1034         !idx[menu->current]->content->unlink;
1035
1036       menu->redraw = REDRAW_INDEX;
1037       /* No send2hook since this doesn't change the message. */
1038       break;
1039
1040     case OP_COMPOSE_GET_ATTACHMENT:
1041       CHECK_COUNT;
1042       if (menu->tagprefix) {
1043         BODY *top;
1044
1045         for (top = msg->content; top; top = top->next) {
1046           if (top->tagged)
1047             mutt_get_tmp_attachment (top);
1048         }
1049         menu->redraw = REDRAW_FULL;
1050       }
1051       else if (mutt_get_tmp_attachment (idx[menu->current]->content) == 0)
1052         menu->redraw = REDRAW_CURRENT;
1053
1054       /* No send2hook since this doesn't change the message. */
1055       break;
1056
1057     case OP_COMPOSE_RENAME_FILE:
1058       CHECK_COUNT;
1059       m_strcpy(fname, sizeof(fname), idx[menu->current]->content->filename);
1060       mutt_pretty_mailbox (fname);
1061       if (mutt_get_field (_("Rename to: "), fname, sizeof (fname), M_FILE)
1062           == 0 && fname[0]) {
1063         if (stat (idx[menu->current]->content->filename, &st) == -1) {
1064           mutt_error (_("Can't stat %s: %s"), fname, strerror (errno));
1065           break;
1066         }
1067
1068         mutt_expand_path (fname, sizeof (fname));
1069         if (mutt_rename_file (idx[menu->current]->content->filename, fname))
1070           break;
1071
1072         m_strreplace(&idx[menu->current]->content->filename, fname);
1073         menu->redraw = REDRAW_CURRENT;
1074
1075         if (idx[menu->current]->content->stamp >= st.st_mtime)
1076           mutt_stamp_attachment (idx[menu->current]->content);
1077
1078       }
1079       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1080       break;
1081
1082     case OP_COMPOSE_NEW_MIME:
1083       {
1084         char type[STRING];
1085         char *p;
1086         int itype;
1087         FILE *fp;
1088
1089         CLEARLINE (LINES - 1);
1090         fname[0] = 0;
1091         if (mutt_get_field (_("New file: "), fname, sizeof (fname), M_FILE)
1092             != 0 || !fname[0])
1093           continue;
1094         mutt_expand_path (fname, sizeof (fname));
1095
1096         /* Call to lookup_mime_type () ?  maybe later */
1097         type[0] = 0;
1098         if (mutt_get_field ("Content-Type: ", type, sizeof (type), 0) != 0
1099             || !type[0])
1100           continue;
1101
1102         if (!(p = strchr (type, '/'))) {
1103           mutt_error _("Content-Type is of the form base/sub");
1104
1105           continue;
1106         }
1107         *p++ = 0;
1108         if ((itype = mutt_check_mime_type (type)) == TYPEOTHER) {
1109           mutt_error (_("Unknown Content-Type %s"), type);
1110           continue;
1111         }
1112         if (idxlen == idxmax) {
1113           p_realloc(&idx, idxmax += 5);
1114           menu->data = idx;
1115         }
1116
1117         idx[idxlen] = p_new(ATTACHPTR, 1);
1118         /* Touch the file */
1119         if (!(fp = safe_fopen (fname, "w"))) {
1120           mutt_error (_("Can't create file %s"), fname);
1121           p_delete(&idx[idxlen]);
1122           continue;
1123         }
1124         m_fclose(&fp);
1125
1126         if ((idx[idxlen]->content = mutt_make_file_attach (fname)) == NULL) {
1127           mutt_error
1128             _("What we have here is a failure to make an attachment");
1129           continue;
1130         }
1131         update_idx (menu, idx, idxlen++);
1132
1133         idx[menu->current]->content->type = itype;
1134         m_strreplace(&idx[menu->current]->content->subtype, p);
1135         idx[menu->current]->content->unlink = 1;
1136         menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
1137
1138         if (mutt_compose_attachment (idx[menu->current]->content)) {
1139           mutt_update_encoding (idx[menu->current]->content);
1140           menu->redraw = REDRAW_FULL;
1141         }
1142       }
1143       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1144       break;
1145
1146     case OP_COMPOSE_EDIT_MIME:
1147       CHECK_COUNT;
1148       if (mutt_edit_attachment (idx[menu->current]->content)) {
1149         mutt_update_encoding (idx[menu->current]->content);
1150         menu->redraw = REDRAW_FULL;
1151       }
1152       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1153       break;
1154
1155     case OP_VIEW_ATTACH:
1156     case OP_DISPLAY_HEADERS:
1157       CHECK_COUNT;
1158       mutt_attach_display_loop (menu, op, NULL, NULL, NULL, &idx, &idxlen,
1159                                 NULL, 0);
1160       menu->redraw = REDRAW_FULL;
1161       /* no send2hook, since this doesn't modify the message */
1162       break;
1163
1164     case OP_SAVE:
1165       CHECK_COUNT;
1166       mutt_save_attachment_list (NULL, menu->tagprefix,
1167                                  menu->tagprefix ? msg->content : idx[menu->
1168                                                                       current]->
1169                                  content, NULL, menu);
1170       MAYBE_REDRAW (menu->redraw);
1171       /* no send2hook, since this doesn't modify the message */
1172       break;
1173
1174     case OP_PRINT:
1175       CHECK_COUNT;
1176       mutt_print_attachment_list (NULL, menu->tagprefix,
1177                                   menu->tagprefix ? msg->content : idx[menu->
1178                                                                        current]->
1179                                   content);
1180       /* no send2hook, since this doesn't modify the message */
1181       break;
1182
1183     case OP_PIPE:
1184     case OP_FILTER:
1185       CHECK_COUNT;
1186       mutt_pipe_attachment_list (NULL, menu->tagprefix,
1187                                  menu->tagprefix ? msg->content : idx[menu->
1188                                                                       current]->
1189                                  content, op == OP_FILTER);
1190       if (op == OP_FILTER)      /* cte might have changed */
1191         menu->redraw = menu->tagprefix ? REDRAW_FULL : REDRAW_CURRENT;
1192       menu->redraw |= REDRAW_STATUS;
1193       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1194       break;
1195
1196     case OP_EXIT:
1197       if ((i =
1198            query_quadoption (OPT_POSTPONE,
1199                              _("Postpone this message?"))) == M_NO) {
1200         while (idxlen-- > 0) {
1201           /* avoid freeing other attachments */
1202           idx[idxlen]->content->next = NULL;
1203           idx[idxlen]->content->parts = NULL;
1204           if (idx[idxlen]->unowned)
1205             idx[idxlen]->content->unlink = 0;
1206           body_list_wipe(&idx[idxlen]->content);
1207           p_delete(&idx[idxlen]->tree);
1208           p_delete(&idx[idxlen]);
1209         }
1210         p_delete(&idx);
1211         idxlen = 0;
1212         idxmax = 0;
1213         r = -1;
1214         loop = 0;
1215         break;
1216       }
1217       else if (i == -1)
1218         break;                  /* abort */
1219
1220       /* fall through to postpone! */
1221
1222     case OP_COMPOSE_POSTPONE_MESSAGE:
1223
1224       if (check_attachments (idx, idxlen) != 0) {
1225         menu->redraw = REDRAW_FULL;
1226         break;
1227       }
1228
1229       loop = 0;
1230       r = 1;
1231       break;
1232
1233     case OP_COMPOSE_ISPELL:
1234       endwin ();
1235       snprintf (buf, sizeof (buf), "%s -x %s", NONULL (Ispell),
1236                 msg->content->filename);
1237       if (mutt_system (buf) == -1)
1238         mutt_error (_("Error running \"%s\"!"), buf);
1239       else {
1240         mutt_update_encoding (msg->content);
1241         menu->redraw |= REDRAW_STATUS;
1242       }
1243       break;
1244
1245     case OP_COMPOSE_WRITE_MESSAGE:
1246
1247       fname[0] = '\0';
1248       if (Context) {
1249         m_strcpy(fname, sizeof(fname), NONULL(Context->path));
1250         mutt_pretty_mailbox (fname);
1251       }
1252       if (idxlen)
1253         msg->content = idx[0]->content;
1254       if (mutt_enter_fname
1255           (_("Write message to mailbox"), fname, sizeof (fname),
1256            &menu->redraw, 1) != -1 && fname[0]) {
1257         mutt_message (_("Writing message to %s ..."), fname);
1258         mutt_expand_path (fname, sizeof (fname));
1259
1260         if (msg->content->next)
1261           msg->content = mutt_make_multipart (msg->content);
1262
1263         if (mutt_write_fcc (NONULL (fname), msg, NULL, 1, NULL) < 0)
1264           msg->content = mutt_remove_multipart (msg->content);
1265         else
1266           mutt_message _("Message written.");
1267       }
1268       break;
1269
1270     case OP_COMPOSE_PGP_MENU:
1271       if (msg->security & APPLICATION_SMIME) {
1272         if (mutt_yesorno (_("S/MIME already selected. Clear & continue ? "),
1273                           M_YES) != M_YES) {
1274           mutt_clear_error ();
1275           break;
1276         }
1277         msg->security = 0;
1278       }
1279       msg->security = crypt_pgp_send_menu (msg, &menu->redraw);
1280       redraw_crypt_lines (msg);
1281       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1282       break;
1283
1284     case OP_FORGET_PASSPHRASE:
1285       crypt_forget_passphrase ();
1286       break;
1287
1288     case OP_COMPOSE_SMIME_MENU:
1289       if (msg->security & APPLICATION_PGP) {
1290         if (mutt_yesorno (_("PGP already selected. Clear & continue ? "),
1291                           M_YES) != M_YES) {
1292           mutt_clear_error ();
1293           break;
1294         }
1295         msg->security = 0;
1296       }
1297       msg->security = crypt_smime_send_menu (msg, &menu->redraw);
1298       redraw_crypt_lines (msg);
1299       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1300       break;
1301
1302     case OP_COMPOSE_MIX:
1303       mix_make_chain (&msg->chain, &menu->redraw);
1304       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1305       break;
1306     }
1307
1308     /* Draw formated compose status line */
1309     if (menu->redraw & REDRAW_STATUS) {
1310       compose_status_line (buf, sizeof (buf), menu, NONULL (ComposeFormat));
1311       CLEARLINE (option (OPTSTATUSONTOP) ? 0 : LINES - 2);
1312       SETCOLOR (MT_COLOR_STATUS);
1313       move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, SW);
1314       printw ("%-*.*s", COLS-SW, COLS-SW, buf);
1315       SETCOLOR (MT_COLOR_NORMAL);
1316       menu->redraw &= ~REDRAW_STATUS;
1317     }
1318   }
1319
1320   mutt_menuDestroy (&menu);
1321
1322   if (idxlen) {
1323     msg->content = idx[0]->content;
1324     for (i = 0; i < idxlen; i++) {
1325       idx[i]->content->aptr = NULL;
1326       p_delete(&idx[i]);
1327     }
1328   } else {
1329     msg->content = NULL;
1330   }
1331
1332   p_delete(&idx);
1333
1334   return (r);
1335 }