implement saving of outgoing messages to current folder.
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkmenu.h>
30 #include <gtk/gtkmenuitem.h>
31 #include <gtk/gtkitemfactory.h>
32 #include <gtk/gtkcheckmenuitem.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkclist.h>
36 #include <gtk/gtkctree.h>
37 #include <gtk/gtkvpaned.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkeditable.h>
40 #include <gtk/gtkwindow.h>
41 #include <gtk/gtksignal.h>
42 #include <gtk/gtkvbox.h>
43 #include <gtk/gtkcontainer.h>
44 #include <gtk/gtkhandlebox.h>
45 #include <gtk/gtktoolbar.h>
46 #include <gtk/gtktable.h>
47 #include <gtk/gtkhbox.h>
48 #include <gtk/gtklabel.h>
49 #include <gtk/gtkscrolledwindow.h>
50 #include <gtk/gtkthemes.h>
51 #include <gtk/gtkdnd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include <unistd.h>
57 #include <time.h>
58 /* #include <sys/utsname.h> */
59 #include <stdlib.h>
60 #include <sys/wait.h>
61 #include <signal.h>
62 #include <errno.h>
63
64 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
65 #  include <wchar.h>
66 #  include <wctype.h>
67 #endif
68
69
70 #include "gtkstext.h"
71
72 #include "intl.h"
73 #include "main.h"
74 #include "mainwindow.h"
75 #include "compose.h"
76 #include "addressbook.h"
77 #include "folderview.h"
78 #include "procmsg.h"
79 #include "menu.h"
80 #include "send.h"
81 #include "imap.h"
82 #include "news.h"
83 #include "customheader.h"
84 #include "prefs_common.h"
85 #include "prefs_account.h"
86 #include "account.h"
87 #include "filesel.h"
88 #include "procheader.h"
89 #include "procmime.h"
90 #include "statusbar.h"
91 #include "about.h"
92 #include "base64.h"
93 #include "codeconv.h"
94 #include "utils.h"
95 #include "gtkutils.h"
96 #include "socket.h"
97 #include "alertpanel.h"
98 #include "manage_window.h"
99 #include "gtkshruler.h"
100 #include "folder.h"
101 #include "addr_compl.h"
102 #include "quote_fmt.h"
103 #include "template.h"
104 #include "undo.h"
105 #include "foldersel.h"
106
107 #if USE_GPGME
108 #  include "rfc2015.h"
109 #endif
110
111 typedef enum
112 {
113         COL_MIMETYPE = 0,
114         COL_SIZE     = 1,
115         COL_NAME     = 2
116 } AttachColumnPos;
117
118 #define N_ATTACH_COLS           3
119
120 #define B64_LINE_SIZE           57
121 #define B64_BUFFSIZE            77
122
123 #define MAX_REFERENCES_LEN      999
124
125 static GdkColor quote_color = {0, 0, 0, 0xbfff};
126
127 static GList *compose_list = NULL;
128
129 Compose *compose_generic_new                    (PrefsAccount   *account,
130                                                  const gchar    *to,
131                                                  FolderItem     *item);
132
133 static Compose *compose_create                  (PrefsAccount   *account,
134                                                  ComposeMode     mode);
135 static void compose_toolbar_create              (Compose        *compose,
136                                                  GtkWidget      *container);
137 static GtkWidget *compose_account_option_menu_create
138                                                 (Compose        *compose);
139 static void compose_set_template_menu           (Compose        *compose);
140 static void compose_template_apply              (Compose        *compose,
141                                                  Template       *tmpl);
142 static void compose_destroy                     (Compose        *compose);
143
144 static void compose_entries_set                 (Compose        *compose,
145                                                  const gchar    *mailto);
146 static gint compose_parse_header                (Compose        *compose,
147                                                  MsgInfo        *msginfo);
148 static gchar *compose_parse_references          (const gchar    *ref,
149                                                  const gchar    *msgid);
150
151 static gchar *compose_quote_fmt                 (Compose        *compose,
152                                                  MsgInfo        *msginfo,
153                                                  const gchar    *fmt,
154                                                  const gchar    *qmark);
155
156 static void compose_reply_set_entry             (Compose        *compose,
157                                                  MsgInfo        *msginfo,
158                                                  gboolean        to_all,
159                                                  gboolean        to_sender,
160                                                  gboolean
161                                                  followup_and_reply_to);
162 static void compose_reedit_set_entry            (Compose        *compose,
163                                                  MsgInfo        *msginfo);
164 static void compose_insert_sig                  (Compose        *compose);
165 static void compose_insert_file                 (Compose        *compose,
166                                                  const gchar    *file);
167 static void compose_attach_append               (Compose        *compose,
168                                                  const gchar    *file,
169                                                  ContentType     cnttype);
170 static void compose_attach_append_with_type(Compose *compose,
171                                             const gchar *file,
172                                             const gchar *type,
173                                             ContentType cnttype);
174 static void compose_wrap_line                   (Compose        *compose);
175 static void compose_wrap_line_all               (Compose        *compose);
176 static void compose_set_title                   (Compose        *compose);
177
178 static PrefsAccount *compose_current_mail_account(void);
179 /* static gint compose_send                     (Compose        *compose); */
180 static gint compose_write_to_file               (Compose        *compose,
181                                                  const gchar    *file,
182                                                  gboolean        is_draft);
183 static gint compose_write_body_to_file          (Compose        *compose,
184                                                  const gchar    *file);
185 static gint compose_save_to_outbox              (Compose        *compose,
186                                                  const gchar    *file);
187 static gint compose_remove_reedit_target        (Compose        *compose);
188 static gint compose_queue                       (Compose        *compose,
189                                                  gint           *msgnum,
190                                                  FolderItem     **item);
191 static void compose_write_attach                (Compose        *compose,
192                                                  FILE           *fp);
193 static gint compose_write_headers               (Compose        *compose,
194                                                  FILE           *fp,
195                                                  const gchar    *charset,
196                                                  EncodingType    encoding,
197                                                  gboolean        is_draft);
198
199 static void compose_convert_header              (gchar          *dest,
200                                                  gint            len,
201                                                  gchar          *src,
202                                                  gint            header_len);
203 static void compose_generate_msgid              (Compose        *compose,
204                                                  gchar          *buf,
205                                                  gint            len);
206
207 static void compose_attach_info_free            (AttachInfo     *ainfo);
208 static void compose_attach_remove_selected      (Compose        *compose);
209
210 static void compose_attach_property             (Compose        *compose);
211 static void compose_attach_property_create      (gboolean       *cancelled);
212 static void attach_property_ok                  (GtkWidget      *widget,
213                                                  gboolean       *cancelled);
214 static void attach_property_cancel              (GtkWidget      *widget,
215                                                  gboolean       *cancelled);
216 static gint attach_property_delete_event        (GtkWidget      *widget,
217                                                  GdkEventAny    *event,
218                                                  gboolean       *cancelled);
219 static void attach_property_key_pressed         (GtkWidget      *widget,
220                                                  GdkEventKey    *event,
221                                                  gboolean       *cancelled);
222
223 static void compose_exec_ext_editor             (Compose           *compose);
224 static gint compose_exec_ext_editor_real        (const gchar       *file);
225 static gboolean compose_ext_editor_kill         (Compose           *compose);
226 static void compose_input_cb                    (gpointer           data,
227                                                  gint               source,
228                                                  GdkInputCondition  condition);
229 static void compose_set_ext_editor_sensitive    (Compose           *compose,
230                                                  gboolean           sensitive);
231
232 static void compose_set_undo                    (UndoMain       *undostruct,
233                                                  gint            undo_state,
234                                                  gint            redo_state,
235                                                  GtkWidget      *changewidget);
236
237 static gint calc_cursor_xpos    (GtkSText       *text,
238                                  gint            extra,
239                                  gint            char_width);
240
241 static void compose_create_header_entry (Compose *compose);
242 static void compose_add_header_entry    (Compose *compose, gchar *header, gchar *text);
243
244 /* callback functions */
245
246 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
247                                          GtkAllocation  *allocation,
248                                          GtkSHRuler     *shruler);
249
250 static void toolbar_send_cb             (GtkWidget      *widget,
251                                          gpointer        data);
252 static void toolbar_send_later_cb       (GtkWidget      *widget,
253                                          gpointer        data);
254 static void toolbar_draft_cb            (GtkWidget      *widget,
255                                          gpointer        data);
256 static void toolbar_insert_cb           (GtkWidget      *widget,
257                                          gpointer        data);
258 static void toolbar_attach_cb           (GtkWidget      *widget,
259                                          gpointer        data);
260 static void toolbar_sig_cb              (GtkWidget      *widget,
261                                          gpointer        data);
262 static void toolbar_ext_editor_cb       (GtkWidget      *widget,
263                                          gpointer        data);
264 static void toolbar_linewrap_cb         (GtkWidget      *widget,
265                                          gpointer        data);
266 static void toolbar_address_cb          (GtkWidget      *widget,
267                                          gpointer        data);
268
269 static void select_account              (Compose        *compose,
270                                          PrefsAccount   *ac);
271
272 static void account_activated           (GtkMenuItem    *menuitem,
273                                          gpointer        data);
274
275 static void attach_selected             (GtkCList       *clist,
276                                          gint            row,
277                                          gint            column,
278                                          GdkEvent       *event,
279                                          gpointer        data);
280 static void attach_button_pressed       (GtkWidget      *widget,
281                                          GdkEventButton *event,
282                                          gpointer        data);
283 static void attach_key_pressed          (GtkWidget      *widget,
284                                          GdkEventKey    *event,
285                                          gpointer        data);
286
287 static void compose_send_cb             (gpointer        data,
288                                          guint           action,
289                                          GtkWidget      *widget);
290 static void compose_send_later_cb       (gpointer        data,
291                                          guint           action,
292                                          GtkWidget      *widget);
293
294 static void compose_draft_cb            (gpointer        data,
295                                          guint           action,
296                                          GtkWidget      *widget);
297
298 static void compose_attach_cb           (gpointer        data,
299                                          guint           action,
300                                          GtkWidget      *widget);
301 static void compose_insert_file_cb      (gpointer        data,
302                                          guint           action,
303                                          GtkWidget      *widget);
304
305 static void compose_close_cb            (gpointer        data,
306                                          guint           action,
307                                          GtkWidget      *widget);
308
309 static void compose_address_cb          (gpointer        data,
310                                          guint           action,
311                                          GtkWidget      *widget);
312 static void compose_template_activate_cb(GtkWidget      *widget,
313                                          gpointer        data);
314
315 static void compose_ext_editor_cb       (gpointer        data,
316                                          guint           action,
317                                          GtkWidget      *widget);
318
319 static gint compose_delete_cb           (GtkWidget      *widget,
320                                          GdkEventAny    *event,
321                                          gpointer        data);
322 static void compose_destroy_cb          (GtkWidget      *widget,
323                                          Compose        *compose);
324
325 static void compose_undo_cb             (Compose        *compose);
326 static void compose_redo_cb             (Compose        *compose);
327 static void compose_cut_cb              (Compose        *compose);
328 static void compose_copy_cb             (Compose        *compose);
329 static void compose_paste_cb            (Compose        *compose);
330 static void compose_allsel_cb           (Compose        *compose);
331
332 static void compose_grab_focus_cb       (GtkWidget      *widget,
333                                          Compose        *compose);
334
335 static void compose_changed_cb          (GtkEditable    *editable,
336                                          Compose        *compose);
337 static void compose_button_press_cb     (GtkWidget      *widget,
338                                          GdkEventButton *event,
339                                          Compose        *compose);
340 #if 0
341 static void compose_key_press_cb        (GtkWidget      *widget,
342                                          GdkEventKey    *event,
343                                          Compose        *compose);
344 #endif
345
346 #if 0
347 static void compose_toggle_to_cb        (gpointer        data,
348                                          guint           action,
349                                          GtkWidget      *widget);
350 static void compose_toggle_cc_cb        (gpointer        data,
351                                          guint           action,
352                                          GtkWidget      *widget);
353 static void compose_toggle_bcc_cb       (gpointer        data,
354                                          guint           action,
355                                          GtkWidget      *widget);
356 static void compose_toggle_replyto_cb   (gpointer        data,
357                                          guint           action,
358                                          GtkWidget      *widget);
359 static void compose_toggle_followupto_cb(gpointer        data,
360                                          guint           action,
361                                          GtkWidget      *widget);
362 static void compose_toggle_attach_cb    (gpointer        data,
363                                          guint           action,
364                                          GtkWidget      *widget);
365 #endif
366 static void compose_toggle_ruler_cb     (gpointer        data,
367                                          guint           action,
368                                          GtkWidget      *widget);
369 #if USE_GPGME
370 static void compose_toggle_sign_cb      (gpointer        data,
371                                          guint           action,
372                                          GtkWidget      *widget);
373 static void compose_toggle_encrypt_cb   (gpointer        data,
374                                          guint           action,
375                                          GtkWidget      *widget);
376 #endif
377 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
378                                              GtkWidget *widget);
379
380 static void compose_attach_drag_received_cb (GtkWidget          *widget,
381                                              GdkDragContext     *drag_context,
382                                              gint                x,
383                                              gint                y,
384                                              GtkSelectionData   *data,
385                                              guint               info,
386                                              guint               time,
387                                              gpointer            user_data);
388 static void compose_insert_drag_received_cb (GtkWidget          *widget,
389                                              GdkDragContext     *drag_context,
390                                              gint                x,
391                                              gint                y,
392                                              GtkSelectionData   *data,
393                                              guint               info,
394                                              guint               time,
395                                              gpointer            user_data);
396
397 #if 0
398 static void to_activated                (GtkWidget      *widget,
399                                          Compose        *compose);
400 static void newsgroups_activated        (GtkWidget      *widget,
401                                          Compose        *compose);
402 static void subject_activated           (GtkWidget      *widget,
403                                          Compose        *compose);
404 static void cc_activated                (GtkWidget      *widget,
405                                          Compose        *compose);
406 static void bcc_activated               (GtkWidget      *widget,
407                                          Compose        *compose);
408 static void replyto_activated           (GtkWidget      *widget,
409                                          Compose        *compose);
410 static void followupto_activated        (GtkWidget      *widget,
411                                          Compose        *compose);
412 #endif
413
414 static void compose_attach_parts        (Compose        *compose,
415                                          MsgInfo        *msginfo);
416
417 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
418                                   gboolean to_all,
419                                   gboolean ignore_replyto,
420                                   gboolean followup_and_reply_to);
421
422 void compose_headerentry_changed_cb        (GtkWidget          *entry,
423                                             ComposeHeaderEntry *headerentry);
424 void compose_headerentry_key_press_event_cb(GtkWidget          *entry,
425                                             GdkEventKey        *event,
426                                             ComposeHeaderEntry *headerentry);
427
428 static GtkItemFactoryEntry compose_popup_entries[] =
429 {
430         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
431         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
432         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
433         {N_("/_Property..."),   NULL, compose_attach_property, 0, NULL}
434 };
435
436 static GtkItemFactoryEntry compose_entries[] =
437 {
438         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
439         {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb,      0, NULL},
440         {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
441         {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig,     0, NULL},
442         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
443         {N_("/_File/_Close"),                   "<alt>W", compose_close_cb, 0, NULL},
444
445         {N_("/_Edit"),             NULL, NULL, 0, "<Branch>"},
446         {N_("/_Edit/_Undo"),       "<control>Z", compose_undo_cb, 0, NULL},
447         {N_("/_Edit/_Redo"),       "<control>Y", compose_redo_cb, 0, NULL},
448         {N_("/_Edit/---"),         NULL, NULL, 0, "<Separator>"},
449         {N_("/_Edit/Cu_t"),        "<control>X", compose_cut_cb,    0, NULL},
450         {N_("/_Edit/_Copy"),       "<control>C", compose_copy_cb,   0, NULL},
451         {N_("/_Edit/_Paste"),      "<control>V", compose_paste_cb,  0, NULL},
452         {N_("/_Edit/Select _all"), "<control>A", compose_allsel_cb, 0, NULL},
453         {N_("/_Edit/---"),         NULL, NULL, 0, "<Separator>"},
454         {N_("/_Edit/_Wrap current paragraph"), "<alt>L", compose_wrap_line, 0, NULL},
455         {N_("/_Edit/Wrap all long _lines"),
456                         "<shift><alt>L", compose_wrap_line_all, 0, NULL},
457         {N_("/_Edit/Edit with e_xternal editor"),
458                         "<alt>X", compose_ext_editor_cb, 0, NULL},
459
460         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
461         {N_("/_Message/_Send"),         "<control>Return",
462                                         compose_send_cb, 0, NULL},
463         {N_("/_Message/Send _later"),   "<shift><alt>S",
464                                         compose_send_later_cb,  0, NULL},
465         {N_("/_Message/Save to _draft folder"),
466                                         "<alt>D", compose_draft_cb, 0, NULL},
467 #if 0 /* NEW COMPOSE GUI */
468         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
469         {N_("/_Message/_To"),           NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
470         {N_("/_Message/_Cc"),           NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
471         {N_("/_Message/_Bcc"),          NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
472         {N_("/_Message/_Reply to"),     NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
473         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
474         {N_("/_Message/_Followup to"),  NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
475         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
476         {N_("/_Message/_Attach"),       NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
477 #endif
478 #if USE_GPGME
479         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
480         {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb   , 0, "<ToggleItem>"},
481         {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
482 #endif /* USE_GPGME */
483         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
484         {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
485         {N_("/_Tool"),                  NULL, NULL, 0, "<Branch>"},
486         {N_("/_Tool/Show _ruler"),      NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
487         {N_("/_Tool/_Address book"),    "<alt>A", compose_address_cb , 0, NULL},
488         {N_("/_Tool/_Template"),        NULL, NULL, 0, "<Branch>"},
489         {N_("/_Help"),                  NULL, NULL, 0, "<LastBranch>"},
490         {N_("/_Help/_About"),           NULL, about_show, 0, NULL}
491 };
492
493 static GtkTargetEntry compose_mime_types[] =
494 {
495         {"text/uri-list", 0, 0}
496 };
497
498 Compose *compose_new(PrefsAccount *account)
499 {
500         return compose_generic_new(account, NULL, NULL);
501 }
502
503 Compose *compose_bounce(PrefsAccount *account, MsgInfo *msginfo)
504 {
505         Compose *c;
506         gchar *filename;
507         GtkItemFactory *ifactory;
508         
509         c = compose_generic_new(account, NULL, NULL);
510
511         filename = procmsg_get_message_file(msginfo);
512         if (filename == NULL)
513                 return NULL;
514
515         c->bounce_filename = filename;
516
517         if (msginfo->subject)
518                 gtk_entry_set_text(GTK_ENTRY(c->subject_entry),
519                                    msginfo->subject);
520         gtk_editable_set_editable(GTK_EDITABLE(c->subject_entry), FALSE);
521
522         compose_quote_fmt(c, msginfo, "%M", NULL);
523         gtk_editable_set_editable(GTK_EDITABLE(c->text), FALSE);
524
525         ifactory = gtk_item_factory_from_widget(c->popupmenu);
526         menu_set_sensitive(ifactory, "/Add...", FALSE);
527         menu_set_sensitive(ifactory, "/Remove", FALSE);
528         menu_set_sensitive(ifactory, "/Property...", FALSE);
529
530         ifactory = gtk_item_factory_from_widget(c->menubar);
531         menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
532         menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
533         menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
534         menu_set_sensitive(ifactory, "/Edit/Paste", FALSE);
535         menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", FALSE);
536         menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", FALSE);
537         menu_set_sensitive(ifactory, "/Edit/Edit with external editor", FALSE);
538         menu_set_sensitive(ifactory, "/Message/Attach", FALSE);
539 #if USE_GPGME
540         menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
541         menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
542 #endif
543         menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
544         menu_set_sensitive(ifactory, "/Tool/Template", FALSE);
545         
546         gtk_widget_set_sensitive(c->insert_btn, FALSE);
547         gtk_widget_set_sensitive(c->attach_btn, FALSE);
548         gtk_widget_set_sensitive(c->sig_btn, FALSE);
549         gtk_widget_set_sensitive(c->exteditor_btn, FALSE);
550         gtk_widget_set_sensitive(c->linewrap_btn, FALSE);
551
552         return c;
553 }
554
555 Compose *compose_new_with_recipient(PrefsAccount *account, const gchar *mailto)
556 {
557         return compose_generic_new(account, mailto, NULL);
558 }
559
560 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
561 {
562         return compose_generic_new(account, NULL, item);
563 }
564
565 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item)
566 {
567         Compose *compose;
568
569         if (item && item->prefs && item->prefs->enable_default_account)
570                 account = account_find_from_id(item->prefs->default_account);
571
572         if (!account) account = cur_account;
573         g_return_val_if_fail(account != NULL, NULL);
574
575         compose = compose_create(account, COMPOSE_NEW);
576         compose->replyinfo = NULL;
577
578         if (prefs_common.auto_sig)
579                 compose_insert_sig(compose);
580         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
581         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
582
583         if (account->protocol != A_NNTP) {
584                 if (mailto) {
585                         compose_entries_set(compose, mailto);
586
587                 } else if(item && item->prefs->enable_default_to) {
588                         compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
589                 }
590                 if (item && item->ret_rcpt) {
591                         GtkItemFactory *ifactory;
592                 
593                         ifactory = gtk_item_factory_from_widget(compose->menubar);
594                         menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
595                 }
596         } else {
597                 if (mailto) {
598                         compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
599                 }
600         }
601
602         /* Set save folder */
603         if(item && item->prefs && item->prefs->save_copy_to_folder) {
604                 gchar *folderidentifier;
605
606                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
607                 folderidentifier = folder_item_get_identifier(item);
608                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
609                 g_free(folderidentifier);
610         }
611
612         gtk_widget_grab_focus(compose->subject_entry);
613
614         if (prefs_common.auto_exteditor)
615                 compose_exec_ext_editor(compose);
616
617         return compose;
618 }
619
620 #define CHANGE_FLAGS(msginfo) \
621 { \
622 if (msginfo->folder->folder->change_flags != NULL) \
623 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
624                                       msginfo->folder, \
625                                       msginfo); \
626 }
627
628 /*
629 Compose *compose_new_followup_and_replyto(PrefsAccount *account,
630                                            const gchar *followupto, gchar * to)
631 {
632         Compose *compose;
633
634         if (!account) account = cur_account;
635         g_return_val_if_fail(account != NULL, NULL);
636         g_return_val_if_fail(account->protocol != A_NNTP, NULL);
637
638         compose = compose_create(account, COMPOSE_NEW);
639
640         if (prefs_common.auto_sig)
641                 compose_insert_sig(compose);
642         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
643         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
644
645         compose_entry_append(compose, to, COMPOSE_TO);
646         compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
647         gtk_widget_grab_focus(compose->subject_entry);
648
649         return compose;
650 }
651 */
652
653 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
654                    gboolean ignore_replyto)
655 {
656         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, FALSE);
657 }
658
659 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
660                                    gboolean to_all,
661                                    gboolean ignore_replyto)
662 {
663         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, TRUE);
664 }
665
666 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
667                                   gboolean to_all,
668                                   gboolean ignore_replyto,
669                                   gboolean followup_and_reply_to)
670 {
671         Compose *compose;
672         PrefsAccount *account;
673         PrefsAccount *reply_account;
674         GtkSText *text;
675
676         g_return_if_fail(msginfo != NULL);
677         g_return_if_fail(msginfo->folder != NULL);
678
679         account = NULL;
680         /* select the account set in folderitem's property (if enabled) */
681         if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
682                 account = account_find_from_id(msginfo->folder->prefs->default_account);
683         
684         /* select the account for the whole folder (IMAP / NNTP) */
685         if (!account)
686                 account = msginfo->folder->folder->account;
687
688         /* select account by to: and cc: header if enabled */
689         if (prefs_common.reply_account_autosel) {
690                 if (!account && msginfo->to) {
691                         gchar *to;
692                         Xstrdup_a(to, msginfo->to, return);
693                         extract_address(to);
694                         account = account_find_from_address(to);
695                 }
696                 if (!account) {
697                         gchar cc[BUFFSIZE];
698                         if(!get_header_from_msginfo(msginfo, cc, sizeof(cc), "CC:")) { /* Found a CC header */
699                                 extract_address(cc);
700                                 account = account_find_from_address(cc);
701                         }        
702                 }
703         }
704
705         /* select current account */
706         if (!account) account = cur_account;
707         g_return_if_fail(account != NULL);
708
709         if (ignore_replyto && account->protocol == A_NNTP &&
710             !followup_and_reply_to) {
711                 reply_account =
712                         account_find_from_address(account->address);
713                 if (!reply_account)
714                         reply_account = compose_current_mail_account();
715                 if (!reply_account)
716                         return;
717         } else
718                 reply_account = account;
719
720         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
721         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
722         if (MSG_IS_IMAP(msginfo->flags))
723                 imap_msg_set_perm_flags(msginfo, MSG_REPLIED);
724         CHANGE_FLAGS(msginfo);
725
726         compose = compose_create(account, COMPOSE_REPLY);
727         compose->replyinfo = procmsg_msginfo_copy(msginfo);
728
729 #if 0 /* NEW COMPOSE GUI */
730         if (followup_and_reply_to) {
731                 gtk_widget_show(compose->to_hbox);
732                 gtk_widget_show(compose->to_entry);
733                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
734                 compose->use_to = TRUE;
735         }
736 #endif
737         if (msginfo->folder && msginfo->folder->ret_rcpt) {
738                 GtkItemFactory *ifactory;
739         
740                 ifactory = gtk_item_factory_from_widget(compose->menubar);
741                 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
742         }
743
744         /* Set save folder */
745         if(msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
746                 gchar *folderidentifier;
747
748                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
749                 folderidentifier = folder_item_get_identifier(msginfo->folder);
750                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
751                 g_free(folderidentifier);
752         }
753
754         if (compose_parse_header(compose, msginfo) < 0) return;
755         compose_reply_set_entry(compose, msginfo, to_all, ignore_replyto,
756                                 followup_and_reply_to);
757
758         text = GTK_STEXT(compose->text);
759         gtk_stext_freeze(text);
760
761         if (quote) {
762                 gchar *qmark;
763                 gchar *quote_str;
764
765                 if (prefs_common.quotemark && *prefs_common.quotemark)
766                         qmark = prefs_common.quotemark;
767                 else
768                         qmark = "> ";
769
770                 quote_str = compose_quote_fmt(compose, msginfo,
771                                               prefs_common.quotefmt,
772                                               qmark);
773         }
774
775         if (prefs_common.auto_sig)
776                 compose_insert_sig(compose);
777
778         if (quote && prefs_common.linewrap_quote)
779                 compose_wrap_line_all(compose);
780
781         gtk_editable_set_position(GTK_EDITABLE(text), 0);
782         gtk_stext_set_point(text, 0);
783
784         gtk_stext_thaw(text);
785         gtk_widget_grab_focus(compose->text);
786
787         if (prefs_common.auto_exteditor)
788                 compose_exec_ext_editor(compose);
789 }
790
791
792 static gchar *procmime_get_file_name(MimeInfo *mimeinfo)
793 {
794         gchar *base;
795         gchar *filename;
796
797         g_return_val_if_fail(mimeinfo != NULL, NULL);
798
799         base = mimeinfo->filename ? mimeinfo->filename
800                 : mimeinfo->name ? mimeinfo->name : NULL;
801
802         if (MIME_TEXT_HTML == mimeinfo->mime_type && base == NULL){
803                 filename = g_strdup_printf("%s%smimetmp.%08x.html",
804                                            get_mime_tmp_dir(),
805                                            G_DIR_SEPARATOR_S,
806                                            (gint)mimeinfo);
807                 return filename;
808         }
809         else {
810                 base = base ? base : "";
811                 base = g_basename(base);
812                 if (*base == '\0') {
813                         filename = g_strdup_printf("%s%smimetmp.%08x",
814                                                    get_mime_tmp_dir(),
815                                                    G_DIR_SEPARATOR_S,
816                                                    (gint)mimeinfo);
817                         return filename;
818                 }
819         }
820
821         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
822                                base, NULL);
823
824         return filename;
825 }
826
827 static gchar *mime_extract_file(gchar *source, MimeInfo *partinfo)
828 {
829         gchar *filename;
830
831         if (!partinfo) return NULL;
832
833         filename = procmime_get_file_name(partinfo);
834
835         if (procmime_get_part(filename, source, partinfo) < 0)
836                 alertpanel_error
837                         (_("Can't get the part of multipart message."));
838
839         return filename;
840 }
841
842 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
843 {
844         FILE *fp;
845         gchar *file;
846         MimeInfo *mimeinfo;
847         MsgInfo *tmpmsginfo;
848         gchar *p;
849         gchar *boundary;
850         gint boundary_len = 0;
851         gchar buf[BUFFSIZE];
852         glong fpos, prev_fpos;
853         gint npart;
854         gchar *source;
855         gchar *filename;
856
857         g_return_if_fail(msginfo != NULL);
858         
859 #if USE_GPGME
860         for (;;) {
861                 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
862                 mimeinfo = procmime_scan_mime_header(fp);
863                 if (!mimeinfo) break;
864
865                 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
866                     rfc2015_is_encrypted(mimeinfo)) {
867                         MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
868                 }
869                 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
870                     !msginfo->plaintext_file  &&
871                     !msginfo->decryption_failed) {
872                         rfc2015_decrypt_message(msginfo, mimeinfo, fp);
873                         if (msginfo->plaintext_file &&
874                             !msginfo->decryption_failed) {
875                                 fclose(fp);
876                                 continue;
877                         }
878                 }
879                 
880                 break;
881         }
882 #else /* !USE_GPGME */
883         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
884         mimeinfo = procmime_scan_mime_header(fp);
885 #endif /* USE_GPGME */
886
887         fclose(fp);
888         if (!mimeinfo) return;
889         if (mimeinfo->mime_type == MIME_TEXT)
890                 return;
891
892         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
893
894         g_return_if_fail(mimeinfo != NULL);
895         g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
896
897         if (mimeinfo->mime_type == MIME_MULTIPART) {
898                 g_return_if_fail(mimeinfo->boundary != NULL);
899                 g_return_if_fail(mimeinfo->sub == NULL);
900         }
901         g_return_if_fail(fp != NULL);
902
903         boundary = mimeinfo->boundary;
904
905         if (boundary) {
906                 boundary_len = strlen(boundary);
907
908                 /* look for first boundary */
909                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
910                         if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
911                 if (!p) {
912                         fclose(fp);
913                         return;
914                 }
915         }
916
917         if ((fpos = ftell(fp)) < 0) {
918                 perror("ftell");
919                 fclose(fp);
920                 return;
921         }
922
923         for (npart = 0;; npart++) {
924                 MimeInfo *partinfo;
925                 gboolean eom = FALSE;
926
927                 prev_fpos = fpos;
928
929                 partinfo = procmime_scan_mime_header(fp);
930                 if (!partinfo) break;
931
932                 if (npart != 0)
933                         procmime_mimeinfo_insert(mimeinfo, partinfo);
934                 else
935                         procmime_mimeinfo_free(partinfo);
936
937                 /* look for next boundary */
938                 buf[0] = '\0';
939                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
940                         if (IS_BOUNDARY(buf, boundary, boundary_len)) {
941                                 if (buf[2 + boundary_len]     == '-' &&
942                                     buf[2 + boundary_len + 1] == '-')
943                                         eom = TRUE;
944                                 break;
945                         }
946                 }
947                 if (p == NULL)
948                         eom = TRUE;     /* broken MIME message */
949                 fpos = ftell(fp);
950
951                 partinfo->size = fpos - prev_fpos - strlen(buf);
952
953                 if (eom) break;
954         }
955
956         source = procmsg_get_message_file_path(msginfo);
957
958         g_return_if_fail(mimeinfo != NULL);
959
960         if (!mimeinfo->main && mimeinfo->parent)
961                 {
962                         filename = mime_extract_file(source, mimeinfo);
963
964                         compose_attach_append_with_type(compose, filename,
965                                                         mimeinfo->content_type,
966                                                         mimeinfo->mime_type);
967
968                         g_free(filename);
969                 }
970
971         if (mimeinfo->sub && mimeinfo->sub->children)
972                 {
973                         filename = mime_extract_file(source, mimeinfo->sub);
974
975                         compose_attach_append_with_type(compose, filename,
976                                                         mimeinfo->content_type,
977                                                         mimeinfo->sub->mime_type);
978
979                         g_free(filename);
980                 }
981
982         if (mimeinfo->children) {
983                 MimeInfo *child;
984
985                 child = mimeinfo->children;
986                 while (child) {
987                         filename = mime_extract_file(source, child);
988
989                         compose_attach_append_with_type(compose, filename,
990                                                         child->content_type,
991                                                         child->mime_type);
992
993                         g_free(filename);
994
995                         child = child->next;
996                 }
997         }
998
999         fclose(fp);
1000
1001         procmime_mimeinfo_free_all(mimeinfo);
1002 }
1003
1004
1005 #define INSERT_FW_HEADER(var, hdr) \
1006 if (msginfo->var && *msginfo->var) { \
1007         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
1008         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
1009         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
1010 }
1011
1012 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
1013                          gboolean as_attach)
1014 {
1015         Compose *compose;
1016         /*      PrefsAccount *account; */
1017         GtkSText *text;
1018
1019         g_return_val_if_fail(msginfo != NULL, NULL);
1020         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1021
1022         account = msginfo->folder->folder->account;
1023         if (!account && msginfo->to && prefs_common.forward_account_autosel) {
1024                 gchar *to;
1025                 Xstrdup_a(to, msginfo->to, return NULL);
1026                 extract_address(to);
1027                 account = account_find_from_address(to);
1028         }
1029
1030         if(!account && prefs_common.forward_account_autosel) {
1031                 gchar cc[BUFFSIZE];
1032                 if(!get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
1033                         extract_address(cc);
1034                         account = account_find_from_address(cc);
1035                 }
1036         }
1037
1038         if (account == NULL) {
1039                 account = cur_account;
1040                 /*
1041                 account = msginfo->folder->folder->account;
1042                 if (!account) account = cur_account;
1043                 */
1044         }
1045         g_return_val_if_fail(account != NULL, NULL);
1046
1047         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
1048         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
1049         if (MSG_IS_IMAP(msginfo->flags))
1050                 imap_msg_unset_perm_flags(msginfo, MSG_REPLIED);
1051         CHANGE_FLAGS(msginfo);
1052
1053         compose = compose_create(account, COMPOSE_FORWARD);
1054
1055         if (msginfo->subject && *msginfo->subject) {
1056                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
1057                 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
1058                                       msginfo->subject);
1059         }
1060
1061         text = GTK_STEXT(compose->text);
1062         gtk_stext_freeze(text);
1063
1064         if (as_attach) {
1065                 gchar *msgfile;
1066
1067                 msgfile = procmsg_get_message_file_path(msginfo);
1068                 if (!is_file_exist(msgfile))
1069                         g_warning(_("%s: file not exist\n"), msgfile);
1070                 else
1071                         compose_attach_append(compose, msgfile,
1072                                               MIME_MESSAGE_RFC822);
1073
1074                 g_free(msgfile);
1075         } else {
1076                 gchar *qmark;
1077                 gchar *quote_str;
1078
1079                 if (prefs_common.fw_quotemark && *prefs_common.fw_quotemark)
1080                         qmark = prefs_common.fw_quotemark;
1081                 else
1082                         qmark = "> ";
1083
1084                 quote_str = compose_quote_fmt(compose, msginfo,
1085                                               prefs_common.fw_quotefmt, qmark);
1086                 compose_attach_parts(compose, msginfo);
1087         }
1088
1089         if (prefs_common.auto_sig)
1090                 compose_insert_sig(compose);
1091
1092         if (prefs_common.linewrap_quote)
1093                 compose_wrap_line_all(compose);
1094
1095         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1096         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1097
1098         gtk_stext_thaw(text);
1099 #if 0 /* NEW COMPOSE GUI */
1100         if (account->protocol != A_NNTP)
1101                 gtk_widget_grab_focus(compose->to_entry);
1102         else
1103                 gtk_widget_grab_focus(compose->newsgroups_entry);
1104 #endif
1105         gtk_widget_grab_focus(compose->header_last->entry);
1106
1107         if (prefs_common.auto_exteditor)
1108                 compose_exec_ext_editor(compose);
1109
1110         return compose;
1111 }
1112
1113 #undef INSERT_FW_HEADER
1114
1115 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1116 {
1117         Compose *compose;
1118         GtkSText *text;
1119         GSList *msginfo;
1120         gchar *msgfile;
1121
1122         g_return_val_if_fail(msginfo_list != NULL, NULL);
1123         
1124         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1125                 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
1126                         return NULL;
1127         }
1128
1129         if (account == NULL) {
1130                 account = cur_account;
1131                 /*
1132                 account = msginfo->folder->folder->account;
1133                 if (!account) account = cur_account;
1134                 */
1135         }
1136         g_return_val_if_fail(account != NULL, NULL);
1137
1138         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1139                 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1140                 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1141                 CHANGE_FLAGS(((MsgInfo *)msginfo->data));
1142         }
1143
1144         compose = compose_create(account, COMPOSE_FORWARD);
1145
1146         text = GTK_STEXT(compose->text);
1147         gtk_stext_freeze(text);
1148
1149         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1150                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1151                 if (!is_file_exist(msgfile))
1152                         g_warning(_("%s: file not exist\n"), msgfile);
1153                 else
1154                         compose_attach_append(compose, msgfile,
1155                                 MIME_MESSAGE_RFC822);
1156                 g_free(msgfile);
1157         }
1158
1159         if (prefs_common.auto_sig)
1160                 compose_insert_sig(compose);
1161
1162         if (prefs_common.linewrap_quote)
1163                 compose_wrap_line_all(compose);
1164
1165         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1166         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1167
1168         gtk_stext_thaw(text);
1169 #if 0 /* NEW COMPOSE GUI */
1170         if (account->protocol != A_NNTP)
1171                 gtk_widget_grab_focus(compose->to_entry);
1172         else
1173                 gtk_widget_grab_focus(compose->newsgroups_entry);
1174 #endif
1175
1176         return compose;
1177 }
1178
1179 void compose_reedit(MsgInfo *msginfo)
1180 {
1181         Compose *compose;
1182         PrefsAccount *account;
1183         GtkSText *text;
1184         FILE *fp;
1185         gchar buf[BUFFSIZE];
1186
1187         g_return_if_fail(msginfo != NULL);
1188         g_return_if_fail(msginfo->folder != NULL);
1189
1190         if (msginfo->folder->stype == F_QUEUE) {
1191                 gchar account_address[BUFFSIZE];
1192                 if (!get_header_from_msginfo(msginfo, account_address, sizeof(account_address), "S:")) {
1193                         account = account_find_from_address(account_address);
1194                 }
1195         } else 
1196                 account = msginfo->folder->folder->account;
1197
1198         if (!account && prefs_common.reedit_account_autosel) {
1199                 gchar from[BUFFSIZE];
1200                 if (!get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
1201                         extract_address(from);
1202                         account = account_find_from_address(from);
1203                 }
1204         }
1205         if (!account) account = cur_account;
1206         g_return_if_fail(account != NULL);
1207
1208         compose = compose_create(account, COMPOSE_REEDIT);
1209         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1210
1211         if (compose_parse_header(compose, msginfo) < 0) return;
1212         compose_reedit_set_entry(compose, msginfo);
1213
1214         text = GTK_STEXT(compose->text);
1215         gtk_stext_freeze(text);
1216
1217         if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1218                 g_warning(_("Can't get text part\n"));
1219         else {
1220                 while (fgets(buf, sizeof(buf), fp) != NULL)
1221                         gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1222                 fclose(fp);
1223         }
1224         compose_attach_parts(compose, msginfo);
1225
1226         gtk_stext_thaw(text);
1227         gtk_widget_grab_focus(compose->text);
1228
1229         if (prefs_common.auto_exteditor)
1230                 compose_exec_ext_editor(compose);
1231 }
1232
1233 GList *compose_get_compose_list(void)
1234 {
1235         return compose_list;
1236 }
1237
1238 void compose_entry_append(Compose *compose, const gchar *address,
1239                           ComposeEntryType type)
1240 {
1241         GtkEntry *entry;
1242         const gchar *text;
1243         gchar *header;
1244
1245         if (!address || *address == '\0') return;
1246
1247 #if 0 /* NEW COMPOSE GUI */
1248         switch (type) {
1249         case COMPOSE_CC:
1250                 entry = GTK_ENTRY(compose->cc_entry);
1251                 break;
1252         case COMPOSE_BCC:
1253                 entry = GTK_ENTRY(compose->bcc_entry);
1254                 break;
1255         case COMPOSE_NEWSGROUPS:
1256                 entry = GTK_ENTRY(compose->newsgroups_entry);
1257                 break;
1258         case COMPOSE_TO:
1259         default:
1260                 entry = GTK_ENTRY(compose->to_entry);
1261                 break;
1262         }
1263
1264         text = gtk_entry_get_text(entry);
1265         if (*text != '\0')
1266                 gtk_entry_append_text(entry, ", ");
1267         gtk_entry_append_text(entry, address);
1268 #endif
1269
1270         switch (type) {
1271         case COMPOSE_CC:
1272                 header = N_("Cc:");
1273                 break;
1274         case COMPOSE_BCC:
1275                 header = N_("Bcc:");
1276                 break;
1277         case COMPOSE_REPLYTO:
1278                 header = N_("Reply-To:");
1279                 break;
1280         case COMPOSE_NEWSGROUPS:
1281                 header = N_("Newsgroups:");
1282                 break;
1283         case COMPOSE_FOLLOWUPTO:
1284                 header = N_( "Followup-To:");
1285                 break;
1286         case COMPOSE_TO:
1287         default:
1288                 header = N_("To:");
1289                 break;
1290         }
1291         header = prefs_common.trans_hdr ? gettext(header) : header;
1292
1293         compose_add_header_entry(compose, header, (gchar *)address);
1294 }
1295
1296 static void compose_entries_set(Compose *compose, const gchar *mailto)
1297 {
1298         gchar *subject = NULL;
1299         gchar *to = NULL;
1300         gchar *cc = NULL;
1301         gchar *bcc = NULL;
1302         gchar *body = NULL;
1303         gchar *p;
1304         gchar *tmp_mailto;
1305
1306         Xstrdup_a(tmp_mailto, mailto, return);
1307
1308         to = tmp_mailto;
1309
1310         p = strchr(tmp_mailto, '?');
1311         if (p) {
1312                 *p = '\0';
1313                 p++;
1314         }
1315
1316         while (p) {
1317                 gchar *field, *value;
1318
1319                 field = p;
1320
1321                 p = strchr(p, '=');
1322                 if (!p) break;
1323                 *p = '\0';
1324                 p++;
1325
1326                 value = p;
1327
1328                 p = strchr(p, '&');
1329                 if (p) {
1330                         *p = '\0';
1331                         p++;
1332                 }
1333
1334                 if (*value == '\0') continue;
1335
1336                 if (!g_strcasecmp(field, "subject")) {
1337                         Xalloca(subject, strlen(value) + 1, return);
1338                         decode_uri(subject, value);
1339                 } else if (!g_strcasecmp(field, "cc")) {
1340                         cc = value;
1341                 } else if (!g_strcasecmp(field, "bcc")) {
1342                         bcc = value;
1343                 } else if (!g_strcasecmp(field, "body")) {
1344                         Xalloca(body, strlen(value) + 1, return);
1345                         decode_uri(body, value);
1346                 }
1347         }
1348
1349         if (to)
1350                 compose_entry_append(compose, to, COMPOSE_TO);
1351         if (subject)
1352                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1353         if (cc)
1354                 compose_entry_append(compose, cc, COMPOSE_CC);
1355         if (bcc)
1356                 compose_entry_append(compose, bcc, COMPOSE_BCC);
1357         if (body) {
1358                 gtk_stext_insert(GTK_STEXT(compose->text),
1359                                 NULL, NULL, NULL, body, -1);
1360                 gtk_stext_insert(GTK_STEXT(compose->text),
1361                                 NULL, NULL, NULL, "\n", 1);
1362         }
1363 }
1364
1365 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1366 {
1367         static HeaderEntry hentry[] = {{"Reply-To:",       NULL, TRUE},
1368                                        {"Cc:",             NULL, FALSE},
1369                                        {"References:",     NULL, FALSE},
1370                                        {"Bcc:",            NULL, FALSE},
1371                                        {"Newsgroups:",     NULL, FALSE},
1372                                        {"Followup-To:",    NULL, FALSE},
1373                                        {"X-Mailing-List:", NULL, FALSE},
1374                                        {"X-BeenThere:",    NULL, FALSE},
1375                                        {NULL,              NULL, FALSE}};
1376
1377         enum
1378         {
1379                 H_REPLY_TO       = 0,
1380                 H_CC             = 1,
1381                 H_REFERENCES     = 2,
1382                 H_BCC            = 3,
1383                 H_NEWSGROUPS     = 4,
1384                 H_FOLLOWUP_TO    = 5,
1385                 H_X_MAILING_LIST = 6,
1386                 H_X_BEENTHERE    = 7
1387         };
1388
1389         FILE *fp;
1390
1391         g_return_val_if_fail(msginfo != NULL, -1);
1392
1393         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1394         procheader_get_header_fields(fp, hentry);
1395         fclose(fp);
1396
1397         if (hentry[H_REPLY_TO].body != NULL) {
1398                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1399                 compose->replyto = hentry[H_REPLY_TO].body;
1400                 hentry[H_REPLY_TO].body = NULL;
1401         }
1402         if (hentry[H_CC].body != NULL) {
1403                 conv_unmime_header_overwrite(hentry[H_CC].body);
1404                 compose->cc = hentry[H_CC].body;
1405                 hentry[H_CC].body = NULL;
1406         }
1407         if (hentry[H_X_MAILING_LIST].body != NULL) {
1408                 /* this is good enough to parse debian-devel */
1409                 gchar *buf = g_malloc(strlen(hentry[H_X_MAILING_LIST].body) + 1);
1410                 g_return_val_if_fail(buf != NULL, -1 );
1411                 if (1 == sscanf(hentry[H_X_MAILING_LIST].body, "<%[^>]>", buf))
1412                         compose->mailinglist = g_strdup(buf);
1413                 g_free(buf);
1414                 g_free(hentry[H_X_MAILING_LIST].body);
1415                 hentry[H_X_MAILING_LIST].body = NULL ;
1416         }
1417         if (hentry[H_X_BEENTHERE].body != NULL) {
1418                 /* this is good enough to parse the sylpheed-claws lists */
1419                 gchar *buf = g_malloc(strlen(hentry[H_X_BEENTHERE].body) + 1);
1420                 g_return_val_if_fail(buf != NULL, -1 );
1421                 if (1 == sscanf(hentry[H_X_BEENTHERE].body, "%[^>]", buf))
1422                         compose->mailinglist = g_strdup(buf);
1423                 g_free(buf);
1424                 g_free(hentry[H_X_BEENTHERE].body);
1425                 hentry[H_X_BEENTHERE].body = NULL ;
1426         }
1427         if (hentry[H_REFERENCES].body != NULL) {
1428                 if (compose->mode == COMPOSE_REEDIT)
1429                         compose->references = hentry[H_REFERENCES].body;
1430                 else {
1431                         compose->references = compose_parse_references
1432                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1433                         g_free(hentry[H_REFERENCES].body);
1434                 }
1435                 hentry[H_REFERENCES].body = NULL;
1436         }
1437         if (hentry[H_BCC].body != NULL) {
1438                 if (compose->mode == COMPOSE_REEDIT) {
1439                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1440                         compose->bcc = hentry[H_BCC].body;
1441                 } else
1442                         g_free(hentry[H_BCC].body);
1443                 hentry[H_BCC].body = NULL;
1444         }
1445         if (hentry[H_NEWSGROUPS].body != NULL) {
1446                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1447                 hentry[H_NEWSGROUPS].body = NULL;
1448         }
1449         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1450                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1451                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1452                 hentry[H_FOLLOWUP_TO].body = NULL;
1453         }
1454
1455         if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1456                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1457         else if (compose->mode != COMPOSE_REEDIT &&
1458                  msginfo->msgid && *msginfo->msgid) {
1459                 compose->inreplyto = g_strdup(msginfo->msgid);
1460
1461                 if (!compose->references) {
1462                         if (msginfo->inreplyto && *msginfo->inreplyto)
1463                                 compose->references =
1464                                         g_strdup_printf("<%s>\n\t<%s>",
1465                                                         msginfo->inreplyto,
1466                                                         msginfo->msgid);
1467                         else
1468                                 compose->references =
1469                                         g_strconcat("<", msginfo->msgid, ">",
1470                                                     NULL);
1471                 }
1472         }
1473
1474         return 0;
1475 }
1476
1477 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1478 {
1479         GSList *ref_id_list, *cur;
1480         GString *new_ref;
1481         gchar *new_ref_str;
1482
1483         ref_id_list = references_list_append(NULL, ref);
1484         if (!ref_id_list) return NULL;
1485         if (msgid && *msgid)
1486                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1487
1488         for (;;) {
1489                 gint len = 0;
1490
1491                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1492                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1493                         len += strlen((gchar *)cur->data) + 5;
1494
1495                 if (len > MAX_REFERENCES_LEN) {
1496                         /* remove second message-ID */
1497                         if (ref_id_list && ref_id_list->next &&
1498                             ref_id_list->next->next) {
1499                                 g_free(ref_id_list->next->data);
1500                                 ref_id_list = g_slist_remove
1501                                         (ref_id_list, ref_id_list->next->data);
1502                         } else {
1503                                 slist_free_strings(ref_id_list);
1504                                 g_slist_free(ref_id_list);
1505                                 return NULL;
1506                         }
1507                 } else
1508                         break;
1509         }
1510
1511         new_ref = g_string_new("");
1512         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1513                 if (new_ref->len > 0)
1514                         g_string_append(new_ref, "\n\t");
1515                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1516         }
1517
1518         slist_free_strings(ref_id_list);
1519         g_slist_free(ref_id_list);
1520
1521         new_ref_str = new_ref->str;
1522         g_string_free(new_ref, FALSE);
1523
1524         return new_ref_str;
1525 }
1526
1527 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1528                                 const gchar *fmt, const gchar *qmark)
1529 {
1530         GtkSText *text = GTK_STEXT(compose->text);
1531         gchar *quote_str = NULL;
1532         gchar *buf;
1533         gchar *p, *lastp;
1534         gint len;
1535
1536         if (qmark != NULL) {
1537                 quote_fmt_init(msginfo, NULL);
1538                 quote_fmt_scan_string(qmark);
1539                 quote_fmt_parse();
1540
1541                 buf = quote_fmt_get_buffer();
1542                 if (buf == NULL)
1543                         alertpanel_error(_("Quote mark format error."));
1544                 else
1545                         Xstrdup_a(quote_str, buf, return NULL)
1546         }
1547
1548         if (fmt && *fmt != '\0') {
1549                 quote_fmt_init(msginfo, quote_str);
1550                 quote_fmt_scan_string(fmt);
1551                 quote_fmt_parse();
1552
1553                 buf = quote_fmt_get_buffer();
1554                 if (buf == NULL) {
1555                         alertpanel_error(_("Message reply/forward format error."));
1556                         return NULL;
1557                 }
1558         } else
1559                 buf = "";
1560
1561         gtk_stext_freeze(text);
1562         gtk_stext_set_point(text, 0);
1563         gtk_stext_forward_delete(text, gtk_stext_get_length(text));
1564
1565         for (p = buf; *p != '\0'; ) {
1566                 lastp = strchr(p, '\n');
1567                 len = lastp ? lastp - p + 1 : -1;
1568                 gtk_stext_insert(text, NULL, NULL, NULL, p, len);
1569                 if (lastp)
1570                         p = lastp + 1;
1571                 else
1572                         break;
1573         }
1574
1575         gtk_stext_thaw(text);
1576
1577         return buf;
1578 }
1579
1580 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1581                                     gboolean to_all, gboolean ignore_replyto,
1582                                     gboolean followup_and_reply_to)
1583 {
1584         GSList *cc_list;
1585         GSList *cur;
1586         gchar *from;
1587         GHashTable *to_table;
1588
1589         g_return_if_fail(compose->account != NULL);
1590         g_return_if_fail(msginfo != NULL);
1591
1592         if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1593                 compose_entry_append(compose,
1594                                     ((compose->replyto && !ignore_replyto)
1595                                      ? compose->replyto
1596                                      : (compose->mailinglist && !ignore_replyto)
1597                                      ? compose->mailinglist
1598                                      : msginfo->from ? msginfo->from : ""),
1599                                      COMPOSE_TO);
1600         if (compose->account->protocol == A_NNTP) {
1601                 if (ignore_replyto)
1602                         compose_entry_append
1603                                 (compose, msginfo->from ? msginfo->from : "",
1604                                  COMPOSE_TO);
1605                 else
1606                         compose_entry_append
1607                                 (compose,
1608                                  compose->followup_to ? compose->followup_to
1609                                  : compose->newsgroups ? compose->newsgroups
1610                                  : "",
1611                                  COMPOSE_NEWSGROUPS);
1612         }
1613
1614         if (msginfo->subject && *msginfo->subject) {
1615                 gchar *buf, *buf2, *p;
1616
1617                 buf = g_strdup(msginfo->subject);
1618                 while (!strncasecmp(buf, "Re:", 3)) {
1619                         p = buf + 3;
1620                         while (isspace(*p)) p++;
1621                         memmove(buf, p, strlen(p) + 1);
1622                 }
1623
1624                 buf2 = g_strdup_printf("Re: %s", buf);
1625                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1626                 g_free(buf2);
1627                 g_free(buf);
1628         } else
1629                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1630
1631         if (!to_all || compose->account->protocol == A_NNTP) return;
1632
1633         from = g_strdup(compose->replyto ? compose->replyto :
1634                         msginfo->from ? msginfo->from : "");
1635         extract_address(from);
1636
1637         cc_list = address_list_append(NULL, msginfo->to);
1638         cc_list = address_list_append(cc_list, compose->cc);
1639
1640         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1641         g_hash_table_insert(to_table, from, GINT_TO_POINTER(1));
1642         if (compose->account)
1643                 g_hash_table_insert(to_table, compose->account->address,
1644                                     GINT_TO_POINTER(1));
1645
1646         /* remove address on To: and that of current account */
1647         for (cur = cc_list; cur != NULL; ) {
1648                 GSList *next = cur->next;
1649
1650                 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1651                         cc_list = g_slist_remove(cc_list, cur->data);
1652                 else
1653                         g_hash_table_insert(to_table, cur->data, cur);
1654
1655                 cur = next;
1656         }
1657         g_hash_table_destroy(to_table);
1658         g_free(from);
1659
1660         if (cc_list) {
1661                 for (cur = cc_list; cur != NULL; cur = cur->next)
1662                         compose_entry_append(compose, (gchar *)cur->data,
1663                                              COMPOSE_CC);
1664                 slist_free_strings(cc_list);
1665                 g_slist_free(cc_list);
1666         }
1667 }
1668
1669 #define SET_ENTRY(entry, str) \
1670 { \
1671         if (str && *str) \
1672                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1673 }
1674
1675 #define SET_ADDRESS(type, str) \
1676 { \
1677         if (str && *str) \
1678                 compose_entry_append(compose, str, type); \
1679 }
1680
1681 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1682 {
1683         g_return_if_fail(msginfo != NULL);
1684
1685         SET_ENTRY(subject_entry, msginfo->subject);
1686         SET_ADDRESS(COMPOSE_TO, msginfo->to);
1687         SET_ADDRESS(COMPOSE_CC, compose->cc);
1688         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1689         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1690
1691 #if 0 /* NEW COMPOSE GUI */
1692         if (compose->bcc) {
1693                 GtkItemFactory *ifactory;
1694                 GtkWidget *menuitem;
1695
1696                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1697                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
1698                 gtk_check_menu_item_set_active
1699                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1700         }
1701         if (compose->replyto) {
1702                 GtkItemFactory *ifactory;
1703                 GtkWidget *menuitem;
1704
1705                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1706                 menuitem = gtk_item_factory_get_item
1707                         (ifactory, "/Message/Reply to");
1708                 gtk_check_menu_item_set_active
1709                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1710         }
1711 #endif
1712 }
1713
1714 #undef SET_ENTRY
1715 #undef SET_ADDRESS
1716
1717 static void compose_exec_sig(Compose *compose, gchar *sigfile)
1718 {
1719         FILE *tmpfp;
1720         pid_t thepid;
1721         gchar *sigtext;
1722         FILE  *sigprg;
1723         gchar  *buf;
1724         size_t buf_len = 128;
1725  
1726         if (strlen(sigfile) < 2)
1727           return;
1728  
1729         sigprg = popen(sigfile+1, "r");
1730         if (sigprg) {
1731
1732                 buf = g_malloc(buf_len);
1733
1734                 if (!buf) {
1735                         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1736                         "Unable to insert signature (malloc failed)\n", -1);
1737
1738                         pclose(sigprg);
1739                         return;
1740                 }
1741
1742                 while (!feof(sigprg)) {
1743                         bzero(buf, buf_len);
1744                         fread(buf, buf_len-1, 1, sigprg);
1745                         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
1746                 }
1747
1748                 g_free(buf);
1749                 pclose(sigprg);
1750         }
1751         else
1752         {
1753                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1754                 "Can't exec file: ", -1);
1755                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1756                 sigfile+1, -1);
1757         }
1758 }
1759
1760 static void compose_insert_sig(Compose *compose)
1761 {
1762         gchar *sigfile;
1763
1764         if (compose->account && compose->account->sig_path)
1765                 sigfile = g_strdup(compose->account->sig_path);
1766         else {
1767                 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1768                                       DEFAULT_SIGNATURE, NULL);
1769         }
1770
1771         if (!is_file_or_fifo_exist(sigfile) & (sigfile[0] != '|')) {
1772                 g_free(sigfile);
1773                 return;
1774         }
1775
1776         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1777         if (prefs_common.sig_sep) {
1778                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1779                                 prefs_common.sig_sep, -1);
1780                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1781                                 "\n", 1);
1782         }
1783
1784         if (sigfile[0] == '|')
1785         {
1786                 compose_exec_sig(compose, sigfile);
1787         }
1788         else
1789         {
1790                 compose_insert_file(compose, sigfile);
1791         }
1792         g_free(sigfile);
1793 }
1794
1795 static void compose_insert_file(Compose *compose, const gchar *file)
1796 {
1797         GtkSText *text = GTK_STEXT(compose->text);
1798         gchar buf[BUFFSIZE];
1799         gint len;
1800         FILE *fp;
1801
1802         g_return_if_fail(file != NULL);
1803
1804         if ((fp = fopen(file, "r")) == NULL) {
1805                 FILE_OP_ERROR(file, "fopen");
1806                 return;
1807         }
1808
1809         gtk_stext_freeze(text);
1810
1811         while (fgets(buf, sizeof(buf), fp) != NULL) {
1812                 /* Strip <CR> if DOS/Windoze file, replace <CR> with <LF> if MAC file */
1813                 len = strlen(buf);
1814                 if (len > 1 && buf[len - 2] == '\r' && buf[len - 1] == '\n') {
1815                         buf[len - 2] = '\n';
1816                         buf[len - 1] = '\0';
1817                 } else {
1818                         while (--len > 0)
1819                                 if (buf[len] == '\r')
1820                                         buf[len] = '\n';
1821                 }
1822
1823                 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1824         }
1825
1826         gtk_stext_thaw(text);
1827
1828         fclose(fp);
1829 }
1830
1831 static void compose_attach_info(Compose * compose, AttachInfo * ainfo,
1832                                 ContentType cnttype)
1833 {
1834         gchar *text[N_ATTACH_COLS];
1835         gint row;
1836
1837         text[COL_MIMETYPE] = ainfo->content_type;
1838         text[COL_SIZE] = to_human_readable(ainfo->size);
1839         text[COL_NAME] = ainfo->name;
1840
1841         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1842         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1843
1844         if (cnttype != MIME_MESSAGE_RFC822)
1845                 compose_changed_cb(NULL, compose);
1846 }
1847
1848 static void compose_attach_append_with_type(Compose *compose,
1849                                             const gchar *file,
1850                                             const gchar *type,
1851                                             ContentType cnttype)
1852 {
1853         AttachInfo *ainfo;
1854         off_t size;
1855
1856         if (!is_file_exist(file)) {
1857                 g_warning(_("File %s doesn't exist\n"), file);
1858                 return;
1859         }
1860         if ((size = get_file_size(file)) < 0) {
1861                 g_warning(_("Can't get file size of %s\n"), file);
1862                 return;
1863         }
1864         if (size == 0) {
1865                 alertpanel_notice(_("File %s is empty\n"), file);
1866                 return;
1867         }
1868 #if 0 /* NEW COMPOSE GUI */
1869         if (!compose->use_attach) {
1870                 GtkItemFactory *ifactory;
1871                 GtkWidget *menuitem;
1872
1873                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1874                 menuitem = gtk_item_factory_get_item(ifactory,
1875                                                      "/Message/Attach");
1876                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1877                                                TRUE);
1878         }
1879 #endif
1880         ainfo = g_new0(AttachInfo, 1);
1881         ainfo->file = g_strdup(file);
1882
1883         if (cnttype == MIME_MESSAGE_RFC822) {
1884                 ainfo->encoding = ENC_7BIT;
1885                 ainfo->name = g_strdup_printf(_("Message: %s"),
1886                                               g_basename(file));
1887         } else {
1888                 ainfo->encoding = ENC_BASE64;
1889                 ainfo->name = g_strdup(g_basename(file));
1890         }
1891
1892         ainfo->content_type = g_strdup(type);
1893         ainfo->size = size;
1894
1895         compose_attach_info(compose, ainfo, cnttype);
1896 }
1897
1898 static void compose_attach_append(Compose *compose, const gchar *file,
1899                                   ContentType cnttype)
1900 {
1901         AttachInfo *ainfo;
1902         gchar *text[N_ATTACH_COLS];
1903         off_t size;
1904         gint row;
1905
1906         if (!is_file_exist(file)) {
1907                 g_warning(_("File %s doesn't exist\n"), file);
1908                 return;
1909         }
1910         if ((size = get_file_size(file)) < 0) {
1911                 g_warning(_("Can't get file size of %s\n"), file);
1912                 return;
1913         }
1914         if (size == 0) {
1915                 alertpanel_notice(_("File %s is empty\n"), file);
1916                 return;
1917         }
1918 #if 0 /* NEW COMPOSE GUI */
1919         if (!compose->use_attach) {
1920                 GtkItemFactory *ifactory;
1921                 GtkWidget *menuitem;
1922
1923                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1924                 menuitem = gtk_item_factory_get_item(ifactory,
1925                                                      "/Message/Attach");
1926                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1927                                                TRUE);
1928         }
1929 #endif
1930         ainfo = g_new0(AttachInfo, 1);
1931         ainfo->file = g_strdup(file);
1932
1933         if (cnttype == MIME_MESSAGE_RFC822) {
1934                 ainfo->content_type = g_strdup("message/rfc822");
1935                 ainfo->encoding = ENC_7BIT;
1936                 ainfo->name = g_strdup_printf(_("Message: %s"),
1937                                               g_basename(file));
1938         } else {
1939                 ainfo->content_type = procmime_get_mime_type(file);
1940                 if (!ainfo->content_type)
1941                         ainfo->content_type =
1942                                 g_strdup("application/octet-stream");
1943                 ainfo->encoding = ENC_BASE64;
1944                 ainfo->name = g_strdup(g_basename(file));
1945         }
1946         ainfo->size = size;
1947
1948         compose_attach_info(compose, ainfo, cnttype);
1949 }
1950
1951 #define GET_CHAR(pos, buf, len)                                              \
1952 {                                                                            \
1953         if (text->use_wchar)                                                 \
1954                 len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos)));     \
1955         else {                                                               \
1956                 buf[0] = GTK_STEXT_INDEX(text, (pos));                       \
1957                 len = 1;                                                     \
1958         }                                                                    \
1959 }
1960
1961 static void compose_wrap_line(Compose *compose)
1962 {
1963         GtkSText *text = GTK_STEXT(compose->text);
1964         gint ch_len, last_ch_len;
1965         gchar cbuf[MB_LEN_MAX], last_ch;
1966         guint text_len;
1967         guint line_end;
1968         guint quoted;
1969         gint p_start, p_end;
1970         gint line_pos, cur_pos;
1971         gint line_len, cur_len;
1972
1973
1974         gtk_stext_freeze(text);
1975
1976         text_len = gtk_stext_get_length(text);
1977
1978         /* check to see if the point is on the paragraph mark (empty line). */
1979         cur_pos = gtk_stext_get_point(text);
1980         GET_CHAR(cur_pos, cbuf, ch_len);
1981         if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
1982                 if (cur_pos == 0)
1983                         goto compose_end; /* on the paragraph mark */
1984                 GET_CHAR(cur_pos - 1, cbuf, ch_len);
1985                 if (ch_len == 1 && *cbuf == '\n')
1986                         goto compose_end; /* on the paragraph mark */
1987         }
1988
1989         /* find paragraph start. */
1990         line_end = quoted = 0;
1991         for (p_start = cur_pos; p_start >= 0; --p_start) {
1992                 GET_CHAR(p_start, cbuf, ch_len);
1993                 if (ch_len == 1 && *cbuf == '\n') {
1994                         if (quoted)
1995                                 goto compose_end; /* quoted part */
1996                         if (line_end) {
1997                                 p_start += 2;
1998                                 break;
1999                         }
2000                         line_end = 1;
2001                 } else {
2002                         if (ch_len == 1 && strchr(">:#", *cbuf))
2003                                 quoted = 1;
2004                         else if (ch_len != 1 || !isspace(*cbuf))
2005                                 quoted = 0;
2006
2007                         line_end = 0;
2008                 }
2009         }
2010         if (p_start < 0)
2011                 p_start = 0;
2012
2013         /* find paragraph end. */
2014         line_end = 0;
2015         for (p_end = cur_pos; p_end < text_len; p_end++) {
2016                 GET_CHAR(p_end, cbuf, ch_len);
2017                 if (ch_len == 1 && *cbuf == '\n') {
2018                         if (line_end) {
2019                                 p_end -= 1;
2020                                 break;
2021                         }
2022                         line_end = 1;
2023                 } else {
2024                         if (line_end && ch_len == 1 && strchr(">:#", *cbuf))
2025                                 goto compose_end; /* quoted part */
2026
2027                         line_end = 0;
2028                 }
2029         }
2030         if (p_end >= text_len)
2031                 p_end = text_len;
2032
2033         if (p_start >= p_end)
2034                 goto compose_end;
2035
2036         line_len = cur_len = 0;
2037         last_ch_len = 0;
2038         last_ch = '\0';
2039         line_pos = p_start;
2040         for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2041                 guint space = 0;
2042
2043                 GET_CHAR(cur_pos, cbuf, ch_len);
2044
2045                 if (ch_len < 0) {
2046                         cbuf[0] = '\0';
2047                         ch_len = 1;
2048                 }
2049
2050                 if (ch_len == 1 && isspace(*cbuf))
2051                         space = 1;
2052
2053                 if (ch_len == 1 && *cbuf == '\n') {
2054                         guint replace = 0;
2055                         if (last_ch_len == 1 && !isspace(last_ch)) {
2056                                 if (cur_pos + 1 < p_end) {
2057                                         GET_CHAR(cur_pos + 1, cbuf, ch_len);
2058                                         if (ch_len == 1 && !isspace(*cbuf))
2059                                                 replace = 1;
2060                                 }
2061                         }
2062                         gtk_stext_set_point(text, cur_pos + 1);
2063                         gtk_stext_backward_delete(text, 1);
2064                         if (replace) {
2065                                 gtk_stext_set_point(text, cur_pos);
2066                                 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2067                                 space = 1;
2068                         }
2069                         else {
2070                                 p_end--;
2071                                 cur_pos--;
2072                                 continue;
2073                         }
2074                 }
2075
2076                 last_ch_len = ch_len;
2077                 last_ch = *cbuf;
2078
2079                 if (space) {
2080                         line_pos = cur_pos + 1;
2081                         line_len = cur_len + ch_len;
2082                 }
2083
2084                 if (cur_len + ch_len > prefs_common.linewrap_len &&
2085                     line_len > 0) {
2086                         gint tlen = ch_len;
2087
2088                         GET_CHAR(line_pos - 1, cbuf, ch_len);
2089                         if (ch_len == 1 && isspace(*cbuf)) {
2090                                 gtk_stext_set_point(text, line_pos);
2091                                 gtk_stext_backward_delete(text, 1);
2092                                 p_end--;
2093                                 cur_pos--;
2094                                 line_pos--;
2095                                 cur_len--;
2096                                 line_len--;
2097                         }
2098                         ch_len = tlen;
2099
2100                         gtk_stext_set_point(text, line_pos);
2101                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2102                         p_end++;
2103                         cur_pos++;
2104                         line_pos++;
2105                         cur_len = cur_len - line_len + ch_len;
2106                         line_len = 0;
2107                         continue;
2108                 }
2109
2110                 if (ch_len > 1) {
2111                         line_pos = cur_pos + 1;
2112                         line_len = cur_len + ch_len;
2113                 }
2114                 cur_len += ch_len;
2115         }
2116
2117 compose_end:
2118         gtk_stext_thaw(text);
2119 }
2120
2121 /* return indent length */
2122 static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
2123 {
2124         gint indent_len = 0;
2125         gint i, ch_len;
2126         gchar cbuf[MB_LEN_MAX];
2127
2128         for (i = start_pos; i < text_len; i++) {
2129                 GET_CHAR(i, cbuf, ch_len);
2130                 if (ch_len > 1)
2131                         break;
2132                 /* allow space, tab, > or | */
2133                 if (cbuf[0] != ' ' && cbuf[0] != '\t' &&
2134                     cbuf[0] != '>' && cbuf[0] != '|')
2135                         break;
2136                 indent_len++;
2137         }
2138
2139         return indent_len;
2140 }
2141
2142 /* insert quotation string when line was wrapped */
2143 static guint ins_quote(GtkSText *text, guint quote_len, guint indent_len,
2144                        guint prev_line_pos, guint text_len,
2145                        gchar *quote_fmt)
2146 {
2147         guint i, ins_len;
2148         gchar ch;
2149
2150         if (indent_len) {
2151                 for (i = 0; i < indent_len; i++) {
2152                         ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2153                         gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2154                 }
2155                 ins_len = indent_len;
2156         } else {
2157                 gtk_stext_insert(text, NULL, NULL, NULL, quote_fmt, quote_len);
2158                 ins_len = quote_len;
2159         }
2160
2161         return ins_len;
2162 }
2163
2164 #undef WRAP_DEBUG
2165 #ifdef WRAP_DEBUG
2166 /* Darko: used when I debug wrapping */
2167 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2168 {
2169         gint i;
2170         gchar ch;
2171
2172         printf("%d [", pos);
2173         for (i = pos; i < tlen; i++) {
2174                 ch = GTK_STEXT_INDEX(text, i);
2175                 if (breakoncr && ch == '\n')
2176                         break;
2177                 printf("%c", ch);
2178         }
2179         printf("]\n");
2180 }
2181 #endif
2182
2183 static void compose_wrap_line_all(Compose *compose)
2184 {
2185         GtkSText *text = GTK_STEXT(compose->text);
2186         guint tlen;
2187         guint line_pos = 0, cur_pos = 0, p_pos = 0;
2188         gint line_len = 0, cur_len = 0;
2189         gint ch_len;
2190         gboolean is_new_line = TRUE, do_delete = FALSE;
2191         guint qlen = 0, i_len = 0;
2192         guint linewrap_quote = prefs_common.linewrap_quote;
2193         guint linewrap_len = prefs_common.linewrap_len;
2194         gchar *qfmt = prefs_common.quotemark;
2195         gchar cbuf[MB_LEN_MAX];
2196
2197         gtk_stext_freeze(text);
2198
2199         /* make text buffer contiguous */
2200         /* gtk_stext_compact_buffer(text); */
2201
2202         tlen = gtk_stext_get_length(text);
2203
2204         for (; cur_pos < tlen; cur_pos++) {
2205                 /* mark position of new line - needed for quotation wrap */
2206                 if (is_new_line) {
2207                         if (linewrap_quote) {
2208                                 qlen = gtkut_text_str_compare
2209                                         (text, cur_pos, tlen, qfmt);
2210                                 if (qlen)
2211                                         i_len = get_indent_length
2212                                                 (text, cur_pos, tlen);
2213                                 else
2214                                         i_len = 0;
2215                         }
2216                         is_new_line = FALSE;
2217                         p_pos = cur_pos;
2218 #ifdef WRAP_DEBUG
2219                         printf("new line i_len=%d qlen=%d p_pos=", i_len, qlen);
2220                         dump_text(text, p_pos, tlen, 1);
2221 #endif
2222                 }
2223
2224                 GET_CHAR(cur_pos, cbuf, ch_len);
2225
2226                 /* fix line length for tabs */
2227                 if (ch_len == 1 && *cbuf == '\t') {
2228                         guint tab_width = text->default_tab_width;
2229                         guint tab_offset = line_len % tab_width;
2230
2231 #ifdef WRAP_DEBUG
2232                         printf("found tab at pos=%d line_len=%d ", cur_pos,
2233                                 line_len);
2234 #endif
2235                         if (tab_offset) {
2236                                 line_len += tab_width - tab_offset - 1;
2237                                 cur_len = line_len;
2238                         }
2239 #ifdef WRAP_DEBUG
2240                         printf("new_len=%d\n", line_len);
2241 #endif
2242                 }
2243
2244                 /* we have encountered line break */
2245                 if (ch_len == 1 && *cbuf == '\n') {
2246                         gint clen;
2247                         guint ilen;
2248                         gchar cb[MB_CUR_MAX];
2249
2250 #ifdef WRAP_DEBUG
2251                         printf("found CR at %d next line is ", cur_pos);
2252                         dump_text(text, cur_pos + 1, tlen, 1);
2253 #endif
2254                         /* if it's just quotation + newline skip it */
2255                         if (i_len && (cur_pos + 1 < tlen)) {
2256                                 /* check if text at new line matches indent */
2257                                 ilen =  gtkut_text_str_compare_n
2258                                         (text, cur_pos + 1, p_pos, i_len, tlen);
2259                                 if (cur_pos + ilen < tlen) {
2260                                         GET_CHAR(cur_pos + ilen + 1, cb, clen);
2261                                         /* no need to join the lines */
2262                                         if (clen == 1 && cb[0] == '\n')
2263                                                 do_delete = FALSE;
2264                                 }
2265                         /* if it's just newline skip it */
2266                         } else if (do_delete && (cur_pos + 1 < tlen)) {
2267                                 GET_CHAR(cur_pos + 1, cb, clen);
2268                                 /* no need to join the lines */
2269                                 if (clen == 1 && cb[0] == '\n')
2270                                         do_delete = FALSE;
2271                         }
2272
2273 #ifdef WRAP_DEBUG
2274                         printf("qlen=%d l_len=%d wrap_len=%d do_del=%d\n",
2275                                 qlen, line_len, linewrap_len, do_delete);
2276 #endif
2277                         /* should we delete to perform smart wrapping */
2278                         if (line_len < linewrap_len && do_delete) {
2279                                 /* get rid of newline */
2280                                 gtk_stext_set_point(text, cur_pos);
2281                                 gtk_stext_forward_delete(text, 1);
2282                                 tlen--;
2283
2284                                 /* if text starts with quote fmt or with
2285                                    indent string, delete them */
2286                                 if (i_len) {
2287                                         ilen =  gtkut_text_str_compare_n
2288                                                 (text, cur_pos, p_pos, i_len,
2289                                                  tlen);
2290                                         if (ilen) {
2291                                                 gtk_stext_forward_delete(text,
2292                                                                          ilen);
2293                                                 tlen -= ilen;
2294                                         }
2295                                 } else if (qlen) {
2296                                         if (gtkut_text_str_compare
2297                                             (text, cur_pos, tlen, qfmt)) {
2298                                                 gtk_stext_forward_delete
2299                                                         (text, qlen);
2300                                                 tlen -= qlen;
2301                                         }
2302                                 }
2303
2304                                 GET_CHAR(cur_pos, cb, clen);
2305
2306                                 /* insert space if it's alphanumeric */
2307                                 if ((cur_pos != line_pos) &&
2308                                     ((clen > 1) || isalnum(cb[0]))) {
2309                                         gtk_stext_insert(text, NULL, NULL,
2310                                                         NULL, " ", 1);
2311                                         /* gtk_text_compact_buffer(text); */
2312                                         tlen++;
2313                                 }
2314
2315                                 /* and start over with current line */
2316                                 cur_pos = p_pos - 1;
2317                                 line_pos = cur_pos;
2318                                 line_len = cur_len = 0;
2319                                 qlen = 0;
2320                                 do_delete = FALSE;
2321                                 is_new_line = TRUE;
2322 #ifdef WRAP_DEBUG
2323                                 printf("after delete l_pos=");
2324                                 dump_text(text, line_pos, tlen, 1);
2325 #endif
2326                                 continue;
2327                         }
2328
2329                         /* mark new line beginning */
2330                         line_pos = cur_pos + 1;
2331                         line_len = cur_len = 0;
2332                         qlen = 0;
2333                         do_delete = FALSE;
2334                         is_new_line = TRUE;
2335                         continue;
2336                 }
2337
2338                 if (ch_len < 0) {
2339                         cbuf[0] = '\0';
2340                         ch_len = 1;
2341                 }
2342
2343                 /* possible line break */
2344                 if (ch_len == 1 && isspace(*cbuf)) {
2345                         line_pos = cur_pos + 1;
2346                         line_len = cur_len + ch_len;
2347                 }
2348
2349                 /* are we over wrapping length set in preferences ? */
2350                 if (cur_len + ch_len > linewrap_len) {
2351                         gint clen;
2352
2353 #ifdef WRAP_DEBUG
2354                         printf("should wrap cur_pos=%d ", cur_pos);
2355                         dump_text(text, p_pos, tlen, 1);
2356                         dump_text(text, line_pos, tlen, 1);
2357 #endif
2358                         /* force wrapping if it is one long word but not URL */
2359                         if (p_pos + i_len == line_pos)
2360                                 if (!gtkut_text_is_uri_string
2361                                     (text, line_pos, tlen))
2362                                         line_pos = cur_pos - 1;
2363 #ifdef WRAP_DEBUG
2364                         printf("new line_pos=%d\n", line_pos);
2365 #endif
2366
2367                         GET_CHAR(line_pos - 1, cbuf, clen);
2368
2369                         /* if next character is space delete it */
2370                         if (clen == 1 && isspace(*cbuf)) {
2371                                 if (p_pos + i_len != line_pos ||
2372                                     !gtkut_text_is_uri_string
2373                                         (text, line_pos, tlen)) {
2374                                         gtk_stext_set_point(text, line_pos);
2375                                         gtk_stext_backward_delete(text, 1);
2376                                         tlen--;
2377                                         cur_pos--;
2378                                         line_pos--;
2379                                         cur_len--;
2380                                         line_len--;
2381                                 }
2382                         }
2383
2384                         /* if it is URL at beginning of line don't wrap */
2385                         if (p_pos + i_len == line_pos &&
2386                             gtkut_text_is_uri_string(text, line_pos, tlen)) {
2387 #ifdef WRAP_DEBUG
2388                                 printf("found URL at ");
2389                                 dump_text(text, line_pos, tlen, 1);
2390 #endif
2391                                 continue;
2392                         }
2393
2394                         /* insert CR */
2395                         gtk_stext_set_point(text, line_pos);
2396                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2397                         /* gtk_stext_compact_buffer(text); */
2398                         tlen++;
2399                         line_pos++;
2400                         /* for loop will increase it */
2401                         cur_pos = line_pos - 1;
2402                         /* start over with current line */
2403                         is_new_line = TRUE;
2404                         line_len = 0;
2405                         cur_len = 0;
2406                         do_delete = TRUE;
2407 #ifdef WRAP_DEBUG
2408                         printf("after CR insert ");
2409                         dump_text(text, line_pos, tlen, 1);
2410                         dump_text(text, cur_pos, tlen, 1);
2411 #endif
2412
2413                         /* should we insert quotation ? */
2414                         if (linewrap_quote && qlen) {
2415                                 /* only if line is not already quoted  */
2416                                 if (!gtkut_text_str_compare
2417                                         (text, line_pos, tlen, qfmt)) {
2418                                         guint ins_len;
2419
2420                                         if (line_pos - p_pos > i_len) {
2421                                                 ins_len = ins_quote
2422                                                         (text, qlen, i_len,
2423                                                          p_pos, tlen, qfmt);
2424
2425                                                 /* gtk_stext_compact_buffer(text); */
2426                                                 tlen += ins_len;
2427                                         }
2428 #ifdef WRAP_DEBUG
2429                                         printf("after quote insert ");
2430                                         dump_text(text, line_pos, tlen, 1);
2431 #endif
2432                                 }
2433                         }
2434                         continue;
2435                 }
2436
2437                 if (ch_len > 1) {
2438                         line_pos = cur_pos + 1;
2439                         line_len = cur_len + ch_len;
2440                 }
2441                 /* advance to next character in buffer */
2442                 cur_len += ch_len;
2443         }
2444
2445         gtk_stext_thaw(text);
2446 }
2447 #undef GET_CHAR
2448
2449 static void compose_set_title(Compose *compose)
2450 {
2451         gchar *str;
2452         gchar *edited;
2453
2454         edited = compose->modified ? _(" [Edited]") : "";
2455         if (compose->account && compose->account->address)
2456                 str = g_strdup_printf(_("%s - Compose message%s"),
2457                                       compose->account->address, edited);
2458         else
2459                 str = g_strdup_printf(_("Compose message%s"), edited);
2460         gtk_window_set_title(GTK_WINDOW(compose->window), str);
2461         g_free(str);
2462 }
2463
2464 /**
2465  * compose_current_mail_account:
2466  * 
2467  * Find a current mail account (the currently selected account, or the
2468  * default account, if a news account is currently selected).  If a
2469  * mail account cannot be found, display an error message.
2470  * 
2471  * Return value: Mail account, or NULL if not found.
2472  **/
2473 static PrefsAccount *
2474 compose_current_mail_account(void)
2475 {
2476         PrefsAccount *ac;
2477
2478         if (cur_account && cur_account->protocol != A_NNTP)
2479                 ac = cur_account;
2480         else {
2481                 ac = account_get_default();
2482                 if (!ac || ac->protocol == A_NNTP) {
2483                         alertpanel_error(_("Account for sending mail is not specified.\n"
2484                                            "Please select a mail account before sending."));
2485                         return NULL;
2486                 }
2487         }
2488         return ac;
2489 }
2490
2491 gboolean compose_check_for_valid_recipient(Compose *compose) {
2492         gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
2493         gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
2494         gboolean recipient_found = FALSE;
2495         GSList *list;
2496         gchar **strptr;
2497
2498         /* free to and newsgroup list */
2499         slist_free_strings(compose->to_list);
2500         g_slist_free(compose->to_list);
2501         compose->to_list = NULL;
2502                         
2503         slist_free_strings(compose->newsgroup_list);
2504         g_slist_free(compose->newsgroup_list);
2505         compose->newsgroup_list = NULL;
2506
2507         /* search header entries for to and newsgroup entries */
2508         for(list = compose->header_list; list; list = list->next) {
2509                 gchar *header;
2510                 gchar *entry;
2511                 header = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry));
2512                 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
2513                 g_strstrip(entry);
2514                 if(entry[0] != '\0') {
2515                         for(strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
2516                                 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2517                                         compose->to_list = address_list_append(compose->to_list, entry);
2518                                         recipient_found = TRUE;
2519                                 }
2520                         }
2521                         for(strptr = recipient_headers_news; *strptr != NULL; strptr++) {
2522                                 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2523                                         compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
2524                                         recipient_found = TRUE;
2525                                 }
2526                         }
2527                 }
2528                 g_free(entry);
2529         }
2530         return recipient_found;
2531 }
2532
2533 gint compose_send(Compose *compose)
2534 {
2535         gint msgnum;
2536         FolderItem *folder;
2537         gint val;
2538
2539         val = compose_queue(compose, &msgnum, &folder);
2540         if (val) {
2541                 alertpanel_error(_("Could not queue message for sending"));
2542                 return -1;
2543         }
2544         
2545         val = procmsg_send_message_queue(folder_item_fetch_msg(folder, msgnum));
2546         if(!val) {
2547                 folder_item_remove_msg(folder, msgnum);
2548                 folderview_update_item(folder, TRUE);
2549         }
2550
2551         return val;
2552 }
2553
2554 #if 0 /* compose restructure */
2555 gint compose_send(Compose *compose)
2556 {
2557         gchar tmp[MAXPATHLEN + 1];
2558         gint ok = 0;
2559         static gboolean lock = FALSE;
2560
2561         if (lock) return 1;
2562
2563         g_return_val_if_fail(compose->account != NULL, -1);
2564         g_return_val_if_fail(compose->orig_account != NULL, -1);
2565
2566         lock = TRUE;
2567
2568         if(!compose_check_for_valid_recipient(compose)) {
2569                 alertpanel_error(_("Recipient is not specified."));
2570                 lock = FALSE;
2571                 return 1;
2572         }
2573
2574         /* write to temporary file */
2575         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
2576                    get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
2577
2578         if (prefs_common.linewrap_at_send)
2579                 compose_wrap_line_all(compose);
2580
2581         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
2582                 lock = FALSE;
2583                 return -1;
2584         }
2585
2586         if (!compose->to_list && !compose->newsgroup_list) {
2587                 g_warning(_("can't get recipient list."));
2588                 unlink(tmp);
2589                 lock = FALSE;
2590                 return -1;
2591         }
2592
2593         if (compose->to_list) {
2594                 PrefsAccount *ac;
2595
2596 #if 0 /* NEW COMPOSE GUI */
2597                 if (compose->account->protocol != A_NNTP)
2598                         ac = compose->account;
2599                 else if (compose->orig_account->protocol != A_NNTP)
2600                         ac = compose->orig_account;
2601                 else if (cur_account && cur_account->protocol != A_NNTP)
2602                         ac = cur_account;
2603                 else {
2604                         ac = compose_current_mail_account();
2605                         if (!ac) {
2606                                 unlink(tmp);
2607                                 lock = FALSE;
2608                                 return -1;
2609                         }
2610                 }
2611 #endif
2612                 ac = compose->account;
2613
2614                 ok = send_message(tmp, ac, compose->to_list);
2615                 statusbar_pop_all();
2616         }
2617
2618         if (ok == 0 && compose->newsgroup_list) {
2619                 Folder *folder;
2620
2621                 if (compose->account->protocol == A_NNTP)
2622                         folder = FOLDER(compose->account->folder);
2623                 else
2624                         folder = FOLDER(compose->orig_account->folder);
2625
2626                 ok = news_post(folder, tmp);
2627                 if (ok < 0) {
2628                         alertpanel_error(_("Error occurred while posting the message to %s ."),
2629                                          compose->account->nntp_server);
2630                         unlink(tmp);
2631                         lock = FALSE;
2632                         return -1;
2633                 }
2634         }
2635
2636         /* queue message if failed to send */
2637         if (ok < 0) {
2638                 if (prefs_common.queue_msg) {
2639                         AlertValue val;
2640
2641                         val = alertpanel
2642                                 (_("Queueing"),
2643                                  _("Error occurred while sending the message.\n"
2644                                    "Put this message into queue folder?"),
2645                                  _("OK"), _("Cancel"), NULL);
2646                         if (G_ALERTDEFAULT == val) {
2647                                 ok = compose_queue(compose, tmp);
2648                                 if (ok < 0)
2649                                         alertpanel_error(_("Can't queue the message."));
2650                         }
2651                 } else
2652                         alertpanel_error(_("Error occurred while sending the message."));
2653         } else {
2654                 if (compose->mode == COMPOSE_REEDIT) {
2655                         compose_remove_reedit_target(compose);
2656                         if (compose->targetinfo)
2657                                 folderview_update_item
2658                                         (compose->targetinfo->folder, TRUE);
2659                 }
2660         }
2661
2662         /* save message to outbox */
2663         if (ok == 0 && prefs_common.savemsg) {
2664                 if (compose_save_to_outbox(compose, tmp) < 0)
2665                         alertpanel_error
2666                                 (_("Can't save the message to outbox."));
2667         }
2668
2669         unlink(tmp);
2670         lock = FALSE;
2671         return ok;
2672 }
2673 #endif
2674
2675 static gboolean compose_use_attach(Compose *compose) {
2676     return(gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL);
2677 }
2678
2679 static gint compose_bounce_write_headers_from_headerlist(Compose *compose, 
2680                                                          FILE *fp)
2681 {
2682         gchar buf[BUFFSIZE];
2683         gchar *str;
2684         gboolean first_address;
2685         GSList *list;
2686         ComposeHeaderEntry *headerentry;
2687         gchar *headerentryname;
2688         gchar *header_w_colon;
2689         gchar *cc_hdr;
2690         gchar *to_hdr;
2691
2692         debug_print(_("Writing bounce header\n"));
2693
2694         header_w_colon = g_strconcat("To:", NULL);
2695         to_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2696         header_w_colon = g_strconcat("Cc:", NULL);
2697         cc_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2698         
2699         first_address = TRUE;
2700         for(list = compose->header_list; list; list = list->next) {
2701                 headerentry = ((ComposeHeaderEntry *)list->data);
2702                 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
2703
2704                 if(g_strcasecmp(headerentryname, cc_hdr) == 0 
2705                    || g_strcasecmp(headerentryname, to_hdr) == 0) {
2706                         str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
2707                         Xstrdup_a(str, str, return -1);
2708                         g_strstrip(str);
2709                         if(str[0] != '\0') {
2710                                 compose_convert_header
2711                                         (buf, sizeof(buf), str,
2712                                         strlen("Resent-To") + 2);
2713                                 if(first_address) {
2714                                         fprintf(fp, "Resent-To: ");
2715                                         first_address = FALSE;
2716                                 } else {
2717                                         fprintf(fp, ",");
2718                                 }
2719                                 fprintf(fp, "%s", buf);
2720                         }
2721                 }
2722         }
2723         /* if(!first_address) { */
2724         fprintf(fp, "\n");
2725         /* } */
2726
2727         return(0);
2728 }
2729
2730 static gint compose_bounce_write_headers(Compose *compose, FILE *fp)
2731 {
2732         gchar buf[BUFFSIZE];
2733         gchar *str;
2734         /* struct utsname utsbuf; */
2735
2736         g_return_val_if_fail(fp != NULL, -1);
2737         g_return_val_if_fail(compose->account != NULL, -1);
2738         g_return_val_if_fail(compose->account->address != NULL, -1);
2739
2740         /* Date */
2741         get_rfc822_date(buf, sizeof(buf));
2742         fprintf(fp, "Resent-Date: %s\n", buf);
2743
2744         /* From */
2745         if (compose->account->name && *compose->account->name) {
2746                 compose_convert_header
2747                         (buf, sizeof(buf), compose->account->name,
2748                          strlen("From: "));
2749                 fprintf(fp, "Resent-From: %s <%s>\n",
2750                         buf, compose->account->address);
2751         } else
2752                 fprintf(fp, "Resent-From: %s\n", compose->account->address);
2753
2754         /* To */
2755         compose_bounce_write_headers_from_headerlist(compose, fp);
2756
2757         /* separator between header and body */
2758         fputs("\n", fp);
2759
2760         return 0;
2761 }
2762
2763 static gint compose_bounce_write_to_file(Compose *compose, const gchar *file)
2764 {
2765         FILE *fp;
2766         FILE *fdest;
2767         size_t len;
2768         gchar buf[BUFFSIZE];
2769
2770         if ((fp = fopen(compose->bounce_filename, "r")) == NULL) {
2771                 FILE_OP_ERROR(file, "fopen");
2772                 return -1;
2773         }
2774
2775         if ((fdest = fopen(file, "w")) == NULL) {
2776                 FILE_OP_ERROR(file, "fopen");
2777                 fclose(fp);
2778                 return -1;
2779         }
2780
2781         /* chmod for security */
2782         if (change_file_mode_rw(fdest, file) < 0) {
2783                 FILE_OP_ERROR(file, "chmod");
2784                 g_warning(_("can't change file mode\n"));
2785         }
2786
2787         while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
2788                 /* should filter returnpath, delivered-to */
2789                 if ((g_strncasecmp(buf, "Return-Path:",
2790                                    strlen("Return-Path:")) == 0)
2791                     || (g_strncasecmp(buf, "Delivered-To:",
2792                                       strlen("Delivered-To:")) == 0))
2793                         continue;
2794
2795                 if (fputs(buf, fdest) == -1)
2796                         goto error;
2797
2798                 if (g_strncasecmp(buf, "From:", strlen("From:")) == 0) {
2799                         fputs(" (by way of ", fdest);
2800                         if (compose->account->name
2801                             && *compose->account->name) {
2802                                 compose_convert_header
2803                                         (buf, sizeof(buf),
2804                                          compose->account->name,
2805                                          strlen("From: "));
2806                                 fprintf(fdest, "%s <%s>",
2807                                         buf, compose->account->address);
2808                         } else
2809                                 fprintf(fdest, "%s",
2810                                         compose->account->address);
2811                         fputs(")", fdest);
2812                 }
2813
2814                 if (fputs("\n", fdest) == -1)
2815                         goto error;
2816         }
2817
2818         compose_bounce_write_headers(compose, fdest);
2819
2820         while ((len = fread(buf, sizeof(gchar), BUFFSIZE, fp)) > 0) {
2821                 if (fwrite(buf, sizeof(gchar), len, fdest) == -1)
2822                         goto error;
2823         }
2824
2825         fclose(fdest);
2826         fclose(fp);
2827
2828         return 0;
2829  error:
2830         fclose(fdest);
2831         fclose(fp);
2832
2833         return -1;
2834 }
2835
2836 static gint compose_write_to_file(Compose *compose, const gchar *file,
2837                                   gboolean is_draft)
2838 {
2839         FILE *fp;
2840         size_t len;
2841         gchar *chars;
2842         gchar *buf;
2843         const gchar *out_codeset;
2844         EncodingType encoding;
2845
2846         if ((fp = fopen(file, "w")) == NULL) {
2847                 FILE_OP_ERROR(file, "fopen");
2848                 return -1;
2849         }
2850
2851         /* chmod for security */
2852         if (change_file_mode_rw(fp, file) < 0) {
2853                 FILE_OP_ERROR(file, "chmod");
2854                 g_warning(_("can't change file mode\n"));
2855         }
2856
2857         /* get all composed text */
2858         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2859         len = strlen(chars);
2860         if (is_ascii_str(chars)) {
2861                 buf = g_strdup(chars);
2862                 out_codeset = CS_US_ASCII;
2863                 encoding = ENC_7BIT;
2864         } else {
2865                 const gchar *src_codeset;
2866
2867                 out_codeset = conv_get_outgoing_charset_str();
2868                 if (!strcasecmp(out_codeset, CS_US_ASCII))
2869                         out_codeset = CS_ISO_8859_1;
2870                 encoding = procmime_get_encoding_for_charset(out_codeset);
2871
2872                 src_codeset = conv_get_current_charset_str();
2873                 /* if current encoding is US-ASCII, set it the same as
2874                    outgoing one to prevent code conversion failure */
2875                 if (!strcasecmp(src_codeset, CS_US_ASCII))
2876                         src_codeset = out_codeset;
2877
2878                 debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
2879                             src_codeset, out_codeset, procmime_get_encoding_str(encoding));
2880
2881                 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
2882                 if (!buf) {
2883                         g_free(chars);
2884                         fclose(fp);
2885                         unlink(file);
2886                         alertpanel_error(_("Can't convert the codeset of the message."));
2887                         return -1;
2888                 }
2889         }
2890         g_free(chars);
2891
2892         /* write headers */
2893         if (compose_write_headers
2894                 (compose, fp, out_codeset, encoding, is_draft) < 0) {
2895                 g_warning(_("can't write headers\n"));
2896                 fclose(fp);
2897                 /* unlink(file); */
2898                 g_free(buf);
2899                 return -1;
2900         }
2901
2902         if (compose_use_attach(compose)) {
2903 #if USE_GPGME
2904             /* This prolog message is ignored by mime software and
2905              * because it would make our signing/encryption task
2906              * tougher, we don't emit it in that case */
2907             if (!compose->use_signing && !compose->use_encryption)
2908 #endif
2909                 fputs("This is a multi-part message in MIME format.\n", fp);
2910
2911                 fprintf(fp, "\n--%s\n", compose->boundary);
2912                 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
2913                         out_codeset);
2914                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2915                         procmime_get_encoding_str(encoding));
2916                 fputc('\n', fp);
2917         }
2918
2919         /* write body */
2920         len = strlen(buf);
2921         if (encoding == ENC_BASE64) {
2922                 gchar outbuf[B64_BUFFSIZE];
2923                 gint i, l;
2924
2925                 for (i = 0; i < len; i += B64_LINE_SIZE) {
2926                         l = MIN(B64_LINE_SIZE, len - i);
2927                         to64frombits(outbuf, buf + i, l);
2928                         fputs(outbuf, fp);
2929                         fputc('\n', fp);
2930                 }
2931         } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
2932                 FILE_OP_ERROR(file, "fwrite");
2933                 fclose(fp);
2934                 unlink(file);
2935                 g_free(buf);
2936                 return -1;
2937         }
2938         g_free(buf);
2939
2940         if (compose_use_attach(compose))
2941                 compose_write_attach(compose, fp);
2942
2943         if (fclose(fp) == EOF) {
2944                 FILE_OP_ERROR(file, "fclose");
2945                 unlink(file);
2946                 return -1;
2947         }
2948
2949 #if USE_GPGME
2950         if (compose->use_signing) {
2951                 if (rfc2015_sign(file, compose->account) < 0) {
2952                         unlink(file);
2953                         return -1;
2954                 }
2955         }
2956         if (compose->use_encryption) {
2957                 if (rfc2015_encrypt(file, compose->to_list) < 0) {
2958                         unlink(file);
2959                         return -1;
2960                 }
2961         }
2962 #endif /* USE_GPGME */
2963
2964         return 0;
2965 }
2966
2967 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
2968 {
2969         FILE *fp;
2970         size_t len;
2971         gchar *chars;
2972
2973         if ((fp = fopen(file, "w")) == NULL) {
2974                 FILE_OP_ERROR(file, "fopen");
2975                 return -1;
2976         }
2977
2978         /* chmod for security */
2979         if (change_file_mode_rw(fp, file) < 0) {
2980                 FILE_OP_ERROR(file, "chmod");
2981                 g_warning(_("can't change file mode\n"));
2982         }
2983
2984         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2985
2986         /* write body */
2987         len = strlen(chars);
2988         if (fwrite(chars, sizeof(gchar), len, fp) != len) {
2989                 FILE_OP_ERROR(file, "fwrite");
2990                 g_free(chars);
2991                 fclose(fp);
2992                 unlink(file);
2993                 return -1;
2994         }
2995
2996         g_free(chars);
2997
2998         if (fclose(fp) == EOF) {
2999                 FILE_OP_ERROR(file, "fclose");
3000                 unlink(file);
3001                 return -1;
3002         }
3003         return 0;
3004 }
3005
3006 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
3007 {
3008         FolderItem *outbox;
3009         gchar *path;
3010         gint num;
3011         FILE *fp;
3012
3013         debug_print(_("saving sent message...\n"));
3014
3015         outbox = folder_get_default_outbox();
3016         path = folder_item_get_path(outbox);
3017         if (!is_dir_exist(path))
3018                 make_dir_hier(path);
3019
3020         folder_item_scan(outbox);
3021         if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
3022                 g_free(path);
3023                 g_warning(_("can't save message\n"));
3024                 return -1;
3025         }
3026
3027         if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
3028                 g_warning(_("can't open mark file\n"));
3029         else {
3030                 MsgInfo newmsginfo;
3031
3032                 newmsginfo.msgnum = num;
3033                 newmsginfo.flags.perm_flags = 0;
3034                 newmsginfo.flags.tmp_flags = 0;
3035                 procmsg_write_flags(&newmsginfo, fp);
3036                 fclose(fp);
3037         }
3038         g_free(path);
3039
3040         return 0;
3041 }
3042
3043 static gint compose_remove_reedit_target(Compose *compose)
3044 {
3045         FolderItem *item;
3046         MsgInfo *msginfo = compose->targetinfo;
3047
3048         g_return_val_if_fail(compose->mode == COMPOSE_REEDIT, -1);
3049         if (!msginfo) return -1;
3050
3051         item = msginfo->folder;
3052         g_return_val_if_fail(item != NULL, -1);
3053
3054         folder_item_scan(item);
3055         if (procmsg_msg_exist(msginfo) &&
3056             (item->stype == F_DRAFT || item->stype == F_QUEUE)) {
3057                 if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
3058                         g_warning(_("can't remove the old message\n"));
3059                         return -1;
3060                 }
3061         }
3062
3063         return 0;
3064 }
3065
3066
3067 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
3068 {
3069         FolderItem *queue;
3070         gchar *tmp, *tmp2, *queue_path;
3071         FILE *fp, *src_fp;
3072         GSList *cur;
3073         gchar buf[BUFFSIZE];
3074         gint num;
3075         static gboolean lock = FALSE;
3076         PrefsAccount *mailac = NULL, *newsac = NULL;
3077         
3078         debug_print(_("queueing message...\n"));
3079         g_return_val_if_fail(compose->account != NULL, -1);
3080         g_return_val_if_fail(compose->orig_account != NULL, -1);
3081
3082         lock = TRUE;
3083         
3084         if(!compose_check_for_valid_recipient(compose)) {
3085                 alertpanel_error(_("Recipient is not specified."));
3086                 lock = FALSE;
3087                 return -1;
3088         }
3089                                                                         
3090         if (!compose->to_list && !compose->newsgroup_list) {
3091                 g_warning(_("can't get recipient list."));
3092                 lock = FALSE;
3093                 return -1;
3094         }
3095
3096         if(compose->to_list) {
3097                 if (compose->account->protocol != A_NNTP)
3098                         mailac = compose->account;
3099                 else if (compose->orig_account->protocol != A_NNTP)
3100                         mailac = compose->orig_account;
3101                 else if (cur_account && cur_account->protocol != A_NNTP)
3102                         mailac = cur_account;
3103                 else if (!(mailac = compose_current_mail_account())) {
3104                         lock = FALSE;
3105                         alertpanel_error(_("No account for sending mails available!"));
3106                         return -1;
3107                 }
3108         }
3109
3110         if(compose->newsgroup_list) {
3111                 if (compose->account->protocol == A_NNTP)
3112                         newsac = compose->account;
3113                 else if(!(newsac = compose->orig_account) || (newsac->protocol != A_NNTP)) {
3114                         lock = FALSE;
3115                         alertpanel_error(_("No account for posting news available!"));
3116                         return -1;
3117                 }                       
3118         }
3119
3120         if (prefs_common.linewrap_at_send)
3121                 compose_wrap_line_all(compose);
3122                         
3123         /* write to temporary file */
3124         tmp2 = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
3125                                       G_DIR_SEPARATOR, (gint)compose);
3126
3127         if (compose->bounce_filename != NULL) {
3128                 if (compose_bounce_write_to_file(compose, tmp2) < 0) {
3129                         unlink(tmp2);
3130                         lock = FALSE;
3131                         return -1;
3132                 }
3133         }
3134         else {
3135                 if (compose_write_to_file(compose, tmp2, FALSE) < 0) {
3136                         unlink(tmp2);
3137                         lock = FALSE;
3138                         return -1;
3139                 }
3140         }
3141
3142         /* add queue header */
3143         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
3144                                       G_DIR_SEPARATOR, (gint)compose);
3145         if ((fp = fopen(tmp, "w")) == NULL) {
3146                 FILE_OP_ERROR(tmp, "fopen");
3147                 g_free(tmp);
3148                 return -1;
3149         }
3150         if ((src_fp = fopen(tmp2, "r")) == NULL) {
3151                 FILE_OP_ERROR(tmp2, "fopen");
3152                 fclose(fp);
3153                 unlink(tmp);
3154                 g_free(tmp);
3155                 unlink(tmp2);
3156                 g_free(tmp2);
3157                 return -1;
3158         }
3159         if (change_file_mode_rw(fp, tmp) < 0) {
3160                 FILE_OP_ERROR(tmp, "chmod");
3161                 g_warning(_("can't change file mode\n"));
3162         }
3163
3164         /* queueing variables */
3165         fprintf(fp, "AF:\n");
3166         fprintf(fp, "NF:0\n");
3167         fprintf(fp, "PS:10\n");
3168         fprintf(fp, "SRH:1\n");
3169         fprintf(fp, "SFN:\n");
3170         fprintf(fp, "DSR:\n");
3171         if (compose->msgid)
3172                 fprintf(fp, "MID:<%s>\n", compose->msgid);
3173         else
3174                 fprintf(fp, "MID:\n");
3175         fprintf(fp, "CFG:\n");
3176         fprintf(fp, "PT:0\n");
3177         fprintf(fp, "S:%s\n", compose->account->address);
3178         fprintf(fp, "RQ:\n");
3179         if (mailac)
3180                 fprintf(fp, "SSV:%s\n", mailac->smtp_server);
3181         else
3182                 fprintf(fp, "SSV:\n");
3183         if (newsac)
3184                 fprintf(fp, "NSV:%s\n", newsac->nntp_server);
3185         else
3186                 fprintf(fp, "NSV:\n");
3187         fprintf(fp, "SSH:\n");
3188         /* write recepient list */
3189         fprintf(fp, "R:");
3190         if(compose->to_list) {
3191                 fprintf(fp, "<%s>", (gchar *)compose->to_list->data);
3192                 for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
3193                         fprintf(fp, ",<%s>", (gchar *)cur->data);
3194         }
3195         fprintf(fp, "\n");
3196         /* write newsgroup list */
3197         fprintf(fp, "NG:");
3198         if(compose->newsgroup_list) {
3199                 fprintf(fp, "%s", (gchar *)compose->newsgroup_list->data);
3200                 for (cur = compose->newsgroup_list->next; cur != NULL; cur = cur->next)
3201                         fprintf(fp, ",%s", (gchar *)cur->data);
3202         }
3203         fprintf(fp, "\n");
3204         /* Sylpheed account IDs */
3205         if(mailac) {
3206                 fprintf(fp, "MAID:%d\n", mailac->account_id);
3207         }
3208         if(newsac) {
3209                 fprintf(fp, "NAID:%d\n", newsac->account_id);
3210         }
3211         /* Save copy folder */
3212         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn))) {
3213                 gchar *str;
3214                 
3215                 str = gtk_editable_get_chars(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
3216                 fprintf(fp, "SCF:%s\n", str);
3217                 g_free(str);
3218         }
3219         fprintf(fp, "\n");
3220
3221         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
3222                 if (fputs(buf, fp) == EOF) {
3223                         FILE_OP_ERROR(tmp, "fputs");
3224                         fclose(fp);
3225                         fclose(src_fp);
3226                         unlink(tmp);
3227                         g_free(tmp);
3228                         unlink(tmp2);
3229                         g_free(tmp2);
3230                         return -1;
3231                 }
3232         }
3233         fclose(src_fp);
3234         if (fclose(fp) == EOF) {
3235                 FILE_OP_ERROR(tmp, "fclose");
3236                 unlink(tmp);
3237                 g_free(tmp);
3238                 unlink(tmp2);
3239                 g_free(tmp2);
3240                 return -1;
3241         }
3242                                                 
3243         /* queue message */
3244         queue = folder_get_default_queue();
3245
3246         folder_item_scan(queue);
3247         queue_path = folder_item_get_path(queue);
3248         if (!is_dir_exist(queue_path))
3249                 make_dir_hier(queue_path);
3250         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
3251                 g_warning(_("can't queue the message\n"));
3252                 unlink(tmp);
3253                 g_free(tmp);
3254                 g_free(queue_path);
3255                 return -1;
3256         }
3257         unlink(tmp);
3258         g_free(tmp);
3259         unlink(tmp2);
3260         g_free(tmp2);
3261
3262         if (compose->mode == COMPOSE_REEDIT) {
3263                 compose_remove_reedit_target(compose);
3264                 if (compose->targetinfo &&
3265                     compose->targetinfo->folder != queue)
3266                         folderview_update_item
3267                                 (compose->targetinfo->folder, TRUE);
3268         }
3269
3270         if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
3271                 g_warning(_("can't open mark file\n"));
3272         else {
3273                 MsgInfo newmsginfo;
3274
3275                 newmsginfo.msgnum = num;
3276                 newmsginfo.flags.perm_flags = 0;
3277                 newmsginfo.flags.tmp_flags = 0;
3278                 procmsg_write_flags(&newmsginfo, fp);
3279                 fclose(fp);
3280         }
3281         g_free(queue_path);
3282
3283         folder_item_scan(queue);
3284         folderview_update_item(queue, TRUE);
3285
3286         if((msgnum != NULL) && (item != NULL)) {
3287                 *msgnum = num;
3288                 *item = queue;
3289         }
3290
3291         return 0;
3292 }
3293