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