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