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