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