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