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