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