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