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"
106 # include "rfc2015.h"
116 #define N_ATTACH_COLS 3
118 #define B64_LINE_SIZE 57
119 #define B64_BUFFSIZE 77
121 #define MAX_REFERENCES_LEN 999
123 static GdkColor quote_color = {0, 0, 0, 0xbfff};
125 static GList *compose_list = NULL;
127 static void compose_set_undo (UndoMain *undostruct,
130 GtkWidget *changewidget);
131 Compose *compose_generic_new (PrefsAccount *account,
135 static Compose *compose_create (PrefsAccount *account,
137 static void compose_toolbar_create (Compose *compose,
138 GtkWidget *container);
139 static GtkWidget *compose_account_option_menu_create
141 static void compose_set_template_menu (Compose *compose);
142 static void compose_template_apply (Compose *compose,
144 static void compose_destroy (Compose *compose);
146 static void compose_entries_set (Compose *compose,
147 const gchar *mailto);
148 static gint compose_parse_header (Compose *compose,
150 static gchar *compose_parse_references (const gchar *ref,
153 static gchar *compose_quote_fmt (Compose *compose,
158 static void compose_reply_set_entry (Compose *compose,
163 followup_and_reply_to);
164 static void compose_reedit_set_entry (Compose *compose,
166 static void compose_insert_sig (Compose *compose);
167 static void compose_insert_file (Compose *compose,
169 static void compose_attach_append (Compose *compose,
171 ContentType cnttype);
172 static void compose_attach_append_with_type(Compose *compose,
175 ContentType cnttype);
176 static void compose_wrap_line (Compose *compose);
177 static void compose_wrap_line_all (Compose *compose);
178 static void compose_set_title (Compose *compose);
180 static PrefsAccount *compose_current_mail_account(void);
181 /* static gint compose_send (Compose *compose); */
182 static gint compose_write_to_file (Compose *compose,
185 static gint compose_write_body_to_file (Compose *compose,
187 static gint compose_save_to_outbox (Compose *compose,
189 static gint compose_remove_reedit_target (Compose *compose);
190 static gint compose_queue (Compose *compose,
193 static void compose_write_attach (Compose *compose,
195 static gint compose_write_headers (Compose *compose,
197 const gchar *charset,
198 EncodingType encoding,
201 static void compose_convert_header (gchar *dest,
205 static void compose_generate_msgid (Compose *compose,
209 static void compose_attach_info_free (AttachInfo *ainfo);
210 static void compose_attach_remove_selected (Compose *compose);
212 static void compose_attach_property (Compose *compose);
213 static void compose_attach_property_create (gboolean *cancelled);
214 static void attach_property_ok (GtkWidget *widget,
215 gboolean *cancelled);
216 static void attach_property_cancel (GtkWidget *widget,
217 gboolean *cancelled);
218 static gint attach_property_delete_event (GtkWidget *widget,
220 gboolean *cancelled);
221 static void attach_property_key_pressed (GtkWidget *widget,
223 gboolean *cancelled);
225 static void compose_exec_ext_editor (Compose *compose);
226 static gint compose_exec_ext_editor_real (const gchar *file);
227 static gboolean compose_ext_editor_kill (Compose *compose);
228 static void compose_input_cb (gpointer data,
230 GdkInputCondition condition);
231 static void compose_set_ext_editor_sensitive (Compose *compose,
234 static gint calc_cursor_xpos (GtkSText *text,
238 static void compose_create_header_entry (Compose *compose);
239 static void compose_add_header_entry (Compose *compose, gchar *header, gchar *text);
241 /* callback functions */
243 static gboolean compose_edit_size_alloc (GtkEditable *widget,
244 GtkAllocation *allocation,
245 GtkSHRuler *shruler);
247 static void toolbar_send_cb (GtkWidget *widget,
249 static void toolbar_send_later_cb (GtkWidget *widget,
251 static void toolbar_draft_cb (GtkWidget *widget,
253 static void toolbar_insert_cb (GtkWidget *widget,
255 static void toolbar_attach_cb (GtkWidget *widget,
257 static void toolbar_sig_cb (GtkWidget *widget,
259 static void toolbar_ext_editor_cb (GtkWidget *widget,
261 static void toolbar_linewrap_cb (GtkWidget *widget,
263 static void toolbar_address_cb (GtkWidget *widget,
266 static void select_account (Compose *compose,
269 static void account_activated (GtkMenuItem *menuitem,
272 static void attach_selected (GtkCList *clist,
277 static void attach_button_pressed (GtkWidget *widget,
278 GdkEventButton *event,
280 static void attach_key_pressed (GtkWidget *widget,
284 static void compose_send_cb (gpointer data,
287 static void compose_send_later_cb (gpointer data,
291 static void compose_draft_cb (gpointer data,
295 static void compose_attach_cb (gpointer data,
298 static void compose_insert_file_cb (gpointer data,
302 static void compose_close_cb (gpointer data,
306 static void compose_address_cb (gpointer data,
309 static void compose_template_activate_cb(GtkWidget *widget,
312 static void compose_ext_editor_cb (gpointer data,
316 static gint compose_delete_cb (GtkWidget *widget,
319 static void compose_destroy_cb (GtkWidget *widget,
322 static void compose_undo_cb (Compose *compose);
323 static void compose_redo_cb (Compose *compose);
324 static void compose_cut_cb (Compose *compose);
325 static void compose_copy_cb (Compose *compose);
326 static void compose_paste_cb (Compose *compose);
327 static void compose_allsel_cb (Compose *compose);
329 static void compose_grab_focus_cb (GtkWidget *widget,
332 static void compose_changed_cb (GtkEditable *editable,
334 static void compose_button_press_cb (GtkWidget *widget,
335 GdkEventButton *event,
338 static void compose_key_press_cb (GtkWidget *widget,
344 static void compose_toggle_to_cb (gpointer data,
347 static void compose_toggle_cc_cb (gpointer data,
350 static void compose_toggle_bcc_cb (gpointer data,
353 static void compose_toggle_replyto_cb (gpointer data,
356 static void compose_toggle_followupto_cb(gpointer data,
359 static void compose_toggle_attach_cb (gpointer data,
363 static void compose_toggle_ruler_cb (gpointer data,
367 static void compose_toggle_sign_cb (gpointer data,
370 static void compose_toggle_encrypt_cb (gpointer data,
374 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
377 static void compose_attach_drag_received_cb (GtkWidget *widget,
378 GdkDragContext *drag_context,
381 GtkSelectionData *data,
385 static void compose_insert_drag_received_cb (GtkWidget *widget,
386 GdkDragContext *drag_context,
389 GtkSelectionData *data,
395 static void to_activated (GtkWidget *widget,
397 static void newsgroups_activated (GtkWidget *widget,
399 static void subject_activated (GtkWidget *widget,
401 static void cc_activated (GtkWidget *widget,
403 static void bcc_activated (GtkWidget *widget,
405 static void replyto_activated (GtkWidget *widget,
407 static void followupto_activated (GtkWidget *widget,
411 static void compose_attach_parts (Compose *compose,
414 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
416 gboolean ignore_replyto,
417 gboolean followup_and_reply_to);
419 void compose_headerentry_changed_cb (GtkWidget *entry,
420 ComposeHeaderEntry *headerentry);
421 void compose_headerentry_key_press_event_cb(GtkWidget *entry,
423 ComposeHeaderEntry *headerentry);
425 static GtkItemFactoryEntry compose_popup_entries[] =
427 {N_("/_Add..."), NULL, compose_attach_cb, 0, NULL},
428 {N_("/_Remove"), NULL, compose_attach_remove_selected, 0, NULL},
429 {N_("/---"), NULL, NULL, 0, "<Separator>"},
430 {N_("/_Property..."), NULL, compose_attach_property, 0, NULL}
433 static GtkItemFactoryEntry compose_entries[] =
435 {N_("/_File"), NULL, NULL, 0, "<Branch>"},
436 {N_("/_File/_Attach file"), "<control>M", compose_attach_cb, 0, NULL},
437 {N_("/_File/_Insert file"), "<control>I", compose_insert_file_cb, 0, NULL},
438 {N_("/_File/Insert si_gnature"), "<control>G", compose_insert_sig, 0, NULL},
439 {N_("/_File/---"), NULL, NULL, 0, "<Separator>"},
440 {N_("/_File/_Close"), "<alt>W", compose_close_cb, 0, NULL},
442 {N_("/_Edit"), NULL, NULL, 0, "<Branch>"},
443 {N_("/_Edit/_Undo"), "<control>Z", compose_undo_cb, 0, NULL},
444 {N_("/_Edit/_Redo"), "<control>Y", compose_redo_cb, 0, NULL},
445 {N_("/_Edit/---"), NULL, NULL, 0, "<Separator>"},
446 {N_("/_Edit/Cu_t"), "<control>X", compose_cut_cb, 0, NULL},
447 {N_("/_Edit/_Copy"), "<control>C", compose_copy_cb, 0, NULL},
448 {N_("/_Edit/_Paste"), "<control>V", compose_paste_cb, 0, NULL},
449 {N_("/_Edit/Select _all"), "<control>A", compose_allsel_cb, 0, NULL},
450 {N_("/_Edit/---"), NULL, NULL, 0, "<Separator>"},
451 {N_("/_Edit/_Wrap current paragraph"), "<alt>L", compose_wrap_line, 0, NULL},
452 {N_("/_Edit/Wrap all long _lines"),
453 "<shift><alt>L", compose_wrap_line_all, 0, NULL},
454 {N_("/_Edit/Edit with e_xternal editor"),
455 "<alt>X", compose_ext_editor_cb, 0, NULL},
457 {N_("/_Message"), NULL, NULL, 0, "<Branch>"},
458 {N_("/_Message/_Send"), "<control>Return",
459 compose_send_cb, 0, NULL},
460 {N_("/_Message/Send _later"), "<shift><alt>S",
461 compose_send_later_cb, 0, NULL},
462 {N_("/_Message/Save to _draft folder"),
463 "<alt>D", compose_draft_cb, 0, NULL},
464 #if 0 /* NEW COMPOSE GUI */
465 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
466 {N_("/_Message/_To"), NULL, compose_toggle_to_cb , 0, "<ToggleItem>"},
467 {N_("/_Message/_Cc"), NULL, compose_toggle_cc_cb , 0, "<ToggleItem>"},
468 {N_("/_Message/_Bcc"), NULL, compose_toggle_bcc_cb , 0, "<ToggleItem>"},
469 {N_("/_Message/_Reply to"), NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
470 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
471 {N_("/_Message/_Followup to"), NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
472 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
473 {N_("/_Message/_Attach"), NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
476 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
477 {N_("/_Message/Si_gn"), NULL, compose_toggle_sign_cb , 0, "<ToggleItem>"},
478 {N_("/_Message/_Encrypt"), NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
479 #endif /* USE_GPGME */
480 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
481 {N_("/_Message/_Request Return Receipt"), NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
482 {N_("/_Tool"), NULL, NULL, 0, "<Branch>"},
483 {N_("/_Tool/Show _ruler"), NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
484 {N_("/_Tool/_Address book"), "<alt>A", compose_address_cb , 0, NULL},
485 {N_("/_Tool/_Template"), NULL, NULL, 0, "<Branch>"},
486 {N_("/_Help"), NULL, NULL, 0, "<LastBranch>"},
487 {N_("/_Help/_About"), NULL, about_show, 0, NULL}
490 static GtkTargetEntry compose_mime_types[] =
492 {"text/uri-list", 0, 0}
495 Compose *compose_new(PrefsAccount *account)
497 return compose_generic_new(account, NULL, NULL);
500 Compose *compose_bounce(PrefsAccount *account, MsgInfo *msginfo)
504 GtkItemFactory *ifactory;
506 c = compose_generic_new(account, NULL, NULL);
508 filename = procmsg_get_message_file(msginfo);
509 if (filename == NULL)
512 c->bounce_filename = filename;
514 if (msginfo->subject)
515 gtk_entry_set_text(GTK_ENTRY(c->subject_entry),
517 gtk_editable_set_editable(GTK_EDITABLE(c->subject_entry), FALSE);
519 compose_quote_fmt(c, msginfo, "%M", NULL);
520 gtk_editable_set_editable(GTK_EDITABLE(c->text), FALSE);
522 ifactory = gtk_item_factory_from_widget(c->popupmenu);
523 menu_set_sensitive(ifactory, "/Add...", FALSE);
524 menu_set_sensitive(ifactory, "/Remove", FALSE);
525 menu_set_sensitive(ifactory, "/Property...", FALSE);
527 ifactory = gtk_item_factory_from_widget(c->menubar);
528 menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
529 menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
530 menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
531 menu_set_sensitive(ifactory, "/Edit/Paste", FALSE);
532 menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", FALSE);
533 menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", FALSE);
534 menu_set_sensitive(ifactory, "/Edit/Edit with external editor", FALSE);
535 menu_set_sensitive(ifactory, "/Message/Attach", FALSE);
537 menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
538 menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
540 menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
541 menu_set_sensitive(ifactory, "/Tool/Template", FALSE);
543 gtk_widget_set_sensitive(c->insert_btn, FALSE);
544 gtk_widget_set_sensitive(c->attach_btn, FALSE);
545 gtk_widget_set_sensitive(c->sig_btn, FALSE);
546 gtk_widget_set_sensitive(c->exteditor_btn, FALSE);
547 gtk_widget_set_sensitive(c->linewrap_btn, FALSE);
552 Compose *compose_new_with_recipient(PrefsAccount *account, const gchar *mailto)
554 return compose_generic_new(account, mailto, NULL);
557 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
559 return compose_generic_new(account, NULL, item);
562 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item)
566 if (item && item->prefs && item->prefs->enable_default_account)
567 account = account_find_from_id(item->prefs->default_account);
569 if (!account) account = cur_account;
570 g_return_val_if_fail(account != NULL, NULL);
572 compose = compose_create(account, COMPOSE_NEW);
573 compose->replyinfo = NULL;
575 if (prefs_common.auto_sig)
576 compose_insert_sig(compose);
577 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
578 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
580 if (account->protocol != A_NNTP) {
582 compose_entries_set(compose, mailto);
584 } else if(item && item->prefs->enable_default_to) {
585 compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
587 if (item && item->ret_rcpt) {
588 GtkItemFactory *ifactory;
590 ifactory = gtk_item_factory_from_widget(compose->menubar);
591 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
595 compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
598 gtk_widget_grab_focus(compose->subject_entry);
600 if (prefs_common.auto_exteditor)
601 compose_exec_ext_editor(compose);
606 #define CHANGE_FLAGS(msginfo) \
608 if (msginfo->folder->folder->change_flags != NULL) \
609 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
615 Compose *compose_new_followup_and_replyto(PrefsAccount *account,
616 const gchar *followupto, gchar * to)
620 if (!account) account = cur_account;
621 g_return_val_if_fail(account != NULL, NULL);
622 g_return_val_if_fail(account->protocol != A_NNTP, NULL);
624 compose = compose_create(account, COMPOSE_NEW);
626 if (prefs_common.auto_sig)
627 compose_insert_sig(compose);
628 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
629 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
631 compose_entry_append(compose, to, COMPOSE_TO);
632 compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
633 gtk_widget_grab_focus(compose->subject_entry);
639 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
640 gboolean ignore_replyto)
642 compose_generic_reply(msginfo, quote, to_all, ignore_replyto, FALSE);
645 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
647 gboolean ignore_replyto)
649 compose_generic_reply(msginfo, quote, to_all, ignore_replyto, TRUE);
652 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
654 gboolean ignore_replyto,
655 gboolean followup_and_reply_to)
658 PrefsAccount *account;
659 PrefsAccount *reply_account;
662 g_return_if_fail(msginfo != NULL);
663 g_return_if_fail(msginfo->folder != NULL);
666 /* select the account set in folderitem's property (if enabled) */
667 if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
668 account = account_find_from_id(msginfo->folder->prefs->default_account);
670 /* select the account for the whole folder (IMAP / NNTP) */
672 account = msginfo->folder->folder->account;
674 /* select account by to: and cc: header if enabled */
675 if (prefs_common.reply_account_autosel) {
676 if (!account && msginfo->to) {
678 Xstrdup_a(to, msginfo->to, return);
680 account = account_find_from_address(to);
684 if(!get_header_from_msginfo(msginfo, cc, sizeof(cc), "CC:")) { /* Found a CC header */
686 account = account_find_from_address(cc);
691 /* select current account */
692 if (!account) account = cur_account;
693 g_return_if_fail(account != NULL);
695 if (ignore_replyto && account->protocol == A_NNTP &&
696 !followup_and_reply_to) {
698 account_find_from_address(account->address);
700 reply_account = compose_current_mail_account();
704 reply_account = account;
706 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
707 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
709 CHANGE_FLAGS(msginfo);
711 compose = compose_create(account, COMPOSE_REPLY);
712 compose->replyinfo = procmsg_msginfo_copy(msginfo);
714 #if 0 /* NEW COMPOSE GUI */
715 if (followup_and_reply_to) {
716 gtk_widget_show(compose->to_hbox);
717 gtk_widget_show(compose->to_entry);
718 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
719 compose->use_to = TRUE;
722 if (msginfo->folder && msginfo->folder->ret_rcpt) {
723 GtkItemFactory *ifactory;
725 ifactory = gtk_item_factory_from_widget(compose->menubar);
726 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
729 if (compose_parse_header(compose, msginfo) < 0) return;
730 compose_reply_set_entry(compose, msginfo, to_all, ignore_replyto,
731 followup_and_reply_to);
733 text = GTK_STEXT(compose->text);
734 gtk_stext_freeze(text);
740 if (prefs_common.quotemark && *prefs_common.quotemark)
741 qmark = prefs_common.quotemark;
745 quote_str = compose_quote_fmt(compose, msginfo,
746 prefs_common.quotefmt,
750 if (prefs_common.auto_sig)
751 compose_insert_sig(compose);
753 if (quote && prefs_common.linewrap_quote)
754 compose_wrap_line_all(compose);
756 gtk_editable_set_position(GTK_EDITABLE(text), 0);
757 gtk_stext_set_point(text, 0);
759 gtk_stext_thaw(text);
760 gtk_widget_grab_focus(compose->text);
762 if (prefs_common.auto_exteditor)
763 compose_exec_ext_editor(compose);
767 static gchar *procmime_get_file_name(MimeInfo *mimeinfo)
772 g_return_val_if_fail(mimeinfo != NULL, NULL);
774 base = mimeinfo->filename ? mimeinfo->filename
775 : mimeinfo->name ? mimeinfo->name : NULL;
777 if (MIME_TEXT_HTML == mimeinfo->mime_type && base == NULL){
778 filename = g_strdup_printf("%s%smimetmp.%08x.html",
785 base = base ? base : "";
786 base = g_basename(base);
788 filename = g_strdup_printf("%s%smimetmp.%08x",
796 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
802 static gchar *mime_extract_file(gchar *source, MimeInfo *partinfo)
806 if (!partinfo) return NULL;
808 filename = procmime_get_file_name(partinfo);
810 if (procmime_get_part(filename, source, partinfo) < 0)
812 (_("Can't get the part of multipart message."));
817 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
825 gint boundary_len = 0;
827 glong fpos, prev_fpos;
832 g_return_if_fail(msginfo != NULL);
836 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
837 mimeinfo = procmime_scan_mime_header(fp);
838 if (!mimeinfo) break;
840 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
841 rfc2015_is_encrypted(mimeinfo)) {
842 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
844 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
845 !msginfo->plaintext_file &&
846 !msginfo->decryption_failed) {
847 rfc2015_decrypt_message(msginfo, mimeinfo, fp);
848 if (msginfo->plaintext_file &&
849 !msginfo->decryption_failed) {
857 #else /* !USE_GPGME */
858 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
859 mimeinfo = procmime_scan_mime_header(fp);
860 #endif /* USE_GPGME */
863 if (!mimeinfo) return;
864 if (mimeinfo->mime_type == MIME_TEXT)
867 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
869 g_return_if_fail(mimeinfo != NULL);
870 g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
872 if (mimeinfo->mime_type == MIME_MULTIPART) {
873 g_return_if_fail(mimeinfo->boundary != NULL);
874 g_return_if_fail(mimeinfo->sub == NULL);
876 g_return_if_fail(fp != NULL);
878 boundary = mimeinfo->boundary;
881 boundary_len = strlen(boundary);
883 /* look for first boundary */
884 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
885 if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
892 if ((fpos = ftell(fp)) < 0) {
898 for (npart = 0;; npart++) {
900 gboolean eom = FALSE;
904 partinfo = procmime_scan_mime_header(fp);
905 if (!partinfo) break;
908 procmime_mimeinfo_insert(mimeinfo, partinfo);
910 procmime_mimeinfo_free(partinfo);
912 /* look for next boundary */
914 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
915 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
916 if (buf[2 + boundary_len] == '-' &&
917 buf[2 + boundary_len + 1] == '-')
923 eom = TRUE; /* broken MIME message */
926 partinfo->size = fpos - prev_fpos - strlen(buf);
931 source = procmsg_get_message_file_path(msginfo);
933 g_return_if_fail(mimeinfo != NULL);
935 if (!mimeinfo->main && mimeinfo->parent)
937 filename = mime_extract_file(source, mimeinfo);
939 compose_attach_append_with_type(compose, filename,
940 mimeinfo->content_type,
941 mimeinfo->mime_type);
946 if (mimeinfo->sub && mimeinfo->sub->children)
948 filename = mime_extract_file(source, mimeinfo->sub);
950 compose_attach_append_with_type(compose, filename,
951 mimeinfo->content_type,
952 mimeinfo->sub->mime_type);
957 if (mimeinfo->children) {
960 child = mimeinfo->children;
962 filename = mime_extract_file(source, child);
964 compose_attach_append_with_type(compose, filename,
976 procmime_mimeinfo_free_all(mimeinfo);
980 #define INSERT_FW_HEADER(var, hdr) \
981 if (msginfo->var && *msginfo->var) { \
982 gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
983 gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
984 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
987 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
991 /* PrefsAccount *account; */
994 g_return_val_if_fail(msginfo != NULL, NULL);
995 g_return_val_if_fail(msginfo->folder != NULL, NULL);
997 account = msginfo->folder->folder->account;
998 if (!account && msginfo->to && prefs_common.forward_account_autosel) {
1000 Xstrdup_a(to, msginfo->to, return NULL);
1001 extract_address(to);
1002 account = account_find_from_address(to);
1005 if(!account && prefs_common.forward_account_autosel) {
1007 if(!get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
1008 extract_address(cc);
1009 account = account_find_from_address(cc);
1013 if (account == NULL) {
1014 account = cur_account;
1016 account = msginfo->folder->folder->account;
1017 if (!account) account = cur_account;
1020 g_return_val_if_fail(account != NULL, NULL);
1022 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
1023 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
1024 CHANGE_FLAGS(msginfo);
1026 compose = compose_create(account, COMPOSE_FORWARD);
1028 if (msginfo->subject && *msginfo->subject) {
1029 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
1030 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
1034 text = GTK_STEXT(compose->text);
1035 gtk_stext_freeze(text);
1040 msgfile = procmsg_get_message_file_path(msginfo);
1041 if (!is_file_exist(msgfile))
1042 g_warning(_("%s: file not exist\n"), msgfile);
1044 compose_attach_append(compose, msgfile,
1045 MIME_MESSAGE_RFC822);
1052 if (prefs_common.fw_quotemark && *prefs_common.fw_quotemark)
1053 qmark = prefs_common.fw_quotemark;
1057 quote_str = compose_quote_fmt(compose, msginfo,
1058 prefs_common.fw_quotefmt, qmark);
1059 compose_attach_parts(compose, msginfo);
1062 if (prefs_common.auto_sig)
1063 compose_insert_sig(compose);
1065 if (prefs_common.linewrap_quote)
1066 compose_wrap_line_all(compose);
1068 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1069 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1071 gtk_stext_thaw(text);
1072 #if 0 /* NEW COMPOSE GUI */
1073 if (account->protocol != A_NNTP)
1074 gtk_widget_grab_focus(compose->to_entry);
1076 gtk_widget_grab_focus(compose->newsgroups_entry);
1078 gtk_widget_grab_focus(compose->header_last->entry);
1080 if (prefs_common.auto_exteditor)
1081 compose_exec_ext_editor(compose);
1086 #undef INSERT_FW_HEADER
1088 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1095 g_return_val_if_fail(msginfo_list != NULL, NULL);
1097 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1098 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
1102 if (account == NULL) {
1103 account = cur_account;
1105 account = msginfo->folder->folder->account;
1106 if (!account) account = cur_account;
1109 g_return_val_if_fail(account != NULL, NULL);
1111 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1112 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1113 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1114 CHANGE_FLAGS(((MsgInfo *)msginfo->data));
1117 compose = compose_create(account, COMPOSE_FORWARD);
1119 text = GTK_STEXT(compose->text);
1120 gtk_stext_freeze(text);
1122 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1123 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1124 if (!is_file_exist(msgfile))
1125 g_warning(_("%s: file not exist\n"), msgfile);
1127 compose_attach_append(compose, msgfile,
1128 MIME_MESSAGE_RFC822);
1132 if (prefs_common.auto_sig)
1133 compose_insert_sig(compose);
1135 if (prefs_common.linewrap_quote)
1136 compose_wrap_line_all(compose);
1138 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1139 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1141 gtk_stext_thaw(text);
1142 #if 0 /* NEW COMPOSE GUI */
1143 if (account->protocol != A_NNTP)
1144 gtk_widget_grab_focus(compose->to_entry);
1146 gtk_widget_grab_focus(compose->newsgroups_entry);
1152 void compose_reedit(MsgInfo *msginfo)
1155 PrefsAccount *account;
1158 gchar buf[BUFFSIZE];
1160 g_return_if_fail(msginfo != NULL);
1161 g_return_if_fail(msginfo->folder != NULL);
1163 account = msginfo->folder->folder->account;
1165 if(!account&& prefs_common.reedit_account_autosel) {
1166 gchar from[BUFFSIZE];
1167 if(!get_header_from_msginfo(msginfo,from,sizeof(from),"FROM:")){ /* Found a FROM header */
1168 extract_address(from);
1169 account = account_find_from_address(from);
1172 if (!account) account = cur_account;
1173 g_return_if_fail(account != NULL);
1175 compose = compose_create(account, COMPOSE_REEDIT);
1176 compose->targetinfo = procmsg_msginfo_copy(msginfo);
1178 if (compose_parse_header(compose, msginfo) < 0) return;
1179 compose_reedit_set_entry(compose, msginfo);
1181 text = GTK_STEXT(compose->text);
1182 gtk_stext_freeze(text);
1184 if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1185 g_warning(_("Can't get text part\n"));
1187 while (fgets(buf, sizeof(buf), fp) != NULL)
1188 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1191 compose_attach_parts(compose, msginfo);
1193 gtk_stext_thaw(text);
1194 gtk_widget_grab_focus(compose->text);
1196 if (prefs_common.auto_exteditor)
1197 compose_exec_ext_editor(compose);
1200 GList *compose_get_compose_list(void)
1202 return compose_list;
1205 void compose_entry_append(Compose *compose, const gchar *address,
1206 ComposeEntryType type)
1212 if (!address || *address == '\0') return;
1214 #if 0 /* NEW COMPOSE GUI */
1217 entry = GTK_ENTRY(compose->cc_entry);
1220 entry = GTK_ENTRY(compose->bcc_entry);
1222 case COMPOSE_NEWSGROUPS:
1223 entry = GTK_ENTRY(compose->newsgroups_entry);
1227 entry = GTK_ENTRY(compose->to_entry);
1231 text = gtk_entry_get_text(entry);
1233 gtk_entry_append_text(entry, ", ");
1234 gtk_entry_append_text(entry, address);
1242 header = N_("Bcc:");
1244 case COMPOSE_REPLYTO:
1245 header = N_("Reply-To:");
1247 case COMPOSE_NEWSGROUPS:
1248 header = N_("Newsgroups:");
1250 case COMPOSE_FOLLOWUPTO:
1251 header = N_( "Followup-To:");
1258 header = prefs_common.trans_hdr ? gettext(header) : header;
1260 compose_add_header_entry(compose, header, (gchar *)address);
1263 static void compose_entries_set(Compose *compose, const gchar *mailto)
1265 gchar *subject = NULL;
1273 Xstrdup_a(tmp_mailto, mailto, return);
1277 p = strchr(tmp_mailto, '?');
1284 gchar *field, *value;
1301 if (*value == '\0') continue;
1303 if (!g_strcasecmp(field, "subject")) {
1304 Xalloca(subject, strlen(value) + 1, return);
1305 decode_uri(subject, value);
1306 } else if (!g_strcasecmp(field, "cc")) {
1308 } else if (!g_strcasecmp(field, "bcc")) {
1310 } else if (!g_strcasecmp(field, "body")) {
1311 Xalloca(body, strlen(value) + 1, return);
1312 decode_uri(body, value);
1317 compose_entry_append(compose, to, COMPOSE_TO);
1319 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1321 compose_entry_append(compose, cc, COMPOSE_CC);
1323 compose_entry_append(compose, bcc, COMPOSE_BCC);
1325 gtk_stext_insert(GTK_STEXT(compose->text),
1326 NULL, NULL, NULL, body, -1);
1327 gtk_stext_insert(GTK_STEXT(compose->text),
1328 NULL, NULL, NULL, "\n", 1);
1332 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1334 static HeaderEntry hentry[] = {{"Reply-To:", NULL, TRUE},
1335 {"Cc:", NULL, FALSE},
1336 {"References:", NULL, FALSE},
1337 {"Bcc:", NULL, FALSE},
1338 {"Newsgroups:", NULL, FALSE},
1339 {"Followup-To:", NULL, FALSE},
1340 {"X-Mailing-List:", NULL, FALSE},
1341 {"X-BeenThere:", NULL, FALSE},
1342 {NULL, NULL, FALSE}};
1352 H_X_MAILING_LIST = 6,
1358 g_return_val_if_fail(msginfo != NULL, -1);
1360 if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1361 procheader_get_header_fields(fp, hentry);
1364 if (hentry[H_REPLY_TO].body != NULL) {
1365 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1366 compose->replyto = hentry[H_REPLY_TO].body;
1367 hentry[H_REPLY_TO].body = NULL;
1369 if (hentry[H_CC].body != NULL) {
1370 conv_unmime_header_overwrite(hentry[H_CC].body);
1371 compose->cc = hentry[H_CC].body;
1372 hentry[H_CC].body = NULL;
1374 if (hentry[H_X_MAILING_LIST].body != NULL) {
1375 /* this is good enough to parse debian-devel */
1376 gchar *buf = g_malloc(strlen(hentry[H_X_MAILING_LIST].body) + 1);
1377 g_return_val_if_fail(buf != NULL, -1 );
1378 if (1 == sscanf(hentry[H_X_MAILING_LIST].body, "<%[^>]>", buf))
1379 compose->mailinglist = g_strdup(buf);
1381 g_free(hentry[H_X_MAILING_LIST].body);
1382 hentry[H_X_MAILING_LIST].body = NULL ;
1384 if (hentry[H_X_BEENTHERE].body != NULL) {
1385 /* this is good enough to parse the sylpheed-claws lists */
1386 gchar *buf = g_malloc(strlen(hentry[H_X_BEENTHERE].body) + 1);
1387 g_return_val_if_fail(buf != NULL, -1 );
1388 if (1 == sscanf(hentry[H_X_BEENTHERE].body, "%[^>]", buf))
1389 compose->mailinglist = g_strdup(buf);
1391 g_free(hentry[H_X_BEENTHERE].body);
1392 hentry[H_X_BEENTHERE].body = NULL ;
1394 if (hentry[H_REFERENCES].body != NULL) {
1395 if (compose->mode == COMPOSE_REEDIT)
1396 compose->references = hentry[H_REFERENCES].body;
1398 compose->references = compose_parse_references
1399 (hentry[H_REFERENCES].body, msginfo->msgid);
1400 g_free(hentry[H_REFERENCES].body);
1402 hentry[H_REFERENCES].body = NULL;
1404 if (hentry[H_BCC].body != NULL) {
1405 if (compose->mode == COMPOSE_REEDIT) {
1406 conv_unmime_header_overwrite(hentry[H_BCC].body);
1407 compose->bcc = hentry[H_BCC].body;
1409 g_free(hentry[H_BCC].body);
1410 hentry[H_BCC].body = NULL;
1412 if (hentry[H_NEWSGROUPS].body != NULL) {
1413 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1414 hentry[H_NEWSGROUPS].body = NULL;
1416 if (hentry[H_FOLLOWUP_TO].body != NULL) {
1417 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1418 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1419 hentry[H_FOLLOWUP_TO].body = NULL;
1422 if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1423 compose->inreplyto = g_strdup(msginfo->inreplyto);
1424 else if (compose->mode != COMPOSE_REEDIT &&
1425 msginfo->msgid && *msginfo->msgid) {
1426 compose->inreplyto = g_strdup(msginfo->msgid);
1428 if (!compose->references) {
1429 if (msginfo->inreplyto && *msginfo->inreplyto)
1430 compose->references =
1431 g_strdup_printf("<%s>\n\t<%s>",
1435 compose->references =
1436 g_strconcat("<", msginfo->msgid, ">",
1444 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1446 GSList *ref_id_list, *cur;
1450 ref_id_list = references_list_append(NULL, ref);
1451 if (!ref_id_list) return NULL;
1452 if (msgid && *msgid)
1453 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1458 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1459 /* "<" + Message-ID + ">" + CR+LF+TAB */
1460 len += strlen((gchar *)cur->data) + 5;
1462 if (len > MAX_REFERENCES_LEN) {
1463 /* remove second message-ID */
1464 if (ref_id_list && ref_id_list->next &&
1465 ref_id_list->next->next) {
1466 g_free(ref_id_list->next->data);
1467 ref_id_list = g_slist_remove
1468 (ref_id_list, ref_id_list->next->data);
1470 slist_free_strings(ref_id_list);
1471 g_slist_free(ref_id_list);
1478 new_ref = g_string_new("");
1479 for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1480 if (new_ref->len > 0)
1481 g_string_append(new_ref, "\n\t");
1482 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1485 slist_free_strings(ref_id_list);
1486 g_slist_free(ref_id_list);
1488 new_ref_str = new_ref->str;
1489 g_string_free(new_ref, FALSE);
1494 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1495 const gchar *fmt, const gchar *qmark)
1497 GtkSText *text = GTK_STEXT(compose->text);
1498 gchar *quote_str = NULL;
1503 if (qmark != NULL) {
1504 quote_fmt_init(msginfo, NULL);
1505 quote_fmt_scan_string(qmark);
1508 buf = quote_fmt_get_buffer();
1510 alertpanel_error(_("Quote mark format error."));
1512 Xstrdup_a(quote_str, buf, return NULL)
1515 if (fmt && *fmt != '\0') {
1516 quote_fmt_init(msginfo, quote_str);
1517 quote_fmt_scan_string(fmt);
1520 buf = quote_fmt_get_buffer();
1522 alertpanel_error(_("Message reply/forward format error."));
1528 gtk_stext_freeze(text);
1529 gtk_stext_set_point(text, 0);
1530 gtk_stext_forward_delete(text, gtk_stext_get_length(text));
1532 for (p = buf; *p != '\0'; ) {
1533 lastp = strchr(p, '\n');
1534 len = lastp ? lastp - p + 1 : -1;
1535 gtk_stext_insert(text, NULL, NULL, NULL, p, len);
1542 gtk_stext_thaw(text);
1547 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1548 gboolean to_all, gboolean ignore_replyto,
1549 gboolean followup_and_reply_to)
1554 GHashTable *to_table;
1556 g_return_if_fail(compose->account != NULL);
1557 g_return_if_fail(msginfo != NULL);
1559 if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1560 compose_entry_append(compose,
1561 ((compose->mailinglist && !ignore_replyto)
1562 ? compose->mailinglist
1563 : (compose->replyto && !ignore_replyto)
1565 : msginfo->from ? msginfo->from : ""),
1567 if (compose->account->protocol == A_NNTP) {
1569 compose_entry_append
1570 (compose, msginfo->from ? msginfo->from : "",
1573 compose_entry_append
1575 compose->followup_to ? compose->followup_to
1576 : compose->newsgroups ? compose->newsgroups
1578 COMPOSE_NEWSGROUPS);
1581 if (msginfo->subject && *msginfo->subject) {
1582 gchar *buf, *buf2, *p;
1584 buf = g_strdup(msginfo->subject);
1585 while (!strncasecmp(buf, "Re:", 3)) {
1587 while (isspace(*p)) p++;
1588 memmove(buf, p, strlen(p) + 1);
1591 buf2 = g_strdup_printf("Re: %s", buf);
1592 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1596 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1598 if (!to_all || compose->account->protocol == A_NNTP) return;
1600 from = g_strdup(compose->replyto ? compose->replyto :
1601 msginfo->from ? msginfo->from : "");
1602 extract_address(from);
1604 cc_list = address_list_append(NULL, msginfo->to);
1605 cc_list = address_list_append(cc_list, compose->cc);
1607 to_table = g_hash_table_new(g_str_hash, g_str_equal);
1608 g_hash_table_insert(to_table, from, GINT_TO_POINTER(1));
1609 if (compose->account)
1610 g_hash_table_insert(to_table, compose->account->address,
1611 GINT_TO_POINTER(1));
1613 /* remove address on To: and that of current account */
1614 for (cur = cc_list; cur != NULL; ) {
1615 GSList *next = cur->next;
1617 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1618 cc_list = g_slist_remove(cc_list, cur->data);
1620 g_hash_table_insert(to_table, cur->data, cur);
1624 g_hash_table_destroy(to_table);
1628 for (cur = cc_list; cur != NULL; cur = cur->next)
1629 compose_entry_append(compose, (gchar *)cur->data,
1631 slist_free_strings(cc_list);
1632 g_slist_free(cc_list);
1636 #define SET_ENTRY(entry, str) \
1639 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1642 #define SET_ADDRESS(type, str) \
1645 compose_entry_append(compose, str, type); \
1648 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1650 g_return_if_fail(msginfo != NULL);
1652 SET_ENTRY(subject_entry, msginfo->subject);
1653 SET_ADDRESS(COMPOSE_TO, msginfo->to);
1654 SET_ADDRESS(COMPOSE_CC, compose->cc);
1655 SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1656 SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1658 #if 0 /* NEW COMPOSE GUI */
1660 GtkItemFactory *ifactory;
1661 GtkWidget *menuitem;
1663 ifactory = gtk_item_factory_from_widget(compose->menubar);
1664 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
1665 gtk_check_menu_item_set_active
1666 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1668 if (compose->replyto) {
1669 GtkItemFactory *ifactory;
1670 GtkWidget *menuitem;
1672 ifactory = gtk_item_factory_from_widget(compose->menubar);
1673 menuitem = gtk_item_factory_get_item
1674 (ifactory, "/Message/Reply to");
1675 gtk_check_menu_item_set_active
1676 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1684 static void compose_exec_sig(Compose *compose, gchar *sigfile)
1691 size_t buf_len = 128;
1693 if (strlen(sigfile) < 2)
1696 sigprg = popen(sigfile+1, "r");
1699 buf = g_malloc(buf_len);
1702 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1703 "Unable to insert signature (malloc failed)\n", -1);
1709 while (!feof(sigprg)) {
1710 bzero(buf, buf_len);
1711 fread(buf, buf_len-1, 1, sigprg);
1712 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
1720 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1721 "Can't exec file: ", -1);
1722 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1727 static void compose_insert_sig(Compose *compose)
1731 if (compose->account && compose->account->sig_path)
1732 sigfile = g_strdup(compose->account->sig_path);
1734 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1735 DEFAULT_SIGNATURE, NULL);
1738 if (!is_file_or_fifo_exist(sigfile) & (sigfile[0] != '|')) {
1743 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1744 if (prefs_common.sig_sep) {
1745 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1746 prefs_common.sig_sep, -1);
1747 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1751 if (sigfile[0] == '|')
1753 compose_exec_sig(compose, sigfile);
1757 compose_insert_file(compose, sigfile);
1762 static void compose_insert_file(Compose *compose, const gchar *file)
1764 GtkSText *text = GTK_STEXT(compose->text);
1765 gchar buf[BUFFSIZE];
1769 g_return_if_fail(file != NULL);
1771 if ((fp = fopen(file, "r")) == NULL) {
1772 FILE_OP_ERROR(file, "fopen");
1776 gtk_stext_freeze(text);
1778 while (fgets(buf, sizeof(buf), fp) != NULL) {
1779 /* Strip <CR> if DOS/Windoze file, replace <CR> with <LF> if MAC file */
1781 if (len > 1 && buf[len - 2] == '\r' && buf[len - 1] == '\n') {
1782 buf[len - 2] = '\n';
1783 buf[len - 1] = '\0';
1786 if (buf[len] == '\r')
1790 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1793 gtk_stext_thaw(text);
1798 static void compose_attach_info(Compose * compose, AttachInfo * ainfo,
1799 ContentType cnttype)
1801 gchar *text[N_ATTACH_COLS];
1804 text[COL_MIMETYPE] = ainfo->content_type;
1805 text[COL_SIZE] = to_human_readable(ainfo->size);
1806 text[COL_NAME] = ainfo->name;
1808 row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1809 gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1811 if (cnttype != MIME_MESSAGE_RFC822)
1812 compose_changed_cb(NULL, compose);
1815 static void compose_attach_append_with_type(Compose *compose,
1818 ContentType cnttype)
1823 if (!is_file_exist(file)) {
1824 g_warning(_("File %s doesn't exist\n"), file);
1827 if ((size = get_file_size(file)) < 0) {
1828 g_warning(_("Can't get file size of %s\n"), file);
1832 alertpanel_notice(_("File %s is empty\n"), file);
1835 #if 0 /* NEW COMPOSE GUI */
1836 if (!compose->use_attach) {
1837 GtkItemFactory *ifactory;
1838 GtkWidget *menuitem;
1840 ifactory = gtk_item_factory_from_widget(compose->menubar);
1841 menuitem = gtk_item_factory_get_item(ifactory,
1843 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1847 ainfo = g_new0(AttachInfo, 1);
1848 ainfo->file = g_strdup(file);
1850 if (cnttype == MIME_MESSAGE_RFC822) {
1851 ainfo->encoding = ENC_7BIT;
1852 ainfo->name = g_strdup_printf(_("Message: %s"),
1855 ainfo->encoding = ENC_BASE64;
1856 ainfo->name = g_strdup(g_basename(file));
1859 ainfo->content_type = g_strdup(type);
1862 compose_attach_info(compose, ainfo, cnttype);
1865 static void compose_attach_append(Compose *compose, const gchar *file,
1866 ContentType cnttype)
1869 gchar *text[N_ATTACH_COLS];
1873 if (!is_file_exist(file)) {
1874 g_warning(_("File %s doesn't exist\n"), file);
1877 if ((size = get_file_size(file)) < 0) {
1878 g_warning(_("Can't get file size of %s\n"), file);
1882 alertpanel_notice(_("File %s is empty\n"), file);
1885 #if 0 /* NEW COMPOSE GUI */
1886 if (!compose->use_attach) {
1887 GtkItemFactory *ifactory;
1888 GtkWidget *menuitem;
1890 ifactory = gtk_item_factory_from_widget(compose->menubar);
1891 menuitem = gtk_item_factory_get_item(ifactory,
1893 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1897 ainfo = g_new0(AttachInfo, 1);
1898 ainfo->file = g_strdup(file);
1900 if (cnttype == MIME_MESSAGE_RFC822) {
1901 ainfo->content_type = g_strdup("message/rfc822");
1902 ainfo->encoding = ENC_7BIT;
1903 ainfo->name = g_strdup_printf(_("Message: %s"),
1906 ainfo->content_type = procmime_get_mime_type(file);
1907 if (!ainfo->content_type)
1908 ainfo->content_type =
1909 g_strdup("application/octet-stream");
1910 ainfo->encoding = ENC_BASE64;
1911 ainfo->name = g_strdup(g_basename(file));
1915 compose_attach_info(compose, ainfo, cnttype);
1918 #define GET_CHAR(pos, buf, len) \
1920 if (text->use_wchar) \
1921 len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos))); \
1923 buf[0] = GTK_STEXT_INDEX(text, (pos)); \
1928 static void compose_wrap_line(Compose *compose)
1930 GtkSText *text = GTK_STEXT(compose->text);
1931 gint ch_len, last_ch_len;
1932 gchar cbuf[MB_LEN_MAX], last_ch;
1936 gint p_start, p_end;
1937 gint line_pos, cur_pos;
1938 gint line_len, cur_len;
1941 gtk_stext_freeze(text);
1943 text_len = gtk_stext_get_length(text);
1945 /* check to see if the point is on the paragraph mark (empty line). */
1946 cur_pos = gtk_stext_get_point(text);
1947 GET_CHAR(cur_pos, cbuf, ch_len);
1948 if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
1950 goto compose_end; /* on the paragraph mark */
1951 GET_CHAR(cur_pos - 1, cbuf, ch_len);
1952 if (ch_len == 1 && *cbuf == '\n')
1953 goto compose_end; /* on the paragraph mark */
1956 /* find paragraph start. */
1957 line_end = quoted = 0;
1958 for (p_start = cur_pos; p_start >= 0; --p_start) {
1959 GET_CHAR(p_start, cbuf, ch_len);
1960 if (ch_len == 1 && *cbuf == '\n') {
1962 goto compose_end; /* quoted part */
1969 if (ch_len == 1 && strchr(">:#", *cbuf))
1971 else if (ch_len != 1 || !isspace(*cbuf))
1980 /* find paragraph end. */
1982 for (p_end = cur_pos; p_end < text_len; p_end++) {
1983 GET_CHAR(p_end, cbuf, ch_len);
1984 if (ch_len == 1 && *cbuf == '\n') {
1991 if (line_end && ch_len == 1 && strchr(">:#", *cbuf))
1992 goto compose_end; /* quoted part */
1997 if (p_end >= text_len)
2000 if (p_start >= p_end)
2003 line_len = cur_len = 0;
2007 for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2010 GET_CHAR(cur_pos, cbuf, ch_len);
2017 if (ch_len == 1 && isspace(*cbuf))
2020 if (ch_len == 1 && *cbuf == '\n') {
2022 if (last_ch_len == 1 && !isspace(last_ch)) {
2023 if (cur_pos + 1 < p_end) {
2024 GET_CHAR(cur_pos + 1, cbuf, ch_len);
2025 if (ch_len == 1 && !isspace(*cbuf))
2029 gtk_stext_set_point(text, cur_pos + 1);
2030 gtk_stext_backward_delete(text, 1);
2032 gtk_stext_set_point(text, cur_pos);
2033 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2043 last_ch_len = ch_len;
2047 line_pos = cur_pos + 1;
2048 line_len = cur_len + ch_len;
2051 if (cur_len + ch_len > prefs_common.linewrap_len &&
2055 GET_CHAR(line_pos - 1, cbuf, ch_len);
2056 if (ch_len == 1 && isspace(*cbuf)) {
2057 gtk_stext_set_point(text, line_pos);
2058 gtk_stext_backward_delete(text, 1);
2067 gtk_stext_set_point(text, line_pos);
2068 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2072 cur_len = cur_len - line_len + ch_len;
2078 line_pos = cur_pos + 1;
2079 line_len = cur_len + ch_len;
2085 gtk_stext_thaw(text);
2088 /* return indent length */
2089 static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
2091 gint indent_len = 0;
2093 gchar cbuf[MB_LEN_MAX];
2095 for (i = start_pos; i < text_len; i++) {
2096 GET_CHAR(i, cbuf, ch_len);
2099 /* allow space, tab, > or | */
2100 if (cbuf[0] != ' ' && cbuf[0] != '\t' &&
2101 cbuf[0] != '>' && cbuf[0] != '|')
2109 /* insert quotation string when line was wrapped */
2110 static guint ins_quote(GtkSText *text, guint quote_len, guint indent_len,
2111 guint prev_line_pos, guint text_len,
2118 for (i = 0; i < indent_len; i++) {
2119 ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2120 gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2122 ins_len = indent_len;
2124 gtk_stext_insert(text, NULL, NULL, NULL, quote_fmt, quote_len);
2125 ins_len = quote_len;
2133 /* Darko: used when I debug wrapping */
2134 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2139 printf("%d [", pos);
2140 for (i = pos; i < tlen; i++) {
2141 ch = GTK_STEXT_INDEX(text, i);
2142 if (breakoncr && ch == '\n')
2150 static void compose_wrap_line_all(Compose *compose)
2152 GtkSText *text = GTK_STEXT(compose->text);
2154 guint line_pos = 0, cur_pos = 0, p_pos = 0;
2155 gint line_len = 0, cur_len = 0;
2157 gboolean is_new_line = TRUE, do_delete = FALSE;
2158 guint qlen = 0, i_len = 0;
2159 guint linewrap_quote = prefs_common.linewrap_quote;
2160 guint linewrap_len = prefs_common.linewrap_len;
2161 gchar *qfmt = prefs_common.quotemark;
2162 gchar cbuf[MB_LEN_MAX];
2164 gtk_stext_freeze(text);
2166 /* make text buffer contiguous */
2167 /* gtk_stext_compact_buffer(text); */
2169 tlen = gtk_stext_get_length(text);
2171 for (; cur_pos < tlen; cur_pos++) {
2172 /* mark position of new line - needed for quotation wrap */
2173 if (linewrap_quote && is_new_line) {
2174 qlen = gtkut_text_str_compare
2175 (text, cur_pos, tlen, qfmt);
2176 is_new_line = FALSE;
2178 i_len = get_indent_length(text, cur_pos, tlen);
2183 printf("new line i_len=%d qlen=%d p_pos=", i_len, qlen);
2184 dump_text(text, p_pos, tlen, 1);
2188 GET_CHAR(cur_pos, cbuf, ch_len);
2190 /* fix line length for tabs */
2191 if (ch_len == 1 && *cbuf == '\t') {
2192 guint tab_width = text->default_tab_width;
2193 guint tab_offset = line_len % tab_width;
2196 printf("found tab at pos=%d line_len=%d ", cur_pos,
2200 line_len += tab_width - tab_offset - 1;
2204 printf("new_len=%d\n", line_len);
2208 /* we have encountered line break */
2209 if (ch_len == 1 && *cbuf == '\n') {
2212 gchar cb[MB_CUR_MAX];
2215 printf("found CR at %d next line is ", cur_pos);
2216 dump_text(text, cur_pos + 1, tlen, 1);
2218 /* if it's just quotation + newline skip it */
2219 if (i_len && (cur_pos + 1 < tlen)) {
2220 /* check if text at new line matches indent */
2221 ilen = gtkut_text_str_compare_n
2222 (text, cur_pos + 1, p_pos, i_len, tlen);
2223 if (cur_pos + ilen < tlen) {
2224 GET_CHAR(cur_pos + ilen + 1, cb, clen);
2225 /* no need to join the lines */
2226 if (clen == 1 && cb[0] == '\n')
2229 /* if it's just newline skip it */
2230 } else if (do_delete && (cur_pos + 1 < tlen)) {
2231 GET_CHAR(cur_pos + 1, cb, clen);
2232 /* no need to join the lines */
2233 if (clen == 1 && cb[0] == '\n')
2238 printf("qlen=%d l_len=%d wrap_len=%d do_del=%d\n",
2239 qlen, line_len, linewrap_len, do_delete);
2241 /* should we delete to perform smart wrapping */
2242 if (line_len < linewrap_len && do_delete) {
2243 /* get rid of newline */
2244 gtk_stext_set_point(text, cur_pos);
2245 gtk_stext_forward_delete(text, 1);
2248 /* if text starts with quote fmt or with
2249 indent string, delete them */
2251 ilen = gtkut_text_str_compare_n
2252 (text, cur_pos, p_pos, i_len,
2255 gtk_stext_forward_delete(text,
2260 if (gtkut_text_str_compare
2261 (text, cur_pos, tlen, qfmt)) {
2262 gtk_stext_forward_delete
2268 GET_CHAR(cur_pos, cb, clen);
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_text_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 (!gtkut_text_is_uri_string
2325 (text, line_pos, tlen))
2326 line_pos = cur_pos - 1;
2328 printf("new line_pos=%d\n", line_pos);
2331 GET_CHAR(line_pos - 1, cbuf, clen);
2333 /* if next character is space delete it */
2334 if (clen == 1 && isspace(*cbuf)) {
2335 if (p_pos + i_len != line_pos ||
2336 !gtkut_text_is_uri_string
2337 (text, line_pos, tlen)) {
2338 gtk_stext_set_point(text, line_pos);
2339 gtk_stext_backward_delete(text, 1);
2348 /* if it is URL at beginning of line don't wrap */
2349 if (p_pos + i_len == line_pos &&
2350 gtkut_text_is_uri_string(text, line_pos, tlen)) {
2352 printf("found URL at ");
2353 dump_text(text, line_pos, tlen, 1);
2359 gtk_stext_set_point(text, line_pos);
2360 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2361 /* gtk_stext_compact_buffer(text); */
2364 /* for loop will increase it */
2365 cur_pos = line_pos - 1;
2366 /* start over with current line */
2372 printf("after CR insert ");
2373 dump_text(text, line_pos, tlen, 1);
2374 dump_text(text, cur_pos, tlen, 1);
2377 /* should we insert quotation ? */
2378 if (linewrap_quote && qlen) {
2379 /* only if line is not already quoted */
2380 if (!gtkut_text_str_compare
2381 (text, line_pos, tlen, qfmt)) {
2384 if (line_pos - p_pos > i_len) {
2389 /* gtk_stext_compact_buffer(text); */
2393 printf("after quote insert ");
2394 dump_text(text, line_pos, tlen, 1);
2402 line_pos = cur_pos + 1;
2403 line_len = cur_len + ch_len;
2405 /* advance to next character in buffer */
2409 gtk_stext_thaw(text);
2413 static void compose_set_title(Compose *compose)
2418 edited = compose->modified ? _(" [Edited]") : "";
2419 if (compose->account && compose->account->address)
2420 str = g_strdup_printf(_("%s - Compose message%s"),
2421 compose->account->address, edited);
2423 str = g_strdup_printf(_("Compose message%s"), edited);
2424 gtk_window_set_title(GTK_WINDOW(compose->window), str);
2429 * compose_current_mail_account:
2431 * Find a current mail account (the currently selected account, or the
2432 * default account, if a news account is currently selected). If a
2433 * mail account cannot be found, display an error message.
2435 * Return value: Mail account, or NULL if not found.
2437 static PrefsAccount *
2438 compose_current_mail_account(void)
2442 if (cur_account && cur_account->protocol != A_NNTP)
2445 ac = account_get_default();
2446 if (!ac || ac->protocol == A_NNTP) {
2447 alertpanel_error(_("Account for sending mail is not specified.\n"
2448 "Please select a mail account before sending."));
2455 gboolean compose_check_for_valid_recipient(Compose *compose) {
2456 gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
2457 gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
2458 gboolean recipient_found = FALSE;
2462 /* free to and newsgroup list */
2463 slist_free_strings(compose->to_list);
2464 g_slist_free(compose->to_list);
2465 compose->to_list = NULL;
2467 slist_free_strings(compose->newsgroup_list);
2468 g_slist_free(compose->newsgroup_list);
2469 compose->newsgroup_list = NULL;
2471 /* search header entries for to and newsgroup entries */
2472 for(list = compose->header_list; list; list = list->next) {
2475 header = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry));
2476 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
2478 if(entry[0] != '\0') {
2479 for(strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
2480 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2481 compose->to_list = address_list_append(compose->to_list, entry);
2482 recipient_found = TRUE;
2485 for(strptr = recipient_headers_news; *strptr != NULL; strptr++) {
2486 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2487 compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
2488 recipient_found = TRUE;
2494 return recipient_found;
2497 gint compose_send(Compose *compose)
2503 val = compose_queue(compose, &msgnum, &folder);
2505 alertpanel_error(_("Could not queue message for sending"));
2509 val = procmsg_send_message_queue(folder_item_fetch_msg(folder, msgnum));
2511 folder_item_remove_msg(folder, msgnum);
2512 folderview_update_item(folder, TRUE);
2518 #if 0 /* compose restructure */
2519 gint compose_send(Compose *compose)
2521 gchar tmp[MAXPATHLEN + 1];
2523 static gboolean lock = FALSE;
2527 g_return_val_if_fail(compose->account != NULL, -1);
2528 g_return_val_if_fail(compose->orig_account != NULL, -1);
2532 if(!compose_check_for_valid_recipient(compose)) {
2533 alertpanel_error(_("Recipient is not specified."));
2538 /* write to temporary file */
2539 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
2540 get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
2542 if (prefs_common.linewrap_at_send)
2543 compose_wrap_line_all(compose);
2545 if (compose_write_to_file(compose, tmp, FALSE) < 0) {
2550 if (!compose->to_list && !compose->newsgroup_list) {
2551 g_warning(_("can't get recipient list."));
2557 if (compose->to_list) {
2560 #if 0 /* NEW COMPOSE GUI */
2561 if (compose->account->protocol != A_NNTP)
2562 ac = compose->account;
2563 else if (compose->orig_account->protocol != A_NNTP)
2564 ac = compose->orig_account;
2565 else if (cur_account && cur_account->protocol != A_NNTP)
2568 ac = compose_current_mail_account();
2576 ac = compose->account;
2578 ok = send_message(tmp, ac, compose->to_list);
2579 statusbar_pop_all();
2582 if (ok == 0 && compose->newsgroup_list) {
2585 if (compose->account->protocol == A_NNTP)
2586 folder = FOLDER(compose->account->folder);
2588 folder = FOLDER(compose->orig_account->folder);
2590 ok = news_post(folder, tmp);
2592 alertpanel_error(_("Error occurred while posting the message to %s ."),
2593 compose->account->nntp_server);
2600 /* queue message if failed to send */
2602 if (prefs_common.queue_msg) {
2607 _("Error occurred while sending the message.\n"
2608 "Put this message into queue folder?"),
2609 _("OK"), _("Cancel"), NULL);
2610 if (G_ALERTDEFAULT == val) {
2611 ok = compose_queue(compose, tmp);
2613 alertpanel_error(_("Can't queue the message."));
2616 alertpanel_error(_("Error occurred while sending the message."));
2618 if (compose->mode == COMPOSE_REEDIT) {
2619 compose_remove_reedit_target(compose);
2620 if (compose->targetinfo)
2621 folderview_update_item
2622 (compose->targetinfo->folder, TRUE);
2626 /* save message to outbox */
2627 if (ok == 0 && prefs_common.savemsg) {
2628 if (compose_save_to_outbox(compose, tmp) < 0)
2630 (_("Can't save the message to outbox."));
2639 static gboolean compose_use_attach(Compose *compose) {
2640 return(gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL);
2643 static gint compose_bounce_write_headers_from_headerlist(Compose *compose,
2646 gchar buf[BUFFSIZE];
2648 gboolean first_address;
2650 ComposeHeaderEntry *headerentry;
2651 gchar *headerentryname;
2652 gchar *header_w_colon;
2656 debug_print(_("Writing bounce header\n"));
2658 header_w_colon = g_strconcat("To:", NULL);
2659 to_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2660 header_w_colon = g_strconcat("Cc:", NULL);
2661 cc_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2663 first_address = TRUE;
2664 for(list = compose->header_list; list; list = list->next) {
2665 headerentry = ((ComposeHeaderEntry *)list->data);
2666 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
2668 if(g_strcasecmp(headerentryname, cc_hdr) == 0
2669 || g_strcasecmp(headerentryname, to_hdr) == 0) {
2670 str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
2671 Xstrdup_a(str, str, return -1);
2673 if(str[0] != '\0') {
2674 compose_convert_header
2675 (buf, sizeof(buf), str,
2676 strlen("Resent-To") + 2);
2678 fprintf(fp, "Resent-To: ");
2679 first_address = FALSE;
2683 fprintf(fp, "%s", buf);
2687 /* if(!first_address) { */
2694 static gint compose_bounce_write_headers(Compose *compose, FILE *fp)
2696 gchar buf[BUFFSIZE];
2698 /* struct utsname utsbuf; */
2700 g_return_val_if_fail(fp != NULL, -1);
2701 g_return_val_if_fail(compose->account != NULL, -1);
2702 g_return_val_if_fail(compose->account->address != NULL, -1);
2705 get_rfc822_date(buf, sizeof(buf));
2706 fprintf(fp, "Resent-Date: %s\n", buf);
2709 if (compose->account->name && *compose->account->name) {
2710 compose_convert_header
2711 (buf, sizeof(buf), compose->account->name,
2713 fprintf(fp, "Resent-From: %s <%s>\n",
2714 buf, compose->account->address);
2716 fprintf(fp, "Resent-From: %s\n", compose->account->address);
2719 compose_bounce_write_headers_from_headerlist(compose, fp);
2721 /* separator between header and body */
2727 static gint compose_bounce_write_to_file(Compose *compose, const gchar *file)
2732 gchar buf[BUFFSIZE];
2734 if ((fp = fopen(compose->bounce_filename, "r")) == NULL) {
2735 FILE_OP_ERROR(file, "fopen");
2739 if ((fdest = fopen(file, "w")) == NULL) {
2740 FILE_OP_ERROR(file, "fopen");
2745 /* chmod for security */
2746 if (change_file_mode_rw(fdest, file) < 0) {
2747 FILE_OP_ERROR(file, "chmod");
2748 g_warning(_("can't change file mode\n"));
2751 while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
2752 /* should filter returnpath, delivered-to */
2753 if ((g_strncasecmp(buf, "Return-Path:",
2754 strlen("Return-Path:")) == 0)
2755 || (g_strncasecmp(buf, "Delivered-To:",
2756 strlen("Delivered-To:")) == 0))
2759 if (fputs(buf, fdest) == -1)
2762 if (g_strncasecmp(buf, "From:", strlen("From:")) == 0) {
2763 fputs(" (by way of ", fdest);
2764 if (compose->account->name
2765 && *compose->account->name) {
2766 compose_convert_header
2768 compose->account->name,
2770 fprintf(fdest, "%s <%s>",
2771 buf, compose->account->address);
2773 fprintf(fdest, "%s",
2774 compose->account->address);
2778 if (fputs("\n", fdest) == -1)
2782 compose_bounce_write_headers(compose, fdest);
2784 while ((len = fread(buf, sizeof(gchar), BUFFSIZE, fp)) > 0) {
2785 if (fwrite(buf, sizeof(gchar), len, fdest) == -1)
2800 static gint compose_write_to_file(Compose *compose, const gchar *file,
2807 const gchar *out_codeset;
2808 EncodingType encoding;
2810 if ((fp = fopen(file, "w")) == NULL) {
2811 FILE_OP_ERROR(file, "fopen");
2815 /* chmod for security */
2816 if (change_file_mode_rw(fp, file) < 0) {
2817 FILE_OP_ERROR(file, "chmod");
2818 g_warning(_("can't change file mode\n"));
2821 /* get all composed text */
2822 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2823 len = strlen(chars);
2824 if (is_ascii_str(chars)) {
2825 buf = g_strdup(chars);
2826 out_codeset = CS_US_ASCII;
2827 encoding = ENC_7BIT;
2829 const gchar *src_codeset;
2831 out_codeset = conv_get_outgoing_charset_str();
2832 if (!strcasecmp(out_codeset, CS_US_ASCII))
2833 out_codeset = CS_ISO_8859_1;
2834 encoding = procmime_get_encoding_for_charset(out_codeset);
2836 src_codeset = conv_get_current_charset_str();
2837 /* if current encoding is US-ASCII, set it the same as
2838 outgoing one to prevent code conversion failure */
2839 if (!strcasecmp(src_codeset, CS_US_ASCII))
2840 src_codeset = out_codeset;
2842 debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
2843 src_codeset, out_codeset, procmime_get_encoding_str(encoding));
2845 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
2850 alertpanel_error(_("Can't convert the codeset of the message."));
2857 if (compose_write_headers
2858 (compose, fp, out_codeset, encoding, is_draft) < 0) {
2859 g_warning(_("can't write headers\n"));
2866 if (compose_use_attach(compose)) {
2868 /* This prolog message is ignored by mime software and
2869 * because it would make our signing/encryption task
2870 * tougher, we don't emit it in that case */
2871 if (!compose->use_signing && !compose->use_encryption)
2873 fputs("This is a multi-part message in MIME format.\n", fp);
2875 fprintf(fp, "\n--%s\n", compose->boundary);
2876 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
2878 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2879 procmime_get_encoding_str(encoding));
2885 if (encoding == ENC_BASE64) {
2886 gchar outbuf[B64_BUFFSIZE];
2889 for (i = 0; i < len; i += B64_LINE_SIZE) {
2890 l = MIN(B64_LINE_SIZE, len - i);
2891 to64frombits(outbuf, buf + i, l);
2895 } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
2896 FILE_OP_ERROR(file, "fwrite");
2904 if (compose_use_attach(compose))
2905 compose_write_attach(compose, fp);
2907 if (fclose(fp) == EOF) {
2908 FILE_OP_ERROR(file, "fclose");
2914 if (compose->use_signing) {
2915 if (rfc2015_sign(file, compose->account) < 0) {
2920 if (compose->use_encryption) {
2921 if (rfc2015_encrypt(file, compose->to_list) < 0) {
2926 #endif /* USE_GPGME */
2931 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
2937 if ((fp = fopen(file, "w")) == NULL) {
2938 FILE_OP_ERROR(file, "fopen");
2942 /* chmod for security */
2943 if (change_file_mode_rw(fp, file) < 0) {
2944 FILE_OP_ERROR(file, "chmod");
2945 g_warning(_("can't change file mode\n"));
2948 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2951 len = strlen(chars);
2952 if (fwrite(chars, sizeof(gchar), len, fp) != len) {
2953 FILE_OP_ERROR(file, "fwrite");
2962 if (fclose(fp) == EOF) {
2963 FILE_OP_ERROR(file, "fclose");
2970 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
2977 debug_print(_("saving sent message...\n"));
2979 outbox = folder_get_default_outbox();
2980 path = folder_item_get_path(outbox);
2981 if (!is_dir_exist(path))
2982 make_dir_hier(path);
2984 folder_item_scan(outbox);
2985 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
2987 g_warning(_("can't save message\n"));
2991 if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
2992 g_warning(_("can't open mark file\n"));
2996 newmsginfo.msgnum = num;
2997 newmsginfo.flags.perm_flags = 0;
2998 newmsginfo.flags.tmp_flags = 0;
2999 procmsg_write_flags(&newmsginfo, fp);
3007 static gint compose_remove_reedit_target(Compose *compose)
3010 MsgInfo *msginfo = compose->targetinfo;
3012 g_return_val_if_fail(compose->mode == COMPOSE_REEDIT, -1);
3013 if (!msginfo) return -1;
3015 item = msginfo->folder;
3016 g_return_val_if_fail(item != NULL, -1);
3018 folder_item_scan(item);
3019 if (procmsg_msg_exist(msginfo) &&
3020 (item->stype == F_DRAFT || item->stype == F_QUEUE)) {
3021 if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
3022 g_warning(_("can't remove the old message\n"));
3031 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
3034 gchar *tmp, *tmp2, *queue_path;
3037 gchar buf[BUFFSIZE];
3039 static gboolean lock = FALSE;
3040 PrefsAccount *mailac = NULL, *newsac = NULL;
3042 debug_print(_("queueing message...\n"));
3043 g_return_val_if_fail(compose->account != NULL, -1);
3044 g_return_val_if_fail(compose->orig_account != NULL, -1);
3048 if(!compose_check_for_valid_recipient(compose)) {
3049 alertpanel_error(_("Recipient is not specified."));
3054 if (!compose->to_list && !compose->newsgroup_list) {
3055 g_warning(_("can't get recipient list."));
3060 if(compose->to_list) {
3061 if (compose->account->protocol != A_NNTP)
3062 mailac = compose->account;
3063 else if (compose->orig_account->protocol != A_NNTP)
3064 mailac = compose->orig_account;
3065 else if (cur_account && cur_account->protocol != A_NNTP)
3066 mailac = cur_account;
3067 else if (!(mailac = compose_current_mail_account())) {
3069 alertpanel_error(_("No account for sending mails available!"));
3074 if(compose->newsgroup_list) {
3075 if (compose->account->protocol == A_NNTP)
3076 newsac = compose->account;
3077 else if(!(newsac = compose->orig_account) || (newsac->protocol != A_NNTP)) {
3079 alertpanel_error(_("No account for posting news available!"));
3084 if (prefs_common.linewrap_at_send)
3085 compose_wrap_line_all(compose);
3087 /* write to temporary file */
3088 tmp2 = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
3089 G_DIR_SEPARATOR, (gint)compose);
3091 if (compose->bounce_filename != NULL) {
3092 if (compose_bounce_write_to_file(compose, tmp2) < 0) {
3099 if (compose_write_to_file(compose, tmp2, FALSE) < 0) {
3106 /* add queue header */
3107 tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
3108 G_DIR_SEPARATOR, (gint)compose);
3109 if ((fp = fopen(tmp, "w")) == NULL) {
3110 FILE_OP_ERROR(tmp, "fopen");
3114 if ((src_fp = fopen(tmp2, "r")) == NULL) {
3115 FILE_OP_ERROR(tmp2, "fopen");
3123 if (change_file_mode_rw(fp, tmp) < 0) {
3124 FILE_OP_ERROR(tmp, "chmod");
3125 g_warning(_("can't change file mode\n"));
3128 /* queueing variables */
3129 fprintf(fp, "AF:\n");
3130 fprintf(fp, "NF:0\n");
3131 fprintf(fp, "PS:10\n");
3132 fprintf(fp, "SRH:1\n");
3133 fprintf(fp, "SFN:\n");
3134 fprintf(fp, "DSR:\n");
3136 fprintf(fp, "MID:<%s>\n", compose->msgid);
3138 fprintf(fp, "MID:\n");
3139 fprintf(fp, "CFG:\n");
3140 fprintf(fp, "PT:0\n");
3141 fprintf(fp, "S:%s\n", compose->account->address);
3142 fprintf(fp, "RQ:\n");
3144 fprintf(fp, "SSV:%s\n", mailac->smtp_server);
3146 fprintf(fp, "SSV:\n");
3148 fprintf(fp, "NSV:%s\n", newsac->nntp_server);
3150 fprintf(fp, "NSV:\n");
3151 fprintf(fp, "SSH:\n");
3152 /* write recepient list */
3154 if(compose->to_list) {
3155 fprintf(fp, "<%s>", (gchar *)compose->to_list->data);
3156 for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
3157 fprintf(fp, ",<%s>", (gchar *)cur->data);
3160 /* write newsgroup list */
3162 if(compose->newsgroup_list) {
3163 fprintf(fp, "%s", (gchar *)compose->newsgroup_list->data);
3164 for (cur = compose->newsgroup_list->next; cur != NULL; cur = cur->next)
3165 fprintf(fp, ",%s", (gchar *)cur->data);
3168 /* Sylpheed account IDs */
3170 fprintf(fp, "MAID:%d\n", mailac->account_id);
3173 fprintf(fp, "NAID:%d\n", newsac->account_id);
3177 while (fgets(buf, sizeof(buf), src_fp) != NULL) {
3178 if (fputs(buf, fp) == EOF) {
3179 FILE_OP_ERROR(tmp, "fputs");
3190 if (fclose(fp) == EOF) {
3191 FILE_OP_ERROR(tmp, "fclose");
3200 queue = folder_get_default_queue();
3202 folder_item_scan(queue);
3203 queue_path = folder_item_get_path(queue);
3204 if (!is_dir_exist(queue_path))
3205 make_dir_hier(queue_path);
3206 if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
3207 g_warning(_("can't queue the message\n"));
3218 if (compose->mode == COMPOSE_REEDIT) {
3219 compose_remove_reedit_target(compose);
3220 if (compose->targetinfo &&
3221 compose->targetinfo->folder != queue)
3222 folderview_update_item
3223 (compose->targetinfo->folder, TRUE);
3226 if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
3227 g_warning(_("can't open mark file\n"));
3231 newmsginfo.msgnum = num;
3232 newmsginfo.flags.perm_flags = 0;
3233 newmsginfo.flags.tmp_flags = 0;
3234 procmsg_write_flags(&newmsginfo, fp);
3239 folder_item_scan(queue);
3240 folderview_update_item(queue, TRUE);
3242 if((msgnum != NULL) && (item != NULL)) {
3250 static void compose_write_attach(Compose *compose, FILE *fp)
3253 GtkCList *clist = GTK_CLIST(compose->attach_clist);
3256 gchar filename[BUFFSIZE];
3259 for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
3261 if ((attach_fp = fopen(ainfo->file, "r")) == NULL) {
3262 g_warning(_("Can't open file %s\n"), ainfo->file);
3266 fprintf(fp, "\n--%s\n", compose->boundary);
3268 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
3269 fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
3270 fprintf(fp, "Content-Disposition: inline\n");
3272 conv_encode_header(filename, sizeof(filename),
3274 fprintf(fp, "Content-Type: %s;\n"
3276 ainfo->content_type, filename);
3277 fprintf(fp, "Content-Disposition: attachment;\n"
3278 " filename=\"%s\"\n", filename);
3281 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
3282 procmime_get_encoding_str(ainfo->encoding));
3284 if (ainfo->encoding == ENC_7BIT) {
3285 gchar buf[BUFFSIZE];
3287 while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
3292 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
3294 while ((len = fread(inbuf, sizeof(gchar),