f2de61f466d87b9f761e4cc51b26da75acf74090
[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
960                 if (prefs_common.quotemark && *prefs_common.quotemark)
961                         qmark = prefs_common.quotemark;
962                 else
963                         qmark = "> ";
964
965                 compose_quote_fmt(compose, compose->replyinfo,
966                                   prefs_common.quotefmt,
967                                   qmark, body);
968         }
969
970         if (account->auto_sig)
971                 compose_insert_sig(compose, FALSE);
972
973         if (quote && prefs_common.linewrap_quote)
974                 compose_wrap_line_all(compose);
975
976         gtk_editable_set_position(GTK_EDITABLE(text), 0);
977         gtk_stext_set_point(text, 0);
978
979         gtk_stext_thaw(text);
980         gtk_widget_grab_focus(compose->text);
981
982         if (prefs_common.auto_exteditor)
983                 compose_exec_ext_editor(compose);
984 }
985
986 #define INSERT_FW_HEADER(var, hdr) \
987 if (msginfo->var && *msginfo->var) { \
988         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
989         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
990         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
991 }
992
993 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
994                          gboolean as_attach, const gchar *body)
995 {
996         Compose *compose;
997         GtkSText *text;
998
999         g_return_val_if_fail(msginfo != NULL, NULL);
1000         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1001
1002         if (msginfo->folder->prefs->enable_default_account)
1003                 account = account_find_from_id(msginfo->folder->prefs->default_account);
1004         if (!account) 
1005                 account = msginfo->folder->folder->account;
1006         if (!account && msginfo->to && prefs_common.forward_account_autosel) {
1007                 gchar *to;
1008                 Xstrdup_a(to, msginfo->to, return NULL);
1009                 extract_address(to);
1010                 account = account_find_from_address(to);
1011         }
1012
1013         if (!account && prefs_common.forward_account_autosel) {
1014                 gchar cc[BUFFSIZE];
1015                 if (!get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
1016                         extract_address(cc);
1017                         account = account_find_from_address(cc);
1018                 }
1019         }
1020
1021         if (account == NULL) {
1022                 account = cur_account;
1023                 /*
1024                 account = msginfo->folder->folder->account;
1025                 if (!account) account = cur_account;
1026                 */
1027         }
1028         g_return_val_if_fail(account != NULL, NULL);
1029
1030         compose = compose_create(account, COMPOSE_FORWARD);
1031
1032         compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
1033         if (!compose->fwdinfo)
1034                 compose->fwdinfo = procmsg_msginfo_copy(msginfo);
1035
1036         if (msginfo->subject && *msginfo->subject) {
1037                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
1038                 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
1039                                       msginfo->subject);
1040         }
1041
1042         text = GTK_STEXT(compose->text);
1043         gtk_stext_freeze(text);
1044
1045         if (as_attach) {
1046                 gchar *msgfile;
1047
1048                 msgfile = procmsg_get_message_file_path(msginfo);
1049                 if (!is_file_exist(msgfile))
1050                         g_warning("%s: file not exist\n", msgfile);
1051                 else
1052                         compose_attach_append(compose, msgfile, msgfile,
1053                                               "message/rfc822");
1054
1055                 g_free(msgfile);
1056         } else {
1057                 gchar *qmark;
1058                 MsgInfo *full_msginfo;
1059
1060                 full_msginfo = procmsg_msginfo_get_full_info(msginfo);
1061                 if (!full_msginfo)
1062                         full_msginfo = procmsg_msginfo_copy(msginfo);
1063
1064                 if (prefs_common.fw_quotemark &&
1065                     *prefs_common.fw_quotemark)
1066                         qmark = prefs_common.fw_quotemark;
1067                 else
1068                         qmark = "> ";
1069
1070                 compose_quote_fmt(compose, full_msginfo,
1071                                   prefs_common.fw_quotefmt,
1072                                   qmark, body);
1073                 compose_attach_parts(compose, msginfo);
1074
1075                 procmsg_msginfo_free(full_msginfo);
1076         }
1077
1078         if (account->auto_sig)
1079                 compose_insert_sig(compose, FALSE);
1080
1081         if (prefs_common.linewrap_quote)
1082                 compose_wrap_line_all(compose);
1083
1084         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1085         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1086
1087         gtk_stext_thaw(text);
1088 #if 0 /* NEW COMPOSE GUI */
1089         if (account->protocol != A_NNTP)
1090                 gtk_widget_grab_focus(compose->to_entry);
1091         else
1092                 gtk_widget_grab_focus(compose->newsgroups_entry);
1093 #endif
1094         gtk_widget_grab_focus(compose->header_last->entry);
1095
1096         if (prefs_common.auto_exteditor)
1097                 compose_exec_ext_editor(compose);
1098         
1099         /*save folder*/
1100         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1101                 gchar *folderidentifier;
1102
1103                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1104                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1105                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1106                 g_free(folderidentifier);
1107         }
1108
1109         return compose;
1110 }
1111
1112 #undef INSERT_FW_HEADER
1113
1114 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1115 {
1116         Compose *compose;
1117         GtkSText *text;
1118         GSList *msginfo;
1119         gchar *msgfile;
1120
1121         g_return_val_if_fail(msginfo_list != NULL, NULL);
1122         
1123         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1124                 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
1125                         return NULL;
1126         }
1127
1128         if (account == NULL) {
1129                 account = cur_account;
1130                 /*
1131                 account = msginfo->folder->folder->account;
1132                 if (!account) account = cur_account;
1133                 */
1134         }
1135         g_return_val_if_fail(account != NULL, NULL);
1136
1137         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1138                 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1139                 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1140         }
1141
1142         compose = compose_create(account, COMPOSE_FORWARD);
1143
1144         text = GTK_STEXT(compose->text);
1145         gtk_stext_freeze(text);
1146
1147         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1148                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1149                 if (!is_file_exist(msgfile))
1150                         g_warning("%s: file not exist\n", msgfile);
1151                 else
1152                         compose_attach_append(compose, msgfile, msgfile,
1153                                 "message/rfc822");
1154                 g_free(msgfile);
1155         }
1156
1157         if (account->auto_sig)
1158                 compose_insert_sig(compose, FALSE);
1159
1160         if (prefs_common.linewrap_quote)
1161                 compose_wrap_line_all(compose);
1162
1163         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1164         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1165
1166         gtk_stext_thaw(text);
1167         gtk_widget_grab_focus(compose->header_last->entry);
1168         
1169 #if 0 /* NEW COMPOSE GUI */
1170         if (account->protocol != A_NNTP)
1171                 gtk_widget_grab_focus(compose->to_entry);
1172         else
1173                 gtk_widget_grab_focus(compose->newsgroups_entry);
1174 #endif
1175
1176         return compose;
1177 }
1178
1179 void compose_reedit(MsgInfo *msginfo)
1180 {
1181         Compose *compose;
1182         PrefsAccount *account = NULL;
1183         GtkSText *text;
1184         FILE *fp;
1185         gchar buf[BUFFSIZE];
1186
1187         g_return_if_fail(msginfo != NULL);
1188         g_return_if_fail(msginfo->folder != NULL);
1189
1190         if (msginfo->folder->stype == F_QUEUE || msginfo->folder->stype == F_DRAFT) {
1191                 gchar queueheader_buf[BUFFSIZE];
1192                 gint id;
1193
1194                 /* Select Account from queue headers */
1195                 if (!get_header_from_msginfo(msginfo, queueheader_buf, 
1196                                              sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
1197                         id = atoi(&queueheader_buf[22]);
1198                         account = account_find_from_id(id);
1199                 }
1200                 if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
1201                                              sizeof(queueheader_buf), "NAID:")) {
1202                         id = atoi(&queueheader_buf[5]);
1203                         account = account_find_from_id(id);
1204                 }
1205                 if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
1206                                                     sizeof(queueheader_buf), "MAID:")) {
1207                         id = atoi(&queueheader_buf[5]);
1208                         account = account_find_from_id(id);
1209                 }
1210                 if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
1211                                                                 sizeof(queueheader_buf), "S:")) {
1212                         account = account_find_from_address(queueheader_buf);
1213                 }
1214         } else 
1215                 account = msginfo->folder->folder->account;
1216
1217         if (!account && prefs_common.reedit_account_autosel) {
1218                 gchar from[BUFFSIZE];
1219                 if (!get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
1220                         extract_address(from);
1221                         account = account_find_from_address(from);
1222                 }
1223         }
1224         if (!account) account = cur_account;
1225         g_return_if_fail(account != NULL);
1226
1227         compose = compose_create(account, COMPOSE_REEDIT);
1228         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1229
1230         if (msginfo->folder->stype == F_QUEUE
1231         ||  msginfo->folder->stype == F_DRAFT) {
1232                 gchar queueheader_buf[BUFFSIZE];
1233
1234                 /* Set message save folder */
1235                 if (!get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1236                         gint startpos = 0;
1237
1238                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1239                         gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1240                         gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1241                 }
1242         }
1243         
1244         if (compose_parse_header(compose, msginfo) < 0) return;
1245         compose_reedit_set_entry(compose, msginfo);
1246
1247         text = GTK_STEXT(compose->text);
1248         gtk_stext_freeze(text);
1249
1250         if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1251                 g_warning("Can't get text part\n");
1252         else {
1253                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1254                         strcrchomp(buf);
1255                         gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1256                 }
1257                 fclose(fp);
1258         }
1259         compose_attach_parts(compose, msginfo);
1260
1261         gtk_stext_thaw(text);
1262         gtk_widget_grab_focus(compose->text);
1263
1264         if (prefs_common.auto_exteditor)
1265                 compose_exec_ext_editor(compose);
1266 }
1267
1268 Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
1269 {
1270         Compose *compose;
1271         gchar *filename;
1272         GtkItemFactory *ifactory;
1273
1274         g_return_val_if_fail(msginfo != NULL, NULL);
1275
1276         if (!account)
1277                 account = cur_account;
1278         g_return_val_if_fail(account != NULL, NULL);
1279
1280         compose = compose_create(account, COMPOSE_REDIRECT);
1281         ifactory = gtk_item_factory_from_widget(compose->menubar);
1282
1283         compose->replyinfo = NULL;
1284         compose->fwdinfo = NULL;
1285
1286         compose_show_first_last_header(compose, TRUE);
1287
1288         gtk_widget_grab_focus(compose->header_last->entry);
1289
1290         filename = procmsg_get_message_file(msginfo);
1291         if (filename == NULL)
1292                 return NULL;
1293
1294         compose->redirect_filename = filename;
1295         
1296         compose_attach_parts(compose, msginfo);
1297
1298         if (msginfo->subject)
1299                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
1300                                    msginfo->subject);
1301         gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
1302
1303         gtk_stext_freeze(GTK_STEXT(compose->text));
1304         compose_quote_fmt(compose, msginfo, "%M", NULL, NULL);
1305         gtk_editable_set_editable(GTK_EDITABLE(compose->text), FALSE);
1306         gtk_stext_thaw(GTK_STEXT(compose->text));
1307
1308         ifactory = gtk_item_factory_from_widget(compose->popupmenu);
1309         menu_set_sensitive(ifactory, "/Add...", FALSE);
1310         menu_set_sensitive(ifactory, "/Remove", FALSE);
1311         menu_set_sensitive(ifactory, "/Property...", FALSE);
1312
1313         ifactory = gtk_item_factory_from_widget(compose->menubar);
1314         menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
1315         menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
1316         menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
1317         menu_set_sensitive(ifactory, "/Edit", FALSE);
1318         menu_set_sensitive(ifactory, "/Message/Save to draft folder", FALSE);
1319         menu_set_sensitive(ifactory, "/Message/Save and keep editing", FALSE);
1320 #if USE_GPGME
1321         menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
1322         menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
1323         menu_set_sensitive(ifactory, "/Message/Mode/MIME", FALSE);
1324         menu_set_sensitive(ifactory, "/Message/Mode/Inline", FALSE);
1325 #endif
1326         menu_set_sensitive(ifactory, "/Message/Priority", FALSE);
1327         menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
1328         menu_set_sensitive(ifactory, "/Tools/Show ruler", FALSE);
1329         menu_set_sensitive(ifactory, "/Tools/Actions", FALSE);
1330         
1331         gtk_widget_set_sensitive(compose->toolbar->draft_btn, FALSE);
1332         gtk_widget_set_sensitive(compose->toolbar->insert_btn, FALSE);
1333         gtk_widget_set_sensitive(compose->toolbar->attach_btn, FALSE);
1334         gtk_widget_set_sensitive(compose->toolbar->sig_btn, FALSE);
1335         gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, FALSE);
1336         gtk_widget_set_sensitive(compose->toolbar->linewrap_btn, FALSE);
1337
1338         return compose;
1339 }
1340
1341 GList *compose_get_compose_list(void)
1342 {
1343         return compose_list;
1344 }
1345
1346 void compose_entry_append(Compose *compose, const gchar *address,
1347                           ComposeEntryType type)
1348 {
1349         gchar *header;
1350
1351         if (!address || *address == '\0') return;
1352
1353 #if 0 /* NEW COMPOSE GUI */
1354         switch (type) {
1355         case COMPOSE_CC:
1356                 entry = GTK_ENTRY(compose->cc_entry);
1357                 break;
1358         case COMPOSE_BCC:
1359                 entry = GTK_ENTRY(compose->bcc_entry);
1360                 break;
1361         case COMPOSE_NEWSGROUPS:
1362                 entry = GTK_ENTRY(compose->newsgroups_entry);
1363                 break;
1364         case COMPOSE_TO:
1365         default:
1366                 entry = GTK_ENTRY(compose->to_entry);
1367                 break;
1368         }
1369
1370         text = gtk_entry_get_text(entry);
1371         if (*text != '\0')
1372                 gtk_entry_append_text(entry, ", ");
1373         gtk_entry_append_text(entry, address);
1374 #endif
1375
1376         switch (type) {
1377         case COMPOSE_CC:
1378                 header = N_("Cc:");
1379                 break;
1380         case COMPOSE_BCC:
1381                 header = N_("Bcc:");
1382                 break;
1383         case COMPOSE_REPLYTO:
1384                 header = N_("Reply-To:");
1385                 break;
1386         case COMPOSE_NEWSGROUPS:
1387                 header = N_("Newsgroups:");
1388                 break;
1389         case COMPOSE_FOLLOWUPTO:
1390                 header = N_( "Followup-To:");
1391                 break;
1392         case COMPOSE_TO:
1393         default:
1394                 header = N_("To:");
1395                 break;
1396         }
1397         header = prefs_common.trans_hdr ? gettext(header) : header;
1398
1399         compose_add_header_entry(compose, header, (gchar *)address);
1400 }
1401
1402 void compose_entry_select (Compose *compose, const gchar *mailto)
1403 {
1404         GSList *header_list;
1405                 
1406         for (header_list = compose->header_list; header_list != NULL; header_list = header_list->next) {
1407                 GtkEntry * entry = GTK_ENTRY(((ComposeHeaderEntry *)header_list->data)->entry);
1408
1409                 if (gtk_entry_get_text(entry) && !g_strcasecmp(gtk_entry_get_text(entry), mailto)) {
1410                         gtk_entry_select_region(entry, 0, -1);
1411                         gtk_widget_grab_focus(GTK_WIDGET(entry));
1412                 }
1413         }
1414 }
1415
1416 void compose_toolbar_cb(gint action, gpointer data)
1417 {
1418         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1419         Compose *compose = (Compose*)toolbar_item->parent;
1420         
1421         g_return_if_fail(compose != NULL);
1422
1423         switch(action) {
1424         case A_SEND:
1425                 compose_send_cb(compose, 0, NULL);
1426                 break;
1427         case A_SENDL:
1428                 compose_send_later_cb(compose, 0, NULL);
1429                 break;
1430         case A_DRAFT:
1431                 compose_draft_cb(compose, 0, NULL);
1432                 break;
1433         case A_INSERT:
1434                 compose_insert_file_cb(compose, 0, NULL);
1435                 break;
1436         case A_ATTACH:
1437                 compose_attach_cb(compose, 0, NULL);
1438                 break;
1439         case A_SIG:
1440                 compose_insert_sig(compose, FALSE);
1441                 break;
1442         case A_EXTEDITOR:
1443                 compose_ext_editor_cb(compose, 0, NULL);
1444                 break;
1445         case A_LINEWRAP:
1446                 compose_wrap_line(compose);
1447                 break;
1448         case A_ADDRBOOK:
1449                 compose_address_cb(compose, 0, NULL);
1450                 break;
1451         default:
1452                 break;
1453         }
1454 }
1455
1456 static void compose_entries_set(Compose *compose, const gchar *mailto)
1457 {
1458         gchar *to = NULL;
1459         gchar *cc = NULL;
1460         gchar *bcc = NULL;
1461         gchar *subject = NULL;
1462         gchar *body = NULL;
1463
1464         scan_mailto_url(mailto, &to, &cc, &bcc, &subject, &body);
1465
1466         if (to)
1467                 compose_entry_append(compose, to, COMPOSE_TO);
1468         if (cc)
1469                 compose_entry_append(compose, cc, COMPOSE_CC);
1470         if (bcc)
1471                 compose_entry_append(compose, bcc, COMPOSE_BCC);
1472         if (subject)
1473                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1474         if (body) {
1475                 gtk_stext_insert(GTK_STEXT(compose->text),
1476                                 NULL, NULL, NULL, body, -1);
1477                 gtk_stext_insert(GTK_STEXT(compose->text),
1478                                 NULL, NULL, NULL, "\n", 1);
1479         }
1480
1481         g_free(to);
1482         g_free(cc);
1483         g_free(bcc);
1484         g_free(subject);
1485         g_free(body);
1486 }
1487
1488 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1489 {
1490         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
1491                                        {"Cc:",          NULL, TRUE},
1492                                        {"References:",  NULL, FALSE},
1493                                        {"Bcc:",         NULL, TRUE},
1494                                        {"Newsgroups:",  NULL, TRUE},
1495                                        {"Followup-To:", NULL, TRUE},
1496                                        {"List-Post:",   NULL, FALSE},
1497                                        {"X-Priority:",  NULL, FALSE},
1498                                        {NULL,           NULL, FALSE}};
1499
1500         enum
1501         {
1502                 H_REPLY_TO      = 0,
1503                 H_CC            = 1,
1504                 H_REFERENCES    = 2,
1505                 H_BCC           = 3,
1506                 H_NEWSGROUPS    = 4,
1507                 H_FOLLOWUP_TO   = 5,
1508                 H_LIST_POST     = 6,
1509                 H_X_PRIORITY    = 7
1510         };
1511
1512         FILE *fp;
1513
1514         g_return_val_if_fail(msginfo != NULL, -1);
1515
1516         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1517         procheader_get_header_fields(fp, hentry);
1518         fclose(fp);
1519
1520         if (hentry[H_REPLY_TO].body != NULL) {
1521                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1522                 compose->replyto = hentry[H_REPLY_TO].body;
1523                 hentry[H_REPLY_TO].body = NULL;
1524         }
1525         if (hentry[H_CC].body != NULL) {
1526                 conv_unmime_header_overwrite(hentry[H_CC].body);
1527                 compose->cc = hentry[H_CC].body;
1528                 hentry[H_CC].body = NULL;
1529         }
1530         if (hentry[H_REFERENCES].body != NULL) {
1531                 if (compose->mode == COMPOSE_REEDIT)
1532                         compose->references = hentry[H_REFERENCES].body;
1533                 else {
1534                         compose->references = compose_parse_references
1535                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1536                         g_free(hentry[H_REFERENCES].body);
1537                 }
1538                 hentry[H_REFERENCES].body = NULL;
1539         }
1540         if (hentry[H_BCC].body != NULL) {
1541                 if (compose->mode == COMPOSE_REEDIT) {
1542                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1543                         compose->bcc = hentry[H_BCC].body;
1544                 } else
1545                         g_free(hentry[H_BCC].body);
1546                 hentry[H_BCC].body = NULL;
1547         }
1548         if (hentry[H_NEWSGROUPS].body != NULL) {
1549                 conv_unmime_header_overwrite(hentry[H_NEWSGROUPS].body);
1550                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1551                 hentry[H_NEWSGROUPS].body = NULL;
1552         }
1553         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1554                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1555                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1556                 hentry[H_FOLLOWUP_TO].body = NULL;
1557         }
1558         if (hentry[H_LIST_POST].body != NULL) {
1559                 gchar *to = NULL;
1560
1561                 extract_address(hentry[H_LIST_POST].body);
1562                 if (hentry[H_LIST_POST].body[0] != '\0') {
1563                         scan_mailto_url(hentry[H_LIST_POST].body,
1564                                         &to, NULL, NULL, NULL, NULL);
1565                         if (to) {
1566                                 g_free(compose->ml_post);
1567                                 compose->ml_post = to;
1568                         }
1569                 }
1570                 g_free(hentry[H_LIST_POST].body);
1571                 hentry[H_LIST_POST].body = NULL;
1572         }
1573
1574         /* CLAWS - X-Priority */
1575         if (compose->mode == COMPOSE_REEDIT)
1576                 if (hentry[H_X_PRIORITY].body != NULL) {
1577                         gint priority;
1578                         
1579                         priority = atoi(hentry[H_X_PRIORITY].body);
1580                         g_free(hentry[H_X_PRIORITY].body);
1581                         
1582                         hentry[H_X_PRIORITY].body = NULL;
1583                         
1584                         if (priority < PRIORITY_HIGHEST || 
1585                             priority > PRIORITY_LOWEST)
1586                                 priority = PRIORITY_NORMAL;
1587                         
1588                         compose->priority =  priority;
1589                 }
1590  
1591         if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1592                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1593         else if (compose->mode != COMPOSE_REEDIT &&
1594                  msginfo->msgid && *msginfo->msgid) {
1595                 compose->inreplyto = g_strdup(msginfo->msgid);
1596
1597                 if (!compose->references) {
1598                         if (msginfo->inreplyto && *msginfo->inreplyto)
1599                                 compose->references =
1600                                         g_strdup_printf("<%s>\n\t<%s>",
1601                                                         msginfo->inreplyto,
1602                                                         msginfo->msgid);
1603                         else
1604                                 compose->references =
1605                                         g_strconcat("<", msginfo->msgid, ">",
1606                                                     NULL);
1607                 }
1608         }
1609
1610         return 0;
1611 }
1612
1613 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1614 {
1615         GSList *ref_id_list, *cur;
1616         GString *new_ref;
1617         gchar *new_ref_str;
1618
1619         ref_id_list = references_list_append(NULL, ref);
1620         if (!ref_id_list) return NULL;
1621         if (msgid && *msgid)
1622                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1623
1624         for (;;) {
1625                 gint len = 0;
1626
1627                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1628                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1629                         len += strlen((gchar *)cur->data) + 5;
1630
1631                 if (len > MAX_REFERENCES_LEN) {
1632                         /* remove second message-ID */
1633                         if (ref_id_list && ref_id_list->next &&
1634                             ref_id_list->next->next) {
1635                                 g_free(ref_id_list->next->data);
1636                                 ref_id_list = g_slist_remove
1637                                         (ref_id_list, ref_id_list->next->data);
1638                         } else {
1639                                 slist_free_strings(ref_id_list);
1640                                 g_slist_free(ref_id_list);
1641                                 return NULL;
1642                         }
1643                 } else
1644                         break;
1645         }
1646
1647         new_ref = g_string_new("");
1648         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1649                 if (new_ref->len > 0)
1650                         g_string_append(new_ref, "\n\t");
1651                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1652         }
1653
1654         slist_free_strings(ref_id_list);
1655         g_slist_free(ref_id_list);
1656
1657         new_ref_str = new_ref->str;
1658         g_string_free(new_ref, FALSE);
1659
1660         return new_ref_str;
1661 }
1662
1663 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1664                                 const gchar *fmt, const gchar *qmark,
1665                                 const gchar *body)
1666 {
1667         GtkSText *text = GTK_STEXT(compose->text);
1668         static MsgInfo dummyinfo;
1669         gchar *quote_str = NULL;
1670         gchar *buf;
1671         gchar *p, *lastp;
1672         gint len;
1673         const gchar *trimmed_body = body;
1674         
1675         if (!msginfo)
1676                 msginfo = &dummyinfo;
1677
1678         if (qmark != NULL) {
1679                 quote_fmt_init(msginfo, NULL, NULL);
1680                 quote_fmt_scan_string(qmark);
1681                 quote_fmt_parse();
1682
1683                 buf = quote_fmt_get_buffer();
1684                 if (buf == NULL)
1685                         alertpanel_error(_("Quote mark format error."));
1686                 else
1687                         Xstrdup_a(quote_str, buf, return NULL)
1688         }
1689
1690         if (fmt && *fmt != '\0') {
1691                 while (trimmed_body && strlen(trimmed_body) > 1
1692                         && trimmed_body[0]=='\n')
1693                         *trimmed_body++;
1694
1695                 quote_fmt_init(msginfo, quote_str, trimmed_body);
1696                 quote_fmt_scan_string(fmt);
1697                 quote_fmt_parse();
1698
1699                 buf = quote_fmt_get_buffer();
1700                 if (buf == NULL) {
1701                         alertpanel_error(_("Message reply/forward format error."));
1702                         return NULL;
1703                 }
1704         } else
1705                 buf = "";
1706
1707         gtk_stext_freeze(text);
1708
1709         for (p = buf; *p != '\0'; ) {
1710                 lastp = strchr(p, '\n');
1711                 len = lastp ? lastp - p + 1 : -1;
1712                 gtk_stext_insert(text, NULL, NULL, NULL, p, len);
1713                 if (lastp)
1714                         p = lastp + 1;
1715                 else
1716                         break;
1717         }
1718
1719         gtk_stext_thaw(text);
1720
1721         return buf;
1722 }
1723
1724 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1725                                     gboolean to_all, gboolean to_ml,
1726                                     gboolean to_sender,
1727                                     gboolean followup_and_reply_to)
1728 {
1729         GSList *cc_list = NULL;
1730         GSList *cur;
1731         gchar *from = NULL;
1732         gchar *replyto = NULL;
1733         GHashTable *to_table;
1734
1735         g_return_if_fail(compose->account != NULL);
1736         g_return_if_fail(msginfo != NULL);
1737
1738         if (compose->account->protocol != A_NNTP) {
1739                 if (!compose->replyto && to_ml && compose->ml_post
1740                     && !(msginfo->folder && msginfo->folder->prefs->enable_default_reply_to))
1741                         compose_entry_append(compose,
1742                                            compose->ml_post,
1743                                            COMPOSE_TO);
1744                 else if (!(to_all || to_sender)
1745                          && msginfo->folder
1746                          && msginfo->folder->prefs->enable_default_reply_to) {
1747                         compose_entry_append(compose,
1748                             msginfo->folder->prefs->default_reply_to,
1749                             COMPOSE_TO);
1750                 } else
1751                         compose_entry_append(compose,
1752                                  (compose->replyto && !to_sender)
1753                                   ? compose->replyto :
1754                                   msginfo->from ? msginfo->from : "",
1755                                   COMPOSE_TO);
1756         } else {
1757                 if (to_sender || (compose->followup_to && 
1758                         !strncmp(compose->followup_to, "poster", 6)))
1759                         compose_entry_append
1760                                 (compose, 
1761                                  (compose->replyto ? compose->replyto :
1762                                         msginfo->from ? msginfo->from : ""),
1763                                  COMPOSE_TO);
1764                                  
1765                 else if (followup_and_reply_to || to_all) {
1766                         compose_entry_append
1767                                 (compose,
1768                                  (compose->replyto ? compose->replyto :
1769                                  msginfo->from ? msginfo->from : ""),
1770                                  COMPOSE_TO);                           
1771                 
1772                         compose_entry_append
1773                                 (compose,
1774                                  compose->followup_to ? compose->followup_to :
1775                                  compose->newsgroups ? compose->newsgroups : "",
1776                                  COMPOSE_NEWSGROUPS);
1777                 } 
1778                 else 
1779                         compose_entry_append
1780                                 (compose,
1781                                  compose->followup_to ? compose->followup_to :
1782                                  compose->newsgroups ? compose->newsgroups : "",
1783                                  COMPOSE_NEWSGROUPS);
1784         }
1785
1786         if (msginfo->subject && *msginfo->subject) {
1787                 gchar *buf, *buf2, *p;
1788
1789                 buf = p = g_strdup(msginfo->subject);
1790                 p += subject_get_reply_prefix_length(p);
1791                 memmove(buf, p, strlen(p) + 1);
1792
1793                 buf2 = g_strdup_printf("Re: %s", buf);
1794                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1795                 g_free(buf2);
1796                 g_free(buf);
1797         } else
1798                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1799
1800         if (to_ml && compose->ml_post) return;
1801         if (!to_all || compose->account->protocol == A_NNTP) return;
1802
1803         if (compose->replyto) {
1804                 Xstrdup_a(replyto, compose->replyto, return);
1805                 extract_address(replyto);
1806         }
1807         if (msginfo->from) {
1808                 Xstrdup_a(from, msginfo->from, return);
1809                 extract_address(from);
1810         }
1811
1812         if (replyto && from)
1813                 cc_list = address_list_append_with_comments(cc_list, from);
1814         cc_list = address_list_append_with_comments(cc_list, msginfo->to);
1815         cc_list = address_list_append_with_comments(cc_list, compose->cc);
1816
1817         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1818         if (replyto)
1819                 g_hash_table_insert(to_table, g_strdup(replyto), GINT_TO_POINTER(1));
1820         if (compose->account)
1821                 g_hash_table_insert(to_table, g_strdup(compose->account->address),
1822                                     GINT_TO_POINTER(1));
1823
1824         /* remove address on To: and that of current account */
1825         for (cur = cc_list; cur != NULL; ) {
1826                 GSList *next = cur->next;
1827                 gchar *addr;
1828
1829                 addr = g_strdup(cur->data);
1830                 extract_address(addr);
1831
1832                 if (GPOINTER_TO_INT(g_hash_table_lookup(to_table, addr)) == 1)
1833                         cc_list = g_slist_remove(cc_list, cur->data);
1834                 else
1835                         g_hash_table_insert(to_table, addr, GINT_TO_POINTER(1));
1836
1837                 cur = next;
1838         }
1839         hash_free_strings(to_table);
1840         g_hash_table_destroy(to_table);
1841
1842         if (cc_list) {
1843                 for (cur = cc_list; cur != NULL; cur = cur->next)
1844                         compose_entry_append(compose, (gchar *)cur->data,
1845                                              COMPOSE_CC);
1846                 slist_free_strings(cc_list);
1847                 g_slist_free(cc_list);
1848         }
1849 }
1850
1851 #define SET_ENTRY(entry, str) \
1852 { \
1853         if (str && *str) \
1854                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1855 }
1856
1857 #define SET_ADDRESS(type, str) \
1858 { \
1859         if (str && *str) \
1860                 compose_entry_append(compose, str, type); \
1861 }
1862
1863 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1864 {
1865         g_return_if_fail(msginfo != NULL);
1866
1867         SET_ENTRY(subject_entry, msginfo->subject);
1868         SET_ADDRESS(COMPOSE_TO, msginfo->to);
1869         SET_ADDRESS(COMPOSE_CC, compose->cc);
1870         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1871         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1872         SET_ADDRESS(COMPOSE_NEWSGROUPS, compose->newsgroups);
1873         SET_ADDRESS(COMPOSE_FOLLOWUPTO, compose->followup_to);
1874
1875         compose_update_priority_menu_item(compose);
1876 #if USE_GPGME   
1877         compose_update_gnupg_mode_menu_item(compose);
1878 #endif
1879         compose_show_first_last_header(compose, TRUE);
1880
1881 }
1882
1883 #undef SET_ENTRY
1884 #undef SET_ADDRESS
1885
1886 static void compose_insert_sig(Compose *compose, gboolean replace)
1887 {
1888         GtkSText *text = GTK_STEXT(compose->text);
1889         gint cur_pos;
1890
1891         g_return_if_fail(compose->account != NULL);
1892
1893         cur_pos = gtk_editable_get_position(GTK_EDITABLE(text));
1894
1895         gtk_stext_freeze(text);
1896
1897         if (replace && compose->sig_str) {
1898                 gint pos;
1899                 gint len;
1900
1901                 if (compose->sig_str[0] == '\0')
1902                         pos = -1;
1903                 else
1904                         pos = gtkut_stext_find(text, 0, compose->sig_str, TRUE);
1905
1906                 if (pos != -1) {
1907                         len = get_mbs_len(compose->sig_str);
1908                         if (len >= 0) {
1909                                 gtk_stext_set_point(text, pos);
1910                                 gtk_stext_forward_delete(text, len);
1911                         } else {
1912                                 len = gtk_stext_get_length(text);
1913                                 gtk_stext_set_point(text, len);
1914                         }
1915                 } else {
1916                         len = gtk_stext_get_length(text);
1917                         gtk_stext_set_point(text, len);
1918                 }
1919         } else
1920                 gtk_stext_insert(text, NULL, NULL, NULL, "\n\n", 2);
1921
1922         g_free(compose->sig_str);
1923         compose->sig_str = compose_get_signature_str(compose);
1924         if (!compose->sig_str || (replace && !compose->account->auto_sig))
1925                 compose->sig_str = g_strdup("");
1926
1927         gtk_stext_insert(text, NULL, NULL, NULL, compose->sig_str, -1);
1928
1929         gtk_stext_thaw(text);
1930
1931         if (cur_pos > gtk_stext_get_length(text))
1932                 cur_pos = gtk_stext_get_length(text);
1933
1934         gtk_editable_set_position(GTK_EDITABLE(text), cur_pos);
1935         gtk_stext_set_point(text, cur_pos);
1936 }
1937
1938 static gchar *compose_get_signature_str(Compose *compose)
1939 {
1940         gchar *sig_body = NULL;
1941         gchar *sig_str = NULL;
1942
1943         g_return_val_if_fail(compose->account != NULL, NULL);
1944
1945         if (!compose->account->sig_path)
1946                 return NULL;
1947
1948         if (compose->account->sig_type == SIG_FILE) {
1949                 if (!is_file_or_fifo_exist(compose->account->sig_path)) {
1950                         g_warning("can't open signature file: %s\n",
1951                                   compose->account->sig_path);
1952                         return NULL;
1953                 }
1954         }
1955
1956         if (compose->account->sig_type == SIG_COMMAND)
1957                 sig_body = get_command_output(compose->account->sig_path);
1958         else {
1959                 gchar *tmp;
1960
1961                 tmp = file_read_to_str(compose->account->sig_path);
1962                 if (!tmp)
1963                         return NULL;
1964                 sig_body = normalize_newlines(tmp);
1965                 g_free(tmp);
1966         }
1967
1968         if (compose->account->sig_sep) {
1969                 sig_str = g_strconcat(compose->account->sig_sep, "\n", sig_body,
1970                                       NULL);
1971                 g_free(sig_body);
1972         } else
1973                 sig_str = sig_body;
1974
1975         return sig_str;
1976 }
1977
1978 static void compose_insert_file(Compose *compose, const gchar *file)
1979 {
1980         GtkSText *text = GTK_STEXT(compose->text);
1981         gchar buf[BUFFSIZE];
1982         gint len;
1983         FILE *fp;
1984
1985         g_return_if_fail(file != NULL);
1986
1987         if ((fp = fopen(file, "rb")) == NULL) {
1988                 FILE_OP_ERROR(file, "fopen");
1989                 return;
1990         }
1991
1992         gtk_stext_freeze(text);
1993
1994         while (fgets(buf, sizeof(buf), fp) != NULL) {
1995                 /* strip <CR> if DOS/Windows file,
1996                    replace <CR> with <LF> if Macintosh file. */
1997                 strcrchomp(buf);
1998                 len = strlen(buf);
1999                 if (len > 0 && buf[len - 1] != '\n') {
2000                         while (--len >= 0)
2001                                 if (buf[len] == '\r') buf[len] = '\n';
2002                 }
2003                 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
2004         }
2005
2006         gtk_stext_thaw(text);
2007
2008         fclose(fp);
2009 }
2010
2011 static void compose_attach_append(Compose *compose, const gchar *file,
2012                                   const gchar *filename,
2013                                   const gchar *content_type)
2014 {
2015         AttachInfo *ainfo;
2016         gchar *text[N_ATTACH_COLS];
2017         FILE *fp;
2018         off_t size;
2019         gint row;
2020
2021         if (!is_file_exist(file)) {
2022                 g_warning("File %s doesn't exist\n", file);
2023                 return;
2024         }
2025         if ((size = get_file_size(file)) < 0) {
2026                 g_warning("Can't get file size of %s\n", file);
2027                 return;
2028         }
2029         if (size == 0) {
2030                 alertpanel_notice(_("File %s is empty."), file);
2031                 return;
2032         }
2033         if ((fp = fopen(file, "rb")) == NULL) {
2034                 alertpanel_error(_("Can't read %s."), file);
2035                 return;
2036         }
2037         fclose(fp);
2038
2039 #if 0 /* NEW COMPOSE GUI */
2040         if (!compose->use_attach) {
2041                 GtkItemFactory *ifactory;
2042                 GtkWidget *menuitem;
2043
2044                 ifactory = gtk_item_factory_from_widget(compose->menubar);
2045                 menuitem = gtk_item_factory_get_item(ifactory,
2046                                                      "/View/Attachment");
2047                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2048                                                TRUE);
2049         }
2050 #endif
2051         ainfo = g_new0(AttachInfo, 1);
2052         ainfo->file = g_strdup(file);
2053
2054         if (content_type) {
2055                 ainfo->content_type = g_strdup(content_type);
2056                 if (!strcasecmp(content_type, "message/rfc822")) {
2057                         if (procmime_get_encoding_for_file(file) == ENC_7BIT)
2058                                 ainfo->encoding = ENC_7BIT;
2059                         else
2060                                 ainfo->encoding = ENC_8BIT;
2061                         ainfo->name = g_strdup_printf
2062                                 (_("Message: %s"),
2063                                  g_basename(filename ? filename : file));
2064                 } else {
2065                         if (!g_strncasecmp(content_type, "text", 4))
2066                                 ainfo->encoding =
2067                                         procmime_get_encoding_for_file(file);
2068                         else
2069                                 ainfo->encoding = ENC_BASE64;
2070                         ainfo->name = g_strdup
2071                                 (g_basename(filename ? filename : file));
2072                 }
2073         } else {
2074                 ainfo->content_type = procmime_get_mime_type(file);
2075                 if (!ainfo->content_type) {
2076                         ainfo->content_type =
2077                                 g_strdup("application/octet-stream");
2078                         ainfo->encoding = ENC_BASE64;
2079                 } else if (!g_strncasecmp(ainfo->content_type, "text", 4))
2080                         ainfo->encoding = procmime_get_encoding_for_file(file);
2081                 else
2082                         ainfo->encoding = ENC_BASE64;
2083                 ainfo->name = g_strdup(g_basename(filename ? filename : file)); 
2084         }
2085         ainfo->size = size;
2086
2087         text[COL_MIMETYPE] = ainfo->content_type;
2088         text[COL_SIZE] = to_human_readable(size);
2089         text[COL_NAME] = ainfo->name;
2090
2091         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
2092         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
2093 }
2094
2095 #define IS_FIRST_PART_TEXT(info) \
2096         ((info->mime_type == MIME_TEXT || info->mime_type == MIME_TEXT_HTML || \
2097           info->mime_type == MIME_TEXT_ENRICHED) || \
2098          (info->mime_type == MIME_MULTIPART && info->content_type && \
2099           !strcasecmp(info->content_type, "multipart/alternative") && \
2100           (info->children && \
2101            (info->children->mime_type == MIME_TEXT || \
2102             info->children->mime_type == MIME_TEXT_HTML || \
2103             info->children->mime_type == MIME_TEXT_ENRICHED))))
2104
2105 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
2106 {
2107         MimeInfo *mimeinfo;
2108         MimeInfo *child;
2109         gchar *infile;
2110         gchar *outfile;
2111
2112         mimeinfo = procmime_scan_message(msginfo);
2113         if (!mimeinfo) return;
2114
2115         /* skip first text (presumably message body) */
2116         child = mimeinfo->children;
2117         if (!child || IS_FIRST_PART_TEXT(mimeinfo)) {
2118                 procmime_mimeinfo_free_all(mimeinfo);
2119                 return;
2120         }
2121
2122         if (IS_FIRST_PART_TEXT(child))
2123                 child = child->next;
2124
2125         infile = procmsg_get_message_file_path(msginfo);
2126
2127         while (child != NULL) {
2128                 if (child->children || child->mime_type == MIME_MULTIPART) {
2129                         child = procmime_mimeinfo_next(child);
2130                         continue;
2131                 }
2132                 if(child->parent && child->parent->parent
2133                 && !strcasecmp(child->parent->parent->content_type, "multipart/signed")
2134                 && child->mime_type == MIME_TEXT) {
2135                         /* this is the main text part of a signed message */
2136                         child = procmime_mimeinfo_next(child);
2137                         continue;
2138                 }
2139                 outfile = procmime_get_tmp_file_name(child);
2140                 if (procmime_get_part(outfile, infile, child) < 0)
2141                         g_warning("Can't get the part of multipart message.");
2142                 else if (compose->mode != COMPOSE_REEDIT || strcmp(child->content_type, "application/pgp-signature"))
2143                         compose_attach_append
2144                                 (compose, outfile,
2145                                  child->filename ? child->filename : child->name,
2146                                  child->content_type);
2147
2148                 child = child->next;
2149         }
2150
2151         g_free(infile);
2152         procmime_mimeinfo_free_all(mimeinfo);
2153 }
2154
2155 #undef IS_FIRST_PART_TEXT
2156
2157 #define GET_CHAR(pos, buf, len)                                              \
2158 {                                                                            \
2159         if (text->use_wchar)                                                 \
2160                 len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos)));    \
2161         else {                                                               \
2162                 buf[0] = GTK_STEXT_INDEX(text, (pos));                       \
2163                 len = 1;                                                     \
2164         }                                                                    \
2165 }
2166
2167 #define DISP_WIDTH(len) \
2168         ((len > 2 && conv_get_current_charset() == C_UTF_8) ? 2 : \
2169          (len == 2 && conv_get_current_charset() == C_UTF_8) ? 1 : len)
2170
2171 #define INDENT_CHARS    ">|#"
2172 #define SPACE_CHARS     " \t"
2173
2174 static void compose_wrap_line(Compose *compose)
2175 {
2176         GtkSText *text = GTK_STEXT(compose->text);
2177         gint ch_len, last_ch_len;
2178         gchar cbuf[MB_LEN_MAX], last_ch;
2179         guint text_len;
2180         guint line_end;
2181         guint quoted;
2182         gint p_start, p_end;
2183         gint line_pos, cur_pos;
2184         gint line_len, cur_len;
2185
2186         gtk_stext_freeze(text);
2187
2188         text_len = gtk_stext_get_length(text);
2189
2190         /* check to see if the point is on the paragraph mark (empty line). */
2191         cur_pos = gtk_stext_get_point(text);
2192         GET_CHAR(cur_pos, cbuf, ch_len);
2193         if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
2194                 if (cur_pos == 0)
2195                         goto compose_end; /* on the paragraph mark */
2196                 GET_CHAR(cur_pos - 1, cbuf, ch_len);
2197                 if (ch_len == 1 && *cbuf == '\n')
2198                         goto compose_end; /* on the paragraph mark */
2199         }
2200
2201         /* find paragraph start. */
2202         line_end = quoted = 0;
2203         for (p_start = cur_pos; p_start >= 0; --p_start) {
2204                 GET_CHAR(p_start, cbuf, ch_len);
2205                 if (ch_len == 1 && *cbuf == '\n') {
2206                         if (quoted)
2207                                 goto compose_end; /* quoted part */
2208                         if (line_end) {
2209                                 p_start += 2;
2210                                 break;
2211                         }
2212                         line_end = 1;
2213                 } else {
2214                         if (ch_len == 1 
2215                             && strchr(prefs_common.quote_chars, *cbuf))
2216                                 quoted = 1;
2217                         else if (ch_len != 1 || !isspace(*cbuf))
2218                                 quoted = 0;
2219
2220                         line_end = 0;
2221                 }
2222         }
2223         if (p_start < 0)
2224                 p_start = 0;
2225
2226         /* find paragraph end. */
2227         line_end = 0;
2228         for (p_end = cur_pos; p_end < text_len; p_end++) {
2229                 GET_CHAR(p_end, cbuf, ch_len);
2230                 if (ch_len == 1 && *cbuf == '\n') {
2231                         if (line_end) {
2232                                 p_end -= 1;
2233                                 break;
2234                         }
2235                         line_end = 1;
2236                 } else {
2237                         if (line_end && ch_len == 1 &&
2238                             strchr(prefs_common.quote_chars, *cbuf))
2239                                 goto compose_end; /* quoted part */
2240
2241                         line_end = 0;
2242                 }
2243         }
2244         if (p_end >= text_len)
2245                 p_end = text_len;
2246
2247         if (p_start >= p_end)
2248                 goto compose_end;
2249
2250         line_len = cur_len = 0;
2251         last_ch_len = 0;
2252         last_ch = '\0';
2253         line_pos = p_start;
2254         for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2255                 guint space = 0;
2256
2257                 GET_CHAR(cur_pos, cbuf, ch_len);
2258
2259                 if (ch_len < 0) {
2260                         cbuf[0] = '\0';
2261                         ch_len = 1;
2262                 }
2263
2264                 if (ch_len == 1 && isspace(*cbuf))
2265                         space = 1;
2266
2267                 if (ch_len == 1 && *cbuf == '\n') {
2268                         guint replace = 0;
2269                         if (last_ch_len == 1 && !isspace(last_ch)) {
2270                                 if (cur_pos + 1 < p_end) {
2271                                         GET_CHAR(cur_pos + 1, cbuf, ch_len);
2272                                         if (ch_len == 1 && !isspace(*cbuf))
2273                                                 replace = 1;
2274                                 }
2275                         }
2276                         gtk_stext_set_point(text, cur_pos + 1);
2277                         gtk_stext_backward_delete(text, 1);
2278                         if (replace) {
2279                                 gtk_stext_set_point(text, cur_pos);
2280                                 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2281                                 space = 1;
2282                         }
2283                         else {
2284                                 p_end--;
2285                                 cur_pos--;
2286                                 continue;
2287                         }
2288                 }
2289
2290                 last_ch_len = ch_len;
2291                 last_ch = *cbuf;
2292
2293                 if (space) {
2294                         line_pos = cur_pos + 1;
2295                         line_len = cur_len + ch_len;
2296                 }
2297
2298                 if (cur_len + DISP_WIDTH(ch_len) > prefs_common.linewrap_len &&
2299                     line_len > 0) {
2300                         gint tlen = ch_len;
2301
2302                         GET_CHAR(line_pos - 1, cbuf, ch_len);
2303                         if (ch_len == 1 && isspace(*cbuf)) {
2304                                 gtk_stext_set_point(text, line_pos);
2305                                 gtk_stext_backward_delete(text, 1);
2306                                 p_end--;
2307                                 cur_pos--;
2308                                 line_pos--;
2309                                 cur_len--;
2310                                 line_len--;
2311                         }
2312                         ch_len = tlen;
2313
2314                         gtk_stext_set_point(text, line_pos);
2315                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2316                         p_end++;
2317                         cur_pos++;
2318                         line_pos++;
2319                         cur_len = cur_len - line_len + DISP_WIDTH(ch_len);
2320                         line_len = 0;
2321                         continue;
2322                 }
2323
2324                 if (ch_len > 1) {
2325                         line_pos = cur_pos + 1;
2326                         line_len = cur_len + DISP_WIDTH(ch_len);
2327                 }
2328                 cur_len += DISP_WIDTH(ch_len);
2329         }
2330
2331 compose_end:
2332         gtk_stext_thaw(text);
2333 }
2334
2335 #undef WRAP_DEBUG
2336 #ifdef WRAP_DEBUG
2337 /* Darko: used when I debug wrapping */
2338 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2339 {
2340         gint i, clen;
2341         gchar cbuf[MB_LEN_MAX];
2342
2343         printf("%d [", pos);
2344         for (i = pos; i < tlen; i++) {
2345                 GET_CHAR(i, cbuf, clen);
2346                 if (clen < 0) break;
2347                 if (breakoncr && clen == 1 && cbuf[0] == '\n')
2348                         break;
2349                 fwrite(cbuf, clen, 1, stdout);
2350         }
2351         printf("]\n");
2352 }
2353 #endif
2354
2355 typedef enum {
2356         WAIT_FOR_SPACE,
2357         WAIT_FOR_INDENT_CHAR,
2358         WAIT_FOR_INDENT_CHAR_OR_SPACE
2359 } IndentState;
2360
2361 /* return indent length, we allow:
2362    > followed by spaces/tabs
2363    | followed by spaces/tabs
2364    uppercase characters immediately followed by >,
2365    and the repeating sequences of the above */
2366 /* return indent length */
2367 static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
2368 {
2369         guint i_len = 0;
2370         guint i, ch_len, alnum_cnt = 0;
2371         IndentState state = WAIT_FOR_INDENT_CHAR;
2372         gchar cbuf[MB_LEN_MAX];
2373         gboolean is_space;
2374         gboolean is_indent;
2375
2376         if (prefs_common.quote_chars == NULL) {
2377                 return 0 ;
2378         }
2379
2380         for (i = start_pos; i < text_len; i++) {
2381                 GET_CHAR(i, cbuf, ch_len);
2382                 if (ch_len > 1)
2383                         break;
2384
2385                 if (cbuf[0] == '\n')
2386                         break;
2387
2388                 is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
2389                 is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
2390
2391                 switch (state) {
2392                 case WAIT_FOR_SPACE:
2393                         if (is_space == FALSE)
2394                                 goto out;
2395                         state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2396                         break;
2397                 case WAIT_FOR_INDENT_CHAR_OR_SPACE:
2398                         if (is_indent == FALSE && is_space == FALSE &&
2399                             !isupper(cbuf[0]))
2400                                 goto out;
2401                         if (is_space == TRUE) {
2402                                 alnum_cnt = 0;
2403                                 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2404                         } else if (is_indent == TRUE) {
2405                                 alnum_cnt = 0;
2406                                 state = WAIT_FOR_SPACE;
2407                         } else {
2408                                 alnum_cnt++;
2409                                 state = WAIT_FOR_INDENT_CHAR;
2410                         }
2411                         break;
2412                 case WAIT_FOR_INDENT_CHAR:
2413                         if (is_indent == FALSE && !isupper(cbuf[0]))
2414                                 goto out;
2415                         if (is_indent == TRUE) {
2416                                 if (alnum_cnt > 0 
2417                                     && !strchr(prefs_common.quote_chars, cbuf[0]))
2418                                         goto out;
2419                                 alnum_cnt = 0;
2420                                 state = WAIT_FOR_SPACE;
2421                         } else {
2422                                 alnum_cnt++;
2423                         }
2424                         break;
2425                 }
2426
2427                 i_len++;
2428         }
2429
2430 out:
2431         if ((i_len > 0) && (state == WAIT_FOR_INDENT_CHAR))
2432                 i_len -= alnum_cnt;
2433
2434         return i_len;
2435 }
2436
2437 /* insert quotation string when line was wrapped */
2438 static guint ins_quote(GtkSText *text, guint indent_len,
2439                        guint prev_line_pos, guint text_len,
2440                        gchar *quote_fmt)
2441 {
2442         guint i, ins_len = 0;
2443         gchar ch;
2444
2445         if (indent_len) {
2446                 for (i = 0; i < indent_len; i++) {
2447                         ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2448                         gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2449                 }
2450                 ins_len = indent_len;
2451         }
2452
2453         return ins_len;
2454 }
2455
2456 /* check if we should join the next line */
2457 static gboolean join_next_line(GtkSText *text, guint start_pos, guint tlen,
2458                                guint prev_ilen, gboolean autowrap)
2459 {
2460         guint indent_len, ch_len;
2461         gboolean do_join = FALSE;
2462         gchar cbuf[MB_LEN_MAX];
2463
2464         indent_len = get_indent_length(text, start_pos, tlen);
2465
2466         if ((autowrap || indent_len > 0) && indent_len == prev_ilen) {
2467                 GET_CHAR(start_pos + indent_len, cbuf, ch_len);
2468                 if (ch_len > 0 && (cbuf[0] != '\n'))
2469                         do_join = TRUE;
2470         }
2471
2472         return do_join;
2473 }
2474
2475 static void compose_wrap_line_all(Compose *compose)
2476 {
2477         compose_wrap_line_all_full(compose, FALSE);
2478 }
2479
2480 #define STEXT_FREEZE() \
2481         if (!frozen) { gtk_stext_freeze(text); frozen = TRUE; }
2482
2483 static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
2484 {
2485         GtkSText *text = GTK_STEXT(compose->text);
2486         guint tlen;
2487         guint line_pos = 0, cur_pos = 0, p_pos = 0;
2488         gint line_len = 0, cur_len = 0;
2489         gint ch_len;
2490         gboolean is_new_line = TRUE, do_delete = FALSE;
2491         guint i_len = 0;
2492         gboolean linewrap_quote = TRUE;
2493         gboolean set_editable_pos = FALSE;
2494         gint editable_pos = 0;
2495         gboolean frozen = FALSE;
2496         guint linewrap_len = prefs_common.linewrap_len;
2497         gchar *qfmt = prefs_common.quotemark;
2498         gchar cbuf[MB_LEN_MAX];
2499
2500         tlen = gtk_stext_get_length(text);
2501
2502         for (; cur_pos < tlen; cur_pos++) {
2503                 /* mark position of new line - needed for quotation wrap */
2504                 if (is_new_line) {
2505                         if (linewrap_quote)
2506                                 i_len = get_indent_length(text, cur_pos, tlen);
2507
2508                         is_new_line = FALSE;
2509                         p_pos = cur_pos;
2510 #ifdef WRAP_DEBUG
2511                         g_print("new line i_len=%d p_pos=", i_len);
2512                         dump_text(text, p_pos, tlen, 1);
2513 #endif
2514                 }
2515
2516                 GET_CHAR(cur_pos, cbuf, ch_len);
2517
2518                 /* fix line length for tabs */
2519                 if (ch_len == 1 && *cbuf == '\t') {
2520                         guint tab_width = text->default_tab_width;
2521                         guint tab_offset = line_len % tab_width;
2522
2523 #ifdef WRAP_DEBUG
2524                         g_print("found tab at pos=%d line_len=%d ", cur_pos,
2525                                 line_len);
2526 #endif
2527                         if (tab_offset) {
2528                                 line_len += tab_width - tab_offset - 1;
2529                                 cur_len = line_len;
2530                         }
2531 #ifdef WRAP_DEBUG
2532                         printf("new_len=%d\n", line_len);
2533 #endif
2534                 }
2535
2536                 /* we have encountered line break */
2537                 if (ch_len == 1 && *cbuf == '\n') {
2538                         gint clen;
2539                         gchar cb[MB_LEN_MAX];
2540
2541                         /* should we join the next line */
2542                         if ((autowrap || i_len != cur_len) && do_delete &&
2543                             join_next_line
2544                                 (text, cur_pos + 1, tlen, i_len, autowrap))
2545                                 do_delete = TRUE;
2546                         else
2547                                 do_delete = FALSE;
2548
2549 #ifdef WRAP_DEBUG
2550                         g_print("found CR at %d do_del is %d next line is ",
2551                                cur_pos, do_delete);
2552                         dump_text(text, cur_pos + 1, tlen, 1);
2553 #endif
2554
2555                         /* skip delete if it is continuous URL */
2556                         if (do_delete && (line_pos - p_pos <= i_len) &&
2557                             gtk_stext_is_uri_string(text, line_pos, tlen))
2558                                 do_delete = FALSE;
2559
2560 #ifdef WRAP_DEBUG
2561                         g_print("l_len=%d wrap_len=%d do_del=%d\n",
2562                                 line_len, linewrap_len, do_delete);
2563 #endif
2564                         /* should we delete to perform smart wrapping */
2565                         if (line_len < linewrap_len && do_delete) {
2566                                 STEXT_FREEZE();
2567                                 /* get rid of newline */
2568                                 gtk_stext_set_point(text, cur_pos);
2569                                 gtk_stext_forward_delete(text, 1);
2570                                 tlen--;
2571
2572                                 /* if text starts with quote fmt or with
2573                                    indent string, delete them */
2574                                 if (i_len) {
2575                                         guint ilen;
2576                                         ilen =  gtk_stext_str_compare_n
2577                                                 (text, cur_pos, p_pos, i_len,
2578                                                  tlen);
2579                                         if (ilen) {
2580                                                 gtk_stext_forward_delete
2581                                                         (text, ilen);
2582                                                 tlen -= ilen;
2583                                         }
2584                                 }
2585
2586                                 GET_CHAR(cur_pos, cb, clen);
2587
2588                                 /* insert space if it's alphanumeric */
2589                                 if ((cur_pos != line_pos) &&
2590                                     ((clen > 1) || isalnum(cb[0]))) {
2591                                         gtk_stext_insert(text, NULL, NULL,
2592                                                         NULL, " ", 1);
2593                                         tlen++;
2594                                 }
2595
2596                                 /* and start over with current line */
2597                                 cur_pos = p_pos - 1;
2598                                 line_pos = cur_pos;
2599                                 line_len = cur_len = 0;
2600                                 do_delete = FALSE;
2601                                 is_new_line = TRUE;
2602 #ifdef WRAP_DEBUG
2603                                 g_print("after delete l_pos=");
2604                                 dump_text(text, line_pos, tlen, 1);
2605 #endif
2606                                 /* move beginning of line if we are on LF */
2607                                 GET_CHAR(line_pos, cb, clen);
2608                                 if (clen == 1 && *cb == '\n')
2609                                         line_pos++;
2610 #ifdef WRAP_DEBUG
2611                                 g_print("new line_pos=%d\n", line_pos);
2612 #endif
2613
2614                                 continue;
2615                         }
2616
2617                         /* mark new line beginning */
2618                         line_pos = cur_pos + 1;
2619                         line_len = cur_len = 0;
2620                         do_delete = FALSE;
2621                         is_new_line = TRUE;
2622                         continue;
2623                 }
2624
2625                 if (ch_len < 0) {
2626                         cbuf[0] = '\0';
2627                         ch_len = 1;
2628                 }
2629
2630                 /* possible line break */
2631                 if (ch_len == 1 && isspace(*cbuf)) {
2632                         line_pos = cur_pos + 1;
2633                         line_len = cur_len + ch_len;
2634                 }
2635
2636                 /* are we over wrapping length set in preferences ? */
2637                 if (cur_len + DISP_WIDTH(ch_len) > linewrap_len) {
2638                         gint clen;
2639
2640 #ifdef WRAP_DEBUG
2641                         g_print("should wrap cur_pos=%d ", cur_pos);
2642                         dump_text(text, p_pos, tlen, 1);
2643                         dump_text(text, line_pos, tlen, 1);
2644 #endif
2645                         /* force wrapping if it is one long word but not URL */
2646                         if (line_pos - p_pos <= i_len)
2647                                 if (!gtk_stext_is_uri_string
2648                                     (text, line_pos, tlen))
2649                                         line_pos = cur_pos - 1;
2650 #ifdef WRAP_DEBUG
2651                         g_print("new line_pos=%d\n", line_pos);
2652 #endif
2653
2654                         GET_CHAR(line_pos - 1, cbuf, clen);
2655
2656                         /* if next character is space delete it */
2657                         if (clen == 1 && isspace(*cbuf)) {
2658                                 if (p_pos + i_len != line_pos ||
2659                                     !gtk_stext_is_uri_string
2660                                         (text, line_pos, tlen)) {
2661                                         STEXT_FREEZE();
2662                                         /* workaround for correct cursor
2663                                            position */
2664                                         if (set_editable_pos == FALSE) {
2665                                                 editable_pos = gtk_editable_get_position(GTK_EDITABLE(text));
2666                                                 if (editable_pos == line_pos)
2667                                                         set_editable_pos = TRUE;
2668                                         }
2669                                         gtk_stext_set_point(text, line_pos);
2670                                         gtk_stext_backward_delete(text, 1);
2671                                         tlen--;
2672                                         cur_pos--;
2673                                         line_pos--;
2674                                         cur_len--;
2675                                         line_len--;
2676                                 }
2677                         }
2678
2679                         /* if it is URL at beginning of line don't wrap */
2680                         if (p_pos + i_len == line_pos &&
2681                             gtk_stext_is_uri_string(text, line_pos, tlen)) {
2682 #ifdef WRAP_DEBUG
2683                                 g_print("found URL at ");
2684                                 dump_text(text, line_pos, tlen, 1);
2685 #endif
2686                                 continue;
2687                         }
2688
2689                         /* insert CR */
2690                         STEXT_FREEZE();
2691                         gtk_stext_set_point(text, line_pos);
2692                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2693                         gtk_stext_compact_buffer(text);
2694                         tlen++;
2695                         line_pos++;
2696                         /* for loop will increase it */
2697                         cur_pos = line_pos - 1;
2698                         /* start over with current line */
2699                         is_new_line = TRUE;
2700                         line_len = cur_len = 0;
2701                         if (autowrap || i_len > 0)
2702                                 do_delete = TRUE;
2703                         else
2704                                 do_delete = FALSE;
2705 #ifdef WRAP_DEBUG
2706                         g_print("after CR insert ");
2707                         dump_text(text, line_pos, tlen, 1);
2708                         dump_text(text, cur_pos, tlen, 1);
2709 #endif
2710
2711                         /* should we insert quotation ? */
2712                         if (linewrap_quote && i_len) {
2713                                 /* only if line is not already quoted  */
2714                                 if (!gtk_stext_str_compare
2715                                         (text, line_pos, tlen, qfmt)) {
2716                                         guint ins_len;
2717
2718                                         if (line_pos - p_pos > i_len) {
2719                                                 ins_len = ins_quote
2720                                                         (text, i_len, p_pos,
2721                                                          tlen, qfmt);
2722                                                 tlen += ins_len;
2723                                         }
2724 #ifdef WRAP_DEBUG
2725                                         g_print("after quote insert ");
2726                                         dump_text(text, line_pos, tlen, 1);
2727 #endif
2728                                 }
2729                         }
2730                         continue;
2731                 }
2732
2733                 if (ch_len > 1) {
2734                         line_pos = cur_pos + 1;
2735                         line_len = cur_len + DISP_WIDTH(ch_len);
2736                 }
2737                 /* advance to next character in buffer */
2738                 cur_len += DISP_WIDTH(ch_len);
2739         }
2740
2741         if (frozen)
2742                 gtk_stext_thaw(text);
2743
2744         if (set_editable_pos && editable_pos <= tlen)
2745                 gtk_editable_set_position(GTK_EDITABLE(text), editable_pos);
2746 }
2747
2748 #undef STEXT_FREEZE
2749 #undef GET_CHAR
2750
2751 static void compose_set_title(Compose *compose)
2752 {
2753         gchar *str;
2754         gchar *edited;
2755
2756         edited = compose->modified ? _(" [Edited]") : "";
2757         if (compose->account && compose->account->address)
2758                 str = g_strdup_printf(_("%s - Compose message%s"),
2759                                       compose->account->address, edited);
2760         else
2761                 str = g_strdup_printf(_("Compose message%s"), edited);
2762         gtk_window_set_title(GTK_WINDOW(compose->window), str);
2763         g_free(str);
2764 }
2765
2766 /**
2767  * compose_current_mail_account:
2768  * 
2769  * Find a current mail account (the currently selected account, or the
2770  * default account, if a news account is currently selected).  If a
2771  * mail account cannot be found, display an error message.
2772  * 
2773  * Return value: Mail account, or NULL if not found.
2774  **/
2775 static PrefsAccount *
2776 compose_current_mail_account(void)
2777 {
2778         PrefsAccount *ac;
2779
2780         if (cur_account && cur_account->protocol != A_NNTP)
2781                 ac = cur_account;
2782         else {
2783                 ac = account_get_default();
2784                 if (!ac || ac->protocol == A_NNTP) {
2785                         alertpanel_error(_("Account for sending mail is not specified.\n"
2786                                            "Please select a mail account before sending."));
2787                         return NULL;
2788                 }
2789         }
2790         return ac;
2791 }
2792
2793 static void compose_select_account(Compose *compose, PrefsAccount *account,
2794                                    gboolean init)
2795 {
2796         GtkWidget *menuitem;
2797         GtkItemFactory *ifactory;
2798
2799         g_return_if_fail(account != NULL);
2800
2801         compose->account = account;
2802
2803         compose_set_title(compose);
2804
2805         ifactory = gtk_item_factory_from_widget(compose->menubar);
2806 #if 0
2807         if (account->protocol == A_NNTP) {
2808                 gtk_widget_show(compose->newsgroups_hbox);
2809                 gtk_widget_show(compose->newsgroups_entry);
2810                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 4);
2811                 compose->use_newsgroups = TRUE;
2812
2813                 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
2814                 gtk_check_menu_item_set_active
2815                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2816                 gtk_widget_set_sensitive(menuitem, TRUE);
2817                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2818                 gtk_check_menu_item_set_active
2819                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2820                 gtk_widget_set_sensitive(menuitem, TRUE);
2821
2822                 menu_set_sensitive(ifactory, "/View/Followup to", TRUE);
2823         } else {
2824                 gtk_widget_hide(compose->newsgroups_hbox);
2825                 gtk_widget_hide(compose->newsgroups_entry);
2826                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 0);
2827                 gtk_widget_queue_resize(compose->table_vbox);
2828                 compose->use_newsgroups = FALSE;
2829
2830                 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
2831                 gtk_check_menu_item_set_active
2832                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2833                 gtk_widget_set_sensitive(menuitem, FALSE);
2834                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2835                 gtk_check_menu_item_set_active
2836                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2837                 gtk_widget_set_sensitive(menuitem, FALSE);
2838
2839                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Followup to");
2840                 gtk_check_menu_item_set_active
2841                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2842                 gtk_widget_set_sensitive(menuitem, FALSE);
2843         }
2844
2845         if (account->set_autocc) {
2846                 compose_entry_show(compose, COMPOSE_ENTRY_CC);
2847                 if (account->auto_cc && compose->mode != COMPOSE_REEDIT)
2848                         compose_entry_set(compose, account->auto_cc,
2849                                           COMPOSE_ENTRY_CC);
2850         }
2851         if (account->set_autobcc) {
2852                 compose_entry_show(compose, COMPOSE_ENTRY_BCC);
2853                 if (account->auto_bcc && compose->mode != COMPOSE_REEDIT)
2854                         compose_entry_set(compose, account->auto_bcc,
2855                                           COMPOSE_ENTRY_BCC);
2856         }
2857         if (account->set_autoreplyto) {
2858                 compose_entry_show(compose, COMPOSE_ENTRY_REPLY_TO);
2859                 if (account->auto_replyto && compose->mode != COMPOSE_REEDIT)
2860                         compose_entry_set(compose, account->auto_replyto,
2861                                           COMPOSE_ENTRY_REPLY_TO);
2862         }
2863
2864         menuitem = gtk_item_factory_get_item(ifactory, "/View/Ruler");
2865         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2866                                        prefs_common.show_ruler);
2867 #endif
2868
2869 #if USE_GPGME
2870         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
2871         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2872                                        account->default_sign);
2873         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
2874         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2875                                        account->default_encrypt);
2876                                        
2877         activate_gnupg_mode(compose, account);          
2878 #endif /* USE_GPGME */
2879
2880         if (!init)
2881                 compose_insert_sig(compose, TRUE);
2882 }
2883
2884 gboolean compose_check_for_valid_recipient(Compose *compose) {
2885         gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
2886         gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
2887         gboolean recipient_found = FALSE;
2888         GSList *list;
2889         gchar **strptr;
2890
2891         /* free to and newsgroup list */
2892         slist_free_strings(compose->to_list);
2893         g_slist_free(compose->to_list);
2894         compose->to_list = NULL;
2895                         
2896         slist_free_strings(compose->newsgroup_list);
2897         g_slist_free(compose->newsgroup_list);
2898         compose->newsgroup_list = NULL;
2899
2900         /* search header entries for to and newsgroup entries */
2901         for (list = compose->header_list; list; list = list->next) {
2902                 gchar *header;
2903                 gchar *entry;
2904                 header = gtk_editable_get_chars(GTK_EDITABLE(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry), 0, -1);
2905                 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
2906                 g_strstrip(entry);
2907                 if (entry[0] != '\0') {
2908                         for (strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
2909                                 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2910                                         compose->to_list = address_list_append(compose->to_list, entry);
2911                                         recipient_found = TRUE;
2912                                 }
2913                         }
2914                         for (strptr = recipient_headers_news; *strptr != NULL; strptr++) {
2915                                 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2916                                         compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
2917                                         recipient_found = TRUE;
2918                                 }
2919                         }
2920                 }
2921                 g_free(header);
2922                 g_free(entry);
2923         }
2924         return recipient_found;
2925 }
2926
2927 static gboolean compose_check_entries(Compose *compose, gboolean check_subject)
2928 {
2929         gchar *str;
2930
2931         if (compose_check_for_valid_recipient(compose) == FALSE) {
2932                 alertpanel_error(_("Recipient is not specified."));
2933                 return FALSE;
2934         }
2935
2936         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
2937         if (*str == '\0' && check_subject == TRUE) {
2938                 AlertValue aval;
2939
2940                 aval = alertpanel(_("Send"),
2941                                   _("Subject is empty. Send it anyway?"),
2942                                   _("Yes"), _("No"), NULL);
2943                 if (aval != G_ALERTDEFAULT)
2944                         return FALSE;
2945         }
2946
2947         return TRUE;
2948 }
2949
2950 gint compose_send(Compose *compose)
2951 {
2952         gint msgnum;
2953         FolderItem *folder;
2954         gint val;
2955         gchar *msgpath;
2956
2957         if (compose_check_entries(compose, TRUE) == FALSE)
2958                 return -1;
2959
2960         val = compose_queue(compose, &msgnum, &folder);
2961         if (val) {
2962                 alertpanel_error(_("Could not queue message for sending"));
2963                 return -1;
2964         }
2965
2966         if (msgnum == 0) {
2967                 alertpanel_error(_("The message was queued but could not be sent.\nUse \"Send queued messages\" from the main window to retry."));
2968                 return 0;
2969         }
2970         
2971         msgpath = folder_item_fetch_msg(folder, msgnum);
2972         val = procmsg_send_message_queue(msgpath);
2973         g_free(msgpath);
2974
2975         folder_item_remove_msg(folder, msgnum);
2976
2977         return val;
2978 }
2979
2980 #if 0 /* compose restructure */
2981 gint compose_send(Compose *compose)
2982 {
2983         gchar tmp[MAXPATHLEN + 1];
2984         gint ok = 0;
2985         static gboolean lock = FALSE;
2986
2987         if (lock) return 1;
2988
2989         g_return_val_if_fail(compose->account != NULL, -1);
2990
2991         lock = TRUE;
2992
2993         if (compose_check_entries(compose, TRUE) == FALSE) {
2994                 lock = FALSE;
2995                 return 1;
2996         }
2997
2998         /* write to temporary file */
2999         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
3000                    get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
3001
3002         if (prefs_common.linewrap_at_send)
3003                 compose_wrap_line_all(compose);
3004
3005         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
3006                 lock = FALSE;
3007                 return -1;
3008         }
3009
3010         if (!compose->to_list && !compose->newsgroup_list) {
3011                 g_warning("can't get recipient list.");
3012                 unlink(tmp);
3013                 lock = FALSE;
3014                 return -1;
3015         }
3016
3017         if (compose->to_list) {
3018                 PrefsAccount *ac;
3019
3020 #if 0 /* NEW COMPOSE GUI */
3021                 if (compose->account->protocol != A_NNTP)
3022                         ac = compose->account;
3023                 else {
3024                         ac = account_find_from_address(compose->account->address);
3025                         if (!ac) {
3026                                 if (cur_account && cur_account->protocol != A_NNTP)
3027                                         ac = cur_account;
3028                                 else
3029                                         ac = account_get_default();
3030                         }
3031                         if (!ac || ac->protocol == A_NNTP) {
3032                                 alertpanel_error(_("Account for sending mail is not specified.\n"
3033                                                    "Please select a mail account before sending."));
3034                                 unlink(tmp);
3035                                 lock = FALSE;
3036                                 return -1;
3037                         }
3038                 }
3039 #endif
3040                 ac = compose->account;
3041
3042                 ok = send_message(tmp, ac, compose->to_list);
3043         }
3044
3045         if (ok == 0 && compose->newsgroup_list) {
3046                 ok = news_post(FOLDER(compose->account->folder), tmp);
3047                 if (ok < 0) {
3048                         alertpanel_error(_("Error occurred while posting the message to %s ."),
3049                                          compose->account->nntp_server);
3050                         unlink(tmp);
3051                         lock = FALSE;
3052                         return -1;
3053                 }
3054         }
3055
3056         /* queue message if failed to send */
3057         if (ok < 0) {
3058                 if (prefs_common.queue_msg) {
3059                         AlertValue val;
3060
3061                         val = alertpanel
3062                                 (_("Queueing"),
3063                                  _("Error occurred while sending the message.\n"
3064                                    "Put this message into queue folder?"),
3065                                  _("OK"), _("Cancel"), NULL);
3066                         if (G_ALERTDEFAULT == val) {
3067                                 ok = compose_queue(compose, tmp);
3068                                 if (ok < 0)
3069                                         alertpanel_error(_("Can't queue the message."));
3070                         }
3071                 } else
3072                         alertpanel_error_log(_("Error occurred while sending the message."));
3073         } else {
3074                 if (compose->mode == COMPOSE_REEDIT) {
3075                         compose_remove_reedit_target(compose);
3076                 }
3077                 /* save message to outbox */
3078                 if (prefs_common.savemsg) {
3079                         FolderItem *outbox;
3080
3081                         outbox = account_get_special_folder
3082                                 (compose->account, F_OUTBOX);
3083                         if (procmsg_save_to_outbox(outbox, tmp, FALSE) < 0)
3084                                 alertpanel_error
3085                                         (_("Can't save the message to Sent."));
3086                 }
3087         }
3088
3089         unlink(tmp);
3090         lock = FALSE;
3091         return ok;
3092 }
3093 #endif
3094
3095 static gboolean compose_use_attach(Compose *compose) 
3096 {
3097         return gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL;
3098 }
3099
3100 static gint compose_redirect_write_headers_from_headerlist(Compose *compose, 
3101                                                            FILE *fp)
3102 {
3103         gchar buf[BUFFSIZE];
3104         gchar *str;
3105         gboolean first_address;
3106         GSList *list;
3107         ComposeHeaderEntry *headerentry;
3108         gchar *headerentryname;
3109         gchar *cc_hdr;
3110         gchar *to_hdr;
3111
3112         debug_print("Writing redirect header\n");
3113
3114         cc_hdr = prefs_common.trans_hdr ? _("Cc:") : "Cc:";
3115         to_hdr = prefs_common.trans_hdr ? _("To:") : "To:";
3116
3117         first_address = TRUE;
3118         for (list = compose->header_list; list; list = list->next) {
3119                 headerentry = ((ComposeHeaderEntry *)list->data);
3120                 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
3121
3122                 if (g_strcasecmp(headerentryname, cc_hdr) == 0 
3123                    || g_strcasecmp(headerentryname, to_hdr) == 0) {
3124                         str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
3125                         Xstrdup_a(str, str, return -1);
3126                         g_strstrip(str);
3127                         if (str[0] != '\0') {
3128                                 compose_convert_header
3129                                         (buf, sizeof(buf), str,
3130                                         strlen("Resent-To") + 2, TRUE);
3131                                 if (first_address) {
3132                                         fprintf(fp, "Resent-To: ");
3133                                         first_address = FALSE;
3134                                 } else {
3135                                         fprintf(fp, ",");
3136                                 }
3137                                 fprintf(fp, "%s", buf);
3138                         }
3139                 }
3140         }
3141         /* if (!first_address) { */
3142         fprintf(fp, "\n");
3143         /* } */
3144
3145         return(0);
3146 }
3147
3148 static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
3149 {
3150         gchar buf[BUFFSIZE];
3151         gchar *str;
3152         /* struct utsname utsbuf; */
3153
3154         g_return_val_if_fail(fp != NULL, -1);
3155         g_return_val_if_fail(compose->account != NULL, -1);
3156         g_return_val_if_fail(compose->account->address != NULL, -1);
3157
3158         /* Resent-Date */
3159         get_rfc822_date(buf, sizeof(buf));
3160         fprintf(fp, "Resent-Date: %s\n", buf);
3161
3162         /* Resent-From */
3163         if (compose->account->name && *compose->account->name) {
3164                 compose_convert_header
3165                         (buf, sizeof(buf), compose->account->name,
3166                          strlen("From: "), TRUE);
3167                 fprintf(fp, "Resent-From: %s <%s>\n",
3168                         buf, compose->account->address);
3169         } else
3170                 fprintf(fp, "Resent-From: %s\n", compose->account->address);
3171
3172         /* Subject */
3173         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
3174         if (*str != '\0') {
3175                 Xstrdup_a(str, str, return -1);
3176                 g_strstrip(str);
3177                 if (*str != '\0') {
3178                         compose_convert_header(buf, sizeof(buf), str,
3179                                                strlen("Subject: "), FALSE);
3180                         fprintf(fp, "Subject: %s\n", buf);
3181                 }
3182         }
3183
3184         /* Resent-Message-ID */
3185         if (compose->account->gen_msgid) {
3186                 compose_generate_msgid(compose, buf, sizeof(buf));
3187                 fprintf(fp, "Resent-Message-Id: <%s>\n", buf);
3188                 compose->msgid = g_strdup(buf);
3189         }
3190
3191         compose_redirect_write_headers_from_headerlist(compose, fp);
3192
3193         /* separator between header and body */
3194         fputs("\n", fp);
3195
3196         return 0;
3197 }
3198
3199 static gint compose_redirect_write_to_file(Compose *compose, const gchar *file)
3200 {
3201         FILE *fp;
3202         FILE *fdest;
3203         size_t len;
3204         gchar buf[BUFFSIZE];
3205
3206         if ((fp = fopen(compose->redirect_filename, "rb")) == NULL) {
3207                 FILE_OP_ERROR(file, "fopen");
3208                 return -1;
3209         }
3210
3211         if ((fdest = fopen(file, "wb")) == NULL) {
3212                 FILE_OP_ERROR(file, "fopen");
3213                 fclose(fp);
3214                 return -1;
3215         }
3216
3217         /* chmod for security */
3218         if (change_file_mode_rw(fdest, file) < 0) {
3219                 FILE_OP_ERROR(file, "chmod");
3220                 g_warning("can't change file mode\n");
3221         }
3222
3223         while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
3224                 /* should filter returnpath, delivered-to */
3225                 if (g_strncasecmp(buf, "Return-Path:",
3226                                    strlen("Return-Path:")) == 0 ||
3227                     g_strncasecmp(buf, "Delivered-To:",
3228                                   strlen("Delivered-To:")) == 0 ||
3229                     g_strncasecmp(buf, "Received:",
3230                                   strlen("Received:")) == 0 ||
3231                     g_strncasecmp(buf, "Subject:",
3232                                   strlen("Subject:")) == 0 ||
3233                     g_strncasecmp(buf, "X-UIDL:",
3234                                   strlen("X-UIDL:")) == 0)
3235                         continue;
3236
3237                 if (fputs(buf, fdest) == -1)
3238                         goto error;
3239
3240                 if (!prefs_common.redirect_keep_from) {
3241                         if (g_strncasecmp(buf, "From:",
3242                                           strlen("From:")) == 0) {
3243                                 fputs(" (by way of ", fdest);
3244                                 if (compose->account->name
3245                                     && *compose->account->name) {
3246                                         compose_convert_header
3247                                                 (buf, sizeof(buf),
3248                                                  compose->account->name,
3249                                                  strlen("From: "),
3250                                                  FALSE);
3251                                         fprintf(fdest, "%s <%s>",
3252                                                 buf,
3253                                                 compose->account->address);
3254                                 } else
3255                                         fprintf(fdest, "%s",
3256                                                 compose->account->address);
3257                                 fputs(")", fdest);
3258                         }
3259                 }
3260
3261                 if (fputs("\n", fdest) == -1)
3262                         goto error;
3263         }
3264
3265         compose_redirect_write_headers(compose, fdest);
3266
3267         while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
3268                 if (fwrite(buf, sizeof(gchar), len, fdest) != len) {
3269                         FILE_OP_ERROR(file, "fwrite");
3270                         goto error;
3271                 }
3272         }
3273
3274         fclose(fp);
3275         if (fclose(fdest) == EOF) {
3276                 FILE_OP_ERROR(file, "fclose");
3277                 unlink(file);
3278                 return -1;
3279         }
3280
3281         return 0;
3282  error:
3283         fclose(fp);
3284         fclose(fdest);
3285         unlink(file);
3286
3287         return -1;
3288 }
3289
3290
3291 #if USE_GPGME
3292 /* interfaces to rfc2015 to keep out the prefs stuff there.
3293  * returns 0 on success and -1 on error. */
3294 static gint compose_create_signers_list(Compose *compose, GSList **pkey_list)
3295 {
3296         const gchar *key_id = NULL;
3297         GSList *key_list;
3298
3299         switch (compose->account->sign_key) {
3300         case SIGN_KEY_DEFAULT:
3301                 *pkey_list = NULL;
3302                 return 0;
3303         case SIGN_KEY_BY_FROM:
3304                 key_id = compose->account->address;
3305                 break;
3306         case SIGN_KEY_CUSTOM:
3307                 key_id = compose->account->sign_key_id;
3308                 break;
3309         default:
3310                 break;
3311         }
3312
3313         key_list = rfc2015_create_signers_list(key_id);
3314
3315         if (!key_list) {
3316                 alertpanel_error(_("Could not find any key associated with "
3317                                    "currently selected key id `%s'."), key_id);
3318                 return -1;
3319         }
3320
3321         *pkey_list = key_list;
3322         return 0;
3323 }
3324
3325 /* clearsign message body text */
3326 static gint compose_clearsign_text(Compose *compose, gchar **text)
3327 {
3328         GSList *key_list;
3329         gchar *tmp_file;
3330
3331         tmp_file = get_tmp_file();
3332         if (str_write_to_file(*text, tmp_file) < 0) {
3333                 g_free(tmp_file);
3334                 return -1;
3335         }
3336
3337         if (compose_create_signers_list(compose, &key_list) < 0 ||
3338             rfc2015_clearsign(tmp_file, key_list) < 0) {
3339                 unlink(tmp_file);
3340                 g_free(tmp_file);
3341                 return -1;
3342         }
3343
3344         g_free(*text);
3345         *text = file_read_to_str(tmp_file);
3346         unlink(tmp_file);
3347         g_free(tmp_file);
3348         if (*text == NULL)
3349                 return -1;
3350
3351         return 0;
3352 }
3353 #endif /* USE_GPGME */
3354
3355 static gint compose_write_to_file(Compose *compose, const gchar *file,
3356                                   gboolean is_draft)
3357 {
3358         FILE *fp;
3359         size_t len;
3360         gchar *chars;
3361         gchar *buf;
3362         gchar *canon_buf;
3363         const gchar *out_codeset;
3364         EncodingType encoding;
3365
3366         if ((fp = fopen(file, "wb")) == NULL) {
3367                 FILE_OP_ERROR(file, "fopen");
3368                 return -1;
3369         }
3370
3371         /* chmod for security */
3372         if (change_file_mode_rw(fp, file) < 0) {
3373                 FILE_OP_ERROR(file, "chmod");
3374                 g_warning("can't change file mode\n");
3375         }
3376
3377         /* get all composed text */
3378         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
3379         len = strlen(chars);
3380         if (is_ascii_str(chars)) {
3381                 buf = chars;
3382                 chars = NULL;
3383                 out_codeset = CS_US_ASCII;
3384                 encoding = ENC_7BIT;
3385         } else {
3386                 const gchar *src_codeset;
3387
3388                 out_codeset = conv_get_outgoing_charset_str();
3389                 if (!strcasecmp(out_codeset, CS_US_ASCII))
3390                         out_codeset = CS_ISO_8859_1;
3391
3392                 if (prefs_common.encoding_method == CTE_BASE64)
3393                         encoding = ENC_BASE64;
3394                 else if (prefs_common.encoding_method == CTE_QUOTED_PRINTABLE)
3395                         encoding = ENC_QUOTED_PRINTABLE;
3396                 else if (prefs_common.encoding_method == CTE_8BIT)
3397                         encoding = ENC_8BIT;
3398                 else
3399                         encoding = procmime_get_encoding_for_charset(out_codeset);
3400
3401 #if USE_GPGME
3402                 if (!is_draft &&
3403                     compose->use_signing && !compose->gnupg_mode &&
3404                     encoding == ENC_8BIT)
3405                         encoding = ENC_BASE64;
3406 #endif
3407
3408                 src_codeset = conv_get_current_charset_str();
3409                 /* if current encoding is US-ASCII, set it the same as
3410                    outgoing one to prevent code conversion failure */
3411                 if (!strcasecmp(src_codeset, CS_US_ASCII))
3412                         src_codeset = out_codeset;
3413
3414                 debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
3415                             src_codeset, out_codeset, procmime_get_encoding_str(encoding));
3416
3417                 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
3418                 if (!buf) {
3419                         AlertValue aval;
3420                         gchar *msg;
3421
3422                         msg = g_strdup_printf(_("Can't convert the character encoding of the message from\n"
3423                                                 "%s to %s.\n"
3424                                                 "Send it anyway?"), src_codeset, out_codeset);
3425                         aval = alertpanel
3426                                 (_("Error"), msg, _("Yes"), _("+No"), NULL);
3427                         g_free(msg);
3428
3429                         if (aval != G_ALERTDEFAULT) {
3430                                 g_free(chars);
3431                                 fclose(fp);
3432                                 unlink(file);
3433                                 return -1;
3434                         } else {
3435                                 buf = chars;
3436                                 out_codeset = src_codeset;
3437                                 chars = NULL;
3438                         }
3439                 }
3440         }
3441         g_free(chars);
3442
3443         canon_buf = canonicalize_str(buf);
3444         g_free(buf);
3445         buf = canon_buf;
3446
3447 #if USE_GPGME
3448         if (!is_draft && compose->use_signing && compose->gnupg_mode) {
3449                 if (compose_clearsign_text(compose, &buf) < 0) {
3450                         g_warning("clearsign failed\n");
3451                         fclose(fp);
3452                         unlink(file);
3453                         g_free(buf);
3454                         return -1;
3455                 }
3456         }
3457 #endif
3458
3459         /* write headers */
3460         if (compose_write_headers
3461                 (compose, fp, out_codeset, encoding, is_draft) < 0) {
3462                 g_warning("can't write headers\n");
3463                 fclose(fp);
3464                 /* unlink(file); */
3465                 g_free(buf);
3466                 return -1;
3467         }
3468
3469         if (compose_use_attach(compose)) {
3470 #if USE_GPGME
3471             /* This prolog message is ignored by mime software and
3472              * because it would make our signing/encryption task
3473              * tougher, we don't emit it in that case */
3474             if (!compose->use_signing && !compose->use_encryption)
3475 #endif
3476                 fputs("This is a multi-part message in MIME format.\n", fp);
3477
3478                 fprintf(fp, "\n--%s\n", compose->boundary);
3479                 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
3480                         out_codeset);
3481                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
3482                         procmime_get_encoding_str(encoding));
3483                 fputc('\n', fp);
3484         }
3485
3486         /* write body */
3487         len = strlen(buf);
3488         if (encoding == ENC_BASE64) {
3489                 gchar outbuf[B64_BUFFSIZE];
3490                 gint i, l;
3491
3492                 for (i = 0; i < len; i += B64_LINE_SIZE) {
3493                         l = MIN(B64_LINE_SIZE, len - i);
3494                         base64_encode(outbuf, buf + i, l);
3495                         fputs(outbuf, fp);
3496                         fputc('\n', fp);
3497                 }
3498         } else if (encoding == ENC_QUOTED_PRINTABLE) {
3499                 gchar *outbuf;
3500                 size_t outlen;
3501
3502                 outbuf = g_malloc(len * 4);
3503                 qp_encode_line(outbuf, buf);
3504                 outlen = strlen(outbuf);
3505                 if (fwrite(outbuf, sizeof(gchar), outlen, fp) != outlen) {
3506                         FILE_OP_ERROR(file, "fwrite");
3507                         fclose(fp);
3508                         unlink(file);
3509                         g_free(outbuf);
3510                         g_free(buf);
3511                         return -1;
3512                 }
3513                 g_free(outbuf);
3514         } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
3515                 FILE_OP_ERROR(file, "fwrite");
3516                 fclose(fp);
3517                 unlink(file);
3518                 g_free(buf);
3519                 return -1;
3520         }
3521         g_free(buf);
3522
3523         if (compose_use_attach(compose))
3524                 compose_write_attach(compose, fp);
3525
3526         if (fclose(fp) == EOF) {
3527                 FILE_OP_ERROR(file, "fclose");
3528                 unlink(file);
3529                 return -1;
3530         }
3531
3532 #if USE_GPGME
3533         if (is_draft) {
3534                 uncanonicalize_file_replace(file);
3535                 return 0;
3536         }
3537
3538         if ((compose->use_signing && !compose->gnupg_mode) ||
3539             compose->use_encryption) {
3540                 if (canonicalize_file_replace(file) < 0) {
3541                         unlink(file);
3542                         return -1;
3543                 }
3544         }
3545
3546         if (compose->use_signing && !compose->gnupg_mode) {
3547                 GSList *key_list;
3548
3549                 if (compose_create_signers_list(compose, &key_list) < 0 ||
3550                     rfc2015_sign(file, key_list) < 0) {
3551                         unlink(file);
3552                         return -1;
3553                 }
3554         }
3555         if (compose->use_encryption) {
3556                 if (rfc2015_encrypt(file, compose->to_list,
3557                                     compose->gnupg_mode) < 0) {
3558                         unlink(file);
3559                         return -1;
3560                 }
3561         }
3562 #endif /* USE_GPGME */
3563
3564         uncanonicalize_file_replace(file);
3565
3566         return 0;
3567 }
3568
3569 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
3570 {
3571         FILE *fp;
3572         size_t len;
3573         gchar *chars;
3574
3575         if ((fp = fopen(file, "wb")) == NULL) {
3576                 FILE_OP_ERROR(file, "fopen");
3577                 return -1;
3578         }
3579
3580         /* chmod for security */
3581         if (change_file_mode_rw(fp, file) < 0) {
3582                 FILE_OP_ERROR(file, "chmod");
3583                 g_warning("can't change file mode\n");
3584         }
3585
3586         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
3587
3588         /* write body */
3589         len = strlen(chars);
3590         if (fwrite(chars, sizeof(gchar), len, fp) != len) {
3591                 FILE_OP_ERROR(file, "fwrite");
3592                 g_free(chars);
3593                 fclose(fp);
3594                 unlink(file);
3595                 return -1;
3596         }
3597
3598         g_free(chars);
3599
3600         if (fclose(fp) == EOF) {
3601                 FILE_OP_ERROR(file, "fclose");
3602                 unlink(file);
3603                 return -1;
3604         }
3605         return 0;
3606 }
3607
3608 static gint compose_remove_reedit_target(Compose *compose)
3609 {
3610         FolderItem *item;
3611         MsgInfo *msginfo = compose->targetinfo;
3612
3613         g_return_val_if_fail(compose->mode == COMPOSE_REEDIT, -1);
3614         if (!msginfo) return -1;
3615
3616         item = msginfo->folder;
3617         g_return_val_if_fail(item != NULL, -1);
3618
3619         if (procmsg_msg_exist(msginfo) &&
3620             (item->stype == F_DRAFT || item->stype == F_QUEUE 
3621              || msginfo == compose->autosaved_draft)) {
3622                 if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
3623                         g_warning("can't remove the old message\n");
3624                         return -1;
3625                 }
3626         }
3627
3628         return 0;
3629 }
3630
3631 void compose_remove_draft(Compose *compose)
3632 {
3633         FolderItem *drafts;
3634         MsgInfo *msginfo = compose->targetinfo;
3635         drafts = account_get_special_folder(compose->account, F_DRAFT);
3636
3637         if (procmsg_msg_exist(msginfo)) {
3638                 folder_item_remove_msg(drafts, msginfo->msgnum);
3639         }
3640
3641 }
3642
3643 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
3644 {
3645         return compose_queue_sub (compose, msgnum, item, FALSE);
3646 }
3647 static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item, gboolean check_subject)
3648 {
3649         FolderItem *queue;
3650         gchar *tmp, *tmp2;
3651         FILE *fp, *src_fp;
3652         GSList *cur;
3653         gchar buf[BUFFSIZE];
3654         gint num;
3655         static gboolean lock = FALSE;
3656         PrefsAccount *mailac = NULL, *newsac = NULL;
3657         
3658         debug_print("queueing message...\n");
3659         g_return_val_if_fail(compose->account != NULL, -1);
3660
3661         lock = TRUE;
3662         
3663         if (compose_check_entries(compose, check_subject) == FALSE) {
3664                 lock = FALSE;
3665                 return -1;
3666         }
3667
3668         if (!compose->to_list && !compose->newsgroup_list) {
3669                 g_warning("can't get recipient list.");
3670                 lock = FALSE;
3671                 return -1;
3672         }
3673
3674         if (compose->to_list) {
3675                 if (compose->account->protocol != A_NNTP)
3676                         mailac = compose->account;
3677                 else if (cur_account && cur_account->protocol != A_NNTP)
3678                         mailac = cur_account;
3679                 else if (!(mailac = compose_current_mail_account())) {
3680                         lock = FALSE;
3681                         alertpanel_error(_("No account for sending mails available!"));
3682                         return -1;
3683                 }
3684         }
3685
3686         if (compose->newsgroup_list) {
3687                 if (compose->account->protocol == A_NNTP)
3688                         newsac = compose->account;
3689                 else if (!newsac->protocol != A_NNTP) {
3690                         lock = FALSE;
3691                         alertpanel_error(_("No account for posting news available!"));
3692                         return -1;
3693                 }                       
3694         }
3695
3696         if (prefs_common.linewrap_at_send)
3697                 compose_wrap_line_all(compose);
3698                         
3699         /* write to temporary file */
3700         tmp2 = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
3701                                       G_DIR_SEPARATOR, (gint)compose);
3702
3703         if (compose->redirect_filename != NULL) {
3704                 if (compose_redirect_write_to_file(compose, tmp2) < 0) {
3705                         unlink(tmp2);
3706                         lock = FALSE;
3707                         return -1;
3708                 }
3709         }
3710         else {
3711                 if (compose_write_to_file(compose, tmp2, FALSE) < 0) {
3712                         unlink(tmp2);
3713                         lock = FALSE;
3714                         return -1;
3715                 }
3716         }
3717
3718         /* add queue header */
3719         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
3720                               G_DIR_SEPARATOR, (gint)compose);
3721         if ((fp = fopen(tmp, "wb")) == NULL) {
3722                 FILE_OP_ERROR(tmp, "fopen");
3723                 g_free(tmp);
3724                 return -1;
3725         }
3726         if ((src_fp = fopen(tmp2, "rb")) == NULL) {
3727                 FILE_OP_ERROR(tmp2, "fopen");
3728                 fclose(fp);
3729                 unlink(tmp);
3730                 g_free(tmp);
3731                 unlink(tmp2);
3732                 g_free(tmp2);
3733                 return -1;
3734         }
3735         if (change_file_mode_rw(fp, tmp) < 0) {
3736                 FILE_OP_ERROR(tmp, "chmod");
3737                 g_warning("can't change file mode\n");
3738         }
3739
3740         /* queueing variables */
3741         fprintf(fp, "AF:\n");
3742         fprintf(fp, "NF:0\n");
3743         fprintf(fp, "PS:10\n");
3744         fprintf(fp, "SRH:1\n");
3745         fprintf(fp, "SFN:\n");
3746         fprintf(fp, "DSR:\n");
3747         if (compose->msgid)
3748                 fprintf(fp, "MID:<%s>\n", compose->msgid);
3749         else
3750                 fprintf(fp, "MID:\n");
3751         fprintf(fp, "CFG:\n");