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