2006-06-25 [ticho] 2.3.1cvs19
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #ifndef PANGO_ENABLE_ENGINE
27 #  define PANGO_ENABLE_ENGINE
28 #endif
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtkmain.h>
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkmenuitem.h>
36 #include <gtk/gtkitemfactory.h>
37 #include <gtk/gtkcheckmenuitem.h>
38 #include <gtk/gtkoptionmenu.h>
39 #include <gtk/gtkwidget.h>
40 #include <gtk/gtkvpaned.h>
41 #include <gtk/gtkentry.h>
42 #include <gtk/gtkeditable.h>
43 #include <gtk/gtkwindow.h>
44 #include <gtk/gtksignal.h>
45 #include <gtk/gtkvbox.h>
46 #include <gtk/gtkcontainer.h>
47 #include <gtk/gtkhandlebox.h>
48 #include <gtk/gtktoolbar.h>
49 #include <gtk/gtktable.h>
50 #include <gtk/gtkhbox.h>
51 #include <gtk/gtklabel.h>
52 #include <gtk/gtkscrolledwindow.h>
53 #include <gtk/gtktreeview.h>
54 #include <gtk/gtkliststore.h>
55 #include <gtk/gtktreeselection.h>
56 #include <gtk/gtktreemodel.h>
57
58 #include <gtk/gtkdnd.h>
59 #include <gtk/gtkclipboard.h>
60 #include <pango/pango-break.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <ctype.h>
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #include <unistd.h>
68 #include <time.h>
69 #include <stdlib.h>
70 #if HAVE_SYS_WAIT_H
71 #  include <sys/wait.h>
72 #endif
73 #include <signal.h>
74 #include <errno.h>
75 #ifndef G_OS_WIN32  /* fixme we should have a configure test. */
76 #include <libgen.h>
77 #endif
78
79 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
80 #  include <wchar.h>
81 #  include <wctype.h>
82 #endif
83
84 #include "sylpheed.h"
85 #include "main.h"
86 #include "mainwindow.h"
87 #include "compose.h"
88 #include "addressbook.h"
89 #include "folderview.h"
90 #include "procmsg.h"
91 #include "menu.h"
92 #include "stock_pixmap.h"
93 #include "send_message.h"
94 #include "imap.h"
95 #include "news.h"
96 #include "customheader.h"
97 #include "prefs_common.h"
98 #include "prefs_account.h"
99 #include "action.h"
100 #include "account.h"
101 #include "filesel.h"
102 #include "procheader.h"
103 #include "procmime.h"
104 #include "statusbar.h"
105 #include "about.h"
106 #include "base64.h"
107 #include "quoted-printable.h"
108 #include "codeconv.h"
109 #include "utils.h"
110 #include "gtkutils.h"
111 #include "socket.h"
112 #include "alertpanel.h"
113 #include "manage_window.h"
114 #include "gtkshruler.h"
115 #include "folder.h"
116 #include "addr_compl.h"
117 #include "quote_fmt.h"
118 #include "undo.h"
119 #include "foldersel.h"
120 #include "toolbar.h"
121 #include "inc.h"
122 #include "message_search.h"
123 #include "combobox.h"
124
125 enum
126 {
127         COL_MIMETYPE = 0,
128         COL_SIZE     = 1,
129         COL_NAME     = 2,
130         COL_DATA     = 3,
131         COL_AUTODATA = 4,
132         N_COL_COLUMNS
133 };
134
135 #define N_ATTACH_COLS   (N_COL_COLUMNS)
136
137 typedef enum
138 {
139         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BEGINNING_OF_LINE,
140         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_CHARACTER,
141         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_CHARACTER,
142         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_WORD,
143         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_WORD,
144         COMPOSE_CALL_ADVANCED_ACTION_MOVE_END_OF_LINE,
145         COMPOSE_CALL_ADVANCED_ACTION_MOVE_NEXT_LINE,
146         COMPOSE_CALL_ADVANCED_ACTION_MOVE_PREVIOUS_LINE,
147         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_CHARACTER,
148         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_CHARACTER,
149         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_WORD,
150         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_WORD,
151         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE,
152         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE_N,
153         COMPOSE_CALL_ADVANCED_ACTION_DELETE_TO_LINE_END
154 } ComposeCallAdvancedAction;
155
156 typedef enum
157 {
158         PRIORITY_HIGHEST = 1,
159         PRIORITY_HIGH,
160         PRIORITY_NORMAL,
161         PRIORITY_LOW,
162         PRIORITY_LOWEST
163 } PriorityLevel;
164
165 typedef enum
166 {
167         COMPOSE_INSERT_SUCCESS,
168         COMPOSE_INSERT_READ_ERROR,
169         COMPOSE_INSERT_INVALID_CHARACTER,
170         COMPOSE_INSERT_NO_FILE
171 } ComposeInsertResult;
172
173 typedef enum
174 {
175         COMPOSE_QUIT_EDITING,
176         COMPOSE_KEEP_EDITING,
177         COMPOSE_AUTO_SAVE
178 } ComposeDraftAction;
179
180 typedef enum
181 {
182         COMPOSE_WRITE_FOR_SEND,
183         COMPOSE_WRITE_FOR_STORE
184 } ComposeWriteType;
185
186 #define B64_LINE_SIZE           57
187 #define B64_BUFFSIZE            77
188
189 #define MAX_REFERENCES_LEN      999
190
191 static GList *compose_list = NULL;
192
193 Compose *compose_generic_new                    (PrefsAccount   *account,
194                                                  const gchar    *to,
195                                                  FolderItem     *item,
196                                                  GPtrArray      *attach_files,
197                                                  GList          *listAddress );
198
199 static Compose *compose_create                  (PrefsAccount   *account,
200                                                  ComposeMode     mode,
201                                                  gboolean batch);
202
203 static GtkWidget *compose_account_option_menu_create
204                                                 (Compose        *compose);
205 static void compose_set_out_encoding            (Compose        *compose);
206 static void compose_set_template_menu           (Compose        *compose);
207 static void compose_template_apply              (Compose        *compose,
208                                                  Template       *tmpl,
209                                                  gboolean        replace);
210 static void compose_destroy                     (Compose        *compose);
211
212 static void compose_entries_set                 (Compose        *compose,
213                                                  const gchar    *mailto);
214 static gint compose_parse_header                (Compose        *compose,
215                                                  MsgInfo        *msginfo);
216 static gchar *compose_parse_references          (const gchar    *ref,
217                                                  const gchar    *msgid);
218
219 static gchar *compose_quote_fmt                 (Compose        *compose,
220                                                  MsgInfo        *msginfo,
221                                                  const gchar    *fmt,
222                                                  const gchar    *qmark,
223                                                  const gchar    *body,
224                                                  gboolean        rewrap);
225
226 static void compose_reply_set_entry             (Compose        *compose,
227                                                  MsgInfo        *msginfo,
228                                                  gboolean        to_all,
229                                                  gboolean        to_ml,
230                                                  gboolean        to_sender,
231                                                  gboolean
232                                                  followup_and_reply_to);
233 static void compose_reedit_set_entry            (Compose        *compose,
234                                                  MsgInfo        *msginfo);
235
236 static void compose_insert_sig                  (Compose        *compose,
237                                                  gboolean        replace);
238 static gchar *compose_get_signature_str         (Compose        *compose);
239 static ComposeInsertResult compose_insert_file  (Compose        *compose,
240                                                  const gchar    *file);
241
242 static void compose_attach_append               (Compose        *compose,
243                                                  const gchar    *file,
244                                                  const gchar    *type,
245                                                  const gchar    *content_type);
246 static void compose_attach_parts                (Compose        *compose,
247                                                  MsgInfo        *msginfo);
248
249 static void compose_beautify_paragraph          (Compose        *compose,
250                                                  GtkTextIter    *par_iter,
251                                                  gboolean        force);
252 static void compose_wrap_all                    (Compose        *compose);
253 static void compose_wrap_all_full               (Compose        *compose,
254                                                  gboolean        autowrap);
255
256 static void compose_set_title                   (Compose        *compose);
257 static void compose_select_account              (Compose        *compose,
258                                                  PrefsAccount   *account,
259                                                  gboolean        init);
260
261 static PrefsAccount *compose_current_mail_account(void);
262 /* static gint compose_send                     (Compose        *compose); */
263 static gboolean compose_check_for_valid_recipient
264                                                 (Compose        *compose);
265 static gboolean compose_check_entries           (Compose        *compose,
266                                                  gboolean       check_subject);
267 static gint compose_write_to_file               (Compose        *compose,
268                                                  FILE           *fp,
269                                                  gint            action,
270                                                  gboolean        attach_parts);
271 static gint compose_write_body_to_file          (Compose        *compose,
272                                                  const gchar    *file);
273 static gint compose_remove_reedit_target        (Compose        *compose,
274                                                  gboolean        force);
275 void compose_remove_draft                       (Compose        *compose);
276 static gint compose_queue_sub                   (Compose        *compose,
277                                                  gint           *msgnum,
278                                                  FolderItem     **item,
279                                                  gchar          **msgpath,
280                                                  gboolean       check_subject,
281                                                  gboolean       remove_reedit_target);
282 static void compose_add_attachments             (Compose        *compose,
283                                                  MimeInfo       *parent);
284 static gchar *compose_get_header                (Compose        *compose);
285
286 static void compose_convert_header              (Compose        *compose,
287                                                  gchar          *dest,
288                                                  gint            len,
289                                                  gchar          *src,
290                                                  gint            header_len,
291                                                  gboolean        addr_field);
292
293 static void compose_attach_info_free            (AttachInfo     *ainfo);
294 static void compose_attach_remove_selected      (Compose        *compose);
295
296 static void compose_attach_property             (Compose        *compose);
297 static void compose_attach_property_create      (gboolean       *cancelled);
298 static void attach_property_ok                  (GtkWidget      *widget,
299                                                  gboolean       *cancelled);
300 static void attach_property_cancel              (GtkWidget      *widget,
301                                                  gboolean       *cancelled);
302 static gint attach_property_delete_event        (GtkWidget      *widget,
303                                                  GdkEventAny    *event,
304                                                  gboolean       *cancelled);
305 static gboolean attach_property_key_pressed     (GtkWidget      *widget,
306                                                  GdkEventKey    *event,
307                                                  gboolean       *cancelled);
308
309 static void compose_exec_ext_editor             (Compose        *compose);
310 #ifdef G_OS_UNIX
311 static gint compose_exec_ext_editor_real        (const gchar    *file);
312 static gboolean compose_ext_editor_kill         (Compose        *compose);
313 static gboolean compose_input_cb                (GIOChannel     *source,
314                                                  GIOCondition    condition,
315                                                  gpointer        data);
316 static void compose_set_ext_editor_sensitive    (Compose        *compose,
317                                                  gboolean        sensitive);
318 #endif /* G_OS_UNIX */
319
320 static void compose_undo_state_changed          (UndoMain       *undostruct,
321                                                  gint            undo_state,
322                                                  gint            redo_state,
323                                                  gpointer        data);
324
325 static void compose_create_header_entry (Compose *compose);
326 static void compose_add_header_entry    (Compose *compose, gchar *header, gchar *text);
327 static void compose_update_priority_menu_item(Compose * compose);
328
329 static void compose_add_field_list      ( Compose *compose,
330                                           GList *listAddress );
331
332 /* callback functions */
333
334 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
335                                          GtkAllocation  *allocation,
336                                          GtkSHRuler     *shruler);
337 static void account_activated           (GtkComboBox *optmenu,
338                                          gpointer        data);
339 static void attach_selected             (GtkTreeView    *tree_view, 
340                                          GtkTreePath    *tree_path,
341                                          GtkTreeViewColumn *column, 
342                                          Compose *compose);
343 static gboolean attach_button_pressed   (GtkWidget      *widget,
344                                          GdkEventButton *event,
345                                          gpointer        data);
346 static gboolean attach_key_pressed      (GtkWidget      *widget,
347                                          GdkEventKey    *event,
348                                          gpointer        data);
349
350 static void compose_send_cb             (gpointer        data,
351                                          guint           action,
352                                          GtkWidget      *widget);
353 static void compose_send_later_cb       (gpointer        data,
354                                          guint           action,
355                                          GtkWidget      *widget);
356
357 static void compose_draft_cb            (gpointer        data,
358                                          guint           action,
359                                          GtkWidget      *widget);
360
361 static void compose_attach_cb           (gpointer        data,
362                                          guint           action,
363                                          GtkWidget      *widget);
364 static void compose_insert_file_cb      (gpointer        data,
365                                          guint           action,
366                                          GtkWidget      *widget);
367 static void compose_insert_sig_cb       (gpointer        data,
368                                          guint           action,
369                                          GtkWidget      *widget);
370
371 static void compose_close_cb            (gpointer        data,
372                                          guint           action,
373                                          GtkWidget      *widget);
374
375 static void compose_set_encoding_cb     (gpointer        data,
376                                          guint           action,
377                                          GtkWidget      *widget);
378
379 static void compose_address_cb          (gpointer        data,
380                                          guint           action,
381                                          GtkWidget      *widget);
382 static void compose_template_activate_cb(GtkWidget      *widget,
383                                          gpointer        data);
384
385 static void compose_ext_editor_cb       (gpointer        data,
386                                          guint           action,
387                                          GtkWidget      *widget);
388
389 static gint compose_delete_cb           (GtkWidget      *widget,
390                                          GdkEventAny    *event,
391                                          gpointer        data);
392
393 static void compose_undo_cb             (Compose        *compose);
394 static void compose_redo_cb             (Compose        *compose);
395 static void compose_cut_cb              (Compose        *compose);
396 static void compose_copy_cb             (Compose        *compose);
397 static void compose_paste_cb            (Compose        *compose);
398 static void compose_paste_as_quote_cb   (Compose        *compose);
399 static void compose_paste_no_wrap_cb    (Compose        *compose);
400 static void compose_paste_wrap_cb       (Compose        *compose);
401 static void compose_allsel_cb           (Compose        *compose);
402
403 static void compose_advanced_action_cb  (Compose                   *compose,
404                                          ComposeCallAdvancedAction  action);
405
406 static void compose_grab_focus_cb       (GtkWidget      *widget,
407                                          Compose        *compose);
408
409 static void compose_changed_cb          (GtkTextBuffer  *textbuf,
410                                          Compose        *compose);
411
412 static void compose_wrap_cb             (gpointer        data,
413                                          guint           action,
414                                          GtkWidget      *widget);
415 static void compose_find_cb             (gpointer        data,
416                                          guint           action,
417                                          GtkWidget      *widget);
418 static void compose_toggle_autowrap_cb  (gpointer        data,
419                                          guint           action,
420                                          GtkWidget      *widget);
421
422 static void compose_toggle_ruler_cb     (gpointer        data,
423                                          guint           action,
424                                          GtkWidget      *widget);
425 static void compose_toggle_sign_cb      (gpointer        data,
426                                          guint           action,
427                                          GtkWidget      *widget);
428 static void compose_toggle_encrypt_cb   (gpointer        data,
429                                          guint           action,
430                                          GtkWidget      *widget);
431 static void compose_set_privacy_system_cb(GtkWidget      *widget,
432                                           gpointer        data);
433 static void compose_update_privacy_system_menu_item(Compose * compose, gboolean warn);
434 static void activate_privacy_system     (Compose *compose, 
435                                          PrefsAccount *account,
436                                          gboolean warn);
437 static void compose_use_signing(Compose *compose, gboolean use_signing);
438 static void compose_use_encryption(Compose *compose, gboolean use_encryption);
439 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
440                                              GtkWidget *widget);
441 static void compose_toggle_remove_refs_cb(gpointer data, guint action,
442                                              GtkWidget *widget);
443 static void compose_set_priority_cb     (gpointer        data,
444                                          guint           action,
445                                          GtkWidget      *widget);
446
447 static void compose_attach_drag_received_cb (GtkWidget          *widget,
448                                              GdkDragContext     *drag_context,
449                                              gint                x,
450                                              gint                y,
451                                              GtkSelectionData   *data,
452                                              guint               info,
453                                              guint               time,
454                                              gpointer            user_data);
455 static void compose_insert_drag_received_cb (GtkWidget          *widget,
456                                              GdkDragContext     *drag_context,
457                                              gint                x,
458                                              gint                y,
459                                              GtkSelectionData   *data,
460                                              guint               info,
461                                              guint               time,
462                                              gpointer            user_data);
463 static void compose_header_drag_received_cb (GtkWidget          *widget,
464                                              GdkDragContext     *drag_context,
465                                              gint                x,
466                                              gint                y,
467                                              GtkSelectionData   *data,
468                                              guint               info,
469                                              guint               time,
470                                              gpointer            user_data);
471
472 static gboolean compose_drag_drop           (GtkWidget *widget,
473                                              GdkDragContext *drag_context,
474                                              gint x, gint y,
475                                              guint time, gpointer user_data);
476
477 static void text_inserted               (GtkTextBuffer  *buffer,
478                                          GtkTextIter    *iter,
479                                          const gchar    *text,
480                                          gint            len,
481                                          Compose        *compose);
482 static Compose *compose_generic_reply(MsgInfo *msginfo, gboolean quote,
483                                   gboolean to_all, gboolean to_ml,
484                                   gboolean to_sender,
485                                   gboolean followup_and_reply_to,
486                                   const gchar *body);
487
488 gboolean compose_headerentry_changed_cb    (GtkWidget          *entry,
489                                             ComposeHeaderEntry *headerentry);
490 gboolean compose_headerentry_key_press_event_cb(GtkWidget              *entry,
491                                             GdkEventKey        *event,
492                                             ComposeHeaderEntry *headerentry);
493
494 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
495
496 static void compose_allow_user_actions (Compose *compose, gboolean allow);
497
498 #if USE_ASPELL
499 static void compose_check_all              (Compose *compose);
500 static void compose_highlight_all          (Compose *compose);
501 static void compose_check_backwards        (Compose *compose);
502 static void compose_check_forwards_go      (Compose *compose);
503 #endif
504
505 static gint compose_defer_auto_save_draft       (Compose        *compose);
506 static PrefsAccount *compose_guess_forward_account_from_msginfo (MsgInfo *msginfo);
507
508 static GtkItemFactoryEntry compose_popup_entries[] =
509 {
510         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
511         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
512         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
513         {N_("/_Properties..."), NULL, compose_attach_property, 0, NULL}
514 };
515
516 static GtkItemFactoryEntry compose_entries[] =
517 {
518         {N_("/_Message"),                               NULL, NULL, 0, "<Branch>"},
519         {N_("/_Message/_Send"),         "<control>Return",
520                                         compose_send_cb, 0, NULL},
521         {N_("/_Message/Send _later"),   "<shift><control>S",
522                                         compose_send_later_cb,  0, NULL},
523         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
524         {N_("/_Message/_Attach file"),          "<control>M", compose_attach_cb,      0, NULL},
525         {N_("/_Message/_Insert file"),          "<control>I", compose_insert_file_cb, 0, NULL},
526         {N_("/_Message/Insert si_gnature"),     "<control>G", compose_insert_sig_cb,  0, NULL},
527         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
528         {N_("/_Message/_Save"),
529                                                 "<control>S", compose_draft_cb, COMPOSE_KEEP_EDITING, NULL},
530         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
531         {N_("/_Message/_Close"),                        "<control>W", compose_close_cb, 0, NULL},
532
533         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
534         {N_("/_Edit/_Undo"),            "<control>Z", compose_undo_cb, 0, NULL},
535         {N_("/_Edit/_Redo"),            "<control>Y", compose_redo_cb, 0, NULL},
536         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
537         {N_("/_Edit/Cu_t"),             "<control>X", compose_cut_cb,    0, NULL},
538         {N_("/_Edit/_Copy"),            "<control>C", compose_copy_cb,   0, NULL},
539         {N_("/_Edit/_Paste"),           "<control>V", compose_paste_cb,  0, NULL},
540         {N_("/_Edit/Special paste"),    NULL, NULL, 0, "<Branch>"},
541         {N_("/_Edit/Special paste/as _quotation"),
542                                         NULL, compose_paste_as_quote_cb, 0, NULL},
543         {N_("/_Edit/Special paste/_wrapped"),
544                                         NULL, compose_paste_wrap_cb, 0, NULL},
545         {N_("/_Edit/Special paste/_unwrapped"),
546                                         NULL, compose_paste_no_wrap_cb, 0, NULL},
547         {N_("/_Edit/Select _all"),      "<control>A", compose_allsel_cb, 0, NULL},
548         {N_("/_Edit/A_dvanced"),        NULL, NULL, 0, "<Branch>"},
549         {N_("/_Edit/A_dvanced/Move a character backward"),
550                                         "<shift><control>B",
551                                         compose_advanced_action_cb,
552                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_CHARACTER,
553                                         NULL},
554         {N_("/_Edit/A_dvanced/Move a character forward"),
555                                         "<shift><control>F",
556                                         compose_advanced_action_cb,
557                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_CHARACTER,
558                                         NULL},
559         {N_("/_Edit/A_dvanced/Move a word backward"),
560                                         NULL, /* "<alt>B" */
561                                         compose_advanced_action_cb,
562                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_WORD,
563                                         NULL},
564         {N_("/_Edit/A_dvanced/Move a word forward"),
565                                         NULL, /* "<alt>F" */
566                                         compose_advanced_action_cb,
567                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_WORD,
568                                         NULL},
569         {N_("/_Edit/A_dvanced/Move to beginning of line"),
570                                         NULL, /* "<control>A" */
571                                         compose_advanced_action_cb,
572                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BEGINNING_OF_LINE,
573                                         NULL},
574         {N_("/_Edit/A_dvanced/Move to end of line"),
575                                         "<control>E",
576                                         compose_advanced_action_cb,
577                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_END_OF_LINE,
578                                         NULL},
579         {N_("/_Edit/A_dvanced/Move to previous line"),
580                                         "<control>P",
581                                         compose_advanced_action_cb,
582                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_PREVIOUS_LINE,
583                                         NULL},
584         {N_("/_Edit/A_dvanced/Move to next line"),
585                                         "<control>N",
586                                         compose_advanced_action_cb,
587                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_NEXT_LINE,
588                                         NULL},
589         {N_("/_Edit/A_dvanced/Delete a character backward"),
590                                         "<control>H",
591                                         compose_advanced_action_cb,
592                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_CHARACTER,
593                                         NULL},
594         {N_("/_Edit/A_dvanced/Delete a character forward"),
595                                         "<control>D",
596                                         compose_advanced_action_cb,
597                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_CHARACTER,
598                                         NULL},
599         {N_("/_Edit/A_dvanced/Delete a word backward"),
600                                         NULL, /* "<control>W" */
601                                         compose_advanced_action_cb,
602                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_WORD,
603                                         NULL},
604         {N_("/_Edit/A_dvanced/Delete a word forward"),
605                                         NULL, /* "<alt>D", */
606                                         compose_advanced_action_cb,
607                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_WORD,
608                                         NULL},
609         {N_("/_Edit/A_dvanced/Delete line"),
610                                         "<control>U",
611                                         compose_advanced_action_cb,
612                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE,
613                                         NULL},
614         {N_("/_Edit/A_dvanced/Delete entire line"),
615                                         NULL,
616                                         compose_advanced_action_cb,
617                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE_N,
618                                         NULL},
619         {N_("/_Edit/A_dvanced/Delete to end of line"),
620                                         "<control>K",
621                                         compose_advanced_action_cb,
622                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_TO_LINE_END,
623                                         NULL},
624         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
625         {N_("/_Edit/_Find"),
626                                         "<control>F", compose_find_cb, 0, NULL},
627         {N_("/_Edit/---"),                      NULL, NULL, 0, "<Separator>"},
628         {N_("/_Edit/_Wrap current paragraph"),
629                                         "<control>L", compose_wrap_cb, 0, NULL},
630         {N_("/_Edit/Wrap all long _lines"),
631                                         "<control><alt>L", compose_wrap_cb, 1, NULL},
632         {N_("/_Edit/Aut_o wrapping"),   "<shift><control>L", compose_toggle_autowrap_cb, 0, "<ToggleItem>"},
633         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
634         {N_("/_Edit/Edit with e_xternal editor"),
635                                         "<shift><control>X", compose_ext_editor_cb, 0, NULL},
636 #if USE_ASPELL
637         {N_("/_Spelling"),              NULL, NULL, 0, "<Branch>"},
638         {N_("/_Spelling/_Check all or check selection"),
639                                         NULL, compose_check_all, 0, NULL},
640         {N_("/_Spelling/_Highlight all misspelled words"),
641                                         NULL, compose_highlight_all, 0, NULL},
642         {N_("/_Spelling/Check _backwards misspelled word"),
643                                         NULL, compose_check_backwards , 0, NULL},
644         {N_("/_Spelling/_Forward to next misspelled word"),
645                                         NULL, compose_check_forwards_go, 0, NULL},
646         {N_("/_Spelling/---"),          NULL, NULL, 0, "<Separator>"},
647         {N_("/_Spelling/Options"),
648                                         NULL, NULL, 0, "<Branch>"},
649 #endif
650         {N_("/_Options"),               NULL, NULL, 0, "<Branch>"},
651         {N_("/_Options/Privacy System"),                NULL, NULL,   0, "<Branch>"},
652         {N_("/_Options/Privacy System/None"),   NULL, NULL,   0, "<RadioItem>"},
653         {N_("/_Options/Si_gn"),         NULL, compose_toggle_sign_cb   , 0, "<ToggleItem>"},
654         {N_("/_Options/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
655         {N_("/_Options/---"),           NULL,           NULL,   0, "<Separator>"},
656         {N_("/_Options/_Priority"),     NULL,           NULL,   0, "<Branch>"},
657         {N_("/_Options/Priority/_Highest"), NULL, compose_set_priority_cb, PRIORITY_HIGHEST, "<RadioItem>"},
658         {N_("/_Options/Priority/Hi_gh"),    NULL, compose_set_priority_cb, PRIORITY_HIGH, "/Options/Priority/Highest"},
659         {N_("/_Options/Priority/_Normal"),  NULL, compose_set_priority_cb, PRIORITY_NORMAL, "/Options/Priority/Highest"},
660         {N_("/_Options/Priority/Lo_w"),    NULL, compose_set_priority_cb, PRIORITY_LOW, "/Options/Priority/Highest"},
661         {N_("/_Options/Priority/_Lowest"),  NULL, compose_set_priority_cb, PRIORITY_LOWEST, "/Options/Priority/Highest"},
662         {N_("/_Options/---"),           NULL,           NULL,   0, "<Separator>"},
663         {N_("/_Options/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
664         {N_("/_Options/---"),           NULL,           NULL,   0, "<Separator>"},
665         {N_("/_Options/Remo_ve references"),    NULL, compose_toggle_remove_refs_cb, 0, "<ToggleItem>"},
666         {N_("/_Options/---"),           NULL,           NULL,   0, "<Separator>"},
667
668 #define ENC_ACTION(action) \
669         NULL, compose_set_encoding_cb, action, \
670         "/Options/Character encoding/Automatic"
671
672         {N_("/_Options/Character _encoding"), NULL, NULL, 0, "<Branch>"},
673         {N_("/_Options/Character _encoding/_Automatic"),
674                         NULL, compose_set_encoding_cb, C_AUTO, "<RadioItem>"},
675         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
676
677         {N_("/_Options/Character _encoding/7bit ascii (US-ASC_II)"),
678          ENC_ACTION(C_US_ASCII)},
679         {N_("/_Options/Character _encoding/Unicode (_UTF-8)"),
680          ENC_ACTION(C_UTF_8)},
681         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
682
683         {N_("/_Options/Character _encoding/Western European (ISO-8859-_1)"),
684          ENC_ACTION(C_ISO_8859_1)},
685         {N_("/_Options/Character _encoding/Western European (ISO-8859-15)"),
686          ENC_ACTION(C_ISO_8859_15)},
687         {N_("/_Options/Character _encoding/Western European (Windows-1252)"),
688          ENC_ACTION(C_WINDOWS_1252)},
689         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
690
691         {N_("/_Options/Character _encoding/Central European (ISO-8859-_2)"),
692          ENC_ACTION(C_ISO_8859_2)},
693         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
694
695         {N_("/_Options/Character _encoding/_Baltic (ISO-8859-13)"),
696          ENC_ACTION(C_ISO_8859_13)},
697         {N_("/_Options/Character _encoding/Baltic (ISO-8859-_4)"),
698          ENC_ACTION(C_ISO_8859_4)},
699         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
700
701         {N_("/_Options/Character _encoding/Greek (ISO-8859-_7)"),
702          ENC_ACTION(C_ISO_8859_7)},
703         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
704
705         {N_("/_Options/Character _encoding/Hebrew (ISO-8859-_8)"),
706          ENC_ACTION(C_ISO_8859_8)},
707         {N_("/_Options/Character _encoding/Hebrew (Windows-1255)"),
708          ENC_ACTION(C_WINDOWS_1255)},
709         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
710
711         {N_("/_Options/Character _encoding/Arabic (ISO-8859-_6)"),
712          ENC_ACTION(C_ISO_8859_6)},
713         {N_("/_Options/Character _encoding/Arabic (Windows-1256)"),
714          ENC_ACTION(C_CP1256)},
715         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
716
717         {N_("/_Options/Character _encoding/Turkish (ISO-8859-_9)"),
718          ENC_ACTION(C_ISO_8859_9)},
719         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
720
721         {N_("/_Options/Character _encoding/Cyrillic (ISO-8859-_5)"),
722          ENC_ACTION(C_ISO_8859_5)},
723         {N_("/_Options/Character _encoding/Cyrillic (KOI8-_R)"),
724          ENC_ACTION(C_KOI8_R)},
725         {N_("/_Options/Character _encoding/Cyrillic (KOI8-U)"),
726          ENC_ACTION(C_KOI8_U)},
727         {N_("/_Options/Character _encoding/Cyrillic (Windows-1251)"),
728          ENC_ACTION(C_WINDOWS_1251)},
729         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
730
731         {N_("/_Options/Character _encoding/Japanese (ISO-2022-_JP)"),
732          ENC_ACTION(C_ISO_2022_JP)},
733         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
734
735         {N_("/_Options/Character _encoding/Simplified Chinese (_GB2312)"),
736          ENC_ACTION(C_GB2312)},
737         {N_("/_Options/Character _encoding/Simplified Chinese (GBK)"),
738          ENC_ACTION(C_GBK)},
739         {N_("/_Options/Character _encoding/Traditional Chinese (_Big5)"),
740          ENC_ACTION(C_BIG5)},
741         {N_("/_Options/Character _encoding/Traditional Chinese (EUC-_TW)"),
742          ENC_ACTION(C_EUC_TW)},
743         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
744
745         {N_("/_Options/Character _encoding/Korean (EUC-_KR)"),
746          ENC_ACTION(C_EUC_KR)},
747         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
748
749         {N_("/_Options/Character _encoding/Thai (TIS-620)"),
750          ENC_ACTION(C_TIS_620)},
751         {N_("/_Options/Character _encoding/Thai (Windows-874)"),
752          ENC_ACTION(C_WINDOWS_874)},
753
754         {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
755         {N_("/_Tools/Show _ruler"),     NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
756         {N_("/_Tools/_Address book"),   "<shift><control>A", compose_address_cb , 0, NULL},
757         {N_("/_Tools/_Template"),       NULL, NULL, 0, "<Branch>"},
758         {N_("/_Tools/Actio_ns"),        NULL, NULL, 0, "<Branch>"},
759         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
760         {N_("/_Help/_About"),           NULL, about_show, 0, NULL}
761 };
762
763 static GtkTargetEntry compose_mime_types[] =
764 {
765         {"text/uri-list", 0, 0},
766         {"text/plain", 0, 0},
767         {"STRING", 0, 0}
768 };
769
770 static gboolean compose_put_existing_to_front(MsgInfo *info)
771 {
772         GList *compose_list = compose_get_compose_list();
773         GList *elem = NULL;
774         
775         if (compose_list) {
776                 for (elem = compose_list; elem != NULL && elem->data != NULL; 
777                      elem = elem->next) {
778                         Compose *c = (Compose*)elem->data;
779
780                         if (!c->targetinfo || !c->targetinfo->msgid ||
781                             !info->msgid)
782                                 continue;
783
784                         if (!strcmp(c->targetinfo->msgid, info->msgid)) {
785                                 gtkut_window_popup(c->window);
786                                 return TRUE;
787                         }
788                 }
789         }
790         return FALSE;
791 }
792
793 static GdkColor quote_color1 = 
794         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
795 static GdkColor quote_color2 = 
796         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
797 static GdkColor quote_color3 = 
798         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
799
800 static GdkColor quote_bgcolor1 = 
801         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
802 static GdkColor quote_bgcolor2 = 
803         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
804 static GdkColor quote_bgcolor3 = 
805         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
806
807 static GdkColor signature_color = {
808         (gulong)0,
809         (gushort)0x7fff,
810         (gushort)0x7fff,
811         (gushort)0x7fff
812 };
813
814 static GdkColor uri_color = {
815         (gulong)0,
816         (gushort)0,
817         (gushort)0,
818         (gushort)0
819 };
820
821 static void compose_create_tags(GtkTextView *text, Compose *compose)
822 {
823         GtkTextBuffer *buffer;
824         GdkColor black = {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
825         GdkColormap *cmap;
826         GdkColor color[8];
827         gboolean success[8];
828         int i;
829
830         buffer = gtk_text_view_get_buffer(text);
831
832         if (prefs_common.enable_color) {
833                 /* grab the quote colors, converting from an int to a GdkColor */
834                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
835                                                &quote_color1);
836                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
837                                                &quote_color2);
838                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
839                                                &quote_color3);
840                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_bgcol,
841                                                &quote_bgcolor1);
842                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_bgcol,
843                                                &quote_bgcolor2);
844                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_bgcol,
845                                                &quote_bgcolor3);
846                 gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
847                                                &signature_color);
848                 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
849                                                &uri_color);
850         } else {
851                 signature_color = quote_color1 = quote_color2 = quote_color3 = 
852                         quote_bgcolor1 = quote_bgcolor2 = quote_bgcolor3 = uri_color = black;
853         }
854
855         if (prefs_common.enable_color && prefs_common.enable_bgcolor) {
856                 gtk_text_buffer_create_tag(buffer, "quote0",
857                                            "foreground-gdk", &quote_color1,
858                                            "paragraph-background-gdk", &quote_bgcolor1,
859                                            NULL);
860                 gtk_text_buffer_create_tag(buffer, "quote1",
861                                            "foreground-gdk", &quote_color2,
862                                            "paragraph-background-gdk", &quote_bgcolor2,
863                                            NULL);
864                 gtk_text_buffer_create_tag(buffer, "quote2",
865                                            "foreground-gdk", &quote_color3,
866                                            "paragraph-background-gdk", &quote_bgcolor3,
867                                            NULL);
868         } else {
869                 gtk_text_buffer_create_tag(buffer, "quote0",
870                                            "foreground-gdk", &quote_color1,
871                                            NULL);
872                 gtk_text_buffer_create_tag(buffer, "quote1",
873                                            "foreground-gdk", &quote_color2,
874                                            NULL);
875                 gtk_text_buffer_create_tag(buffer, "quote2",
876                                            "foreground-gdk", &quote_color3,
877                                            NULL);
878         }
879         
880         gtk_text_buffer_create_tag(buffer, "signature",
881                                    "foreground-gdk", &signature_color,
882                                    NULL);
883         gtk_text_buffer_create_tag(buffer, "link",
884                                         "foreground-gdk", &uri_color,
885                                          NULL);
886         compose->no_wrap_tag = gtk_text_buffer_create_tag(buffer, "no_wrap", NULL);
887         compose->no_join_tag = gtk_text_buffer_create_tag(buffer, "no_join", NULL);
888
889         color[0] = quote_color1;
890         color[1] = quote_color2;
891         color[2] = quote_color3;
892         color[3] = quote_bgcolor1;
893         color[4] = quote_bgcolor2;
894         color[5] = quote_bgcolor3;
895         color[6] = signature_color;
896         color[7] = uri_color;
897         cmap = gdk_drawable_get_colormap(compose->window->window);
898         gdk_colormap_alloc_colors(cmap, color, 8, FALSE, TRUE, success);
899
900         for (i = 0; i < 8; i++) {
901                 if (success[i] == FALSE) {
902                         GtkStyle *style;
903
904                         g_warning("Compose: color allocation failed.\n");
905                         style = gtk_widget_get_style(GTK_WIDGET(text));
906                         quote_color1 = quote_color2 = quote_color3 = 
907                                 quote_bgcolor1 = quote_bgcolor2 = quote_bgcolor3 = 
908                                 signature_color = uri_color = black;
909                 }
910         }
911 }
912
913 Compose *compose_new(PrefsAccount *account, const gchar *mailto,
914                      GPtrArray *attach_files)
915 {
916         return compose_generic_new(account, mailto, NULL, attach_files, NULL);
917 }
918
919 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
920 {
921         return compose_generic_new(account, NULL, item, NULL, NULL);
922 }
923
924 Compose *compose_new_with_list( PrefsAccount *account, GList *listAddress )
925 {
926         return compose_generic_new( account, NULL, NULL, NULL, listAddress );
927 }
928
929 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item,
930                              GPtrArray *attach_files, GList *listAddress )
931 {
932         Compose *compose;
933         GtkTextView *textview;
934         GtkTextBuffer *textbuf;
935         GtkTextIter iter;
936         GtkItemFactory *ifactory;
937
938         if (item && item->prefs && item->prefs->enable_default_account)
939                 account = account_find_from_id(item->prefs->default_account);
940
941         if (!account) account = cur_account;
942         g_return_val_if_fail(account != NULL, NULL);
943
944         compose = compose_create(account, COMPOSE_NEW, FALSE);
945
946         ifactory = gtk_item_factory_from_widget(compose->menubar);
947
948         compose->replyinfo = NULL;
949         compose->fwdinfo   = NULL;
950
951         textview = GTK_TEXT_VIEW(compose->text);
952         textbuf = gtk_text_view_get_buffer(textview);
953         compose_create_tags(textview, compose);
954
955         undo_block(compose->undostruct);
956 #ifdef USE_ASPELL
957         if (item && item->prefs && item->prefs->enable_default_dictionary &&
958             compose->gtkaspell) 
959                 gtkaspell_change_dict(compose->gtkaspell, 
960                     item->prefs->default_dictionary);
961 #endif
962
963         if (account->auto_sig)
964                 compose_insert_sig(compose, FALSE);
965         gtk_text_buffer_get_start_iter(textbuf, &iter);
966         gtk_text_buffer_place_cursor(textbuf, &iter);
967
968         if (account->protocol != A_NNTP) {
969                 if (mailto && *mailto != '\0') {
970                         compose_entries_set(compose, mailto);
971
972                 } else if (item && item->prefs->enable_default_to) {
973                         compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
974                         compose_entry_mark_default_to(compose, item->prefs->default_to);
975                 }
976                 if (item && item->ret_rcpt) {
977                         menu_set_active(ifactory, "/Options/Request Return Receipt", TRUE);
978                 }
979         } else {
980                 if (mailto) {
981                         compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
982                 } else if (item) {
983                         compose_entry_append(compose, item->path, COMPOSE_NEWSGROUPS);
984                 }
985                 /*
986                  * CLAWS: just don't allow return receipt request, even if the user
987                  * may want to send an email. simple but foolproof.
988                  */
989                 menu_set_sensitive(ifactory, "/Options/Request Return Receipt", FALSE); 
990         }
991         compose_add_field_list( compose, listAddress );
992
993         if (attach_files) {
994                 gint i;
995                 gchar *file;
996
997                 for (i = 0; i < attach_files->len; i++) {
998                         file = g_ptr_array_index(attach_files, i);
999                         compose_attach_append(compose, file, file, NULL);
1000                 }
1001         }
1002
1003         compose_show_first_last_header(compose, TRUE);
1004
1005         /* Set save folder */
1006         if (item && item->prefs && item->prefs->save_copy_to_folder) {
1007                 gchar *folderidentifier;
1008
1009                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
1010                 folderidentifier = folder_item_get_identifier(item);
1011                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1012                 g_free(folderidentifier);
1013         }
1014         
1015         gtk_widget_grab_focus(compose->header_last->entry);
1016
1017         undo_unblock(compose->undostruct);
1018
1019         if (prefs_common.auto_exteditor)
1020                 compose_exec_ext_editor(compose);
1021
1022         compose->modified = FALSE;
1023         compose_set_title(compose);
1024         return compose;
1025 }
1026
1027 static void compose_force_encryption(Compose *compose, PrefsAccount *account,
1028                 gboolean override_pref)
1029 {
1030         gchar *privacy = NULL;
1031
1032         g_return_if_fail(compose != NULL);
1033         g_return_if_fail(account != NULL);
1034
1035         if (override_pref == FALSE && account->default_encrypt_reply == FALSE)
1036                 return;
1037
1038         if (account->default_privacy_system
1039         &&  strlen(account->default_privacy_system)) {
1040                 privacy = account->default_privacy_system;
1041         } else {
1042                 GSList *privacy_avail = privacy_get_system_ids();
1043                 if (privacy_avail && g_slist_length(privacy_avail)) {
1044                         privacy = (gchar *)(privacy_avail->data);
1045                 }
1046         }
1047         if (privacy != NULL) {
1048                 compose->privacy_system = g_strdup(privacy);
1049                 compose_update_privacy_system_menu_item(compose, FALSE);
1050                 compose_use_encryption(compose, TRUE);
1051         }
1052 }       
1053
1054 static void compose_force_signing(Compose *compose, PrefsAccount *account)
1055 {
1056         gchar *privacy = NULL;
1057
1058         if (account->default_privacy_system
1059         &&  strlen(account->default_privacy_system)) {
1060                 privacy = account->default_privacy_system;
1061         } else {
1062                 GSList *privacy_avail = privacy_get_system_ids();
1063                 if (privacy_avail && g_slist_length(privacy_avail)) {
1064                         privacy = (gchar *)(privacy_avail->data);
1065                 }
1066         }
1067         if (privacy != NULL) {
1068                 compose->privacy_system = g_strdup(privacy);
1069                 compose_update_privacy_system_menu_item(compose, FALSE);
1070                 compose_use_signing(compose, TRUE);
1071         }
1072 }       
1073
1074 Compose *compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
1075 {
1076         MsgInfo *msginfo;
1077         guint list_len;
1078         Compose *compose = NULL;
1079         g_return_val_if_fail(msginfo_list != NULL, NULL);
1080
1081         msginfo = (MsgInfo*)g_slist_nth_data(msginfo_list, 0);
1082         g_return_val_if_fail(msginfo != NULL, NULL);
1083
1084         list_len = g_slist_length(msginfo_list);
1085
1086         switch (mode) {
1087         case COMPOSE_REPLY:
1088                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1089                               FALSE, prefs_common.default_reply_list, FALSE, body);
1090                 break;
1091         case COMPOSE_REPLY_WITH_QUOTE:
1092                 compose = compose_reply(msginfo, TRUE, 
1093                         FALSE, prefs_common.default_reply_list, FALSE, body);
1094                 break;
1095         case COMPOSE_REPLY_WITHOUT_QUOTE:
1096                 compose = compose_reply(msginfo, FALSE, 
1097                         FALSE, prefs_common.default_reply_list, FALSE, NULL);
1098                 break;
1099         case COMPOSE_REPLY_TO_SENDER:
1100                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1101                               FALSE, FALSE, TRUE, body);
1102                 break;
1103         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
1104                 compose = compose_followup_and_reply_to(msginfo,
1105                                               prefs_common.reply_with_quote,
1106                                               FALSE, FALSE, body);
1107                 break;
1108         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
1109                 compose = compose_reply(msginfo, TRUE, 
1110                         FALSE, FALSE, TRUE, body);
1111                 break;
1112         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
1113                 compose = compose_reply(msginfo, FALSE, 
1114                         FALSE, FALSE, TRUE, NULL);
1115                 break;
1116         case COMPOSE_REPLY_TO_ALL:
1117                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1118                         TRUE, FALSE, FALSE, body);
1119                 break;
1120         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
1121                 compose = compose_reply(msginfo, TRUE, 
1122                         TRUE, FALSE, FALSE, body);
1123                 break;
1124         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
1125                 compose = compose_reply(msginfo, FALSE, 
1126                         TRUE, FALSE, FALSE, NULL);
1127                 break;
1128         case COMPOSE_REPLY_TO_LIST:
1129                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1130                         FALSE, TRUE, FALSE, body);
1131                 break;
1132         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
1133                 compose = compose_reply(msginfo, TRUE, 
1134                         FALSE, TRUE, FALSE, body);
1135                 break;
1136         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
1137                 compose = compose_reply(msginfo, FALSE, 
1138                         FALSE, TRUE, FALSE, NULL);
1139                 break;
1140         case COMPOSE_FORWARD:
1141                 if (prefs_common.forward_as_attachment) {
1142                         compose = compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, msginfo_list, body);
1143                         return compose;
1144                 } else {
1145                         compose = compose_reply_mode(COMPOSE_FORWARD_INLINE, msginfo_list, body);
1146                         return compose;
1147                 }
1148                 break;
1149         case COMPOSE_FORWARD_INLINE:
1150                 /* check if we reply to more than one Message */
1151                 if (list_len == 1) {
1152                         compose = compose_forward(NULL, msginfo, FALSE, body, FALSE, FALSE);
1153                         break;
1154                 } 
1155                 /* more messages FALL THROUGH */
1156         case COMPOSE_FORWARD_AS_ATTACH:
1157                 compose = compose_forward_multiple(NULL, msginfo_list);
1158                 break;
1159         case COMPOSE_REDIRECT:
1160                 compose = compose_redirect(NULL, msginfo, FALSE);
1161                 break;
1162         default:
1163                 g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
1164         }
1165         return compose;
1166 }
1167
1168 Compose *compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
1169                    gboolean to_ml, gboolean to_sender, 
1170                    const gchar *body)
1171 {
1172         return compose_generic_reply(msginfo, quote, to_all, to_ml, 
1173                               to_sender, FALSE, body);
1174 }
1175
1176 Compose *compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
1177                                    gboolean to_all,
1178                                    gboolean to_sender,
1179                                    const gchar *body)
1180 {
1181         return compose_generic_reply(msginfo, quote, to_all, FALSE, 
1182                               to_sender, TRUE, body);
1183 }
1184
1185 static void compose_extract_original_charset(Compose *compose)
1186 {
1187         MsgInfo *info = NULL;
1188         if (compose->replyinfo) {
1189                 info = compose->replyinfo;
1190         } else if (compose->fwdinfo) {
1191                 info = compose->fwdinfo;
1192         } else if (compose->targetinfo) {
1193                 info = compose->targetinfo;
1194         }
1195         if (info) {
1196                 MimeInfo *mimeinfo = procmime_scan_message(info);
1197                 MimeInfo *partinfo = mimeinfo;
1198                 while (partinfo && partinfo->type != MIMETYPE_TEXT)
1199                         partinfo = procmime_mimeinfo_next(partinfo);
1200                 if (partinfo) {
1201                         compose->orig_charset = 
1202                                 g_strdup(procmime_mimeinfo_get_parameter(
1203                                                 partinfo, "charset"));
1204                 }
1205                 procmime_mimeinfo_free_all(mimeinfo);
1206         }
1207 }
1208
1209 #define SIGNAL_BLOCK(buffer) {                                  \
1210         g_signal_handlers_block_by_func(G_OBJECT(buffer),       \
1211                                 G_CALLBACK(compose_changed_cb), \
1212                                 compose);                       \
1213         g_signal_handlers_block_by_func(G_OBJECT(buffer),       \
1214                                 G_CALLBACK(text_inserted),      \
1215                                 compose);                       \
1216 }
1217
1218 #define SIGNAL_UNBLOCK(buffer) {                                \
1219         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),     \
1220                                 G_CALLBACK(compose_changed_cb), \
1221                                 compose);                       \
1222         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),     \
1223                                 G_CALLBACK(text_inserted),      \
1224                                 compose);                       \
1225 }
1226
1227 static Compose *compose_generic_reply(MsgInfo *msginfo, gboolean quote,
1228                                   gboolean to_all, gboolean to_ml,
1229                                   gboolean to_sender,
1230                                   gboolean followup_and_reply_to,
1231                                   const gchar *body)
1232 {
1233         GtkItemFactory *ifactory;
1234         Compose *compose;
1235         PrefsAccount *account = NULL;
1236         PrefsAccount *reply_account;
1237         GtkTextView *textview;
1238         GtkTextBuffer *textbuf;
1239
1240         g_return_val_if_fail(msginfo != NULL, NULL);
1241         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1242
1243         account = account_get_reply_account(msginfo, prefs_common.reply_account_autosel);
1244         
1245         g_return_val_if_fail(account != NULL, NULL);
1246
1247         if (to_sender && account->protocol == A_NNTP &&
1248             !followup_and_reply_to) {
1249                 reply_account =
1250                         account_find_from_address(account->address);
1251                 if (!reply_account)
1252                         reply_account = compose_current_mail_account();
1253                 if (!reply_account)
1254                         return NULL;
1255         } else
1256                 reply_account = account;
1257
1258         compose = compose_create(account, COMPOSE_REPLY, FALSE);
1259
1260         compose->updating = TRUE;
1261
1262         ifactory = gtk_item_factory_from_widget(compose->menubar);
1263
1264         menu_set_active(ifactory, "/Options/Remove references", FALSE);
1265         menu_set_sensitive(ifactory, "/Options/Remove references", TRUE);
1266
1267         compose->replyinfo = procmsg_msginfo_get_full_info(msginfo);
1268         if (!compose->replyinfo)
1269                 compose->replyinfo = procmsg_msginfo_copy(msginfo);
1270
1271         compose_extract_original_charset(compose);
1272         
1273         if (msginfo->folder && msginfo->folder->ret_rcpt)
1274                 menu_set_active(ifactory, "/Options/Request Return Receipt", TRUE);
1275
1276         /* Set save folder */
1277         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1278                 gchar *folderidentifier;
1279
1280                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1281                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1282                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1283                 g_free(folderidentifier);
1284         }
1285
1286         if (compose_parse_header(compose, msginfo) < 0) return NULL;
1287         compose_reply_set_entry(compose, msginfo, to_all, to_ml, 
1288                                 to_sender, followup_and_reply_to);
1289         compose_show_first_last_header(compose, TRUE);
1290
1291         textview = (GTK_TEXT_VIEW(compose->text));
1292         textbuf = gtk_text_view_get_buffer(textview);
1293         compose_create_tags(textview, compose);
1294
1295         undo_block(compose->undostruct);
1296 #ifdef USE_ASPELL
1297         if (msginfo->folder && msginfo->folder->prefs && 
1298             msginfo->folder->prefs && 
1299             msginfo->folder->prefs->enable_default_dictionary &&
1300             compose->gtkaspell)
1301                 gtkaspell_change_dict(compose->gtkaspell, 
1302                     msginfo->folder->prefs->default_dictionary);
1303 #endif
1304
1305         if (quote) {
1306                 gchar *qmark;
1307
1308                 if (prefs_common.quotemark && *prefs_common.quotemark)
1309                         qmark = prefs_common.quotemark;
1310                 else
1311                         qmark = "> ";
1312
1313                 compose_quote_fmt(compose, compose->replyinfo,
1314                                   prefs_common.quotefmt,
1315                                   qmark, body, FALSE);
1316         }
1317         if (procmime_msginfo_is_encrypted(compose->replyinfo)) {
1318                 compose_force_encryption(compose, account, FALSE);
1319         }
1320
1321         SIGNAL_BLOCK(textbuf);
1322         
1323         if (account->auto_sig)
1324                 compose_insert_sig(compose, FALSE);
1325
1326         compose_wrap_all(compose);
1327
1328         SIGNAL_UNBLOCK(textbuf);
1329         
1330         gtk_widget_grab_focus(compose->text);
1331
1332         undo_unblock(compose->undostruct);
1333
1334         if (prefs_common.auto_exteditor)
1335                 compose_exec_ext_editor(compose);
1336                 
1337         compose->modified = FALSE;
1338         compose_set_title(compose);
1339
1340         compose->updating = FALSE;
1341
1342         if (compose->deferred_destroy) {
1343                 compose_destroy(compose);
1344                 return NULL;
1345         }
1346
1347         return compose;
1348 }
1349
1350 #define INSERT_FW_HEADER(var, hdr) \
1351 if (msginfo->var && *msginfo->var) { \
1352         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
1353         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
1354         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
1355 }
1356
1357 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
1358                          gboolean as_attach, const gchar *body,
1359                          gboolean no_extedit,
1360                          gboolean batch)
1361 {
1362         Compose *compose;
1363         GtkTextView *textview;
1364         GtkTextBuffer *textbuf;
1365         GtkTextIter iter;
1366
1367         g_return_val_if_fail(msginfo != NULL, NULL);
1368         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1369
1370         if (!account && 
1371             !(account = compose_guess_forward_account_from_msginfo
1372                                 (msginfo)))
1373                 account = cur_account;
1374
1375         compose = compose_create(account, COMPOSE_FORWARD, batch);
1376
1377         compose->updating = TRUE;
1378         compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
1379         if (!compose->fwdinfo)
1380                 compose->fwdinfo = procmsg_msginfo_copy(msginfo);
1381
1382         compose_extract_original_charset(compose);
1383
1384         if (msginfo->subject && *msginfo->subject) {
1385                 gchar *buf, *buf2, *p;
1386
1387                 buf = p = g_strdup(msginfo->subject);
1388                 p += subject_get_prefix_length(p);
1389                 memmove(buf, p, strlen(p) + 1);
1390
1391                 buf2 = g_strdup_printf("Fw: %s", buf);
1392                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1393                 
1394                 g_free(buf);
1395                 g_free(buf2);
1396         }
1397
1398         textview = GTK_TEXT_VIEW(compose->text);
1399         textbuf = gtk_text_view_get_buffer(textview);
1400         compose_create_tags(textview, compose);
1401         
1402         undo_block(compose->undostruct);
1403         if (as_attach) {
1404                 gchar *msgfile;
1405
1406                 msgfile = procmsg_get_message_file_path(msginfo);
1407                 if (!is_file_exist(msgfile))
1408                         g_warning("%s: file not exist\n", msgfile);
1409                 else
1410                         compose_attach_append(compose, msgfile, msgfile,
1411                                               "message/rfc822");
1412
1413                 g_free(msgfile);
1414         } else {
1415                 gchar *qmark;
1416                 MsgInfo *full_msginfo;
1417
1418                 full_msginfo = procmsg_msginfo_get_full_info(msginfo);
1419                 if (!full_msginfo)
1420                         full_msginfo = procmsg_msginfo_copy(msginfo);
1421
1422                 if (prefs_common.fw_quotemark &&
1423                     *prefs_common.fw_quotemark)
1424                         qmark = prefs_common.fw_quotemark;
1425                 else
1426                         qmark = "> ";
1427
1428                 compose_quote_fmt(compose, full_msginfo,
1429                                   prefs_common.fw_quotefmt,
1430                                   qmark, body, FALSE);
1431                 compose_attach_parts(compose, msginfo);
1432
1433                 procmsg_msginfo_free(full_msginfo);
1434         }
1435
1436         SIGNAL_BLOCK(textbuf);
1437
1438         if (account->auto_sig)
1439                 compose_insert_sig(compose, FALSE);
1440
1441         compose_wrap_all(compose);
1442
1443         SIGNAL_UNBLOCK(textbuf);
1444         
1445         gtk_text_buffer_get_start_iter(textbuf, &iter);
1446         gtk_text_buffer_place_cursor(textbuf, &iter);
1447
1448         gtk_widget_grab_focus(compose->header_last->entry);
1449
1450         if (!no_extedit && prefs_common.auto_exteditor)
1451                 compose_exec_ext_editor(compose);
1452         
1453         /*save folder*/
1454         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1455                 gchar *folderidentifier;
1456
1457                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1458                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1459                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1460                 g_free(folderidentifier);
1461         }
1462
1463         undo_unblock(compose->undostruct);
1464         
1465         compose->modified = FALSE;
1466         compose_set_title(compose);
1467
1468         compose->updating = FALSE;
1469
1470         if (compose->deferred_destroy) {
1471                 compose_destroy(compose);
1472                 return NULL;
1473         }
1474
1475         return compose;
1476 }
1477
1478 #undef INSERT_FW_HEADER
1479
1480 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1481 {
1482         Compose *compose;
1483         GtkTextView *textview;
1484         GtkTextBuffer *textbuf;
1485         GtkTextIter iter;
1486         GSList *msginfo;
1487         gchar *msgfile;
1488         gboolean single_mail = TRUE;
1489         
1490         g_return_val_if_fail(msginfo_list != NULL, NULL);
1491
1492         if (g_slist_length(msginfo_list) > 1)
1493                 single_mail = FALSE;
1494
1495         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next)
1496                 if (((MsgInfo *)msginfo->data)->folder == NULL)
1497                         return NULL;
1498
1499         /* guess account from first selected message */
1500         if (!account && 
1501             !(account = compose_guess_forward_account_from_msginfo
1502                                 (msginfo_list->data)))
1503                 account = cur_account;
1504
1505         g_return_val_if_fail(account != NULL, NULL);
1506
1507         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1508                 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1509                 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1510         }
1511
1512         compose = compose_create(account, COMPOSE_FORWARD, FALSE);
1513
1514         compose->updating = TRUE;
1515
1516         textview = GTK_TEXT_VIEW(compose->text);
1517         textbuf = gtk_text_view_get_buffer(textview);
1518         compose_create_tags(textview, compose);
1519         
1520         undo_block(compose->undostruct);
1521         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1522                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1523
1524                 if (!is_file_exist(msgfile))
1525                         g_warning("%s: file not exist\n", msgfile);
1526                 else
1527                         compose_attach_append(compose, msgfile, msgfile,
1528                                 "message/rfc822");
1529                 g_free(msgfile);
1530         }
1531         
1532         if (single_mail) {
1533                 MsgInfo *info = (MsgInfo *)msginfo_list->data;
1534                 if (info->subject && *info->subject) {
1535                         gchar *buf, *buf2, *p;
1536
1537                         buf = p = g_strdup(info->subject);
1538                         p += subject_get_prefix_length(p);
1539                         memmove(buf, p, strlen(p) + 1);
1540
1541                         buf2 = g_strdup_printf("Fw: %s", buf);
1542                         gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1543
1544                         g_free(buf);
1545                         g_free(buf2);
1546                 }
1547         } else {
1548                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
1549                         _("Fw: multiple emails"));
1550         }
1551
1552         SIGNAL_BLOCK(textbuf);
1553         
1554         if (account->auto_sig)
1555                 compose_insert_sig(compose, FALSE);
1556
1557         compose_wrap_all(compose);
1558
1559         SIGNAL_UNBLOCK(textbuf);
1560         
1561         gtk_text_buffer_get_start_iter(textbuf, &iter);
1562         gtk_text_buffer_place_cursor(textbuf, &iter);
1563
1564         gtk_widget_grab_focus(compose->header_last->entry);
1565         undo_unblock(compose->undostruct);
1566         compose->modified = FALSE;
1567         compose_set_title(compose);
1568
1569         compose->updating = FALSE;
1570
1571         if (compose->deferred_destroy) {
1572                 compose_destroy(compose);
1573                 return NULL;
1574         }
1575
1576         return compose;
1577 }
1578
1579 static gboolean compose_is_sig_separator(Compose *compose, GtkTextBuffer *textbuf, GtkTextIter *iter) 
1580 {
1581         GtkTextIter start = *iter;
1582         GtkTextIter end_iter;
1583         int start_pos = gtk_text_iter_get_offset(&start);
1584         gchar *str = NULL;
1585         if (!compose->account->sig_sep)
1586                 return FALSE;
1587         
1588         gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
1589                 start_pos+strlen(compose->account->sig_sep));
1590
1591         /* check sig separator */
1592         str = gtk_text_iter_get_text(&start, &end_iter);
1593         if (!strcmp(str, compose->account->sig_sep)) {
1594                 gchar *tmp = NULL;
1595                 /* check end of line (\n) */
1596                 gtk_text_buffer_get_iter_at_offset(textbuf, &start,
1597                         start_pos+strlen(compose->account->sig_sep));
1598                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
1599                         start_pos+strlen(compose->account->sig_sep)+1);
1600                 tmp = gtk_text_iter_get_text(&start, &end_iter);
1601                 if (!strcmp(tmp,"\n")) {
1602                         g_free(str);
1603                         g_free(tmp);
1604                         return TRUE;
1605                 }
1606                 g_free(tmp);    
1607         }
1608         g_free(str);
1609
1610         return FALSE;
1611 }
1612
1613 static void compose_colorize_signature(Compose *compose)
1614 {
1615         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(compose->text));
1616         GtkTextIter iter;
1617         GtkTextIter end_iter;
1618         gtk_text_buffer_get_start_iter(buffer, &iter);
1619         while (gtk_text_iter_forward_line(&iter))
1620                 if (compose_is_sig_separator(compose, buffer, &iter)) {
1621                         gtk_text_buffer_get_end_iter(buffer, &end_iter);
1622                         gtk_text_buffer_apply_tag_by_name(buffer,"signature",&iter, &end_iter);
1623                 }
1624 }
1625
1626 Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
1627 {
1628         Compose *compose = NULL;
1629         PrefsAccount *account = NULL;
1630         GtkTextView *textview;
1631         GtkTextBuffer *textbuf;
1632         GtkTextMark *mark;
1633         GtkTextIter iter;
1634         FILE *fp;
1635         gchar buf[BUFFSIZE];
1636         gboolean use_signing = FALSE;
1637         gboolean use_encryption = FALSE;
1638         gchar *privacy_system = NULL;
1639
1640         g_return_val_if_fail(msginfo != NULL, NULL);
1641         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1642
1643         if (compose_put_existing_to_front(msginfo)) {
1644                 return NULL;
1645         }
1646
1647         if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
1648             folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
1649                 gchar queueheader_buf[BUFFSIZE];
1650                 gint id, param;
1651
1652                 /* Select Account from queue headers */
1653                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1654                                              sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
1655                         id = atoi(&queueheader_buf[strlen("X-Sylpheed-Account-Id:")]);
1656                         account = account_find_from_id(id);
1657                 }
1658                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1659                                              sizeof(queueheader_buf), "NAID:")) {
1660                         id = atoi(&queueheader_buf[strlen("NAID:")]);
1661                         account = account_find_from_id(id);
1662                 }
1663                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1664                                                     sizeof(queueheader_buf), "MAID:")) {
1665                         id = atoi(&queueheader_buf[strlen("MAID:")]);
1666                         account = account_find_from_id(id);
1667                 }
1668                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1669                                                                 sizeof(queueheader_buf), "S:")) {
1670                         account = account_find_from_address(queueheader_buf);
1671                 }
1672                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1673                                              sizeof(queueheader_buf), "X-Sylpheed-Sign:")) {
1674                         param = atoi(&queueheader_buf[strlen("X-Sylpheed-Sign:")]);
1675                         use_signing = param;
1676                         
1677                 }
1678                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1679                                              sizeof(queueheader_buf), "X-Sylpheed-Encrypt:")) {
1680                         param = atoi(&queueheader_buf[strlen("X-Sylpheed-Encrypt:")]);
1681                         use_encryption = param;
1682                 }
1683                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1684                                             sizeof(queueheader_buf), "X-Sylpheed-Privacy-System:")) {
1685                         privacy_system = g_strdup(&queueheader_buf[strlen("X-Sylpheed-Privacy-System:")]);
1686                 }
1687                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1688                                              sizeof(queueheader_buf), "X-Priority: ")) {
1689                         param = atoi(&queueheader_buf[strlen("X-Priority: ")]); /* mind the space */
1690                         compose->priority = param;
1691                 }
1692         } else {
1693                 account = msginfo->folder->folder->account;
1694         }
1695
1696         if (!account && prefs_common.reedit_account_autosel) {
1697                 gchar from[BUFFSIZE];
1698                 if (!procheader_get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")) {
1699                         extract_address(from);
1700                         account = account_find_from_address(from);
1701                 }
1702         }
1703         if (!account) {
1704                 account = cur_account;
1705         }
1706         g_return_val_if_fail(account != NULL, NULL);
1707
1708         compose = compose_create(account, COMPOSE_REEDIT, batch);
1709         
1710         compose->updating = TRUE;
1711         
1712         if (privacy_system != NULL) {
1713                 compose->privacy_system = privacy_system;
1714                 compose_use_signing(compose, use_signing);
1715                 compose_use_encryption(compose, use_encryption);
1716                 compose_update_privacy_system_menu_item(compose, FALSE);
1717         } else {
1718                 activate_privacy_system(compose, account, FALSE);
1719         }
1720
1721         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1722
1723         compose_extract_original_charset(compose);
1724
1725         if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
1726             folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
1727                 gchar queueheader_buf[BUFFSIZE];
1728
1729                 /* Set message save folder */
1730                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1731                         gint startpos = 0;
1732
1733                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1734                         gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1735                         gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1736                 }
1737                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "RRCPT:")) {
1738                         gint active = atoi(&queueheader_buf[strlen("RRCPT:")]);
1739                         if (active) {
1740                                 GtkItemFactory *ifactory;
1741                                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1742                                 menu_set_active(ifactory, "/Options/Request Return Receipt", TRUE);
1743                         }
1744                 }
1745         }
1746         
1747         if (compose_parse_header(compose, msginfo) < 0) {
1748                 compose->updating = FALSE;
1749                 compose_destroy(compose);
1750                 return NULL;
1751         }
1752         compose_reedit_set_entry(compose, msginfo);
1753
1754         textview = GTK_TEXT_VIEW(compose->text);
1755         textbuf = gtk_text_view_get_buffer(textview);
1756         compose_create_tags(textview, compose);
1757
1758         mark = gtk_text_buffer_get_insert(textbuf);
1759         gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1760
1761         g_signal_handlers_block_by_func(G_OBJECT(textbuf),
1762                                         G_CALLBACK(compose_changed_cb),
1763                                         compose);
1764         
1765         if (procmime_msginfo_is_encrypted(msginfo)) {
1766                 fp = procmime_get_first_encrypted_text_content(msginfo);
1767                 if (fp) {
1768                         compose_force_encryption(compose, account, TRUE);
1769                 }
1770         } else {
1771                 fp = procmime_get_first_text_content(msginfo);
1772         }
1773         if (fp == NULL) {
1774                 g_warning("Can't get text part\n");
1775         }
1776
1777         if (fp != NULL) {
1778                 gboolean prev_autowrap = compose->autowrap;
1779
1780                 compose->autowrap = FALSE;
1781                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1782                         strcrchomp(buf);
1783                         gtk_text_buffer_insert(textbuf, &iter, buf, -1);
1784                 }
1785                 compose_wrap_all_full(compose, FALSE);
1786                 compose->autowrap = prev_autowrap;
1787                 fclose(fp);
1788         }
1789         
1790         compose_attach_parts(compose, msginfo);
1791
1792         compose_colorize_signature(compose);
1793
1794         g_signal_handlers_unblock_by_func(G_OBJECT(textbuf),
1795                                         G_CALLBACK(compose_changed_cb),
1796                                         compose);
1797
1798         gtk_widget_grab_focus(compose->text);
1799
1800         if (prefs_common.auto_exteditor) {
1801                 compose_exec_ext_editor(compose);
1802         }
1803         compose->modified = FALSE;
1804         compose_set_title(compose);
1805
1806         compose->updating = FALSE;
1807
1808         if (compose->deferred_destroy) {
1809                 compose_destroy(compose);
1810                 return NULL;
1811         }
1812         return compose;
1813 }
1814
1815 Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
1816                                                  gboolean batch)
1817 {
1818         Compose *compose;
1819         gchar *filename;
1820         GtkItemFactory *ifactory;
1821         FolderItem *item;
1822
1823         g_return_val_if_fail(msginfo != NULL, NULL);
1824
1825         if (!account)
1826                 account = account_get_reply_account(msginfo,
1827                                         prefs_common.reply_account_autosel);
1828         g_return_val_if_fail(account != NULL, NULL);
1829
1830         compose = compose_create(account, COMPOSE_REDIRECT, batch);
1831
1832         compose->updating = TRUE;
1833
1834         ifactory = gtk_item_factory_from_widget(compose->menubar);
1835         compose_create_tags(GTK_TEXT_VIEW(compose->text), compose);
1836         compose->replyinfo = NULL;
1837         compose->fwdinfo = NULL;
1838
1839         compose_show_first_last_header(compose, TRUE);
1840
1841         gtk_widget_grab_focus(compose->header_last->entry);
1842
1843         filename = procmsg_get_message_file_path(msginfo);
1844
1845         if (filename == NULL) {
1846                 compose->updating = FALSE;
1847                 compose_destroy(compose);
1848
1849                 return NULL;
1850         }
1851
1852         compose->redirect_filename = filename;
1853         
1854         /* Set save folder */
1855         item = msginfo->folder;
1856         if (item && item->prefs && item->prefs->save_copy_to_folder) {
1857                 gchar *folderidentifier;
1858
1859                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
1860                 folderidentifier = folder_item_get_identifier(item);
1861                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1862                 g_free(folderidentifier);
1863         }
1864
1865         compose_attach_parts(compose, msginfo);
1866
1867         if (msginfo->subject)
1868                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
1869                                    msginfo->subject);
1870         gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
1871
1872         compose_quote_fmt(compose, msginfo, "%M", NULL, NULL, FALSE);
1873         gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), FALSE);
1874
1875         compose_colorize_signature(compose);
1876
1877         ifactory = gtk_item_factory_from_widget(compose->popupmenu);
1878         menu_set_sensitive(ifactory, "/Add...", FALSE);
1879         menu_set_sensitive(ifactory, "/Remove", FALSE);
1880         menu_set_sensitive(ifactory, "/Properties...", FALSE);
1881
1882         ifactory = gtk_item_factory_from_widget(compose->menubar);
1883         menu_set_sensitive(ifactory, "/Message/Save", FALSE);
1884         menu_set_sensitive(ifactory, "/Message/Insert file", FALSE);
1885         menu_set_sensitive(ifactory, "/Message/Attach file", FALSE);
1886         menu_set_sensitive(ifactory, "/Message/Insert signature", FALSE);
1887         menu_set_sensitive(ifactory, "/Edit", FALSE);
1888         menu_set_sensitive(ifactory, "/Options", FALSE);
1889         menu_set_sensitive(ifactory, "/Tools/Show ruler", FALSE);
1890         menu_set_sensitive(ifactory, "/Tools/Actions", FALSE);
1891         
1892         if (compose->toolbar->draft_btn)
1893                 gtk_widget_set_sensitive(compose->toolbar->draft_btn, FALSE);
1894         if (compose->toolbar->insert_btn)
1895                 gtk_widget_set_sensitive(compose->toolbar->insert_btn, FALSE);
1896         if (compose->toolbar->attach_btn)
1897                 gtk_widget_set_sensitive(compose->toolbar->attach_btn, FALSE);
1898         if (compose->toolbar->sig_btn)
1899                 gtk_widget_set_sensitive(compose->toolbar->sig_btn, FALSE);
1900         if (compose->toolbar->exteditor_btn)
1901                 gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, FALSE);
1902         if (compose->toolbar->linewrap_current_btn)
1903                 gtk_widget_set_sensitive(compose->toolbar->linewrap_current_btn, FALSE);
1904         if (compose->toolbar->linewrap_all_btn)
1905                 gtk_widget_set_sensitive(compose->toolbar->linewrap_all_btn, FALSE);
1906
1907         compose->modified = FALSE;
1908         compose_set_title(compose);
1909         compose->updating = FALSE;
1910
1911         if (compose->deferred_destroy) {
1912                 compose_destroy(compose);
1913                 return NULL;
1914         }
1915         
1916         return compose;
1917 }
1918
1919 GList *compose_get_compose_list(void)
1920 {
1921         return compose_list;
1922 }
1923
1924 void compose_entry_append(Compose *compose, const gchar *address,
1925                           ComposeEntryType type)
1926 {
1927         gchar *header;
1928         gchar *cur, *begin;
1929         gboolean in_quote = FALSE;
1930         if (!address || *address == '\0') return;
1931
1932         switch (type) {
1933         case COMPOSE_CC:
1934                 header = N_("Cc:");
1935                 break;
1936         case COMPOSE_BCC:
1937                 header = N_("Bcc:");
1938                 break;
1939         case COMPOSE_REPLYTO:
1940                 header = N_("Reply-To:");
1941                 break;
1942         case COMPOSE_NEWSGROUPS:
1943                 header = N_("Newsgroups:");
1944                 break;
1945         case COMPOSE_FOLLOWUPTO:
1946                 header = N_( "Followup-To:");
1947                 break;
1948         case COMPOSE_TO:
1949         default:
1950                 header = N_("To:");
1951                 break;
1952         }
1953         header = prefs_common.trans_hdr ? gettext(header) : header;
1954         
1955         cur = begin = (gchar *)address;
1956         
1957         /* we separate the line by commas, but not if we're inside a quoted
1958          * string */
1959         while (*cur != '\0') {
1960                 if (*cur == '"') 
1961                         in_quote = !in_quote;
1962                 if (*cur == ',' && !in_quote) {
1963                         gchar *tmp = g_strdup(begin);
1964                         gchar *o_tmp = tmp;
1965                         tmp[cur-begin]='\0';
1966                         cur++;
1967                         begin = cur;
1968                         while (*tmp == ' ' || *tmp == '\t')
1969                                 tmp++;
1970                         compose_add_header_entry(compose, header, tmp);
1971                         g_free(o_tmp);
1972                         continue;
1973                 }
1974                 cur++;
1975         }
1976         if (begin < cur) {
1977                 gchar *tmp = g_strdup(begin);
1978                 gchar *o_tmp = tmp;
1979                 tmp[cur-begin]='\0';
1980                 cur++;
1981                 begin = cur;
1982                 while (*tmp == ' ' || *tmp == '\t')
1983                         tmp++;
1984                 compose_add_header_entry(compose, header, tmp);
1985                 g_free(o_tmp);          
1986         }
1987 }
1988
1989 void compose_entry_mark_default_to(Compose *compose, const gchar *mailto)
1990 {
1991         static GdkColor yellow;
1992         static GdkColor black;
1993         static gboolean yellow_initialised = FALSE;
1994         GSList *h_list;
1995         GtkEntry *entry;
1996                 
1997         if (!yellow_initialised) {
1998                 gdk_color_parse("#f5f6be", &yellow);
1999                 gdk_color_parse("#000000", &black);
2000                 yellow_initialised = gdk_colormap_alloc_color(
2001                         gdk_colormap_get_system(), &yellow, FALSE, TRUE);
2002                 yellow_initialised &= gdk_colormap_alloc_color(
2003                         gdk_colormap_get_system(), &black, FALSE, TRUE);
2004         }
2005
2006         for (h_list = compose->header_list; h_list != NULL; h_list = h_list->next) {
2007                 entry = GTK_ENTRY(((ComposeHeaderEntry *)h_list->data)->entry);
2008                 if (gtk_entry_get_text(entry) && 
2009                     !g_utf8_collate(gtk_entry_get_text(entry), mailto)) {
2010                         if (yellow_initialised) {
2011                                 gtk_widget_modify_base(
2012                                         GTK_WIDGET(((ComposeHeaderEntry *)h_list->data)->entry),
2013                                         GTK_STATE_NORMAL, &yellow);
2014                                 gtk_widget_modify_text(
2015                                         GTK_WIDGET(((ComposeHeaderEntry *)h_list->data)->entry),
2016                                         GTK_STATE_NORMAL, &black);
2017                         }
2018                 }
2019         }
2020 }
2021
2022 void compose_toolbar_cb(gint action, gpointer data)
2023 {
2024         ToolbarItem *toolbar_item = (ToolbarItem*)data;
2025         Compose *compose = (Compose*)toolbar_item->parent;
2026         
2027         g_return_if_fail(compose != NULL);
2028
2029         switch(action) {
2030         case A_SEND:
2031                 compose_send_cb(compose, 0, NULL);
2032                 break;
2033         case A_SENDL:
2034                 compose_send_later_cb(compose, 0, NULL);
2035                 break;
2036         case A_DRAFT:
2037                 compose_draft_cb(compose, COMPOSE_QUIT_EDITING, NULL);
2038                 break;
2039         case A_INSERT:
2040                 compose_insert_file_cb(compose, 0, NULL);
2041                 break;
2042         case A_ATTACH:
2043                 compose_attach_cb(compose, 0, NULL);
2044                 break;
2045         case A_SIG:
2046                 compose_insert_sig(compose, FALSE);
2047                 break;
2048         case A_EXTEDITOR:
2049                 compose_ext_editor_cb(compose, 0, NULL);
2050                 break;
2051         case A_LINEWRAP_CURRENT:
2052                 compose_beautify_paragraph(compose, NULL, TRUE);
2053                 break;
2054         case A_LINEWRAP_ALL:
2055                 compose_wrap_all_full(compose, TRUE);
2056                 break;
2057         case A_ADDRBOOK:
2058                 compose_address_cb(compose, 0, NULL);
2059                 break;
2060 #ifdef USE_ASPELL
2061         case A_CHECK_SPELLING:
2062                 compose_check_all(compose);
2063                 break;
2064 #endif
2065         default:
2066                 break;
2067         }
2068 }
2069
2070 static void compose_entries_set(Compose *compose, const gchar *mailto)
2071 {
2072         gchar *to = NULL;
2073         gchar *cc = NULL;
2074         gchar *subject = NULL;
2075         gchar *body = NULL;
2076         gchar *temp = NULL;
2077         gint  len = 0;
2078
2079         scan_mailto_url(mailto, &to, &cc, NULL, &subject, &body);
2080
2081         if (to)
2082                 compose_entry_append(compose, to, COMPOSE_TO);
2083         if (cc)
2084                 compose_entry_append(compose, cc, COMPOSE_CC);
2085         if (subject) {
2086                 if (!g_utf8_validate (subject, -1, NULL)) {
2087                         temp = g_locale_to_utf8 (subject, -1, NULL, &len, NULL);
2088                         gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), temp);
2089                         g_free(temp);
2090                 } else {
2091                         gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
2092                 }
2093         }
2094         if (body) {
2095                 GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2096                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2097                 GtkTextMark *mark;
2098                 GtkTextIter iter;
2099                 gboolean prev_autowrap = compose->autowrap;
2100
2101                 compose->autowrap = FALSE;
2102
2103                 mark = gtk_text_buffer_get_insert(buffer);
2104                 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2105
2106                 if (!g_utf8_validate (body, -1, NULL)) {
2107                         temp = g_locale_to_utf8 (body, -1, NULL, &len, NULL);
2108                         gtk_text_buffer_insert(buffer, &iter, temp, -1);
2109                         g_free(temp);
2110                 } else {
2111                         gtk_text_buffer_insert(buffer, &iter, body, -1);
2112                 }
2113                 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
2114
2115                 compose->autowrap = prev_autowrap;
2116                 if (compose->autowrap)
2117                         compose_wrap_all(compose);
2118         }
2119
2120         g_free(to);
2121         g_free(cc);
2122         g_free(subject);
2123         g_free(body);
2124 }
2125
2126 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
2127 {
2128         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
2129                                        {"Cc:",          NULL, TRUE},
2130                                        {"References:",  NULL, FALSE},
2131                                        {"Bcc:",         NULL, TRUE},
2132                                        {"Newsgroups:",  NULL, TRUE},
2133                                        {"Followup-To:", NULL, TRUE},
2134                                        {"List-Post:",   NULL, FALSE},
2135                                        {"X-Priority:",  NULL, FALSE},
2136                                        {NULL,           NULL, FALSE}};
2137
2138         enum
2139         {
2140                 H_REPLY_TO      = 0,
2141                 H_CC            = 1,
2142                 H_REFERENCES    = 2,
2143                 H_BCC           = 3,
2144                 H_NEWSGROUPS    = 4,
2145                 H_FOLLOWUP_TO   = 5,
2146                 H_LIST_POST     = 6,
2147                 H_X_PRIORITY    = 7
2148         };
2149
2150         FILE *fp;
2151
2152         g_return_val_if_fail(msginfo != NULL, -1);
2153
2154         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
2155         procheader_get_header_fields(fp, hentry);
2156         fclose(fp);
2157
2158         if (hentry[H_REPLY_TO].body != NULL) {
2159                 if (hentry[H_REPLY_TO].body[0] != '\0') {
2160                         compose->replyto =
2161                                 conv_unmime_header(hentry[H_REPLY_TO].body,
2162                                                    NULL);
2163                 }
2164                 g_free(hentry[H_REPLY_TO].body);
2165                 hentry[H_REPLY_TO].body = NULL;
2166         }
2167         if (hentry[H_CC].body != NULL) {
2168                 compose->cc = conv_unmime_header(hentry[H_CC].body, NULL);
2169                 g_free(hentry[H_CC].body);
2170                 hentry[H_CC].body = NULL;
2171         }
2172         if (hentry[H_REFERENCES].body != NULL) {
2173                 if (compose->mode == COMPOSE_REEDIT)
2174                         compose->references = hentry[H_REFERENCES].body;
2175                 else {
2176                         compose->references = compose_parse_references
2177                                 (hentry[H_REFERENCES].body, msginfo->msgid);
2178                         g_free(hentry[H_REFERENCES].body);
2179                 }
2180                 hentry[H_REFERENCES].body = NULL;
2181         }
2182         if (hentry[H_BCC].body != NULL) {
2183                 if (compose->mode == COMPOSE_REEDIT)
2184                         compose->bcc =
2185                                 conv_unmime_header(hentry[H_BCC].body, NULL);
2186                 g_free(hentry[H_BCC].body);
2187                 hentry[H_BCC].body = NULL;
2188         }
2189         if (hentry[H_NEWSGROUPS].body != NULL) {
2190                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
2191                 hentry[H_NEWSGROUPS].body = NULL;
2192         }
2193         if (hentry[H_FOLLOWUP_TO].body != NULL) {
2194                 if (hentry[H_FOLLOWUP_TO].body[0] != '\0') {
2195                         compose->followup_to =
2196                                 conv_unmime_header(hentry[H_FOLLOWUP_TO].body,
2197                                                    NULL);
2198                 }
2199                 g_free(hentry[H_FOLLOWUP_TO].body);
2200                 hentry[H_FOLLOWUP_TO].body = NULL;
2201         }
2202         if (hentry[H_LIST_POST].body != NULL) {
2203                 gchar *to = NULL;
2204
2205                 extract_address(hentry[H_LIST_POST].body);
2206                 if (hentry[H_LIST_POST].body[0] != '\0') {
2207                         scan_mailto_url(hentry[H_LIST_POST].body,
2208                                         &to, NULL, NULL, NULL, NULL);
2209                         if (to) {
2210                                 g_free(compose->ml_post);
2211                                 compose->ml_post = to;
2212                         }
2213                 }
2214                 g_free(hentry[H_LIST_POST].body);
2215                 hentry[H_LIST_POST].body = NULL;
2216         }
2217
2218         /* CLAWS - X-Priority */
2219         if (compose->mode == COMPOSE_REEDIT)
2220                 if (hentry[H_X_PRIORITY].body != NULL) {
2221                         gint priority;
2222                         
2223                         priority = atoi(hentry[H_X_PRIORITY].body);
2224                         g_free(hentry[H_X_PRIORITY].body);
2225                         
2226                         hentry[H_X_PRIORITY].body = NULL;
2227                         
2228                         if (priority < PRIORITY_HIGHEST || 
2229                             priority > PRIORITY_LOWEST)
2230                                 priority = PRIORITY_NORMAL;
2231                         
2232                         compose->priority =  priority;
2233                 }
2234  
2235         if (compose->mode == COMPOSE_REEDIT) {
2236                 if (msginfo->inreplyto && *msginfo->inreplyto)
2237                         compose->inreplyto = g_strdup(msginfo->inreplyto);
2238                 return 0;
2239         }
2240
2241         if (msginfo->msgid && *msginfo->msgid)
2242                 compose->inreplyto = g_strdup(msginfo->msgid);
2243
2244         if (!compose->references) {
2245                 if (msginfo->msgid && *msginfo->msgid) {
2246                         if (msginfo->inreplyto && *msginfo->inreplyto)
2247                                 compose->references =
2248                                         g_strdup_printf("<%s>\n\t<%s>",
2249                                                         msginfo->inreplyto,
2250                                                         msginfo->msgid);
2251                         else
2252                                 compose->references =
2253                                         g_strconcat("<", msginfo->msgid, ">",
2254                                                     NULL);
2255                 } else if (msginfo->inreplyto && *msginfo->inreplyto) {
2256                         compose->references =
2257                                 g_strconcat("<", msginfo->inreplyto, ">",
2258                                             NULL);
2259                 }
2260         }
2261
2262         return 0;
2263 }
2264
2265 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
2266 {
2267         GSList *ref_id_list, *cur;
2268         GString *new_ref;
2269         gchar *new_ref_str;
2270
2271         ref_id_list = references_list_append(NULL, ref);
2272         if (!ref_id_list) return NULL;
2273         if (msgid && *msgid)
2274                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
2275
2276         for (;;) {
2277                 gint len = 0;
2278
2279                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
2280                         /* "<" + Message-ID + ">" + CR+LF+TAB */
2281                         len += strlen((gchar *)cur->data) + 5;
2282
2283                 if (len > MAX_REFERENCES_LEN) {
2284                         /* remove second message-ID */
2285                         if (ref_id_list && ref_id_list->next &&
2286                             ref_id_list->next->next) {
2287                                 g_free(ref_id_list->next->data);
2288                                 ref_id_list = g_slist_remove
2289                                         (ref_id_list, ref_id_list->next->data);
2290                         } else {
2291                                 slist_free_strings(ref_id_list);
2292                                 g_slist_free(ref_id_list);
2293                                 return NULL;
2294                         }
2295                 } else
2296                         break;
2297         }
2298
2299         new_ref = g_string_new("");
2300         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
2301                 if (new_ref->len > 0)
2302                         g_string_append(new_ref, "\n\t");
2303                 g_string_append_printf(new_ref, "<%s>", (gchar *)cur->data);
2304         }
2305
2306         slist_free_strings(ref_id_list);
2307         g_slist_free(ref_id_list);
2308
2309         new_ref_str = new_ref->str;
2310         g_string_free(new_ref, FALSE);
2311
2312         return new_ref_str;
2313 }
2314
2315 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
2316                                 const gchar *fmt, const gchar *qmark,
2317                                 const gchar *body, gboolean rewrap)
2318 {
2319         static MsgInfo dummyinfo;
2320         gchar *quote_str = NULL;
2321         gchar *buf;
2322         gboolean prev_autowrap;
2323         const gchar *trimmed_body = body;
2324         gint cursor_pos = -1;
2325         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2326         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2327         GtkTextIter iter;
2328         GtkTextMark *mark;
2329         
2330
2331         SIGNAL_BLOCK(buffer);
2332
2333         if (!msginfo)
2334                 msginfo = &dummyinfo;
2335
2336         if (qmark != NULL) {
2337                 quote_fmt_init(msginfo, NULL, NULL, FALSE);
2338                 quote_fmt_scan_string(qmark);
2339                 quote_fmt_parse();
2340
2341                 buf = quote_fmt_get_buffer();
2342                 if (buf == NULL)
2343                         alertpanel_error(_("Quote mark format error."));
2344                 else
2345                         Xstrdup_a(quote_str, buf, goto error)
2346         }
2347
2348         if (fmt && *fmt != '\0') {
2349                 while (trimmed_body && strlen(trimmed_body) > 1
2350                         && trimmed_body[0]=='\n')
2351                         *trimmed_body++;
2352
2353                 quote_fmt_init(msginfo, quote_str, trimmed_body, FALSE);
2354                 quote_fmt_scan_string(fmt);
2355                 quote_fmt_parse();
2356
2357                 buf = quote_fmt_get_buffer();
2358                 if (buf == NULL) {
2359                         alertpanel_error(_("Message reply/forward format error."));
2360                         goto error;
2361                 }
2362         } else
2363                 buf = "";
2364
2365         prev_autowrap = compose->autowrap;
2366         compose->autowrap = FALSE;
2367
2368         mark = gtk_text_buffer_get_insert(buffer);
2369         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2370         if (g_utf8_validate(buf, -1, NULL)) { 
2371                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
2372         } else {
2373                 gchar *tmpout = NULL;
2374                 tmpout = conv_codeset_strdup
2375                         (buf, conv_get_locale_charset_str_no_utf8(),
2376                          CS_INTERNAL);
2377                 if (!tmpout || !g_utf8_validate(tmpout, -1, NULL)) {
2378                         g_free(tmpout);
2379                         tmpout = g_malloc(strlen(buf)*2+1);
2380                         conv_localetodisp(tmpout, strlen(buf)*2+1, buf);
2381                 }
2382                 gtk_text_buffer_insert(buffer, &iter, tmpout, -1);
2383                 g_free(tmpout);
2384         }
2385
2386         cursor_pos = quote_fmt_get_cursor_pos();
2387         compose->set_cursor_pos = cursor_pos;
2388         if (cursor_pos == -1) {
2389                 cursor_pos = 0;
2390         }
2391         gtk_text_buffer_get_start_iter(buffer, &iter);
2392         gtk_text_buffer_get_iter_at_offset(buffer, &iter, cursor_pos);
2393         gtk_text_buffer_place_cursor(buffer, &iter);
2394
2395         compose->autowrap = prev_autowrap;
2396         if (compose->autowrap && rewrap)
2397                 compose_wrap_all(compose);
2398
2399         goto ok;
2400
2401 error:
2402         buf = NULL;
2403 ok:
2404         SIGNAL_UNBLOCK(buffer);
2405
2406
2407         return buf;
2408 }
2409
2410 /* if ml_post is of type addr@host and from is of type
2411  * addr-anything@host, return TRUE
2412  */
2413 static gboolean is_subscription(const gchar *ml_post, const gchar *from)
2414 {
2415         gchar *left_ml = NULL;
2416         gchar *right_ml = NULL;
2417         gchar *left_from = NULL;
2418         gchar *right_from = NULL;
2419         gboolean result = FALSE;
2420         
2421         if (!ml_post || !from)
2422                 return FALSE;
2423         
2424         left_ml = g_strdup(ml_post);
2425         if (strstr(left_ml, "@")) {
2426                 right_ml = strstr(left_ml, "@")+1;
2427                 *(strstr(left_ml, "@")) = '\0';
2428         }
2429         
2430         left_from = g_strdup(from);
2431         if (strstr(left_from, "@")) {
2432                 right_from = strstr(left_from, "@")+1;
2433                 *(strstr(left_from, "@")) = '\0';
2434         }
2435         
2436         if (left_ml && left_from && right_ml && right_from
2437         &&  !strncmp(left_from, left_ml, strlen(left_ml))
2438         &&  !strcmp(right_from, right_ml)) {
2439                 result = TRUE;
2440         }
2441         g_free(left_ml);
2442         g_free(left_from);
2443         
2444         return result;
2445 }
2446
2447 static gboolean same_address(const gchar *addr1, const gchar *addr2)
2448 {
2449         gchar *my_addr1, *my_addr2;
2450         
2451         if (!addr1 || !addr2)
2452                 return FALSE;
2453
2454         Xstrdup_a(my_addr1, addr1, return FALSE);
2455         Xstrdup_a(my_addr2, addr2, return FALSE);
2456         
2457         extract_address(my_addr1);
2458         extract_address(my_addr2);
2459         
2460         return !strcmp(my_addr1, my_addr2);
2461 }
2462
2463 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
2464                                     gboolean to_all, gboolean to_ml,
2465                                     gboolean to_sender,
2466                                     gboolean followup_and_reply_to)
2467 {
2468         GSList *cc_list = NULL;
2469         GSList *cur;
2470         gchar *from = NULL;
2471         gchar *replyto = NULL;
2472         GHashTable *to_table;
2473
2474         gboolean reply_to_ml = FALSE;
2475         gboolean default_reply_to = FALSE;
2476
2477         g_return_if_fail(compose->account != NULL);
2478         g_return_if_fail(msginfo != NULL);
2479
2480         reply_to_ml = to_ml && compose->ml_post;
2481
2482         default_reply_to = msginfo->folder && 
2483                 msginfo->folder->prefs->enable_default_reply_to;
2484
2485         if (compose->account->protocol != A_NNTP) {
2486                 if (reply_to_ml && !default_reply_to) {
2487                         
2488                         gboolean is_subscr = is_subscription(compose->ml_post,
2489                                                              msginfo->from);
2490                         if (!is_subscr) {
2491                                 /* normal answer to ml post with a reply-to */
2492                                 compose_entry_append(compose,
2493                                            compose->ml_post,
2494                                            COMPOSE_TO);
2495                                 if (compose->replyto
2496                                 &&  !same_address(compose->ml_post, compose->replyto))
2497                                         compose_entry_append(compose,
2498                                                 compose->replyto,
2499                                                 COMPOSE_CC);
2500                         } else {
2501                                 /* answer to subscription confirmation */
2502                                 if (compose->replyto)
2503                                         compose_entry_append(compose,
2504                                                 compose->replyto,
2505                                                 COMPOSE_TO);
2506                                 else if (msginfo->from)
2507                                         compose_entry_append(compose,
2508                                                 msginfo->from,
2509                                                 COMPOSE_TO);
2510                         }
2511                 }
2512                 else if (!(to_all || to_sender) && default_reply_to) {
2513                         compose_entry_append(compose,
2514                             msginfo->folder->prefs->default_reply_to,
2515                             COMPOSE_TO);
2516                         compose_entry_mark_default_to(compose,
2517                                 msginfo->folder->prefs->default_reply_to);
2518                 } else {
2519                         gchar *tmp1 = NULL;
2520                         if (!msginfo->from)
2521                                 return;
2522                         Xstrdup_a(tmp1, msginfo->from, return);
2523                         extract_address(tmp1);
2524                         if (to_all || to_sender ||
2525                             !account_find_from_address(tmp1))
2526                                 compose_entry_append(compose,
2527                                  (compose->replyto && !to_sender)
2528                                           ? compose->replyto :
2529                                           msginfo->from ? msginfo->from : "",
2530                                           COMPOSE_TO);
2531                         else if (!to_all && !to_sender) {
2532                                 /* reply to the last list of recipients */
2533                                 compose_entry_append(compose,
2534                                           msginfo->to ? msginfo->to : "",
2535                                           COMPOSE_TO);
2536                                 compose_entry_append(compose,
2537                                           msginfo->cc ? msginfo->cc : "",
2538                                           COMPOSE_CC);
2539                         }
2540                 }
2541         } else {
2542                 if (to_sender || (compose->followup_to && 
2543                         !strncmp(compose->followup_to, "poster", 6)))
2544                         compose_entry_append
2545                                 (compose, 
2546                                  (compose->replyto ? compose->replyto :
2547                                         msginfo->from ? msginfo->from : ""),
2548                                  COMPOSE_TO);
2549                                  
2550                 else if (followup_and_reply_to || to_all) {
2551                         compose_entry_append
2552                                 (compose,
2553                                  (compose->replyto ? compose->replyto :
2554                                  msginfo->from ? msginfo->from : ""),
2555                                  COMPOSE_TO);                           
2556                 
2557                         compose_entry_append
2558                                 (compose,
2559                                  compose->followup_to ? compose->followup_to :
2560                                  compose->newsgroups ? compose->newsgroups : "",
2561                                  COMPOSE_NEWSGROUPS);
2562                 } 
2563                 else 
2564                         compose_entry_append
2565                                 (compose,
2566                                  compose->followup_to ? compose->followup_to :
2567                                  compose->newsgroups ? compose->newsgroups : "",
2568                                  COMPOSE_NEWSGROUPS);
2569         }
2570
2571         if (msginfo->subject && *msginfo->subject) {
2572                 gchar *buf, *buf2;
2573                 guchar *p;
2574
2575                 buf = p = g_strdup(msginfo->subject);
2576                 p += subject_get_prefix_length(p);
2577                 memmove(buf, p, strlen(p) + 1);
2578
2579                 buf2 = g_strdup_printf("Re: %s", buf);
2580                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
2581
2582                 g_free(buf2);
2583                 g_free(buf);
2584         } else
2585                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
2586
2587         if (to_ml && compose->ml_post) return;
2588         if (!to_all || compose->account->protocol == A_NNTP) return;
2589
2590         if (compose->replyto) {
2591                 Xstrdup_a(replyto, compose->replyto, return);
2592                 extract_address(replyto);
2593         }
2594         if (msginfo->from) {
2595                 Xstrdup_a(from, msginfo->from, return);
2596                 extract_address(from);
2597         }
2598
2599         if (replyto && from)
2600                 cc_list = address_list_append_with_comments(cc_list, from);
2601         if (to_all && msginfo->folder && 
2602             msginfo->folder->prefs->enable_default_reply_to)
2603                 cc_list = address_list_append_with_comments(cc_list,
2604                                 msginfo->folder->prefs->default_reply_to);
2605         cc_list = address_list_append_with_comments(cc_list, msginfo->to);
2606         cc_list = address_list_append_with_comments(cc_list, compose->cc);
2607
2608         to_table = g_hash_table_new(g_str_hash, g_str_equal);
2609         if (replyto)
2610                 g_hash_table_insert(to_table, g_strdup(replyto), GINT_TO_POINTER(1));
2611         if (compose->account)
2612                 g_hash_table_insert(to_table, g_strdup(compose->account->address),
2613                                     GINT_TO_POINTER(1));
2614
2615         /* remove address on To: and that of current account */
2616         for (cur = cc_list; cur != NULL; ) {
2617                 GSList *next = cur->next;
2618                 gchar *addr;
2619
2620                 addr = g_strdup(cur->data);
2621                 extract_address(addr);
2622
2623                 if (GPOINTER_TO_INT(g_hash_table_lookup(to_table, addr)) == 1)
2624                         cc_list = g_slist_remove(cc_list, cur->data);
2625                 else
2626                         g_hash_table_insert(to_table, addr, GINT_TO_POINTER(1));
2627
2628                 cur = next;
2629         }
2630         hash_free_strings(to_table);
2631         g_hash_table_destroy(to_table);
2632
2633         if (cc_list) {
2634                 for (cur = cc_list; cur != NULL; cur = cur->next)
2635                         compose_entry_append(compose, (gchar *)cur->data,
2636                                              COMPOSE_CC);
2637                 slist_free_strings(cc_list);
2638                 g_slist_free(cc_list);
2639         }
2640
2641 }
2642
2643 #define SET_ENTRY(entry, str) \
2644 { \
2645         if (str && *str) \
2646                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
2647 }
2648
2649 #define SET_ADDRESS(type, str) \
2650 { \
2651         if (str && *str) \
2652                 compose_entry_append(compose, str, type); \
2653 }
2654
2655 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
2656 {
2657         g_return_if_fail(msginfo != NULL);
2658
2659         SET_ENTRY(subject_entry, msginfo->subject);
2660         SET_ENTRY(from_name, msginfo->from);
2661         SET_ADDRESS(COMPOSE_TO, msginfo->to);
2662         SET_ADDRESS(COMPOSE_CC, compose->cc);
2663         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
2664         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
2665         SET_ADDRESS(COMPOSE_NEWSGROUPS, compose->newsgroups);
2666         SET_ADDRESS(COMPOSE_FOLLOWUPTO, compose->followup_to);
2667
2668         compose_update_priority_menu_item(compose);
2669         compose_update_privacy_system_menu_item(compose, FALSE);
2670         compose_show_first_last_header(compose, TRUE);
2671 }
2672
2673 #undef SET_ENTRY
2674 #undef SET_ADDRESS
2675
2676 static void compose_insert_sig(Compose *compose, gboolean replace)
2677 {
2678         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2679         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2680         GtkTextMark *mark;
2681         GtkTextIter iter, iter_end;
2682         gint cur_pos;
2683         gboolean prev_autowrap;
2684
2685         
2686         g_return_if_fail(compose->account != NULL);
2687
2688         prev_autowrap = compose->autowrap;
2689         compose->autowrap = FALSE;
2690
2691         g_signal_handlers_block_by_func(G_OBJECT(buffer),
2692                                         G_CALLBACK(compose_changed_cb),
2693                                         compose);
2694         
2695         mark = gtk_text_buffer_get_insert(buffer);
2696         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2697         cur_pos = gtk_text_iter_get_offset (&iter);
2698
2699         gtk_text_buffer_get_end_iter(buffer, &iter);
2700
2701         if (replace && compose->sig_str) {
2702                 gboolean found;
2703                 GtkTextIter first_iter, start_iter, end_iter;
2704
2705                 gtk_text_buffer_get_start_iter(buffer, &first_iter);
2706
2707                 if (compose->sig_str[0] == '\0')
2708                         found = FALSE;
2709                 else
2710                         found = gtk_text_iter_forward_search(&first_iter,
2711                                                              compose->sig_str,
2712                                                              GTK_TEXT_SEARCH_TEXT_ONLY,
2713                                                              &start_iter, &end_iter,
2714                                                              NULL);
2715
2716                 if (found) {
2717                         gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
2718                         iter = start_iter;
2719                 }
2720         }
2721
2722         g_free(compose->sig_str);
2723         compose->sig_str = compose_get_signature_str(compose);
2724         if (!compose->sig_str || (replace && !compose->account->auto_sig))
2725                 compose->sig_str = g_strdup("");
2726
2727         cur_pos = gtk_text_iter_get_offset(&iter);
2728         gtk_text_buffer_insert(buffer, &iter, compose->sig_str, -1);
2729         /* skip \n\n */
2730         gtk_text_buffer_get_iter_at_offset(buffer, &iter, cur_pos);
2731         gtk_text_iter_forward_char(&iter);
2732         gtk_text_iter_forward_char(&iter);
2733         gtk_text_buffer_get_end_iter(buffer, &iter_end);
2734         gtk_text_buffer_apply_tag_by_name(buffer,"signature",&iter, &iter_end);
2735
2736         if (cur_pos > gtk_text_buffer_get_char_count (buffer))
2737                 cur_pos = gtk_text_buffer_get_char_count (buffer);
2738
2739         /* put the cursor where it should be 
2740          * either where the quote_fmt says, either before the signature */
2741         if (compose->set_cursor_pos < 0)
2742                 gtk_text_buffer_get_iter_at_offset(buffer, &iter, cur_pos);
2743         else
2744                 gtk_text_buffer_get_iter_at_offset(buffer, &iter, 
2745                         compose->set_cursor_pos);
2746                 
2747         gtk_text_buffer_place_cursor(buffer, &iter);
2748         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
2749                                         G_CALLBACK(compose_changed_cb),
2750                                         compose);
2751                 
2752         compose->autowrap = prev_autowrap;
2753         if (compose->autowrap)
2754                 compose_wrap_all(compose);
2755 }
2756
2757 static gchar *compose_get_signature_str(Compose *compose)
2758 {
2759         gchar *sig_body = NULL;
2760         gchar *sig_str = NULL;
2761         gchar *utf8_sig_str = NULL;
2762
2763         g_return_val_if_fail(compose->account != NULL, NULL);
2764
2765         if (!compose->account->sig_path)
2766                 return NULL;
2767
2768         if (compose->account->sig_type == SIG_FILE) {
2769                 if (!is_file_or_fifo_exist(compose->account->sig_path)) {
2770                         g_warning("can't open signature file: %s\n",
2771                                   compose->account->sig_path);
2772                         return NULL;
2773                 }
2774         }
2775
2776         if (compose->account->sig_type == SIG_COMMAND)
2777                 sig_body = get_command_output(compose->account->sig_path);
2778         else {
2779                 gchar *tmp;
2780
2781                 tmp = file_read_to_str(compose->account->sig_path);
2782                 if (!tmp)
2783                         return NULL;
2784                 sig_body = normalize_newlines(tmp);
2785                 g_free(tmp);
2786         }
2787
2788         if (compose->account->sig_sep) {
2789                 sig_str = g_strconcat("\n\n", compose->account->sig_sep, "\n", sig_body,
2790                                       NULL);
2791                 g_free(sig_body);
2792         } else
2793                 sig_str = g_strconcat("\n\n", sig_body, NULL);
2794
2795         if (sig_str) {
2796                 if (g_utf8_validate(sig_str, -1, NULL) == TRUE)
2797                         utf8_sig_str = sig_str;
2798                 else {
2799                         utf8_sig_str = conv_codeset_strdup
2800                                 (sig_str, conv_get_locale_charset_str_no_utf8(),
2801                                  CS_INTERNAL);
2802                         g_free(sig_str);
2803                 }
2804         }
2805
2806         return utf8_sig_str;
2807 }
2808
2809 static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *file)
2810 {
2811         GtkTextView *text;
2812         GtkTextBuffer *buffer;
2813         GtkTextMark *mark;
2814         GtkTextIter iter;
2815         const gchar *cur_encoding;
2816         gchar buf[BUFFSIZE];
2817         gint len;
2818         FILE *fp;
2819         gboolean prev_autowrap;
2820         gboolean badtxt = FALSE;
2821
2822         g_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
2823
2824         if ((fp = g_fopen(file, "rb")) == NULL) {
2825                 FILE_OP_ERROR(file, "fopen");
2826                 return COMPOSE_INSERT_READ_ERROR;
2827         }
2828
2829         prev_autowrap = compose->autowrap;
2830         compose->autowrap = FALSE;
2831
2832         text = GTK_TEXT_VIEW(compose->text);
2833         buffer = gtk_text_view_get_buffer(text);
2834         mark = gtk_text_buffer_get_insert(buffer);
2835         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2836
2837         g_signal_handlers_block_by_func(G_OBJECT(buffer),
2838                                         G_CALLBACK(text_inserted),
2839                                         compose);
2840
2841         cur_encoding = conv_get_locale_charset_str_no_utf8();
2842
2843         while (fgets(buf, sizeof(buf), fp) != NULL) {
2844                 gchar *str;
2845
2846                 if (g_utf8_validate(buf, -1, NULL) == TRUE)
2847                         str = g_strdup(buf);
2848                 else
2849                         str = conv_codeset_strdup
2850                                 (buf, cur_encoding, CS_INTERNAL);
2851                 if (!str) continue;
2852
2853                 /* strip <CR> if DOS/Windows file,
2854                    replace <CR> with <LF> if Macintosh file. */
2855                 strcrchomp(str);
2856                 len = strlen(str);
2857                 if (len > 0 && str[len - 1] != '\n') {
2858                         while (--len >= 0)
2859                                 if (str[len] == '\r') str[len] = '\n';
2860                 }
2861
2862                 gtk_text_buffer_insert(buffer, &iter, str, -1);
2863                 g_free(str);
2864         }
2865
2866         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
2867                                           G_CALLBACK(text_inserted),
2868                                           compose);
2869         compose->autowrap = prev_autowrap;
2870         if (compose->autowrap)
2871                 compose_wrap_all(compose);
2872
2873         fclose(fp);
2874
2875         if (badtxt)
2876                 return COMPOSE_INSERT_INVALID_CHARACTER;
2877         else 
2878                 return COMPOSE_INSERT_SUCCESS;
2879 }
2880
2881 static void compose_attach_append(Compose *compose, const gchar *file,
2882                                   const gchar *filename,
2883                                   const gchar *content_type)
2884 {
2885         AttachInfo *ainfo;
2886         GtkTreeIter iter;
2887         FILE *fp;
2888         off_t size;
2889         GAuto *auto_ainfo;
2890         gchar *size_text;
2891         GtkListStore *store;
2892         gchar *name;
2893         gboolean has_binary = FALSE;
2894
2895         if (!is_file_exist(file)) {
2896                 g_warning("File %s doesn't exist\n", filename);
2897                 return;
2898         }
2899         if ((size = get_file_size(file)) < 0) {
2900                 g_warning("Can't get file size of %s\n", filename);
2901                 return;
2902         }
2903         if (size == 0) {
2904                 alertpanel_notice(_("File %s is empty."), filename);
2905                 return;
2906         }
2907         if ((fp = g_fopen(file, "rb")) == NULL) {
2908                 alertpanel_error(_("Can't read %s."), filename);
2909                 return;
2910         }
2911         fclose(fp);
2912
2913         ainfo = g_new0(AttachInfo, 1);
2914         auto_ainfo = g_auto_pointer_new_with_free
2915                         (ainfo, (GFreeFunc) compose_attach_info_free); 
2916         ainfo->file = g_strdup(file);
2917
2918         if (content_type) {
2919                 ainfo->content_type = g_strdup(content_type);
2920                 if (!g_ascii_strcasecmp(content_type, "message/rfc822")) {
2921                         MsgInfo *msginfo;
2922                         MsgFlags flags = {0, 0};
2923
2924                         if (procmime_get_encoding_for_text_file(file, &has_binary) == ENC_7BIT)
2925                                 ainfo->encoding = ENC_7BIT;
2926                         else
2927                                 ainfo->encoding = ENC_8BIT;
2928
2929                         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
2930                         if (msginfo && msginfo->subject)
2931                                 name = g_strdup(msginfo->subject);
2932                         else
2933                                 name = g_path_get_basename(filename ? filename : file);
2934
2935                         ainfo->name = g_strdup_printf(_("Message: %s"), name);
2936
2937                         procmsg_msginfo_free(msginfo);
2938                 } else {
2939                         if (!g_ascii_strncasecmp(content_type, "text", 4))
2940                                 ainfo->encoding = procmime_get_encoding_for_text_file(file, &has_binary);
2941                         else
2942                                 ainfo->encoding = ENC_BASE64;
2943                         name = g_path_get_basename(filename ? filename : file);
2944                         ainfo->name = g_strdup(name);
2945                 }
2946                 g_free(name);
2947         } else {
2948                 ainfo->content_type = procmime_get_mime_type(file);
2949                 if (!ainfo->content_type) {
2950                         ainfo->content_type =
2951                                 g_strdup("application/octet-stream");
2952                         ainfo->encoding = ENC_BASE64;
2953                 } else if (!g_ascii_strncasecmp(ainfo->content_type, "text", 4))
2954                         ainfo->encoding =
2955                                 procmime_get_encoding_for_text_file(file, &has_binary);
2956                 else
2957                         ainfo->encoding = ENC_BASE64;
2958                 name = g_path_get_basename(filename ? filename : file);
2959                 ainfo->name = g_strdup(name);   
2960                 g_free(name);
2961         }
2962
2963         if (!strcmp(ainfo->content_type, "unknown") || has_binary) {
2964                 g_free(ainfo->content_type);
2965                 ainfo->content_type = g_strdup("application/octet-stream");
2966         }
2967
2968         ainfo->size = size;
2969         size_text = to_human_readable(size);
2970
2971         store = GTK_LIST_STORE(gtk_tree_view_get_model
2972                         (GTK_TREE_VIEW(compose->attach_clist)));
2973                 
2974         gtk_list_store_append(store, &iter);
2975         gtk_list_store_set(store, &iter, 
2976                            COL_MIMETYPE, ainfo->content_type,
2977                            COL_SIZE, size_text,
2978                            COL_NAME, ainfo->name,
2979                            COL_DATA, ainfo,
2980                            COL_AUTODATA, auto_ainfo,
2981                            -1);
2982         
2983         g_auto_pointer_free(auto_ainfo);
2984 }
2985
2986 static void compose_use_signing(Compose *compose, gboolean use_signing)
2987 {
2988         GtkItemFactory *ifactory;
2989         GtkWidget *menuitem = NULL;
2990
2991         compose->use_signing = use_signing;
2992         ifactory = gtk_item_factory_from_widget(compose->menubar);
2993         menuitem = gtk_item_factory_get_item
2994                 (ifactory, "/Options/Sign");
2995         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
2996                                        use_signing);
2997 }
2998
2999 static void compose_use_encryption(Compose *compose, gboolean use_encryption)
3000 {
3001         GtkItemFactory *ifactory;
3002         GtkWidget *menuitem = NULL;
3003
3004         compose->use_encryption = use_encryption;
3005         ifactory = gtk_item_factory_from_widget(compose->menubar);
3006         menuitem = gtk_item_factory_get_item
3007                 (ifactory, "/Options/Encrypt");
3008
3009         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
3010                                        use_encryption);
3011 }
3012
3013 #define NEXT_PART_NOT_CHILD(info)  \
3014 {  \
3015         node = info->node;  \
3016         while (node->children)  \
3017                 node = g_node_last_child(node);  \
3018         info = procmime_mimeinfo_next((MimeInfo *)node->data);  \
3019 }
3020
3021 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
3022 {
3023         MimeInfo *mimeinfo;
3024         MimeInfo *child;
3025         MimeInfo *firsttext = NULL;
3026         MimeInfo *encrypted = NULL;
3027         GNode    *node;
3028         gchar *outfile;
3029         const gchar *partname = NULL;
3030
3031         mimeinfo = procmime_scan_message(msginfo);
3032         if (!mimeinfo) return;
3033
3034         if (mimeinfo->node->children == NULL) {
3035                 procmime_mimeinfo_free_all(mimeinfo);
3036                 return;
3037         }
3038
3039         /* find first content part */
3040         child = (MimeInfo *) mimeinfo->node->children->data;
3041         while (child && child->node->children && (child->type == MIMETYPE_MULTIPART))
3042                 child = (MimeInfo *)child->node->children->data;
3043
3044         if (child->type == MIMETYPE_TEXT) {
3045                 firsttext = child;
3046                 debug_print("First text part found\n");
3047         } else if (compose->mode == COMPOSE_REEDIT &&
3048                  child->type == MIMETYPE_APPLICATION &&
3049                  !g_ascii_strcasecmp(child->subtype, "pgp-encrypted")) {
3050                 encrypted = (MimeInfo *)child->node->parent->data;
3051         }
3052      
3053         child = (MimeInfo *) mimeinfo->node->children->data;
3054         while (child != NULL) {
3055                 if (child == encrypted) {
3056                         /* skip this part of tree */
3057                         NEXT_PART_NOT_CHILD(child);
3058                         continue;
3059                 }
3060
3061                 if (child->type == MIMETYPE_MULTIPART) {
3062                         /* get the actual content */
3063                         child = procmime_mimeinfo_next(child);
3064                         continue;
3065                 }
3066                     
3067                 if (child == firsttext) {
3068                         child = procmime_mimeinfo_next(child);
3069                         continue;
3070                 }
3071
3072                 outfile = procmime_get_tmp_file_name(child);
3073                 if (procmime_get_part(outfile, child) < 0)
3074                         g_warning("Can't get the part of multipart message.");
3075                 else {
3076                         gchar *content_type;
3077
3078                         content_type = procmime_get_content_type_str(child->type, child->subtype);
3079
3080                         /* if we meet a pgp signature, we don't attach it, but
3081                          * we force signing. */
3082                         if (strcmp(content_type, "application/pgp-signature")) {
3083                                 partname = procmime_mimeinfo_get_parameter(child, "filename");
3084                                 if (partname == NULL)
3085                                         partname = procmime_mimeinfo_get_parameter(child, "name");
3086                                 if (partname == NULL)
3087                                         partname = "";
3088                                 compose_attach_append(compose, outfile, 
3089                                                       partname, content_type);
3090                         } else {
3091                                 compose_force_signing(compose, compose->account);
3092                         }
3093                         g_free(content_type);
3094                 }
3095                 g_free(outfile);
3096                 NEXT_PART_NOT_CHILD(child);
3097         }
3098         procmime_mimeinfo_free_all(mimeinfo);
3099 }
3100
3101 #undef NEXT_PART_NOT_CHILD
3102
3103
3104
3105 typedef enum {
3106         WAIT_FOR_INDENT_CHAR,
3107         WAIT_FOR_INDENT_CHAR_OR_SPACE,
3108 } IndentState;
3109
3110 /* return indent length, we allow:
3111    indent characters followed by indent characters or spaces/tabs,
3112    alphabets and numbers immediately followed by indent characters,
3113    and the repeating sequences of the above
3114    If quote ends with multiple spaces, only the first one is included. */
3115 static gchar *compose_get_quote_str(GtkTextBuffer *buffer,
3116                                     const GtkTextIter *start, gint *len)
3117 {
3118         GtkTextIter iter = *start;
3119         gunichar wc;
3120         gchar ch[6];
3121         gint clen;
3122         IndentState state = WAIT_FOR_INDENT_CHAR;
3123         gboolean is_space;
3124         gboolean is_indent;
3125         gint alnum_count = 0;
3126         gint space_count = 0;
3127         gint quote_len = 0;
3128
3129         if (prefs_common.quote_chars == NULL) {
3130                 return 0 ;
3131         }
3132
3133         while (!gtk_text_iter_ends_line(&iter)) {
3134                 wc = gtk_text_iter_get_char(&iter);
3135                 if (g_unichar_iswide(wc))
3136                         break;
3137                 clen = g_unichar_to_utf8(wc, ch);
3138                 if (clen != 1)
3139                         break;
3140
3141                 is_indent = strchr(prefs_common.quote_chars, ch[0]) ? TRUE : FALSE;
3142                 is_space = g_unichar_isspace(wc);
3143
3144                 if (state == WAIT_FOR_INDENT_CHAR) {
3145                         if (!is_indent && !g_unichar_isalnum(wc))
3146                                 break;
3147                         if (is_indent) {
3148                                 quote_len += alnum_count + space_count + 1;
3149                                 alnum_count = space_count = 0;
3150                                 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
3151                         } else
3152                                 alnum_count++;
3153                 } else if (state == WAIT_FOR_INDENT_CHAR_OR_SPACE) {
3154                         if (!is_indent && !is_space && !g_unichar_isalnum(wc))
3155                                 break;
3156                         if (is_space)
3157                                 space_count++;
3158                         else if (is_indent) {
3159                                 quote_len += alnum_count + space_count + 1;
3160                                 alnum_count = space_count = 0;
3161                         } else {
3162                                 alnum_count++;
3163                                 state = WAIT_FOR_INDENT_CHAR;
3164                         }
3165                 }
3166
3167                 gtk_text_iter_forward_char(&iter);
3168         }
3169
3170         if (quote_len > 0 && space_count > 0)
3171                 quote_len++;
3172
3173         if (len)
3174                 *len = quote_len;
3175
3176         if (quote_len > 0) {
3177                 iter = *start;
3178                 gtk_text_iter_forward_chars(&iter, quote_len);
3179                 return gtk_text_buffer_get_text(buffer, start, &iter, FALSE);
3180         }
3181
3182         return NULL;
3183 }
3184
3185 /* return TRUE if the line is itemized */
3186 static gboolean compose_is_itemized(GtkTextBuffer *buffer,
3187                                     const GtkTextIter *start)
3188 {
3189         GtkTextIter iter = *start;
3190         gunichar wc;
3191         gchar ch[6];
3192         gint clen;
3193
3194         if (gtk_text_iter_ends_line(&iter))
3195                 return FALSE;
3196
3197         while (1) {
3198                 wc = gtk_text_iter_get_char(&iter);
3199                 if (!g_unichar_isspace(wc))
3200                         break;
3201                 gtk_text_iter_forward_char(&iter);
3202                 if (gtk_text_iter_ends_line(&iter))
3203                         return FALSE;
3204         }
3205
3206         clen = g_unichar_to_utf8(wc, ch);
3207         if (clen != 1)
3208                 return FALSE;
3209
3210         if (!strchr("*-+", ch[0]))
3211                 return FALSE;
3212
3213         gtk_text_iter_forward_char(&iter);
3214         if (gtk_text_iter_ends_line(&iter))
3215                 return FALSE;
3216         wc = gtk_text_iter_get_char(&iter);
3217         if (g_unichar_isspace(wc))
3218                 return TRUE;
3219
3220         return FALSE;
3221 }
3222
3223 static gboolean compose_get_line_break_pos(GtkTextBuffer *buffer,
3224                                            const GtkTextIter *start,
3225                                            GtkTextIter *break_pos,
3226                                            gint max_col,
3227                                            gint quote_len)
3228 {
3229         GtkTextIter iter = *start, line_end = *start;
3230         PangoLogAttr *attrs;
3231         gchar *str;
3232         gchar *p;
3233         gint len;
3234         gint i;
3235         gint col = 0;
3236         gint pos = 0;
3237         gboolean can_break = FALSE;
3238         gboolean do_break = FALSE;
3239         gboolean was_white = FALSE;
3240         gboolean prev_dont_break = FALSE;
3241
3242         gtk_text_iter_forward_to_line_end(&line_end);
3243         str = gtk_text_buffer_get_text(buffer, &iter, &line_end, FALSE);
3244         len = g_utf8_strlen(str, -1);
3245         /* g_print("breaking line: %d: %s (len = %d)\n",
3246                 gtk_text_iter_get_line(&iter), str, len); */
3247         attrs = g_new(PangoLogAttr, len + 1);
3248
3249         pango_default_break(str, -1, NULL, attrs, len + 1);
3250
3251         p = str;