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