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