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