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