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