sync with sylpheed 0.6.5cvs20
[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         if (fmt && *fmt != '\0') {
1516                 quote_fmt_init(msginfo, quote_str);
1517                 quote_fmt_scan_string(fmt);
1518                 quote_fmt_parse();
1519
1520                 buf = quote_fmt_get_buffer();
1521                 if (buf == NULL) {
1522                         alertpanel_error(_("Message reply/forward format error."));
1523                         return NULL;
1524                 }
1525         } else
1526                 buf = "";
1527
1528         gtk_stext_freeze(text);
1529         gtk_stext_set_point(text, 0);
1530         gtk_stext_forward_delete(text, gtk_stext_get_length(text));
1531
1532         for (p = buf; *p != '\0'; ) {
1533                 lastp = strchr(p, '\n');
1534                 len = lastp ? lastp - p + 1 : -1;
1535                 gtk_stext_insert(text, NULL, NULL, NULL, p, len);
1536                 if (lastp)
1537                         p = lastp + 1;
1538                 else
1539                         break;
1540         }
1541
1542         gtk_stext_thaw(text);
1543
1544         return buf;
1545 }
1546
1547 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1548                                     gboolean to_all, gboolean ignore_replyto,
1549                                     gboolean followup_and_reply_to)
1550 {
1551         GSList *cc_list;
1552         GSList *cur;
1553         gchar *from;
1554         GHashTable *to_table;
1555
1556         g_return_if_fail(compose->account != NULL);
1557         g_return_if_fail(msginfo != NULL);
1558
1559         if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1560                 compose_entry_append(compose,
1561                                     ((compose->mailinglist && !ignore_replyto)
1562                                      ? compose->mailinglist
1563                                      : (compose->replyto && !ignore_replyto)
1564                                      ? compose->replyto
1565                                      : msginfo->from ? msginfo->from : ""),
1566                                      COMPOSE_TO);
1567         if (compose->account->protocol == A_NNTP) {
1568                 if (ignore_replyto)
1569                         compose_entry_append
1570                                 (compose, msginfo->from ? msginfo->from : "",
1571                                  COMPOSE_TO);
1572                 else
1573                         compose_entry_append
1574                                 (compose,
1575                                  compose->followup_to ? compose->followup_to
1576                                  : compose->newsgroups ? compose->newsgroups
1577                                  : "",
1578                                  COMPOSE_NEWSGROUPS);
1579         }
1580
1581         if (msginfo->subject && *msginfo->subject) {
1582                 gchar *buf, *buf2, *p;
1583
1584                 buf = g_strdup(msginfo->subject);
1585                 while (!strncasecmp(buf, "Re:", 3)) {
1586                         p = buf + 3;
1587                         while (isspace(*p)) p++;
1588                         memmove(buf, p, strlen(p) + 1);
1589                 }
1590
1591                 buf2 = g_strdup_printf("Re: %s", buf);
1592                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1593                 g_free(buf2);
1594                 g_free(buf);
1595         } else
1596                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1597
1598         if (!to_all || compose->account->protocol == A_NNTP) return;
1599
1600         from = g_strdup(compose->replyto ? compose->replyto :
1601                         msginfo->from ? msginfo->from : "");
1602         extract_address(from);
1603
1604         cc_list = address_list_append(NULL, msginfo->to);
1605         cc_list = address_list_append(cc_list, compose->cc);
1606
1607         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1608         g_hash_table_insert(to_table, from, GINT_TO_POINTER(1));
1609         if (compose->account)
1610                 g_hash_table_insert(to_table, compose->account->address,
1611                                     GINT_TO_POINTER(1));
1612
1613         /* remove address on To: and that of current account */
1614         for (cur = cc_list; cur != NULL; ) {
1615                 GSList *next = cur->next;
1616
1617                 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1618                         cc_list = g_slist_remove(cc_list, cur->data);
1619                 else
1620                         g_hash_table_insert(to_table, cur->data, cur);
1621
1622                 cur = next;
1623         }
1624         g_hash_table_destroy(to_table);
1625         g_free(from);
1626
1627         if (cc_list) {
1628                 for (cur = cc_list; cur != NULL; cur = cur->next)
1629                         compose_entry_append(compose, (gchar *)cur->data,
1630                                              COMPOSE_CC);
1631                 slist_free_strings(cc_list);
1632                 g_slist_free(cc_list);
1633         }
1634 }
1635
1636 #define SET_ENTRY(entry, str) \
1637 { \
1638         if (str && *str) \
1639                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1640 }
1641
1642 #define SET_ADDRESS(type, str) \
1643 { \
1644         if (str && *str) \
1645                 compose_entry_append(compose, str, type); \
1646 }
1647
1648 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1649 {
1650         g_return_if_fail(msginfo != NULL);
1651
1652         SET_ENTRY(subject_entry, msginfo->subject);
1653         SET_ADDRESS(COMPOSE_TO, msginfo->to);
1654         SET_ADDRESS(COMPOSE_CC, compose->cc);
1655         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1656         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1657
1658 #if 0 /* NEW COMPOSE GUI */
1659         if (compose->bcc) {
1660                 GtkItemFactory *ifactory;
1661                 GtkWidget *menuitem;
1662
1663                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1664                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
1665                 gtk_check_menu_item_set_active
1666                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1667         }
1668         if (compose->replyto) {
1669                 GtkItemFactory *ifactory;
1670                 GtkWidget *menuitem;
1671
1672                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1673                 menuitem = gtk_item_factory_get_item
1674                         (ifactory, "/Message/Reply to");
1675                 gtk_check_menu_item_set_active
1676                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1677         }
1678 #endif
1679 }
1680
1681 #undef SET_ENTRY
1682 #undef SET_ADDRESS
1683
1684 static void compose_exec_sig(Compose *compose, gchar *sigfile)
1685 {
1686         FILE *tmpfp;
1687         pid_t thepid;
1688         gchar *sigtext;
1689         FILE  *sigprg;
1690         gchar  *buf;
1691         size_t buf_len = 128;
1692  
1693         if (strlen(sigfile) < 2)
1694           return;
1695  
1696         sigprg = popen(sigfile+1, "r");
1697         if (sigprg) {
1698
1699                 buf = g_malloc(buf_len);
1700
1701                 if (!buf) {
1702                         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1703                         "Unable to insert signature (malloc failed)\n", -1);
1704
1705                         pclose(sigprg);
1706                         return;
1707                 }
1708
1709                 while (!feof(sigprg)) {
1710                         bzero(buf, buf_len);
1711                         fread(buf, buf_len-1, 1, sigprg);
1712                         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
1713                 }
1714
1715                 g_free(buf);
1716                 pclose(sigprg);
1717         }
1718         else
1719         {
1720                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1721                 "Can't exec file: ", -1);
1722                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1723                 sigfile+1, -1);
1724         }
1725 }
1726
1727 static void compose_insert_sig(Compose *compose)
1728 {
1729         gchar *sigfile;
1730
1731         if (compose->account && compose->account->sig_path)
1732                 sigfile = g_strdup(compose->account->sig_path);
1733         else {
1734                 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1735                                       DEFAULT_SIGNATURE, NULL);
1736         }
1737
1738         if (!is_file_or_fifo_exist(sigfile) & (sigfile[0] != '|')) {
1739                 g_free(sigfile);
1740                 return;
1741         }
1742
1743         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1744         if (prefs_common.sig_sep) {
1745                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1746                                 prefs_common.sig_sep, -1);
1747                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1748                                 "\n", 1);
1749         }
1750
1751         if (sigfile[0] == '|')
1752         {
1753                 compose_exec_sig(compose, sigfile);
1754         }
1755         else
1756         {
1757                 compose_insert_file(compose, sigfile);
1758         }
1759         g_free(sigfile);
1760 }
1761
1762 static void compose_insert_file(Compose *compose, const gchar *file)
1763 {
1764         GtkSText *text = GTK_STEXT(compose->text);
1765         gchar buf[BUFFSIZE];
1766         gint len;
1767         FILE *fp;
1768
1769         g_return_if_fail(file != NULL);
1770
1771         if ((fp = fopen(file, "r")) == NULL) {
1772                 FILE_OP_ERROR(file, "fopen");
1773                 return;
1774         }
1775
1776         gtk_stext_freeze(text);
1777
1778         while (fgets(buf, sizeof(buf), fp) != NULL) {
1779                 /* Strip <CR> if DOS/Windoze file, replace <CR> with <LF> if MAC file */
1780                 len = strlen(buf);
1781                 if (len > 1 && buf[len - 2] == '\r' && buf[len - 1] == '\n') {
1782                         buf[len - 2] = '\n';
1783                         buf[len - 1] = '\0';
1784                 } else {
1785                         while (--len > 0)
1786                                 if (buf[len] == '\r')
1787                                         buf[len] = '\n';
1788                 }
1789
1790                 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1791         }
1792
1793         gtk_stext_thaw(text);
1794
1795         fclose(fp);
1796 }
1797
1798 static void compose_attach_info(Compose * compose, AttachInfo * ainfo,
1799                                 ContentType cnttype)
1800 {
1801         gchar *text[N_ATTACH_COLS];
1802         gint row;
1803
1804         text[COL_MIMETYPE] = ainfo->content_type;
1805         text[COL_SIZE] = to_human_readable(ainfo->size);
1806         text[COL_NAME] = ainfo->name;
1807
1808         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1809         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1810
1811         if (cnttype != MIME_MESSAGE_RFC822)
1812                 compose_changed_cb(NULL, compose);
1813 }
1814
1815 static void compose_attach_append_with_type(Compose *compose,
1816                                             const gchar *file,
1817                                             const gchar *type,
1818                                             ContentType cnttype)
1819 {
1820         AttachInfo *ainfo;
1821         off_t size;
1822
1823         if (!is_file_exist(file)) {
1824                 g_warning(_("File %s doesn't exist\n"), file);
1825                 return;
1826         }
1827         if ((size = get_file_size(file)) < 0) {
1828                 g_warning(_("Can't get file size of %s\n"), file);
1829                 return;
1830         }
1831         if (size == 0) {
1832                 alertpanel_notice(_("File %s is empty\n"), file);
1833                 return;
1834         }
1835 #if 0 /* NEW COMPOSE GUI */
1836         if (!compose->use_attach) {
1837                 GtkItemFactory *ifactory;
1838                 GtkWidget *menuitem;
1839
1840                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1841                 menuitem = gtk_item_factory_get_item(ifactory,
1842                                                      "/Message/Attach");
1843                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1844                                                TRUE);
1845         }
1846 #endif
1847         ainfo = g_new0(AttachInfo, 1);
1848         ainfo->file = g_strdup(file);
1849
1850         if (cnttype == MIME_MESSAGE_RFC822) {
1851                 ainfo->encoding = ENC_7BIT;
1852                 ainfo->name = g_strdup_printf(_("Message: %s"),
1853                                               g_basename(file));
1854         } else {
1855                 ainfo->encoding = ENC_BASE64;
1856                 ainfo->name = g_strdup(g_basename(file));
1857         }
1858
1859         ainfo->content_type = g_strdup(type);
1860         ainfo->size = size;
1861
1862         compose_attach_info(compose, ainfo, cnttype);
1863 }
1864
1865 static void compose_attach_append(Compose *compose, const gchar *file,
1866                                   ContentType cnttype)
1867 {
1868         AttachInfo *ainfo;
1869         gchar *text[N_ATTACH_COLS];
1870         off_t size;
1871         gint row;
1872
1873         if (!is_file_exist(file)) {
1874                 g_warning(_("File %s doesn't exist\n"), file);
1875                 return;
1876         }
1877         if ((size = get_file_size(file)) < 0) {
1878                 g_warning(_("Can't get file size of %s\n"), file);
1879                 return;
1880         }
1881         if (size == 0) {
1882                 alertpanel_notice(_("File %s is empty\n"), file);
1883                 return;
1884         }
1885 #if 0 /* NEW COMPOSE GUI */
1886         if (!compose->use_attach) {
1887                 GtkItemFactory *ifactory;
1888                 GtkWidget *menuitem;
1889
1890                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1891                 menuitem = gtk_item_factory_get_item(ifactory,
1892                                                      "/Message/Attach");
1893                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1894                                                TRUE);
1895         }
1896 #endif
1897         ainfo = g_new0(AttachInfo, 1);
1898         ainfo->file = g_strdup(file);
1899
1900         if (cnttype == MIME_MESSAGE_RFC822) {
1901                 ainfo->content_type = g_strdup("message/rfc822");
1902                 ainfo->encoding = ENC_7BIT;
1903                 ainfo->name = g_strdup_printf(_("Message: %s"),
1904                                               g_basename(file));
1905         } else {
1906                 ainfo->content_type = procmime_get_mime_type(file);
1907                 if (!ainfo->content_type)
1908                         ainfo->content_type =
1909                                 g_strdup("application/octet-stream");
1910                 ainfo->encoding = ENC_BASE64;
1911                 ainfo->name = g_strdup(g_basename(file));
1912         }
1913         ainfo->size = size;
1914
1915         compose_attach_info(compose, ainfo, cnttype);
1916 }
1917
1918 #define GET_CHAR(pos, buf, len)                                              \
1919 {                                                                            \
1920         if (text->use_wchar)                                                 \
1921                 len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos)));     \
1922         else {                                                               \
1923                 buf[0] = GTK_STEXT_INDEX(text, (pos));                       \
1924                 len = 1;                                                     \
1925         }                                                                    \
1926 }
1927
1928 static void compose_wrap_line(Compose *compose)
1929 {
1930         GtkSText *text = GTK_STEXT(compose->text);
1931         gint ch_len, last_ch_len;
1932         gchar cbuf[MB_LEN_MAX], last_ch;
1933         guint text_len;
1934         guint line_end;
1935         guint quoted;
1936         gint p_start, p_end;
1937         gint line_pos, cur_pos;
1938         gint line_len, cur_len;
1939
1940
1941         gtk_stext_freeze(text);
1942
1943         text_len = gtk_stext_get_length(text);
1944
1945         /* check to see if the point is on the paragraph mark (empty line). */
1946         cur_pos = gtk_stext_get_point(text);
1947         GET_CHAR(cur_pos, cbuf, ch_len);
1948         if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
1949                 if (cur_pos == 0)
1950                         goto compose_end; /* on the paragraph mark */
1951                 GET_CHAR(cur_pos - 1, cbuf, ch_len);
1952                 if (ch_len == 1 && *cbuf == '\n')
1953                         goto compose_end; /* on the paragraph mark */
1954         }
1955
1956         /* find paragraph start. */
1957         line_end = quoted = 0;
1958         for (p_start = cur_pos; p_start >= 0; --p_start) {
1959                 GET_CHAR(p_start, cbuf, ch_len);
1960                 if (ch_len == 1 && *cbuf == '\n') {
1961                         if (quoted)
1962                                 goto compose_end; /* quoted part */
1963                         if (line_end) {
1964                                 p_start += 2;
1965                                 break;
1966                         }
1967                         line_end = 1;
1968                 } else {
1969                         if (ch_len == 1 && strchr(">:#", *cbuf))
1970                                 quoted = 1;
1971                         else if (ch_len != 1 || !isspace(*cbuf))
1972                                 quoted = 0;
1973
1974                         line_end = 0;
1975                 }
1976         }
1977         if (p_start < 0)
1978                 p_start = 0;
1979
1980         /* find paragraph end. */
1981         line_end = 0;
1982         for (p_end = cur_pos; p_end < text_len; p_end++) {
1983                 GET_CHAR(p_end, cbuf, ch_len);
1984                 if (ch_len == 1 && *cbuf == '\n') {
1985                         if (line_end) {
1986                                 p_end -= 1;
1987                                 break;
1988                         }
1989                         line_end = 1;
1990                 } else {
1991                         if (line_end && ch_len == 1 && strchr(">:#", *cbuf))
1992                                 goto compose_end; /* quoted part */
1993
1994                         line_end = 0;
1995                 }
1996         }
1997         if (p_end >= text_len)
1998                 p_end = text_len;
1999
2000         if (p_start >= p_end)
2001                 goto compose_end;
2002
2003         line_len = cur_len = 0;
2004         last_ch_len = 0;
2005         last_ch = '\0';
2006         line_pos = p_start;
2007         for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2008                 guint space = 0;
2009
2010                 GET_CHAR(cur_pos, cbuf, ch_len);
2011
2012                 if (ch_len < 0) {
2013                         cbuf[0] = '\0';
2014                         ch_len = 1;
2015                 }
2016
2017                 if (ch_len == 1 && isspace(*cbuf))
2018                         space = 1;
2019
2020                 if (ch_len == 1 && *cbuf == '\n') {
2021                         guint replace = 0;
2022                         if (last_ch_len == 1 && !isspace(last_ch)) {
2023                                 if (cur_pos + 1 < p_end) {
2024                                         GET_CHAR(cur_pos + 1, cbuf, ch_len);
2025                                         if (ch_len == 1 && !isspace(*cbuf))
2026                                                 replace = 1;
2027                                 }
2028                         }
2029                         gtk_stext_set_point(text, cur_pos + 1);
2030                         gtk_stext_backward_delete(text, 1);
2031                         if (replace) {
2032                                 gtk_stext_set_point(text, cur_pos);
2033                                 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2034                                 space = 1;
2035                         }
2036                         else {
2037                                 p_end--;
2038                                 cur_pos--;
2039                                 continue;
2040                         }
2041                 }
2042
2043                 last_ch_len = ch_len;
2044                 last_ch = *cbuf;
2045
2046                 if (space) {
2047                         line_pos = cur_pos + 1;
2048                         line_len = cur_len + ch_len;
2049                 }
2050
2051                 if (cur_len + ch_len > prefs_common.linewrap_len &&
2052                     line_len > 0) {
2053                         gint tlen = ch_len;
2054
2055                         GET_CHAR(line_pos - 1, cbuf, ch_len);
2056                         if (ch_len == 1 && isspace(*cbuf)) {
2057                                 gtk_stext_set_point(text, line_pos);
2058                                 gtk_stext_backward_delete(text, 1);
2059                                 p_end--;
2060                                 cur_pos--;
2061                                 line_pos--;
2062                                 cur_len--;
2063                                 line_len--;
2064                         }
2065                         ch_len = tlen;
2066
2067                         gtk_stext_set_point(text, line_pos);
2068                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2069                         p_end++;
2070                         cur_pos++;
2071                         line_pos++;
2072                         cur_len = cur_len - line_len + ch_len;
2073                         line_len = 0;
2074                         continue;
2075                 }
2076
2077                 if (ch_len > 1) {
2078                         line_pos = cur_pos + 1;
2079                         line_len = cur_len + ch_len;
2080                 }
2081                 cur_len += ch_len;
2082         }
2083
2084 compose_end:
2085         gtk_stext_thaw(text);
2086 }
2087
2088 /* return indent length */
2089 static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
2090 {
2091         gint indent_len = 0;
2092         gint i, ch_len;
2093         gchar cbuf[MB_LEN_MAX];
2094
2095         for (i = start_pos; i < text_len; i++) {
2096                 GET_CHAR(i, cbuf, ch_len);
2097                 if (ch_len > 1)
2098                         break;
2099                 /* allow space, tab, > or | */
2100                 if (cbuf[0] != ' ' && cbuf[0] != '\t' &&
2101                     cbuf[0] != '>' && cbuf[0] != '|')
2102                         break;
2103                 indent_len++;
2104         }
2105
2106         return indent_len;
2107 }
2108
2109 /* insert quotation string when line was wrapped */
2110 static guint ins_quote(GtkSText *text, guint quote_len, guint indent_len,
2111                        guint prev_line_pos, guint text_len,
2112                        gchar *quote_fmt)
2113 {
2114         guint i, ins_len;
2115         gchar ch;
2116
2117         if (indent_len) {
2118                 for (i = 0; i < indent_len; i++) {
2119                         ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2120                         gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2121                 }
2122                 ins_len = indent_len;
2123         } else {
2124                 gtk_stext_insert(text, NULL, NULL, NULL, quote_fmt, quote_len);
2125                 ins_len = quote_len;
2126         }
2127
2128         return ins_len;
2129 }
2130
2131 #undef WRAP_DEBUG
2132 #ifdef WRAP_DEBUG
2133 /* Darko: used when I debug wrapping */
2134 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2135 {
2136         gint i;
2137         gchar ch;
2138
2139         printf("%d [", pos);
2140         for (i = pos; i < tlen; i++) {
2141                 ch = GTK_STEXT_INDEX(text, i);
2142                 if (breakoncr && ch == '\n')
2143                         break;
2144                 printf("%c", ch);
2145         }
2146         printf("]\n");
2147 }
2148 #endif
2149
2150 static void compose_wrap_line_all(Compose *compose)
2151 {
2152         GtkSText *text = GTK_STEXT(compose->text);
2153         guint tlen;
2154         guint line_pos = 0, cur_pos = 0, p_pos = 0;
2155         gint line_len = 0, cur_len = 0;
2156         gint ch_len;
2157         gboolean is_new_line = TRUE, do_delete = FALSE;
2158         guint qlen = 0, i_len = 0;
2159         guint linewrap_quote = prefs_common.linewrap_quote;
2160         guint linewrap_len = prefs_common.linewrap_len;
2161         gchar *qfmt = prefs_common.quotemark;
2162         gchar cbuf[MB_LEN_MAX];
2163
2164         gtk_stext_freeze(text);
2165
2166         /* make text buffer contiguous */
2167         /* gtk_stext_compact_buffer(text); */
2168
2169         tlen = gtk_stext_get_length(text);
2170
2171         for (; cur_pos < tlen; cur_pos++) {
2172                 /* mark position of new line - needed for quotation wrap */
2173                 if (linewrap_quote && is_new_line) {
2174                         qlen = gtkut_text_str_compare
2175                                 (text, cur_pos, tlen, qfmt);
2176                         is_new_line = FALSE;
2177                         if (qlen)
2178                                 i_len = get_indent_length(text, cur_pos, tlen);
2179                         else
2180                                 i_len = 0;
2181                         p_pos = cur_pos;
2182 #ifdef WRAP_DEBUG
2183                         printf("new line i_len=%d qlen=%d p_pos=", i_len, qlen);
2184                         dump_text(text, p_pos, tlen, 1);
2185 #endif
2186                 }
2187
2188                 GET_CHAR(cur_pos, cbuf, ch_len);
2189
2190                 /* fix line length for tabs */
2191                 if (ch_len == 1 && *cbuf == '\t') {
2192                         guint tab_width = text->default_tab_width;
2193                         guint tab_offset = line_len % tab_width;
2194
2195 #ifdef WRAP_DEBUG
2196                         printf("found tab at pos=%d line_len=%d ", cur_pos,
2197                                 line_len);
2198 #endif
2199                         if (tab_offset) {
2200                                 line_len += tab_width - tab_offset - 1;
2201                                 cur_len = line_len;
2202                         }
2203 #ifdef WRAP_DEBUG
2204                         printf("new_len=%d\n", line_len);
2205 #endif
2206                 }
2207
2208                 /* we have encountered line break */
2209                 if (ch_len == 1 && *cbuf == '\n') {
2210                         gint clen;
2211                         guint ilen;
2212                         gchar cb[MB_CUR_MAX];
2213
2214 #ifdef WRAP_DEBUG
2215                         printf("found CR at %d next line is ", cur_pos);
2216                         dump_text(text, cur_pos + 1, tlen, 1);
2217 #endif
2218                         /* if it's just quotation + newline skip it */
2219                         if (i_len && (cur_pos + 1 < tlen)) {
2220                                 /* check if text at new line matches indent */
2221                                 ilen =  gtkut_text_str_compare_n
2222                                         (text, cur_pos + 1, p_pos, i_len, tlen);
2223                                 if (cur_pos + ilen < tlen) {
2224                                         GET_CHAR(cur_pos + ilen + 1, cb, clen);
2225                                         /* no need to join the lines */
2226                                         if (clen == 1 && cb[0] == '\n')
2227                                                 do_delete = FALSE;
2228                                 }
2229                         /* if it's just newline skip it */
2230                         } else if (do_delete && (cur_pos + 1 < tlen)) {
2231                                 GET_CHAR(cur_pos + 1, cb, clen);
2232                                 /* no need to join the lines */
2233                                 if (clen == 1 && cb[0] == '\n')
2234                                         do_delete = FALSE;
2235                         }
2236
2237 #ifdef WRAP_DEBUG
2238                         printf("qlen=%d l_len=%d wrap_len=%d do_del=%d\n",
2239                                 qlen, line_len, linewrap_len, do_delete);
2240 #endif
2241                         /* should we delete to perform smart wrapping */
2242                         if (line_len < linewrap_len && do_delete) {
2243                                 /* get rid of newline */
2244                                 gtk_stext_set_point(text, cur_pos);
2245                                 gtk_stext_forward_delete(text, 1);
2246                                 tlen--;
2247
2248                                 /* if text starts with quote fmt or with
2249                                    indent string, delete them */
2250                                 if (i_len) {
2251                                         ilen =  gtkut_text_str_compare_n
2252                                                 (text, cur_pos, p_pos, i_len,
2253                                                  tlen);
2254                                         if (ilen) {
2255                                                 gtk_stext_forward_delete(text,
2256                                                                          ilen);
2257                                                 tlen -= ilen;
2258                                         }
2259                                 } else if (qlen) {
2260                                         if (gtkut_text_str_compare
2261                                             (text, cur_pos, tlen, qfmt)) {
2262                                                 gtk_stext_forward_delete
2263                                                         (text, qlen);
2264                                                 tlen -= qlen;
2265                                         }
2266                                 }
2267
2268                                 GET_CHAR(cur_pos, cb, clen);
2269
2270                                 /* insert space if it's alphanumeric */
2271                                 if ((cur_pos != line_pos) &&
2272                                     ((clen > 1) || isalnum(cb[0]))) {
2273                                         gtk_stext_insert(text, NULL, NULL,
2274                                                         NULL, " ", 1);
2275                                         /* gtk_text_compact_buffer(text); */
2276                                         tlen++;
2277                                 }
2278
2279                                 /* and start over with current line */
2280                                 cur_pos = p_pos - 1;
2281                                 line_pos = cur_pos;
2282                                 line_len = cur_len = 0;
2283                                 qlen = 0;
2284                                 do_delete = FALSE;
2285                                 is_new_line = TRUE;
2286 #ifdef WRAP_DEBUG
2287                                 printf("after delete l_pos=");
2288                                 dump_text(text, line_pos, tlen, 1);
2289 #endif
2290                                 continue;
2291                         }
2292
2293                         /* mark new line beginning */
2294                         line_pos = cur_pos + 1;
2295                         line_len = cur_len = 0;
2296                         qlen = 0;
2297                         do_delete = FALSE;
2298                         is_new_line = TRUE;
2299                         continue;
2300                 }
2301
2302                 if (ch_len < 0) {
2303                         cbuf[0] = '\0';
2304                         ch_len = 1;
2305                 }
2306
2307                 /* possible line break */
2308                 if (ch_len == 1 && isspace(*cbuf)) {
2309                         line_pos = cur_pos + 1;
2310                         line_len = cur_len + ch_len;
2311                 }
2312
2313                 /* are we over wrapping length set in preferences ? */
2314                 if (cur_len + ch_len > linewrap_len) {
2315                         gint clen;
2316
2317 #ifdef WRAP_DEBUG
2318                         printf("should wrap cur_pos=%d ", cur_pos);
2319                         dump_text(text, p_pos, tlen, 1);
2320                         dump_text(text, line_pos, tlen, 1);
2321 #endif
2322                         /* force wrapping if it is one long word but not URL */
2323                         if (p_pos + i_len == line_pos)
2324                                 if (!gtkut_text_is_uri_string
2325                                     (text, line_pos, tlen))
2326                                         line_pos = cur_pos - 1;
2327 #ifdef WRAP_DEBUG
2328                         printf("new line_pos=%d\n", line_pos);
2329 #endif
2330
2331                         GET_CHAR(line_pos - 1, cbuf, clen);
2332
2333                         /* if next character is space delete it */
2334                         if (clen == 1 && isspace(*cbuf)) {
2335                                 if (p_pos + i_len != line_pos ||
2336                                     !gtkut_text_is_uri_string
2337                                         (text, line_pos, tlen)) {
2338                                         gtk_stext_set_point(text, line_pos);
2339                                         gtk_stext_backward_delete(text, 1);
2340                                         tlen--;
2341                                         cur_pos--;
2342                                         line_pos--;
2343                                         cur_len--;
2344                                         line_len--;
2345                                 }
2346                         }
2347
2348                         /* if it is URL at beginning of line don't wrap */
2349                         if (p_pos + i_len == line_pos &&
2350                             gtkut_text_is_uri_string(text, line_pos, tlen)) {
2351 #ifdef WRAP_DEBUG
2352                                 printf("found URL at ");
2353                                 dump_text(text, line_pos, tlen, 1);
2354 #endif
2355                                 continue;
2356                         }
2357
2358                         /* insert CR */
2359                         gtk_stext_set_point(text, line_pos);
2360                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2361                         /* gtk_stext_compact_buffer(text); */
2362                         tlen++;
2363                         line_pos++;
2364                         /* for loop will increase it */
2365                         cur_pos = line_pos - 1;
2366                         /* start over with current line */
2367                         is_new_line = TRUE;
2368                         line_len = 0;
2369                         cur_len = 0;
2370                         do_delete = TRUE;
2371 #ifdef WRAP_DEBUG
2372                         printf("after CR insert ");
2373                         dump_text(text, line_pos, tlen, 1);
2374                         dump_text(text, cur_pos, tlen, 1);
2375 #endif
2376
2377                         /* should we insert quotation ? */
2378                         if (linewrap_quote && qlen) {
2379                                 /* only if line is not already quoted  */
2380                                 if (!gtkut_text_str_compare
2381                                         (text, line_pos, tlen, qfmt)) {
2382                                         guint ins_len;
2383
2384                                         if (line_pos - p_pos > i_len) {
2385                                                 ins_len = ins_quote
2386                                                         (text, qlen, i_len,
2387                                                          p_pos, tlen, qfmt);
2388
2389                                                 /* gtk_stext_compact_buffer(text); */
2390                                                 tlen += ins_len;
2391                                         }
2392 #ifdef WRAP_DEBUG
2393                                         printf("after quote insert ");
2394                                         dump_text(text, line_pos, tlen, 1);
2395 #endif
2396                                 }
2397                         }
2398                         continue;
2399                 }
2400
2401                 if (ch_len > 1) {
2402                         line_pos = cur_pos + 1;
2403                         line_len = cur_len + ch_len;
2404                 }
2405                 /* advance to next character in buffer */
2406                 cur_len += ch_len;
2407         }
2408
2409         gtk_stext_thaw(text);
2410 }
2411 #undef GET_CHAR
2412
2413 static void compose_set_title(Compose *compose)
2414 {
2415         gchar *str;
2416         gchar *edited;
2417
2418         edited = compose->modified ? _(" [Edited]") : "";
2419         if (compose->account && compose->account->address)
2420                 str = g_strdup_printf(_("%s - Compose message%s"),
2421                                       compose->account->address, edited);
2422         else
2423                 str = g_strdup_printf(_("Compose message%s"), edited);
2424         gtk_window_set_title(GTK_WINDOW(compose->window), str);
2425         g_free(str);
2426 }
2427
2428 /**
2429  * compose_current_mail_account:
2430  * 
2431  * Find a current mail account (the currently selected account, or the
2432  * default account, if a news account is currently selected).  If a
2433  * mail account cannot be found, display an error message.
2434  * 
2435  * Return value: Mail account, or NULL if not found.
2436  **/
2437 static PrefsAccount *
2438 compose_current_mail_account(void)
2439 {
2440         PrefsAccount *ac;
2441
2442         if (cur_account && cur_account->protocol != A_NNTP)
2443                 ac = cur_account;
2444         else {
2445                 ac = account_get_default();
2446                 if (!ac || ac->protocol == A_NNTP) {
2447                         alertpanel_error(_("Account for sending mail is not specified.\n"
2448                                            "Please select a mail account before sending."));
2449                         return NULL;
2450                 }
2451         }
2452         return ac;
2453 }
2454
2455 gboolean compose_check_for_valid_recipient(Compose *compose) {
2456         gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
2457         gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
2458         gboolean recipient_found = FALSE;
2459         GSList *list;
2460         gchar **strptr;
2461
2462         /* free to and newsgroup list */
2463         slist_free_strings(compose->to_list);
2464         g_slist_free(compose->to_list);
2465         compose->to_list = NULL;
2466                         
2467         slist_free_strings(compose->newsgroup_list);
2468         g_slist_free(compose->newsgroup_list);
2469         compose->newsgroup_list = NULL;
2470
2471         /* search header entries for to and newsgroup entries */
2472         for(list = compose->header_list; list; list = list->next) {
2473                 gchar *header;
2474                 gchar *entry;
2475                 header = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry));
2476                 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
2477                 g_strstrip(entry);
2478                 if(entry[0] != '\0') {
2479                         for(strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
2480                                 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2481                                         compose->to_list = address_list_append(compose->to_list, entry);
2482                                         recipient_found = TRUE;
2483                                 }
2484                         }
2485                         for(strptr = recipient_headers_news; *strptr != NULL; strptr++) {
2486                                 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2487                                         compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
2488                                         recipient_found = TRUE;
2489                                 }
2490                         }
2491                 }
2492                 g_free(entry);
2493         }
2494         return recipient_found;
2495 }
2496
2497 gint compose_send(Compose *compose)
2498 {
2499         gint msgnum;
2500         FolderItem *folder;
2501         gint val;
2502
2503         val = compose_queue(compose, &msgnum, &folder);
2504         if (val) {
2505                 alertpanel_error(_("Could not queue message for sending"));
2506                 return -1;
2507         }
2508         
2509         val = procmsg_send_message_queue(folder_item_fetch_msg(folder, msgnum));
2510         if(!val) {
2511                 folder_item_remove_msg(folder, msgnum);
2512                 folderview_update_item(folder, TRUE);
2513         }
2514
2515         return val;
2516 }
2517
2518 #if 0 /* compose restructure */
2519 gint compose_send(Compose *compose)
2520 {
2521         gchar tmp[MAXPATHLEN + 1];
2522         gint ok = 0;
2523         static gboolean lock = FALSE;
2524
2525         if (lock) return 1;
2526
2527         g_return_val_if_fail(compose->account != NULL, -1);
2528         g_return_val_if_fail(compose->orig_account != NULL, -1);
2529
2530         lock = TRUE;
2531
2532         if(!compose_check_for_valid_recipient(compose)) {
2533                 alertpanel_error(_("Recipient is not specified."));
2534                 lock = FALSE;
2535                 return 1;
2536         }
2537
2538         /* write to temporary file */
2539         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
2540                    get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
2541
2542         if (prefs_common.linewrap_at_send)
2543                 compose_wrap_line_all(compose);
2544
2545         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
2546                 lock = FALSE;
2547                 return -1;
2548         }
2549
2550         if (!compose->to_list && !compose->newsgroup_list) {
2551                 g_warning(_("can't get recipient list."));
2552                 unlink(tmp);
2553                 lock = FALSE;
2554                 return -1;
2555         }
2556
2557         if (compose->to_list) {
2558                 PrefsAccount *ac;
2559
2560 #if 0 /* NEW COMPOSE GUI */
2561                 if (compose->account->protocol != A_NNTP)
2562                         ac = compose->account;
2563                 else if (compose->orig_account->protocol != A_NNTP)
2564                         ac = compose->orig_account;
2565                 else if (cur_account && cur_account->protocol != A_NNTP)
2566                         ac = cur_account;
2567                 else {
2568                         ac = compose_current_mail_account();
2569                         if (!ac) {
2570                                 unlink(tmp);
2571                                 lock = FALSE;
2572                                 return -1;
2573                         }
2574                 }
2575 #endif
2576                 ac = compose->account;
2577
2578                 ok = send_message(tmp, ac, compose->to_list);
2579                 statusbar_pop_all();
2580         }
2581
2582         if (ok == 0 && compose->newsgroup_list) {
2583                 Folder *folder;
2584
2585                 if (compose->account->protocol == A_NNTP)
2586                         folder = FOLDER(compose->account->folder);
2587                 else
2588                         folder = FOLDER(compose->orig_account->folder);
2589
2590                 ok = news_post(folder, tmp);
2591                 if (ok < 0) {
2592                         alertpanel_error(_("Error occurred while posting the message to %s ."),
2593                                          compose->account->nntp_server);
2594                         unlink(tmp);
2595                         lock = FALSE;
2596                         return -1;
2597                 }
2598         }
2599
2600         /* queue message if failed to send */
2601         if (ok < 0) {
2602                 if (prefs_common.queue_msg) {
2603                         AlertValue val;
2604
2605                         val = alertpanel
2606                                 (_("Queueing"),
2607                                  _("Error occurred while sending the message.\n"
2608                                    "Put this message into queue folder?"),
2609                                  _("OK"), _("Cancel"), NULL);
2610                         if (G_ALERTDEFAULT == val) {
2611                                 ok = compose_queue(compose, tmp);
2612                                 if (ok < 0)
2613                                         alertpanel_error(_("Can't queue the message."));
2614                         }
2615                 } else
2616                         alertpanel_error(_("Error occurred while sending the message."));
2617         } else {
2618                 if (compose->mode == COMPOSE_REEDIT) {
2619                         compose_remove_reedit_target(compose);
2620                         if (compose->targetinfo)
2621                                 folderview_update_item
2622                                         (compose->targetinfo->folder, TRUE);
2623                 }
2624         }
2625
2626         /* save message to outbox */
2627         if (ok == 0 && prefs_common.savemsg) {
2628                 if (compose_save_to_outbox(compose, tmp) < 0)
2629                         alertpanel_error
2630                                 (_("Can't save the message to outbox."));
2631         }
2632
2633         unlink(tmp);
2634         lock = FALSE;
2635         return ok;
2636 }
2637 #endif
2638
2639 static gboolean compose_use_attach(Compose *compose) {
2640     return(gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL);
2641 }
2642
2643 static gint compose_bounce_write_headers_from_headerlist(Compose *compose, 
2644                                                          FILE *fp)
2645 {
2646         gchar buf[BUFFSIZE];
2647         gchar *str;
2648         gboolean first_address;
2649         GSList *list;
2650         ComposeHeaderEntry *headerentry;
2651         gchar *headerentryname;
2652         gchar *header_w_colon;
2653         gchar *cc_hdr;
2654         gchar *to_hdr;
2655
2656         debug_print(_("Writing bounce header\n"));
2657
2658         header_w_colon = g_strconcat("To:", NULL);
2659         to_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2660         header_w_colon = g_strconcat("Cc:", NULL);
2661         cc_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2662         
2663         first_address = TRUE;
2664         for(list = compose->header_list; list; list = list->next) {
2665                 headerentry = ((ComposeHeaderEntry *)list->data);
2666                 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
2667
2668                 if(g_strcasecmp(headerentryname, cc_hdr) == 0 
2669                    || g_strcasecmp(headerentryname, to_hdr) == 0) {
2670                         str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
2671                         Xstrdup_a(str, str, return -1);
2672                         g_strstrip(str);
2673                         if(str[0] != '\0') {
2674                                 compose_convert_header
2675                                         (buf, sizeof(buf), str,
2676                                         strlen("Resent-To") + 2);
2677                                 if(first_address) {
2678                                         fprintf(fp, "Resent-To: ");
2679                                         first_address = FALSE;
2680                                 } else {
2681                                         fprintf(fp, ",");
2682                                 }
2683                                 fprintf(fp, "%s", buf);
2684                         }
2685                 }
2686         }
2687         /* if(!first_address) { */
2688         fprintf(fp, "\n");
2689         /* } */
2690
2691         return(0);
2692 }
2693
2694 static gint compose_bounce_write_headers(Compose *compose, FILE *fp)
2695 {
2696         gchar buf[BUFFSIZE];
2697         gchar *str;
2698         /* struct utsname utsbuf; */
2699
2700         g_return_val_if_fail(fp != NULL, -1);
2701         g_return_val_if_fail(compose->account != NULL, -1);
2702         g_return_val_if_fail(compose->account->address != NULL, -1);
2703
2704         /* Date */
2705         get_rfc822_date(buf, sizeof(buf));
2706         fprintf(fp, "Resent-Date: %s\n", buf);
2707
2708         /* From */
2709         if (compose->account->name && *compose->account->name) {
2710                 compose_convert_header
2711                         (buf, sizeof(buf), compose->account->name,
2712                          strlen("From: "));
2713                 fprintf(fp, "Resent-From: %s <%s>\n",
2714                         buf, compose->account->address);
2715         } else
2716                 fprintf(fp, "Resent-From: %s\n", compose->account->address);
2717
2718         /* To */
2719         compose_bounce_write_headers_from_headerlist(compose, fp);
2720
2721         /* separator between header and body */
2722         fputs("\n", fp);
2723
2724         return 0;
2725 }
2726
2727 static gint compose_bounce_write_to_file(Compose *compose, const gchar *file)
2728 {
2729         FILE *fp;
2730         FILE *fdest;
2731         size_t len;
2732         gchar buf[BUFFSIZE];
2733
2734         if ((fp = fopen(compose->bounce_filename, "r")) == NULL) {
2735                 FILE_OP_ERROR(file, "fopen");
2736                 return -1;
2737         }
2738
2739         if ((fdest = fopen(file, "w")) == NULL) {
2740                 FILE_OP_ERROR(file, "fopen");
2741                 fclose(fp);
2742                 return -1;
2743         }
2744
2745         /* chmod for security */
2746         if (change_file_mode_rw(fdest, file) < 0) {
2747                 FILE_OP_ERROR(file, "chmod");
2748                 g_warning(_("can't change file mode\n"));
2749         }
2750
2751         while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
2752                 /* should filter returnpath, delivered-to */
2753                 if ((g_strncasecmp(buf, "Return-Path:",
2754                                    strlen("Return-Path:")) == 0)
2755                     || (g_strncasecmp(buf, "Delivered-To:",
2756                                       strlen("Delivered-To:")) == 0))
2757                         continue;
2758
2759                 if (fputs(buf, fdest) == -1)
2760                         goto error;
2761
2762                 if (g_strncasecmp(buf, "From:", strlen("From:")) == 0) {
2763                         fputs(" (by way of ", fdest);
2764                         if (compose->account->name
2765                             && *compose->account->name) {
2766                                 compose_convert_header
2767                                         (buf, sizeof(buf),
2768                                          compose->account->name,
2769                                          strlen("From: "));
2770                                 fprintf(fdest, "%s <%s>",
2771                                         buf, compose->account->address);
2772                         } else
2773                                 fprintf(fdest, "%s",
2774                                         compose->account->address);
2775                         fputs(")", fdest);
2776                 }
2777
2778                 if (fputs("\n", fdest) == -1)
2779                         goto error;
2780         }
2781
2782         compose_bounce_write_headers(compose, fdest);
2783
2784         while ((len = fread(buf, sizeof(gchar), BUFFSIZE, fp)) > 0) {
2785                 if (fwrite(buf, sizeof(gchar), len, fdest) == -1)
2786                         goto error;
2787         }
2788
2789         fclose(fdest);
2790         fclose(fp);
2791
2792         return 0;
2793  error:
2794         fclose(fdest);
2795         fclose(fp);
2796
2797         return -1;
2798 }
2799
2800 static gint compose_write_to_file(Compose *compose, const gchar *file,
2801                                   gboolean is_draft)
2802 {
2803         FILE *fp;
2804         size_t len;
2805         gchar *chars;
2806         gchar *buf;
2807         const gchar *out_codeset;
2808         EncodingType encoding;
2809
2810         if ((fp = fopen(file, "w")) == NULL) {
2811                 FILE_OP_ERROR(file, "fopen");
2812                 return -1;
2813         }
2814
2815         /* chmod for security */
2816         if (change_file_mode_rw(fp, file) < 0) {
2817                 FILE_OP_ERROR(file, "chmod");
2818                 g_warning(_("can't change file mode\n"));
2819         }
2820
2821         /* get all composed text */
2822         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2823         len = strlen(chars);
2824         if (is_ascii_str(chars)) {
2825                 buf = g_strdup(chars);
2826                 out_codeset = CS_US_ASCII;
2827                 encoding = ENC_7BIT;
2828         } else {
2829                 const gchar *src_codeset;
2830
2831                 out_codeset = conv_get_outgoing_charset_str();
2832                 if (!strcasecmp(out_codeset, CS_US_ASCII))
2833                         out_codeset = CS_ISO_8859_1;
2834                 encoding = procmime_get_encoding_for_charset(out_codeset);
2835
2836                 src_codeset = conv_get_current_charset_str();
2837                 /* if current encoding is US-ASCII, set it the same as
2838                    outgoing one to prevent code conversion failure */
2839                 if (!strcasecmp(src_codeset, CS_US_ASCII))
2840                         src_codeset = out_codeset;
2841
2842                 debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
2843                             src_codeset, out_codeset, procmime_get_encoding_str(encoding));
2844
2845                 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
2846                 if (!buf) {
2847                         g_free(chars);
2848                         fclose(fp);
2849                         unlink(file);
2850                         alertpanel_error(_("Can't convert the codeset of the message."));
2851                         return -1;
2852                 }
2853         }
2854         g_free(chars);
2855
2856         /* write headers */
2857         if (compose_write_headers
2858                 (compose, fp, out_codeset, encoding, is_draft) < 0) {
2859                 g_warning(_("can't write headers\n"));
2860                 fclose(fp);
2861                 /* unlink(file); */
2862                 g_free(buf);
2863                 return -1;
2864         }
2865
2866         if (compose_use_attach(compose)) {
2867 #if USE_GPGME
2868             /* This prolog message is ignored by mime software and
2869              * because it would make our signing/encryption task
2870              * tougher, we don't emit it in that case */
2871             if (!compose->use_signing && !compose->use_encryption)
2872 #endif
2873                 fputs("This is a multi-part message in MIME format.\n", fp);
2874
2875                 fprintf(fp, "\n--%s\n", compose->boundary);
2876                 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
2877                         out_codeset);
2878                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2879                         procmime_get_encoding_str(encoding));
2880                 fputc('\n', fp);
2881         }
2882
2883         /* write body */
2884         len = strlen(buf);
2885         if (encoding == ENC_BASE64) {
2886                 gchar outbuf[B64_BUFFSIZE];
2887                 gint i, l;
2888
2889                 for (i = 0; i < len; i += B64_LINE_SIZE) {
2890                         l = MIN(B64_LINE_SIZE, len - i);
2891                         to64frombits(outbuf, buf + i, l);
2892                         fputs(outbuf, fp);
2893                         fputc('\n', fp);
2894                 }
2895         } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
2896                 FILE_OP_ERROR(file, "fwrite");
2897                 fclose(fp);
2898                 unlink(file);
2899                 g_free(buf);
2900                 return -1;
2901         }
2902         g_free(buf);
2903
2904         if (compose_use_attach(compose))
2905                 compose_write_attach(compose, fp);
2906
2907         if (fclose(fp) == EOF) {
2908                 FILE_OP_ERROR(file, "fclose");
2909                 unlink(file);
2910                 return -1;
2911         }
2912
2913 #if USE_GPGME
2914         if (compose->use_signing) {
2915                 if (rfc2015_sign(file, compose->account) < 0) {
2916                         unlink(file);
2917                         return -1;
2918                 }
2919         }
2920         if (compose->use_encryption) {
2921                 if (rfc2015_encrypt(file, compose->to_list) < 0) {
2922                         unlink(file);
2923                         return -1;
2924                 }
2925         }
2926 #endif /* USE_GPGME */
2927
2928         return 0;
2929 }
2930
2931 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
2932 {
2933         FILE *fp;
2934         size_t len;
2935         gchar *chars;
2936
2937         if ((fp = fopen(file, "w")) == NULL) {
2938                 FILE_OP_ERROR(file, "fopen");
2939                 return -1;
2940         }
2941
2942         /* chmod for security */
2943         if (change_file_mode_rw(fp, file) < 0) {
2944                 FILE_OP_ERROR(file, "chmod");
2945                 g_warning(_("can't change file mode\n"));
2946         }
2947
2948         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2949
2950         /* write body */
2951         len = strlen(chars);
2952         if (fwrite(chars, sizeof(gchar), len, fp) != len) {
2953                 FILE_OP_ERROR(file, "fwrite");
2954                 g_free(chars);
2955                 fclose(fp);
2956                 unlink(file);
2957                 return -1;
2958         }
2959
2960         g_free(chars);
2961
2962         if (fclose(fp) == EOF) {
2963                 FILE_OP_ERROR(file, "fclose");
2964                 unlink(file);
2965                 return -1;
2966         }
2967         return 0;
2968 }
2969
2970 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
2971 {
2972         FolderItem *outbox;
2973         gchar *path;
2974         gint num;
2975         FILE *fp;
2976
2977         debug_print(_("saving sent message...\n"));
2978
2979         outbox = folder_get_default_outbox();
2980         path = folder_item_get_path(outbox);
2981         if (!is_dir_exist(path))
2982                 make_dir_hier(path);
2983
2984         folder_item_scan(outbox);
2985         if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
2986                 g_free(path);
2987                 g_warning(_("can't save message\n"));
2988                 return -1;
2989         }
2990
2991         if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
2992                 g_warning(_("can't open mark file\n"));
2993         else {
2994                 MsgInfo newmsginfo;
2995
2996                 newmsginfo.msgnum = num;
2997                 newmsginfo.flags.perm_flags = 0;
2998                 newmsginfo.flags.tmp_flags = 0;
2999                 procmsg_write_flags(&newmsginfo, fp);
3000                 fclose(fp);
3001         }
3002         g_free(path);
3003
3004         return 0;
3005 }
3006
3007 static gint compose_remove_reedit_target(Compose *compose)
3008 {
3009         FolderItem *item;
3010         MsgInfo *msginfo = compose->targetinfo;
3011
3012         g_return_val_if_fail(compose->mode == COMPOSE_REEDIT, -1);
3013         if (!msginfo) return -1;
3014
3015         item = msginfo->folder;
3016         g_return_val_if_fail(item != NULL, -1);
3017
3018         folder_item_scan(item);
3019         if (procmsg_msg_exist(msginfo) &&
3020             (item->stype == F_DRAFT || item->stype == F_QUEUE)) {
3021                 if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
3022                         g_warning(_("can't remove the old message\n"));
3023                         return -1;
3024                 }
3025         }
3026
3027         return 0;
3028 }
3029
3030
3031 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
3032 {
3033         FolderItem *queue;
3034         gchar *tmp, *tmp2, *queue_path;
3035         FILE *fp, *src_fp;
3036         GSList *cur;
3037         gchar buf[BUFFSIZE];
3038         gint num;
3039         static gboolean lock = FALSE;
3040         PrefsAccount *mailac = NULL, *newsac = NULL;
3041         
3042         debug_print(_("queueing message...\n"));
3043         g_return_val_if_fail(compose->account != NULL, -1);
3044         g_return_val_if_fail(compose->orig_account != NULL, -1);
3045
3046         lock = TRUE;
3047         
3048         if(!compose_check_for_valid_recipient(compose)) {
3049                 alertpanel_error(_("Recipient is not specified."));
3050                 lock = FALSE;
3051                 return -1;
3052         }
3053                                                                         
3054         if (!compose->to_list && !compose->newsgroup_list) {
3055                 g_warning(_("can't get recipient list."));
3056                 lock = FALSE;
3057                 return -1;
3058         }
3059
3060         if(compose->to_list) {
3061                 if (compose->account->protocol != A_NNTP)
3062                         mailac = compose->account;
3063                 else if (compose->orig_account->protocol != A_NNTP)
3064                         mailac = compose->orig_account;
3065                 else if (cur_account && cur_account->protocol != A_NNTP)
3066                         mailac = cur_account;
3067                 else if (!(mailac = compose_current_mail_account())) {
3068                         lock = FALSE;
3069                         alertpanel_error(_("No account for sending mails available!"));
3070                         return -1;
3071                 }
3072         }
3073
3074         if(compose->newsgroup_list) {
3075                 if (compose->account->protocol == A_NNTP)
3076                         newsac = compose->account;
3077                 else if(!(newsac = compose->orig_account) || (newsac->protocol != A_NNTP)) {
3078                         lock = FALSE;
3079                         alertpanel_error(_("No account for posting news available!"));
3080                         return -1;
3081                 }                       
3082         }
3083
3084         if (prefs_common.linewrap_at_send)
3085                 compose_wrap_line_all(compose);
3086                         
3087         /* write to temporary file */
3088         tmp2 = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
3089                                       G_DIR_SEPARATOR, (gint)compose);
3090
3091         if (compose->bounce_filename != NULL) {
3092                 if (compose_bounce_write_to_file(compose, tmp2) < 0) {
3093                         unlink(tmp2);
3094                         lock = FALSE;
3095                         return -1;
3096                 }
3097         }
3098         else {
3099                 if (compose_write_to_file(compose, tmp2, FALSE) < 0) {
3100                         unlink(tmp2);
3101                         lock = FALSE;
3102                         return -1;
3103                 }
3104         }
3105
3106         /* add queue header */
3107         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
3108                                       G_DIR_SEPARATOR, (gint)compose);
3109         if ((fp = fopen(tmp, "w")) == NULL) {
3110                 FILE_OP_ERROR(tmp, "fopen");
3111                 g_free(tmp);
3112                 return -1;
3113         }
3114         if ((src_fp = fopen(tmp2, "r")) == NULL) {
3115                 FILE_OP_ERROR(tmp2, "fopen");
3116                 fclose(fp);
3117                 unlink(tmp);
3118                 g_free(tmp);
3119                 unlink(tmp2);
3120                 g_free(tmp2);
3121                 return -1;
3122         }
3123         if (change_file_mode_rw(fp, tmp) < 0) {
3124                 FILE_OP_ERROR(tmp, "chmod");
3125                 g_warning(_("can't change file mode\n"));
3126         }
3127
3128         /* queueing variables */
3129         fprintf(fp, "AF:\n");
3130         fprintf(fp, "NF:0\n");
3131         fprintf(fp, "PS:10\n");
3132         fprintf(fp, "SRH:1\n");
3133         fprintf(fp, "SFN:\n");
3134         fprintf(fp, "DSR:\n");
3135         if (compose->msgid)
3136                 fprintf(fp, "MID:<%s>\n", compose->msgid);
3137         else
3138                 fprintf(fp, "MID:\n");
3139         fprintf(fp, "CFG:\n");
3140         fprintf(fp, "PT:0\n");
3141         fprintf(fp, "S:%s\n", compose->account->address);
3142         fprintf(fp, "RQ:\n");
3143         if (mailac)
3144                 fprintf(fp, "SSV:%s\n", mailac->smtp_server);
3145         else
3146                 fprintf(fp, "SSV:\n");
3147         if (newsac)
3148                 fprintf(fp, "NSV:%s\n", newsac->nntp_server);
3149         else
3150                 fprintf(fp, "NSV:\n");
3151         fprintf(fp, "SSH:\n");
3152         /* write recepient list */
3153         fprintf(fp, "R:");
3154         if(compose->to_list) {
3155                 fprintf(fp, "<%s>", (gchar *)compose->to_list->data);
3156                 for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
3157                         fprintf(fp, ",<%s>", (gchar *)cur->data);
3158         }
3159         fprintf(fp, "\n");
3160         /* write newsgroup list */
3161         fprintf(fp, "NG:");
3162         if(compose->newsgroup_list) {
3163                 fprintf(fp, "%s", (gchar *)compose->newsgroup_list->data);
3164                 for (cur = compose->newsgroup_list->next; cur != NULL; cur = cur->next)
3165                         fprintf(fp, ",%s", (gchar *)cur->data);
3166         }
3167         fprintf(fp, "\n");
3168         /* Sylpheed account IDs */
3169         if(mailac) {
3170                 fprintf(fp, "MAID:%d\n", mailac->account_id);
3171         }
3172         if(newsac) {
3173                 fprintf(fp, "NAID:%d\n", newsac->account_id);
3174         }
3175         fprintf(fp, "\n");
3176
3177         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
3178                 if (fputs(buf, fp) == EOF) {
3179                         FILE_OP_ERROR(tmp, "fputs");
3180                         fclose(fp);
3181                         fclose(src_fp);
3182                         unlink(tmp);
3183                         g_free(tmp);
3184                         unlink(tmp2);
3185                         g_free(tmp2);
3186                         return -1;
3187                 }
3188         }
3189         fclose(src_fp);
3190         if (fclose(fp) == EOF) {
3191                 FILE_OP_ERROR(tmp, "fclose");
3192                 unlink(tmp);
3193                 g_free(tmp);
3194                 unlink(tmp2);
3195                 g_free(tmp2);
3196                 return -1;
3197         }
3198                                                 
3199         /* queue message */
3200         queue = folder_get_default_queue();
3201
3202         folder_item_scan(queue);
3203         queue_path = folder_item_get_path(queue);
3204         if (!is_dir_exist(queue_path))
3205                 make_dir_hier(queue_path);
3206         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
3207                 g_warning(_("can't queue the message\n"));
3208                 unlink(tmp);
3209                 g_free(tmp);
3210                 g_free(queue_path);
3211                 return -1;
3212         }
3213         unlink(tmp);
3214         g_free(tmp);
3215         unlink(tmp2);
3216         g_free(tmp2);
3217
3218         if (compose->mode == COMPOSE_REEDIT) {
3219                 compose_remove_reedit_target(compose);
3220                 if (compose->targetinfo &&
3221                     compose->targetinfo->folder != queue)
3222                         folderview_update_item
3223                                 (compose->targetinfo->folder, TRUE);
3224         }
3225
3226         if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
3227                 g_warning(_("can't open mark file\n"));
3228         else {
3229                 MsgInfo newmsginfo;
3230
3231                 newmsginfo.msgnum = num;
3232                 newmsginfo.flags.perm_flags = 0;
3233                 newmsginfo.flags.tmp_flags = 0;
3234                 procmsg_write_flags(&newmsginfo, fp);
3235                 fclose(fp);
3236         }
3237         g_free(queue_path);
3238
3239         folder_item_scan(queue);
3240         folderview_update_item(queue, TRUE);
3241
3242         if((msgnum != NULL) && (item != NULL)) {
3243                 *msgnum = num;
3244                 *item = queue;
3245         }
3246
3247         return 0;
3248 }
3249
3250 static void compose_write_attach(Compose *compose, FILE *fp)
3251 {
3252         AttachInfo *ainfo;
3253         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3254         gint row;
3255         FILE *attach_fp;
3256         gchar filename[BUFFSIZE];
3257         gint len;
3258
3259         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
3260              row++) {
3261                 if ((attach_fp = fopen(ainfo->file, "r")) == NULL) {
3262                         g_warning(_("Can't open file %s\n"), ainfo->file);
3263                         continue;
3264                 }
3265
3266                 fprintf(fp, "\n--%s\n", compose->boundary);
3267
3268                 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
3269                         fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
3270                         fprintf(fp, "Content-Disposition: inline\n");
3271                 } else {
3272                         conv_encode_header(filename, sizeof(filename),
3273                                            ainfo->name, 12);
3274                         fprintf(fp, "Content-Type: %s;\n"
3275                                     " name=\"%s\"\n",
3276                                 ainfo->content_type, filename);
3277                         fprintf(fp, "Content-Disposition: attachment;\n"
3278                                     " filename=\"%s\"\n", filename);
3279                 }
3280
3281                 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
3282                         procmime_get_encoding_str(ainfo->encoding));
3283
3284                 if (ainfo->encoding == ENC_7BIT) {
3285                         gchar buf[BUFFSIZE];
3286
3287                         while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
3288                                 strcrchomp(buf);
3289                                 fputs(buf, fp);
3290                         }
3291                 } else {
3292                         gchar&nb