fixed compilation bug in prefs_common.c
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 <unistd.h>
57 #include <time.h>
58 /* #include <sys/utsname.h> */
59 #include <stdlib.h>
60 #include <sys/wait.h>
61 #include <signal.h>
62 #include <errno.h>
63
64 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
65 #  include <wchar.h>
66 #  include <wctype.h>
67 #endif
68
69
70 #include "gtkstext.h"
71
72 #include "intl.h"
73 #include "main.h"
74 #include "mainwindow.h"
75 #include "compose.h"
76 #include "addressbook.h"
77 #include "folderview.h"
78 #include "procmsg.h"
79 #include "menu.h"
80 #include "send.h"
81 #include "news.h"
82 #include "customheader.h"
83 #include "prefs_common.h"
84 #include "prefs_account.h"
85 #include "account.h"
86 #include "filesel.h"
87 #include "procheader.h"
88 #include "procmime.h"
89 #include "statusbar.h"
90 #include "about.h"
91 #include "base64.h"
92 #include "codeconv.h"
93 #include "utils.h"
94 #include "gtkutils.h"
95 #include "socket.h"
96 #include "alertpanel.h"
97 #include "manage_window.h"
98 #include "gtkshruler.h"
99 #include "folder.h"
100 #include "addr_compl.h"
101 #include "template_select.h"
102
103 #if USE_GPGME
104 #  include "rfc2015.h"
105 #endif
106
107 #include "quote_fmt.h"
108
109 typedef enum
110 {
111         COL_MIMETYPE = 0,
112         COL_SIZE     = 1,
113         COL_NAME     = 2
114 } AttachColumnPos;
115
116 #define N_ATTACH_COLS           3
117
118 #define B64_LINE_SIZE           57
119 #define B64_BUFFSIZE            77
120
121 #define MAX_REFERENCES_LEN      999
122
123 static GdkColor quote_color = {0, 0, 0, 0xbfff};
124
125 static GList *compose_list = NULL;
126
127 static Compose *compose_create                  (PrefsAccount   *account);
128 static void compose_toolbar_create              (Compose        *compose,
129                                                  GtkWidget      *container);
130 static GtkWidget *compose_account_option_menu_create
131                                                 (Compose        *compose);
132 static void compose_destroy                     (Compose        *compose);
133
134 static gint compose_parse_header                (Compose        *compose,
135                                                  MsgInfo        *msginfo);
136 static gchar *compose_parse_references          (const gchar    *ref,
137                                                  const gchar    *msgid);
138 static void compose_quote_file                  (Compose        *compose,
139                                                  MsgInfo        *msginfo,
140                                                  FILE           *fp);
141 static gchar *compose_quote_parse_fmt           (Compose        *compose,
142                                                  MsgInfo        *msginfo,
143                                                  const gchar    *fmt);
144 static void compose_reply_set_entry             (Compose        *compose,
145                                                  MsgInfo        *msginfo,
146                                                  gboolean        to_all,
147                                                  gboolean        to_sender,
148                                                  gboolean
149                                                  followup_and_reply_to);
150 static void compose_reedit_set_entry            (Compose        *compose,
151                                                  MsgInfo        *msginfo);
152 static void compose_insert_sig                  (Compose        *compose);
153 static void compose_insert_file                 (Compose        *compose,
154                                                  const gchar    *file);
155 static void compose_attach_append               (Compose        *compose,
156                                                  const gchar    *file,
157                                                  ContentType     cnttype);
158 static void compose_attach_append_with_type(Compose *compose,
159                                             const gchar *file,
160                                             const gchar *type,
161                                             ContentType cnttype);
162 static void compose_wrap_line                   (Compose        *compose);
163 static void compose_set_title                   (Compose        *compose);
164
165 static PrefsAccount *compose_current_mail_account(void);
166 /* static gint compose_send                     (Compose        *compose); */
167 static gint compose_write_to_file               (Compose        *compose,
168                                                  const gchar    *file,
169                                                  gboolean        is_draft);
170 static gint compose_write_body_to_file          (Compose        *compose,
171                                                  const gchar    *file);
172 static gint compose_save_to_outbox              (Compose        *compose,
173                                                  const gchar    *file);
174 static gint compose_queue                       (Compose        *compose,
175                                                  const gchar    *file);
176 static void compose_write_attach                (Compose        *compose,
177                                                  FILE           *fp);
178 static gint compose_write_headers               (Compose        *compose,
179                                                  FILE           *fp,
180                                                  const gchar    *charset,
181                                                  EncodingType    encoding,
182                                                  gboolean        is_draft);
183
184 static void compose_convert_header              (gchar          *dest,
185                                                  gint            len,
186                                                  gchar          *src,
187                                                  gint            header_len);
188 static void compose_generate_msgid              (Compose        *compose,
189                                                  gchar          *buf,
190                                                  gint            len);
191
192 static void compose_attach_info_free            (AttachInfo     *ainfo);
193 static void compose_attach_remove_selected      (Compose        *compose);
194
195 static void compose_attach_property             (Compose        *compose);
196 static void compose_attach_property_create      (gboolean       *cancelled);
197 static void attach_property_ok                  (GtkWidget      *widget,
198                                                  gboolean       *cancelled);
199 static void attach_property_cancel              (GtkWidget      *widget,
200                                                  gboolean       *cancelled);
201 static gint attach_property_delete_event        (GtkWidget      *widget,
202                                                  GdkEventAny    *event,
203                                                  gboolean       *cancelled);
204 static void attach_property_key_pressed         (GtkWidget      *widget,
205                                                  GdkEventKey    *event,
206                                                  gboolean       *cancelled);
207
208 static void compose_exec_ext_editor             (Compose           *compose);
209 static gint compose_exec_ext_editor_real        (const gchar       *file);
210 static gboolean compose_ext_editor_kill         (Compose           *compose);
211 static void compose_input_cb                    (gpointer           data,
212                                                  gint               source,
213                                                  GdkInputCondition  condition);
214 static void compose_set_ext_editor_sensitive    (Compose           *compose,
215                                                  gboolean           sensitive);
216
217 static gint calc_cursor_xpos    (GtkSText       *text,
218                                  gint            extra,
219                                  gint            char_width);
220
221 /* callback functions */
222
223 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
224                                          GtkAllocation  *allocation,
225                                          GtkSHRuler     *shruler);
226
227 static void toolbar_send_cb             (GtkWidget      *widget,
228                                          gpointer        data);
229 static void toolbar_send_later_cb       (GtkWidget      *widget,
230                                          gpointer        data);
231 static void toolbar_draft_cb            (GtkWidget      *widget,
232                                          gpointer        data);
233 static void toolbar_insert_cb           (GtkWidget      *widget,
234                                          gpointer        data);
235 static void toolbar_attach_cb           (GtkWidget      *widget,
236                                          gpointer        data);
237 static void toolbar_sig_cb              (GtkWidget      *widget,
238                                          gpointer        data);
239 static void toolbar_ext_editor_cb       (GtkWidget      *widget,
240                                          gpointer        data);
241 static void toolbar_linewrap_cb         (GtkWidget      *widget,
242                                          gpointer        data);
243 static void toolbar_address_cb          (GtkWidget      *widget,
244                                          gpointer        data);
245 static void template_select_cb          (gpointer        daat,
246                                          guint           action,
247                                          GtkWidget      *widget);
248
249
250 static void select_account(Compose * compose, PrefsAccount * ac);
251 static void account_activated           (GtkMenuItem    *menuitem,
252                                          gpointer        data);
253
254 static void attach_selected             (GtkCList       *clist,
255                                          gint            row,
256                                          gint            column,
257                                          GdkEvent       *event,
258                                          gpointer        data);
259 static void attach_button_pressed       (GtkWidget      *widget,
260                                          GdkEventButton *event,
261                                          gpointer        data);
262 static void attach_key_pressed          (GtkWidget      *widget,
263                                          GdkEventKey    *event,
264                                          gpointer        data);
265
266 static void compose_send_cb             (gpointer        data,
267                                          guint           action,
268                                          GtkWidget      *widget);
269 static void compose_send_later_cb       (gpointer        data,
270                                          guint           action,
271                                          GtkWidget      *widget);
272
273 static void compose_draft_cb            (gpointer        data,
274                                          guint           action,
275                                          GtkWidget      *widget);
276
277 static void compose_attach_cb           (gpointer        data,
278                                          guint           action,
279                                          GtkWidget      *widget);
280 static void compose_insert_file_cb      (gpointer        data,
281                                          guint           action,
282                                          GtkWidget      *widget);
283
284 static void compose_close_cb            (gpointer        data,
285                                          guint           action,
286                                          GtkWidget      *widget);
287 static void compose_address_cb          (gpointer        data,
288                                          guint           action,
289                                          GtkWidget      *widget);
290
291 static void compose_ext_editor_cb       (gpointer        data,
292                                          guint           action,
293                                          GtkWidget      *widget);
294
295 static gint compose_delete_cb           (GtkWidget      *widget,
296                                          GdkEventAny    *event,
297                                          gpointer        data);
298 static void compose_destroy_cb          (GtkWidget      *widget,
299                                          Compose        *compose);
300
301 static void compose_cut_cb              (Compose        *compose);
302 static void compose_copy_cb             (Compose        *compose);
303 static void compose_paste_cb            (Compose        *compose);
304 static void compose_allsel_cb           (Compose        *compose);
305
306 static void compose_grab_focus_cb       (GtkWidget      *widget,
307                                          Compose        *compose);
308
309 static void compose_changed_cb          (GtkEditable    *editable,
310                                          Compose        *compose);
311 static void compose_button_press_cb     (GtkWidget      *widget,
312                                          GdkEventButton *event,
313                                          Compose        *compose);
314 #if 0
315 static void compose_key_press_cb        (GtkWidget      *widget,
316                                          GdkEventKey    *event,
317                                          Compose        *compose);
318 #endif
319
320 static void compose_toggle_to_cb        (gpointer        data,
321                                          guint           action,
322                                          GtkWidget      *widget);
323 static void compose_toggle_cc_cb        (gpointer        data,
324                                          guint           action,
325                                          GtkWidget      *widget);
326 static void compose_toggle_bcc_cb       (gpointer        data,
327                                          guint           action,
328                                          GtkWidget      *widget);
329 static void compose_toggle_replyto_cb   (gpointer        data,
330                                          guint           action,
331                                          GtkWidget      *widget);
332 static void compose_toggle_followupto_cb(gpointer        data,
333                                          guint           action,
334                                          GtkWidget      *widget);
335 static void compose_toggle_attach_cb    (gpointer        data,
336                                          guint           action,
337                                          GtkWidget      *widget);
338 static void compose_toggle_ruler_cb     (gpointer        data,
339                                          guint           action,
340                                          GtkWidget      *widget);
341 #if USE_GPGME
342 static void compose_toggle_sign_cb      (gpointer        data,
343                                          guint           action,
344                                          GtkWidget      *widget);
345 static void compose_toggle_encrypt_cb   (gpointer        data,
346                                          guint           action,
347                                          GtkWidget      *widget);
348 #endif
349 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
350                                              GtkWidget *widget);
351
352 static void compose_attach_drag_received_cb (GtkWidget          *widget,
353                                              GdkDragContext     *drag_context,
354                                              gint                x,
355                                              gint                y,
356                                              GtkSelectionData   *data,
357                                              guint               info,
358                                              guint               time,
359                                              gpointer            user_data);
360 static void compose_insert_drag_received_cb (GtkWidget          *widget,
361                                              GdkDragContext     *drag_context,
362                                              gint                x,
363                                              gint                y,
364                                              GtkSelectionData   *data,
365                                              guint               info,
366                                              guint               time,
367                                              gpointer            user_data);
368
369 static void to_activated                (GtkWidget      *widget,
370                                          Compose        *compose);
371 static void newsgroups_activated        (GtkWidget      *widget,
372                                          Compose        *compose);
373 static void subject_activated           (GtkWidget      *widget,
374                                          Compose        *compose);
375 static void cc_activated                (GtkWidget      *widget,
376                                          Compose        *compose);
377 static void bcc_activated               (GtkWidget      *widget,
378                                          Compose        *compose);
379 static void replyto_activated           (GtkWidget      *widget,
380                                          Compose        *compose);
381 static void followupto_activated        (GtkWidget      *widget,
382                                          Compose        *compose);
383 static void compose_attach_parts(Compose * compose,
384                                  MsgInfo * msginfo);
385
386 static gchar *compose_quote_fmt         (Compose        *compose,
387                                          MsgInfo        *msginfo,
388                                          const gchar    *fmt,
389                                          const gchar    * qmark);
390
391 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
392                                   gboolean to_all,
393                                   gboolean ignore_replyto,
394                                   gboolean followup_and_reply_to);
395
396 Compose * compose_generic_new (PrefsAccount     *account,
397                                const gchar      *to,
398                                FolderItem       *item);
399
400 static GtkItemFactoryEntry compose_popup_entries[] =
401 {
402         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
403         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
404         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
405         {N_("/_Property..."),   NULL, compose_attach_property, 0, NULL}
406 };
407
408 static GtkItemFactoryEntry compose_entries[] =
409 {
410         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
411         {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb, 0, NULL},
412         {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
413         {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig, 0, NULL},
414         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
415         {N_("/_File/_Close"),                   "<alt>W", compose_close_cb, 0, NULL},
416
417         {N_("/_Edit"),             NULL,         NULL,  0, "<Branch>"},
418         {N_("/_Edit/_Undo"),       "<control>Z", NULL,  0, NULL},
419         {N_("/_Edit/_Redo"),       "<control>Y", NULL,  0, NULL},
420         {N_("/_Edit/---"),         NULL,         NULL,  0, "<Separator>"},
421         {N_("/_Edit/Cu_t"),        "<control>X", compose_cut_cb,    0, NULL},
422         {N_("/_Edit/_Copy"),       "<control>C", compose_copy_cb,   0, NULL},
423         {N_("/_Edit/_Paste"),      "<control>V", compose_paste_cb,  0, NULL},
424         {N_("/_Edit/Select _all"), "<control>A", compose_allsel_cb, 0, NULL},
425         {N_("/_Edit/---"),         NULL,         NULL,  0, "<Separator>"},
426         {N_("/_Edit/Wrap long _lines"), "<alt>L", compose_wrap_line, 0, NULL},
427         {N_("/_Edit/Edit with e_xternal editor"), "<alt>X",
428                                         compose_ext_editor_cb,  0, NULL},
429
430         {N_("/_Message"),               NULL,           NULL,   0, "<Branch>"},
431         {N_("/_Message/_Send"),         "<shift><control>S",
432                                         compose_send_cb, 0, NULL},
433         {N_("/_Message/Send _later"),   "<shift><alt>S",
434                                         compose_send_later_cb,  0, NULL},
435         {N_("/_Message/Save to _draft folder"),
436                                         "<alt>D",       compose_draft_cb, 0, NULL},
437         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
438         {N_("/_Message/_To"),           NULL, compose_toggle_to_cb, 0, "<ToggleItem>"},
439         {N_("/_Message/_Cc"),           NULL, compose_toggle_cc_cb, 0, "<ToggleItem>"},
440         {N_("/_Message/_Bcc"),          NULL, compose_toggle_bcc_cb, 0, "<ToggleItem>"},
441         {N_("/_Message/_Reply to"),     NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
442         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
443         {N_("/_Message/_Followup to"),  NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
444         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
445         {N_("/_Message/_Attach"),       NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
446 #if USE_GPGME
447         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
448         {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb, 0, "<ToggleItem>"},
449         {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
450 #endif /* USE_GPGME */
451         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
452         {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
453         {N_("/_Tool"),                  NULL, NULL,     0, "<Branch>"},
454         {N_("/_Tool/Show _ruler"),      NULL, compose_toggle_ruler_cb,  0, "<ToggleItem>"},
455         {N_("/_Tool/_Address book"),    "<alt>A",       compose_address_cb, 0, NULL},
456         {N_("/_Tool/_Templates ..."),   NULL, template_select_cb, 0, NULL},
457         {N_("/_Help"),                  NULL, NULL,     0, "<LastBranch>"},
458         {N_("/_Help/_About"),           NULL,           about_show,     0, NULL}
459 };
460
461 static GtkTargetEntry compose_mime_types[] =
462 {
463         {"text/uri-list", 0, 0}
464 };
465
466 Compose * compose_new(PrefsAccount *account)
467 {
468         return compose_generic_new(account, NULL, NULL);
469 }
470
471 Compose * compose_new_with_recipient(PrefsAccount *account, const gchar *to)
472 {
473         return compose_generic_new(account, to, NULL);
474 }
475
476 Compose * compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
477 {
478         return compose_generic_new(account, NULL, item);
479 }
480
481 Compose * compose_generic_new(PrefsAccount *account, const gchar *to, FolderItem *item)
482 {
483         Compose *compose;
484
485         if (!account) account = cur_account;
486         g_return_val_if_fail(account != NULL, NULL);
487
488         compose = compose_create(account);
489         compose->mode = COMPOSE_NEW;
490         compose->replyinfo = NULL;
491
492         if (prefs_common.auto_sig)
493                 compose_insert_sig(compose);
494         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
495         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
496
497         if (account->protocol != A_NNTP) {
498                 if (to) {
499                         compose_entry_append(compose, to, COMPOSE_TO);
500                         gtk_widget_grab_focus(compose->subject_entry);
501                 } else {
502                         if(item && item->prefs->enable_default_to) {
503                                 compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
504                         } else {
505                                 gtk_widget_grab_focus(compose->to_entry);
506                         }
507                 }
508                 if(item && item->prefs->request_return_receipt) {
509                         GtkItemFactory *ifactory;
510                 
511                         ifactory = gtk_item_factory_from_widget(compose->menubar);
512                         menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
513                 }
514         } else {
515                 if (to) {
516                         compose_entry_append(compose, to, COMPOSE_NEWSGROUPS);
517                         gtk_widget_grab_focus(compose->subject_entry);
518                 } else
519                         gtk_widget_grab_focus(compose->newsgroups_entry);
520         }
521
522         return compose;
523 }
524
525 #define CHANGE_FLAGS(msginfo) \
526 { \
527 if (msginfo->folder->folder->change_flags != NULL) \
528 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
529                                       msginfo->folder, \
530                                       msginfo); \
531 }
532
533 /*
534 Compose * compose_new_followup_and_replyto(PrefsAccount *account,
535                                            const gchar *followupto, gchar * to)
536 {
537         Compose *compose;
538
539         if (!account) account = cur_account;
540         g_return_val_if_fail(account != NULL, NULL);
541         g_return_val_if_fail(account->protocol != A_NNTP, NULL);
542
543         compose = compose_create(account);
544         compose->mode = COMPOSE_NEW;
545
546         if (prefs_common.auto_sig)
547                 compose_insert_sig(compose);
548         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
549         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
550
551         compose_entry_append(compose, to, COMPOSE_TO);
552         compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
553         gtk_widget_grab_focus(compose->subject_entry);
554
555         return compose;
556 }
557 */
558
559 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
560                    gboolean ignore_replyto)
561 {
562         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, FALSE);
563 }
564
565 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
566                                    gboolean to_all,
567                                    gboolean ignore_replyto)
568 {
569         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, TRUE);
570 }
571
572 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
573                                   gboolean to_all,
574                                   gboolean ignore_replyto,
575                                   gboolean followup_and_reply_to)
576 {
577         Compose *compose;
578         PrefsAccount *account;
579         PrefsAccount *reply_account;
580         GtkSText *text;
581
582         g_return_if_fail(msginfo != NULL);
583         g_return_if_fail(msginfo->folder != NULL);
584
585         account = msginfo->folder->folder->account;
586         if (!account && msginfo->to && prefs_common.reply_account_autosel) {
587                 gchar *to;
588                 Xstrdup_a(to, msginfo->to, return);
589                 extract_address(to);
590                 account = account_find_from_address(to);
591         }
592         if (!account) account = cur_account;
593         g_return_if_fail(account != NULL);
594
595         if (ignore_replyto && account->protocol == A_NNTP &&
596             !followup_and_reply_to) {
597                 reply_account =
598                         account_find_from_address(account->address);
599                 if (!reply_account)
600                         reply_account = compose_current_mail_account();
601                 if (!reply_account)
602                         return;
603         } else
604                 reply_account = account;
605         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
606         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
607
608         CHANGE_FLAGS(msginfo);
609
610         compose = compose_create(reply_account);
611         compose->mode = COMPOSE_REPLY;
612         compose->replyinfo = msginfo;
613
614
615         if (followup_and_reply_to) {
616                 gtk_widget_show(compose->to_hbox);
617                 gtk_widget_show(compose->to_entry);
618                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
619                 compose->use_to = TRUE;
620         }
621
622         if (compose_parse_header(compose, msginfo) < 0) return;
623         compose_reply_set_entry(compose, msginfo, to_all, ignore_replyto,
624                                 followup_and_reply_to);
625
626         text = GTK_STEXT(compose->text);
627         gtk_stext_freeze(text);
628
629         if (quote) {
630                 FILE *fp;
631                 gchar *quote_str;
632
633                 if ((fp = procmime_get_text_part(msginfo)) == NULL)
634                         g_warning(_("Can't get text part\n"));
635                 else {
636                         gchar * qmark;
637
638                         if (prefs_common.quotemark && *prefs_common.quotemark)
639                                 qmark = prefs_common.quotemark;
640                         else
641                                 qmark = "> ";
642
643                         quote_str = compose_quote_fmt(compose, msginfo,
644                                                       prefs_common.quotefmt,
645                                                       qmark);
646
647                         /*
648                         quote_str = compose_quote_parse_fmt
649                                 (compose, msginfo, prefs_common.quotefmt);
650                         */
651
652                         if (quote_str != NULL)
653                                 gtk_stext_insert(text, NULL, NULL, NULL,
654                                                  quote_str, -1);
655                         /*                      g_free(quote_str); */
656                         /* compose_quote_file(compose, msginfo, fp); */
657                         fclose(fp);
658                 }
659         }
660
661         if (prefs_common.auto_sig)
662                 compose_insert_sig(compose);
663         gtk_editable_set_position(GTK_EDITABLE(text), 0);
664         gtk_stext_set_point(text, 0);
665
666         gtk_stext_thaw(text);
667         gtk_widget_grab_focus(compose->text);
668 }
669
670
671 static gchar *procmime_get_file_name(MimeInfo *mimeinfo)
672 {
673         gchar *base;
674         gchar *filename;
675
676         g_return_val_if_fail(mimeinfo != NULL, NULL);
677
678         base = mimeinfo->filename ? mimeinfo->filename
679                 : mimeinfo->name ? mimeinfo->name : NULL;
680
681         if (MIME_TEXT_HTML == mimeinfo->mime_type && base == NULL){
682                 filename = g_strdup_printf("%s%smimetmp%i.html",
683                                            get_mime_tmp_dir(),
684                                            G_DIR_SEPARATOR_S,
685                                            mimeinfo);
686                 return filename;
687         }
688         else {
689                 base = base ? base : "";
690                 base = g_basename(base);
691                 if (*base == '\0') {
692                         filename = g_strdup_printf("%s%smimetmp%i",
693                                                    get_mime_tmp_dir(),
694                                                    G_DIR_SEPARATOR_S,
695                                                    mimeinfo);
696                         return filename;
697                 }
698         }
699
700         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
701                                base, NULL);
702
703         return filename;
704 }
705
706 static gchar * mime_extract_file(gchar * source, MimeInfo *partinfo)
707 {
708         gchar *filename;
709
710         if (!partinfo) return;
711
712         filename = procmime_get_file_name(partinfo);
713
714         if (procmime_get_part(filename, source, partinfo) < 0)
715                 alertpanel_error
716                         (_("Can't get the part of multipart message."));
717
718         return filename;
719 }
720
721 static void compose_attach_parts(Compose * compose,
722                                  MsgInfo * msginfo)
723 {
724
725         FILE *fp;
726         gchar *file;
727         MimeInfo *mimeinfo;
728         MsgInfo *tmpmsginfo;
729         gchar *p;
730         gchar *boundary;
731         gint boundary_len = 0;
732         gchar buf[BUFFSIZE];
733         glong fpos, prev_fpos;
734         gint npart;
735         gchar * source;
736         gchar * filename;
737
738         g_return_if_fail(msginfo != NULL);
739         
740 #if USE_GPGME
741         for (;;) {
742                 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
743                 mimeinfo = procmime_scan_mime_header(fp);
744                 if (!mimeinfo) break;
745
746                 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
747                     rfc2015_is_encrypted(mimeinfo)) {
748                         MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
749                 }
750                 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
751                     !msginfo->plaintext_file  &&
752                     !msginfo->decryption_failed) {
753                         rfc2015_decrypt_message(msginfo, mimeinfo, fp);
754                         if (msginfo->plaintext_file &&
755                             !msginfo->decryption_failed) {
756                                 fclose(fp);
757                                 continue;
758                         }
759                 }
760                 
761                 break;
762         }
763 #else /* !USE_GPGME */
764         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
765         mimeinfo = procmime_scan_mime_header(fp);
766 #endif /* USE_GPGME */
767
768         fclose(fp);
769         if (!mimeinfo) return;
770         if (mimeinfo->mime_type == MIME_TEXT)
771                 return;
772
773         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
774
775         g_return_if_fail(mimeinfo != NULL);
776         g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
777
778         if (mimeinfo->mime_type == MIME_MULTIPART) {
779                 g_return_if_fail(mimeinfo->boundary != NULL);
780                 g_return_if_fail(mimeinfo->sub == NULL);
781         }
782         g_return_if_fail(fp != NULL);
783
784         boundary = mimeinfo->boundary;
785
786         if (boundary) {
787                 boundary_len = strlen(boundary);
788
789                 /* look for first boundary */
790                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
791                         if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
792                 if (!p) {
793                         fclose(fp);
794                         return;
795                 }
796         }
797
798         if ((fpos = ftell(fp)) < 0) {
799                 perror("ftell");
800                 fclose(fp);
801                 return;
802         }
803
804         for (npart = 0;; npart++) {
805                 MimeInfo *partinfo;
806                 gboolean eom = FALSE;
807
808                 prev_fpos = fpos;
809
810                 partinfo = procmime_scan_mime_header(fp);
811                 if (!partinfo) break;
812
813                 if (npart != 0)
814                         procmime_mimeinfo_insert(mimeinfo, partinfo);
815                 else
816                         procmime_mimeinfo_free(partinfo);
817
818                 /* look for next boundary */
819                 buf[0] = '\0';
820                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
821                         if (IS_BOUNDARY(buf, boundary, boundary_len)) {
822                                 if (buf[2 + boundary_len]     == '-' &&
823                                     buf[2 + boundary_len + 1] == '-')
824                                         eom = TRUE;
825                                 break;
826                         }
827                 }
828                 if (p == NULL)
829                         eom = TRUE;     /* broken MIME message */
830                 fpos = ftell(fp);
831
832                 partinfo->size = fpos - prev_fpos - strlen(buf);
833
834                 if (eom) break;
835         }
836
837         source = procmsg_get_message_file_path(msginfo);
838
839         g_return_if_fail(mimeinfo != NULL);
840
841         if (!mimeinfo->main && mimeinfo->parent)
842                 {
843                         filename = mime_extract_file(source, mimeinfo);
844
845                         compose_attach_append_with_type(compose, filename,
846                                                         mimeinfo->content_type,
847                                                         mimeinfo->mime_type);
848
849                         g_free(filename);
850                 }
851
852         if (mimeinfo->sub && mimeinfo->sub->children)
853                 {
854                         filename = mime_extract_file(source, mimeinfo->sub);
855
856                         compose_attach_append_with_type(compose, filename,
857                                                         mimeinfo->content_type,
858                                                         mimeinfo->sub->mime_type);
859
860                         g_free(filename);
861                 }
862
863         if (mimeinfo->children) {
864                 MimeInfo *child;
865
866                 child = mimeinfo->children;
867                 while (child) {
868                         filename = mime_extract_file(source, child);
869
870                         compose_attach_append_with_type(compose, filename,
871                                                         child->content_type,
872                                                         child->mime_type);
873
874                         g_free(filename);
875
876                         child = child->next;
877                 }
878         }
879
880         fclose(fp);
881
882         procmime_mimeinfo_free_all(mimeinfo);
883 }
884
885
886 #define INSERT_FW_HEADER(var, hdr) \
887 if (msginfo->var && *msginfo->var) { \
888         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
889         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
890         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
891 }
892
893 Compose * compose_forward(PrefsAccount * account, MsgInfo *msginfo,
894                           gboolean as_attach)
895 {
896         Compose *compose;
897         /*      PrefsAccount *account; */
898         GtkSText *text;
899         FILE *fp;
900         gchar buf[BUFFSIZE];
901
902         g_return_val_if_fail(msginfo != NULL, NULL);
903         g_return_val_if_fail(msginfo->folder != NULL, NULL);
904
905         if (account == NULL) {
906                 account = cur_account;
907                 /*
908                 account = msginfo->folder->folder->account;
909                 if (!account) account = cur_account;
910                 */
911         }
912         g_return_val_if_fail(account != NULL, NULL);
913
914         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
915         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
916         CHANGE_FLAGS(msginfo);
917
918         compose = compose_create(account);
919         compose->mode = COMPOSE_FORWARD;
920
921         if (msginfo->subject && *msginfo->subject) {
922                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
923                 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
924                                       msginfo->subject);
925         }
926
927         text = GTK_STEXT(compose->text);
928         gtk_stext_freeze(text);
929
930         if (as_attach) {
931                 gchar *msgfile;
932
933                 msgfile = procmsg_get_message_file_path(msginfo);
934                 if (!is_file_exist(msgfile))
935                         g_warning(_("%s: file not exist\n"), msgfile);
936                 else
937                         compose_attach_append(compose, msgfile,
938                                               MIME_MESSAGE_RFC822);
939
940                 g_free(msgfile);
941         } else {
942                 FILE *fp;
943                 gchar *quote_str;
944
945                 if ((fp = procmime_get_text_part(msginfo)) == NULL)
946                         g_warning(_("Can't get text part\n"));
947                 else {
948                         gchar * qmark;
949
950                         if (prefs_common.fw_quotemark &&
951                             *prefs_common.fw_quotemark)
952                                 qmark = prefs_common.fw_quotemark;
953                         else
954                                 qmark = "> ";
955
956                         quote_str = compose_quote_fmt(compose, msginfo,
957                                                       prefs_common.fw_quotefmt,
958                                                       qmark);
959
960                         if (quote_str != NULL)
961                                 gtk_stext_insert(text, NULL, NULL, NULL,
962                                                  quote_str, -1);
963
964                         fclose(fp);
965                 }
966
967                 compose_attach_parts(compose, msginfo);
968         }
969
970         if (prefs_common.auto_sig)
971                 compose_insert_sig(compose);
972         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
973         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
974
975         gtk_stext_thaw(text);
976         if (account->protocol != A_NNTP)
977                 gtk_widget_grab_focus(compose->to_entry);
978         else
979                 gtk_widget_grab_focus(compose->newsgroups_entry);
980
981         return compose;
982 }
983
984 #undef INSERT_FW_HEADER
985
986 Compose * compose_forward_multiple(PrefsAccount * account, 
987                           GSList *msginfo_list) 
988 {
989         Compose *compose;
990         GtkSText *text;
991         GSList *msginfo;
992         gchar *msgfile;
993
994         g_return_val_if_fail(msginfo_list != NULL, NULL);
995         
996         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
997                 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
998                         return NULL;
999         }
1000         
1001         if (account == NULL) {
1002                 account = cur_account;
1003                 /*
1004                 account = msginfo->folder->folder->account;
1005                 if (!account) account = cur_account;
1006                 */
1007         }
1008         g_return_val_if_fail(account != NULL, NULL);
1009
1010         compose = compose_create(account);
1011         compose->mode = COMPOSE_FORWARD;
1012
1013         text = GTK_STEXT(compose->text);
1014         gtk_stext_freeze(text);
1015
1016         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1017                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1018                 if (!is_file_exist(msgfile))
1019                         g_warning(_("%s: file not exist\n"), msgfile);
1020                 else
1021                         compose_attach_append(compose, msgfile,
1022                                 MIME_MESSAGE_RFC822);
1023                 g_free(msgfile);
1024         }
1025
1026         if (prefs_common.auto_sig)
1027                 compose_insert_sig(compose);
1028         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1029         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1030
1031         gtk_stext_thaw(text);
1032         if (account->protocol != A_NNTP)
1033                 gtk_widget_grab_focus(compose->to_entry);
1034         else
1035                 gtk_widget_grab_focus(compose->newsgroups_entry);
1036
1037         return compose;
1038 }
1039
1040 void compose_reedit(MsgInfo *msginfo)
1041 {
1042         Compose *compose;
1043         PrefsAccount *account;
1044         GtkSText *text;
1045         FILE *fp;
1046         gchar buf[BUFFSIZE];
1047
1048         g_return_if_fail(msginfo != NULL);
1049         g_return_if_fail(msginfo->folder != NULL);
1050
1051         account = msginfo->folder->folder->account;
1052         if (!account) account = cur_account;
1053         g_return_if_fail(account != NULL);
1054
1055         compose = compose_create(account);
1056         compose->mode = COMPOSE_REEDIT_DRAFT;
1057         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1058
1059         if (compose_parse_header(compose, msginfo) < 0) return;
1060         compose_reedit_set_entry(compose, msginfo);
1061
1062         text = GTK_STEXT(compose->text);
1063         gtk_stext_freeze(text);
1064
1065         if ((fp = procmime_get_text_part(msginfo)) == NULL)
1066                 g_warning(_("Can't get text part\n"));
1067         else {
1068                 while (fgets(buf, sizeof(buf), fp) != NULL)
1069                         gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1070                 fclose(fp);
1071         }
1072         compose_attach_parts(compose, msginfo);
1073
1074         gtk_stext_thaw(text);
1075         gtk_widget_grab_focus(compose->text);
1076 }
1077
1078 GList *compose_get_compose_list(void)
1079 {
1080         return compose_list;
1081 }
1082
1083 void compose_entry_append(Compose *compose, const gchar *address,
1084                           ComposeEntryType type)
1085 {
1086         GtkEntry *entry;
1087         const gchar *text;
1088
1089         if (!address || *address == '\0') return;
1090
1091         switch (type) {
1092         case COMPOSE_CC:
1093                 entry = GTK_ENTRY(compose->cc_entry);
1094                 break;
1095         case COMPOSE_BCC:
1096                 entry = GTK_ENTRY(compose->bcc_entry);
1097                 break;
1098         case COMPOSE_NEWSGROUPS:
1099                 entry = GTK_ENTRY(compose->newsgroups_entry);
1100                 break;
1101         case COMPOSE_TO:
1102         default:
1103                 entry = GTK_ENTRY(compose->to_entry);
1104                 break;
1105         }
1106
1107         text = gtk_entry_get_text(entry);
1108         if (*text != '\0')
1109                 gtk_entry_append_text(entry, ", ");
1110         gtk_entry_append_text(entry, address);
1111 }
1112
1113 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1114 {
1115         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
1116                                        {"Cc:",          NULL, FALSE},
1117                                        {"References:",  NULL, FALSE},
1118                                        {"Bcc:",         NULL, FALSE},
1119                                        {"Newsgroups:",  NULL, FALSE},
1120                                        {"Followup-To:", NULL, FALSE},
1121                                        {NULL,           NULL, FALSE}};
1122
1123         enum
1124         {
1125                 H_REPLY_TO      = 0,
1126                 H_CC            = 1,
1127                 H_REFERENCES    = 2,
1128                 H_BCC           = 3,
1129                 H_NEWSGROUPS    = 4,
1130                 H_FOLLOWUP_TO   = 5
1131         };
1132
1133         FILE *fp;
1134
1135         g_return_val_if_fail(msginfo != NULL, -1);
1136
1137         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1138         procheader_get_header_fields(fp, hentry);
1139         fclose(fp);
1140
1141         if (hentry[H_REPLY_TO].body != NULL) {
1142                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1143                 compose->replyto = hentry[H_REPLY_TO].body;
1144                 hentry[H_REPLY_TO].body = NULL;
1145         }
1146         if (hentry[H_CC].body != NULL) {
1147                 conv_unmime_header_overwrite(hentry[H_CC].body);
1148                 compose->cc = hentry[H_CC].body;
1149                 hentry[H_CC].body = NULL;
1150         }
1151         if (hentry[H_REFERENCES].body != NULL) {
1152                 if (compose->mode == COMPOSE_REEDIT_DRAFT)
1153                         compose->references = hentry[H_REFERENCES].body;
1154                 else {
1155                         compose->references = compose_parse_references
1156                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1157                         g_free(hentry[H_REFERENCES].body);
1158                 }
1159                 hentry[H_REFERENCES].body = NULL;
1160         }
1161         if (hentry[H_BCC].body != NULL) {
1162                 if (compose->mode == COMPOSE_REEDIT_DRAFT) {
1163                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1164                         compose->bcc = hentry[H_BCC].body;
1165                 } else
1166                         g_free(hentry[H_BCC].body);
1167                 hentry[H_BCC].body = NULL;
1168         }
1169         if (hentry[H_NEWSGROUPS].body != NULL) {
1170                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1171                 hentry[H_NEWSGROUPS].body = NULL;
1172         }
1173         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1174                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1175                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1176                 hentry[H_FOLLOWUP_TO].body = NULL;
1177         }
1178
1179         if (compose->mode == COMPOSE_REEDIT_DRAFT && msginfo->inreplyto)
1180                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1181         else if (compose->mode != COMPOSE_REEDIT_DRAFT &&
1182                  msginfo->msgid && *msginfo->msgid) {
1183                 compose->inreplyto = g_strdup(msginfo->msgid);
1184
1185                 if (!compose->references) {
1186                         if (msginfo->inreplyto && *msginfo->inreplyto)
1187                                 compose->references =
1188                                         g_strdup_printf("<%s>\n\t<%s>",
1189                                                         msginfo->inreplyto,
1190                                                         msginfo->msgid);
1191                         else
1192                                 compose->references =
1193                                         g_strconcat("<", msginfo->msgid, ">",
1194                                                     NULL);
1195                 }
1196         }
1197
1198         return 0;
1199 }
1200
1201 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1202 {
1203         GSList *ref_id_list, *cur;
1204         GString *new_ref;
1205         gchar *new_ref_str;
1206
1207         ref_id_list = references_list_append(NULL, ref);
1208         if (!ref_id_list) return NULL;
1209         if (msgid && *msgid)
1210                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1211
1212         for (;;) {
1213                 gint len = 0;
1214
1215                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1216                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1217                         len += strlen((gchar *)cur->data) + 5;
1218
1219                 if (len > MAX_REFERENCES_LEN) {
1220                         /* remove second message-ID */
1221                         if (ref_id_list && ref_id_list->next &&
1222                             ref_id_list->next->next) {
1223                                 g_free(ref_id_list->next->data);
1224                                 ref_id_list = g_slist_remove
1225                                         (ref_id_list, ref_id_list->next->data);
1226                         } else {
1227                                 slist_free_strings(ref_id_list);
1228                                 g_slist_free(ref_id_list);
1229                                 return NULL;
1230                         }
1231                 } else
1232                         break;
1233         }
1234
1235         new_ref = g_string_new("");
1236         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1237                 if (new_ref->len > 0)
1238                         g_string_append(new_ref, "\n\t");
1239                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1240         }
1241
1242         slist_free_strings(ref_id_list);
1243         g_slist_free(ref_id_list);
1244
1245         new_ref_str = new_ref->str;
1246         g_string_free(new_ref, FALSE);
1247
1248         return new_ref_str;
1249 }
1250
1251 /*
1252 static void compose_quote_file(Compose *compose, MsgInfo *msginfo, FILE *fp)
1253 {
1254         GtkSText *text = GTK_STEXT(compose->text);
1255         gchar *qmark;
1256         gchar *quote_str;
1257         GdkColor *qcolor = NULL;
1258         gchar buf[BUFFSIZE];
1259         gint qlen;
1260         gchar *linep, *cur, *leftp;
1261         gint line_len, cur_len;
1262         gint wrap_len;
1263         gint str_len;
1264         gint ch_len;
1265
1266         // if (prefs_common.enable_color) qcolor = &quote_color;
1267         if (prefs_common.quotemark && *prefs_common.quotemark)
1268                 qmark = prefs_common.quotemark;
1269         else
1270                 qmark = "> ";
1271         quote_str = compose_quote_parse_fmt(compose, msginfo, qmark);
1272         g_return_if_fail(quote_str != NULL);
1273         qlen = strlen(quote_str);
1274
1275         if (!prefs_common.linewrap_quote ||
1276             prefs_common.linewrap_len <= qlen) {
1277                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1278                         gtk_stext_insert(text, NULL, qcolor, NULL,
1279                                         quote_str, -1);
1280                         gtk_stext_insert(text, NULL, qcolor, NULL, buf, -1);
1281                 }
1282                 g_free(quote_str);
1283                 return;
1284         }
1285
1286         wrap_len = prefs_common.linewrap_len - qlen;
1287
1288         while (fgets(buf, sizeof(buf), fp) != NULL) {
1289                 strretchomp(buf);
1290                 str_len = strlen(buf);
1291
1292                 if (str_len <= wrap_len) {
1293                         gtk_stext_insert(text, NULL, qcolor, NULL,
1294                                         quote_str, -1);
1295                         gtk_stext_insert(text, NULL, qcolor, NULL, buf, -1);
1296                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1297                         continue;
1298                 }
1299
1300                 linep = cur = leftp = buf;
1301                 line_len = cur_len = 0;
1302
1303                 while (*cur != '\0') {
1304                         ch_len = mblen(cur, MB_CUR_MAX);
1305                         if (ch_len < 0) ch_len = 1;
1306
1307                         if (ch_len == 1 && isspace(*cur)) {
1308                                 linep = cur + ch_len;
1309                                 line_len = cur_len + ch_len;
1310                         }
1311
1312                         if (cur_len + ch_len > wrap_len && line_len > 0) {
1313                                 gtk_stext_insert(text, NULL, qcolor, NULL,
1314                                                 quote_str, -1);
1315
1316                                 if (isspace(*(linep - 1)))
1317                                         gtk_stext_insert(text, NULL,
1318                                                         qcolor, NULL,
1319                                                         leftp, line_len - 1);
1320                                 else
1321                                         gtk_stext_insert(text, NULL,
1322                                                         qcolor, NULL,
1323                                                         leftp, line_len);
1324                                 gtk_stext_insert(text, NULL, NULL, NULL,
1325                                                 "\n", 1);
1326
1327                                 leftp = linep;
1328                                 cur_len = cur_len - line_len + ch_len;
1329                                 line_len = 0;
1330                                 cur += ch_len;
1331                                 continue;
1332                         }
1333
1334                         if (ch_len > 1) {
1335                                 linep = cur + ch_len;
1336                                 line_len = cur_len + ch_len;
1337                         }
1338                         cur_len += ch_len;
1339                         cur += ch_len;
1340                 }
1341
1342                 if (*leftp) {
1343                         gtk_stext_insert(text, NULL, qcolor, NULL,
1344                                         quote_str, -1);
1345                         gtk_stext_insert(text, NULL, qcolor, NULL, leftp, -1);
1346                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1347                 }
1348         }
1349
1350         g_free(quote_str);
1351 }
1352 */
1353
1354 /*
1355 static gchar *compose_quote_parse_fmt(Compose *compose, MsgInfo *msginfo,
1356                                       const gchar *fmt)
1357 {
1358         gchar *ext_str;
1359         size_t buf_len = 1024;
1360         size_t ext_len = 0;
1361         gchar *str;
1362         gchar *mbs;
1363         wchar_t *wcsfmt;
1364         wchar_t *sp;
1365         gchar tmp[3];
1366
1367         if (!fmt || *fmt == '\0') return 0;
1368
1369         Xalloca(mbs, sizeof(wchar_t) + 1, return 0);
1370         Xalloca(wcsfmt, (strlen(fmt) + 1) * sizeof(wchar_t), return 0);
1371         mbstowcs(wcsfmt, fmt, strlen(fmt) + 1);
1372         sp = wcsfmt;
1373
1374         ext_str = g_malloc(sizeof(gchar) * buf_len);
1375         g_return_val_if_fail(ext_str != NULL, NULL);
1376
1377         while (*sp) {
1378                 gint len;
1379
1380                 len = wctomb(mbs, *sp);
1381                 mbs[len] = '\0';
1382
1383                 if (*mbs == '%') {
1384                         gchar *p;
1385
1386                         wctomb(mbs, *(++sp));
1387                         str = NULL;
1388
1389                         switch (*mbs) {
1390                         case 'd':
1391                                 str = msginfo->date;
1392                                 sp++;
1393                                 break;
1394                         case 'f':
1395                                 str = msginfo->from;
1396                                 sp++;
1397                                 break;
1398                         case 'I':
1399                                 if (!msginfo->fromname) {sp++; break;}
1400                                 p = msginfo->fromname;
1401                                 tmp[0] = tmp[1] = tmp[2] = '\0';
1402
1403                                 if (*p && isalnum(*p))
1404                                         tmp[0] = toupper(*p);
1405                                 else {
1406                                         sp++;
1407                                         break;
1408                                 }
1409
1410                                 while (*p) {
1411                                         while (*p && !isspace(*p)) p++;
1412                                         while (*p && isspace(*p)) p++;
1413                                         if (*p && isalnum(*p))
1414                                                 tmp[1] = toupper(*p);
1415                                 }
1416
1417                                 if (tmp[1]) str = tmp;
1418                                 sp++;
1419                                 break;
1420                         case 'n':
1421                                 str = msginfo->fromname;
1422                                 sp++;
1423                                 break;
1424                         case 'N':
1425                                 if (!msginfo->fromname) {sp++; break;}
1426                                 Xstrdup_a(str, msginfo->fromname,
1427                                           {sp++; break;});
1428                                 p = str;
1429                                 while (*p && !isspace(*p)) p++;
1430                                 *p = '\0';
1431                                 sp++;
1432                                 break;
1433                         case 's':
1434                                 str = msginfo->subject;
1435                                 sp++;
1436                                 break;
1437                         case 't':
1438                                 str = msginfo->to;
1439                                 sp++;
1440                                 break;
1441                         case 'c':
1442                                 str = msginfo->cc;
1443                                 sp++;
1444                                 break;
1445                         case 'i':
1446                                 if (!msginfo->msgid) {sp++; break;}
1447                                 Xalloca(str, strlen(msginfo->msgid) + 3,
1448                                         {sp++; break;});
1449                                 g_snprintf(str, strlen(msginfo->msgid) + 3,
1450                                            "<%s>", msginfo->msgid);
1451                                 sp++;
1452                                 break;
1453                         case '%':
1454                                 str = "%";
1455                                 sp++;
1456                                 break;
1457                         default:
1458                                 break;
1459                         }
1460
1461                         if (str) {
1462                                 while (ext_len + strlen(str) + 1 > buf_len)
1463                                         buf_len += 1024;
1464                                 ext_str = g_realloc(ext_str,
1465                                                     sizeof(gchar) * buf_len);
1466                                 g_return_val_if_fail(ext_str != NULL, NULL);
1467                                 strcpy(ext_str + ext_len, str);
1468                                 ext_len += strlen(str);
1469                         }
1470                 } else if (*mbs == '\\') {
1471                         wctomb(mbs, *(++sp));
1472                         str = NULL;
1473
1474                         switch (*mbs) {
1475                         case 'n':
1476                                 str = "\n";
1477                                 break;
1478                         case 't':
1479                                 str = "\t";
1480                                 break;
1481                         case '\\':
1482                                 str = "\\";
1483                                 break;
1484                         default:
1485                                 break;
1486                         }
1487
1488                         if (str) {
1489                                 while (ext_len + strlen(str) + 1 > buf_len)
1490                                         buf_len += 1024;
1491                                 ext_str = g_realloc(ext_str,
1492                                                     sizeof(gchar) * buf_len);
1493                                 g_return_val_if_fail(ext_str != NULL, NULL);
1494                                 strcpy(ext_str + ext_len, str);
1495                                 ext_len += strlen(str);
1496                                 sp++;
1497                         }
1498                 } else {
1499                         while (ext_len + len + 1 > buf_len) buf_len += 1024;
1500                         ext_str = g_realloc(ext_str, sizeof(gchar) * buf_len);
1501                         g_return_val_if_fail(ext_str != NULL, NULL);
1502                         strcpy(ext_str + ext_len, mbs);
1503                         ext_len += len;
1504                         sp++;
1505                 }
1506         }
1507
1508         if (ext_str)
1509                 ext_str = g_realloc(ext_str, strlen(ext_str) + 1);
1510
1511         return ext_str;
1512 }
1513 */
1514
1515 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1516                                     gboolean to_all, gboolean ignore_replyto,
1517                                     gboolean followup_and_reply_to)
1518 {
1519         GSList *cc_list;
1520         GSList *cur;
1521         gchar *from;
1522         GHashTable *to_table;
1523
1524         g_return_if_fail(compose->account != NULL);
1525         g_return_if_fail(msginfo != NULL);
1526
1527         if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1528                 gtk_entry_set_text(GTK_ENTRY(compose->to_entry),
1529                                    ( (compose->replyto && !ignore_replyto) 
1530                                      ? compose->replyto
1531                                      : msginfo->from ? msginfo->from : ""));
1532         if (compose->account->protocol == A_NNTP)
1533                 gtk_entry_set_text(GTK_ENTRY(compose->newsgroups_entry),
1534                                    compose->followup_to ? compose->followup_to
1535                                    : compose->newsgroups ? compose->newsgroups
1536                                    : "");
1537
1538         if (msginfo->subject && *msginfo->subject) {
1539                 gchar *buf, *buf2, *p;
1540
1541                 buf = g_strdup(msginfo->subject);
1542                 while (!strncasecmp(buf, "Re:", 3)) {
1543                         p = buf + 3;
1544                         while (isspace(*p)) p++;
1545                         memmove(buf, p, strlen(p) + 1);
1546                 }
1547
1548                 buf2 = g_strdup_printf("Re: %s", buf);
1549                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1550                 g_free(buf2);
1551                 g_free(buf);
1552         } else
1553                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1554
1555         if (!to_all || compose->account->protocol == A_NNTP) return;
1556
1557         from = g_strdup(compose->replyto ? compose->replyto :
1558                         msginfo->from ? msginfo->from : "");
1559         extract_address(from);
1560
1561         cc_list = address_list_append(NULL, msginfo->to);
1562         cc_list = address_list_append(cc_list, compose->cc);
1563
1564         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1565         g_hash_table_insert(to_table, from, GINT_TO_POINTER(1));
1566         if (compose->account)
1567                 g_hash_table_insert(to_table, compose->account->address,
1568                                     GINT_TO_POINTER(1));
1569
1570         /* remove address on To: and that of current account */
1571         for (cur = cc_list; cur != NULL; ) {
1572                 GSList *next = cur->next;
1573
1574                 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1575                         cc_list = g_slist_remove(cc_list, cur->data);
1576                 else
1577                         g_hash_table_insert(to_table, cur->data, cur);
1578
1579                 cur = next;
1580         }
1581         g_hash_table_destroy(to_table);
1582         g_free(from);
1583
1584         if (cc_list) {
1585                 for (cur = cc_list; cur != NULL; cur = cur->next)
1586                         compose_entry_append(compose, (gchar *)cur->data,
1587                                              COMPOSE_CC);
1588                 slist_free_strings(cc_list);
1589                 g_slist_free(cc_list);
1590         }
1591 }
1592
1593 #define SET_ENTRY(entry, str) \
1594 { \
1595         if (str && *str) \
1596                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1597 }
1598
1599 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1600 {
1601         g_return_if_fail(msginfo != NULL);
1602
1603         SET_ENTRY(to_entry, msginfo->to);
1604         SET_ENTRY(subject_entry, msginfo->subject);
1605         SET_ENTRY(cc_entry, compose->cc);
1606         SET_ENTRY(bcc_entry, compose->bcc);
1607         SET_ENTRY(reply_entry, compose->replyto);
1608
1609         if (compose->bcc) {
1610                 GtkItemFactory *ifactory;
1611                 GtkWidget *menuitem;
1612
1613                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1614                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
1615                 gtk_check_menu_item_set_active
1616                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1617         }
1618         if (compose->replyto) {
1619                 GtkItemFactory *ifactory;
1620                 GtkWidget *menuitem;
1621
1622                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1623                 menuitem = gtk_item_factory_get_item
1624                         (ifactory, "/Message/Reply to");
1625                 gtk_check_menu_item_set_active
1626                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1627         }
1628 }
1629
1630 #undef SET_ENTRY
1631
1632 static void compose_exec_sig(Compose *compose, gchar *sigfile)
1633 {
1634         FILE *tmpfp;
1635         pid_t thepid;
1636         gchar *sigtext;
1637         FILE  *sigprg;
1638         gchar  *buf;
1639         size_t buf_len = 128;
1640  
1641         if (strlen(sigfile) < 2)
1642           return;
1643  
1644         sigprg = popen(sigfile+1, "r");
1645         if (sigprg) {
1646
1647                 buf = g_malloc(buf_len);
1648
1649                 if (!buf) {
1650                         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1651                         "Unable to insert signature (malloc failed)\n", -1);
1652
1653                         pclose(sigprg);
1654                         return;
1655                 }
1656
1657                 while (!feof(sigprg)) {
1658                         bzero(buf, buf_len);
1659                         fread(buf, buf_len-1, 1, sigprg);
1660                         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
1661                 }
1662
1663                 g_free(buf);
1664                 pclose(sigprg);
1665         }
1666         else
1667         {
1668                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1669                 "Can't exec file: ", -1);
1670                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1671                 sigfile+1, -1);
1672         }
1673 }
1674
1675 static void compose_insert_sig(Compose *compose)
1676 {
1677         gchar *sigfile;
1678
1679         if (compose->account && compose->account->sig_path)
1680                 sigfile = g_strdup(compose->account->sig_path);
1681         else {
1682                 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1683                                       DEFAULT_SIGNATURE, NULL);
1684         }
1685
1686         if (!is_file_or_fifo_exist(sigfile) & (sigfile[0] != '|')) {
1687                 g_free(sigfile);
1688                 return;
1689         }
1690
1691         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1692         if (prefs_common.sig_sep) {
1693                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1694                                 prefs_common.sig_sep, -1);
1695                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1696                                 "\n", 1);
1697         }
1698
1699         if (sigfile[0] == '|')
1700         {
1701                 compose_exec_sig(compose, sigfile);
1702         }
1703         else
1704         {
1705                 compose_insert_file(compose, sigfile);
1706         }
1707         g_free(sigfile);
1708 }
1709
1710 static void compose_insert_file(Compose *compose, const gchar *file)
1711 {
1712         GtkSText *text = GTK_STEXT(compose->text);
1713         gchar buf[BUFFSIZE];
1714         FILE *fp;
1715
1716         g_return_if_fail(file != NULL);
1717
1718         if ((fp = fopen(file, "r")) == NULL) {
1719                 FILE_OP_ERROR(file, "fopen");
1720                 return;
1721         }
1722
1723         gtk_stext_freeze(text);
1724
1725         while (fgets(buf, sizeof(buf), fp) != NULL)
1726                 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1727
1728         gtk_stext_thaw(text);
1729
1730         fclose(fp);
1731 }
1732
1733 static void compose_attach_info(Compose * compose, AttachInfo * ainfo,
1734                                 ContentType cnttype)
1735 {
1736         gchar *text[N_ATTACH_COLS];
1737         gint row;
1738
1739         text[COL_MIMETYPE] = ainfo->content_type;
1740         text[COL_SIZE] = to_human_readable(ainfo->size);
1741         text[COL_NAME] = ainfo->name;
1742
1743         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1744         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1745
1746         if (cnttype != MIME_MESSAGE_RFC822)
1747                 compose_changed_cb(NULL, compose);
1748 }
1749
1750 static void compose_attach_append_with_type(Compose *compose,
1751                                             const gchar *file,
1752                                             const gchar *type,
1753                                             ContentType cnttype)
1754 {
1755         AttachInfo *ainfo;
1756         off_t size;
1757
1758         if (!is_file_exist(file)) {
1759                 g_warning(_("File %s doesn't exist\n"), file);
1760                 return;
1761         }
1762         if ((size = get_file_size(file)) < 0) {
1763                 g_warning(_("Can't get file size of %s\n"), file);
1764                 return;
1765         }
1766         if (size == 0) {
1767                 alertpanel_notice(_("File %s is empty\n"), file);
1768                 return;
1769         }
1770
1771         if (!compose->use_attach) {
1772                 GtkItemFactory *ifactory;
1773                 GtkWidget *menuitem;
1774
1775                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1776                 menuitem = gtk_item_factory_get_item(ifactory,
1777                                                      "/Message/Attach");
1778                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1779                                                TRUE);
1780         }
1781
1782         ainfo = g_new0(AttachInfo, 1);
1783         ainfo->file = g_strdup(file);
1784
1785         if (cnttype == MIME_MESSAGE_RFC822) {
1786                 ainfo->encoding = ENC_7BIT;
1787                 ainfo->name = g_strdup_printf(_("Message: %s"),
1788                                               g_basename(file));
1789         } else {
1790                 ainfo->encoding = ENC_BASE64;
1791                 ainfo->name = g_strdup(g_basename(file));
1792         }
1793
1794         ainfo->content_type = g_strdup(type);
1795         ainfo->size = size;
1796
1797         compose_attach_info(compose, ainfo, cnttype);
1798 }
1799
1800 static void compose_attach_append(Compose *compose, const gchar *file,
1801                                   ContentType cnttype)
1802 {
1803         AttachInfo *ainfo;
1804         gchar *text[N_ATTACH_COLS];
1805         off_t size;
1806         gint row;
1807
1808         if (!is_file_exist(file)) {
1809                 g_warning(_("File %s doesn't exist\n"), file);
1810                 return;
1811         }
1812         if ((size = get_file_size(file)) < 0) {
1813                 g_warning(_("Can't get file size of %s\n"), file);
1814                 return;
1815         }
1816         if (size == 0) {
1817                 alertpanel_notice(_("File %s is empty\n"), file);
1818                 return;
1819         }
1820
1821         if (!compose->use_attach) {
1822                 GtkItemFactory *ifactory;
1823                 GtkWidget *menuitem;
1824
1825                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1826                 menuitem = gtk_item_factory_get_item(ifactory,
1827                                                      "/Message/Attach");
1828                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1829                                                TRUE);
1830         }
1831
1832         ainfo = g_new0(AttachInfo, 1);
1833         ainfo->file = g_strdup(file);
1834
1835         if (cnttype == MIME_MESSAGE_RFC822) {
1836                 ainfo->content_type = g_strdup("message/rfc822");
1837                 ainfo->encoding = ENC_7BIT;
1838                 ainfo->name = g_strdup_printf(_("Message: %s"),
1839                                               g_basename(file));
1840         } else {
1841                 ainfo->content_type = procmime_get_mime_type(file);
1842                 if (!ainfo->content_type)
1843                         ainfo->content_type =
1844                                 g_strdup("application/octet-stream");
1845                 ainfo->encoding = ENC_BASE64;
1846                 ainfo->name = g_strdup(g_basename(file));
1847         }
1848         ainfo->size = size;
1849
1850         compose_attach_info(compose, ainfo, cnttype);
1851 }
1852
1853 static void compose_wrap_line(Compose *compose)
1854 {
1855         GtkSText *text = GTK_STEXT(compose->text);
1856         guint text_len;
1857         guint line_pos = 0, cur_pos = 0;
1858         gint line_len = 0, cur_len = 0;
1859         gint ch_len;
1860         gchar cbuf[MB_CUR_MAX];
1861
1862         gtk_stext_freeze(text);
1863
1864         text_len = gtk_stext_get_length(text);
1865
1866         for (; cur_pos < text_len; cur_pos++) {
1867                 if (text->use_wchar)
1868                         ch_len = wctomb
1869                                 (cbuf, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
1870                 else {
1871                         cbuf[0] = GTK_STEXT_INDEX(text, cur_pos);
1872                         ch_len = 1;
1873                 }
1874
1875                 if (ch_len == 1 && *cbuf == '\n') {
1876                         line_pos = cur_pos + 1;
1877                         line_len = cur_len = 0;
1878                         continue;
1879                 }
1880
1881                 if (ch_len < 0) {
1882                         cbuf[0] = '\0';
1883                         ch_len = 1;
1884                 }
1885
1886                 if (ch_len == 1 && isspace(*cbuf)) {
1887                         line_pos = cur_pos + 1;
1888                         line_len = cur_len + ch_len;
1889                 }
1890
1891                 if (cur_len + ch_len > prefs_common.linewrap_len &&
1892                     line_len > 0) {
1893                         gint tlen;
1894
1895                         if (text->use_wchar)
1896                                 tlen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
1897                         else {
1898                                 cbuf[0] = GTK_STEXT_INDEX(text, line_pos - 1);
1899                                 tlen = 1;
1900                         }
1901                         if (tlen == 1 && isspace(*cbuf)) {
1902                                 gtk_stext_set_point(text, line_pos);
1903                                 gtk_stext_backward_delete(text, 1);
1904                                 text_len--;
1905                                 cur_pos--;
1906                                 line_pos--;
1907                                 cur_len--;
1908                                 line_len--;
1909                         }
1910
1911                         gtk_stext_set_point(text, line_pos);
1912                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1913                         text_len++;
1914                         cur_pos++;
1915                         line_pos++;
1916                         cur_len = cur_len - line_len + ch_len;
1917                         line_len = 0;
1918                         continue;
1919                 }
1920
1921                 if (ch_len > 1) {
1922                         line_pos = cur_pos + 1;
1923                         line_len = cur_len + ch_len;
1924                 }
1925                 cur_len += ch_len;
1926         }
1927
1928         gtk_stext_thaw(text);
1929 }
1930
1931 static void compose_set_title(Compose *compose)
1932 {
1933         gchar *str;
1934         gchar *edited;
1935
1936         edited = compose->modified ? _(" [Edited]") : "";
1937         if (compose->account && compose->account->address)
1938                 str = g_strdup_printf(_("%s - Compose message%s"),
1939                                       compose->account->address, edited);
1940         else
1941                 str = g_strdup_printf(_("Compose message%s"), edited);
1942         gtk_window_set_title(GTK_WINDOW(compose->window), str);
1943         g_free(str);
1944 }
1945
1946 /**
1947  * compose_current_mail_account:
1948  * 
1949  * Find a current mail account (the currently selected account, or the
1950  * default account, if a news account is currently selected).  If a
1951  * mail account cannot be found, display an error message.
1952  * 
1953  * Return value: Mail account, or NULL if not found.
1954  **/
1955 static PrefsAccount *
1956 compose_current_mail_account(void)
1957 {
1958         PrefsAccount *ac;
1959
1960         if (cur_account && cur_account->protocol != A_NNTP)
1961                 ac = cur_account;
1962         else {
1963                 ac = account_get_default();
1964                 if (!ac || ac->protocol == A_NNTP) {
1965                         alertpanel_error(_("Account for sending mail is not specified.\n"
1966                                            "Please select a mail account before sending."));
1967                         return NULL;
1968                 }
1969         }
1970         return ac;
1971 }
1972
1973 gint compose_send(Compose *compose)
1974 {
1975         gchar tmp[MAXPATHLEN + 1];
1976         gchar *to, *newsgroups;
1977         gint ok = 0;
1978         static gboolean lock = FALSE;
1979
1980         if (lock) return 1;
1981
1982         g_return_val_if_fail(compose->account != NULL, -1);
1983         g_return_val_if_fail(compose->orig_account != NULL, -1);
1984
1985         lock = TRUE;
1986
1987         to = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
1988         newsgroups = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
1989         if (*to == '\0' && *newsgroups == '\0') {
1990                 alertpanel_error(_("Recipient is not specified."));
1991                 lock = FALSE;
1992                 return 1;
1993         }
1994
1995         /* write to temporary file */
1996         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
1997                    get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
1998
1999         if (prefs_common.linewrap_at_send)
2000                 compose_wrap_line(compose);
2001
2002         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
2003                 lock = FALSE;
2004                 return -1;
2005         }
2006
2007         if (!compose->to_list && !compose->newsgroup_list) {
2008                 g_warning(_("can't get recipient list."));
2009                 unlink(tmp);
2010                 lock = FALSE;
2011                 return -1;
2012         }
2013
2014         if (compose->to_list) {
2015                 PrefsAccount *ac;
2016
2017                 /*
2018                 if (compose->account->protocol != A_NNTP)
2019                         ac = compose->account;
2020                 else if (compose->orig_account->protocol != A_NNTP)
2021                         ac = compose->orig_account;
2022                 else if (cur_account && cur_account->protocol != A_NNTP)
2023                         ac = cur_account;
2024                 else {
2025                         ac = compose_current_mail_account();
2026                         if (!ac) {
2027                                 unlink(tmp);
2028                                 lock = FALSE;
2029                                 return -1;
2030                         }
2031                 }
2032                 */
2033                 ac = compose->account;
2034
2035                 ok = send_message(tmp, ac, compose->to_list);
2036                 statusbar_pop_all();
2037         }
2038
2039         if (ok == 0 && compose->newsgroup_list) {
2040                 Folder *folder;
2041
2042                 if (compose->account->protocol == A_NNTP)
2043                         folder = FOLDER(compose->account->folder);
2044                 else
2045                         folder = FOLDER(compose->orig_account->folder);
2046
2047                 ok = news_post(folder, tmp);
2048                 if (ok < 0) {
2049                         alertpanel_error(_("Error occurred while posting the message to %s ."),
2050                                          compose->account->nntp_server);
2051                         unlink(tmp);
2052                         lock = FALSE;
2053                         return -1;
2054                 }
2055         }
2056
2057         /* queue message if failed to send */
2058         if (ok < 0) {
2059                 if (prefs_common.queue_msg) {
2060                         AlertValue val;
2061
2062                         val = alertpanel
2063                                 (_("Queueing"),
2064                                  _("Error occurred while sending the message.\n"
2065                                    "Put this message into queue folder?"),
2066                                  _("OK"), _("Cancel"), NULL);
2067                         if (G_ALERTDEFAULT == val) {
2068                                 ok = compose_queue(compose, tmp);
2069                                 if (ok < 0)
2070                                         alertpanel_error(_("Can't queue the message."));
2071                         }
2072                 } else
2073                         alertpanel_error(_("Error occurred while sending the message."));
2074         }
2075
2076         /* save message to outbox */
2077         if (ok == 0 && prefs_common.savemsg) {
2078                 if (compose_save_to_outbox(compose, tmp) < 0)
2079                         alertpanel_error
2080                                 (_("Can't save the message to outbox."));
2081         }
2082
2083         unlink(tmp);
2084         lock = FALSE;
2085         return ok;
2086 }
2087
2088 static gint compose_write_to_file(Compose *compose, const gchar *file,
2089                                   gboolean is_draft)
2090 {
2091         FILE *fp;
2092         size_t len;
2093         gchar *chars;
2094         gchar *buf;
2095         const gchar *out_codeset;
2096         EncodingType encoding;
2097
2098         if ((fp = fopen(file, "w")) == NULL) {
2099                 FILE_OP_ERROR(file, "fopen");
2100                 return -1;
2101         }
2102
2103         /* chmod for security */
2104         if (change_file_mode_rw(fp, file) < 0) {
2105                 FILE_OP_ERROR(file, "chmod");
2106                 g_warning(_("can't change file mode\n"));
2107         }
2108
2109         /* get all composed text */
2110         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2111         len = strlen(chars);
2112         if (is_ascii_str(chars)) {
2113                 buf = g_strdup(chars);
2114                 out_codeset = "US-ASCII";
2115                 encoding = ENC_7BIT;
2116         } else {
2117                 const gchar *src_codeset;
2118
2119                 out_codeset = conv_get_outgoing_charset_str();
2120                 if (!strcasecmp(out_codeset, "US-ASCII"))
2121                         out_codeset = "ISO-8859-1";
2122                 encoding = procmime_get_encoding_for_charset(out_codeset);
2123                 debug_print("charset = %s, encoding = %s\n",
2124                             out_codeset, procmime_get_encoding_str(encoding));
2125
2126                 src_codeset = conv_get_current_charset_str();
2127                 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
2128                 if (!buf) {
2129                         g_free(chars);
2130                         fclose(fp);
2131                         unlink(file);
2132                         alertpanel_error(_("Can't convert the codeset of the message."));
2133                         return -1;
2134                 }
2135         }
2136         g_free(chars);
2137
2138         /* write headers */
2139         if (compose_write_headers
2140                 (compose, fp, out_codeset, encoding, is_draft) < 0) {
2141                 g_warning(_("can't write headers\n"));
2142                 fclose(fp);
2143                 /* unlink(file); */
2144                 g_free(buf);
2145                 return -1;
2146         }
2147
2148         if (compose->use_attach) {
2149 #if USE_GPGME
2150             /* This prolog message is ignored by mime software and
2151              * because it would make our signing/encryption task
2152              * tougher, we don't emit it in that case */
2153             if (!compose->use_signing && !compose->use_encryption)
2154 #endif
2155                 fputs("This is a multi-part message in MIME format.\n", fp);
2156
2157                 fprintf(fp, "\n--%s\n", compose->boundary);
2158                 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
2159                         out_codeset);
2160                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2161                         procmime_get_encoding_str(encoding));
2162                 fputc('\n', fp);
2163         }
2164
2165         /* write body */
2166         len = strlen(buf);
2167         if (encoding == ENC_BASE64) {
2168                 gchar outbuf[B64_BUFFSIZE];
2169                 gint i, l;
2170
2171                 for (i = 0; i < len; i += B64_LINE_SIZE) {
2172                         l = MIN(B64_LINE_SIZE, len - i);
2173                         to64frombits(outbuf, buf + i, l);
2174                         fputs(outbuf, fp);
2175                         fputc('\n', fp);
2176                 }
2177         } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
2178                 FILE_OP_ERROR(file, "fwrite");
2179                 fclose(fp);
2180                 unlink(file);
2181                 g_free(buf);
2182                 return -1;
2183         }
2184         g_free(buf);
2185
2186         if (compose->use_attach)
2187                 compose_write_attach(compose, fp);
2188
2189         if (fclose(fp) == EOF) {
2190                 FILE_OP_ERROR(file, "fclose");
2191                 unlink(file);
2192                 return -1;
2193         }
2194
2195 #if USE_GPGME
2196         if (compose->use_signing) {
2197                 if (rfc2015_sign(file, compose->account) < 0) {
2198                         unlink(file);
2199                         return -1;
2200                 }
2201         }
2202         if (compose->use_encryption) {
2203                 if (rfc2015_encrypt(file, compose->to_list) < 0) {
2204                         unlink(file);
2205                         return -1;
2206                 }
2207         }
2208 #endif /* USE_GPGME */
2209
2210         return 0;
2211 }
2212
2213 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
2214 {
2215         FILE *fp;
2216         size_t len;
2217         gchar *chars;
2218
2219         if ((fp = fopen(file, "w")) == NULL) {
2220                 FILE_OP_ERROR(file, "fopen");
2221                 return -1;
2222         }
2223
2224         /* chmod for security */
2225         if (change_file_mode_rw(fp, file) < 0) {
2226                 FILE_OP_ERROR(file, "chmod");
2227                 g_warning(_("can't change file mode\n"));
2228         }
2229
2230         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2231
2232         /* write body */
2233         len = strlen(chars);
2234         if (fwrite(chars, sizeof(gchar), len, fp) != len) {
2235                 FILE_OP_ERROR(file, "fwrite");
2236                 g_free(chars);
2237                 fclose(fp);
2238                 unlink(file);
2239                 return -1;
2240         }
2241
2242         g_free(chars);
2243
2244         if (fclose(fp) == EOF) {
2245                 FILE_OP_ERROR(file, "fclose");
2246                 unlink(file);
2247                 return -1;
2248         }
2249         return 0;
2250 }
2251
2252 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
2253 {
2254         FolderItem *outbox;
2255         gchar *path;
2256         gint num;
2257         FILE *fp;
2258
2259         debug_print(_("saving sent message...\n"));
2260
2261         outbox = folder_get_default_outbox();
2262         path = folder_item_get_path(outbox);
2263         if (!is_dir_exist(path))
2264                 make_dir_hier(path);
2265
2266         folder_item_scan(outbox);
2267         if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
2268                 g_free(path);
2269                 g_warning(_("can't save message\n"));
2270                 return -1;
2271         }
2272
2273         if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
2274                 g_warning(_("can't open mark file\n"));
2275         else {
2276                 MsgInfo newmsginfo;
2277
2278                 newmsginfo.msgnum = num;
2279                 newmsginfo.flags.perm_flags = 0;
2280                 newmsginfo.flags.tmp_flags = 0;
2281                 procmsg_write_flags(&newmsginfo, fp);
2282                 fclose(fp);
2283         }
2284         g_free(path);
2285
2286         return 0;
2287 }
2288
2289 static gint compose_queue(Compose *compose, const gchar *file)
2290 {
2291         FolderItem *queue;
2292         gchar *tmp, *queue_path;
2293         FILE *fp, *src_fp;
2294         GSList *cur;
2295         gchar buf[BUFFSIZE];
2296         gint num;
2297
2298         debug_print(_("queueing message...\n"));
2299         g_return_val_if_fail(compose->to_list != NULL, -1);
2300         g_return_val_if_fail(compose->account != NULL, -1);
2301
2302         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
2303                               G_DIR_SEPARATOR, (gint)compose);
2304         if ((fp = fopen(tmp, "w")) == NULL) {
2305                 FILE_OP_ERROR(tmp, "fopen");
2306                 g_free(tmp);
2307                 return -1;
2308         }
2309         if ((src_fp = fopen(file, "r")) == NULL) {
2310                 FILE_OP_ERROR(file, "fopen");
2311                 fclose(fp);
2312                 unlink(tmp);
2313                 g_free(tmp);
2314                 return -1;
2315         }
2316         if (change_file_mode_rw(fp, tmp) < 0) {
2317                 FILE_OP_ERROR(tmp, "chmod");
2318                 g_warning(_("can't change file mode\n"));
2319         }
2320
2321         /* queueing variables */
2322         fprintf(fp, "AF:\n");
2323         fprintf(fp, "NF:0\n");
2324         fprintf(fp, "PS:10\n");
2325         fprintf(fp, "SRH:1\n");
2326         fprintf(fp, "SFN:\n");
2327         fprintf(fp, "DSR:\n");
2328         if (compose->msgid)
2329                 fprintf(fp, "MID:<%s>\n", compose->msgid);
2330         else
2331                 fprintf(fp, "MID:\n");
2332         fprintf(fp, "CFG:\n");
2333         fprintf(fp, "PT:0\n");
2334         fprintf(fp, "S:%s\n", compose->account->address);
2335         fprintf(fp, "RQ:\n");
2336         if (compose->account->smtp_server)
2337                 fprintf(fp, "SSV:%s\n", compose->account->smtp_server);
2338         else
2339                 fprintf(fp, "SSV:\n");
2340         if (compose->account->nntp_server)
2341                 fprintf(fp, "NSV:%s\n", compose->account->nntp_server);
2342         else
2343                 fprintf(fp, "NSV:\n");
2344         fprintf(fp, "SSH:\n");
2345         fprintf(fp, "R:<%s>", (gchar *)compose->to_list->data);
2346         for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
2347                 fprintf(fp, ",<%s>", (gchar *)cur->data);
2348         fprintf(fp, "\n");
2349         fprintf(fp, "\n");
2350
2351         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
2352                 if (fputs(buf, fp) == EOF) {
2353                         FILE_OP_ERROR(tmp, "fputs");
2354                         fclose(fp);
2355                         fclose(src_fp);
2356                         unlink(tmp);
2357                         g_free(tmp);
2358                         return -1;
2359                 }
2360         }
2361
2362         fclose(src_fp);
2363         if (fclose(fp) == EOF) {
2364                 FILE_OP_ERROR(tmp, "fclose");
2365                 unlink(tmp);
2366                 g_free(tmp);
2367                 return -1;
2368         }
2369
2370         queue = folder_get_default_queue();
2371         folder_item_scan(queue);
2372         queue_path = folder_item_get_path(queue);
2373         if (!is_dir_exist(queue_path))
2374                 make_dir_hier(queue_path);
2375         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
2376                 g_warning(_("can't queue the message\n"));
2377                 unlink(tmp);
2378                 g_free(tmp);
2379                 g_free(queue_path);
2380                 return -1;
2381         }
2382         g_free(tmp);
2383
2384         if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
2385                 g_warning(_("can't open mark file\n"));
2386         else {
2387                 MsgInfo newmsginfo;
2388
2389                 newmsginfo.msgnum = num;
2390                 newmsginfo.flags.perm_flags = 0;
2391                 newmsginfo.flags.tmp_flags = 0;
2392                 procmsg_write_flags(&newmsginfo, fp);
2393                 fclose(fp);
2394         }
2395         g_free(queue_path);
2396
2397         folder_item_scan(queue);
2398         folderview_update_item(queue, TRUE);
2399
2400         return 0;
2401 }
2402
2403 static void compose_write_attach(Compose *compose, FILE *fp)
2404 {
2405         AttachInfo *ainfo;
2406         GtkCList *clist = GTK_CLIST(compose->attach_clist);
2407         gint row;
2408         FILE *attach_fp;
2409         gchar filename[BUFFSIZE];
2410         gint len;
2411
2412         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
2413              row++) {
2414                 if ((attach_fp = fopen(ainfo->file, "r")) == NULL) {
2415                         g_warning(_("Can't open file %s\n"), ainfo->file);
2416                         continue;
2417                 }
2418
2419                 fprintf(fp, "\n--%s\n", compose->boundary);
2420
2421                 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
2422                         fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
2423                         fprintf(fp, "Content-Disposition: inline\n");
2424                 } else {
2425                         conv_encode_header(filename, sizeof(filename),
2426                                            ainfo->name, 12);
2427                         fprintf(fp, "Content-Type: %s;\n"
2428                                     " name=\"%s\"\n",
2429                                 ainfo->content_type, filename);
2430                         fprintf(fp, "Content-Disposition: attachment;\n"
2431                                     " filename=\"%s\"\n", filename);
2432                 }
2433
2434                 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
2435                         procmime_get_encoding_str(ainfo->encoding));
2436
2437                 if (ainfo->encoding == ENC_7BIT) {
2438                         gchar buf[BUFFSIZE];
2439
2440                         while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
2441                                 len = strlen(buf);
2442                                 if (len > 1 && buf[len - 1] == '\n' &&
2443                                     buf[len - 2] == '\r') {
2444                                         buf[len - 2] = '\n';
2445                                         buf[len - 1] = '\0';
2446                                 }
2447                                 fputs(buf, fp);
2448                         }
2449                 } else {
2450                         gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
2451
2452                         while ((len = fread(inbuf, sizeof(gchar),
2453                                             B64_LINE_SIZE, attach_fp))
2454                                == B64_LINE_SIZE) {
2455                                 to64frombits(outbuf, inbuf, B64_LINE_SIZE);
2456                                 fputs(outbuf, fp);
2457                                 fputc('\n', fp);
2458                         }
2459                         if (len > 0 && feof(attach_fp)) {
2460                                 to64frombits(outbuf, inbuf, len);
2461                                 fputs(outbuf, fp);
2462                                 fputc('\n', fp);
2463                         }
2464                 }
2465
2466                 fclose(attach_fp);
2467         }
2468
2469         fprintf(fp, "\n--%s--\n", compose->boundary);
2470 }
2471
2472 #define IS_IN_CUSTOM_HEADER(header) \
2473         (compose->account->add_customhdr && \
2474          custom_header_find(compose->account->customhdr_list, header) != NULL)
2475
2476 static gint compose_write_headers(Compose *compose, FILE *fp,
2477                                   const gchar *charset, EncodingType encoding,
2478                                   gboolean is_draft)
2479 {
2480         gchar buf[BUFFSIZE];
2481         gchar *str;
2482         /* struct utsname utsbuf; */
2483
2484         g_return_val_if_fail(fp != NULL, -1);
2485         g_return_val_if_fail(charset != NULL, -1);
2486         g_return_val_if_fail(compose->account != NULL, -1);
2487         g_return_val_if_fail(compose->account->address != NULL, -1);
2488
2489         /* Date */
2490         if (compose->account->add_date) {
2491                 get_rfc822_date(buf, sizeof(buf));
2492                 fprintf(fp, "Date: %s\n", buf);
2493         }
2494
2495         /* From */
2496         if (!IS_IN_CUSTOM_HEADER("From")) {
2497                 if (compose->account->name && *compose->account->name) {
2498                         compose_convert_header
2499                                 (buf, sizeof(buf), compose->account->name,
2500                                  strlen("From: "));
2501                         fprintf(fp, "From: %s <%s>\n",
2502                                 buf, compose->account->address);
2503                 } else
2504                         fprintf(fp, "From: %s\n", compose->account->address);
2505         }
2506         
2507         slist_free_strings(compose->to_list);
2508         g_slist_free(compose->to_list);
2509         compose->to_list = NULL;
2510
2511         /* To */
2512         if (compose->use_to) {
2513                 str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
2514                 if (*str != '\0') {
2515                         Xstrdup_a(str, str, return -1);
2516                         g_strstrip(str);
2517                         if (*str != '\0') {
2518                                 compose->to_list = address_list_append
2519                                         (compose->to_list, str);
2520                                 if (!IS_IN_CUSTOM_HEADER("To")) {
2521                                         compose_convert_header
2522                                                 (buf, sizeof(buf), str,
2523                                                  strlen("To: "));
2524                                         fprintf(fp, "To: %s\n", buf);
2525                                 }
2526                         }
2527                 }
2528         }
2529
2530         slist_free_strings(compose->newsgroup_list);
2531         g_slist_free(compose->newsgroup_list);
2532         compose->newsgroup_list = NULL;
2533
2534         /* Newsgroups */
2535         str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
2536         if (*str != '\0') {
2537                 Xstrdup_a(str, str, return -1);
2538                 g_strstrip(str);
2539                 remove_space(str);
2540                 if (*str != '\0') {
2541                         compose->newsgroup_list =
2542                                 newsgroup_list_append(compose->newsgroup_list,
2543                                                       str);
2544                         if (!IS_IN_CUSTOM_HEADER("Newsgroups")) {
2545                                 compose_convert_header(buf, sizeof(buf), str,
2546                                                        strlen("Newsgroups: "));
2547                                 fprintf(fp, "Newsgroups: %s\n", buf);
2548                         }
2549                 }
2550         }
2551
2552         if (!is_draft && !compose->to_list && !compose->newsgroup_list)
2553                 return -1;
2554
2555         /* Cc */
2556         if (compose->use_cc) {
2557                 str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
2558                 if (*str != '\0') {
2559                         Xstrdup_a(str, str, return -1);
2560                         g_strstrip(str);
2561                         if (*str != '\0') {
2562                                 compose->to_list = address_list_append
2563                                         (compose->to_list, str);
2564                                 if (!IS_IN_CUSTOM_HEADER("Cc")) {
2565                                         compose_convert_header
2566                                                 (buf, sizeof(buf), str,
2567                                                  strlen("Cc: "));
2568                                         fprintf(fp, "Cc: %s\n", buf);
2569                                 }
2570                         }
2571                 }
2572         }
2573
2574         /* Bcc */
2575         if (compose->use_bcc) {
2576                 str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
2577                 if (*str != '\0') {
2578                         Xstrdup_a(str, str, return -1);
2579                         g_strstrip(str);
2580                         if (*str != '\0') {
2581                                 compose->to_list = address_list_append
2582                                         (compose->to_list, str);
2583                                 if (is_draft) {
2584                                         compose_convert_header
2585                                                 (buf, sizeof(buf), str,
2586                                                  strlen("Bcc: "));
2587                                         fprintf(fp, "Bcc: %s\n", buf);
2588                                 }
2589                         }
2590                 }
2591         }
2592
2593         /* Subject */
2594         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
2595         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("Subject")) {
2596                 Xstrdup_a(str, str, return -1);
2597                 g_strstrip(str);
2598                 if (*str != '\0') {
2599                         compose_convert_header(buf, sizeof(buf), str,
2600                                                strlen("Subject: "));
2601                         fprintf(fp, "Subject: %s\n", buf);
2602                 }
2603         }
2604
2605         /* Message-ID */
2606         if (compose->account->gen_msgid) {
2607                 compose_generate_msgid(compose, buf, sizeof(buf));
2608                 fprintf(fp, "Message-Id: <%s>\n", buf);
2609                 compose->msgid = g_strdup(buf);
2610         }
2611
2612         /* In-Reply-To */
2613         if (compose->inreplyto && compose->to_list)
2614                 fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
2615
2616         /* References */
2617         if (compose->references)
2618                 fprintf(fp, "References: %s\n", compose->references);
2619
2620         /* Followup-To */
2621         if (compose->use_followupto && !IS_IN_CUSTOM_HEADER("Followup-To")) {
2622                 str = gtk_entry_get_text(GTK_ENTRY(compose->followup_entry));
2623                 if (*str != '\0') {
2624                         Xstrdup_a(str, str, return -1);
2625                         g_strstrip(str);
2626                         remove_space(str);
2627                         if (*str != '\0') {
2628                                 compose_convert_header(buf, sizeof(buf), str,
2629                                                        strlen("Followup-To: "));
2630                                 fprintf(fp, "Followup-To: %s\n", buf);
2631                         }
2632                 }
2633         }
2634
2635         /* Reply-To */
2636         if (compose->use_replyto && !IS_IN_CUSTOM_HEADER("Reply-To")) {
2637                 str = gtk_entry_get_text(GTK_ENTRY(compose->reply_entry));
2638                 if (*str != '\0') {
2639                         Xstrdup_a(str, str, return -1);
2640                         g_strstrip(str);
2641                         if (*str != '\0') {
2642                                 compose_convert_header(buf, sizeof(buf), str,
2643                                                        strlen("Reply-To: "));
2644                                 fprintf(fp, "Reply-To: %s\n", buf);
2645                         }
2646                 }
2647         }
2648
2649         /* Organization */
2650         if (compose->account->organization &&
2651             !IS_IN_CUSTOM_HEADER("Organization")) {
2652                 compose_convert_header(buf, sizeof(buf),
2653                                        compose->account->organization,
2654                                        strlen("Organization: "));
2655                 fprintf(fp, "Organization: %s\n", buf);
2656         }
2657
2658         /* Program version and system info */
2659         /* uname(&utsbuf); */
2660         str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
2661         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("X-Mailer")) {
2662                 fprintf(fp, "X-Mailer: %s (GTK+ %d.%d.%d; %s)\n",
2663                         prog_version,
2664                         gtk_major_version, gtk_minor_version, gtk_micro_version,
2665                         HOST_ALIAS);
2666                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2667         }
2668         str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
2669         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("X-Newsreader")) {
2670                 fprintf(fp, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
2671                         prog_version,
2672                         gtk_major_version, gtk_minor_version, gtk_micro_version,
2673                         HOST_ALIAS);
2674                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2675         }
2676
2677         /* custom headers */
2678         if (compose->account->add_customhdr) {
2679                 GSList *cur;
2680
2681                 for (cur = compose->account->customhdr_list; cur != NULL;
2682                      cur = cur->next) {
2683                         CustomHeader *chdr = (CustomHeader *)cur->data;
2684
2685                         if (strcasecmp(chdr->name, "Date")         != 0 &&
2686                             strcasecmp(chdr->name, "From")         != 0 &&
2687                             strcasecmp(chdr->name, "To")           != 0 &&
2688                             strcasecmp(chdr->name, "Sender")       != 0 &&
2689                             strcasecmp(chdr->name, "Message-Id")   != 0 &&
2690                             strcasecmp(chdr->name, "In-Reply-To")  != 0 &&
2691                             strcasecmp(chdr->name, "References")   != 0 &&
2692                             strcasecmp(chdr->name, "Mime-Version") != 0 &&
2693                             strcasecmp(chdr->name, "Content-Type") != 0 &&
2694                             strcasecmp(chdr->name, "Content-Transfer-Encoding")
2695                             != 0)
2696                                 compose_convert_header
2697                                         (buf, sizeof(buf),
2698                                          chdr->value ? chdr->value : "",
2699                                          strlen(chdr->name) + 2);
2700                                 fprintf(fp, "%s: %s\n", chdr->name, buf);
2701                 }
2702         }
2703
2704         /* MIME */
2705         fprintf(fp, "Mime-Version: 1.0\n");
2706         if (compose->use_attach) {
2707                 get_rfc822_date(buf, sizeof(buf));
2708                 subst_char(buf, ' ', '_');
2709                 subst_char(buf, ',', '_');
2710                 compose->boundary = g_strdup_printf("Multipart_%s_%08x",
2711                                                     buf, (guint)compose);
2712                 fprintf(fp,
2713                         "Content-Type: multipart/mixed;\n"
2714                         " boundary=\"%s\"\n", compose->boundary);
2715         } else {
2716                 fprintf(fp, "Content-Type: text/plain; charset=%s\n", charset);
2717                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2718                         procmime_get_encoding_str(encoding));
2719         }
2720
2721         /* Request Return Receipt */
2722         if (!IS_IN_CUSTOM_HEADER("Disposition-Notification-To")) {
2723                 if (compose->return_receipt) {
2724                         if (compose->account->name
2725                             && *compose->account->name) {
2726                                 compose_convert_header(buf, sizeof(buf), compose->account->name, strlen("Disposition-Notification-To: "));
2727                                 fprintf(fp, "Disposition-Notification-To: %s <%s>\n", buf, compose->account->address);
2728                         } else
2729                                 fprintf(fp, "Disposition-Notification-To: %s\n", compose->account->address);
2730                 }
2731         }
2732
2733         /* separator between header and body */
2734         fputs("\n", fp);
2735
2736         return 0;
2737 }
2738
2739 #undef IS_IN_CUSTOM_HEADER
2740
2741 static void compose_convert_header(gchar *dest, gint len, gchar *src,
2742                                    gint header_len)
2743 {
2744         g_return_if_fail(src != NULL);
2745         g_return_if_fail(dest != NULL);
2746
2747         if (len < 1) return;
2748
2749         remove_return(src);
2750
2751         if (is_ascii_str(src)) {
2752                 strncpy2(dest, src, len);
2753                 dest[len - 1] = '\0';
2754                 return;
2755         } else
2756                 conv_encode_header(dest, len, src, header_len);
2757 }
2758
2759 static void compose_generate_msgid(Compose *compose, gchar *buf, gint len)
2760 {
2761         struct tm *lt;
2762         time_t t;
2763         gchar *addr;
2764
2765         t = time(NULL);
2766         lt = localtime(&t);
2767
2768         if (compose->account && compose->account->address &&
2769             *compose->account->address) {
2770                 if (strchr(compose->account->address, '@'))
2771                         addr = g_strdup(compose->account->address);
2772                 else
2773                         addr = g_strconcat(compose->account->address, "@",
2774                                            get_domain_name(), NULL);
2775         } else
2776                 addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
2777                                    NULL);
2778
2779         g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x.%s",
2780                    lt->tm_year + 1900, lt->tm_mon + 1,
2781                    lt->tm_mday, lt->tm_hour,
2782                    lt->tm_min, lt->tm_sec,
2783                    (guint)random(), addr);
2784
2785         debug_print(_("generated Message-ID: %s\n"), buf);
2786
2787         g_free(addr);
2788 }
2789
2790 static void compose_add_entry_field(GtkWidget *table, GtkWidget **hbox,
2791                                     GtkWidget **entry, gint *count,
2792                                     const gchar *label_str,
2793                                     gboolean is_addr_entry)
2794 {
2795         GtkWidget *label;
2796
2797         if (GTK_TABLE(table)->nrows < (*count) + 1)
2798                 gtk_table_resize(GTK_TABLE(table), (*count) + 1, 2);
2799
2800         *hbox = gtk_hbox_new(FALSE, 0);
2801         label = gtk_label_new
2802                 (prefs_common.trans_hdr ? gettext(label_str) : label_str);
2803         gtk_box_pack_end(GTK_BOX(*hbox), label, FALSE, FALSE, 0);
2804         gtk_table_attach(GTK_TABLE(table), *hbox, 0, 1, *count, (*count) + 1,
2805                          GTK_FILL, 0, 2, 0);
2806         *entry = gtk_entry_new();
2807         gtk_table_attach_defaults
2808                 (GTK_TABLE(table), *entry, 1, 2, *count, (*count) + 1);
2809         if (GTK_TABLE(table)->nrows > (*count) + 1)
2810                 gtk_table_set_row_spacing(GTK_TABLE(table), *count, 4);
2811
2812         if (is_addr_entry)
2813                 address_completion_register_entry(GTK_ENTRY(*entry));
2814
2815         (*count)++;
2816 }
2817
2818 static Compose *compose_create(PrefsAccount *account)
2819 {
2820         Compose   *compose;
2821         GtkWidget *window;
2822         GtkWidget *vbox;
2823         GtkWidget *menubar;
2824         GtkWidget *handlebox;
2825
2826         GtkWidget *vbox2;
2827
2828         GtkWidget *table_vbox;
2829         GtkWidget *label;
2830         GtkWidget *from_optmenu_hbox;
2831         GtkWidget *to_entry;
2832         GtkWidget *to_hbox;
2833         GtkWidget *newsgroups_entry;
2834         GtkWidget *newsgroups_hbox;
2835         GtkWidget *subject_entry;
2836         GtkWidget *cc_entry;
2837         GtkWidget *cc_hbox;
2838         GtkWidget *bcc_entry;
2839         GtkWidget *bcc_hbox;
2840         GtkWidget *reply_entry;
2841         GtkWidget *reply_hbox;
2842         GtkWidget *followup_entry;
2843         GtkWidget *followup_hbox;
2844
2845         GtkWidget *paned;
2846
2847         GtkWidget *attach_scrwin;
2848         GtkWidget *attach_clist;
2849
2850         GtkWidget *edit_vbox;
2851         GtkWidget *ruler_hbox;
2852         GtkWidget *ruler;
2853         GtkWidget *scrolledwin;
2854         GtkWidget *text;
2855
2856         GtkWidget *table;
2857         GtkWidget *hbox;
2858
2859         gchar *titles[] = {_("MIME type"), _("Size"), _("Name")};
2860         guint n_menu_entries;
2861         GtkStyle  *style, *new_style;
2862         GdkColormap *cmap;
2863         GdkColor color[1];
2864         gboolean success[1];
2865         GdkFont   *font;
2866         GtkWidget *popupmenu;
2867         GtkWidget *menuitem;
2868         GtkItemFactory *popupfactory;
2869         GtkItemFactory *ifactory;
2870         gint n_entries;
2871         gint count = 0;
2872         gint i;
2873
2874         g_return_val_if_fail(account != NULL, NULL);
2875
2876         debug_print(_("Creating compose window...\n"));
2877         compose = g_new0(Compose, 1);
2878
2879         compose->account = account;
2880         compose->orig_account = account;
2881
2882         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2883         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
2884         gtk_widget_set_usize(window, -1, prefs_common.compose_height);
2885         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
2886                            GTK_SIGNAL_FUNC(compose_delete_cb), compose);
2887         gtk_signal_connect(GTK_OBJECT(window), "destroy",
2888                            GTK_SIGNAL_FUNC(compose_destroy_cb), compose);
2889         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
2890                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
2891         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
2892                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
2893         gtk_widget_realize(window);
2894
2895         vbox = gtk_vbox_new(FALSE, 0);
2896         gtk_container_add(GTK_CONTAINER(window), vbox);
2897
2898         n_menu_entries = sizeof(compose_entries) / sizeof(compose_entries[0]);
2899         menubar = menubar_create(window, compose_entries,
2900                                  n_menu_entries, "<Compose>", compose);
2901         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
2902
2903         handlebox = gtk_handle_box_new();
2904         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
2905
2906         compose_toolbar_create(compose, handlebox);
2907
2908         vbox2 = gtk_vbox_new(FALSE, 2);
2909         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
2910         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
2911
2912         table_vbox = gtk_vbox_new(FALSE, 0);
2913         gtk_box_pack_start(GTK_BOX(vbox2), table_vbox, FALSE, TRUE, 0);
2914         gtk_container_set_border_width(GTK_CONTAINER(table_vbox),
2915                                        BORDER_WIDTH * 2);
2916
2917         table = gtk_table_new(8, 2, FALSE);
2918         gtk_box_pack_start(GTK_BOX(table_vbox), table, FALSE, TRUE, 0);
2919
2920         /* option menu for selecting accounts */
2921         hbox = gtk_hbox_new(FALSE, 0);
2922         label = gtk_label_new(prefs_common.trans_hdr ? _("From:") : "From:");
2923         gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
2924         gtk_table_attach(GTK_TABLE(table), hbox, 0, 1, count, count + 1,
2925                          GTK_FILL, 0, 2, 0);
2926         from_optmenu_hbox = compose_account_option_menu_create(compose);
2927         gtk_table_attach_defaults(GTK_TABLE(table), from_optmenu_hbox,
2928                                   1, 2, count, count + 1);
2929         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
2930         count++;
2931
2932         /* header labels and entries */
2933         compose_add_entry_field(table, &to_hbox, &to_entry, &count,
2934                                 "To:", TRUE); 
2935         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
2936         compose_add_entry_field(table, &newsgroups_hbox, &newsgroups_entry,
2937                                 &count, "Newsgroups:", FALSE);
2938         gtk_table_set_row_spacing(GTK_TABLE(table), 1, 4);
2939         compose_add_entry_field(table, &hbox, &subject_entry, &count,
2940                                 "Subject:", FALSE);
2941         gtk_table_set_row_spacing(GTK_TABLE(table), 2, 4);
2942         compose_add_entry_field(table, &cc_hbox, &cc_entry, &count,
2943                                 "Cc:", TRUE);
2944         gtk_table_set_row_spacing(GTK_TABLE(table), 3, 4);
2945         compose_add_entry_field(table, &bcc_hbox, &bcc_entry, &count,
2946                                 "Bcc:", TRUE);
2947         gtk_table_set_row_spacing(GTK_TABLE(table), 4, 4);
2948         compose_add_entry_field(table, &reply_hbox, &reply_entry, &count,
2949                                 "Reply-To:", TRUE);
2950         gtk_table_set_row_spacing(GTK_TABLE(table), 5, 4);
2951         compose_add_entry_field(table, &followup_hbox, &followup_entry, &count,
2952                                 "Followup-To:", FALSE);
2953         gtk_table_set_row_spacing(GTK_TABLE(table), 6, 4);
2954
2955         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
2956
2957         gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
2958                            GTK_SIGNAL_FUNC(to_activated), compose);
2959         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "activate",
2960                            GTK_SIGNAL_FUNC(newsgroups_activated), compose);
2961         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
2962                            GTK_SIGNAL_FUNC(subject_activated), compose);
2963         gtk_signal_connect(GTK_OBJECT(cc_entry), "activate",
2964                            GTK_SIGNAL_FUNC(cc_activated), compose);
2965         gtk_signal_connect(GTK_OBJECT(bcc_entry), "activate",
2966                            GTK_SIGNAL_FUNC(bcc_activated), compose);
2967         gtk_signal_connect(GTK_OBJECT(reply_entry), "activate",
2968                            GTK_SIGNAL_FUNC(replyto_activated), compose);
2969         gtk_signal_connect(GTK_OBJECT(followup_entry), "activate",
2970                            GTK_SIGNAL_FUNC(followupto_activated), compose);
2971
2972         gtk_signal_connect(GTK_OBJECT(subject_entry), "grab_focus",
2973                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2974         gtk_signal_connect(GTK_OBJECT(to_entry), "grab_focus",
2975                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2976         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "grab_focus",
2977                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2978         gtk_signal_connect(GTK_OBJECT(cc_entry), "grab_focus",
2979                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2980         gtk_signal_connect(GTK_OBJECT(bcc_entry), "grab_focus",
2981                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2982         gtk_signal_connect(GTK_OBJECT(reply_entry), "grab_focus",
2983                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2984         gtk_signal_connect(GTK_OBJECT(followup_entry), "grab_focus",
2985                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2986
2987         /* attachment list */
2988         attach_scrwin = gtk_scrolled_window_new(NULL, NULL);
2989         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(attach_scrwin),
2990                                        GTK_POLICY_AUTOMATIC,
2991                                        GTK_POLICY_ALWAYS);
2992         gtk_widget_set_usize(attach_scrwin, -1, 80);
2993
2994         attach_clist = gtk_clist_new_with_titles(N_ATTACH_COLS, titles);
2995         gtk_clist_set_column_justification(GTK_CLIST(attach_clist), COL_SIZE,
2996                                            GTK_JUSTIFY_RIGHT);
2997         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_MIMETYPE, 240);
2998         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_SIZE, 64);
2999         gtk_clist_set_selection_mode(GTK_CLIST(attach_clist),
3000                                      GTK_SELECTION_EXTENDED);
3001         for (i = 0; i < N_ATTACH_COLS; i++)
3002                 GTK_WIDGET_UNSET_FLAGS
3003                         (GTK_CLIST(attach_clist)->column[i].button,
3004                          GTK_CAN_FOCUS);
3005         gtk_container_add(GTK_CONTAINER(attach_scrwin), attach_clist);
3006
3007         gtk_signal_connect(GTK_OBJECT(attach_clist), "select_row",
3008                            GTK_SIGNAL_FUNC(attach_selected), compose);
3009         gtk_signal_connect(GTK_OBJECT(attach_clist), "button_press_event",
3010                            GTK_SIGNAL_FUNC(attach_button_pressed), compose);
3011         gtk_signal_connect(GTK_OBJECT(attach_clist), "key_press_event",
3012                            GTK_SIGNAL_FUNC(attach_key_pressed), compose);
3013
3014         /* drag and drop */
3015         gtk_drag_dest_set(attach_clist,
3016                           GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
3017                           GDK_ACTION_COPY);
3018         gtk_signal_connect(GTK_OBJECT(attach_clist), "drag_data_received",
3019                            GTK_SIGNAL_FUNC(compose_attach_drag_received_cb),
3020                            compose);
3021
3022         /* pane between attach clist and text */
3023         paned = gtk_vpaned_new();
3024         gtk_paned_add1(GTK_PANED(paned), attach_scrwin);
3025         gtk_widget_ref(paned);
3026         gtk_widget_show_all(paned);
3027
3028         edit_vbox = gtk_vbox_new(FALSE, 0);
3029         gtk_box_pack_start(GTK_BOX(vbox2), edit_vbox, TRUE, TRUE, 0);
3030
3031         /* ruler */
3032         ruler_hbox = gtk_hbox_new(FALSE, 0);
3033         gtk_box_pack_start(GTK_BOX(edit_vbox), ruler_hbox, FALSE, FALSE, 0);
3034
3035         ruler = gtk_shruler_new();
3036         gtk_ruler_set_range(GTK_RULER(ruler), 0.0, 100.0, 1.0, 100.0);
3037         gtk_box_pack_start(GTK_BOX(ruler_hbox), ruler, TRUE, TRUE,
3038                            BORDER_WIDTH + 1);
3039         gtk_widget_set_usize(ruler_hbox, 1, -1);
3040
3041         /* text widget */
3042         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
3043         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
3044                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
3045         gtk_box_pack_start(GTK_BOX(edit_vbox), scrolledwin, TRUE, TRUE, 0);
3046         gtk_widget_set_usize(scrolledwin, prefs_common.compose_width, -1);
3047
3048         text = gtk_stext_new(gtk_scrolled_window_get_hadjustment
3049                             (GTK_SCROLLED_WINDOW(scrolledwin)),
3050                             gtk_scrolled_window_get_vadjustment
3051                             (GTK_SCROLLED_WINDOW(scrolledwin)));
3052         GTK_STEXT(text)->default_tab_width = 8;
3053         gtk_stext_set_editable(GTK_STEXT(text), TRUE);
3054         if (prefs_common.smart_wrapping) {      
3055                 gtk_stext_set_word_wrap(GTK_STEXT(text), TRUE);
3056                 gtk_stext_set_wrap_rmargin(GTK_STEXT(text), prefs_common.linewrap_len);
3057         }               
3058
3059         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
3060
3061         gtk_signal_connect(GTK_OBJECT(text), "changed",
3062                            GTK_SIGNAL_FUNC(compose_changed_cb), compose);
3063         gtk_signal_connect(GTK_OBJECT(text), "grab_focus",
3064                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
3065         gtk_signal_connect_after(GTK_OBJECT(text), "button_press_event",
3066                                  GTK_SIGNAL_FUNC(compose_button_press_cb),
3067                                  compose);
3068 #if 0
3069         gtk_signal_connect_after(GTK_OBJECT(text), "key_press_event",
3070                                  GTK_SIGNAL_FUNC(compose_key_press_cb),
3071                                  compose);
3072 #endif
3073         gtk_signal_connect_after(GTK_OBJECT(text), "size_allocate",
3074                                  GTK_SIGNAL_FUNC(compose_edit_size_alloc),
3075                                  ruler);
3076
3077         /* drag and drop */
3078         gtk_drag_dest_set(text, GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
3079                           GDK_ACTION_COPY);
3080         gtk_signal_connect(GTK_OBJECT(text), "drag_data_received",
3081                            GTK_SIGNAL_FUNC(compose_insert_drag_received_cb),
3082                            compose);
3083
3084         gtk_widget_show_all(vbox);
3085
3086         style = gtk_widget_get_style(text);
3087
3088         /* workaround for the slow down of GtkText when using Pixmap theme */
3089         if (style->engine) {
3090                 GtkThemeEngine *engine;
3091
3092                 engine = style->engine;
3093                 style->engine = NULL;
3094                 new_style = gtk_style_copy(style);
3095                 style->engine = engine;
3096         } else
3097                 new_style = gtk_style_copy(style);
3098
3099         if (prefs_common.textfont) {
3100                 CharSet charset;
3101
3102                 charset = conv_get_current_charset();
3103                 if (MB_CUR_MAX == 1) {
3104                         gchar *fontstr, *p;
3105
3106                         Xstrdup_a(fontstr, prefs_common.textfont, );
3107                         if (fontstr && (p = strchr(fontstr, ',')) != NULL)
3108                                 *p = '\0';
3109                         font = gdk_font_load(fontstr);
3110                 } else
3111                         font = gdk_fontset_load(prefs_common.textfont);
3112                 if (font) {
3113                         gdk_font_unref(new_style->font);
3114                         new_style->font = font;
3115                 }
3116         }
3117
3118         gtk_widget_set_style(text, new_style);
3119
3120         color[0] = quote_color;
3121         cmap = gdk_window_get_colormap(window->window);
3122         gdk_colormap_alloc_colors(cmap, color, 1, FALSE, TRUE, success);
3123         if (success[0] == FALSE) {
3124                 g_warning("Compose: color allocation failed.\n");
3125                 style = gtk_widget_get_style(text);
3126                 quote_color = style->black;
3127         }
3128
3129         n_entries = sizeof(compose_popup_entries) /
3130                 sizeof(compose_popup_entries[0]);
3131         popupmenu = menu_create_items(compose_popup_entries, n_entries,
3132                                       "<Compose>", &popupfactory,
3133                                       compose);
3134
3135         ifactory = gtk_item_factory_from_widget(menubar);
3136         menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
3137         menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
3138
3139         /*
3140         if (account->protocol == A_NNTP) {
3141                 gtk_widget_hide(to_hbox);
3142                 gtk_widget_hide(to_entry);
3143                 gtk_widget_hide(cc_hbox);
3144                 gtk_widget_hide(cc_entry);
3145                 gtk_table_set_row_spacing(GTK_TABLE(table), 1, 0);
3146                 gtk_table_set_row_spacing(GTK_TABLE(table), 3, 0);
3147         } else {
3148                 gtk_widget_hide(newsgroups_hbox);
3149                 gtk_widget_hide(newsgroups_entry);
3150                 gtk_table_set_row_spacing(GTK_TABLE(table), 2, 0);
3151
3152                 menu_set_sensitive(ifactory, "/Message/Followup to", FALSE);
3153         }
3154         */
3155
3156         switch (prefs_common.toolbar_style) {
3157         case TOOLBAR_NONE:
3158                 gtk_widget_hide(handlebox);
3159                 break;
3160         case TOOLBAR_ICON:
3161                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3162                                       GTK_TOOLBAR_ICONS);
3163                 break;
3164         case TOOLBAR_TEXT:
3165                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3166                                       GTK_TOOLBAR_TEXT);
3167                 break;
3168         case TOOLBAR_BOTH:
3169                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3170                                       GTK_TOOLBAR_BOTH);
3171                 break;
3172         }
3173
3174         gtk_widget_show(window);
3175
3176         address_completion_start(window);
3177
3178         compose->window        = window;
3179         compose->vbox          = vbox;
3180         compose->menubar       = menubar;
3181         compose->handlebox     = handlebox;
3182
3183         compose->vbox2         = vbox2;
3184
3185         compose->table_vbox       = table_vbox;
3186         compose->table            = table;
3187         compose->to_hbox          = to_hbox;
3188         compose->to_entry         = to_entry;
3189         compose->newsgroups_hbox  = newsgroups_hbox;
3190         compose->newsgroups_entry = newsgroups_entry;
3191         compose->subject_entry    = subject_entry;
3192         compose->cc_hbox          = cc_hbox;
3193         compose->cc_entry         = cc_entry;
3194         compose->bcc_hbox         = bcc_hbox;
3195         compose->bcc_entry        = bcc_entry;
3196         compose->reply_hbox       = reply_hbox;
3197         compose->reply_entry      = reply_entry;
3198         compose->followup_hbox    = followup_hbox;
3199         compose->followup_entry   = followup_entry;
3200
3201         compose->paned = paned;
3202
3203         compose->attach_scrwin = attach_scrwin;
3204         compose->attach_clist  = attach_clist;
3205
3206         compose->edit_vbox     = edit_vbox;
3207         compose->ruler_hbox    = ruler_hbox;
3208         compose->ruler         = ruler;
3209         compose->scrolledwin   = scrolledwin;
3210         compose->text          = text;
3211
3212         compose->focused_editable = NULL;
3213
3214         compose->popupmenu    = popupmenu;
3215         compose->popupfactory = popupfactory;
3216
3217         compose->mode = COMPOSE_NEW;
3218
3219         compose->replyto     = NULL;
3220         compose->cc          = NULL;
3221         compose->bcc         = NULL;
3222         compose->followup_to = NULL;
3223         compose->inreplyto   = NULL;
3224         compose->references  = NULL;
3225         compose->msgid       = NULL;
3226         compose->boundary    = NULL;
3227
3228 #if USE_GPGME
3229         compose->use_signing    = FALSE;
3230         compose->use_encryption = FALSE;
3231 #endif /* USE_GPGME */
3232
3233         compose->modified = FALSE;
3234
3235         compose->return_receipt = FALSE;
3236
3237         compose->to_list        = NULL;
3238         compose->newsgroup_list = NULL;
3239
3240         compose->exteditor_file    = NULL;
3241         compose->exteditor_pid     = -1;
3242         compose->exteditor_readdes = -1;
3243         compose->exteditor_tag     = -1;
3244
3245         compose_set_title(compose);
3246
3247         compose->use_bcc        = FALSE;
3248         compose->use_replyto    = FALSE;
3249         compose->use_followupto = FALSE;
3250
3251         /*
3252         if (account->protocol != A_NNTP) {
3253                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
3254                 gtk_check_menu_item_set_active
3255                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3256                 gtk_widget_set_sensitive(menuitem, FALSE);
3257                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
3258                 gtk_check_menu_item_set_active
3259                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3260                 gtk_widget_set_sensitive(menuitem, FALSE);
3261         }
3262         */
3263         if (account->set_autocc && account->auto_cc) {
3264                 compose->use_cc = TRUE;
3265                 gtk_entry_set_text(GTK_ENTRY(cc_entry), account->auto_cc);
3266                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
3267                 gtk_check_menu_item_set_active
3268                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3269         }
3270
3271         if (account->set_autobcc) {
3272                 compose->use_bcc = TRUE;
3273                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");