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