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