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