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