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