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