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