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