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