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