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