Even less options.
[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   while (loop) {
446 #ifdef USE_NNTP
447     unset_option (OPTNEWS);     /* for any case */
448 #endif
449     switch (op = mutt_menuLoop (menu)) {
450     case OP_REDRAW:
451       draw_envelope (msg, fcc);
452       menu->offset = HDR_ATTACH;
453       menu->pagelen = LINES - HDR_ATTACH - 2;
454       break;
455     case OP_COMPOSE_EDIT_FROM:
456       menu->redraw = edit_address_list (HDR_FROM, &msg->env->from);
457       mutt_message_hook (NULL, msg, M_SEND2HOOK);
458       break;
459     case OP_COMPOSE_EDIT_TO:
460 #ifdef USE_NNTP
461       if (!news) {
462 #endif
463         menu->redraw = edit_address_list (HDR_TO, &msg->env->to);
464         mutt_message_hook (NULL, msg, M_SEND2HOOK);
465 #ifdef USE_NNTP
466       }
467 #endif
468       break;
469     case OP_COMPOSE_EDIT_BCC:
470 #ifdef USE_NNTP
471       if (!news) {
472 #endif
473         menu->redraw = edit_address_list (HDR_BCC, &msg->env->bcc);
474         mutt_message_hook (NULL, msg, M_SEND2HOOK);
475 #ifdef USE_NNTP
476       }
477 #endif
478       break;
479     case OP_COMPOSE_EDIT_CC:
480 #ifdef USE_NNTP
481       if (!news) {
482 #endif
483         menu->redraw = edit_address_list (HDR_CC, &msg->env->cc);
484         mutt_message_hook (NULL, msg, M_SEND2HOOK);
485 #ifdef USE_NNTP
486       }
487 #endif
488       break;
489 #ifdef USE_NNTP
490     case OP_COMPOSE_EDIT_NEWSGROUPS:
491       if (news) {
492         if (msg->env->newsgroups)
493           m_strcpy(buf, sizeof(buf), msg->env->newsgroups);
494         else
495           buf[0] = 0;
496         if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) == 0
497             && buf[0]) {
498           p_delete(&msg->env->newsgroups);
499           m_strrtrim(buf);
500           msg->env->newsgroups = m_strdup(skipspaces(buf));
501           wmove (main_w, HDR_TO, HDR_XOFFSET);
502           wclrtoeol (main_w);
503           if (msg->env->newsgroups)
504             wprintw (main_w, "%-*.*s", W, W, msg->env->newsgroups);
505         }
506       }
507       break;
508
509     case OP_COMPOSE_EDIT_FOLLOWUP_TO:
510       if (news) {
511         buf[0] = 0;
512         if (msg->env->followup_to)
513           m_strcpy(buf, sizeof(buf), msg->env->followup_to);
514         if (mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) == 0
515             && buf[0]) {
516           p_delete(&msg->env->followup_to);
517           m_strrtrim(buf);
518           msg->env->followup_to = m_strdup(skipspaces(buf));
519           wmove (main_w, HDR_CC, HDR_XOFFSET);
520           wclrtoeol (main_w);
521           if (msg->env->followup_to)
522             wprintw (main_w, "%-*.*s", W, W, msg->env->followup_to);
523         }
524       }
525       break;
526
527 #endif
528     case OP_COMPOSE_EDIT_SUBJECT:
529       if (msg->env->subject)
530         m_strcpy(buf, sizeof(buf), msg->env->subject);
531       else
532         buf[0] = 0;
533       if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) == 0) {
534         m_strreplace(&msg->env->subject, buf);
535         wmove (main_w, HDR_SUBJECT, HDR_XOFFSET);
536         wclrtoeol (main_w);
537         if (msg->env->subject)
538           mutt_paddstr (main_w, W, msg->env->subject);
539       }
540       mutt_message_hook (NULL, msg, M_SEND2HOOK);
541       break;
542     case OP_COMPOSE_EDIT_REPLY_TO:
543       menu->redraw = edit_address_list (HDR_REPLYTO, &msg->env->reply_to);
544       mutt_message_hook (NULL, msg, M_SEND2HOOK);
545       break;
546     case OP_COMPOSE_EDIT_FCC:
547       m_strcpy(buf, sizeof(buf), fcc);
548       if (mutt_get_field ("Fcc: ", buf, sizeof (buf), M_FILE | M_CLEAR) == 0) {
549         m_strcpy(fcc, _POSIX_PATH_MAX, buf);
550         mutt_pretty_mailbox (fcc);
551         wmove (main_w, HDR_FCC, HDR_XOFFSET);
552         mutt_paddstr (main_w, W, fcc);
553         fccSet = 1;
554       }
555       MAYBE_REDRAW (menu->redraw);
556       mutt_message_hook (NULL, msg, M_SEND2HOOK);
557       break;
558     case OP_COMPOSE_EDIT_MESSAGE:
559       if (!option (OPTEDITHDRS)) {
560         mutt_edit_file(msg->content->filename);
561         mutt_update_encoding (msg->content);
562         menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
563         mutt_message_hook (NULL, msg, M_SEND2HOOK);
564         break;
565       }
566       /* fall through */
567     case OP_COMPOSE_EDIT_HEADERS:
568       if ((op == OP_COMPOSE_EDIT_HEADERS ||
569            (op == OP_COMPOSE_EDIT_MESSAGE && option (OPTEDITHDRS)))) {
570         const char *tag = NULL;
571         char *err = NULL;
572
573         mutt_env_to_local (msg->env);
574         mutt_edit_headers(msg->content->filename, msg, fcc, fcclen);
575         if (mutt_env_to_idna (msg->env, &tag, &err)) {
576           mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
577           p_delete(&err);
578         }
579       }
580       mutt_update_encoding (msg->content);
581
582       /* attachments may have been added */
583       if (idxlen && idx[idxlen - 1]->content->next) {
584         for (i = 0; i < idxlen; i++)
585           p_delete(&idx[i]);
586         idxlen = 0;
587         idx =
588           mutt_gen_attach_list (msg->content, -1, idx, &idxlen, &idxmax, 0,
589                                 1);
590         menu->data = idx;
591         menu->max = idxlen;
592       }
593
594       menu->redraw = REDRAW_FULL;
595       mutt_message_hook (NULL, msg, M_SEND2HOOK);
596       break; 
597
598     case OP_COMPOSE_ATTACH_FILE:
599       {
600         const char *prompt;
601         char **files;
602         int error, numfiles;
603
604         fname[0] = 0;
605         prompt = _("Attach file");
606         numfiles = 0;
607         files = NULL;
608
609         if (_mutt_enter_fname
610             (prompt, fname, sizeof (fname), &menu->redraw, 0, 1, &files,
611              &numfiles) == -1 || *fname == '\0')
612           break;
613
614         if (idxlen + numfiles >= idxmax) {
615           p_realloc(&idx, idxmax += 5 + numfiles);
616           menu->data = idx;
617         }
618
619         error = 0;
620         if (numfiles > 1)
621           mutt_message _("Attaching selected files...");
622
623         for (i = 0; i < numfiles; i++) {
624           char *att = files[i];
625
626           idx[idxlen] = p_new(ATTACHPTR, 1);
627           idx[idxlen]->unowned = 1;
628           idx[idxlen]->content = mutt_make_file_attach (att);
629           if (idx[idxlen]->content != NULL)
630             update_idx (menu, idx, idxlen++);
631           else {
632             error = 1;
633             mutt_error (_("Unable to attach %s!"), att);
634             p_delete(&idx[idxlen]);
635           }
636         }
637
638         p_delete(&files);
639         if (!error)
640           mutt_clear_error ();
641
642         menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
643       }
644       mutt_message_hook (NULL, msg, M_SEND2HOOK);
645       break;
646
647     case OP_COMPOSE_ATTACH_MESSAGE:
648 #ifdef USE_NNTP
649     case OP_COMPOSE_ATTACH_NEWS_MESSAGE:
650 #endif
651       {
652         const char *prompt;
653         HEADER *h;
654
655         fname[0] = 0;
656         prompt = _("Open mailbox to attach message from");
657
658 #ifdef USE_NNTP
659         unset_option (OPTNEWS);
660         if (op == OP_COMPOSE_ATTACH_NEWS_MESSAGE) {
661           if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)))
662             break;
663
664           prompt = _("Open newsgroup to attach message from");
665           set_option (OPTNEWS);
666         }
667 #endif
668
669         if (Context)
670 #ifdef USE_NNTP
671           if ((op == OP_COMPOSE_ATTACH_MESSAGE) ^ (Context->magic == M_NNTP))
672 #endif
673           {
674             m_strcpy(fname, sizeof(fname), NONULL(Context->path));
675             mutt_pretty_mailbox (fname);
676           }
677
678         if (mutt_enter_fname (prompt, fname, sizeof (fname), &menu->redraw, 1)
679             == -1 || !fname[0])
680           break;
681
682 #ifdef USE_NNTP
683         if (option (OPTNEWS))
684           nntp_expand_path (fname, sizeof (fname),
685                             &CurrentNewsSrv->conn->account);
686         else
687 #endif
688           mutt_expand_path (fname, sizeof (fname));
689         if (mx_get_magic (fname) != M_IMAP)
690           if (mx_get_magic (fname) != M_POP)
691 #ifdef USE_NNTP
692             if (mx_get_magic (fname) != M_NNTP && !option (OPTNEWS))
693 #endif
694               /* check to make sure the file exists and is readable */
695               if (access (fname, R_OK) == -1) {
696                 mutt_perror (fname);
697                 break;
698               }
699
700         menu->redraw = REDRAW_FULL;
701
702         ctx = mx_open_mailbox (fname, M_READONLY, NULL);
703         if (ctx == NULL) {
704           mutt_perror (fname);
705           break;
706         }
707
708         if (!ctx->msgcount) {
709           mx_close_mailbox (ctx, NULL);
710           p_delete(&ctx);
711           mutt_error _("No messages in that folder.");
712
713           break;
714         }
715
716         this = Context;         /* remember current folder and sort methods */
717         oldSort = Sort;
718         oldSortAux = SortAux;
719
720         Context = ctx;
721         set_option (OPTATTACHMSG);
722         mutt_message _("Tag the messages you want to attach!");
723
724         closed = mutt_index_menu ();
725         unset_option (OPTATTACHMSG);
726
727         if (!Context) {
728           /* go back to the folder we started from */
729           Context = this;
730           /* Restore old $sort and $sort_aux */
731           Sort = oldSort;
732           SortAux = oldSortAux;
733           menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
734           break;
735         }
736
737         if (idxlen + Context->tagged >= idxmax) {
738           p_realloc(&idx, idxmax += 5 + Context->tagged);
739           menu->data = idx;
740         }
741
742         for (i = 0; i < Context->msgcount; i++) {
743           h = Context->hdrs[i];
744           if (h->tagged) {
745             idx[idxlen] = p_new(ATTACHPTR, 1);
746             idx[idxlen]->content = mutt_make_message_attach (Context, h, 1);
747             if (idx[idxlen]->content != NULL)
748               update_idx (menu, idx, idxlen++);
749             else {
750               mutt_error _("Unable to attach!");
751
752               p_delete(&idx[idxlen]);
753             }
754           }
755         }
756         menu->redraw |= REDRAW_FULL;
757
758         if (closed == OP_QUIT)
759           mx_close_mailbox (Context, NULL);
760         else
761           mx_fastclose_mailbox (Context);
762         p_delete(&Context);
763
764         /* go back to the folder we started from */
765         Context = this;
766         /* Restore old $sort and $sort_aux */
767         Sort = oldSort;
768         SortAux = oldSortAux;
769       }
770       mutt_message_hook (NULL, msg, M_SEND2HOOK);
771       break;
772
773     case OP_DELETE:
774       CHECK_COUNT;
775       if (idx[menu->current]->unowned)
776         idx[menu->current]->content->unlink = 0;
777       if (delete_attachment (menu, &idxlen, menu->current) == -1)
778         break;
779       mutt_update_tree (idx, idxlen);
780       if (idxlen) {
781         if (menu->current > idxlen - 1)
782           menu->current = idxlen - 1;
783       }
784       else
785         menu->current = 0;
786
787       if (menu->current == 0)
788         msg->content = idx[0]->content;
789
790       menu->redraw |= REDRAW_STATUS;
791       mutt_message_hook (NULL, msg, M_SEND2HOOK);
792       break;
793
794 #define CURRENT idx[menu->current]->content
795
796     case OP_COMPOSE_TOGGLE_RECODE:
797       {
798         CHECK_COUNT;
799         if (!mutt_is_text_part (CURRENT)) {
800           mutt_error (_("Recoding only affects text attachments."));
801           break;
802         }
803         CURRENT->noconv = !CURRENT->noconv;
804         if (CURRENT->noconv)
805           mutt_message (_("The current attachment won't be converted."));
806         else
807           mutt_message (_("The current attachment will be converted."));
808         menu->redraw = REDRAW_CURRENT;
809         mutt_message_hook (NULL, msg, M_SEND2HOOK);
810         break;
811       }
812 #undef CURRENT
813
814     case OP_COMPOSE_EDIT_DESCRIPTION:
815       CHECK_COUNT;
816       m_strcpy(buf, sizeof(buf),
817                NONULL(idx[menu->current]->content->description));
818       /* header names should not be translated */
819       if (mutt_get_field ("Description: ", buf, sizeof (buf), 0) == 0) {
820         m_strreplace(&idx[menu->current]->content->description, buf);
821         menu->redraw = REDRAW_CURRENT;
822       }
823       mutt_message_hook (NULL, msg, M_SEND2HOOK);
824       break;
825
826     case OP_COMPOSE_UPDATE_ENCODING:
827       CHECK_COUNT;
828       if (menu->tagprefix) {
829         BODY *top;
830
831         for (top = msg->content; top; top = top->next) {
832           if (top->tagged)
833             mutt_update_encoding (top);
834         }
835         menu->redraw = REDRAW_FULL;
836       }
837       else {
838         mutt_update_encoding (idx[menu->current]->content);
839         menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
840       }
841       mutt_message_hook (NULL, msg, M_SEND2HOOK);
842       break;
843
844     case OP_COMPOSE_TOGGLE_DISPOSITION:
845       /* toggle the content-disposition between inline/attachment */
846       idx[menu->current]->content->disposition =
847         (idx[menu->current]->content->disposition ==
848          DISPINLINE) ? DISPATTACH : DISPINLINE;
849       menu->redraw = REDRAW_CURRENT;
850       break;
851
852     case OP_EDIT_TYPE:
853       CHECK_COUNT;
854       {
855         mutt_edit_content_type (NULL, idx[menu->current]->content, NULL);
856
857         /* this may have been a change to text/something */
858         mutt_update_encoding (idx[menu->current]->content);
859
860         menu->redraw = REDRAW_CURRENT;
861       }
862       mutt_message_hook (NULL, msg, M_SEND2HOOK);
863       break;
864
865     case OP_COMPOSE_EDIT_ENCODING:
866       CHECK_COUNT;
867       m_strcpy(buf, sizeof(buf),
868                ENCODING(idx[menu->current]->content->encoding));
869       if (mutt_get_field ("Content-Transfer-Encoding: ", buf,
870                           sizeof (buf), 0) == 0 && buf[0]) {
871         if ((i = mutt_check_encoding (buf)) != ENCOTHER && i != ENCUUENCODED) {
872           idx[menu->current]->content->encoding = i;
873           menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
874           mutt_clear_error ();
875         }
876         else
877           mutt_error _("Invalid encoding.");
878       }
879       mutt_message_hook (NULL, msg, M_SEND2HOOK);
880       break;
881
882     case OP_COMPOSE_SEND_MESSAGE:
883
884       /* Note: We don't invoke send2-hook here, since we want to leave
885        * users an opportunity to change settings from the ":" prompt.
886        */
887
888       if (check_attachments (idx, idxlen) != 0) {
889         menu->redraw = REDRAW_FULL;
890         break;
891       }
892
893       if (!fccSet && *fcc) {
894         if ((i = query_quadoption (OPT_COPY,
895                                    _("Save a copy of this message?"))) == -1)
896           break;
897         else if (i == M_NO)
898           *fcc = 0;
899       }
900
901       loop = 0;
902       r = 0;
903       break;
904
905     case OP_COMPOSE_EDIT_FILE:
906       CHECK_COUNT;
907       mutt_edit_file(idx[menu->current]->content->filename);
908       mutt_update_encoding (idx[menu->current]->content);
909       menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
910       mutt_message_hook (NULL, msg, M_SEND2HOOK);
911       break;
912
913     case OP_COMPOSE_TOGGLE_UNLINK:
914       CHECK_COUNT;
915       idx[menu->current]->content->unlink =
916         !idx[menu->current]->content->unlink;
917
918       menu->redraw = REDRAW_INDEX;
919       /* No send2hook since this doesn't change the message. */
920       break;
921
922     case OP_COMPOSE_GET_ATTACHMENT:
923       CHECK_COUNT;
924       if (menu->tagprefix) {
925         BODY *top;
926
927         for (top = msg->content; top; top = top->next) {
928           if (top->tagged)
929             mutt_get_tmp_attachment (top);
930         }
931         menu->redraw = REDRAW_FULL;
932       }
933       else if (mutt_get_tmp_attachment (idx[menu->current]->content) == 0)
934         menu->redraw = REDRAW_CURRENT;
935
936       /* No send2hook since this doesn't change the message. */
937       break;
938
939     case OP_COMPOSE_RENAME_FILE:
940       CHECK_COUNT;
941       m_strcpy(fname, sizeof(fname), idx[menu->current]->content->filename);
942       mutt_pretty_mailbox (fname);
943       if (mutt_get_field (_("Rename to: "), fname, sizeof (fname), M_FILE)
944           == 0 && fname[0]) {
945         if (stat (idx[menu->current]->content->filename, &st) == -1) {
946           mutt_error (_("Can't stat %s: %s"), fname, strerror (errno));
947           break;
948         }
949
950         mutt_expand_path (fname, sizeof (fname));
951         if (mutt_rename_file (idx[menu->current]->content->filename, fname))
952           break;
953
954         m_strreplace(&idx[menu->current]->content->filename, fname);
955         menu->redraw = REDRAW_CURRENT;
956
957         if (idx[menu->current]->content->stamp >= st.st_mtime)
958           mutt_stamp_attachment (idx[menu->current]->content);
959
960       }
961       mutt_message_hook (NULL, msg, M_SEND2HOOK);
962       break;
963
964     case OP_COMPOSE_NEW_MIME:
965       {
966         char type[STRING];
967         char *p;
968         int itype;
969         FILE *fp;
970
971         CLEARLINE(main_w, LINES - 1);
972         fname[0] = 0;
973         if (mutt_get_field (_("New file: "), fname, sizeof (fname), M_FILE)
974             != 0 || !fname[0])
975           continue;
976         mutt_expand_path (fname, sizeof (fname));
977
978         /* Call to lookup_mime_type () ?  maybe later */
979         type[0] = 0;
980         if (mutt_get_field ("Content-Type: ", type, sizeof (type), 0) != 0
981             || !type[0])
982           continue;
983
984         if (!(p = strchr (type, '/'))) {
985           mutt_error _("Content-Type is of the form base/sub");
986
987           continue;
988         }
989         *p++ = 0;
990         if ((itype = mutt_check_mime_type (type)) == TYPEOTHER) {
991           mutt_error (_("Unknown Content-Type %s"), type);
992           continue;
993         }
994         if (idxlen == idxmax) {
995           p_realloc(&idx, idxmax += 5);
996           menu->data = idx;
997         }
998
999         idx[idxlen] = p_new(ATTACHPTR, 1);
1000         /* Touch the file */
1001         if (!(fp = safe_fopen (fname, "w"))) {
1002           mutt_error (_("Can't create file %s"), fname);
1003           p_delete(&idx[idxlen]);
1004           continue;
1005         }
1006         m_fclose(&fp);
1007
1008         if ((idx[idxlen]->content = mutt_make_file_attach (fname)) == NULL) {
1009           mutt_error
1010             _("What we have here is a failure to make an attachment");
1011           continue;
1012         }
1013         update_idx (menu, idx, idxlen++);
1014
1015         idx[menu->current]->content->type = itype;
1016         m_strreplace(&idx[menu->current]->content->subtype, p);
1017         idx[menu->current]->content->unlink = 1;
1018         menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
1019
1020         if (mutt_compose_attachment (idx[menu->current]->content)) {
1021           mutt_update_encoding (idx[menu->current]->content);
1022           menu->redraw = REDRAW_FULL;
1023         }
1024       }
1025       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1026       break;
1027
1028     case OP_COMPOSE_EDIT_MIME:
1029       CHECK_COUNT;
1030       if (mutt_edit_attachment (idx[menu->current]->content)) {
1031         mutt_update_encoding (idx[menu->current]->content);
1032         menu->redraw = REDRAW_FULL;
1033       }
1034       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1035       break;
1036
1037     case OP_VIEW_ATTACH:
1038     case OP_DISPLAY_HEADERS:
1039       CHECK_COUNT;
1040       mutt_attach_display_loop (menu, op, NULL, NULL, NULL, &idx, &idxlen,
1041                                 NULL, 0);
1042       menu->redraw = REDRAW_FULL;
1043       /* no send2hook, since this doesn't modify the message */
1044       break;
1045
1046     case OP_SAVE:
1047       CHECK_COUNT;
1048       mutt_save_attachment_list (NULL, menu->tagprefix,
1049                                  menu->tagprefix ? msg->content : idx[menu->
1050                                                                       current]->
1051                                  content, NULL, menu);
1052       MAYBE_REDRAW (menu->redraw);
1053       /* no send2hook, since this doesn't modify the message */
1054       break;
1055
1056     case OP_PRINT:
1057       CHECK_COUNT;
1058       mutt_print_attachment_list (NULL, menu->tagprefix,
1059                                   menu->tagprefix ? msg->content : idx[menu->
1060                                                                        current]->
1061                                   content);
1062       /* no send2hook, since this doesn't modify the message */
1063       break;
1064
1065     case OP_PIPE:
1066     case OP_FILTER:
1067       CHECK_COUNT;
1068       mutt_pipe_attachment_list (NULL, menu->tagprefix,
1069                                  menu->tagprefix ? msg->content : idx[menu->
1070                                                                       current]->
1071                                  content, op == OP_FILTER);
1072       if (op == OP_FILTER)      /* cte might have changed */
1073         menu->redraw = menu->tagprefix ? REDRAW_FULL : REDRAW_CURRENT;
1074       menu->redraw |= REDRAW_STATUS;
1075       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1076       break;
1077
1078     case OP_EXIT:
1079       if ((i =
1080            query_quadoption (OPT_POSTPONE,
1081                              _("Postpone this message?"))) == M_NO) {
1082         while (idxlen-- > 0) {
1083           /* avoid freeing other attachments */
1084           idx[idxlen]->content->next = NULL;
1085           idx[idxlen]->content->parts = NULL;
1086           if (idx[idxlen]->unowned)
1087             idx[idxlen]->content->unlink = 0;
1088           body_list_wipe(&idx[idxlen]->content);
1089           p_delete(&idx[idxlen]->tree);
1090           p_delete(&idx[idxlen]);
1091         }
1092         p_delete(&idx);
1093         idxlen = 0;
1094         idxmax = 0;
1095         r = -1;
1096         loop = 0;
1097         break;
1098       }
1099       else if (i == -1)
1100         break;                  /* abort */
1101
1102       /* fall through to postpone! */
1103
1104     case OP_COMPOSE_POSTPONE_MESSAGE:
1105
1106       if (check_attachments (idx, idxlen) != 0) {
1107         menu->redraw = REDRAW_FULL;
1108         break;
1109       }
1110
1111       loop = 0;
1112       r = 1;
1113       break;
1114
1115     case OP_COMPOSE_WRITE_MESSAGE:
1116       fname[0] = '\0';
1117       if (Context) {
1118         m_strcpy(fname, sizeof(fname), NONULL(Context->path));
1119         mutt_pretty_mailbox (fname);
1120       }
1121       if (idxlen)
1122         msg->content = idx[0]->content;
1123       if (mutt_enter_fname
1124           (_("Write message to mailbox"), fname, sizeof (fname),
1125            &menu->redraw, 1) != -1 && fname[0]) {
1126         mutt_message (_("Writing message to %s ..."), fname);
1127         mutt_expand_path (fname, sizeof (fname));
1128
1129         if (msg->content->next)
1130           msg->content = mutt_make_multipart (msg->content);
1131
1132         if (mutt_write_fcc (NONULL (fname), msg, NULL, 1, NULL) < 0)
1133           msg->content = mutt_remove_multipart (msg->content);
1134         else
1135           mutt_message _("Message written.");
1136       }
1137       break;
1138
1139     case OP_COMPOSE_PGP_MENU:
1140       if (msg->security & APPLICATION_SMIME) {
1141         if (mutt_yesorno (_("S/MIME already selected. Clear & continue ? "),
1142                           M_YES) != M_YES) {
1143           mutt_clear_error ();
1144           break;
1145         }
1146         msg->security = 0;
1147       }
1148       msg->security = crypt_send_menu (msg, &menu->redraw, 0);
1149       redraw_crypt_lines (msg);
1150       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1151       break;
1152
1153     case OP_COMPOSE_SMIME_MENU:
1154       if (msg->security & APPLICATION_PGP) {
1155         if (mutt_yesorno (_("PGP already selected. Clear & continue ? "),
1156                           M_YES) != M_YES) {
1157           mutt_clear_error ();
1158           break;
1159         }
1160         msg->security = 0;
1161       }
1162       msg->security = crypt_send_menu(msg, &menu->redraw, 1);
1163       redraw_crypt_lines (msg);
1164       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1165       break;
1166     }
1167
1168     /* Draw formated compose status line */
1169     if (menu->redraw & REDRAW_STATUS) {
1170       compose_status_line (buf, sizeof (buf), menu, NONULL (ComposeFormat));
1171       CLEARLINE(main_w, 0);
1172       SETCOLOR(main_w, MT_COLOR_STATUS);
1173       wmove (main_w, 0, 0);
1174       wprintw (main_w, "%-*.*s", getmaxx(main_w), getmaxx(main_w), buf);
1175       SETCOLOR(main_w, MT_COLOR_NORMAL);
1176       menu->redraw &= ~REDRAW_STATUS;
1177     }
1178   }
1179
1180   mutt_menuDestroy (&menu);
1181
1182   if (idxlen) {
1183     msg->content = idx[0]->content;
1184     for (i = 0; i < idxlen; i++) {
1185       idx[i]->content->aptr = NULL;
1186       p_delete(&idx[i]);
1187     }
1188   } else {
1189     msg->content = NULL;
1190   }
1191
1192   p_delete(&idx);
1193
1194   return (r);
1195 }