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