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