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