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