2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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.
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.
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.
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>
58 /* #include <sys/utsname.h> */
64 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
74 #include "mainwindow.h"
76 #include "addressbook.h"
77 #include "folderview.h"
82 #include "customheader.h"
83 #include "prefs_common.h"
84 #include "prefs_account.h"
87 #include "procheader.h"
89 #include "statusbar.h"
96 #include "alertpanel.h"
97 #include "manage_window.h"
98 #include "gtkshruler.h"
100 #include "addr_compl.h"
101 #include "quote_fmt.h"
102 #include "template.h"
105 # include "rfc2015.h"
115 #define N_ATTACH_COLS 3
117 #define B64_LINE_SIZE 57
118 #define B64_BUFFSIZE 77
120 #define MAX_REFERENCES_LEN 999
122 static GdkColor quote_color = {0, 0, 0, 0xbfff};
124 static GList *compose_list = NULL;
126 Compose *compose_generic_new (PrefsAccount *account,
130 static Compose *compose_create (PrefsAccount *account,
132 static void compose_toolbar_create (Compose *compose,
133 GtkWidget *container);
134 static GtkWidget *compose_account_option_menu_create
136 static void compose_set_template_menu (Compose *compose);
137 static void compose_destroy (Compose *compose);
139 static void compose_entries_set (Compose *compose,
140 const gchar *mailto);
141 static gint compose_parse_header (Compose *compose,
143 static gchar *compose_parse_references (const gchar *ref,
146 static gchar *compose_quote_fmt (Compose *compose,
151 static void compose_reply_set_entry (Compose *compose,
156 followup_and_reply_to);
157 static void compose_reedit_set_entry (Compose *compose,
159 static void compose_insert_sig (Compose *compose);
160 static void compose_insert_file (Compose *compose,
162 static void compose_attach_append (Compose *compose,
164 ContentType cnttype);
165 static void compose_attach_append_with_type(Compose *compose,
168 ContentType cnttype);
169 static void compose_wrap_line (Compose *compose);
170 static void compose_wrap_line_all (Compose *compose);
171 static void compose_set_title (Compose *compose);
173 static PrefsAccount *compose_current_mail_account(void);
174 /* static gint compose_send (Compose *compose); */
175 static gint compose_write_to_file (Compose *compose,
178 static gint compose_write_body_to_file (Compose *compose,
180 static gint compose_save_to_outbox (Compose *compose,
182 static gint compose_remove_reedit_target (Compose *compose);
183 static gint compose_queue (Compose *compose,
186 static void compose_write_attach (Compose *compose,
188 static gint compose_write_headers (Compose *compose,
190 const gchar *charset,
191 EncodingType encoding,
194 static void compose_convert_header (gchar *dest,
198 static void compose_generate_msgid (Compose *compose,
202 static void compose_attach_info_free (AttachInfo *ainfo);
203 static void compose_attach_remove_selected (Compose *compose);
205 static void compose_attach_property (Compose *compose);
206 static void compose_attach_property_create (gboolean *cancelled);
207 static void attach_property_ok (GtkWidget *widget,
208 gboolean *cancelled);
209 static void attach_property_cancel (GtkWidget *widget,
210 gboolean *cancelled);
211 static gint attach_property_delete_event (GtkWidget *widget,
213 gboolean *cancelled);
214 static void attach_property_key_pressed (GtkWidget *widget,
216 gboolean *cancelled);
218 static void compose_exec_ext_editor (Compose *compose);
219 static gint compose_exec_ext_editor_real (const gchar *file);
220 static gboolean compose_ext_editor_kill (Compose *compose);
221 static void compose_input_cb (gpointer data,
223 GdkInputCondition condition);
224 static void compose_set_ext_editor_sensitive (Compose *compose,
227 static gint calc_cursor_xpos (GtkSText *text,
231 static void compose_create_header_entry (Compose *compose);
232 static void compose_add_header_entry (Compose *compose, gchar *header, gchar *text);
234 /* callback functions */
236 static gboolean compose_edit_size_alloc (GtkEditable *widget,
237 GtkAllocation *allocation,
238 GtkSHRuler *shruler);
240 static void toolbar_send_cb (GtkWidget *widget,
242 static void toolbar_send_later_cb (GtkWidget *widget,
244 static void toolbar_draft_cb (GtkWidget *widget,
246 static void toolbar_insert_cb (GtkWidget *widget,
248 static void toolbar_attach_cb (GtkWidget *widget,
250 static void toolbar_sig_cb (GtkWidget *widget,
252 static void toolbar_ext_editor_cb (GtkWidget *widget,
254 static void toolbar_linewrap_cb (GtkWidget *widget,
256 static void toolbar_address_cb (GtkWidget *widget,
259 static void select_account (Compose *compose,
262 static void account_activated (GtkMenuItem *menuitem,
265 static void attach_selected (GtkCList *clist,
270 static void attach_button_pressed (GtkWidget *widget,
271 GdkEventButton *event,
273 static void attach_key_pressed (GtkWidget *widget,
277 static void compose_send_cb (gpointer data,
280 static void compose_send_later_cb (gpointer data,
284 static void compose_draft_cb (gpointer data,
288 static void compose_attach_cb (gpointer data,
291 static void compose_insert_file_cb (gpointer data,
295 static void compose_close_cb (gpointer data,
299 static void compose_address_cb (gpointer data,
302 static void compose_template_activate_cb(GtkWidget *widget,
305 static void compose_ext_editor_cb (gpointer data,
309 static gint compose_delete_cb (GtkWidget *widget,
312 static void compose_destroy_cb (GtkWidget *widget,
315 static void compose_cut_cb (Compose *compose);
316 static void compose_copy_cb (Compose *compose);
317 static void compose_paste_cb (Compose *compose);
318 static void compose_allsel_cb (Compose *compose);
320 static void compose_grab_focus_cb (GtkWidget *widget,
323 static void compose_changed_cb (GtkEditable *editable,
325 static void compose_button_press_cb (GtkWidget *widget,
326 GdkEventButton *event,
329 static void compose_key_press_cb (GtkWidget *widget,
335 static void compose_toggle_to_cb (gpointer data,
338 static void compose_toggle_cc_cb (gpointer data,
341 static void compose_toggle_bcc_cb (gpointer data,
344 static void compose_toggle_replyto_cb (gpointer data,
347 static void compose_toggle_followupto_cb(gpointer data,
350 static void compose_toggle_attach_cb (gpointer data,
354 static void compose_toggle_ruler_cb (gpointer data,
358 static void compose_toggle_sign_cb (gpointer data,
361 static void compose_toggle_encrypt_cb (gpointer data,
365 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
368 static void compose_attach_drag_received_cb (GtkWidget *widget,
369 GdkDragContext *drag_context,
372 GtkSelectionData *data,
376 static void compose_insert_drag_received_cb (GtkWidget *widget,
377 GdkDragContext *drag_context,
380 GtkSelectionData *data,
386 static void to_activated (GtkWidget *widget,
388 static void newsgroups_activated (GtkWidget *widget,
390 static void subject_activated (GtkWidget *widget,
392 static void cc_activated (GtkWidget *widget,
394 static void bcc_activated (GtkWidget *widget,
396 static void replyto_activated (GtkWidget *widget,
398 static void followupto_activated (GtkWidget *widget,
402 static void compose_attach_parts (Compose *compose,
405 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
407 gboolean ignore_replyto,
408 gboolean followup_and_reply_to);
410 void compose_headerentry_changed_cb (GtkWidget *entry,
411 ComposeHeaderEntry *headerentry);
412 void compose_headerentry_key_press_event_cb(GtkWidget *entry,
414 ComposeHeaderEntry *headerentry);
416 static GtkItemFactoryEntry compose_popup_entries[] =
418 {N_("/_Add..."), NULL, compose_attach_cb, 0, NULL},
419 {N_("/_Remove"), NULL, compose_attach_remove_selected, 0, NULL},
420 {N_("/---"), NULL, NULL, 0, "<Separator>"},
421 {N_("/_Property..."), NULL, compose_attach_property, 0, NULL}
424 static GtkItemFactoryEntry compose_entries[] =
426 {N_("/_File"), NULL, NULL, 0, "<Branch>"},
427 {N_("/_File/_Attach file"), "<control>M", compose_attach_cb, 0, NULL},
428 {N_("/_File/_Insert file"), "<control>I", compose_insert_file_cb, 0, NULL},
429 {N_("/_File/Insert si_gnature"), "<control>G", compose_insert_sig, 0, NULL},
430 {N_("/_File/---"), NULL, NULL, 0, "<Separator>"},
431 {N_("/_File/_Close"), "<alt>W", compose_close_cb, 0, NULL},
433 {N_("/_Edit"), NULL, NULL, 0, "<Branch>"},
434 {N_("/_Edit/_Undo"), "<control>Z", NULL, 0, NULL},
435 {N_("/_Edit/_Redo"), "<control>Y", NULL, 0, NULL},
436 {N_("/_Edit/---"), NULL, NULL, 0, "<Separator>"},
437 {N_("/_Edit/Cu_t"), "<control>X", compose_cut_cb, 0, NULL},
438 {N_("/_Edit/_Copy"), "<control>C", compose_copy_cb, 0, NULL},
439 {N_("/_Edit/_Paste"), "<control>V", compose_paste_cb, 0, NULL},
440 {N_("/_Edit/Select _all"), "<control>A", compose_allsel_cb, 0, NULL},
441 {N_("/_Edit/---"), NULL, NULL, 0, "<Separator>"},
442 {N_("/_Edit/_Wrap current paragraph"), "<alt>L", compose_wrap_line, 0, NULL},
443 {N_("/_Edit/Wrap all long _lines"),
444 "<shift><alt>L", compose_wrap_line_all, 0, NULL},
445 {N_("/_Edit/Edit with e_xternal editor"),
446 "<alt>X", compose_ext_editor_cb, 0, NULL},
448 {N_("/_Message"), NULL, NULL, 0, "<Branch>"},
449 {N_("/_Message/_Send"), "<control>Return",
450 compose_send_cb, 0, NULL},
451 {N_("/_Message/Send _later"), "<shift><alt>S",
452 compose_send_later_cb, 0, NULL},
453 {N_("/_Message/Save to _draft folder"),
454 "<alt>D", compose_draft_cb, 0, NULL},
455 #if 0 /* NEW COMPOSE GUI */
456 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
457 {N_("/_Message/_To"), NULL, compose_toggle_to_cb , 0, "<ToggleItem>"},
458 {N_("/_Message/_Cc"), NULL, compose_toggle_cc_cb , 0, "<ToggleItem>"},
459 {N_("/_Message/_Bcc"), NULL, compose_toggle_bcc_cb , 0, "<ToggleItem>"},
460 {N_("/_Message/_Reply to"), NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
461 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
462 {N_("/_Message/_Followup to"), NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
463 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
464 {N_("/_Message/_Attach"), NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
467 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
468 {N_("/_Message/Si_gn"), NULL, compose_toggle_sign_cb , 0, "<ToggleItem>"},
469 {N_("/_Message/_Encrypt"), NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
470 #endif /* USE_GPGME */
471 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
472 {N_("/_Message/_Request Return Receipt"), NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
473 {N_("/_Tool"), NULL, NULL, 0, "<Branch>"},
474 {N_("/_Tool/Show _ruler"), NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
475 {N_("/_Tool/_Address book"), "<alt>A", compose_address_cb , 0, NULL},
476 {N_("/_Tool/_Template"), NULL, NULL, 0, "<Branch>"},
477 {N_("/_Help"), NULL, NULL, 0, "<LastBranch>"},
478 {N_("/_Help/_About"), NULL, about_show, 0, NULL}
481 static GtkTargetEntry compose_mime_types[] =
483 {"text/uri-list", 0, 0}
486 Compose *compose_new(PrefsAccount *account)
488 return compose_generic_new(account, NULL, NULL);
491 Compose *compose_bounce(PrefsAccount *account, MsgInfo *msginfo)
495 GtkItemFactory *ifactory;
497 c = compose_generic_new(account, NULL, NULL);
499 filename = procmsg_get_message_file(msginfo);
500 if (filename == NULL)
503 c->bounce_filename = filename;
505 if (msginfo->subject)
506 gtk_entry_set_text(GTK_ENTRY(c->subject_entry),
508 gtk_editable_set_editable(GTK_EDITABLE(c->subject_entry), FALSE);
510 compose_quote_fmt(c, msginfo, "%M", NULL);
511 gtk_editable_set_editable(GTK_EDITABLE(c->text), FALSE);
513 ifactory = gtk_item_factory_from_widget(c->popupmenu);
514 menu_set_sensitive(ifactory, "/Add...", FALSE);
515 menu_set_sensitive(ifactory, "/Remove", FALSE);
516 menu_set_sensitive(ifactory, "/Property...", FALSE);
518 ifactory = gtk_item_factory_from_widget(c->menubar);
519 menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
520 menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
521 menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
522 menu_set_sensitive(ifactory, "/Edit/Paste", FALSE);
523 menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", FALSE);
524 menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", FALSE);
525 menu_set_sensitive(ifactory, "/Edit/Edit with external editor", FALSE);
526 menu_set_sensitive(ifactory, "/Message/Attach", FALSE);
528 menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
529 menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
531 menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
532 menu_set_sensitive(ifactory, "/Tool/Template", FALSE);
534 gtk_widget_set_sensitive(c->insert_btn, FALSE);
535 gtk_widget_set_sensitive(c->attach_btn, FALSE);
536 gtk_widget_set_sensitive(c->sig_btn, FALSE);
537 gtk_widget_set_sensitive(c->exteditor_btn, FALSE);
538 gtk_widget_set_sensitive(c->linewrap_btn, FALSE);
543 Compose *compose_new_with_recipient(PrefsAccount *account, const gchar *mailto)
545 return compose_generic_new(account, mailto, NULL);
548 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
550 return compose_generic_new(account, NULL, item);
553 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item)
557 if (item && item->prefs && item->prefs->enable_default_account)
558 account = account_find_from_id(item->prefs->default_account);
560 if (!account) account = cur_account;
561 g_return_val_if_fail(account != NULL, NULL);
563 compose = compose_create(account, COMPOSE_NEW);
564 compose->replyinfo = NULL;
566 if (prefs_common.auto_sig)
567 compose_insert_sig(compose);
568 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
569 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
571 if (account->protocol != A_NNTP) {
573 compose_entries_set(compose, mailto);
575 } else if(item && item->prefs->enable_default_to) {
576 compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
578 if (item && item->ret_rcpt) {
579 GtkItemFactory *ifactory;
581 ifactory = gtk_item_factory_from_widget(compose->menubar);
582 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
586 compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
589 gtk_widget_grab_focus(compose->subject_entry);
591 if (prefs_common.auto_exteditor)
592 compose_exec_ext_editor(compose);
597 #define CHANGE_FLAGS(msginfo) \
599 if (msginfo->folder->folder->change_flags != NULL) \
600 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
606 Compose *compose_new_followup_and_replyto(PrefsAccount *account,
607 const gchar *followupto, gchar * to)
611 if (!account) account = cur_account;
612 g_return_val_if_fail(account != NULL, NULL);
613 g_return_val_if_fail(account->protocol != A_NNTP, NULL);
615 compose = compose_create(account, COMPOSE_NEW);
617 if (prefs_common.auto_sig)
618 compose_insert_sig(compose);
619 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
620 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
622 compose_entry_append(compose, to, COMPOSE_TO);
623 compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
624 gtk_widget_grab_focus(compose->subject_entry);
630 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
631 gboolean ignore_replyto)
633 compose_generic_reply(msginfo, quote, to_all, ignore_replyto, FALSE);
636 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
638 gboolean ignore_replyto)
640 compose_generic_reply(msginfo, quote, to_all, ignore_replyto, TRUE);
643 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
645 gboolean ignore_replyto,
646 gboolean followup_and_reply_to)
649 PrefsAccount *account;
650 PrefsAccount *reply_account;
653 g_return_if_fail(msginfo != NULL);
654 g_return_if_fail(msginfo->folder != NULL);
657 /* select the account set in folderitem's property (if enabled) */
658 if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
659 account = account_find_from_id(msginfo->folder->prefs->default_account);
661 /* select the account for the whole folder (IMAP / NNTP) */
663 account = msginfo->folder->folder->account;
665 /* select account by to: and cc: header if enabled */
666 if (prefs_common.reply_account_autosel) {
667 if (!account && msginfo->to) {
669 Xstrdup_a(to, msginfo->to, return);
671 account = account_find_from_address(to);
675 if(!get_header_from_msginfo(msginfo, cc, sizeof(cc), "CC:")) { /* Found a CC header */
677 account = account_find_from_address(cc);
682 /* select current account */
683 if (!account) account = cur_account;
684 g_return_if_fail(account != NULL);
686 if (ignore_replyto && account->protocol == A_NNTP &&
687 !followup_and_reply_to) {
689 account_find_from_address(account->address);
691 reply_account = compose_current_mail_account();
695 reply_account = account;
697 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
698 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
700 CHANGE_FLAGS(msginfo);
702 compose = compose_create(account, COMPOSE_REPLY);
703 compose->replyinfo = procmsg_msginfo_copy(msginfo);
705 #if 0 /* NEW COMPOSE GUI */
706 if (followup_and_reply_to) {
707 gtk_widget_show(compose->to_hbox);
708 gtk_widget_show(compose->to_entry);
709 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
710 compose->use_to = TRUE;
713 if (msginfo->folder && msginfo->folder->ret_rcpt) {
714 GtkItemFactory *ifactory;
716 ifactory = gtk_item_factory_from_widget(compose->menubar);
717 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
720 if (compose_parse_header(compose, msginfo) < 0) return;
721 compose_reply_set_entry(compose, msginfo, to_all, ignore_replyto,
722 followup_and_reply_to);
724 text = GTK_STEXT(compose->text);
725 gtk_stext_freeze(text);
731 if (prefs_common.quotemark && *prefs_common.quotemark)
732 qmark = prefs_common.quotemark;
736 quote_str = compose_quote_fmt(compose, msginfo,
737 prefs_common.quotefmt,
741 if (prefs_common.auto_sig)
742 compose_insert_sig(compose);
743 gtk_editable_set_position(GTK_EDITABLE(text), 0);
744 gtk_stext_set_point(text, 0);
746 if (quote && prefs_common.linewrap_quote) {
747 compose_wrap_line_all(compose);
750 gtk_stext_thaw(text);
751 gtk_widget_grab_focus(compose->text);
753 if (prefs_common.auto_exteditor)
754 compose_exec_ext_editor(compose);
758 static gchar *procmime_get_file_name(MimeInfo *mimeinfo)
763 g_return_val_if_fail(mimeinfo != NULL, NULL);
765 base = mimeinfo->filename ? mimeinfo->filename
766 : mimeinfo->name ? mimeinfo->name : NULL;
768 if (MIME_TEXT_HTML == mimeinfo->mime_type && base == NULL){
769 filename = g_strdup_printf("%s%smimetmp.%08x.html",
776 base = base ? base : "";
777 base = g_basename(base);
779 filename = g_strdup_printf("%s%smimetmp.%08x",
787 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
793 static gchar *mime_extract_file(gchar *source, MimeInfo *partinfo)
797 if (!partinfo) return NULL;
799 filename = procmime_get_file_name(partinfo);
801 if (procmime_get_part(filename, source, partinfo) < 0)
803 (_("Can't get the part of multipart message."));
808 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
816 gint boundary_len = 0;
818 glong fpos, prev_fpos;
823 g_return_if_fail(msginfo != NULL);
827 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
828 mimeinfo = procmime_scan_mime_header(fp);
829 if (!mimeinfo) break;
831 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
832 rfc2015_is_encrypted(mimeinfo)) {
833 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
835 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
836 !msginfo->plaintext_file &&
837 !msginfo->decryption_failed) {
838 rfc2015_decrypt_message(msginfo, mimeinfo, fp);
839 if (msginfo->plaintext_file &&
840 !msginfo->decryption_failed) {
848 #else /* !USE_GPGME */
849 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
850 mimeinfo = procmime_scan_mime_header(fp);
851 #endif /* USE_GPGME */
854 if (!mimeinfo) return;
855 if (mimeinfo->mime_type == MIME_TEXT)
858 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
860 g_return_if_fail(mimeinfo != NULL);
861 g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
863 if (mimeinfo->mime_type == MIME_MULTIPART) {
864 g_return_if_fail(mimeinfo->boundary != NULL);
865 g_return_if_fail(mimeinfo->sub == NULL);
867 g_return_if_fail(fp != NULL);
869 boundary = mimeinfo->boundary;
872 boundary_len = strlen(boundary);
874 /* look for first boundary */
875 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
876 if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
883 if ((fpos = ftell(fp)) < 0) {
889 for (npart = 0;; npart++) {
891 gboolean eom = FALSE;
895 partinfo = procmime_scan_mime_header(fp);
896 if (!partinfo) break;
899 procmime_mimeinfo_insert(mimeinfo, partinfo);
901 procmime_mimeinfo_free(partinfo);
903 /* look for next boundary */
905 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
906 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
907 if (buf[2 + boundary_len] == '-' &&
908 buf[2 + boundary_len + 1] == '-')
914 eom = TRUE; /* broken MIME message */
917 partinfo->size = fpos - prev_fpos - strlen(buf);
922 source = procmsg_get_message_file_path(msginfo);
924 g_return_if_fail(mimeinfo != NULL);
926 if (!mimeinfo->main && mimeinfo->parent)
928 filename = mime_extract_file(source, mimeinfo);
930 compose_attach_append_with_type(compose, filename,
931 mimeinfo->content_type,
932 mimeinfo->mime_type);
937 if (mimeinfo->sub && mimeinfo->sub->children)
939 filename = mime_extract_file(source, mimeinfo->sub);
941 compose_attach_append_with_type(compose, filename,
942 mimeinfo->content_type,
943 mimeinfo->sub->mime_type);
948 if (mimeinfo->children) {
951 child = mimeinfo->children;
953 filename = mime_extract_file(source, child);
955 compose_attach_append_with_type(compose, filename,
967 procmime_mimeinfo_free_all(mimeinfo);
971 #define INSERT_FW_HEADER(var, hdr) \
972 if (msginfo->var && *msginfo->var) { \
973 gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
974 gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
975 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
978 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
982 /* PrefsAccount *account; */
985 g_return_val_if_fail(msginfo != NULL, NULL);
986 g_return_val_if_fail(msginfo->folder != NULL, NULL);
988 account = msginfo->folder->folder->account;
989 if (!account && msginfo->to && prefs_common.forward_account_autosel) {
991 Xstrdup_a(to, msginfo->to, return NULL);
993 account = account_find_from_address(to);
996 if(!account && prefs_common.forward_account_autosel) {
998 if(!get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
1000 account = account_find_from_address(cc);
1004 if (account == NULL) {
1005 account = cur_account;
1007 account = msginfo->folder->folder->account;
1008 if (!account) account = cur_account;
1011 g_return_val_if_fail(account != NULL, NULL);
1013 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
1014 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
1015 CHANGE_FLAGS(msginfo);
1017 compose = compose_create(account, COMPOSE_FORWARD);
1019 if (msginfo->subject && *msginfo->subject) {
1020 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
1021 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
1025 text = GTK_STEXT(compose->text);
1026 gtk_stext_freeze(text);
1031 msgfile = procmsg_get_message_file_path(msginfo);
1032 if (!is_file_exist(msgfile))
1033 g_warning(_("%s: file not exist\n"), msgfile);
1035 compose_attach_append(compose, msgfile,
1036 MIME_MESSAGE_RFC822);
1043 if (prefs_common.fw_quotemark && *prefs_common.fw_quotemark)
1044 qmark = prefs_common.fw_quotemark;
1048 quote_str = compose_quote_fmt(compose, msginfo,
1049 prefs_common.fw_quotefmt, qmark);
1050 compose_attach_parts(compose, msginfo);
1053 if (prefs_common.auto_sig)
1054 compose_insert_sig(compose);
1055 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1056 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1058 gtk_stext_thaw(text);
1059 #if 0 /* NEW COMPOSE GUI */
1060 if (account->protocol != A_NNTP)
1061 gtk_widget_grab_focus(compose->to_entry);
1063 gtk_widget_grab_focus(compose->newsgroups_entry);
1065 gtk_widget_grab_focus(compose->header_last->entry);
1067 if (prefs_common.auto_exteditor)
1068 compose_exec_ext_editor(compose);
1073 #undef INSERT_FW_HEADER
1075 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1082 g_return_val_if_fail(msginfo_list != NULL, NULL);
1084 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1085 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
1089 if (account == NULL) {
1090 account = cur_account;
1092 account = msginfo->folder->folder->account;
1093 if (!account) account = cur_account;
1096 g_return_val_if_fail(account != NULL, NULL);
1098 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1099 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1100 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1101 CHANGE_FLAGS(((MsgInfo *)msginfo->data));
1104 compose = compose_create(account, COMPOSE_FORWARD);
1106 text = GTK_STEXT(compose->text);
1107 gtk_stext_freeze(text);
1109 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1110 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1111 if (!is_file_exist(msgfile))
1112 g_warning(_("%s: file not exist\n"), msgfile);
1114 compose_attach_append(compose, msgfile,
1115 MIME_MESSAGE_RFC822);
1119 if (prefs_common.auto_sig)
1120 compose_insert_sig(compose);
1121 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1122 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1124 gtk_stext_thaw(text);
1125 #if 0 /* NEW COMPOSE GUI */
1126 if (account->protocol != A_NNTP)
1127 gtk_widget_grab_focus(compose->to_entry);
1129 gtk_widget_grab_focus(compose->newsgroups_entry);
1135 void compose_reedit(MsgInfo *msginfo)
1138 PrefsAccount *account;
1141 gchar buf[BUFFSIZE];
1143 g_return_if_fail(msginfo != NULL);
1144 g_return_if_fail(msginfo->folder != NULL);
1146 account = msginfo->folder->folder->account;
1148 if(!account&& prefs_common.reedit_account_autosel) {
1149 gchar from[BUFFSIZE];
1150 if(!get_header_from_msginfo(msginfo,from,sizeof(from),"FROM:")){ /* Found a FROM header */
1151 extract_address(from);
1152 account = account_find_from_address(from);
1155 if (!account) account = cur_account;
1156 g_return_if_fail(account != NULL);
1158 compose = compose_create(account, COMPOSE_REEDIT);
1159 compose->targetinfo = procmsg_msginfo_copy(msginfo);
1161 if (compose_parse_header(compose, msginfo) < 0) return;
1162 compose_reedit_set_entry(compose, msginfo);
1164 text = GTK_STEXT(compose->text);
1165 gtk_stext_freeze(text);
1167 if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1168 g_warning(_("Can't get text part\n"));
1170 while (fgets(buf, sizeof(buf), fp) != NULL)
1171 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1174 compose_attach_parts(compose, msginfo);
1176 gtk_stext_thaw(text);
1177 gtk_widget_grab_focus(compose->text);
1179 if (prefs_common.auto_exteditor)
1180 compose_exec_ext_editor(compose);
1183 GList *compose_get_compose_list(void)
1185 return compose_list;
1188 void compose_entry_append(Compose *compose, const gchar *address,
1189 ComposeEntryType type)
1195 if (!address || *address == '\0') return;
1197 #if 0 /* NEW COMPOSE GUI */
1200 entry = GTK_ENTRY(compose->cc_entry);
1203 entry = GTK_ENTRY(compose->bcc_entry);
1205 case COMPOSE_NEWSGROUPS:
1206 entry = GTK_ENTRY(compose->newsgroups_entry);
1210 entry = GTK_ENTRY(compose->to_entry);
1214 text = gtk_entry_get_text(entry);
1216 gtk_entry_append_text(entry, ", ");
1217 gtk_entry_append_text(entry, address);
1225 header = N_("Bcc:");
1227 case COMPOSE_REPLYTO:
1228 header = N_("Reply-To:");
1230 case COMPOSE_NEWSGROUPS:
1231 header = N_("Newsgroups:");
1233 case COMPOSE_FOLLOWUPTO:
1234 header = N_( "Followup-To:");
1241 header = prefs_common.trans_hdr ? gettext(header) : header;
1243 compose_add_header_entry(compose, header, (gchar *)address);
1246 static void compose_entries_set(Compose *compose, const gchar *mailto)
1248 gchar *subject = NULL;
1256 Xstrdup_a(tmp_mailto, mailto, return);
1260 p = strchr(tmp_mailto, '?');
1267 gchar *field, *value;
1284 if (*value == '\0') continue;
1286 if (!g_strcasecmp(field, "subject")) {
1287 Xalloca(subject, strlen(value) + 1, return);
1288 decode_uri(subject, value);
1289 } else if (!g_strcasecmp(field, "cc")) {
1291 } else if (!g_strcasecmp(field, "bcc")) {
1293 } else if (!g_strcasecmp(field, "body")) {
1294 Xalloca(body, strlen(value) + 1, return);
1295 decode_uri(body, value);
1300 compose_entry_append(compose, to, COMPOSE_TO);
1302 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1304 compose_entry_append(compose, cc, COMPOSE_CC);
1306 compose_entry_append(compose, bcc, COMPOSE_BCC);
1308 gtk_stext_insert(GTK_STEXT(compose->text),
1309 NULL, NULL, NULL, body, -1);
1310 gtk_stext_insert(GTK_STEXT(compose->text),
1311 NULL, NULL, NULL, "\n", 1);
1315 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1317 static HeaderEntry hentry[] = {{"Reply-To:", NULL, TRUE},
1318 {"Cc:", NULL, FALSE},
1319 {"References:", NULL, FALSE},
1320 {"Bcc:", NULL, FALSE},
1321 {"Newsgroups:", NULL, FALSE},
1322 {"Followup-To:", NULL, FALSE},
1323 {"X-Mailing-List:", NULL, FALSE},
1324 {"X-BeenThere:", NULL, FALSE},
1325 {NULL, NULL, FALSE}};
1335 H_X_MAILING_LIST = 6,
1341 g_return_val_if_fail(msginfo != NULL, -1);
1343 if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1344 procheader_get_header_fields(fp, hentry);
1347 if (hentry[H_REPLY_TO].body != NULL) {
1348 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1349 compose->replyto = hentry[H_REPLY_TO].body;
1350 hentry[H_REPLY_TO].body = NULL;
1352 if (hentry[H_CC].body != NULL) {
1353 conv_unmime_header_overwrite(hentry[H_CC].body);
1354 compose->cc = hentry[H_CC].body;
1355 hentry[H_CC].body = NULL;
1357 if (hentry[H_X_MAILING_LIST].body != NULL) {
1358 /* this is good enough to parse debian-devel */
1359 gchar *buf = g_malloc(strlen(hentry[H_X_MAILING_LIST].body) + 1);
1360 g_return_val_if_fail(buf != NULL, -1 );
1361 if (1 == sscanf(hentry[H_X_MAILING_LIST].body, "<%[^>]>", buf))
1362 compose->mailinglist = g_strdup(buf);
1364 g_free(hentry[H_X_MAILING_LIST].body);
1365 hentry[H_X_MAILING_LIST].body = NULL ;
1367 if (hentry[H_X_BEENTHERE].body != NULL) {
1368 /* this is good enough to parse the sylpheed-claws lists */
1369 gchar *buf = g_malloc(strlen(hentry[H_X_BEENTHERE].body) + 1);
1370 g_return_val_if_fail(buf != NULL, -1 );
1371 if (1 == sscanf(hentry[H_X_BEENTHERE].body, "%[^>]", buf))
1372 compose->mailinglist = g_strdup(buf);
1374 g_free(hentry[H_X_BEENTHERE].body);
1375 hentry[H_X_BEENTHERE].body = NULL ;
1377 if (hentry[H_REFERENCES].body != NULL) {
1378 if (compose->mode == COMPOSE_REEDIT)
1379 compose->references = hentry[H_REFERENCES].body;
1381 compose->references = compose_parse_references
1382 (hentry[H_REFERENCES].body, msginfo->msgid);
1383 g_free(hentry[H_REFERENCES].body);
1385 hentry[H_REFERENCES].body = NULL;
1387 if (hentry[H_BCC].body != NULL) {
1388 if (compose->mode == COMPOSE_REEDIT) {
1389 conv_unmime_header_overwrite(hentry[H_BCC].body);
1390 compose->bcc = hentry[H_BCC].body;
1392 g_free(hentry[H_BCC].body);
1393 hentry[H_BCC].body = NULL;
1395 if (hentry[H_NEWSGROUPS].body != NULL) {
1396 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1397 hentry[H_NEWSGROUPS].body = NULL;
1399 if (hentry[H_FOLLOWUP_TO].body != NULL) {
1400 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1401 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1402 hentry[H_FOLLOWUP_TO].body = NULL;
1405 if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1406 compose->inreplyto = g_strdup(msginfo->inreplyto);
1407 else if (compose->mode != COMPOSE_REEDIT &&
1408 msginfo->msgid && *msginfo->msgid) {
1409 compose->inreplyto = g_strdup(msginfo->msgid);
1411 if (!compose->references) {
1412 if (msginfo->inreplyto && *msginfo->inreplyto)
1413 compose->references =
1414 g_strdup_printf("<%s>\n\t<%s>",
1418 compose->references =
1419 g_strconcat("<", msginfo->msgid, ">",
1427 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1429 GSList *ref_id_list, *cur;
1433 ref_id_list = references_list_append(NULL, ref);
1434 if (!ref_id_list) return NULL;
1435 if (msgid && *msgid)
1436 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1441 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1442 /* "<" + Message-ID + ">" + CR+LF+TAB */
1443 len += strlen((gchar *)cur->data) + 5;
1445 if (len > MAX_REFERENCES_LEN) {
1446 /* remove second message-ID */
1447 if (ref_id_list && ref_id_list->next &&
1448 ref_id_list->next->next) {
1449 g_free(ref_id_list->next->data);
1450 ref_id_list = g_slist_remove
1451 (ref_id_list, ref_id_list->next->data);
1453 slist_free_strings(ref_id_list);
1454 g_slist_free(ref_id_list);
1461 new_ref = g_string_new("");
1462 for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1463 if (new_ref->len > 0)
1464 g_string_append(new_ref, "\n\t");
1465 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1468 slist_free_strings(ref_id_list);
1469 g_slist_free(ref_id_list);
1471 new_ref_str = new_ref->str;
1472 g_string_free(new_ref, FALSE);
1477 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1478 const gchar *fmt, const gchar *qmark)
1480 GtkSText *text = GTK_STEXT(compose->text);
1481 gchar *quote_str = NULL;
1486 if (qmark != NULL) {
1487 quote_fmt_init(msginfo, NULL);
1488 quote_fmt_scan_string(qmark);
1491 buf = quote_fmt_get_buffer();
1493 alertpanel_error(_("Quote mark format error."));
1495 Xstrdup_a(quote_str, buf, return NULL)
1498 quote_fmt_init(msginfo, quote_str);
1499 quote_fmt_scan_string(fmt);
1502 buf = quote_fmt_get_buffer();
1504 alertpanel_error(_("Message reply/forward format error."));
1508 gtk_stext_freeze(text);
1509 gtk_stext_set_point(text, 0);
1510 gtk_stext_forward_delete(text, gtk_stext_get_length(text));
1512 for (p = buf; *p != '\0'; ) {
1513 lastp = strchr(p, '\n');
1514 len = lastp ? lastp - p + 1 : -1;
1515 gtk_stext_insert(text, NULL, NULL, NULL, p, len);
1522 gtk_stext_thaw(text);
1527 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1528 gboolean to_all, gboolean ignore_replyto,
1529 gboolean followup_and_reply_to)
1534 GHashTable *to_table;
1536 g_return_if_fail(compose->account != NULL);
1537 g_return_if_fail(msginfo != NULL);
1539 if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1540 compose_entry_append(compose,
1541 ((compose->mailinglist && !ignore_replyto)
1542 ? compose->mailinglist
1543 : (compose->replyto && !ignore_replyto)
1545 : msginfo->from ? msginfo->from : ""),
1547 if (compose->account->protocol == A_NNTP) {
1549 compose_entry_append
1550 (compose, msginfo->from ? msginfo->from : "",
1553 compose_entry_append
1555 compose->followup_to ? compose->followup_to
1556 : compose->newsgroups ? compose->newsgroups
1558 COMPOSE_NEWSGROUPS);
1561 if (msginfo->subject && *msginfo->subject) {
1562 gchar *buf, *buf2, *p;
1564 buf = g_strdup(msginfo->subject);
1565 while (!strncasecmp(buf, "Re:", 3)) {
1567 while (isspace(*p)) p++;
1568 memmove(buf, p, strlen(p) + 1);
1571 buf2 = g_strdup_printf("Re: %s", buf);
1572 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1576 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1578 if (!to_all || compose->account->protocol == A_NNTP) return;
1580 from = g_strdup(compose->replyto ? compose->replyto :
1581 msginfo->from ? msginfo->from : "");
1582 extract_address(from);
1584 cc_list = address_list_append(NULL, msginfo->to);
1585 cc_list = address_list_append(cc_list, compose->cc);
1587 to_table = g_hash_table_new(g_str_hash, g_str_equal);
1588 g_hash_table_insert(to_table, from, GINT_TO_POINTER(1));
1589 if (compose->account)
1590 g_hash_table_insert(to_table, compose->account->address,
1591 GINT_TO_POINTER(1));
1593 /* remove address on To: and that of current account */
1594 for (cur = cc_list; cur != NULL; ) {
1595 GSList *next = cur->next;
1597 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1598 cc_list = g_slist_remove(cc_list, cur->data);
1600 g_hash_table_insert(to_table, cur->data, cur);
1604 g_hash_table_destroy(to_table);
1608 for (cur = cc_list; cur != NULL; cur = cur->next)
1609 compose_entry_append(compose, (gchar *)cur->data,
1611 slist_free_strings(cc_list);
1612 g_slist_free(cc_list);
1616 #define SET_ENTRY(entry, str) \
1619 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1622 #define SET_ADDRESS(type, str) \
1625 compose_entry_append(compose, str, type); \
1628 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1630 g_return_if_fail(msginfo != NULL);
1632 SET_ENTRY(subject_entry, msginfo->subject);
1633 SET_ADDRESS(COMPOSE_TO, msginfo->to);
1634 SET_ADDRESS(COMPOSE_CC, compose->cc);
1635 SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1636 SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1638 #if 0 /* NEW COMPOSE GUI */
1640 GtkItemFactory *ifactory;
1641 GtkWidget *menuitem;
1643 ifactory = gtk_item_factory_from_widget(compose->menubar);
1644 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
1645 gtk_check_menu_item_set_active
1646 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1648 if (compose->replyto) {
1649 GtkItemFactory *ifactory;
1650 GtkWidget *menuitem;
1652 ifactory = gtk_item_factory_from_widget(compose->menubar);
1653 menuitem = gtk_item_factory_get_item
1654 (ifactory, "/Message/Reply to");
1655 gtk_check_menu_item_set_active
1656 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1664 static void compose_exec_sig(Compose *compose, gchar *sigfile)
1671 size_t buf_len = 128;
1673 if (strlen(sigfile) < 2)
1676 sigprg = popen(sigfile+1, "r");
1679 buf = g_malloc(buf_len);
1682 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1683 "Unable to insert signature (malloc failed)\n", -1);
1689 while (!feof(sigprg)) {
1690 bzero(buf, buf_len);
1691 fread(buf, buf_len-1, 1, sigprg);
1692 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
1700 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1701 "Can't exec file: ", -1);
1702 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1707 static void compose_insert_sig(Compose *compose)
1711 if (compose->account && compose->account->sig_path)
1712 sigfile = g_strdup(compose->account->sig_path);
1714 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1715 DEFAULT_SIGNATURE, NULL);
1718 if (!is_file_or_fifo_exist(sigfile) & (sigfile[0] != '|')) {
1723 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1724 if (prefs_common.sig_sep) {
1725 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1726 prefs_common.sig_sep, -1);
1727 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1731 if (sigfile[0] == '|')
1733 compose_exec_sig(compose, sigfile);
1737 compose_insert_file(compose, sigfile);
1742 static void compose_insert_file(Compose *compose, const gchar *file)
1744 GtkSText *text = GTK_STEXT(compose->text);
1745 gchar buf[BUFFSIZE];
1749 g_return_if_fail(file != NULL);
1751 if ((fp = fopen(file, "r")) == NULL) {
1752 FILE_OP_ERROR(file, "fopen");
1756 gtk_stext_freeze(text);
1758 while (fgets(buf, sizeof(buf), fp) != NULL) {
1759 /* Strip <CR> if DOS/Windoze file, replace <CR> with <LF> if MAC file */
1761 if (len > 1 && buf[len - 2] == '\r' && buf[len - 1] == '\n') {
1762 buf[len - 2] = '\n';
1763 buf[len - 1] = '\0';
1766 if (buf[len] == '\r')
1770 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1773 gtk_stext_thaw(text);
1778 static void compose_attach_info(Compose * compose, AttachInfo * ainfo,
1779 ContentType cnttype)
1781 gchar *text[N_ATTACH_COLS];
1784 text[COL_MIMETYPE] = ainfo->content_type;
1785 text[COL_SIZE] = to_human_readable(ainfo->size);
1786 text[COL_NAME] = ainfo->name;
1788 row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1789 gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1791 if (cnttype != MIME_MESSAGE_RFC822)
1792 compose_changed_cb(NULL, compose);
1795 static void compose_attach_append_with_type(Compose *compose,
1798 ContentType cnttype)
1803 if (!is_file_exist(file)) {
1804 g_warning(_("File %s doesn't exist\n"), file);
1807 if ((size = get_file_size(file)) < 0) {
1808 g_warning(_("Can't get file size of %s\n"), file);
1812 alertpanel_notice(_("File %s is empty\n"), file);
1815 #if 0 /* NEW COMPOSE GUI */
1816 if (!compose->use_attach) {
1817 GtkItemFactory *ifactory;
1818 GtkWidget *menuitem;
1820 ifactory = gtk_item_factory_from_widget(compose->menubar);
1821 menuitem = gtk_item_factory_get_item(ifactory,
1823 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1827 ainfo = g_new0(AttachInfo, 1);
1828 ainfo->file = g_strdup(file);
1830 if (cnttype == MIME_MESSAGE_RFC822) {
1831 ainfo->encoding = ENC_7BIT;
1832 ainfo->name = g_strdup_printf(_("Message: %s"),
1835 ainfo->encoding = ENC_BASE64;
1836 ainfo->name = g_strdup(g_basename(file));
1839 ainfo->content_type = g_strdup(type);
1842 compose_attach_info(compose, ainfo, cnttype);
1845 static void compose_attach_append(Compose *compose, const gchar *file,
1846 ContentType cnttype)
1849 gchar *text[N_ATTACH_COLS];
1853 if (!is_file_exist(file)) {
1854 g_warning(_("File %s doesn't exist\n"), file);
1857 if ((size = get_file_size(file)) < 0) {
1858 g_warning(_("Can't get file size of %s\n"), file);
1862 alertpanel_notice(_("File %s is empty\n"), file);
1865 #if 0 /* NEW COMPOSE GUI */
1866 if (!compose->use_attach) {
1867 GtkItemFactory *ifactory;
1868 GtkWidget *menuitem;
1870 ifactory = gtk_item_factory_from_widget(compose->menubar);
1871 menuitem = gtk_item_factory_get_item(ifactory,
1873 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1877 ainfo = g_new0(AttachInfo, 1);
1878 ainfo->file = g_strdup(file);
1880 if (cnttype == MIME_MESSAGE_RFC822) {
1881 ainfo->content_type = g_strdup("message/rfc822");
1882 ainfo->encoding = ENC_7BIT;
1883 ainfo->name = g_strdup_printf(_("Message: %s"),
1886 ainfo->content_type = procmime_get_mime_type(file);
1887 if (!ainfo->content_type)
1888 ainfo->content_type =
1889 g_strdup("application/octet-stream");
1890 ainfo->encoding = ENC_BASE64;
1891 ainfo->name = g_strdup(g_basename(file));
1895 compose_attach_info(compose, ainfo, cnttype);
1898 static void compose_wrap_line(Compose *compose)
1900 GtkSText *text = GTK_STEXT(compose->text);
1901 gint ch_len, last_ch_len;
1902 gchar cbuf[MB_LEN_MAX], last_ch;
1906 gint p_start, p_end;
1907 gint line_pos, cur_pos;
1908 gint line_len, cur_len;
1910 #define GET_STEXT(pos) \
1911 if (text->use_wchar) \
1912 ch_len = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, (pos))); \
1914 cbuf[0] = GTK_STEXT_INDEX(text, (pos)); \
1918 gtk_stext_freeze(text);
1920 text_len = gtk_stext_get_length(text);
1922 /* check to see if the point is on the paragraph mark (empty line). */
1923 cur_pos = gtk_stext_get_point(text);
1925 if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
1927 goto compose_end; /* on the paragraph mark */
1928 GET_STEXT(cur_pos - 1);
1929 if (ch_len == 1 && *cbuf == '\n')
1930 goto compose_end; /* on the paragraph mark */
1933 /* find paragraph start. */
1934 line_end = quoted = 0;
1935 for (p_start = cur_pos; p_start >= 0; --p_start) {
1937 if (ch_len == 1 && *cbuf == '\n') {
1939 goto compose_end; /* quoted part */
1946 if (ch_len == 1 && strchr(">:#", *cbuf))
1948 else if (ch_len != 1 || !isspace(*cbuf))
1957 /* find paragraph end. */
1959 for (p_end = cur_pos; p_end < text_len; p_end++) {
1961 if (ch_len == 1 && *cbuf == '\n') {
1968 if (line_end && ch_len == 1 && strchr(">:#", *cbuf))
1969 goto compose_end; /* quoted part */
1974 if (p_end >= text_len)
1977 if (p_start >= p_end)
1980 line_len = cur_len = 0;
1984 for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
1994 if (ch_len == 1 && isspace(*cbuf))
1997 if (ch_len == 1 && *cbuf == '\n') {
1999 if (last_ch_len == 1 && !isspace(last_ch)) {
2000 if (cur_pos + 1 < p_end) {
2001 GET_STEXT(cur_pos + 1);
2002 if (ch_len == 1 && !isspace(*cbuf))
2006 gtk_stext_set_point(text, cur_pos + 1);
2007 gtk_stext_backward_delete(text, 1);
2009 gtk_stext_set_point(text, cur_pos);
2010 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2020 last_ch_len = ch_len;
2024 line_pos = cur_pos + 1;
2025 line_len = cur_len + ch_len;
2028 if (cur_len + ch_len > prefs_common.linewrap_len &&
2032 GET_STEXT(line_pos - 1);
2033 if (ch_len == 1 && isspace(*cbuf)) {
2034 gtk_stext_set_point(text, line_pos);
2035 gtk_stext_backward_delete(text, 1);
2044 gtk_stext_set_point(text, line_pos);
2045 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2049 cur_len = cur_len - line_len + ch_len;
2055 line_pos = cur_pos + 1;
2056 line_len = cur_len + ch_len;
2062 gtk_stext_thaw(text);
2067 /* return indent length */
2068 static guint get_indent_length(GtkSText *text, guint start_pos,
2070 gint indent_len = 0;
2072 gchar cbuf[MB_CUR_MAX];
2074 for (i = start_pos; i < text_len; i++) {
2075 if (text->use_wchar)
2077 (cbuf, (wchar_t)GTK_STEXT_INDEX(text, i));
2079 cbuf[0] = GTK_STEXT_INDEX(text, i);
2084 /* allow space, tab, > or | */
2085 if (cbuf[0] != ' ' && cbuf[0] != '\t' &&
2086 cbuf[0] != '>' && cbuf[0] != '|')
2094 /* insert quotation string when line was wrapped, we know these
2095 are single byte characters */
2096 static guint ins_quote(GtkSText *text, guint quote_len, guint indent_len,
2097 guint prev_line_pos, guint text_len,
2103 for (i = 0; i < indent_len; i++) {
2104 ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2105 gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2107 ins_len = indent_len;
2110 gtk_stext_insert(text, NULL, NULL, NULL, quote_fmt, quote_len);
2111 ins_len = quote_len;
2119 /* Darko: used when I debug wrapping */
2120 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2125 printf("%d [", pos);
2126 for (i = pos; i < tlen; i++)
2128 ch = GTK_STEXT_INDEX(text, i);
2129 if (breakoncr && ch == '\n')
2137 static void compose_wrap_line_all(Compose *compose)
2139 GtkSText *text = GTK_STEXT(compose->text);
2141 guint line_pos = 0, cur_pos = 0, p_pos = 0;
2142 gint line_len = 0, cur_len = 0;
2144 gint is_new_line = 1, do_delete = 0;
2145 guint qlen = 0, i_len = 0;
2146 guint linewrap_quote = prefs_common.linewrap_quote;
2147 guint linewrap_len = prefs_common.linewrap_len;
2148 gchar *qfmt = prefs_common.quotemark;
2149 gchar cbuf[MB_LEN_MAX];
2151 gtk_stext_freeze(text);
2153 /* make text buffer contiguous */
2154 gtk_stext_compact_buffer(text);
2156 tlen = gtk_stext_get_length(text);
2158 for (; cur_pos < tlen; cur_pos++) {
2159 /* mark position of new line - needed for quotation wrap */
2160 if (linewrap_quote && is_new_line) {
2161 qlen = gtkstext_str_strcmp(text, cur_pos, tlen, qfmt);
2164 i_len = get_indent_length(text, cur_pos, tlen);
2170 printf("new line i_len=%d qlen=%d p_pos=", i_len, qlen);
2171 dump_text(text, p_pos, tlen, 1);
2175 /* get character(s) at current position */
2176 if (text->use_wchar)
2178 (cbuf, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
2180 cbuf[0] = GTK_STEXT_INDEX(text, cur_pos);
2184 /* fix line length for tabs */
2185 if (ch_len == 1 && *cbuf == '\t') {
2186 guint tab_width = text->default_tab_width;
2187 guint tab_offset = line_len % tab_width;
2190 printf("found tab at pos=%d line_len=%d ", cur_pos,
2194 line_len += tab_width - tab_offset - 1;
2198 printf("new_len=%d\n", line_len);
2202 /* we have encountered line break */
2203 if (ch_len == 1 && *cbuf == '\n') {
2206 gchar cb[MB_CUR_MAX];
2209 printf("found CR at %d\n", cur_pos);
2211 /* if it's just quotation + newline skip it */
2212 if (i_len && (cur_pos + 1 < tlen)) {
2213 /* check if text at new line matches indent */
2214 ilen = gtkstext_strncmp(text, cur_pos + 1,
2215 p_pos, i_len, tlen);
2216 if (cur_pos + ilen < tlen) {
2217 if (text->use_wchar)
2218 clen = wctomb(cb, (wchar_t)GTK_STEXT_INDEX(text, cur_pos + ilen + 1));
2220 cb[0] = GTK_STEXT_INDEX(text,
2221 cur_pos + ilen + 1);
2224 /* no need to join the lines */
2225 if ((clen == 1) && (cb[0] == '\n'))
2231 printf("qlen=%d l_len=%d wrap_len=%d do_del=%d\n",
2232 qlen, line_len, linewrap_len, do_delete);
2234 /* should we delete to perform smart wrapping */
2235 if (qlen && line_len < linewrap_len && do_delete) {
2236 /* get rid of newline */
2237 gtk_stext_set_point(text, cur_pos);
2238 gtk_stext_forward_delete(text, 1);
2241 /* if text starts with quote fmt or with
2242 indent string, delete them */
2244 ilen = gtkstext_strncmp(text, cur_pos,
2248 gtk_stext_forward_delete(text,
2254 if (gtkstext_str_strcmp(text, cur_pos,
2256 gtk_stext_forward_delete(text,
2262 if (text->use_wchar)
2264 (cb, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
2266 cb[0] = GTK_STEXT_INDEX(text, cur_pos);
2270 /* insert space if it's alphanumeric */
2271 if ((cur_pos != line_pos) &&
2272 ((clen > 1) || isalnum(cb[0]))) {
2273 gtk_stext_insert(text, NULL, NULL,
2275 gtk_stext_compact_buffer(text);
2279 /* and start over with current line */
2280 cur_pos = p_pos - 1;
2282 line_len = cur_len = 0;
2287 printf("after delete l_pos=");
2288 dump_text(text, line_pos, tlen, 1);
2293 /* mark new line beginning */
2294 line_pos = cur_pos + 1;
2295 line_len = cur_len = 0;
2307 /* possible line break */
2308 if (ch_len == 1 && isspace(*cbuf)) {
2309 line_pos = cur_pos + 1;
2310 line_len = cur_len + ch_len;
2313 /* are we over wrapping length set in preferences ? */
2314 if (cur_len + ch_len > linewrap_len) {
2318 printf("should wrap cur_pos=%d ", cur_pos);
2319 dump_text(text, p_pos, tlen, 1);
2320 dump_text(text, line_pos, tlen, 1);
2322 /* force wrapping if it is one long word but not URL */
2323 if (p_pos + i_len == line_pos)
2324 if (!is_url_string(text, line_pos, tlen))
2325 line_pos = cur_pos - 1;
2327 printf("new line_pos=%d\n", line_pos);
2330 /* if next character is space delete it */
2331 if (text->use_wchar)
2332 clen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
2334 cbuf[0] = GTK_STEXT_INDEX(text, line_pos - 1);
2337 if (clen == 1 && isspace(*cbuf)) {
2338 if (p_pos + i_len != line_pos ||
2339 !is_url_string(text, line_pos, tlen)) {
2340 gtk_stext_set_point(text, line_pos);
2341 gtk_stext_backward_delete(text, 1);
2350 /* if it is URL at beginning of line don't wrap */
2351 if (p_pos + i_len == line_pos &&
2352 is_url_string(text, line_pos, tlen)) {
2354 printf("found URL at ");
2355 dump_text(text, line_pos, tlen, 1);
2361 gtk_stext_set_point(text, line_pos);
2362 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2363 gtk_stext_compact_buffer(text);
2367 cur_len = cur_len - line_len + ch_len;
2370 printf("after CR insert ");
2371 dump_text(text, line_pos, tlen, 1);
2374 /* should we insert quotation ? */
2375 if (linewrap_quote && qlen) {
2376 /* only if line is not already quoted */
2377 if (!gtkstext_str_strcmp(text, line_pos,
2381 if (line_pos - p_pos > i_len) {
2382 ins_len = ins_quote(text,
2389 gtk_stext_compact_buffer(text);
2393 /* for loop will increase it */
2394 cur_pos = line_pos - 1;
2397 /* start over with current line */
2401 printf("after quote insert ");
2402 dump_text(text, line_pos, tlen, 1);
2410 line_pos = cur_pos + 1;
2411 line_len = cur_len + ch_len;
2413 /* advance to next character in buffer */
2417 gtk_stext_thaw(text);
2420 static void compose_set_title(Compose *compose)
2425 edited = compose->modified ? _(" [Edited]") : "";
2426 if (compose->account && compose->account->address)
2427 str = g_strdup_printf(_("%s - Compose message%s"),
2428 compose->account->address, edited);
2430 str = g_strdup_printf(_("Compose message%s"), edited);
2431 gtk_window_set_title(GTK_WINDOW(compose->window), str);
2436 * compose_current_mail_account:
2438 * Find a current mail account (the currently selected account, or the
2439 * default account, if a news account is currently selected). If a
2440 * mail account cannot be found, display an error message.
2442 * Return value: Mail account, or NULL if not found.
2444 static PrefsAccount *
2445 compose_current_mail_account(void)
2449 if (cur_account && cur_account->protocol != A_NNTP)
2452 ac = account_get_default();
2453 if (!ac || ac->protocol == A_NNTP) {
2454 alertpanel_error(_("Account for sending mail is not specified.\n"
2455 "Please select a mail account before sending."));
2462 gboolean compose_check_for_valid_recipient(Compose *compose) {
2463 gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
2464 gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
2465 gboolean recipient_found = FALSE;
2469 /* free to and newsgroup list */
2470 slist_free_strings(compose->to_list);
2471 g_slist_free(compose->to_list);
2472 compose->to_list = NULL;
2474 slist_free_strings(compose->newsgroup_list);
2475 g_slist_free(compose->newsgroup_list);
2476 compose->newsgroup_list = NULL;
2478 /* search header entries for to and newsgroup entries */
2479 for(list = compose->header_list; list; list = list->next) {
2482 header = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry));
2483 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
2485 if(entry[0] != '\0') {
2486 for(strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
2487 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2488 compose->to_list = address_list_append(compose->to_list, entry);
2489 recipient_found = TRUE;
2492 for(strptr = recipient_headers_news; *strptr != NULL; strptr++) {
2493 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2494 compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
2495 recipient_found = TRUE;
2501 return recipient_found;
2504 gint compose_send(Compose *compose)
2510 val = compose_queue(compose, &msgnum, &folder);
2512 alertpanel_error(_("Could not queue message for sending"));
2516 val = procmsg_send_message_queue(folder_item_fetch_msg(folder, msgnum));
2518 folder_item_remove_msg(folder, msgnum);
2519 folderview_update_item(folder, TRUE);
2525 #if 0 /* compose restructure */
2526 gint compose_send(Compose *compose)
2528 gchar tmp[MAXPATHLEN + 1];
2530 static gboolean lock = FALSE;
2534 g_return_val_if_fail(compose->account != NULL, -1);
2535 g_return_val_if_fail(compose->orig_account != NULL, -1);
2539 if(!compose_check_for_valid_recipient(compose)) {
2540 alertpanel_error(_("Recipient is not specified."));
2545 /* write to temporary file */
2546 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
2547 get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
2549 if (prefs_common.linewrap_at_send)
2550 compose_wrap_line_all(compose);
2552 if (compose_write_to_file(compose, tmp, FALSE) < 0) {
2557 if (!compose->to_list && !compose->newsgroup_list) {
2558 g_warning(_("can't get recipient list."));
2564 if (compose->to_list) {
2567 #if 0 /* NEW COMPOSE GUI */
2568 if (compose->account->protocol != A_NNTP)
2569 ac = compose->account;
2570 else if (compose->orig_account->protocol != A_NNTP)
2571 ac = compose->orig_account;
2572 else if (cur_account && cur_account->protocol != A_NNTP)
2575 ac = compose_current_mail_account();
2583 ac = compose->account;
2585 ok = send_message(tmp, ac, compose->to_list);
2586 statusbar_pop_all();
2589 if (ok == 0 && compose->newsgroup_list) {
2592 if (compose->account->protocol == A_NNTP)
2593 folder = FOLDER(compose->account->folder);
2595 folder = FOLDER(compose->orig_account->folder);
2597 ok = news_post(folder, tmp);
2599 alertpanel_error(_("Error occurred while posting the message to %s ."),
2600 compose->account->nntp_server);
2607 /* queue message if failed to send */
2609 if (prefs_common.queue_msg) {
2614 _("Error occurred while sending the message.\n"
2615 "Put this message into queue folder?"),
2616 _("OK"), _("Cancel"), NULL);
2617 if (G_ALERTDEFAULT == val) {
2618 ok = compose_queue(compose, tmp);
2620 alertpanel_error(_("Can't queue the message."));
2623 alertpanel_error(_("Error occurred while sending the message."));
2625 if (compose->mode == COMPOSE_REEDIT) {
2626 compose_remove_reedit_target(compose);
2627 if (compose->targetinfo)
2628 folderview_update_item
2629 (compose->targetinfo->folder, TRUE);
2633 /* save message to outbox */
2634 if (ok == 0 && prefs_common.savemsg) {
2635 if (compose_save_to_outbox(compose, tmp) < 0)
2637 (_("Can't save the message to outbox."));
2646 static gboolean compose_use_attach(Compose *compose) {
2647 return(gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL);
2650 static gint compose_bounce_write_headers_from_headerlist(Compose *compose,
2653 gchar buf[BUFFSIZE];
2655 gboolean first_address;
2657 ComposeHeaderEntry *headerentry;
2658 gchar *headerentryname;
2659 gchar *header_w_colon;
2663 debug_print(_("Writing bounce header\n"));
2665 header_w_colon = g_strconcat("To:", NULL);
2666 to_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2667 header_w_colon = g_strconcat("Cc:", NULL);
2668 cc_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2670 first_address = TRUE;
2671 for(list = compose->header_list; list; list = list->next) {
2672 headerentry = ((ComposeHeaderEntry *)list->data);
2673 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
2675 if(g_strcasecmp(headerentryname, cc_hdr) == 0
2676 || g_strcasecmp(headerentryname, to_hdr) == 0) {
2677 str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
2678 Xstrdup_a(str, str, return -1);
2680 if(str[0] != '\0') {
2681 compose_convert_header
2682 (buf, sizeof(buf), str,
2683 strlen("Resent-To") + 2);
2685 fprintf(fp, "Resent-To: ");
2686 first_address = FALSE;
2690 fprintf(fp, "%s", buf);
2694 /* if(!first_address) { */
2701 static gint compose_bounce_write_headers(Compose *compose, FILE *fp)
2703 gchar buf[BUFFSIZE];
2705 /* struct utsname utsbuf; */
2707 g_return_val_if_fail(fp != NULL, -1);
2708 g_return_val_if_fail(compose->account != NULL, -1);
2709 g_return_val_if_fail(compose->account->address != NULL, -1);
2712 get_rfc822_date(buf, sizeof(buf));
2713 fprintf(fp, "Resent-Date: %s\n", buf);
2716 if (compose->account->name && *compose->account->name) {
2717 compose_convert_header
2718 (buf, sizeof(buf), compose->account->name,
2720 fprintf(fp, "Resent-From: %s <%s>\n",
2721 buf, compose->account->address);
2723 fprintf(fp, "Resent-From: %s\n", compose->account->address);
2726 compose_bounce_write_headers_from_headerlist(compose, fp);
2728 /* separator between header and body */
2734 static gint compose_bounce_write_to_file(Compose *compose, const gchar *file)
2739 gchar buf[BUFFSIZE];
2741 if ((fp = fopen(compose->bounce_filename, "r")) == NULL) {
2742 FILE_OP_ERROR(file, "fopen");
2746 if ((fdest = fopen(file, "a+")) == NULL) {
2747 FILE_OP_ERROR(file, "fopen");
2752 /* chmod for security */
2753 if (change_file_mode_rw(fdest, file) < 0) {
2754 FILE_OP_ERROR(file, "chmod");
2755 g_warning(_("can't change file mode\n"));
2758 while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
2759 /* should filter returnpath, delivered-to */
2760 if ((g_strncasecmp(buf, "Return-Path:",
2761 strlen("Return-Path:")) == 0)
2762 || (g_strncasecmp(buf, "Delivered-To:",
2763 strlen("Delivered-To:")) == 0))
2765 if (fputs(buf, fdest) == -1)
2767 if (fputs("\n", fdest) == -1)
2771 compose_bounce_write_headers(compose, fdest);
2773 while ((len = fread(buf, sizeof(gchar), BUFFSIZE, fp)) > 0) {
2774 if (fwrite(buf, sizeof(gchar), len, fdest) == -1)
2789 static gint compose_write_to_file(Compose *compose, const gchar *file,
2796 const gchar *out_codeset;
2797 EncodingType encoding;
2799 if ((fp = fopen(file, "a+")) == NULL) {
2800 FILE_OP_ERROR(file, "fopen");
2804 /* chmod for security */
2805 if (change_file_mode_rw(fp, file) < 0) {
2806 FILE_OP_ERROR(file, "chmod");
2807 g_warning(_("can't change file mode\n"));
2810 /* get all composed text */
2811 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2812 len = strlen(chars);
2813 if (is_ascii_str(chars)) {
2814 buf = g_strdup(chars);
2815 out_codeset = CS_US_ASCII;
2816 encoding = ENC_7BIT;
2818 const gchar *src_codeset;
2820 out_codeset = conv_get_outgoing_charset_str();
2821 if (!strcasecmp(out_codeset, CS_US_ASCII))
2822 out_codeset = CS_ISO_8859_1;
2823 encoding = procmime_get_encoding_for_charset(out_codeset);
2825 src_codeset = conv_get_current_charset_str();
2826 /* if current encoding is US-ASCII, set it the same as
2827 outgoing one to prevent code conversion failure */
2828 if (!strcasecmp(src_codeset, CS_US_ASCII))
2829 src_codeset = out_codeset;
2831 debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
2832 src_codeset, out_codeset, procmime_get_encoding_str(encoding));
2834 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
2839 alertpanel_error(_("Can't convert the codeset of the message."));
2846 if (compose_write_headers
2847 (compose, fp, out_codeset, encoding, is_draft) < 0) {
2848 g_warning(_("can't write headers\n"));
2855 if (compose_use_attach(compose)) {
2857 /* This prolog message is ignored by mime software and
2858 * because it would make our signing/encryption task
2859 * tougher, we don't emit it in that case */
2860 if (!compose->use_signing && !compose->use_encryption)
2862 fputs("This is a multi-part message in MIME format.\n", fp);
2864 fprintf(fp, "\n--%s\n", compose->boundary);
2865 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
2867 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2868 procmime_get_encoding_str(encoding));
2874 if (encoding == ENC_BASE64) {
2875 gchar outbuf[B64_BUFFSIZE];
2878 for (i = 0; i < len; i += B64_LINE_SIZE) {
2879 l = MIN(B64_LINE_SIZE, len - i);
2880 to64frombits(outbuf, buf + i, l);
2884 } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
2885 FILE_OP_ERROR(file, "fwrite");
2893 if (compose_use_attach(compose))
2894 compose_write_attach(compose, fp);
2896 if (fclose(fp) == EOF) {
2897 FILE_OP_ERROR(file, "fclose");
2903 if (compose->use_signing) {
2904 if (rfc2015_sign(file, compose->account) < 0) {
2909 if (compose->use_encryption) {
2910 if (rfc2015_encrypt(file, compose->to_list) < 0) {
2915 #endif /* USE_GPGME */
2920 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
2926 if ((fp = fopen(file, "w")) == NULL) {
2927 FILE_OP_ERROR(file, "fopen");
2931 /* chmod for security */
2932 if (change_file_mode_rw(fp, file) < 0) {
2933 FILE_OP_ERROR(file, "chmod");
2934 g_warning(_("can't change file mode\n"));
2937 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2940 len = strlen(chars);
2941 if (fwrite(chars, sizeof(gchar), len, fp) != len) {
2942 FILE_OP_ERROR(file, "fwrite");
2951 if (fclose(fp) == EOF) {
2952 FILE_OP_ERROR(file, "fclose");
2959 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
2966 debug_print(_("saving sent message...\n"));
2968 outbox = folder_get_default_outbox();
2969 path = folder_item_get_path(outbox);
2970 if (!is_dir_exist(path))
2971 make_dir_hier(path);
2973 folder_item_scan(outbox);
2974 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
2976 g_warning(_("can't save message\n"));
2980 if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
2981 g_warning(_("can't open mark file\n"));
2985 newmsginfo.msgnum = num;
2986 newmsginfo.flags.perm_flags = 0;
2987 newmsginfo.flags.tmp_flags = 0;
2988 procmsg_write_flags(&newmsginfo, fp);
2996 static gint compose_remove_reedit_target(Compose *compose)
2999 MsgInfo *msginfo = compose->targetinfo;
3001 g_return_val_if_fail(compose->mode == COMPOSE_REEDIT, -1);
3002 if (!msginfo) return -1;
3004 item = msginfo->folder;
3005 g_return_val_if_fail(item != NULL, -1);
3007 folder_item_scan(item);
3008 if (procmsg_msg_exist(msginfo) &&
3009 (item->stype == F_DRAFT || item->stype == F_QUEUE)) {
3010 if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
3011 g_warning(_("can't remove the old message\n"));
3020 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
3023 gchar *tmpfilename, *queue_path;
3026 gchar buf[BUFFSIZE];
3028 static gboolean lock = FALSE;
3029 PrefsAccount *mailac = NULL, *newsac = NULL;
3031 debug_print(_("queueing message...\n"));
3032 g_return_val_if_fail(compose->account != NULL, -1);
3033 g_return_val_if_fail(compose->orig_account != NULL, -1);
3037 if(!compose_check_for_valid_recipient(compose)) {
3038 alertpanel_error(_("Recipient is not specified."));
3043 if (!compose->to_list && !compose->newsgroup_list) {
3044 g_warning(_("can't get recipient list."));
3049 if (prefs_common.linewrap_at_send)
3050 compose_wrap_line_all(compose);
3052 /* write to temporary file */
3053 tmpfilename = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
3054 G_DIR_SEPARATOR, (gint)compose);
3055 if ((fp = fopen(tmpfilename, "w")) == NULL) {
3056 FILE_OP_ERROR(tmpfilename, "fopen");
3057 g_free(tmpfilename);
3060 if (change_file_mode_rw(fp, tmpfilename) < 0) {
3061 FILE_OP_ERROR(tmpfilename, "chmod");
3062 g_warning(_("can't change file mode\n"));
3065 if(compose->to_list) {
3066 if (compose->account->protocol != A_NNTP)
3067 mailac = compose->account;
3068 else if (compose->orig_account->protocol != A_NNTP)
3069 mailac = compose->orig_account;
3070 else if (cur_account && cur_account->protocol != A_NNTP)
3071 mailac = cur_account;
3072 else if (!(mailac = compose_current_mail_account())) {
3073 unlink(tmpfilename);
3075 alertpanel_error(_("No account for sending mails available!"));
3080 if(compose->newsgroup_list) {
3081 if (compose->account->protocol == A_NNTP)
3082 newsac = compose->account;
3083 else if(!(newsac = compose->orig_account) || (newsac->protocol != A_NNTP)) {
3084 unlink(tmpfilename);
3086 alertpanel_error(_("No account for posting news available!"));
3091 /* queueing variables */
3092 fprintf(fp, "AF:\n");
3093 fprintf(fp, "NF:0\n");
3094 fprintf(fp, "PS:10\n");
3095 fprintf(fp, "SRH:1\n");
3096 fprintf(fp, "SFN:\n");
3097 fprintf(fp, "DSR:\n");
3099 fprintf(fp, "MID:<%s>\n", compose->msgid);
3101 fprintf(fp, "MID:\n");
3102 fprintf(fp, "CFG:\n");
3103 fprintf(fp, "PT:0\n");
3104 fprintf(fp, "S:%s\n", compose->account->address);
3105 fprintf(fp, "RQ:\n");
3107 fprintf(fp, "SSV:%s\n", mailac->smtp_server);
3109 fprintf(fp, "SSV:\n");
3111 fprintf(fp, "NSV:%s\n", newsac->nntp_server);
3113 fprintf(fp, "NSV:\n");
3114 fprintf(fp, "SSH:\n");
3115 /* write recepient list */
3117 if(compose->to_list) {
3118 fprintf(fp, "<%s>", (gchar *)compose->to_list->data);
3119 for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
3120 fprintf(fp, ",<%s>", (gchar *)cur->data);
3123 /* write newsgroup list */
3125 if(compose->newsgroup_list) {
3126 fprintf(fp, "%s", (gchar *)compose->newsgroup_list->data);
3127 for (cur = compose->newsgroup_list->next; cur != NULL; cur = cur->next)
3128 fprintf(fp, ",%s", (gchar *)cur->data);
3131 /* Sylpheed account IDs */
3133 fprintf(fp, "MAID:%d\n", mailac->account_id);
3136 fprintf(fp, "NAID:%d\n", newsac->account_id);
3141 if (compose->bounce_filename != NULL) {
3142 if (compose_bounce_write_to_file(compose, tmpfilename) < 0) {
3143 unlink(tmpfilename);
3149 if (compose_write_to_file(compose, tmpfilename, FALSE) < 0) {
3150 unlink(tmpfilename);
3157 queue = folder_get_default_queue();
3159 folder_item_scan(queue);
3160 queue_path = folder_item_get_path(queue);
3161 if (!is_dir_exist(queue_path))
3162 make_dir_hier(queue_path);
3163 if ((num = folder_item_add_msg(queue, tmpfilename, TRUE)) < 0) {
3164 g_warning(_("can't queue the message\n"));
3165 unlink(tmpfilename);
3166 g_free(tmpfilename);
3170 g_free(tmpfilename);
3172 if (compose->mode == COMPOSE_REEDIT) {
3173 compose_remove_reedit_target(compose);
3174 if (compose->targetinfo &&
3175 compose->targetinfo->folder != queue)
3176 folderview_update_item
3177 (compose->targetinfo->folder, TRUE);
3180 if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
3181 g_warning(_("can't open mark file\n"));
3185 newmsginfo.msgnum = num;
3186 newmsginfo.flags.perm_flags = 0;
3187 newmsginfo.flags.tmp_flags = 0;
3188 procmsg_write_flags(&newmsginfo, fp);
3193 folder_item_scan(queue);
3194 folderview_update_item(queue, TRUE);
3196 if((msgnum != NULL) && (item != NULL)) {
3204 static void compose_write_attach(Compose *compose, FILE *fp)
3207 GtkCList *clist = GTK_CLIST(compose->attach_clist);
3210 gchar filename[BUFFSIZE];
3213 for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
3215 if ((attach_fp = fopen(ainfo->file, "r")) == NULL) {
3216 g_warning(_("Can't open file %s\n"), ainfo->file);
3220 fprintf(fp, "\n--%s\n", compose->boundary);
3222 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
3223 fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
3224 fprintf(fp, "Content-Disposition: inline\n");
3226 conv_encode_header(filename, sizeof(filename),
3228 fprintf(fp, "Content-Type: %s;\n"
3230 ainfo->content_type, filename);
3231 fprintf(fp, "Content-Disposition: attachment;\n"
3232 " filename=\"%s\"\n", filename);
3235 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
3236 procmime_get_encoding_str(ainfo->encoding));
3238 if (ainfo->encoding == ENC_7BIT) {
3239 gchar buf[BUFFSIZE];
3241 while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
3246 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
3248 while ((len = fread(inbuf, sizeof(gchar),
3249 B64_LINE_SIZE, attach_fp))
3251 to64frombits(outbuf, inbuf, B64_LINE_SIZE);
3255 if (len > 0 && feof(attach_fp)) {
3256 to64frombits(outbuf, inbuf, len);
3265 fprintf(fp, "\n--%s--\n", compose->boundary);
3268 #define IS_IN_CUSTOM_HEADER(header) \
3269 (compose->account->add_customhdr && \
3270 custom_header_find(compose->account->customhdr_list, header) != NULL)
3272 static gint compose_write_headers_from_headerlist(Compose *compose,
3276 gchar buf[BUFFSIZE];
3277 gchar *str, *header_w_colon, *trans_hdr;
3278 gboolean first_address;
3280 ComposeHeaderEntry *headerentry;
3281 gchar * headerentryname;
3283 if (IS_IN_CUSTOM_HEADER(header)) {
3287 debug_print(_("Writing %s-header\n"), header);
<