revert last commit
[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 && to_ml && compose->ml_post)
1666                         compose_entry_append(compose,
1667                                            compose->ml_post,
1668                                            COMPOSE_TO);
1669                 else if (!(to_all || ignore_replyto)
1670                          && msginfo->folder
1671                          && msginfo->folder->prefs->enable_default_reply_to) {
1672                         compose_entry_append(compose,
1673                             msginfo->folder->prefs->default_reply_to,
1674                             COMPOSE_TO);
1675                 } else
1676                         compose_entry_append(compose,
1677                                  (compose->replyto && !ignore_replyto)
1678                                  ? compose->replyto
1679                                  : msginfo->from ? msginfo->from : "",
1680                                  COMPOSE_TO);
1681         } else {
1682                 if (ignore_replyto)
1683                         compose_entry_append
1684                                 (compose, msginfo->from ? msginfo->from : "",
1685                                  COMPOSE_TO);
1686                 else {
1687                         if (compose->followup_to && !strncmp(compose->followup_to, "poster", 6)) {
1688                                 compose_entry_append
1689                                         (compose,
1690                                         ((compose->replyto && !ignore_replyto)
1691                                         ? compose->replyto
1692                                         : msginfo->from ? msginfo->from : ""),
1693                                         COMPOSE_TO);                            
1694                         } else {
1695                                 compose_entry_append
1696                                         (compose,
1697                                          compose->followup_to ? compose->followup_to
1698                                          : compose->newsgroups ? compose->newsgroups
1699                                          : "",
1700                                          COMPOSE_NEWSGROUPS);
1701                         }
1702                 }
1703         }
1704
1705         if (msginfo->subject && *msginfo->subject) {
1706                 gchar *buf, *buf2, *p;
1707
1708                 buf = g_strdup(msginfo->subject);
1709                 while (!strncasecmp(buf, "Re:", 3)) {
1710                         p = buf + 3;
1711                         while (isspace(*p)) p++;
1712                         memmove(buf, p, strlen(p) + 1);
1713                 }
1714
1715                 buf2 = g_strdup_printf("Re: %s", buf);
1716                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1717                 g_free(buf2);
1718                 g_free(buf);
1719         } else
1720                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1721
1722         if (to_ml && compose->ml_post) return;
1723         if (!to_all || compose->account->protocol == A_NNTP) return;
1724
1725         if (compose->replyto) {
1726                 Xstrdup_a(replyto, compose->replyto, return);
1727                 extract_address(replyto);
1728         }
1729         if (msginfo->from) {
1730                 Xstrdup_a(from, msginfo->from, return);
1731                 extract_address(from);
1732         }
1733
1734         if (replyto && from)
1735                 cc_list = address_list_append(cc_list, from);
1736         cc_list = address_list_append(cc_list, msginfo->to);
1737         cc_list = address_list_append(cc_list, compose->cc);
1738
1739         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1740         if (replyto)
1741                 g_hash_table_insert(to_table, replyto, GINT_TO_POINTER(1));
1742         if (compose->account)
1743                 g_hash_table_insert(to_table, compose->account->address,
1744                                     GINT_TO_POINTER(1));
1745
1746         /* remove address on To: and that of current account */
1747         for (cur = cc_list; cur != NULL; ) {
1748                 GSList *next = cur->next;
1749
1750                 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1751                         cc_list = g_slist_remove(cc_list, cur->data);
1752                 else
1753                         g_hash_table_insert(to_table, cur->data, cur);
1754
1755                 cur = next;
1756         }
1757         g_hash_table_destroy(to_table);
1758
1759         if (cc_list) {
1760                 for (cur = cc_list; cur != NULL; cur = cur->next)
1761                         compose_entry_append(compose, (gchar *)cur->data,
1762                                              COMPOSE_CC);
1763                 slist_free_strings(cc_list);
1764                 g_slist_free(cc_list);
1765         }
1766 }
1767
1768 #define SET_ENTRY(entry, str) \
1769 { \
1770         if (str && *str) \
1771                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1772 }
1773
1774 #define SET_ADDRESS(type, str) \
1775 { \
1776         if (str && *str) \
1777                 compose_entry_append(compose, str, type); \
1778 }
1779
1780 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1781 {
1782         g_return_if_fail(msginfo != NULL);
1783
1784         SET_ENTRY(subject_entry, msginfo->subject);
1785         SET_ADDRESS(COMPOSE_TO, msginfo->to);
1786         SET_ADDRESS(COMPOSE_CC, compose->cc);
1787         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1788         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1789
1790         compose_update_priority_menu_item(compose);
1791 #if USE_GPGME   
1792         compose_update_sigmode_menu_item(compose);
1793         compose_update_encmode_menu_item(compose);
1794 #endif
1795         compose_show_first_last_header(compose, TRUE);
1796
1797 #if 0 /* NEW COMPOSE GUI */
1798         if (compose->bcc) {
1799                 GtkItemFactory *ifactory;
1800                 GtkWidget *menuitem;
1801
1802                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1803                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Bcc");
1804                 gtk_check_menu_item_set_active
1805                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1806         }
1807         if (compose->replyto) {
1808                 GtkItemFactory *ifactory;
1809                 GtkWidget *menuitem;
1810
1811                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1812                 menuitem = gtk_item_factory_get_item
1813                         (ifactory, "/View/Reply to");
1814                 gtk_check_menu_item_set_active
1815                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1816         }
1817 #endif
1818 }
1819
1820 #undef SET_ENTRY
1821 #undef SET_ADDRESS
1822
1823 static void compose_exec_sig(Compose *compose, gchar *sigfile)
1824 {
1825         FILE  *sigprg;
1826         gchar  *buf;
1827         size_t buf_len = 128;
1828  
1829         if (strlen(sigfile) < 2)
1830           return;
1831  
1832         sigprg = popen(sigfile+1, "r");
1833         if (sigprg) {
1834
1835                 buf = g_malloc(buf_len);
1836
1837                 if (!buf) {
1838                         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1839                         "Unable to insert signature (malloc failed)\n", -1);
1840
1841                         pclose(sigprg);
1842                         return;
1843                 }
1844
1845                 while (!feof(sigprg)) {
1846                         bzero(buf, buf_len);
1847                         fread(buf, buf_len-1, 1, sigprg);
1848                         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
1849                 }
1850
1851                 g_free(buf);
1852                 pclose(sigprg);
1853         }
1854         else
1855         {
1856                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1857                 "Can't exec file: ", -1);
1858                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1859                 sigfile+1, -1);
1860         }
1861 }
1862
1863 static void compose_insert_sig(Compose *compose)
1864 {
1865         gchar *sigfile;
1866
1867         if (compose->account && compose->account->sig_path)
1868                 sigfile = g_strdup(compose->account->sig_path);
1869         else
1870                 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1871                                       DEFAULT_SIGNATURE, NULL);
1872
1873         if (!is_file_or_fifo_exist(sigfile) && sigfile[0] != '|') {
1874                 g_free(sigfile);
1875                 return;
1876         }
1877
1878         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1879         if (prefs_common.sig_sep) {
1880                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1881                                 prefs_common.sig_sep, -1);
1882                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1883                                 "\n", 1);
1884         }
1885
1886         if (sigfile[0] == '|')
1887                 compose_exec_sig(compose, sigfile);
1888         else
1889                 compose_insert_file(compose, sigfile);
1890         g_free(sigfile);
1891 }
1892
1893 static void compose_insert_file(Compose *compose, const gchar *file)
1894 {
1895         GtkSText *text = GTK_STEXT(compose->text);
1896         gchar buf[BUFFSIZE];
1897         gint len;
1898         FILE *fp;
1899
1900         g_return_if_fail(file != NULL);
1901
1902         if ((fp = fopen(file, "rb")) == NULL) {
1903                 FILE_OP_ERROR(file, "fopen");
1904                 return;
1905         }
1906
1907         gtk_stext_freeze(text);
1908
1909         while (fgets(buf, sizeof(buf), fp) != NULL) {
1910                 /* strip <CR> if DOS/Windows file,
1911                    replace <CR> with <LF> if Macintosh file. */
1912                 strcrchomp(buf);
1913                 len = strlen(buf);
1914                 if (len > 0 && buf[len - 1] != '\n') {
1915                         while (--len >= 0)
1916                                 if (buf[len] == '\r') buf[len] = '\n';
1917                 }
1918                 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1919         }
1920
1921         gtk_stext_thaw(text);
1922
1923         fclose(fp);
1924 }
1925
1926 static void compose_attach_append(Compose *compose, const gchar *file,
1927                                   const gchar *filename,
1928                                   const gchar *content_type)
1929 {
1930         AttachInfo *ainfo;
1931         gchar *text[N_ATTACH_COLS];
1932         FILE *fp;
1933         off_t size;
1934         gint row;
1935
1936         if (!is_file_exist(file)) {
1937                 g_warning(_("File %s doesn't exist\n"), file);
1938                 return;
1939         }
1940         if ((size = get_file_size(file)) < 0) {
1941                 g_warning(_("Can't get file size of %s\n"), file);
1942                 return;
1943         }
1944         if (size == 0) {
1945                 alertpanel_notice(_("File %s is empty."), file);
1946                 return;
1947         }
1948         if ((fp = fopen(file, "rb")) == NULL) {
1949                 alertpanel_error(_("Can't read %s."), file);
1950                 return;
1951         }
1952         fclose(fp);
1953
1954 #if 0 /* NEW COMPOSE GUI */
1955         if (!compose->use_attach) {
1956                 GtkItemFactory *ifactory;
1957                 GtkWidget *menuitem;
1958
1959                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1960                 menuitem = gtk_item_factory_get_item(ifactory,
1961                                                      "/View/Attachment");
1962                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1963                                                TRUE);
1964         }
1965 #endif
1966         ainfo = g_new0(AttachInfo, 1);
1967         ainfo->file = g_strdup(file);
1968
1969         if (content_type) {
1970                 ainfo->content_type = g_strdup(content_type);
1971                 if (!strcasecmp(content_type, "message/rfc822")) {
1972                         ainfo->encoding = ENC_7BIT;
1973                         ainfo->name = g_strdup_printf
1974                                 (_("Message: %s"),
1975                                  g_basename(filename ? filename : file));
1976                 } else {
1977                         if (!g_strncasecmp(content_type, "text", 4))
1978                                 ainfo->encoding =
1979                                         procmime_get_encoding_for_file(file);
1980                         else
1981                                 ainfo->encoding = ENC_BASE64;
1982                         ainfo->name = g_strdup
1983                                 (g_basename(filename ? filename : file));
1984                 }
1985         } else {
1986                 ainfo->content_type = procmime_get_mime_type(file);
1987                 if (!ainfo->content_type) {
1988                         ainfo->content_type =
1989                                 g_strdup("application/octet-stream");
1990                         ainfo->encoding = ENC_BASE64;
1991                 } else if (!g_strncasecmp(ainfo->content_type, "text", 4))
1992                         ainfo->encoding = procmime_get_encoding_for_file(file);
1993                 else
1994                         ainfo->encoding = ENC_BASE64;
1995                 ainfo->name = g_strdup(g_basename(filename ? filename : file)); 
1996         }
1997         ainfo->size = size;
1998
1999         text[COL_MIMETYPE] = ainfo->content_type;
2000         text[COL_SIZE] = to_human_readable(size);
2001         text[COL_NAME] = ainfo->name;
2002
2003         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
2004         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
2005 }
2006
2007 #define IS_FIRST_PART_TEXT(info) \
2008         ((info->mime_type == MIME_TEXT || info->mime_type == MIME_TEXT_HTML || \
2009           info->mime_type == MIME_TEXT_ENRICHED) || \
2010          (info->mime_type == MIME_MULTIPART && info->content_type && \
2011           !strcasecmp(info->content_type, "multipart/alternative") && \
2012           (info->children && \
2013            (info->children->mime_type == MIME_TEXT || \
2014             info->children->mime_type == MIME_TEXT_HTML || \
2015             info->children->mime_type == MIME_TEXT_ENRICHED))))
2016
2017 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
2018 {
2019         MimeInfo *mimeinfo;
2020         MimeInfo *child;
2021         gchar *infile;
2022         gchar *outfile;
2023
2024         mimeinfo = procmime_scan_message(msginfo);
2025         if (!mimeinfo) return;
2026
2027         /* skip first text (presumably message body) */
2028         child = mimeinfo->children;
2029         if (!child || IS_FIRST_PART_TEXT(mimeinfo)) {
2030                 procmime_mimeinfo_free_all(mimeinfo);
2031                 return;
2032         }
2033
2034         if (IS_FIRST_PART_TEXT(child))
2035                 child = child->next;
2036
2037         infile = procmsg_get_message_file_path(msginfo);
2038
2039         while (child != NULL) {
2040                 if (child->children || child->mime_type == MIME_MULTIPART) {
2041                         child = procmime_mimeinfo_next(child);
2042                         continue;
2043                 }
2044                 if(child->parent && child->parent->parent
2045                 && !strcasecmp(child->parent->parent->content_type, "multipart/signed")
2046                 && child->mime_type == MIME_TEXT) {
2047                         /* this is the main text part of a signed message */
2048                         child = procmime_mimeinfo_next(child);
2049                         continue;
2050                 }
2051                 outfile = procmime_get_tmp_file_name(child);
2052                 if (procmime_get_part(outfile, infile, child) < 0)
2053                         g_warning(_("Can't get the part of multipart message."));
2054                 else if (compose->mode != COMPOSE_REEDIT || strcmp(child->content_type, "application/pgp-signature"))
2055                         compose_attach_append
2056                                 (compose, outfile,
2057                                  child->filename ? child->filename : child->name,
2058                                  child->content_type);
2059
2060                 child = child->next;
2061         }
2062
2063         g_free(infile);
2064         procmime_mimeinfo_free_all(mimeinfo);
2065 }
2066
2067 #undef IS_FIRST_PART_TEXT
2068
2069 #define GET_CHAR(pos, buf, len)                                              \
2070 {                                                                            \
2071         if (text->use_wchar)                                                 \
2072                 len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos)));    \
2073         else {                                                               \
2074                 buf[0] = GTK_STEXT_INDEX(text, (pos));                       \
2075                 len = 1;                                                     \
2076         }                                                                    \
2077 }
2078
2079 #define INDENT_CHARS    ">|#"
2080 #define SPACE_CHARS     " \t"
2081
2082 static void compose_wrap_line(Compose *compose)
2083 {
2084         GtkSText *text = GTK_STEXT(compose->text);
2085         gint ch_len, last_ch_len;
2086         gchar cbuf[MB_LEN_MAX], last_ch;
2087         guint text_len;
2088         guint line_end;
2089         guint quoted;
2090         gint p_start, p_end;
2091         gint line_pos, cur_pos;
2092         gint line_len, cur_len;
2093
2094         gtk_stext_freeze(text);
2095
2096         text_len = gtk_stext_get_length(text);
2097
2098         /* check to see if the point is on the paragraph mark (empty line). */
2099         cur_pos = gtk_stext_get_point(text);
2100         GET_CHAR(cur_pos, cbuf, ch_len);
2101         if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
2102                 if (cur_pos == 0)
2103                         goto compose_end; /* on the paragraph mark */
2104                 GET_CHAR(cur_pos - 1, cbuf, ch_len);
2105                 if (ch_len == 1 && *cbuf == '\n')
2106                         goto compose_end; /* on the paragraph mark */
2107         }
2108
2109         /* find paragraph start. */
2110         line_end = quoted = 0;
2111         for (p_start = cur_pos; p_start >= 0; --p_start) {
2112                 GET_CHAR(p_start, cbuf, ch_len);
2113                 if (ch_len == 1 && *cbuf == '\n') {
2114                         if (quoted)
2115                                 goto compose_end; /* quoted part */
2116                         if (line_end) {
2117                                 p_start += 2;
2118                                 break;
2119                         }
2120                         line_end = 1;
2121                 } else {
2122                         if (ch_len == 1 
2123                             && strchr(prefs_common.quote_chars, *cbuf))
2124                                 quoted = 1;
2125                         else if (ch_len != 1 || !isspace(*cbuf))
2126                                 quoted = 0;
2127
2128                         line_end = 0;
2129                 }
2130         }
2131         if (p_start < 0)
2132                 p_start = 0;
2133
2134         /* find paragraph end. */
2135         line_end = 0;
2136         for (p_end = cur_pos; p_end < text_len; p_end++) {
2137                 GET_CHAR(p_end, cbuf, ch_len);
2138                 if (ch_len == 1 && *cbuf == '\n') {
2139                         if (line_end) {
2140                                 p_end -= 1;
2141                                 break;
2142                         }
2143                         line_end = 1;
2144                 } else {
2145                         if (line_end && ch_len == 1 &&
2146                             strchr(prefs_common.quote_chars, *cbuf))
2147                                 goto compose_end; /* quoted part */
2148
2149                         line_end = 0;
2150                 }
2151         }
2152         if (p_end >= text_len)
2153                 p_end = text_len;
2154
2155         if (p_start >= p_end)
2156                 goto compose_end;
2157
2158         line_len = cur_len = 0;
2159         last_ch_len = 0;
2160         last_ch = '\0';
2161         line_pos = p_start;
2162         for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2163                 guint space = 0;
2164
2165                 GET_CHAR(cur_pos, cbuf, ch_len);
2166
2167                 if (ch_len < 0) {
2168                         cbuf[0] = '\0';
2169                         ch_len = 1;
2170                 }
2171
2172                 if (ch_len == 1 && isspace(*cbuf))
2173                         space = 1;
2174
2175                 if (ch_len == 1 && *cbuf == '\n') {
2176                         guint replace = 0;
2177                         if (last_ch_len == 1 && !isspace(last_ch)) {
2178                                 if (cur_pos + 1 < p_end) {
2179                                         GET_CHAR(cur_pos + 1, cbuf, ch_len);
2180                                         if (ch_len == 1 && !isspace(*cbuf))
2181                                                 replace = 1;
2182                                 }
2183                         }
2184                         gtk_stext_set_point(text, cur_pos + 1);
2185                         gtk_stext_backward_delete(text, 1);
2186                         if (replace) {
2187                                 gtk_stext_set_point(text, cur_pos);
2188                                 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2189                                 space = 1;
2190                         }
2191                         else {
2192                                 p_end--;
2193                                 cur_pos--;
2194                                 continue;
2195                         }
2196                 }
2197
2198                 last_ch_len = ch_len;
2199                 last_ch = *cbuf;
2200
2201                 if (space) {
2202                         line_pos = cur_pos + 1;
2203                         line_len = cur_len + ch_len;
2204                 }
2205
2206                 if (cur_len + ch_len > prefs_common.linewrap_len &&
2207                     line_len > 0) {
2208                         gint tlen = ch_len;
2209
2210                         GET_CHAR(line_pos - 1, cbuf, ch_len);
2211                         if (ch_len == 1 && isspace(*cbuf)) {
2212                                 gtk_stext_set_point(text, line_pos);
2213                                 gtk_stext_backward_delete(text, 1);
2214                                 p_end--;
2215                                 cur_pos--;
2216                                 line_pos--;
2217                                 cur_len--;
2218                                 line_len--;
2219                         }
2220                         ch_len = tlen;
2221
2222                         gtk_stext_set_point(text, line_pos);
2223                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2224                         p_end++;
2225                         cur_pos++;
2226                         line_pos++;
2227                         cur_len = cur_len - line_len + ch_len;
2228                         line_len = 0;
2229                         continue;
2230                 }
2231
2232                 if (ch_len > 1) {
2233                         line_pos = cur_pos + 1;
2234                         line_len = cur_len + ch_len;
2235                 }
2236                 cur_len += ch_len;
2237         }
2238
2239 compose_end:
2240         gtk_stext_thaw(text);
2241 }
2242
2243 #undef WRAP_DEBUG
2244 #ifdef WRAP_DEBUG
2245 /* Darko: used when I debug wrapping */
2246 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2247 {
2248         gint i, clen;
2249         gchar cbuf[MB_LEN_MAX];
2250
2251         printf("%d [", pos);
2252         for (i = pos; i < tlen; i++) {
2253                 GET_CHAR(i, cbuf, clen);
2254                 if (clen < 0) break;
2255                 if (breakoncr && clen == 1 && cbuf[0] == '\n')
2256                         break;
2257                 fwrite(cbuf, clen, 1, stdout);
2258         }
2259         printf("]\n");
2260 }
2261 #endif
2262
2263 typedef enum {
2264         WAIT_FOR_SPACE,
2265         WAIT_FOR_INDENT_CHAR,
2266         WAIT_FOR_INDENT_CHAR_OR_SPACE
2267 } IndentState;
2268
2269 /* return indent length, we allow:
2270    > followed by spaces/tabs
2271    | followed by spaces/tabs
2272    uppercase characters immediately followed by >,
2273    and the repeating sequences of the above */
2274 /* return indent length */
2275 static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
2276 {
2277         guint i_len = 0;
2278         guint i, ch_len, alnum_cnt = 0;
2279         IndentState state = WAIT_FOR_INDENT_CHAR;
2280         gchar cbuf[MB_LEN_MAX];
2281         gboolean is_space;
2282         gboolean is_indent;
2283
2284         for (i = start_pos; i < text_len; i++) {
2285                 GET_CHAR(i, cbuf, ch_len);
2286                 if (ch_len > 1)
2287                         break;
2288
2289                 if (cbuf[0] == '\n')
2290                         break;
2291
2292                 is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
2293                 is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
2294
2295                 switch (state) {
2296                 case WAIT_FOR_SPACE:
2297                         if (is_space == FALSE)
2298                                 goto out;
2299                         state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2300                         break;
2301                 case WAIT_FOR_INDENT_CHAR_OR_SPACE:
2302                         if (is_indent == FALSE && is_space == FALSE &&
2303                             !isupper(cbuf[0]))
2304                                 goto out;
2305                         if (is_space == TRUE) {
2306                                 alnum_cnt = 0;
2307                                 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2308                         } else if (is_indent == TRUE) {
2309                                 alnum_cnt = 0;
2310                                 state = WAIT_FOR_SPACE;
2311                         } else {
2312                                 alnum_cnt++;
2313                                 state = WAIT_FOR_INDENT_CHAR;
2314                         }
2315                         break;
2316                 case WAIT_FOR_INDENT_CHAR:
2317                         if (is_indent == FALSE && !isupper(cbuf[0]))
2318                                 goto out;
2319                         if (is_indent == TRUE) {
2320                                 if (alnum_cnt > 0 
2321                                     && !strchr(prefs_common.quote_chars, cbuf[0]))
2322                                         goto out;
2323                                 alnum_cnt = 0;
2324                                 state = WAIT_FOR_SPACE;
2325                         } else {
2326                                 alnum_cnt++;
2327                         }
2328                         break;
2329                 }
2330
2331                 i_len++;
2332         }
2333
2334 out:
2335         if ((i_len > 0) && (state == WAIT_FOR_INDENT_CHAR))
2336                 i_len -= alnum_cnt;
2337
2338         return i_len;
2339 }
2340
2341 /* insert quotation string when line was wrapped */
2342 static guint ins_quote(GtkSText *text, guint indent_len,
2343                        guint prev_line_pos, guint text_len,
2344                        gchar *quote_fmt)
2345 {
2346         guint i, ins_len = 0;
2347         gchar ch;
2348
2349         if (indent_len) {
2350                 for (i = 0; i < indent_len; i++) {
2351                         ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2352                         gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2353                 }
2354                 ins_len = indent_len;
2355         }
2356
2357         return ins_len;
2358 }
2359
2360 /* check if we should join the next line */
2361 static gboolean join_next_line(GtkSText *text, guint start_pos, guint tlen,
2362                                guint prev_ilen, gboolean autowrap)
2363 {
2364         guint indent_len, ch_len;
2365         gboolean do_join = FALSE;
2366         gchar cbuf[MB_LEN_MAX];
2367
2368         indent_len = get_indent_length(text, start_pos, tlen);
2369
2370         if ((autowrap || indent_len > 0) && indent_len == prev_ilen) {
2371                 GET_CHAR(start_pos + indent_len, cbuf, ch_len);
2372                 if (ch_len > 0 && (cbuf[0] != '\n'))
2373                         do_join = TRUE;
2374         }
2375
2376         return do_join;
2377 }
2378
2379 static void compose_wrap_line_all(Compose *compose)
2380 {
2381         compose_wrap_line_all_full(compose, FALSE);
2382 }
2383
2384 #define STEXT_FREEZE() \
2385         if (!frozen) { gtk_stext_freeze(text); frozen = TRUE; }
2386
2387 static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
2388 {
2389         GtkSText *text = GTK_STEXT(compose->text);
2390         guint tlen;
2391         guint line_pos = 0, cur_pos = 0, p_pos = 0;
2392         gint line_len = 0, cur_len = 0;
2393         gint ch_len;
2394         gboolean is_new_line = TRUE, do_delete = FALSE;
2395         guint i_len = 0;
2396         gboolean linewrap_quote = TRUE;
2397         gboolean set_editable_pos = FALSE;
2398         gint editable_pos = 0;
2399         gboolean frozen = FALSE;
2400         guint linewrap_len = prefs_common.linewrap_len;
2401         gchar *qfmt = prefs_common.quotemark;
2402         gchar cbuf[MB_LEN_MAX];
2403
2404         tlen = gtk_stext_get_length(text);
2405
2406         for (; cur_pos < tlen; cur_pos++) {
2407                 /* mark position of new line - needed for quotation wrap */
2408                 if (is_new_line) {
2409                         if (linewrap_quote)
2410                                 i_len = get_indent_length(text, cur_pos, tlen);
2411
2412                         is_new_line = FALSE;
2413                         p_pos = cur_pos;
2414 #ifdef WRAP_DEBUG
2415                         g_print("new line i_len=%d p_pos=", i_len);
2416                         dump_text(text, p_pos, tlen, 1);
2417 #endif
2418                 }
2419
2420                 GET_CHAR(cur_pos, cbuf, ch_len);
2421
2422                 /* fix line length for tabs */
2423                 if (ch_len == 1 && *cbuf == '\t') {
2424                         guint tab_width = text->default_tab_width;
2425                         guint tab_offset = line_len % tab_width;
2426
2427 #ifdef WRAP_DEBUG
2428                         g_print("found tab at pos=%d line_len=%d ", cur_pos,
2429                                 line_len);
2430 #endif
2431                         if (tab_offset) {
2432                                 line_len += tab_width - tab_offset - 1;
2433                                 cur_len = line_len;
2434                         }
2435 #ifdef WRAP_DEBUG
2436                         printf("new_len=%d\n", line_len);
2437 #endif
2438                 }
2439
2440                 /* we have encountered line break */
2441                 if (ch_len == 1 && *cbuf == '\n') {
2442                         gint clen;
2443                         gchar cb[MB_LEN_MAX];
2444
2445                         /* should we join the next line */
2446                         if ((autowrap || i_len != cur_len) && do_delete &&
2447                             join_next_line
2448                                 (text, cur_pos + 1, tlen, i_len, autowrap))
2449                                 do_delete = TRUE;
2450                         else
2451                                 do_delete = FALSE;
2452
2453 #ifdef WRAP_DEBUG
2454                         g_print("found CR at %d do_del is %d next line is ",
2455                                cur_pos, do_delete);
2456                         dump_text(text, cur_pos + 1, tlen, 1);
2457 #endif
2458
2459                         /* skip delete if it is continuous URL */
2460                         if (do_delete && (line_pos - p_pos <= i_len) &&
2461                             gtkut_stext_is_uri_string(text, line_pos, tlen))
2462                                 do_delete = FALSE;
2463
2464 #ifdef WRAP_DEBUG
2465                         g_print("l_len=%d wrap_len=%d do_del=%d\n",
2466                                 line_len, linewrap_len, do_delete);
2467 #endif
2468                         /* should we delete to perform smart wrapping */
2469                         if (line_len < linewrap_len && do_delete) {
2470                                 STEXT_FREEZE();
2471                                 /* get rid of newline */
2472                                 gtk_stext_set_point(text, cur_pos);
2473                                 gtk_stext_forward_delete(text, 1);
2474                                 tlen--;
2475
2476                                 /* if text starts with quote fmt or with
2477                                    indent string, delete them */
2478                                 if (i_len) {
2479                                         guint ilen;
2480                                         ilen =  gtkut_stext_str_compare_n
2481                                                 (text, cur_pos, p_pos, i_len,
2482                                                  tlen);
2483                                         if (ilen) {
2484                                                 gtk_stext_forward_delete
2485                                                         (text, ilen);
2486                                                 tlen -= ilen;
2487                                         }
2488                                 }
2489
2490                                 GET_CHAR(cur_pos, cb, clen);
2491
2492                                 /* insert space if it's alphanumeric */
2493                                 if ((cur_pos != line_pos) &&
2494                                     ((clen > 1) || isalnum(cb[0]))) {
2495                                         gtk_stext_insert(text, NULL, NULL,
2496                                                         NULL, " ", 1);
2497                                         tlen++;
2498                                 }
2499
2500                                 /* and start over with current line */
2501                                 cur_pos = p_pos - 1;
2502                                 line_pos = cur_pos;
2503                                 line_len = cur_len = 0;
2504                                 do_delete = FALSE;
2505                                 is_new_line = TRUE;
2506 #ifdef WRAP_DEBUG
2507                                 g_print("after delete l_pos=");
2508                                 dump_text(text, line_pos, tlen, 1);
2509 #endif
2510                                 continue;
2511                         }
2512
2513                         /* mark new line beginning */
2514                         line_pos = cur_pos + 1;
2515                         line_len = cur_len = 0;
2516                         do_delete = FALSE;
2517                         is_new_line = TRUE;
2518                         continue;
2519                 }
2520
2521                 if (ch_len < 0) {
2522                         cbuf[0] = '\0';
2523                         ch_len = 1;
2524                 }
2525
2526                 /* possible line break */
2527                 if (ch_len == 1 && isspace(*cbuf)) {
2528                         line_pos = cur_pos + 1;
2529                         line_len = cur_len + ch_len;
2530                 }
2531
2532                 /* are we over wrapping length set in preferences ? */
2533                 if (cur_len + ch_len > linewrap_len) {
2534                         gint clen;
2535
2536 #ifdef WRAP_DEBUG
2537                         g_print("should wrap cur_pos=%d ", cur_pos);
2538                         dump_text(text, p_pos, tlen, 1);
2539                         dump_text(text, line_pos, tlen, 1);
2540 #endif
2541                         /* force wrapping if it is one long word but not URL */
2542                         if (line_pos - p_pos <= i_len)
2543                                 if (!gtkut_stext_is_uri_string
2544                                     (text, line_pos, tlen))
2545                                         line_pos = cur_pos - 1;
2546 #ifdef WRAP_DEBUG
2547                         g_print("new line_pos=%d\n", line_pos);
2548 #endif
2549
2550                         GET_CHAR(line_pos - 1, cbuf, clen);
2551
2552                         /* if next character is space delete it */
2553                         if (clen == 1 && isspace(*cbuf)) {
2554                                 if (p_pos + i_len != line_pos ||
2555                                     !gtkut_stext_is_uri_string
2556                                         (text, line_pos, tlen)) {
2557                                         STEXT_FREEZE();
2558                                         /* workaround for correct cursor
2559                                            position */
2560                                         if (set_editable_pos == FALSE) {
2561                                                 editable_pos = gtk_editable_get_position(GTK_EDITABLE(text));
2562                                                 if (editable_pos == line_pos)
2563                                                         set_editable_pos = TRUE;
2564                                         }
2565                                         gtk_stext_set_point(text, line_pos);
2566                                         gtk_stext_backward_delete(text, 1);
2567                                         tlen--;
2568                                         cur_pos--;
2569                                         line_pos--;
2570                                         cur_len--;
2571                                         line_len--;
2572                                 }
2573                         }
2574
2575                         /* if it is URL at beginning of line don't wrap */
2576                         if (p_pos + i_len == line_pos &&
2577                             gtkut_stext_is_uri_string(text, line_pos, tlen)) {
2578 #ifdef WRAP_DEBUG
2579                                 g_print("found URL at ");
2580                                 dump_text(text, line_pos, tlen, 1);
2581 #endif
2582                                 continue;
2583                         }
2584
2585                         /* insert CR */
2586                         STEXT_FREEZE();
2587                         gtk_stext_set_point(text, line_pos);
2588                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2589                         /* gtk_stext_compact_buffer(text); */
2590                         tlen++;
2591                         line_pos++;
2592                         /* for loop will increase it */
2593                         cur_pos = line_pos - 1;
2594                         /* start over with current line */
2595                         is_new_line = TRUE;
2596                         line_len = cur_len = 0;
2597                         if (autowrap || i_len > 0)
2598                                 do_delete = TRUE;
2599                         else
2600                                 do_delete = FALSE;
2601 #ifdef WRAP_DEBUG
2602                         g_print("after CR insert ");
2603                         dump_text(text, line_pos, tlen, 1);
2604                         dump_text(text, cur_pos, tlen, 1);
2605 #endif
2606
2607                         /* should we insert quotation ? */
2608                         if (linewrap_quote && i_len) {
2609                                 /* only if line is not already quoted  */
2610                                 if (!gtkut_stext_str_compare
2611                                         (text, line_pos, tlen, qfmt)) {
2612                                         guint ins_len;
2613
2614                                         if (line_pos - p_pos > i_len) {
2615                                                 ins_len = ins_quote
2616                                                         (text, i_len, p_pos,
2617                                                          tlen, qfmt);
2618                                                 tlen += ins_len;
2619                                         }
2620 #ifdef WRAP_DEBUG
2621                                         g_print("after quote insert ");
2622                                         dump_text(text, line_pos, tlen, 1);
2623 #endif
2624                                 }
2625                         }
2626                         continue;
2627                 }
2628
2629                 if (ch_len > 1) {
2630                         line_pos = cur_pos + 1;
2631                         line_len = cur_len + ch_len;
2632                 }
2633                 /* advance to next character in buffer */
2634                 cur_len += ch_len;
2635         }
2636
2637         if (frozen)
2638                 gtk_stext_thaw(text);
2639
2640         if (set_editable_pos && editable_pos <= tlen)
2641                 gtk_editable_set_position(GTK_EDITABLE(text), editable_pos);
2642 }
2643
2644 #undef STEXT_FREEZE
2645 #undef GET_CHAR
2646
2647 static void compose_set_title(Compose *compose)
2648 {
2649         gchar *str;
2650         gchar *edited;
2651
2652         edited = compose->modified ? _(" [Edited]") : "";
2653         if (compose->account && compose->account->address)
2654                 str = g_strdup_printf(_("%s - Compose message%s"),
2655                                       compose->account->address, edited);
2656         else
2657                 str = g_strdup_printf(_("Compose message%s"), edited);
2658         gtk_window_set_title(GTK_WINDOW(compose->window), str);
2659         g_free(str);
2660 }
2661
2662 /**
2663  * compose_current_mail_account:
2664  * 
2665  * Find a current mail account (the currently selected account, or the
2666  * default account, if a news account is currently selected).  If a
2667  * mail account cannot be found, display an error message.
2668  * 
2669  * Return value: Mail account, or NULL if not found.
2670  **/
2671 static PrefsAccount *
2672 compose_current_mail_account(void)
2673 {
2674         PrefsAccount *ac;
2675
2676         if (cur_account && cur_account->protocol != A_NNTP)
2677                 ac = cur_account;
2678         else {
2679                 ac = account_get_default();
2680                 if (!ac || ac->protocol == A_NNTP) {
2681                         alertpanel_error(_("Account for sending mail is not specified.\n"
2682                                            "Please select a mail account before sending."));
2683                         return NULL;
2684                 }
2685         }
2686         return ac;
2687 }
2688
2689 static void compose_select_account(Compose *compose, PrefsAccount *account)
2690 {
2691         GtkWidget *menuitem;
2692         GtkItemFactory *ifactory;
2693
2694         g_return_if_fail(account != NULL);
2695
2696         compose->account = account;
2697
2698         compose_set_title(compose);
2699
2700         ifactory = gtk_item_factory_from_widget(compose->menubar);
2701 #if 0
2702         if (account->protocol == A_NNTP) {
2703                 gtk_widget_show(compose->newsgroups_hbox);
2704                 gtk_widget_show(compose->newsgroups_entry);
2705                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 4);
2706                 compose->use_newsgroups = TRUE;
2707
2708                 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
2709                 gtk_check_menu_item_set_active
2710                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2711                 gtk_widget_set_sensitive(menuitem, TRUE);
2712                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2713                 gtk_check_menu_item_set_active
2714                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2715                 gtk_widget_set_sensitive(menuitem, TRUE);
2716
2717                 menu_set_sensitive(ifactory, "/View/Followup to", TRUE);
2718         } else {
2719                 gtk_widget_hide(compose->newsgroups_hbox);
2720                 gtk_widget_hide(compose->newsgroups_entry);
2721                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 0);
2722                 gtk_widget_queue_resize(compose->table_vbox);
2723                 compose->use_newsgroups = FALSE;
2724
2725                 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
2726                 gtk_check_menu_item_set_active
2727                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2728                 gtk_widget_set_sensitive(menuitem, FALSE);
2729                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2730                 gtk_check_menu_item_set_active
2731                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2732                 gtk_widget_set_sensitive(menuitem, FALSE);
2733
2734                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Followup to");
2735                 gtk_check_menu_item_set_active
2736                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2737                 gtk_widget_set_sensitive(menuitem, FALSE);
2738         }
2739
2740         if (account->set_autocc && account->auto_cc &&
2741             compose->mode != COMPOSE_REEDIT) {
2742                 gtk_entry_set_text
2743                         (GTK_ENTRY(compose->cc_entry), account->auto_cc);
2744                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2745                 gtk_check_menu_item_set_active
2746                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2747         }
2748         if (account->set_autobcc) {
2749                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Bcc");
2750                 gtk_check_menu_item_set_active
2751                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2752                 if (account->auto_bcc && compose->mode != COMPOSE_REEDIT)
2753                         gtk_entry_set_text(GTK_ENTRY(compose->bcc_entry),
2754                                            account->auto_bcc);
2755         }
2756         if (account->set_autoreplyto) {
2757                 menuitem = gtk_item_factory_get_item(ifactory,
2758                                                      "/View/Reply to");
2759                 gtk_check_menu_item_set_active
2760                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2761                 if (account->auto_replyto && compose->mode != COMPOSE_REEDIT)
2762                         gtk_entry_set_text(GTK_ENTRY(compose->reply_entry),
2763                                            account->auto_replyto);
2764         }
2765
2766         menuitem = gtk_item_factory_get_item(ifactory, "/View/Ruler");
2767         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2768                                        prefs_common.show_ruler);
2769 #endif
2770
2771 #if USE_GPGME
2772         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
2773         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2774                                        account->default_sign);
2775         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
2776         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2777                                        account->default_encrypt);
2778                                        
2779         activate_gnupg_mode(compose, account);          
2780 #endif /* USE_GPGME */
2781 }
2782
2783 gboolean compose_check_for_valid_recipient(Compose *compose) {
2784         gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
2785         gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
2786         gboolean recipient_found = FALSE;
2787         GSList *list;
2788         gchar **strptr;
2789
2790         /* free to and newsgroup list */
2791         slist_free_strings(compose->to_list);
2792         g_slist_free(compose->to_list);
2793         compose->to_list = NULL;
2794                         
2795         slist_free_strings(compose->newsgroup_list);
2796         g_slist_free(compose->newsgroup_list);
2797         compose->newsgroup_list = NULL;
2798
2799         /* search header entries for to and newsgroup entries */
2800         for (list = compose->header_list; list; list = list->next) {
2801                 gchar *header;
2802                 gchar *entry;
2803                 header = gtk_editable_get_chars(GTK_EDITABLE(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry), 0, -1);
2804                 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
2805                 g_strstrip(entry);
2806                 if (entry[0] != '\0') {
2807                         for (strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
2808                                 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2809                                         compose->to_list = address_list_append(compose->to_list, entry);
2810                                         recipient_found = TRUE;
2811                                 }
2812                         }
2813                         for (strptr = recipient_headers_news; *strptr != NULL; strptr++) {
2814                                 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2815                                         compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
2816                                         recipient_found = TRUE;
2817                                 }
2818                         }
2819                 }
2820                 g_free(header);
2821                 g_free(entry);
2822         }
2823         return recipient_found;
2824 }
2825
2826 static gboolean compose_check_entries(Compose *compose, gboolean check_subject)
2827 {
2828         gchar *str;
2829
2830         if (compose_check_for_valid_recipient(compose) == FALSE) {
2831                 alertpanel_error(_("Recipient is not specified."));
2832                 return FALSE;
2833         }
2834
2835         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
2836         if (*str == '\0' && check_subject == TRUE) {
2837                 AlertValue aval;
2838
2839                 aval = alertpanel(_("Send"),
2840                                   _("Subject is empty. Send it anyway?"),
2841                                   _("Yes"), _("No"), NULL);
2842                 if (aval != G_ALERTDEFAULT)
2843                         return FALSE;
2844         }
2845
2846         return TRUE;
2847 }
2848
2849 gint compose_send(Compose *compose)
2850 {
2851         gint msgnum;
2852         FolderItem *folder;
2853         gint val;
2854         gchar *msgpath;
2855
2856         if (compose_check_entries(compose, TRUE) == FALSE)
2857                 return -1;
2858
2859         val = compose_queue(compose, &msgnum, &folder);
2860         if (val) {
2861                 alertpanel_error(_("Could not queue message for sending"));
2862                 return -1;
2863         }
2864         
2865         msgpath = folder_item_fetch_msg(folder, msgnum);
2866         val = procmsg_send_message_queue(msgpath);
2867         g_free(msgpath);
2868
2869         folder_item_remove_msg(folder, msgnum);
2870         folderview_update_item(folder, TRUE);
2871
2872         return val;
2873 }
2874
2875 #if 0 /* compose restructure */
2876 gint compose_send(Compose *compose)
2877 {
2878         gchar tmp[MAXPATHLEN + 1];
2879         gint ok = 0;
2880         static gboolean lock = FALSE;
2881
2882         if (lock) return 1;
2883
2884         g_return_val_if_fail(compose->account != NULL, -1);
2885
2886         lock = TRUE;
2887
2888         if (compose_check_entries(compose, TRUE) == FALSE) {
2889                 lock = FALSE;
2890                 return 1;
2891         }
2892
2893         /* write to temporary file */
2894         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
2895                    get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
2896
2897         if (prefs_common.linewrap_at_send)
2898                 compose_wrap_line_all(compose);
2899
2900         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
2901                 lock = FALSE;
2902                 return -1;
2903         }
2904
2905         if (!compose->to_list && !compose->newsgroup_list) {
2906                 g_warning(_("can't get recipient list."));
2907                 unlink(tmp);
2908                 lock = FALSE;
2909                 return -1;
2910         }
2911
2912         if (compose->to_list) {
2913                 PrefsAccount *ac;
2914
2915 #if 0 /* NEW COMPOSE GUI */
2916                 if (compose->account->protocol != A_NNTP)
2917                         ac = compose->account;
2918                 else {
2919                         ac = account_find_from_address(compose->account->address);
2920                         if (!ac) {
2921                                 if (cur_account && cur_account->protocol != A_NNTP)
2922                                         ac = cur_account;
2923                                 else
2924                                         ac = account_get_default();
2925                         }
2926                         if (!ac || ac->protocol == A_NNTP) {
2927                                 alertpanel_error(_("Account for sending mail is not specified.\n"
2928                                                    "Please select a mail account before sending."));
2929                                 unlink(tmp);
2930                                 lock = FALSE;
2931                                 return -1;
2932                         }
2933                 }
2934 #endif
2935                 ac = compose->account;
2936
2937                 ok = send_message(tmp, ac, compose->to_list);
2938                 statusbar_pop_all();
2939         }
2940
2941         if (ok == 0 && compose->newsgroup_list) {
2942                 ok = news_post(FOLDER(compose->account->folder), tmp);
2943                 if (ok < 0) {
2944                         alertpanel_error(_("Error occurred while posting the message to %s ."),
2945                                          compose->account->nntp_server);
2946                         unlink(tmp);
2947                         lock = FALSE;
2948                         return -1;
2949                 }
2950         }
2951
2952         /* queue message if failed to send */
2953         if (ok < 0) {
2954                 if (prefs_common.queue_msg) {
2955                         AlertValue val;
2956
2957                         val = alertpanel
2958                                 (_("Queueing"),
2959                                  _("Error occurred while sending the message.\n"
2960                                    "Put this message into queue folder?"),
2961                                  _("OK"), _("Cancel"), NULL);
2962                         if (G_ALERTDEFAULT == val) {
2963                                 ok = compose_queue(compose, tmp);
2964                                 if (ok < 0)
2965                                         alertpanel_error(_("Can't queue the message."));
2966                         }
2967                 } else
2968                         alertpanel_error(_("Error occurred while sending the message."));
2969         } else {
2970                 if (compose->mode == COMPOSE_REEDIT) {
2971                         compose_remove_reedit_target(compose);
2972                         if (compose->targetinfo)
2973                                 folderview_update_item
2974                                         (compose->targetinfo->folder, TRUE);
2975                 }
2976                 /* save message to outbox */
2977                 if (prefs_common.savemsg) {
2978                         FolderItem *outbox;
2979
2980                         outbox = account_get_special_folder
2981                                 (compose->account, F_OUTBOX);
2982                         if (procmsg_save_to_outbox(outbox, tmp, FALSE) < 0)
2983                                 alertpanel_error
2984                                         (_("Can't save the message to Sent."));
2985                         else
2986                                 folderview_update_item(outbox, TRUE);
2987                 }
2988         }
2989
2990         unlink(tmp);
2991         lock = FALSE;
2992         return ok;
2993 }
2994 #endif
2995
2996 static gboolean compose_use_attach(Compose *compose) {
2997     return(gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL);
2998 }
2999
3000 static gint compose_redirect_write_headers_from_headerlist(Compose *compose, 
3001                                                            FILE *fp)
3002 {
3003         gchar buf[BUFFSIZE];
3004         gchar *str;
3005         gboolean first_address;
3006         GSList *list;
3007         ComposeHeaderEntry *headerentry;
3008         gchar *headerentryname;
3009         gchar *cc_hdr;
3010         gchar *to_hdr;
3011
3012         debug_print("Writing redirect header\n");
3013
3014         cc_hdr = prefs_common.trans_hdr ? _("Cc:") : "Cc:";
3015         to_hdr = prefs_common.trans_hdr ? _("To:") : "To:";
3016
3017         first_address = TRUE;
3018         for (list = compose->header_list; list; list = list->next) {
3019                 headerentry = ((ComposeHeaderEntry *)list->data);
3020                 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
3021
3022                 if (g_strcasecmp(headerentryname, cc_hdr) == 0 
3023                    || g_strcasecmp(headerentryname, to_hdr) == 0) {
3024                         str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
3025                         Xstrdup_a(str, str, return -1);
3026                         g_strstrip(str);
3027                         if (str[0] != '\0') {
3028                                 compose_convert_header
3029                                         (buf, sizeof(buf), str,
3030                                         strlen("Resent-To") + 2);
3031                                 if (first_address) {
3032                                         fprintf(fp, "Resent-To: ");
3033                                         first_address = FALSE;
3034                                 } else {
3035                                         fprintf(fp, ",");
3036                                 }
3037                                 fprintf(fp, "%s", buf);
3038                         }
3039                 }
3040         }
3041         /* if (!first_address) { */
3042         fprintf(fp, "\n");
3043         /* } */
3044
3045         return(0);
3046 }
3047
3048 static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
3049 {
3050         gchar buf[BUFFSIZE];
3051         gchar *str;
3052         /* struct utsname utsbuf; */
3053
3054         g_return_val_if_fail(fp != NULL, -1);
3055         g_return_val_if_fail(compose->account != NULL, -1);
3056         g_return_val_if_fail(compose->account->address != NULL, -1);
3057
3058         /* Resent-Date */
3059         get_rfc822_date(buf, sizeof(buf));
3060         fprintf(fp, "Resent-Date: %s\n", buf);
3061
3062         /* Resent-From */
3063         if (compose->account->name && *compose->account->name) {
3064                 compose_convert_header
3065                         (buf, sizeof(buf), compose->account->name,
3066                          strlen("From: "));
3067                 fprintf(fp, "Resent-From: %s <%s>\n",
3068                         buf, compose->account->address);
3069         } else
3070                 fprintf(fp, "Resent-From: %s\n", compose->account->address);
3071
3072         /* Subject */
3073         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
3074         if (*str != '\0') {
3075                 Xstrdup_a(str, str, return -1);
3076                 g_strstrip(str);
3077                 if (*str != '\0') {
3078                         compose_convert_header(buf, sizeof(buf), str,
3079                                                strlen("Subject: "));
3080                         fprintf(fp, "Subject: %s\n", buf);
3081                 }
3082         }
3083
3084         /* Resent-Message-ID */
3085         if (compose->account->gen_msgid) {
3086                 compose_generate_msgid(compose, buf, sizeof(buf));
3087                 fprintf(fp, "Resent-Message-Id: <%s>\n", buf);
3088                 compose->msgid = g_strdup(buf);
3089         }
3090
3091         compose_redirect_write_headers_from_headerlist(compose, fp);
3092
3093         /* separator between header and body */
3094         fputs("\n", fp);
3095
3096         return 0;
3097 }
3098
3099 static gint compose_redirect_write_to_file(Compose *compose, const gchar *file)
3100 {
3101         FILE *fp;
3102         FILE *fdest;
3103         size_t len;
3104         gchar buf[BUFFSIZE];
3105
3106         if ((fp = fopen(compose->redirect_filename, "rb")) == NULL) {
3107                 FILE_OP_ERROR(file, "fopen");
3108                 return -1;
3109         }
3110
3111         if ((fdest = fopen(file, "wb")) == NULL) {
3112                 FILE_OP_ERROR(file, "fopen");
3113                 fclose(fp);
3114                 return -1;
3115         }
3116
3117         /* chmod for security */
3118         if (change_file_mode_rw(fdest, file) < 0) {
3119                 FILE_OP_ERROR(file, "chmod");
3120                 g_warning(_("can't change file mode\n"));
3121         }
3122
3123         while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
3124                 /* should filter returnpath, delivered-to */
3125                 if (g_strncasecmp(buf, "Return-Path:",
3126                                    strlen("Return-Path:")) == 0 ||
3127                     g_strncasecmp(buf, "Delivered-To:",
3128                                   strlen("Delivered-To:")) == 0 ||
3129                     g_strncasecmp(buf, "Received:",
3130                                   strlen("Received:")) == 0 ||
3131                     g_strncasecmp(buf, "Subject:",
3132                                   strlen("Subject:")) == 0 ||
3133                     g_strncasecmp(buf, "X-UIDL:",
3134                                   strlen("X-UIDL:")) == 0)
3135                         continue;
3136
3137                 if (fputs(buf, fdest) == -1)
3138                         goto error;
3139
3140                 if (!prefs_common.redirect_keep_from) {
3141                         if (g_strncasecmp(buf, "From:",
3142                                           strlen("From:")) == 0) {
3143                                 fputs(" (by way of ", fdest);
3144                                 if (compose->account->name
3145                                     && *compose->account->name) {
3146                                         compose_convert_header
3147                                                 (buf, sizeof(buf),
3148                                                  compose->account->name,
3149                                                  strlen("From: "));
3150                                         fprintf(fdest, "%s <%s>",
3151                                                 buf,
3152                                                 compose->account->address);
3153                                 } else
3154                                         fprintf(fdest, "%s",
3155                                                 compose->account->address);
3156                                 fputs(")", fdest);
3157                         }
3158                 }
3159
3160                 if (fputs("\n", fdest) == -1)
3161                         goto error;
3162         }
3163
3164         compose_redirect_write_headers(compose, fdest);
3165
3166         while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
3167                 if (fwrite(buf, sizeof(gchar), len, fdest) != len) {
3168                         FILE_OP_ERROR(file, "fwrite");
3169                         goto error;
3170                 }
3171         }
3172
3173         fclose(fp);
3174         if (fclose(fdest) == EOF) {
3175                 FILE_OP_ERROR(file, "fclose");
3176                 unlink(file);
3177                 return -1;
3178         }
3179
3180         return 0;
3181  error:
3182         fclose(fp);
3183         fclose(fdest);
3184         unlink(file);
3185
3186         return -1;