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