custom toolbar: compose win 1
[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         if (IS_FIRST_PART_TEXT(child))
2047                 child = child->next;
2048
2049         infile = procmsg_get_message_file_path(msginfo);
2050
2051         while (child != NULL) {
2052                 if (child->children || child->mime_type == MIME_MULTIPART) {
2053                         child = procmime_mimeinfo_next(child);
2054                         continue;
2055                 }
2056
2057                 outfile = procmime_get_tmp_file_name(child);
2058                 if (procmime_get_part(outfile, infile, child) < 0)
2059                         g_warning(_("Can't get the part of multipart message."));
2060                 else
2061                         compose_attach_append
2062                                 (compose, outfile,
2063                                  child->filename ? child->filename : child->name,
2064                                  child->content_type);
2065
2066                 child = child->next;
2067         }
2068
2069         g_free(infile);
2070         procmime_mimeinfo_free_all(mimeinfo);
2071 }
2072
2073 #undef IS_FIRST_PART_TEXT
2074
2075 #define GET_CHAR(pos, buf, len)                                              \
2076 {                                                                            \
2077         if (text->use_wchar)                                                 \
2078                 len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos)));    \
2079         else {                                                               \
2080                 buf[0] = GTK_STEXT_INDEX(text, (pos));                       \
2081                 len = 1;                                                     \
2082         }                                                                    \
2083 }
2084
2085 #define INDENT_CHARS    ">|#"
2086 #define SPACE_CHARS     " \t"
2087
2088 static void compose_wrap_line(Compose *compose)
2089 {
2090         GtkSText *text = GTK_STEXT(compose->text);
2091         gint ch_len, last_ch_len;
2092         gchar cbuf[MB_LEN_MAX], last_ch;
2093         guint text_len;
2094         guint line_end;
2095         guint quoted;
2096         gint p_start, p_end;
2097         gint line_pos, cur_pos;
2098         gint line_len, cur_len;
2099
2100         gtk_stext_freeze(text);
2101
2102         text_len = gtk_stext_get_length(text);
2103
2104         /* check to see if the point is on the paragraph mark (empty line). */
2105         cur_pos = gtk_stext_get_point(text);
2106         GET_CHAR(cur_pos, cbuf, ch_len);
2107         if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
2108                 if (cur_pos == 0)
2109                         goto compose_end; /* on the paragraph mark */
2110                 GET_CHAR(cur_pos - 1, cbuf, ch_len);
2111                 if (ch_len == 1 && *cbuf == '\n')
2112                         goto compose_end; /* on the paragraph mark */
2113         }
2114
2115         /* find paragraph start. */
2116         line_end = quoted = 0;
2117         for (p_start = cur_pos; p_start >= 0; --p_start) {
2118                 GET_CHAR(p_start, cbuf, ch_len);
2119                 if (ch_len == 1 && *cbuf == '\n') {
2120                         if (quoted)
2121                                 goto compose_end; /* quoted part */
2122                         if (line_end) {
2123                                 p_start += 2;
2124                                 break;
2125                         }
2126                         line_end = 1;
2127                 } else {
2128                         if (ch_len == 1 
2129                             && strchr(prefs_common.quote_chars, *cbuf))
2130                                 quoted = 1;
2131                         else if (ch_len != 1 || !isspace(*cbuf))
2132                                 quoted = 0;
2133
2134                         line_end = 0;
2135                 }
2136         }
2137         if (p_start < 0)
2138                 p_start = 0;
2139
2140         /* find paragraph end. */
2141         line_end = 0;
2142         for (p_end = cur_pos; p_end < text_len; p_end++) {
2143                 GET_CHAR(p_end, cbuf, ch_len);
2144                 if (ch_len == 1 && *cbuf == '\n') {
2145                         if (line_end) {
2146                                 p_end -= 1;
2147                                 break;
2148                         }
2149                         line_end = 1;
2150                 } else {
2151                         if (line_end && ch_len == 1 &&
2152                             strchr(prefs_common.quote_chars, *cbuf))
2153                                 goto compose_end; /* quoted part */
2154
2155                         line_end = 0;
2156                 }
2157         }
2158         if (p_end >= text_len)
2159                 p_end = text_len;
2160
2161         if (p_start >= p_end)
2162                 goto compose_end;
2163
2164         line_len = cur_len = 0;
2165         last_ch_len = 0;
2166         last_ch = '\0';
2167         line_pos = p_start;
2168         for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2169                 guint space = 0;
2170
2171                 GET_CHAR(cur_pos, cbuf, ch_len);
2172
2173                 if (ch_len < 0) {
2174                         cbuf[0] = '\0';
2175                         ch_len = 1;
2176                 }
2177
2178                 if (ch_len == 1 && isspace(*cbuf))
2179                         space = 1;
2180
2181                 if (ch_len == 1 && *cbuf == '\n') {
2182                         guint replace = 0;
2183                         if (last_ch_len == 1 && !isspace(last_ch)) {
2184                                 if (cur_pos + 1 < p_end) {
2185                                         GET_CHAR(cur_pos + 1, cbuf, ch_len);
2186                                         if (ch_len == 1 && !isspace(*cbuf))
2187                                                 replace = 1;
2188                                 }
2189                         }
2190                         gtk_stext_set_point(text, cur_pos + 1);
2191                         gtk_stext_backward_delete(text, 1);
2192                         if (replace) {
2193                                 gtk_stext_set_point(text, cur_pos);
2194                                 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2195                                 space = 1;
2196                         }
2197                         else {
2198                                 p_end--;
2199                                 cur_pos--;
2200                                 continue;
2201                         }
2202                 }
2203
2204                 last_ch_len = ch_len;
2205                 last_ch = *cbuf;
2206
2207                 if (space) {
2208                         line_pos = cur_pos + 1;
2209                         line_len = cur_len + ch_len;
2210                 }
2211
2212                 if (cur_len + ch_len > prefs_common.linewrap_len &&
2213                     line_len > 0) {
2214                         gint tlen = ch_len;
2215
2216                         GET_CHAR(line_pos - 1, cbuf, ch_len);
2217                         if (ch_len == 1 && isspace(*cbuf)) {
2218                                 gtk_stext_set_point(text, line_pos);
2219                                 gtk_stext_backward_delete(text, 1);
2220                                 p_end--;
2221                                 cur_pos--;
2222                                 line_pos--;
2223                                 cur_len--;
2224                                 line_len--;
2225                         }
2226                         ch_len = tlen;
2227
2228                         gtk_stext_set_point(text, line_pos);
2229                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2230                         p_end++;
2231                         cur_pos++;
2232                         line_pos++;
2233                         cur_len = cur_len - line_len + ch_len;
2234                         line_len = 0;
2235                         continue;
2236                 }
2237
2238                 if (ch_len > 1) {
2239                         line_pos = cur_pos + 1;
2240                         line_len = cur_len + ch_len;
2241                 }
2242                 cur_len += ch_len;
2243         }
2244
2245 compose_end:
2246         gtk_stext_thaw(text);
2247 }
2248
2249 #undef WRAP_DEBUG
2250 #ifdef WRAP_DEBUG
2251 /* Darko: used when I debug wrapping */
2252 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2253 {
2254         gint i, clen;
2255         gchar cbuf[MB_LEN_MAX];
2256
2257         printf("%d [", pos);
2258         for (i = pos; i < tlen; i++) {
2259                 GET_CHAR(i, cbuf, clen);
2260                 if (clen < 0) break;
2261                 if (breakoncr && clen == 1 && cbuf[0] == '\n')
2262                         break;
2263                 fwrite(cbuf, clen, 1, stdout);
2264         }
2265         printf("]\n");
2266 }
2267 #endif
2268
2269 typedef enum {
2270         WAIT_FOR_SPACE,
2271         WAIT_FOR_INDENT_CHAR,
2272         WAIT_FOR_INDENT_CHAR_OR_SPACE
2273 } IndentState;
2274
2275 /* return indent length, we allow:
2276    > followed by spaces/tabs
2277    | followed by spaces/tabs
2278    uppercase characters immediately followed by >,
2279    and the repeating sequences of the above */
2280 /* return indent length */
2281 static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
2282 {
2283         guint i_len = 0;
2284         guint i, ch_len, alnum_cnt = 0;
2285         IndentState state = WAIT_FOR_INDENT_CHAR;
2286         gchar cbuf[MB_LEN_MAX];
2287         gboolean is_space;
2288         gboolean is_indent;
2289
2290         for (i = start_pos; i < text_len; i++) {
2291                 GET_CHAR(i, cbuf, ch_len);
2292                 if (ch_len > 1)
2293                         break;
2294
2295                 if (cbuf[0] == '\n')
2296                         break;
2297
2298                 is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
2299                 is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
2300
2301                 switch (state) {
2302                 case WAIT_FOR_SPACE:
2303                         if (is_space == FALSE)
2304                                 goto out;
2305                         state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2306                         break;
2307                 case WAIT_FOR_INDENT_CHAR_OR_SPACE:
2308                         if (is_indent == FALSE && is_space == FALSE &&
2309                             !isupper(cbuf[0]))
2310                                 goto out;
2311                         if (is_space == TRUE) {
2312                                 alnum_cnt = 0;
2313                                 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2314                         } else if (is_indent == TRUE) {
2315                                 alnum_cnt = 0;
2316                                 state = WAIT_FOR_SPACE;
2317                         } else {
2318                                 alnum_cnt++;
2319                                 state = WAIT_FOR_INDENT_CHAR;
2320                         }
2321                         break;
2322                 case WAIT_FOR_INDENT_CHAR:
2323                         if (is_indent == FALSE && !isupper(cbuf[0]))
2324                                 goto out;
2325                         if (is_indent == TRUE) {
2326                                 if (alnum_cnt > 0 
2327                                     && !strchr(prefs_common.quote_chars, cbuf[0]))
2328                                         goto out;
2329                                 alnum_cnt = 0;
2330                                 state = WAIT_FOR_SPACE;
2331                         } else {
2332                                 alnum_cnt++;
2333                         }
2334                         break;
2335                 }
2336
2337                 i_len++;
2338         }
2339
2340 out:
2341         if ((i_len > 0) && (state == WAIT_FOR_INDENT_CHAR))
2342                 i_len -= alnum_cnt;
2343
2344         return i_len;
2345 }
2346
2347 /* insert quotation string when line was wrapped */
2348 static guint ins_quote(GtkSText *text, guint indent_len,
2349                        guint prev_line_pos, guint text_len,
2350                        gchar *quote_fmt)
2351 {
2352         guint i, ins_len = 0;
2353         gchar ch;
2354
2355         if (indent_len) {
2356                 for (i = 0; i < indent_len; i++) {
2357                         ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2358                         gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2359                 }
2360                 ins_len = indent_len;
2361         }
2362
2363         return ins_len;
2364 }
2365
2366 /* check if we should join the next line */
2367 static gboolean join_next_line(GtkSText *text, guint start_pos, guint tlen,
2368                                guint prev_ilen, gboolean autowrap)
2369 {
2370         guint indent_len, ch_len;
2371         gboolean do_join = FALSE;
2372         gchar cbuf[MB_LEN_MAX];
2373
2374         indent_len = get_indent_length(text, start_pos, tlen);
2375
2376         if ((autowrap || indent_len > 0) && indent_len == prev_ilen) {
2377                 GET_CHAR(start_pos + indent_len, cbuf, ch_len);
2378                 if (ch_len > 0 && (cbuf[0] != '\n'))
2379                         do_join = TRUE;
2380         }
2381
2382         return do_join;
2383 }
2384
2385 static void compose_wrap_line_all(Compose *compose)
2386 {
2387         compose_wrap_line_all_full(compose, FALSE);
2388 }
2389
2390 #define STEXT_FREEZE() \
2391         if (!frozen) { gtk_stext_freeze(text); frozen = TRUE; }
2392
2393 static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
2394 {
2395         GtkSText *text = GTK_STEXT(compose->text);
2396         guint tlen;
2397         guint line_pos = 0, cur_pos = 0, p_pos = 0;
2398         gint line_len = 0, cur_len = 0;
2399         gint ch_len;
2400         gboolean is_new_line = TRUE, do_delete = FALSE;
2401         guint i_len = 0;
2402         gboolean linewrap_quote = TRUE;
2403         gboolean set_editable_pos = FALSE;
2404         gint editable_pos = 0;
2405         gboolean frozen = FALSE;
2406         guint linewrap_len = prefs_common.linewrap_len;
2407         gchar *qfmt = prefs_common.quotemark;
2408         gchar cbuf[MB_LEN_MAX];
2409
2410         tlen = gtk_stext_get_length(text);
2411
2412         for (; cur_pos < tlen; cur_pos++) {
2413                 /* mark position of new line - needed for quotation wrap */
2414                 if (is_new_line) {
2415                         if (linewrap_quote)
2416                                 i_len = get_indent_length(text, cur_pos, tlen);
2417
2418                         is_new_line = FALSE;
2419                         p_pos = cur_pos;
2420 #ifdef WRAP_DEBUG
2421                         g_print("new line i_len=%d p_pos=", i_len);
2422                         dump_text(text, p_pos, tlen, 1);
2423 #endif
2424                 }
2425
2426                 GET_CHAR(cur_pos, cbuf, ch_len);
2427
2428                 /* fix line length for tabs */
2429                 if (ch_len == 1 && *cbuf == '\t') {
2430                         guint tab_width = text->default_tab_width;
2431                         guint tab_offset = line_len % tab_width;
2432
2433 #ifdef WRAP_DEBUG
2434                         g_print("found tab at pos=%d line_len=%d ", cur_pos,
2435                                 line_len);
2436 #endif
2437                         if (tab_offset) {
2438                                 line_len += tab_width - tab_offset - 1;
2439                                 cur_len = line_len;
2440                         }
2441 #ifdef WRAP_DEBUG
2442                         printf("new_len=%d\n", line_len);
2443 #endif
2444                 }
2445
2446                 /* we have encountered line break */
2447                 if (ch_len == 1 && *cbuf == '\n') {
2448                         gint clen;
2449                         gchar cb[MB_LEN_MAX];
2450
2451                         /* should we join the next line */
2452                         if ((autowrap || i_len != cur_len) && do_delete &&
2453                             join_next_line
2454                                 (text, cur_pos + 1, tlen, i_len, autowrap))
2455                                 do_delete = TRUE;
2456                         else
2457                                 do_delete = FALSE;
2458
2459 #ifdef WRAP_DEBUG
2460                         g_print("found CR at %d do_del is %d next line is ",
2461                                cur_pos, do_delete);
2462                         dump_text(text, cur_pos + 1, tlen, 1);
2463 #endif
2464
2465                         /* skip delete if it is continuous URL */
2466                         if (do_delete && (line_pos - p_pos <= i_len) &&
2467                             gtkut_stext_is_uri_string(text, line_pos, tlen))
2468                                 do_delete = FALSE;
2469
2470 #ifdef WRAP_DEBUG
2471                         g_print("l_len=%d wrap_len=%d do_del=%d\n",
2472                                 line_len, linewrap_len, do_delete);
2473 #endif
2474                         /* should we delete to perform smart wrapping */
2475                         if (line_len < linewrap_len && do_delete) {
2476                                 STEXT_FREEZE();
2477                                 /* get rid of newline */
2478                                 gtk_stext_set_point(text, cur_pos);
2479                                 gtk_stext_forward_delete(text, 1);
2480                                 tlen--;
2481
2482                                 /* if text starts with quote fmt or with
2483                                    indent string, delete them */
2484                                 if (i_len) {
2485                                         guint ilen;
2486                                         ilen =  gtkut_stext_str_compare_n
2487                                                 (text, cur_pos, p_pos, i_len,
2488                                                  tlen);
2489                                         if (ilen) {
2490                                                 gtk_stext_forward_delete
2491                                                         (text, ilen);
2492                                                 tlen -= ilen;
2493                                         }
2494                                 }
2495
2496                                 GET_CHAR(cur_pos, cb, clen);
2497
2498                                 /* insert space if it's alphanumeric */
2499                                 if ((cur_pos != line_pos) &&
2500                                     ((clen > 1) || isalnum(cb[0]))) {
2501                                         gtk_stext_insert(text, NULL, NULL,
2502                                                         NULL, " ", 1);
2503                                         tlen++;
2504                                 }
2505
2506                                 /* and start over with current line */
2507                                 cur_pos = p_pos - 1;
2508                                 line_pos = cur_pos;
2509                                 line_len = cur_len = 0;
2510                                 do_delete = FALSE;
2511                                 is_new_line = TRUE;
2512 #ifdef WRAP_DEBUG
2513                                 g_print("after delete l_pos=");
2514                                 dump_text(text, line_pos, tlen, 1);
2515 #endif
2516                                 continue;
2517                         }
2518
2519                         /* mark new line beginning */
2520                         line_pos = cur_pos + 1;
2521                         line_len = cur_len = 0;
2522                         do_delete = FALSE;
2523                         is_new_line = TRUE;
2524                         continue;
2525                 }
2526
2527                 if (ch_len < 0) {
2528                         cbuf[0] = '\0';
2529                         ch_len = 1;
2530                 }
2531
2532                 /* possible line break */
2533                 if (ch_len == 1 && isspace(*cbuf)) {
2534                         line_pos = cur_pos + 1;
2535                         line_len = cur_len + ch_len;
2536                 }
2537
2538                 /* are we over wrapping length set in preferences ? */
2539                 if (cur_len + ch_len > linewrap_len) {
2540                         gint clen;
2541
2542 #ifdef WRAP_DEBUG
2543                         g_print("should wrap cur_pos=%d ", cur_pos);
2544                         dump_text(text, p_pos, tlen, 1);
2545                         dump_text(text, line_pos, tlen, 1);
2546 #endif
2547                         /* force wrapping if it is one long word but not URL */
2548                         if (line_pos - p_pos <= i_len)
2549                                 if (!gtkut_stext_is_uri_string
2550                                     (text, line_pos, tlen))
2551                                         line_pos = cur_pos - 1;
2552 #ifdef WRAP_DEBUG
2553                         g_print("new line_pos=%d\n", line_pos);
2554 #endif
2555
2556                         GET_CHAR(line_pos - 1, cbuf, clen);
2557
2558                         /* if next character is space delete it */
2559                         if (clen == 1 && isspace(*cbuf)) {
2560                                 if (p_pos + i_len != line_pos ||
2561                                     !gtkut_stext_is_uri_string
2562                                         (text, line_pos, tlen)) {
2563                                         STEXT_FREEZE();
2564                                         /* workaround for correct cursor
2565                                            position */
2566                                         if (set_editable_pos == FALSE) {
2567                                                 editable_pos = gtk_editable_get_position(GTK_EDITABLE(text));
2568                                                 if (editable_pos == line_pos)
2569                                                         set_editable_pos = TRUE;
2570                                         }
2571                                         gtk_stext_set_point(text, line_pos);
2572                                         gtk_stext_backward_delete(text, 1);
2573                                         tlen--;
2574                                         cur_pos--;
2575                                         line_pos--;
2576                                         cur_len--;
2577                                         line_len--;
2578                                 }
2579                         }
2580
2581                         /* if it is URL at beginning of line don't wrap */
2582                         if (p_pos + i_len == line_pos &&
2583                             gtkut_stext_is_uri_string(text, line_pos, tlen)) {
2584 #ifdef WRAP_DEBUG
2585                                 g_print("found URL at ");
2586                                 dump_text(text, line_pos, tlen, 1);
2587 #endif
2588                                 continue;
2589                         }
2590
2591                         /* insert CR */
2592                         STEXT_FREEZE();
2593                         gtk_stext_set_point(text, line_pos);
2594                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2595                         /* gtk_stext_compact_buffer(text); */
2596                         tlen++;
2597                         line_pos++;
2598                         /* for loop will increase it */
2599                         cur_pos = line_pos - 1;
2600                         /* start over with current line */
2601                         is_new_line = TRUE;
2602                         line_len = cur_len = 0;
2603                         if (autowrap || i_len > 0)
2604                                 do_delete = TRUE;
2605                         else
2606                                 do_delete = FALSE;
2607 #ifdef WRAP_DEBUG
2608                         g_print("after CR insert ");
2609                         dump_text(text, line_pos, tlen, 1);
2610                         dump_text(text, cur_pos, tlen, 1);
2611 #endif
2612
2613                         /* should we insert quotation ? */
2614                         if (linewrap_quote && i_len) {
2615                                 /* only if line is not already quoted  */
2616                                 if (!gtkut_stext_str_compare
2617                                         (text, line_pos, tlen, qfmt)) {
2618                                         guint ins_len;
2619
2620                                         if (line_pos - p_pos > i_len) {
2621                                                 ins_len = ins_quote
2622                                                         (text, i_len, p_pos,
2623                                                          tlen, qfmt);
2624                                                 tlen += ins_len;
2625                                         }
2626 #ifdef WRAP_DEBUG
2627                                         g_print("after quote insert ");
2628                                         dump_text(text, line_pos, tlen, 1);
2629 #endif
2630                                 }
2631                         }
2632                         continue;
2633                 }
2634
2635                 if (ch_len > 1) {
2636                         line_pos = cur_pos + 1;
2637                         line_len = cur_len + ch_len;
2638                 }
2639                 /* advance to next character in buffer */
2640                 cur_len += ch_len;
2641         }
2642
2643         if (frozen)
2644                 gtk_stext_thaw(text);
2645
2646         if (set_editable_pos && editable_pos <= tlen)
2647                 gtk_editable_set_position(GTK_EDITABLE(text), editable_pos);
2648 }
2649
2650 #undef STEXT_FREEZE
2651 #undef GET_CHAR
2652
2653 static void compose_set_title(Compose *compose)
2654 {
2655         gchar *str;
2656         gchar *edited;
2657
2658         edited = compose->modified ? _(" [Edited]") : "";
2659         if (compose->account && compose->account->address)
2660                 str = g_strdup_printf(_("%s - Compose message%s"),
2661                                       compose->account->address, edited);
2662         else
2663                 str = g_strdup_printf(_("Compose message%s"), edited);
2664         gtk_window_set_title(GTK_WINDOW(compose->window), str);
2665         g_free(str);
2666 }
2667
2668 /**
2669  * compose_current_mail_account:
2670  * 
2671  * Find a current mail account (the currently selected account, or the
2672  * default account, if a news account is currently selected).  If a
2673  * mail account cannot be found, display an error message.
2674  * 
2675  * Return value: Mail account, or NULL if not found.
2676  **/
2677 static PrefsAccount *
2678 compose_current_mail_account(void)
2679 {
2680         PrefsAccount *ac;
2681
2682         if (cur_account && cur_account->protocol != A_NNTP)
2683                 ac = cur_account;
2684         else {
2685                 ac = account_get_default();
2686                 if (!ac || ac->protocol == A_NNTP) {
2687                         alertpanel_error(_("Account for sending mail is not specified.\n"
2688                                            "Please select a mail account before sending."));
2689                         return NULL;
2690                 }
2691         }
2692         return ac;
2693 }
2694
2695 static void compose_select_account(Compose *compose, PrefsAccount *account)
2696 {
2697         GtkWidget *menuitem;
2698         GtkItemFactory *ifactory;
2699
2700         g_return_if_fail(account != NULL);
2701
2702         compose->account = account;
2703
2704         compose_set_title(compose);
2705
2706         ifactory = gtk_item_factory_from_widget(compose->menubar);
2707 #if 0
2708         if (account->protocol == A_NNTP) {
2709                 gtk_widget_show(compose->newsgroups_hbox);
2710                 gtk_widget_show(compose->newsgroups_entry);
2711                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 4);
2712
2713                 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
2714                 gtk_check_menu_item_set_active
2715                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2716                 gtk_widget_set_sensitive(menuitem, TRUE);
2717                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2718                 gtk_check_menu_item_set_active
2719                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2720                 gtk_widget_set_sensitive(menuitem, TRUE);
2721
2722                 menu_set_sensitive(ifactory, "/View/Followup to", TRUE);
2723         } else {
2724                 gtk_widget_hide(compose->newsgroups_hbox);
2725                 gtk_widget_hide(compose->newsgroups_entry);
2726                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 0);
2727                 gtk_widget_queue_resize(compose->table_vbox);
2728
2729                 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
2730                 gtk_check_menu_item_set_active
2731                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2732                 gtk_widget_set_sensitive(menuitem, FALSE);
2733                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2734                 gtk_check_menu_item_set_active
2735                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2736                 gtk_widget_set_sensitive(menuitem, FALSE);
2737
2738                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Followup to");
2739                 gtk_check_menu_item_set_active
2740                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2741                 gtk_widget_set_sensitive(menuitem, FALSE);
2742         }
2743
2744         if (account->set_autocc && account->auto_cc &&
2745             compose->mode != COMPOSE_REEDIT) {
2746                 gtk_entry_set_text
2747                         (GTK_ENTRY(compose->cc_entry), account->auto_cc);
2748                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2749                 gtk_check_menu_item_set_active
2750                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2751         }
2752         if (account->set_autobcc) {
2753                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Bcc");
2754                 gtk_check_menu_item_set_active
2755                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2756                 if (account->auto_bcc && compose->mode != COMPOSE_REEDIT)
2757                         gtk_entry_set_text(GTK_ENTRY(compose->bcc_entry),
2758                                            account->auto_bcc);
2759         }
2760         if (account->set_autoreplyto) {
2761                 menuitem = gtk_item_factory_get_item(ifactory,
2762                                                      "/View/Reply to");
2763                 gtk_check_menu_item_set_active
2764                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2765                 if (account->auto_replyto && compose->mode != COMPOSE_REEDIT)
2766                         gtk_entry_set_text(GTK_ENTRY(compose->reply_entry),
2767                                            account->auto_replyto);
2768         }
2769
2770         menuitem = gtk_item_factory_get_item(ifactory, "/View/Ruler");
2771         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2772                                        prefs_common.show_ruler);
2773 #endif
2774
2775 #if USE_GPGME
2776         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
2777         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2778                                        account->default_sign);
2779         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
2780         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2781                                        account->default_encrypt);
2782 #endif /* USE_GPGME */
2783 }
2784
2785 gboolean compose_check_for_valid_recipient(Compose *compose) {
2786         gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
2787         gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
2788         gboolean recipient_found = FALSE;
2789         GSList *list;
2790         gchar **strptr;
2791
2792         /* free to and newsgroup list */
2793         slist_free_strings(compose->to_list);
2794         g_slist_free(compose->to_list);
2795         compose->to_list = NULL;
2796                         
2797         slist_free_strings(compose->newsgroup_list);
2798         g_slist_free(compose->newsgroup_list);
2799         compose->newsgroup_list = NULL;
2800
2801         /* search header entries for to and newsgroup entries */
2802         for (list = compose->header_list; list; list = list->next) {
2803                 gchar *header;
2804                 gchar *entry;
2805                 header = gtk_editable_get_chars(GTK_EDITABLE(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry), 0, -1);
2806                 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
2807                 g_strstrip(entry);
2808                 if (entry[0] != '\0') {
2809                         for (strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
2810                                 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2811                                         compose->to_list = address_list_append(compose->to_list, entry);
2812                                         recipient_found = TRUE;
2813                                 }
2814                         }
2815                         for (strptr = recipient_headers_news; *strptr != NULL; strptr++) {
2816                                 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2817                                         compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
2818                                         recipient_found = TRUE;
2819                                 }
2820                         }
2821                 }
2822                 g_free(header);
2823                 g_free(entry);
2824         }
2825         return recipient_found;
2826 }
2827
2828 static gboolean compose_check_entries(Compose *compose, gboolean check_subject)
2829 {
2830         gchar *str;
2831
2832         if (compose_check_for_valid_recipient(compose) == FALSE) {
2833                 alertpanel_error(_("Recipient is not specified."));
2834                 return FALSE;
2835         }
2836
2837         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
2838         if (*str == '\0' && check_subject == TRUE) {
2839                 AlertValue aval;
2840
2841                 aval = alertpanel(_("Send"),
2842                                   _("Subject is empty. Send it anyway?"),
2843                                   _("Yes"), _("No"), NULL);
2844                 if (aval != G_ALERTDEFAULT)
2845                         return FALSE;
2846         }
2847
2848         return TRUE;
2849 }
2850
2851 gint compose_send(Compose *compose)
2852 {
2853         gint msgnum;
2854         FolderItem *folder;
2855         gint val;
2856         gchar *msgpath;
2857
2858         if (compose_check_entries(compose, TRUE) == FALSE)
2859                 return -1;
2860
2861         val = compose_queue(compose, &msgnum, &folder);
2862         if (val) {
2863                 alertpanel_error(_("Could not queue message for sending"));
2864                 return -1;
2865         }
2866         
2867         msgpath = folder_item_fetch_msg(folder, msgnum);
2868         val = procmsg_send_message_queue(msgpath);
2869         g_free(msgpath);
2870
2871         folder_item_remove_msg(folder, msgnum);
2872         folderview_update_item(folder, TRUE);
2873
2874         return val;
2875 }
2876
2877 #if 0 /* compose restructure */
2878 gint compose_send(Compose *compose)
2879 {
2880         gchar tmp[MAXPATHLEN + 1];
2881         gint ok = 0;
2882         static gboolean lock = FALSE;
2883
2884         if (lock) return 1;
2885
2886         g_return_val_if_fail(compose->account != NULL, -1);
2887         g_return_val_if_fail(compose->orig_account != NULL, -1);
2888
2889         lock = TRUE;
2890
2891         if (compose_check_entries(compose, TRUE) == FALSE) {
2892                 lock = FALSE;
2893                 return 1;
2894         }
2895
2896         /* write to temporary file */
2897         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
2898                    get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
2899
2900         if (prefs_common.linewrap_at_send)
2901                 compose_wrap_line_all(compose);
2902
2903         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
2904                 lock = FALSE;
2905                 return -1;
2906         }
2907
2908         if (!compose->to_list && !compose->newsgroup_list) {
2909                 g_warning(_("can't get recipient list."));
2910                 unlink(tmp);
2911                 lock = FALSE;
2912                 return -1;
2913         }
2914
2915         if (compose->to_list) {
2916                 PrefsAccount *ac;
2917
2918 #if 0 /* NEW COMPOSE GUI */
2919                 if (compose->account->protocol != A_NNTP)
2920                         ac = compose->account;
2921                 else if (compose->orig_account->protocol != A_NNTP)
2922                         ac = compose->orig_account;
2923                 else if (cur_account && cur_account->protocol != A_NNTP)
2924                         ac = cur_account;
2925                 else {
2926                         ac = compose_current_mail_account();
2927                         if (!ac) {
2928                                 unlink(tmp);
2929                                 lock = FALSE;
2930                                 return -1;
2931                         }
2932                 }
2933 #endif
2934                 ac = compose->account;
2935
2936                 ok = send_message(tmp, ac, compose->to_list);
2937                 statusbar_pop_all();
2938         }
2939
2940         if (ok == 0 && compose->newsgroup_list) {
2941                 Folder *folder;
2942
2943                 if (compose->account->protocol == A_NNTP)
2944                         folder = FOLDER(compose->account->folder);
2945                 else
2946                         folder = FOLDER(compose->orig_account->folder);
2947
2948                 ok = news_post(folder, tmp);
2949                 if (ok < 0) {
2950                         alertpanel_error(_("Error occurred while posting the message to %s ."),
2951                                          compose->account->nntp_server);
2952                         unlink(tmp);
2953                         lock = FALSE;
2954                         return -1;
2955                 }
2956         }
2957
2958         /* queue message if failed to send */
2959         if (ok < 0) {
2960                 if (prefs_common.queue_msg) {
2961                         AlertValue val;
2962
2963                         val = alertpanel
2964                                 (_("Queueing"),
2965                                  _("Error occurred while sending the message.\n"
2966                                    "Put this message into queue folder?"),
2967                                  _("OK"), _("Cancel"), NULL);
2968                         if (G_ALERTDEFAULT == val) {
2969                                 ok = compose_queue(compose, tmp);
2970                                 if (ok < 0)
2971                                         alertpanel_error(_("Can't queue the message."));
2972                         }
2973                 } else
2974                         alertpanel_error(_("Error occurred while sending the message."));
2975         } else {
2976                 if (compose->mode == COMPOSE_REEDIT) {
2977                         compose_remove_reedit_target(compose);
2978                         if (compose->targetinfo)
2979                                 folderview_update_item
2980                                         (compose->targetinfo->folder, TRUE);
2981                 }
2982                 /* save message to outbox */
2983                 if (prefs_common.savemsg) {
2984                         FolderItem *outbox;
2985
2986                         outbox = account_get_special_folder
2987                                 (compose->account, F_OUTBOX);
2988                         if (procmsg_save_to_outbox(outbox, tmp, FALSE) < 0)
2989                                 alertpanel_error
2990                                         (_("Can't save the message to Sent."));
2991                         else
2992                                 folderview_update_item(outbox, TRUE);
2993                 }
2994         }
2995
2996         unlink(tmp);
2997         lock = FALSE;
2998         return ok;
2999 }
3000 #endif
3001
3002 static gboolean compose_use_attach(Compose *compose) {
3003     return(gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL);
3004 }
3005
3006 static gint compose_redirect_write_headers_from_headerlist(Compose *compose, 
3007                                                            FILE *fp)
3008 {
3009         gchar buf[BUFFSIZE];
3010         gchar *str;
3011         gboolean first_address;
3012         GSList *list;
3013         ComposeHeaderEntry *headerentry;
3014         gchar *headerentryname;
3015         gchar *cc_hdr;
3016         gchar *to_hdr;
3017
3018         debug_print("Writing redirect header\n");
3019
3020         cc_hdr = prefs_common.trans_hdr ? _("Cc:") : "Cc:";
3021         to_hdr = prefs_common.trans_hdr ? _("To:") : "To:";
3022
3023         first_address = TRUE;
3024         for (list = compose->header_list; list; list = list->next) {
3025                 headerentry = ((ComposeHeaderEntry *)list->data);
3026                 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
3027
3028                 if (g_strcasecmp(headerentryname, cc_hdr) == 0 
3029                    || g_strcasecmp(headerentryname, to_hdr) == 0) {
3030                         str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
3031                         Xstrdup_a(str, str, return -1);
3032                         g_strstrip(str);
3033                         if (str[0] != '\0') {
3034                                 compose_convert_header
3035                                         (buf, sizeof(buf), str,
3036                                         strlen("Resent-To") + 2);
3037                                 if (first_address) {
3038                                         fprintf(fp, "Resent-To: ");
3039                                         first_address = FALSE;
3040                                 } else {
3041                                         fprintf(fp, ",");
3042                                 }
3043                                 fprintf(fp, "%s", buf);
3044                         }
3045                 }
3046         }
3047         /* if (!first_address) { */
3048         fprintf(fp, "\n");
3049         /* } */
3050
3051         return(0);
3052 }
3053
3054 static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
3055 {
3056         gchar buf[BUFFSIZE];
3057         gchar *str;
3058         /* struct utsname utsbuf; */
3059
3060         g_return_val_if_fail(fp != NULL, -1);
3061         g_return_val_if_fail(compose->account != NULL, -1);
3062         g_return_val_if_fail(compose->account->address != NULL, -1);
3063
3064         /* Resent-Date */
3065         get_rfc822_date(buf, sizeof(buf));
3066         fprintf(fp, "Resent-Date: %s\n", buf);
3067
3068         /* Resent-From */
3069         if (compose->account->name && *compose->account->name) {
3070                 compose_convert_header
3071                         (buf, sizeof(buf), compose->account->name,
3072                          strlen("From: "));
3073                 fprintf(fp, "Resent-From: %s <%s>\n",
3074                         buf, compose->account->address);
3075         } else
3076                 fprintf(fp, "Resent-From: %s\n", compose->account->address);
3077
3078         /* Subject */
3079         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
3080         if (*str != '\0') {
3081                 Xstrdup_a(str, str, return -1);
3082                 g_strstrip(str);
3083                 if (*str != '\0') {
3084                         compose_convert_header(buf, sizeof(buf), str,
3085                                                strlen("Subject: "));
3086                         fprintf(fp, "Subject: %s\n", buf);
3087                 }
3088         }
3089
3090         /* Resent-Message-ID */
3091         if (compose->account->gen_msgid) {
3092                 compose_generate_msgid(compose, buf, sizeof(buf));
3093                 fprintf(fp, "Resent-Message-Id: <%s>\n", buf);
3094                 compose->msgid = g_strdup(buf);
3095         }
3096
3097         compose_redirect_write_headers_from_headerlist(compose, fp);
3098
3099         /* separator between header and body */
3100         fputs("\n", fp);
3101
3102         return 0;
3103 }
3104
3105 static gint compose_redirect_write_to_file(Compose *compose, const gchar *file)
3106 {
3107         FILE *fp;
3108         FILE *fdest;
3109         size_t len;
3110         gchar buf[BUFFSIZE];
3111
3112         if ((fp = fopen(compose->redirect_filename, "rb")) == NULL) {
3113                 FILE_OP_ERROR(file, "fopen");
3114                 return -1;
3115         }
3116
3117         if ((fdest = fopen(file, "wb")) == NULL) {
3118                 FILE_OP_ERROR(file, "fopen");
3119                 fclose(fp);
3120                 return -1;
3121         }
3122
3123         /* chmod for security */
3124         if (change_file_mode_rw(fdest, file) < 0) {
3125                 FILE_OP_ERROR(file, "chmod");
3126                 g_warning(_("can't change file mode\n"));
3127         }
3128
3129         while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
3130                 /* should filter returnpath, delivered-to */
3131                 if (g_strncasecmp(buf, "Return-Path:",
3132                                    strlen("Return-Path:")) == 0 ||
3133                     g_strncasecmp(buf, "Delivered-To:",
3134                                   strlen("Delivered-To:")) == 0 ||
3135                     g_strncasecmp(buf, "Received:",
3136                                   strlen("Received:")) == 0 ||
3137                     g_strncasecmp(buf, "Subject:",
3138                                   strlen("Subject:")) == 0 ||
3139                     g_strncasecmp(buf, "X-UIDL:",
3140                                   strlen("X-UIDL:")) == 0)
3141                         continue;
3142
3143                 if (fputs(buf, fdest) == -1)
3144                         goto error;
3145
3146                 if (!prefs_common.redirect_keep_from) {
3147                         if (g_strncasecmp(buf, "From:",
3148                                           strlen("From:")) == 0) {
3149                                 fputs(" (by way of ", fdest);
3150                                 if (compose->account->name
3151                                     && *compose->account->name) {
3152                                         compose_convert_header
3153                                                 (buf, sizeof(buf),
3154                                                  compose->account->name,
3155                                                  strlen("From: "));
3156                                         fprintf(fdest, "%s <%s>",
3157                                                 buf,
3158                                                 compose->account->address);
3159                                 } else
3160                                         fprintf(fdest, "%s",
3161                                                 compose->account->address);
3162                                 fputs(")", fdest);
3163                         }
3164                 }
3165
3166                 if (fputs("\n", fdest) == -1)
3167                         goto error;
3168         }
3169
3170         compose_redirect_write_headers(compose, fdest);
3171
3172         while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
3173                 if (fwrite(buf, sizeof(gchar), len, fdest) != len) {
3174                         FILE_OP_ERROR(file, "fwrite");
3175                         goto error;
3176                 }
3177         }
3178
3179         fclose(fp);
3180         if (fclose(fdest) == EOF) {
3181                 FILE_OP_ERROR(file, "fclose");
3182                 unlink(file);
3183                 return -1;
3184         }
3185
3186         return 0;
3187  error:
3188         fclose(fp);
3189         fclose(fdest);
3190         unlink(file);
3191
3192         return -1;
3193 }
3194
3195
3196 #if USE_GPGME
3197 /* interfaces to rfc2015 to keep out the prefs stuff there.
3198  * returns 0 on success and -1 on error. */
3199 static gint compose_create_signers_list(Compose *compose, GSList **pkey_list)
3200 {
3201         const gchar *key_id = NULL;