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