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