2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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>
56 #include <sys/types.h>
60 /* #include <sys/utsname.h> */
66 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
73 #include "mainwindow.h"
76 #include "addressbook.h"
77 #include "folderview.h"
80 #include "stock_pixmap.h"
84 #include "customheader.h"
85 #include "prefs_common.h"
86 #include "prefs_account.h"
87 #include "prefs_actions.h"
90 #include "procheader.h"
92 #include "statusbar.h"
99 #include "alertpanel.h"
100 #include "manage_window.h"
101 #include "gtkshruler.h"
103 #include "addr_compl.h"
104 #include "quote_fmt.h"
105 #include "template.h"
107 #include "foldersel.h"
110 # include "rfc2015.h"
120 #define N_ATTACH_COLS 3
124 COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE,
125 COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_CHARACTER,
126 COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_CHARACTER,
127 COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_WORD,
128 COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_WORD,
129 COMPOSE_CALL_GTK_STEXT_MOVE_END_OF_LINE,
130 COMPOSE_CALL_GTK_STEXT_MOVE_NEXT_LINE,
131 COMPOSE_CALL_GTK_STEXT_MOVE_PREVIOUS_LINE,
132 COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_CHARACTER,
133 COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_CHARACTER,
134 COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_WORD,
135 COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_WORD,
136 COMPOSE_CALL_GTK_STEXT_DELETE_LINE,
137 COMPOSE_CALL_GTK_STEXT_DELETE_LINE_N,
138 COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END
139 } ComposeCallGtkSTextAction;
143 PRIORITY_HIGHEST = 1,
150 #define B64_LINE_SIZE 57
151 #define B64_BUFFSIZE 77
153 #define MAX_REFERENCES_LEN 999
155 static GdkColor quote_color = {0, 0, 0, 0xbfff};
157 static GList *compose_list = NULL;
159 Compose *compose_generic_new (PrefsAccount *account,
162 GPtrArray *attach_files);
164 static Compose *compose_create (PrefsAccount *account,
166 static void compose_toolbar_create (Compose *compose,
167 GtkWidget *container);
168 static GtkWidget *compose_account_option_menu_create
170 static void compose_set_template_menu (Compose *compose);
171 static void compose_template_apply (Compose *compose,
174 static void compose_destroy (Compose *compose);
176 static void compose_entries_set (Compose *compose,
177 const gchar *mailto);
178 static gint compose_parse_header (Compose *compose,
180 static gchar *compose_parse_references (const gchar *ref,
183 static gchar *compose_quote_fmt (Compose *compose,
189 static void compose_reply_set_entry (Compose *compose,
194 followup_and_reply_to);
195 static void compose_reedit_set_entry (Compose *compose,
197 static void compose_insert_sig (Compose *compose);
198 static void compose_insert_file (Compose *compose,
200 static void compose_attach_append (Compose *compose,
203 const gchar *content_type);
204 static void compose_attach_parts (Compose *compose,
206 static void compose_wrap_line (Compose *compose);
207 static void compose_wrap_line_all (Compose *compose);
208 static void compose_wrap_line_all_full (Compose *compose,
210 static void compose_set_title (Compose *compose);
211 static void compose_select_account (Compose *compose,
212 PrefsAccount *account);
214 static PrefsAccount *compose_current_mail_account(void);
215 /* static gint compose_send (Compose *compose); */
216 static gboolean compose_check_for_valid_recipient
218 static gboolean compose_check_entries (Compose *compose,
219 gboolean check_subject);
220 static gint compose_write_to_file (Compose *compose,
223 static gint compose_write_body_to_file (Compose *compose,
225 static gint compose_remove_reedit_target (Compose *compose);
226 void compose_remove_draft (Compose *compose);
227 static gint compose_queue (Compose *compose,
230 static gint compose_queue_sub (Compose *compose,
233 gboolean check_subject);
234 static void compose_write_attach (Compose *compose,
236 static gint compose_write_headers (Compose *compose,
238 const gchar *charset,
239 EncodingType encoding,
242 static void compose_convert_header (gchar *dest,
246 static void compose_generate_msgid (Compose *compose,
250 static void compose_attach_info_free (AttachInfo *ainfo);
251 static void compose_attach_remove_selected (Compose *compose);
253 static void compose_attach_property (Compose *compose);
254 static void compose_attach_property_create (gboolean *cancelled);
255 static void attach_property_ok (GtkWidget *widget,
256 gboolean *cancelled);
257 static void attach_property_cancel (GtkWidget *widget,
258 gboolean *cancelled);
259 static gint attach_property_delete_event (GtkWidget *widget,
261 gboolean *cancelled);
262 static void attach_property_key_pressed (GtkWidget *widget,
264 gboolean *cancelled);
266 static void compose_exec_ext_editor (Compose *compose);
267 static gint compose_exec_ext_editor_real (const gchar *file);
268 static gboolean compose_ext_editor_kill (Compose *compose);
269 static void compose_input_cb (gpointer data,
271 GdkInputCondition condition);
272 static void compose_set_ext_editor_sensitive (Compose *compose,
275 static void compose_undo_state_changed (UndoMain *undostruct,
280 static gint calc_cursor_xpos (GtkSText *text,
284 static void compose_create_header_entry (Compose *compose);
285 static void compose_add_header_entry (Compose *compose, gchar *header, gchar *text);
286 static void compose_update_priority_menu_item(Compose * compose);
288 /* callback functions */
290 static gboolean compose_edit_size_alloc (GtkEditable *widget,
291 GtkAllocation *allocation,
292 GtkSHRuler *shruler);
294 static void toolbar_send_cb (GtkWidget *widget,
296 static void toolbar_send_later_cb (GtkWidget *widget,
298 static void toolbar_draft_cb (GtkWidget *widget,
300 static void toolbar_insert_cb (GtkWidget *widget,
302 static void toolbar_attach_cb (GtkWidget *widget,
304 static void toolbar_sig_cb (GtkWidget *widget,
306 static void toolbar_ext_editor_cb (GtkWidget *widget,
308 static void toolbar_linewrap_cb (GtkWidget *widget,
310 static void toolbar_address_cb (GtkWidget *widget,
313 static void account_activated (GtkMenuItem *menuitem,
316 static void attach_selected (GtkCList *clist,
321 static void attach_button_pressed (GtkWidget *widget,
322 GdkEventButton *event,
324 static void attach_key_pressed (GtkWidget *widget,
328 static void compose_send_cb (gpointer data,
331 static void compose_send_later_cb (gpointer data,
335 static void compose_draft_cb (gpointer data,
339 static void compose_attach_cb (gpointer data,
342 static void compose_insert_file_cb (gpointer data,
346 static void compose_close_cb (gpointer data,
350 static void compose_address_cb (gpointer data,
353 static void compose_template_activate_cb(GtkWidget *widget,
356 static void compose_ext_editor_cb (gpointer data,
360 static gint compose_delete_cb (GtkWidget *widget,
363 static void compose_destroy_cb (GtkWidget *widget,
366 static void compose_undo_cb (Compose *compose);
367 static void compose_redo_cb (Compose *compose);
368 static void compose_cut_cb (Compose *compose);
369 static void compose_copy_cb (Compose *compose);
370 static void compose_paste_cb (Compose *compose);
371 static void compose_paste_as_quote_cb (Compose *compose);
372 static void compose_allsel_cb (Compose *compose);
374 static void compose_gtk_stext_action_cb (Compose *compose,
375 ComposeCallGtkSTextAction action);
377 static void compose_grab_focus_cb (GtkWidget *widget,
380 static void compose_changed_cb (GtkEditable *editable,
382 static void compose_button_press_cb (GtkWidget *widget,
383 GdkEventButton *event,
386 static void compose_key_press_cb (GtkWidget *widget,
392 static void compose_toggle_to_cb (gpointer data,
395 static void compose_toggle_cc_cb (gpointer data,
398 static void compose_toggle_bcc_cb (gpointer data,
401 static void compose_toggle_replyto_cb (gpointer data,
404 static void compose_toggle_followupto_cb(gpointer data,
407 static void compose_toggle_attach_cb (gpointer data,
411 static void compose_toggle_ruler_cb (gpointer data,
415 static void compose_toggle_sign_cb (gpointer data,
418 static void compose_toggle_encrypt_cb (gpointer data,
422 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
424 static void compose_set_priority_cb (gpointer data,
428 static void compose_attach_drag_received_cb (GtkWidget *widget,
429 GdkDragContext *drag_context,
432 GtkSelectionData *data,
436 static void compose_insert_drag_received_cb (GtkWidget *widget,
437 GdkDragContext *drag_context,
440 GtkSelectionData *data,
446 static void to_activated (GtkWidget *widget,
448 static void newsgroups_activated (GtkWidget *widget,
450 static void cc_activated (GtkWidget *widget,
452 static void bcc_activated (GtkWidget *widget,
454 static void replyto_activated (GtkWidget *widget,
456 static void followupto_activated (GtkWidget *widget,
458 static void subject_activated (GtkWidget *widget,
462 static void text_activated (GtkWidget *widget,
464 static void text_inserted (GtkWidget *widget,
469 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
471 gboolean ignore_replyto,
472 gboolean followup_and_reply_to,
475 void compose_headerentry_changed_cb (GtkWidget *entry,
476 ComposeHeaderEntry *headerentry);
477 void compose_headerentry_key_press_event_cb(GtkWidget *entry,
479 ComposeHeaderEntry *headerentry);
481 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
484 static void compose_check_all (Compose *compose);
485 static void compose_highlight_all (Compose *compose);
486 static void compose_check_backwards (Compose *compose);
487 static void compose_check_forwards_go (Compose *compose);
490 static gboolean compose_send_control_enter (Compose *compose);
492 static GtkItemFactoryEntry compose_popup_entries[] =
494 {N_("/_Add..."), NULL, compose_attach_cb, 0, NULL},
495 {N_("/_Remove"), NULL, compose_attach_remove_selected, 0, NULL},
496 {N_("/---"), NULL, NULL, 0, "<Separator>"},
497 {N_("/_Property..."), NULL, compose_attach_property, 0, NULL}
500 static GtkItemFactoryEntry compose_entries[] =
502 {N_("/_File"), NULL, NULL, 0, "<Branch>"},
503 {N_("/_File/_Attach file"), "<control>M", compose_attach_cb, 0, NULL},
504 {N_("/_File/_Insert file"), "<control>I", compose_insert_file_cb, 0, NULL},
505 {N_("/_File/Insert si_gnature"), "<control>G", compose_insert_sig, 0, NULL},
506 {N_("/_File/---"), NULL, NULL, 0, "<Separator>"},
507 {N_("/_File/_Close"), "<control>W", compose_close_cb, 0, NULL},
509 {N_("/_Edit"), NULL, NULL, 0, "<Branch>"},
510 {N_("/_Edit/_Undo"), "<control>Z", compose_undo_cb, 0, NULL},
511 {N_("/_Edit/_Redo"), "<control>Y", compose_redo_cb, 0, NULL},
512 {N_("/_Edit/---"), NULL, NULL, 0, "<Separator>"},
513 {N_("/_Edit/Cu_t"), "<control>X", compose_cut_cb, 0, NULL},
514 {N_("/_Edit/_Copy"), "<control>C", compose_copy_cb, 0, NULL},
515 {N_("/_Edit/_Paste"), "<control>V", compose_paste_cb, 0, NULL},
516 {N_("/_Edit/Paste as _quotation"),
517 NULL, compose_paste_as_quote_cb, 0, NULL},
518 {N_("/_Edit/Select _all"), "<control>A", compose_allsel_cb, 0, NULL},
519 {N_("/_Edit/A_dvanced"), NULL, NULL, 0, "<Branch>"},
520 {N_("/_Edit/A_dvanced/Move a character backward"),
522 compose_gtk_stext_action_cb,
523 COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_CHARACTER,
525 {N_("/_Edit/A_dvanced/Move a character forward"),
527 compose_gtk_stext_action_cb,
528 COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_CHARACTER,
530 {N_("/_Edit/A_dvanced/Move a word backward"),
532 compose_gtk_stext_action_cb,
533 COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_WORD,
535 {N_("/_Edit/A_dvanced/Move a word forward"),
537 compose_gtk_stext_action_cb,
538 COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_WORD,
540 {N_("/_Edit/A_dvanced/Move to beginning of line"),
541 NULL, /* "<control>A" */
542 compose_gtk_stext_action_cb,
543 COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE,
545 {N_("/_Edit/A_dvanced/Move to end of line"),
547 compose_gtk_stext_action_cb,
548 COMPOSE_CALL_GTK_STEXT_MOVE_END_OF_LINE,
550 {N_("/_Edit/A_dvanced/Move to previous line"),
552 compose_gtk_stext_action_cb,
553 COMPOSE_CALL_GTK_STEXT_MOVE_PREVIOUS_LINE,
555 {N_("/_Edit/A_dvanced/Move to next line"),
557 compose_gtk_stext_action_cb,
558 COMPOSE_CALL_GTK_STEXT_MOVE_NEXT_LINE,
560 {N_("/_Edit/A_dvanced/Delete a character backward"),
562 compose_gtk_stext_action_cb,
563 COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_CHARACTER,
565 {N_("/_Edit/A_dvanced/Delete a character forward"),
567 compose_gtk_stext_action_cb,
568 COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_CHARACTER,
570 {N_("/_Edit/A_dvanced/Delete a word backward"),
571 NULL, /* "<control>W" */
572 compose_gtk_stext_action_cb,
573 COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_WORD,
575 {N_("/_Edit/A_dvanced/Delete a word forward"),
576 NULL, /* "<alt>D", */
577 compose_gtk_stext_action_cb,
578 COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_WORD,
580 {N_("/_Edit/A_dvanced/Delete line"),
582 compose_gtk_stext_action_cb,
583 COMPOSE_CALL_GTK_STEXT_DELETE_LINE,
585 {N_("/_Edit/A_dvanced/Delete entire line"),
587 compose_gtk_stext_action_cb,
588 COMPOSE_CALL_GTK_STEXT_DELETE_LINE_N,
590 {N_("/_Edit/A_dvanced/Delete to end of line"),
592 compose_gtk_stext_action_cb,
593 COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END,
595 {N_("/_Edit/---"), NULL, NULL, 0, "<Separator>"},
596 {N_("/_Edit/_Wrap current paragraph"),
597 "<control>L", compose_wrap_line, 0, NULL},
598 {N_("/_Edit/Wrap all long _lines"),
599 "<control><alt>L", compose_wrap_line_all, 0, NULL},
600 {N_("/_Edit/Edit with e_xternal editor"),
601 "<shift><control>X", compose_ext_editor_cb, 0, NULL},
603 {N_("/_Spelling"), NULL, NULL, 0, "<Branch>"},
604 {N_("/_Spelling/_Check all or check selection"),
605 NULL, compose_check_all, 0, NULL},
606 {N_("/_Spelling/_Highlight all misspelled words"),
607 NULL, compose_highlight_all, 0, NULL},
608 {N_("/_Spelling/Check _backwards misspelled word"),
609 NULL, compose_check_backwards , 0, NULL},
610 {N_("/_Spelling/_Forward to next misspelled word"),
611 NULL, compose_check_forwards_go, 0, NULL},
612 {N_("/_Spelling/---"), NULL, NULL, 0, "<Separator>"},
613 {N_("/_Spelling/_Spelling Configuration"),
614 NULL, NULL, 0, "<Branch>"},
616 #if 0 /* NEW COMPOSE GUI */
617 {N_("/_View"), NULL, NULL, 0, "<Branch>"},
618 {N_("/_View/_To"), NULL, compose_toggle_to_cb , 0, "<ToggleItem>"},
619 {N_("/_View/_Cc"), NULL, compose_toggle_cc_cb , 0, "<ToggleItem>"},
620 {N_("/_View/_Bcc"), NULL, compose_toggle_bcc_cb , 0, "<ToggleItem>"},
621 {N_("/_View/_Reply to"), NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
622 {N_("/_View/---"), NULL, NULL, 0, "<Separator>"},
623 {N_("/_View/_Followup to"), NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
624 {N_("/_View/---"), NULL, NULL, 0, "<Separator>"},
625 {N_("/_View/R_uler"), NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
626 {N_("/_View/---"), NULL, NULL, 0, "<Separator>"},
627 {N_("/_View/_Attachment"), NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
629 {N_("/_Message"), NULL, NULL, 0, "<Branch>"},
630 {N_("/_Message/_Send"), "<control>Return",
631 compose_send_cb, 0, NULL},
632 {N_("/_Message/Send _later"), "<shift><control>S",
633 compose_send_later_cb, 0, NULL},
634 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
635 {N_("/_Message/Save to _draft folder"),
636 "<shift><control>D", compose_draft_cb, 0, NULL},
637 {N_("/_Message/Save and _keep editing"),
638 "<control>S", compose_draft_cb, 1, NULL},
639 #if 0 /* NEW COMPOSE GUI */
640 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
641 {N_("/_Message/_To"), NULL, compose_toggle_to_cb , 0, "<ToggleItem>"},
642 {N_("/_Message/_Cc"), NULL, compose_toggle_cc_cb , 0, "<ToggleItem>"},
643 {N_("/_Message/_Bcc"), NULL, compose_toggle_bcc_cb , 0, "<ToggleItem>"},
644 {N_("/_Message/_Reply to"), NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
645 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
646 {N_("/_Message/_Followup to"), NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
647 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
648 {N_("/_Message/_Attach"), NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
651 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
652 {N_("/_Message/Si_gn"), NULL, compose_toggle_sign_cb , 0, "<ToggleItem>"},
653 {N_("/_Message/_Encrypt"), NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
654 #endif /* USE_GPGME */
655 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
656 {N_("/_Message/_Priority"), NULL, NULL, 0, "<Branch>"},
657 {N_("/_Message/Priority/_Highest"), NULL, compose_set_priority_cb, PRIORITY_HIGHEST, "<RadioItem>"},
658 {N_("/_Message/Priority/Hi_gh"), NULL, compose_set_priority_cb, PRIORITY_HIGH, "/Message/Priority/Highest"},
659 {N_("/_Message/Priority/_Normal"), NULL, compose_set_priority_cb, PRIORITY_NORMAL, "/Message/Priority/Highest"},
660 {N_("/_Message/Priority/Lo_w"), NULL, compose_set_priority_cb, PRIORITY_LOW, "/Message/Priority/Highest"},
661 {N_("/_Message/Priority/_Lowest"), NULL, compose_set_priority_cb, PRIORITY_LOWEST, "/Message/Priority/Highest"},
662 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
663 {N_("/_Message/_Request Return Receipt"), NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
664 {N_("/_Tools"), NULL, NULL, 0, "<Branch>"},
665 {N_("/_Tools/Show _ruler"), NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
666 {N_("/_Tools/_Address book"), "<shift><control>A", compose_address_cb , 0, NULL},
667 {N_("/_Tools/_Template"), NULL, NULL, 0, "<Branch>"},
668 {N_("/_Tools/Actio_ns"), NULL, NULL, 0, "<Branch>"},
669 {N_("/_Help"), NULL, NULL, 0, "<Branch>"},
670 {N_("/_Help/_About"), NULL, about_show, 0, NULL}
673 static GtkTargetEntry compose_mime_types[] =
675 {"text/uri-list", 0, 0}
678 Compose *compose_new(PrefsAccount *account, const gchar *mailto,
679 GPtrArray *attach_files)
681 return compose_generic_new(account, mailto, NULL, attach_files);
684 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
686 return compose_generic_new(account, NULL, item, NULL);
689 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item,
690 GPtrArray *attach_files)
694 GtkItemFactory *ifactory;
695 gboolean grab_focus_on_last = TRUE;
697 if (item && item->prefs && item->prefs->enable_default_account)
698 account = account_find_from_id(item->prefs->default_account);
700 if (!account) account = cur_account;
701 g_return_val_if_fail(account != NULL, NULL);
703 compose = compose_create(account, COMPOSE_NEW);
704 ifactory = gtk_item_factory_from_widget(compose->menubar);
706 compose->replyinfo = NULL;
708 text = GTK_STEXT(compose->text);
709 gtk_stext_freeze(text);
711 if (prefs_common.auto_sig)
712 compose_insert_sig(compose);
713 gtk_editable_set_position(GTK_EDITABLE(text), 0);
714 gtk_stext_set_point(text, 0);
716 gtk_stext_thaw(text);
718 /* workaround for initial XIM problem */
719 gtk_widget_grab_focus(compose->text);
720 gtkut_widget_wait_for_draw(compose->text);
722 if (account->protocol != A_NNTP) {
723 if (mailto && *mailto != '\0') {
724 compose_entries_set(compose, mailto);
726 } else if (item && item->prefs->enable_default_to) {
727 compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
728 compose_entry_select(compose, item->prefs->default_to);
729 grab_focus_on_last = FALSE;
731 if (item && item->ret_rcpt) {
732 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
736 compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
739 * CLAWS: just don't allow return receipt request, even if the user
740 * may want to send an email. simple but foolproof.
742 menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
749 for (i = 0; i < attach_files->len; i++) {
750 file = g_ptr_array_index(attach_files, i);
751 compose_attach_append(compose, file, file, NULL);
755 compose_show_first_last_header(compose, TRUE);
757 /* Set save folder */
758 if (item && item->prefs && item->prefs->save_copy_to_folder) {
759 gchar *folderidentifier;
761 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
762 folderidentifier = folder_item_get_identifier(item);
763 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
764 g_free(folderidentifier);
767 /* Grab focus on last header only if no default_to was set */
768 if (grab_focus_on_last)
769 gtk_widget_grab_focus(compose->header_last->entry);
771 if (prefs_common.auto_exteditor)
772 compose_exec_ext_editor(compose);
777 #define CHANGE_FLAGS(msginfo) \
779 if (msginfo->folder->folder->change_flags != NULL) \
780 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
786 Compose *compose_new_followup_and_replyto(PrefsAccount *account,
787 const gchar *followupto, gchar * to)
791 if (!account) account = cur_account;
792 g_return_val_if_fail(account != NULL, NULL);
793 g_return_val_if_fail(account->protocol != A_NNTP, NULL);
795 compose = compose_create(account, COMPOSE_NEW);
797 if (prefs_common.auto_sig)
798 compose_insert_sig(compose);
799 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
800 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
802 compose_entry_append(compose, to, COMPOSE_TO);
803 compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
804 gtk_widget_grab_focus(compose->subject_entry);
810 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
811 gboolean ignore_replyto, const gchar *body)
813 compose_generic_reply(msginfo, quote, to_all, ignore_replyto, FALSE,
817 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
819 gboolean ignore_replyto,
822 compose_generic_reply(msginfo, quote, to_all, ignore_replyto, TRUE,
826 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
828 gboolean ignore_replyto,
829 gboolean followup_and_reply_to,
833 PrefsAccount *account;
834 PrefsAccount *reply_account;
837 g_return_if_fail(msginfo != NULL);
838 g_return_if_fail(msginfo->folder != NULL);
841 /* select the account set in folderitem's property (if enabled) */
842 if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
843 account = account_find_from_id(msginfo->folder->prefs->default_account);
845 /* select the account for the whole folder (IMAP / NNTP) */
847 /* FIXME: this is not right, because folder may be nested. we should
848 * ascend the tree until we find a parent with proper account
850 account = msginfo->folder->folder->account;
852 /* select account by to: and cc: header if enabled */
853 if (prefs_common.reply_account_autosel) {
854 if (!account && msginfo->to) {
856 Xstrdup_a(to, msginfo->to, return);
858 account = account_find_from_address(to);
862 if (!get_header_from_msginfo(msginfo, cc, sizeof(cc), "CC:")) { /* Found a CC header */
864 account = account_find_from_address(cc);
869 /* select current account */
870 if (!account) account = cur_account;
871 g_return_if_fail(account != NULL);
873 if (ignore_replyto && account->protocol == A_NNTP &&
874 !followup_and_reply_to) {
876 account_find_from_address(account->address);
878 reply_account = compose_current_mail_account();
882 reply_account = account;
884 compose = compose_create(account, COMPOSE_REPLY);
885 compose->replyinfo = procmsg_msginfo_new_ref(msginfo);
887 #if 0 /* NEW COMPOSE GUI */
888 if (followup_and_reply_to) {
889 gtk_widget_show(compose->to_hbox);
890 gtk_widget_show(compose->to_entry);
891 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
892 compose->use_to = TRUE;
895 if (msginfo->folder && msginfo->folder->ret_rcpt) {
896 GtkItemFactory *ifactory;
898 ifactory = gtk_item_factory_from_widget(compose->menubar);
899 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
902 /* Set save folder */
903 if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
904 gchar *folderidentifier;
906 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
907 folderidentifier = folder_item_get_identifier(msginfo->folder);
908 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
909 g_free(folderidentifier);
912 if (compose_parse_header(compose, msginfo) < 0) return;
913 compose_reply_set_entry(compose, msginfo, to_all, ignore_replyto,
914 followup_and_reply_to);
915 compose_show_first_last_header(compose, TRUE);
917 text = GTK_STEXT(compose->text);
918 gtk_stext_freeze(text);
924 if (prefs_common.quotemark && *prefs_common.quotemark)
925 qmark = prefs_common.quotemark;
929 quote_str = compose_quote_fmt(compose, msginfo,
930 prefs_common.quotefmt,
934 if (prefs_common.auto_sig)
935 compose_insert_sig(compose);
937 if (quote && prefs_common.linewrap_quote)
938 compose_wrap_line_all(compose);
940 gtk_editable_set_position(GTK_EDITABLE(text), 0);
941 gtk_stext_set_point(text, 0);
943 gtk_stext_thaw(text);
944 gtk_widget_grab_focus(compose->text);
946 if (prefs_common.auto_exteditor)
947 compose_exec_ext_editor(compose);
950 static void set_toolbar_style(Compose *compose);
952 #define INSERT_FW_HEADER(var, hdr) \
953 if (msginfo->var && *msginfo->var) { \
954 gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
955 gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
956 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
959 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
960 gboolean as_attach, const gchar *body)
963 /* PrefsAccount *account; */
966 g_return_val_if_fail(msginfo != NULL, NULL);
967 g_return_val_if_fail(msginfo->folder != NULL, NULL);
969 account = msginfo->folder->folder->account;
970 if (!account && msginfo->to && prefs_common.forward_account_autosel) {
972 Xstrdup_a(to, msginfo->to, return NULL);
974 account = account_find_from_address(to);
977 if (!account && prefs_common.forward_account_autosel) {
979 if (!get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
981 account = account_find_from_address(cc);
985 if (account == NULL) {
986 account = cur_account;
988 account = msginfo->folder->folder->account;
989 if (!account) account = cur_account;
992 g_return_val_if_fail(account != NULL, NULL);
994 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
995 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
996 if (MSG_IS_IMAP(msginfo->flags))
997 imap_msg_unset_perm_flags(msginfo, MSG_REPLIED);
998 CHANGE_FLAGS(msginfo);
1000 compose = compose_create(account, COMPOSE_FORWARD);
1002 if (msginfo->subject && *msginfo->subject) {
1003 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
1004 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
1008 text = GTK_STEXT(compose->text);
1009 gtk_stext_freeze(text);
1015 msgfile = procmsg_get_message_file_path(msginfo);
1016 if (!is_file_exist(msgfile))
1017 g_warning(_("%s: file not exist\n"), msgfile);
1019 compose_attach_append(compose, msgfile, msgfile,
1027 if (prefs_common.fw_quotemark &&
1028 *prefs_common.fw_quotemark)
1029 qmark = prefs_common.fw_quotemark;
1033 quote_str = compose_quote_fmt(compose, msginfo,
1034 prefs_common.fw_quotefmt,
1036 compose_attach_parts(compose, msginfo);
1039 if (prefs_common.auto_sig)
1040 compose_insert_sig(compose);
1042 if (prefs_common.linewrap_quote)
1043 compose_wrap_line_all(compose);
1045 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1046 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1048 gtk_stext_thaw(text);
1049 #if 0 /* NEW COMPOSE GUI */
1050 if (account->protocol != A_NNTP)
1051 gtk_widget_grab_focus(compose->to_entry);
1053 gtk_widget_grab_focus(compose->newsgroups_entry);
1055 gtk_widget_grab_focus(compose->header_last->entry);
1057 if (prefs_common.auto_exteditor)
1058 compose_exec_ext_editor(compose);
1063 #undef INSERT_FW_HEADER
1065 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1072 g_return_val_if_fail(msginfo_list != NULL, NULL);
1074 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1075 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
1079 if (account == NULL) {
1080 account = cur_account;
1082 account = msginfo->folder->folder->account;
1083 if (!account) account = cur_account;
1086 g_return_val_if_fail(account != NULL, NULL);
1088 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1089 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1090 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1091 CHANGE_FLAGS(((MsgInfo *)msginfo->data));
1094 compose = compose_create(account, COMPOSE_FORWARD);
1096 text = GTK_STEXT(compose->text);
1097 gtk_stext_freeze(text);
1099 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1100 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1101 if (!is_file_exist(msgfile))
1102 g_warning(_("%s: file not exist\n"), msgfile);
1104 compose_attach_append(compose, msgfile, msgfile,
1109 if (prefs_common.auto_sig)
1110 compose_insert_sig(compose);
1112 if (prefs_common.linewrap_quote)
1113 compose_wrap_line_all(compose);
1115 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1116 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1118 gtk_stext_thaw(text);
1119 gtk_widget_grab_focus(compose->header_last->entry);
1121 #if 0 /* NEW COMPOSE GUI */
1122 if (account->protocol != A_NNTP)
1123 gtk_widget_grab_focus(compose->to_entry);
1125 gtk_widget_grab_focus(compose->newsgroups_entry);
1131 void compose_reedit(MsgInfo *msginfo)
1134 PrefsAccount *account = NULL;
1137 gchar buf[BUFFSIZE];
1139 g_return_if_fail(msginfo != NULL);
1140 g_return_if_fail(msginfo->folder != NULL);
1142 if (msginfo->folder->stype == F_QUEUE) {
1143 gchar queueheader_buf[BUFFSIZE];
1146 /* Select Account from queue headers */
1147 if (!get_header_from_msginfo(msginfo, queueheader_buf,
1148 sizeof(queueheader_buf), "NAID:")) {
1149 id = atoi(&queueheader_buf[5]);
1150 account = account_find_from_id(id);
1152 if (!account && !get_header_from_msginfo(msginfo, queueheader_buf,
1153 sizeof(queueheader_buf), "MAID:")) {
1154 id = atoi(&queueheader_buf[5]);
1155 account = account_find_from_id(id);
1157 if (!account && !get_header_from_msginfo(msginfo, queueheader_buf,
1158 sizeof(queueheader_buf), "S:")) {
1159 account = account_find_from_address(queueheader_buf);
1162 account = msginfo->folder->folder->account;
1164 if (!account && prefs_common.reedit_account_autosel) {
1165 gchar from[BUFFSIZE];
1166 if (!get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
1167 extract_address(from);
1168 account = account_find_from_address(from);
1171 if (!account) account = cur_account;
1172 g_return_if_fail(account != NULL);
1174 compose = compose_create(account, COMPOSE_REEDIT);
1175 compose->targetinfo = procmsg_msginfo_copy(msginfo);
1177 if (msginfo->folder->stype == F_QUEUE
1178 || msginfo->folder->stype == F_DRAFT) {
1179 gchar queueheader_buf[BUFFSIZE];
1181 /* Set message save folder */
1182 if (!get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1185 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1186 gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1187 gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1191 if (compose_parse_header(compose, msginfo) < 0) return;
1192 compose_reedit_set_entry(compose, msginfo);
1194 text = GTK_STEXT(compose->text);
1195 gtk_stext_freeze(text);
1197 if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1198 g_warning(_("Can't get text part\n"));
1200 while (fgets(buf, sizeof(buf), fp) != NULL)
1201 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1204 compose_attach_parts(compose, msginfo);
1206 gtk_stext_thaw(text);
1207 gtk_widget_grab_focus(compose->text);
1209 if (prefs_common.auto_exteditor)
1210 compose_exec_ext_editor(compose);
1213 Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
1217 GtkItemFactory *ifactory;
1219 g_return_val_if_fail(msginfo != NULL, NULL);
1222 account = cur_account;
1223 g_return_val_if_fail(account != NULL, NULL);
1225 compose = compose_create(account, COMPOSE_REDIRECT);
1226 ifactory = gtk_item_factory_from_widget(compose->menubar);
1228 compose->replyinfo = NULL;
1230 compose_show_first_last_header(compose, TRUE);
1232 gtk_widget_grab_focus(compose->header_last->entry);
1234 filename = procmsg_get_message_file(msginfo);
1235 if (filename == NULL)
1238 compose->redirect_filename = filename;
1240 if (msginfo->subject)
1241 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
1243 gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
1245 gtk_stext_freeze(GTK_STEXT(compose->text));
1246 compose_quote_fmt(compose, msginfo, "%M", NULL, NULL);
1247 gtk_editable_set_editable(GTK_EDITABLE(compose->text), FALSE);
1248 gtk_stext_thaw(GTK_STEXT(compose->text));
1250 ifactory = gtk_item_factory_from_widget(compose->popupmenu);
1251 menu_set_sensitive(ifactory, "/Add...", FALSE);
1252 menu_set_sensitive(ifactory, "/Remove", FALSE);
1253 menu_set_sensitive(ifactory, "/Property...", FALSE);
1255 ifactory = gtk_item_factory_from_widget(compose->menubar);
1256 menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
1257 menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
1258 menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
1259 menu_set_sensitive(ifactory, "/Edit/Paste", FALSE);
1260 menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", FALSE);
1261 menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", FALSE);
1262 menu_set_sensitive(ifactory, "/Edit/Edit with external editor", FALSE);
1263 menu_set_sensitive(ifactory, "/Message/Attach", FALSE);
1265 menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
1266 menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
1268 menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
1269 menu_set_sensitive(ifactory, "/Tools/Template", FALSE);
1271 gtk_widget_set_sensitive(compose->insert_btn, FALSE);
1272 gtk_widget_set_sensitive(compose->attach_btn, FALSE);
1273 gtk_widget_set_sensitive(compose->sig_btn, FALSE);
1274 gtk_widget_set_sensitive(compose->exteditor_btn, FALSE);
1275 gtk_widget_set_sensitive(compose->linewrap_btn, FALSE);
1280 GList *compose_get_compose_list(void)
1282 return compose_list;
1285 void compose_entry_append(Compose *compose, const gchar *address,
1286 ComposeEntryType type)
1290 if (!address || *address == '\0') return;
1292 #if 0 /* NEW COMPOSE GUI */
1295 entry = GTK_ENTRY(compose->cc_entry);
1298 entry = GTK_ENTRY(compose->bcc_entry);
1300 case COMPOSE_NEWSGROUPS:
1301 entry = GTK_ENTRY(compose->newsgroups_entry);
1305 entry = GTK_ENTRY(compose->to_entry);
1309 text = gtk_entry_get_text(entry);
1311 gtk_entry_append_text(entry, ", ");
1312 gtk_entry_append_text(entry, address);
1320 header = N_("Bcc:");
1322 case COMPOSE_REPLYTO:
1323 header = N_("Reply-To:");
1325 case COMPOSE_NEWSGROUPS:
1326 header = N_("Newsgroups:");
1328 case COMPOSE_FOLLOWUPTO:
1329 header = N_( "Followup-To:");
1336 header = prefs_common.trans_hdr ? gettext(header) : header;
1338 compose_add_header_entry(compose, header, (gchar *)address);
1341 void compose_entry_select (Compose *compose, const gchar *mailto)
1343 GSList *header_list;
1345 for (header_list = compose->header_list; header_list != NULL; header_list = header_list->next) {
1346 GtkEntry * entry = GTK_ENTRY(((ComposeHeaderEntry *)header_list->data)->entry);
1348 if (gtk_entry_get_text(entry) && !g_strcasecmp(gtk_entry_get_text(entry), mailto)) {
1349 gtk_entry_select_region(entry, 0, -1);
1350 gtk_widget_grab_focus(GTK_WIDGET(entry));
1355 static void compose_entries_set(Compose *compose, const gchar *mailto)
1357 gchar *subject = NULL;
1365 Xstrdup_a(tmp_mailto, mailto, return);
1369 p = strchr(tmp_mailto, '?');
1376 gchar *field, *value;
1393 if (*value == '\0') continue;
1395 if (!g_strcasecmp(field, "subject")) {
1396 Xalloca(subject, strlen(value) + 1, return);
1397 decode_uri(subject, value);
1398 } else if (!g_strcasecmp(field, "cc")) {
1400 } else if (!g_strcasecmp(field, "bcc")) {
1402 } else if (!g_strcasecmp(field, "body")) {
1403 Xalloca(body, strlen(value) + 1, return);
1404 decode_uri(body, value);
1409 compose_entry_append(compose, to, COMPOSE_TO);
1411 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1413 compose_entry_append(compose, cc, COMPOSE_CC);
1415 compose_entry_append(compose, bcc, COMPOSE_BCC);
1417 gtk_stext_insert(GTK_STEXT(compose->text),
1418 NULL, NULL, NULL, body, -1);
1419 gtk_stext_insert(GTK_STEXT(compose->text),
1420 NULL, NULL, NULL, "\n", 1);
1424 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1426 static HeaderEntry hentry[] = {{"Reply-To:", NULL, TRUE},
1427 {"Cc:", NULL, FALSE},
1428 {"References:", NULL, FALSE},
1429 {"Bcc:", NULL, FALSE},
1430 {"Newsgroups:", NULL, FALSE},
1431 {"Followup-To:", NULL, FALSE},
1432 {"X-Mailing-List:", NULL, FALSE},
1433 {"X-BeenThere:", NULL, FALSE},
1434 {"X-Priority:", NULL, FALSE},
1435 {NULL, NULL, FALSE}};
1445 H_X_MAILING_LIST = 6,
1452 g_return_val_if_fail(msginfo != NULL, -1);
1454 if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1455 procheader_get_header_fields(fp, hentry);
1458 if (hentry[H_REPLY_TO].body != NULL) {
1459 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1460 compose->replyto = hentry[H_REPLY_TO].body;
1461 hentry[H_REPLY_TO].body = NULL;
1463 if (hentry[H_CC].body != NULL) {
1464 conv_unmime_header_overwrite(hentry[H_CC].body);
1465 compose->cc = hentry[H_CC].body;
1466 hentry[H_CC].body = NULL;
1468 if (hentry[H_X_MAILING_LIST].body != NULL) {
1469 /* this is good enough to parse debian-devel */
1470 gchar *buf = g_malloc(strlen(hentry[H_X_MAILING_LIST].body) + 1);
1471 g_return_val_if_fail(buf != NULL, -1 );
1472 if (1 == sscanf(hentry[H_X_MAILING_LIST].body, "<%[^>]>", buf))
1473 compose->mailinglist = g_strdup(buf);
1475 g_free(hentry[H_X_MAILING_LIST].body);
1476 hentry[H_X_MAILING_LIST].body = NULL ;
1478 if (hentry[H_X_BEENTHERE].body != NULL) {
1479 /* this is good enough to parse the sylpheed-claws lists */
1480 gchar *buf = g_malloc(strlen(hentry[H_X_BEENTHERE].body) + 1);
1481 g_return_val_if_fail(buf != NULL, -1 );
1482 if (1 == sscanf(hentry[H_X_BEENTHERE].body, "%[^>]", buf))
1483 compose->mailinglist = g_strdup(buf);
1485 g_free(hentry[H_X_BEENTHERE].body);
1486 hentry[H_X_BEENTHERE].body = NULL ;
1488 if (hentry[H_REFERENCES].body != NULL) {
1489 if (compose->mode == COMPOSE_REEDIT)
1490 compose->references = hentry[H_REFERENCES].body;
1492 compose->references = compose_parse_references
1493 (hentry[H_REFERENCES].body, msginfo->msgid);
1494 g_free(hentry[H_REFERENCES].body);
1496 hentry[H_REFERENCES].body = NULL;
1498 if (hentry[H_BCC].body != NULL) {
1499 if (compose->mode == COMPOSE_REEDIT) {
1500 conv_unmime_header_overwrite(hentry[H_BCC].body);
1501 compose->bcc = hentry[H_BCC].body;
1503 g_free(hentry[H_BCC].body);
1504 hentry[H_BCC].body = NULL;
1506 if (hentry[H_NEWSGROUPS].body != NULL) {
1507 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1508 hentry[H_NEWSGROUPS].body = NULL;
1510 if (hentry[H_FOLLOWUP_TO].body != NULL) {
1511 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1512 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1513 hentry[H_FOLLOWUP_TO].body = NULL;
1516 if (compose->mode == COMPOSE_REEDIT)
1517 if (hentry[H_X_PRIORITY].body != NULL) {
1520 priority = atoi(hentry[H_X_PRIORITY].body);
1521 g_free(hentry[H_X_PRIORITY].body);
1523 hentry[H_X_PRIORITY].body = NULL;
1525 if (priority < PRIORITY_HIGHEST ||
1526 priority > PRIORITY_LOWEST)
1527 priority = PRIORITY_NORMAL;
1529 compose->priority = priority;
1532 if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1533 compose->inreplyto = g_strdup(msginfo->inreplyto);
1534 else if (compose->mode != COMPOSE_REEDIT &&
1535 msginfo->msgid && *msginfo->msgid) {
1536 compose->inreplyto = g_strdup(msginfo->msgid);
1538 if (!compose->references) {
1539 if (msginfo->inreplyto && *msginfo->inreplyto)
1540 compose->references =
1541 g_strdup_printf("<%s>\n\t<%s>",
1545 compose->references =
1546 g_strconcat("<", msginfo->msgid, ">",
1554 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1556 GSList *ref_id_list, *cur;
1560 ref_id_list = references_list_append(NULL, ref);
1561 if (!ref_id_list) return NULL;
1562 if (msgid && *msgid)
1563 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1568 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1569 /* "<" + Message-ID + ">" + CR+LF+TAB */
1570 len += strlen((gchar *)cur->data) + 5;
1572 if (len > MAX_REFERENCES_LEN) {
1573 /* remove second message-ID */
1574 if (ref_id_list && ref_id_list->next &&
1575 ref_id_list->next->next) {
1576 g_free(ref_id_list->next->data);
1577 ref_id_list = g_slist_remove
1578 (ref_id_list, ref_id_list->next->data);
1580 slist_free_strings(ref_id_list);
1581 g_slist_free(ref_id_list);
1588 new_ref = g_string_new("");
1589 for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1590 if (new_ref->len > 0)
1591 g_string_append(new_ref, "\n\t");
1592 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1595 slist_free_strings(ref_id_list);
1596 g_slist_free(ref_id_list);
1598 new_ref_str = new_ref->str;
1599 g_string_free(new_ref, FALSE);
1604 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1605 const gchar *fmt, const gchar *qmark,
1608 GtkSText *text = GTK_STEXT(compose->text);
1609 static MsgInfo dummyinfo;
1610 gchar *quote_str = NULL;
1616 msginfo = &dummyinfo;
1618 if (qmark != NULL) {
1619 quote_fmt_init(msginfo, NULL, NULL);
1620 quote_fmt_scan_string(qmark);
1623 buf = quote_fmt_get_buffer();
1625 alertpanel_error(_("Quote mark format error."));
1627 Xstrdup_a(quote_str, buf, return NULL)
1630 if (fmt && *fmt != '\0') {
1631 quote_fmt_init(msginfo, quote_str, body);
1632 quote_fmt_scan_string(fmt);
1635 buf = quote_fmt_get_buffer();
1637 alertpanel_error(_("Message reply/forward format error."));
1643 gtk_stext_freeze(text);
1645 for (p = buf; *p != '\0'; ) {
1646 lastp = strchr(p, '\n');
1647 len = lastp ? lastp - p + 1 : -1;
1648 gtk_stext_insert(text, NULL, NULL, NULL, p, len);
1655 gtk_stext_thaw(text);
1660 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1661 gboolean to_all, gboolean ignore_replyto,
1662 gboolean followup_and_reply_to)
1664 GSList *cc_list = NULL;
1667 gchar *replyto = NULL;
1668 GHashTable *to_table;
1670 g_return_if_fail(compose->account != NULL);
1671 g_return_if_fail(msginfo != NULL);
1673 if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1674 compose_entry_append(compose,
1675 ((compose->replyto && !ignore_replyto)
1677 : (compose->mailinglist && !ignore_replyto)
1678 ? compose->mailinglist
1679 : msginfo->from ? msginfo->from : ""),
1682 if (compose->account->protocol == A_NNTP) {
1684 compose_entry_append
1685 (compose, msginfo->from ? msginfo->from : "",
1688 if (compose->followup_to && !strncmp(compose->followup_to,"poster",6)) {
1689 compose_entry_append
1691 ((compose->replyto && !ignore_replyto)
1693 : (compose->mailinglist && !ignore_replyto)
1694 ? compose->mailinglist
1695 : msginfo->from ? msginfo->from : ""),
1698 compose_entry_append
1700 compose->followup_to ? compose->followup_to
1701 : compose->newsgroups ? compose->newsgroups
1703 COMPOSE_NEWSGROUPS);
1708 if (msginfo->subject && *msginfo->subject) {
1709 gchar *buf, *buf2, *p;
1711 buf = g_strdup(msginfo->subject);
1712 while (!strncasecmp(buf, "Re:", 3)) {
1714 while (isspace(*p)) p++;
1715 memmove(buf, p, strlen(p) + 1);
1718 buf2 = g_strdup_printf("Re: %s", buf);
1719 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1723 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1725 if (!to_all || compose->account->protocol == A_NNTP) return;
1727 if (compose->replyto) {
1728 Xstrdup_a(replyto, compose->replyto, return);
1729 extract_address(replyto);
1731 if (msginfo->from) {
1732 Xstrdup_a(from, msginfo->from, return);
1733 extract_address(from);
1736 if (compose->mailinglist && from) {
1737 cc_list = address_list_append(cc_list, from);
1740 if (replyto && from)
1741 cc_list = address_list_append(cc_list, from);
1743 if (!compose->mailinglist)
1744 cc_list = address_list_append(cc_list, msginfo->to);
1746 cc_list = address_list_append(cc_list, compose->cc);
1748 to_table = g_hash_table_new(g_str_hash, g_str_equal);
1750 g_hash_table_insert(to_table, replyto, GINT_TO_POINTER(1));
1751 if (compose->account)
1752 g_hash_table_insert(to_table, compose->account->address,
1753 GINT_TO_POINTER(1));
1755 /* remove address on To: and that of current account */
1756 for (cur = cc_list; cur != NULL; ) {
1757 GSList *next = cur->next;
1759 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1760 cc_list = g_slist_remove(cc_list, cur->data);
1762 g_hash_table_insert(to_table, cur->data, cur);
1766 g_hash_table_destroy(to_table);
1769 for (cur = cc_list; cur != NULL; cur = cur->next)
1770 compose_entry_append(compose, (gchar *)cur->data,
1772 slist_free_strings(cc_list);
1773 g_slist_free(cc_list);
1777 #define SET_ENTRY(entry, str) \
1780 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1783 #define SET_ADDRESS(type, str) \
1786 compose_entry_append(compose, str, type); \
1789 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1791 g_return_if_fail(msginfo != NULL);
1793 SET_ENTRY(subject_entry, msginfo->subject);
1794 SET_ADDRESS(COMPOSE_TO, msginfo->to);
1795 SET_ADDRESS(COMPOSE_CC, compose->cc);
1796 SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1797 SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1799 compose_update_priority_menu_item(compose);
1801 compose_show_first_last_header(compose, TRUE);
1803 #if 0 /* NEW COMPOSE GUI */
1805 GtkItemFactory *ifactory;
1806 GtkWidget *menuitem;
1808 ifactory = gtk_item_factory_from_widget(compose->menubar);
1809 menuitem = gtk_item_factory_get_item(ifactory, "/View/Bcc");
1810 gtk_check_menu_item_set_active
1811 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1813 if (compose->replyto) {
1814 GtkItemFactory *ifactory;
1815 GtkWidget *menuitem;
1817 ifactory = gtk_item_factory_from_widget(compose->menubar);
1818 menuitem = gtk_item_factory_get_item
1819 (ifactory, "/View/Reply to");
1820 gtk_check_menu_item_set_active
1821 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1829 static void compose_exec_sig(Compose *compose, gchar *sigfile)
1833 size_t buf_len = 128;
1835 if (strlen(sigfile) < 2)
1838 sigprg = popen(sigfile+1, "r");
1841 buf = g_malloc(buf_len);
1844 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1845 "Unable to insert signature (malloc failed)\n", -1);
1851 while (!feof(sigprg)) {
1852 bzero(buf, buf_len);
1853 fread(buf, buf_len-1, 1, sigprg);
1854 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
1862 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1863 "Can't exec file: ", -1);
1864 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1869 static void compose_insert_sig(Compose *compose)
1873 if (compose->account && compose->account->sig_path)
1874 sigfile = g_strdup(compose->account->sig_path);
1876 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1877 DEFAULT_SIGNATURE, NULL);
1879 if (!is_file_or_fifo_exist(sigfile) && sigfile[0] != '|') {
1884 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1885 if (prefs_common.sig_sep) {
1886 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1887 prefs_common.sig_sep, -1);
1888 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1892 if (sigfile[0] == '|')
1893 compose_exec_sig(compose, sigfile);
1895 compose_insert_file(compose, sigfile);
1899 static void compose_insert_file(Compose *compose, const gchar *file)
1901 GtkSText *text = GTK_STEXT(compose->text);
1902 gchar buf[BUFFSIZE];
1906 g_return_if_fail(file != NULL);
1908 if ((fp = fopen(file, "rb")) == NULL) {
1909 FILE_OP_ERROR(file, "fopen");
1913 gtk_stext_freeze(text);
1915 while (fgets(buf, sizeof(buf), fp) != NULL) {
1916 /* strip <CR> if DOS/Windows file,
1917 replace <CR> with <LF> if Macintosh file. */
1920 if (len > 0 && buf[len - 1] != '\n') {
1922 if (buf[len] == '\r') buf[len] = '\n';
1924 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1927 gtk_stext_thaw(text);
1932 static void compose_attach_append(Compose *compose, const gchar *file,
1933 const gchar *filename,
1934 const gchar *content_type)
1937 gchar *text[N_ATTACH_COLS];
1942 if (!is_file_exist(file)) {
1943 g_warning(_("File %s doesn't exist\n"), file);
1946 if ((size = get_file_size(file)) < 0) {
1947 g_warning(_("Can't get file size of %s\n"), file);
1951 alertpanel_notice(_("File %s is empty."), file);
1954 if ((fp = fopen(file, "rb")) == NULL) {
1955 alertpanel_error(_("Can't read %s."), file);
1960 #if 0 /* NEW COMPOSE GUI */
1961 if (!compose->use_attach) {
1962 GtkItemFactory *ifactory;
1963 GtkWidget *menuitem;
1965 ifactory = gtk_item_factory_from_widget(compose->menubar);
1966 menuitem = gtk_item_factory_get_item(ifactory,
1967 "/View/Attachment");
1968 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1972 ainfo = g_new0(AttachInfo, 1);
1973 ainfo->file = g_strdup(file);
1976 ainfo->content_type = g_strdup(content_type);
1977 if (!strcasecmp(content_type, "message/rfc822")) {
1978 ainfo->encoding = ENC_7BIT;
1979 ainfo->name = g_strdup_printf
1981 g_basename(filename ? filename : file));
1983 if (!g_strncasecmp(content_type, "text", 4))
1985 procmime_get_encoding_for_file(file);
1987 ainfo->encoding = ENC_BASE64;
1988 ainfo->name = g_strdup
1989 (g_basename(filename ? filename : file));
1992 ainfo->content_type = procmime_get_mime_type(file);
1993 if (!ainfo->content_type) {
1994 ainfo->content_type =
1995 g_strdup("application/octet-stream");
1996 ainfo->encoding = ENC_BASE64;
1997 } else if (!g_strncasecmp(ainfo->content_type, "text", 4))
1998 ainfo->encoding = procmime_get_encoding_for_file(file);
2000 ainfo->encoding = ENC_BASE64;
2001 ainfo->name = g_strdup(g_basename(filename ? filename : file));
2005 text[COL_MIMETYPE] = ainfo->content_type;
2006 text[COL_SIZE] = to_human_readable(size);
2007 text[COL_NAME] = ainfo->name;
2009 row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
2010 gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
2013 #define IS_FIRST_PART_TEXT(info) \
2014 ((info->mime_type == MIME_TEXT || info->mime_type == MIME_TEXT_HTML || \
2015 info->mime_type == MIME_TEXT_ENRICHED) || \
2016 (info->mime_type == MIME_MULTIPART && info->content_type && \
2017 !strcasecmp(info->content_type, "multipart/alternative") && \
2018 (info->children && \
2019 (info->children->mime_type == MIME_TEXT || \
2020 info->children->mime_type == MIME_TEXT_HTML || \
2021 info->children->mime_type == MIME_TEXT_ENRICHED))))
2023 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
2030 mimeinfo = procmime_scan_message(msginfo);
2031 if (!mimeinfo) return;
2033 /* skip first text (presumably message body) */
2034 child = mimeinfo->children;
2035 if (!child || IS_FIRST_PART_TEXT(mimeinfo)) {
2036 procmime_mimeinfo_free_all(mimeinfo);
2039 if (IS_FIRST_PART_TEXT(child))
2040 child = child->next;
2042 infile = procmsg_get_message_file_path(msginfo);
2044 while (child != NULL) {
2045 if (child->children || child->mime_type == MIME_MULTIPART) {
2046 child = procmime_mimeinfo_next(child);
2050 outfile = procmime_get_tmp_file_name(child);
2051 if (procmime_get_part(outfile, infile, child) < 0)
2052 g_warning(_("Can't get the part of multipart message."));
2054 compose_attach_append
2056 child->filename ? child->filename : child->name,
2057 child->content_type);
2059 child = child->next;
2063 procmime_mimeinfo_free_all(mimeinfo);
2066 #undef IS_FIRST_PART_TEXT
2068 #define GET_CHAR(pos, buf, len) \
2070 if (text->use_wchar) \
2071 len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos))); \
2073 buf[0] = GTK_STEXT_INDEX(text, (pos)); \
2078 #define INDENT_CHARS ">|#"
2079 #define SPACE_CHARS " \t"
2081 static void compose_wrap_line(Compose *compose)
2083 GtkSText *text = GTK_STEXT(compose->text);
2084 gint ch_len, last_ch_len;
2085 gchar cbuf[MB_LEN_MAX], last_ch;
2089 gint p_start, p_end;
2090 gint line_pos, cur_pos;
2091 gint line_len, cur_len;
2093 gtk_stext_freeze(text);
2095 text_len = gtk_stext_get_length(text);
2097 /* check to see if the point is on the paragraph mark (empty line). */
2098 cur_pos = gtk_stext_get_point(text);
2099 GET_CHAR(cur_pos, cbuf, ch_len);
2100 if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
2102 goto compose_end; /* on the paragraph mark */
2103 GET_CHAR(cur_pos - 1, cbuf, ch_len);
2104 if (ch_len == 1 && *cbuf == '\n')
2105 goto compose_end; /* on the paragraph mark */
2108 /* find paragraph start. */
2109 line_end = quoted = 0;
2110 for (p_start = cur_pos; p_start >= 0; --p_start) {
2111 GET_CHAR(p_start, cbuf, ch_len);
2112 if (ch_len == 1 && *cbuf == '\n') {
2114 goto compose_end; /* quoted part */
2122 && strchr(prefs_common.quote_chars, *cbuf))
2124 else if (ch_len != 1 || !isspace(*cbuf))
2133 /* find paragraph end. */
2135 for (p_end = cur_pos; p_end < text_len; p_end++) {
2136 GET_CHAR(p_end, cbuf, ch_len);
2137 if (ch_len == 1 && *cbuf == '\n') {
2144 if (line_end && ch_len == 1 &&
2145 strchr(prefs_common.quote_chars, *cbuf))
2146 goto compose_end; /* quoted part */
2151 if (p_end >= text_len)
2154 if (p_start >= p_end)
2157 line_len = cur_len = 0;
2161 for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2164 GET_CHAR(cur_pos, cbuf, ch_len);
2171 if (ch_len == 1 && isspace(*cbuf))
2174 if (ch_len == 1 && *cbuf == '\n') {
2176 if (last_ch_len == 1 && !isspace(last_ch)) {
2177 if (cur_pos + 1 < p_end) {
2178 GET_CHAR(cur_pos + 1, cbuf, ch_len);
2179 if (ch_len == 1 && !isspace(*cbuf))
2183 gtk_stext_set_point(text, cur_pos + 1);
2184 gtk_stext_backward_delete(text, 1);
2186 gtk_stext_set_point(text, cur_pos);
2187 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2197 last_ch_len = ch_len;
2201 line_pos = cur_pos + 1;
2202 line_len = cur_len + ch_len;
2205 if (cur_len + ch_len > prefs_common.linewrap_len &&
2209 GET_CHAR(line_pos - 1, cbuf, ch_len);
2210 if (ch_len == 1 && isspace(*cbuf)) {
2211 gtk_stext_set_point(text, line_pos);
2212 gtk_stext_backward_delete(text, 1);
2221 gtk_stext_set_point(text, line_pos);
2222 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2226 cur_len = cur_len - line_len + ch_len;
2232 line_pos = cur_pos + 1;
2233 line_len = cur_len + ch_len;
2239 gtk_stext_thaw(text);
2244 /* Darko: used when I debug wrapping */
2245 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2248 gchar cbuf[MB_LEN_MAX];
2250 printf("%d [", pos);
2251 for (i = pos; i < tlen; i++) {
2252 GET_CHAR(i, cbuf, clen);
2253 if (clen < 0) break;
2254 if (breakoncr && clen == 1 && cbuf[0] == '\n')
2256 fwrite(cbuf, clen, 1, stdout);
2264 WAIT_FOR_INDENT_CHAR,
2265 WAIT_FOR_INDENT_CHAR_OR_SPACE
2268 /* return indent length, we allow:
2269 > followed by spaces/tabs
2270 | followed by spaces/tabs
2271 uppercase characters immediately followed by >,
2272 and the repeating sequences of the above */
2273 /* return indent length */
2274 static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
2277 guint i, ch_len, alnum_cnt = 0;
2278 IndentState state = WAIT_FOR_INDENT_CHAR;
2279 gchar cbuf[MB_LEN_MAX];
2283 for (i = start_pos; i < text_len; i++) {
2284 GET_CHAR(i, cbuf, ch_len);
2288 if (cbuf[0] == '\n')
2291 is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
2292 is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
2295 case WAIT_FOR_SPACE:
2296 if (is_space == FALSE)
2298 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2300 case WAIT_FOR_INDENT_CHAR_OR_SPACE:
2301 if (is_indent == FALSE && is_space == FALSE &&
2304 if (is_space == TRUE) {
2306 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2307 } else if (is_indent == TRUE) {
2309 state = WAIT_FOR_SPACE;
2312 state = WAIT_FOR_INDENT_CHAR;
2315 case WAIT_FOR_INDENT_CHAR:
2316 if (is_indent == FALSE && !isupper(cbuf[0]))
2318 if (is_indent == TRUE) {
2320 && !strchr(prefs_common.quote_chars, cbuf[0]))
2323 state = WAIT_FOR_SPACE;
2334 if ((i_len > 0) && (state == WAIT_FOR_INDENT_CHAR))
2340 /* insert quotation string when line was wrapped */
2341 static guint ins_quote(GtkSText *text, guint indent_len,
2342 guint prev_line_pos, guint text_len,
2345 guint i, ins_len = 0;
2349 for (i = 0; i < indent_len; i++) {
2350 ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2351 gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2353 ins_len = indent_len;
2359 /* check if we should join the next line */
2360 static gboolean join_next_line(GtkSText *text, guint start_pos, guint tlen,
2361 guint prev_ilen, gboolean autowrap)
2363 guint indent_len, ch_len;
2364 gboolean do_join = FALSE;
2365 gchar cbuf[MB_LEN_MAX];
2367 indent_len = get_indent_length(text, start_pos, tlen);
2369 if ((autowrap || indent_len > 0) && indent_len == prev_ilen) {
2370 GET_CHAR(start_pos + indent_len, cbuf, ch_len);
2371 if (ch_len > 0 && (cbuf[0] != '\n'))
2378 static void compose_wrap_line_all(Compose *compose)
2380 compose_wrap_line_all_full(compose, FALSE);
2383 #define STEXT_FREEZE() \
2384 if (!frozen) { gtk_stext_freeze(text); frozen = TRUE; }
2386 static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
2388 GtkSText *text = GTK_STEXT(compose->text);
2390 guint line_pos = 0, cur_pos = 0, p_pos = 0;
2391 gint line_len = 0, cur_len = 0;
2393 gboolean is_new_line = TRUE, do_delete = FALSE;
2395 gboolean linewrap_quote = TRUE;
2396 gboolean set_editable_pos = FALSE;
2397 gint editable_pos = 0;
2398 gboolean frozen = FALSE;
2399 guint linewrap_len = prefs_common.linewrap_len;
2400 gchar *qfmt = prefs_common.quotemark;
2401 gchar cbuf[MB_LEN_MAX];
2403 tlen = gtk_stext_get_length(text);
2405 for (; cur_pos < tlen; cur_pos++) {
2406 /* mark position of new line - needed for quotation wrap */
2409 i_len = get_indent_length(text, cur_pos, tlen);
2411 is_new_line = FALSE;
2414 g_print("new line i_len=%d p_pos=", i_len);
2415 dump_text(text, p_pos, tlen, 1);
2419 GET_CHAR(cur_pos, cbuf, ch_len);
2421 /* fix line length for tabs */
2422 if (ch_len == 1 && *cbuf == '\t') {
2423 guint tab_width = text->default_tab_width;
2424 guint tab_offset = line_len % tab_width;
2427 g_print("found tab at pos=%d line_len=%d ", cur_pos,
2431 line_len += tab_width - tab_offset - 1;
2435 printf("new_len=%d\n", line_len);
2439 /* we have encountered line break */
2440 if (ch_len == 1 && *cbuf == '\n') {
2442 gchar cb[MB_LEN_MAX];
2444 /* should we join the next line */
2445 if ((autowrap || i_len != cur_len) && do_delete &&
2447 (text, cur_pos + 1, tlen, i_len, autowrap))
2453 g_print("found CR at %d do_del is %d next line is ",
2454 cur_pos, do_delete);
2455 dump_text(text, cur_pos + 1, tlen, 1);
2458 /* skip delete if it is continuous URL */
2459 if (do_delete && (line_pos - p_pos <= i_len) &&
2460 gtkut_stext_is_uri_string(text, line_pos, tlen))
2464 g_print("l_len=%d wrap_len=%d do_del=%d\n",
2465 line_len, linewrap_len, do_delete);
2467 /* should we delete to perform smart wrapping */
2468 if (line_len < linewrap_len && do_delete) {
2470 /* get rid of newline */
2471 gtk_stext_set_point(text, cur_pos);
2472 gtk_stext_forward_delete(text, 1);
2475 /* if text starts with quote fmt or with
2476 indent string, delete them */
2479 ilen = gtkut_stext_str_compare_n
2480 (text, cur_pos, p_pos, i_len,
2483 gtk_stext_forward_delete
2489 GET_CHAR(cur_pos, cb, clen);
2491 /* insert space if it's alphanumeric */
2492 if ((cur_pos != line_pos) &&
2493 ((clen > 1) || isalnum(cb[0]))) {
2494 gtk_stext_insert(text, NULL, NULL,
2499 /* and start over with current line */
2500 cur_pos = p_pos - 1;
2502 line_len = cur_len = 0;
2506 g_print("after delete l_pos=");
2507 dump_text(text, line_pos, tlen, 1);
2512 /* mark new line beginning */
2513 line_pos = cur_pos + 1;
2514 line_len = cur_len = 0;
2525 /* possible line break */
2526 if (ch_len == 1 && isspace(*cbuf)) {
2527 line_pos = cur_pos + 1;
2528 line_len = cur_len + ch_len;
2531 /* are we over wrapping length set in preferences ? */
2532 if (cur_len + ch_len > linewrap_len) {
2536 g_print("should wrap cur_pos=%d ", cur_pos);
2537 dump_text(text, p_pos, tlen, 1);
2538 dump_text(text, line_pos, tlen, 1);
2540 /* force wrapping if it is one long word but not URL */
2541 if (line_pos - p_pos <= i_len)
2542 if (!gtkut_stext_is_uri_string
2543 (text, line_pos, tlen))
2544 line_pos = cur_pos - 1;
2546 g_print("new line_pos=%d\n", line_pos);
2549 GET_CHAR(line_pos - 1, cbuf, clen);
2551 /* if next character is space delete it */
2552 if (clen == 1 && isspace(*cbuf)) {
2553 if (p_pos + i_len != line_pos ||
2554 !gtkut_stext_is_uri_string
2555 (text, line_pos, tlen)) {
2557 /* workaround for correct cursor
2559 if (set_editable_pos == FALSE) {
2560 editable_pos = gtk_editable_get_position(GTK_EDITABLE(text));
2561 if (editable_pos == line_pos)
2562 set_editable_pos = TRUE;
2564 gtk_stext_set_point(text, line_pos);
2565 gtk_stext_backward_delete(text, 1);
2574 /* if it is URL at beginning of line don't wrap */
2575 if (p_pos + i_len == line_pos &&
2576 gtkut_stext_is_uri_string(text, line_pos, tlen)) {
2578 g_print("found URL at ");
2579 dump_text(text, line_pos, tlen, 1);
2586 gtk_stext_set_point(text, line_pos);
2587 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2588 /* gtk_stext_compact_buffer(text); */
2591 /* for loop will increase it */
2592 cur_pos = line_pos - 1;
2593 /* start over with current line */
2595 line_len = cur_len = 0;
2596 if (autowrap || i_len > 0)
2601 g_print("after CR insert ");
2602 dump_text(text, line_pos, tlen, 1);
2603 dump_text(text, cur_pos, tlen, 1);
2606 /* should we insert quotation ? */
2607 if (linewrap_quote && i_len) {
2608 /* only if line is not already quoted */
2609 if (!gtkut_stext_str_compare
2610 (text, line_pos, tlen, qfmt)) {
2613 if (line_pos - p_pos > i_len) {
2615 (text, i_len, p_pos,
2620 g_print("after quote insert ");
2621 dump_text(text, line_pos, tlen, 1);
2629 line_pos = cur_pos + 1;
2630 line_len = cur_len + ch_len;
2632 /* advance to next character in buffer */
2637 gtk_stext_thaw(text);
2639 if (set_editable_pos && editable_pos <= tlen)
2640 gtk_editable_set_position(GTK_EDITABLE(text), editable_pos);
2646 static void compose_set_title(Compose *compose)
2651 edited = compose->modified ? _(" [Edited]") : "";
2652 if (compose->account && compose->account->address)
2653 str = g_strdup_printf(_("%s - Compose message%s"),
2654 compose->account->address, edited);
2656 str = g_strdup_printf(_("Compose message%s"), edited);
2657 gtk_window_set_title(GTK_WINDOW(compose->window), str);
2662 * compose_current_mail_account:
2664 * Find a current mail account (the currently selected account, or the
2665 * default account, if a news account is currently selected). If a
2666 * mail account cannot be found, display an error message.
2668 * Return value: Mail account, or NULL if not found.
2670 static PrefsAccount *
2671 compose_current_mail_account(void)
2675 if (cur_account && cur_account->protocol != A_NNTP)
2678 ac = account_get_default();
2679 if (!ac || ac->protocol == A_NNTP) {
2680 alertpanel_error(_("Account for sending mail is not specified.\n"
2681 "Please select a mail account before sending."));
2688 static void compose_select_account(Compose *compose, PrefsAccount *account)
2690 GtkWidget *menuitem;
2691 GtkItemFactory *ifactory;
2693 g_return_if_fail(account != NULL);
2695 compose->account = account;
2697 compose_set_title(compose);
2699 ifactory = gtk_item_factory_from_widget(compose->menubar);
2701 if (account->protocol == A_NNTP) {
2702 gtk_widget_show(compose->newsgroups_hbox);
2703 gtk_widget_show(compose->newsgroups_entry);
2704 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 4);
2706 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
2707 gtk_check_menu_item_set_active
2708 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2709 gtk_widget_set_sensitive(menuitem, TRUE);
2710 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2711 gtk_check_menu_item_set_active
2712 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2713 gtk_widget_set_sensitive(menuitem, TRUE);
2715 menu_set_sensitive(ifactory, "/View/Followup to", TRUE);
2717 gtk_widget_hide(compose->newsgroups_hbox);
2718 gtk_widget_hide(compose->newsgroups_entry);
2719 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 0);
2720 gtk_widget_queue_resize(compose->table_vbox);
2722 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
2723 gtk_check_menu_item_set_active
2724 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2725 gtk_widget_set_sensitive(menuitem, FALSE);
2726 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2727 gtk_check_menu_item_set_active
2728 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2729 gtk_widget_set_sensitive(menuitem, FALSE);
2731 menuitem = gtk_item_factory_get_item(ifactory, "/View/Followup to");
2732 gtk_check_menu_item_set_active
2733 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2734 gtk_widget_set_sensitive(menuitem, FALSE);
2737 if (account->set_autocc && account->auto_cc &&
2738 compose->mode != COMPOSE_REEDIT) {
2740 (GTK_ENTRY(compose->cc_entry), account->auto_cc);
2741 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2742 gtk_check_menu_item_set_active
2743 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2745 if (account->set_autobcc) {
2746 menuitem = gtk_item_factory_get_item(ifactory, "/View/Bcc");
2747 gtk_check_menu_item_set_active
2748 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2749 if (account->auto_bcc && compose->mode != COMPOSE_REEDIT)
2750 gtk_entry_set_text(GTK_ENTRY(compose->bcc_entry),
2753 if (account->set_autoreplyto) {
2754 menuitem = gtk_item_factory_get_item(ifactory,
2756 gtk_check_menu_item_set_active
2757 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2758 if (account->auto_replyto && compose->mode != COMPOSE_REEDIT)
2759 gtk_entry_set_text(GTK_ENTRY(compose->reply_entry),
2760 account->auto_replyto);
2763 menuitem = gtk_item_factory_get_item(ifactory, "/View/Ruler");
2764 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2765 prefs_common.show_ruler);
2769 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
2770 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2771 account->default_sign);
2772 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
2773 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2774 account->default_encrypt);
2775 #endif /* USE_GPGME */
2778 gboolean compose_check_for_valid_recipient(Compose *compose) {
2779 gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
2780 gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
2781 gboolean recipient_found = FALSE;
2785 /* free to and newsgroup list */
2786 slist_free_strings(compose->to_list);
2787 g_slist_free(compose->to_list);
2788 compose->to_list = NULL;
2790 slist_free_strings(compose->newsgroup_list);
2791 g_slist_free(compose->newsgroup_list);
2792 compose->newsgroup_list = NULL;
2794 /* search header entries for to and newsgroup entries */
2795 for (list = compose->header_list; list; list = list->next) {
2798 header = gtk_editable_get_chars(GTK_EDITABLE(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry), 0, -1);
2799 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
2801 if (entry[0] != '\0') {
2802 for (strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
2803 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2804 compose->to_list = address_list_append(compose->to_list, entry);
2805 recipient_found = TRUE;
2808 for (strptr = recipient_headers_news; *strptr != NULL; strptr++) {
2809 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2810 compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
2811 recipient_found = TRUE;
2818 return recipient_found;
2821 static gboolean compose_check_entries(Compose *compose, gboolean check_subject)
2825 if (compose_check_for_valid_recipient(compose) == FALSE) {
2826 alertpanel_error(_("Recipient is not specified."));
2830 str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
2831 if (*str == '\0' && check_subject == TRUE) {
2834 aval = alertpanel(_("Send"),
2835 _("Subject is empty. Send it anyway?"),
2836 _("Yes"), _("No"), NULL);
2837 if (aval != G_ALERTDEFAULT)
2844 gint compose_send(Compose *compose)
2851 if (compose_check_entries(compose, TRUE) == FALSE)
2854 val = compose_queue(compose, &msgnum, &folder);
2856 alertpanel_error(_("Could not queue message for sending"));
2860 msgpath = folder_item_fetch_msg(folder, msgnum);
2861 val = procmsg_send_message_queue(msgpath);
2864 folder_item_remove_msg(folder, msgnum);
2865 folderview_update_item(folder, TRUE);
2870 #if 0 /* compose restructure */
2871 gint compose_send(Compose *compose)
2873 gchar tmp[MAXPATHLEN + 1];
2875 static gboolean lock = FALSE;
2879 g_return_val_if_fail(compose->account != NULL, -1);
2880 g_return_val_if_fail(compose->orig_account != NULL, -1);
2884 if (compose_check_entries(compose, TRUE) == FALSE) {
2889 /* write to temporary file */
2890 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
2891 get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
2893 if (prefs_common.linewrap_at_send)
2894 compose_wrap_line_all(compose);
2896 if (compose_write_to_file(compose, tmp, FALSE) < 0) {
2901 if (!compose->to_list && !compose->newsgroup_list) {
2902 g_warning(_("can't get recipient list."));
2908 if (compose->to_list) {
2911 #if 0 /* NEW COMPOSE GUI */
2912 if (compose->account->protocol != A_NNTP)
2913 ac = compose->account;
2914 else if (compose->orig_account->protocol != A_NNTP)
2915 ac = compose->orig_account;
2916 else if (cur_account && cur_account->protocol != A_NNTP)
2919 ac = compose_current_mail_account();
2927 ac = compose->account;
2929 ok = send_message(tmp, ac, compose->to_list);
2930 statusbar_pop_all();
2933 if (ok == 0 && compose->newsgroup_list) {
2936 if (compose->account->protocol == A_NNTP)
2937 folder = FOLDER(compose->account->folder);
2939 folder = FOLDER(compose->orig_account->folder);
2941 ok = news_post(folder, tmp);
2943 alertpanel_error(_("Error occurred while posting the message to %s ."),
2944 compose->account->nntp_server);
2951 /* queue message if failed to send */
2953 if (prefs_common.queue_msg) {
2958 _("Error occurred while sending the message.\n"
2959 "Put this message into queue folder?"),
2960 _("OK"), _("Cancel"), NULL);
2961 if (G_ALERTDEFAULT == val) {
2962 ok = compose_queue(compose, tmp);
2964 alertpanel_error(_("Can't queue the message."));
2967 alertpanel_error(_("Error occurred while sending the message."));
2969 if (compose->mode == COMPOSE_REEDIT) {
2970 compose_remove_reedit_target(compose);
2971 if (compose->targetinfo)
2972 folderview_update_item
2973 (compose->targetinfo->folder, TRUE);
2975 /* save message to outbox */
2976 if (prefs_common.savemsg) {
2979 outbox = account_get_special_folder
2980 (compose->account, F_OUTBOX);
2981 if (procmsg_save_to_outbox(outbox, tmp, FALSE) < 0)
2983 (_("Can't save the message to Sent."));
2985 folderview_update_item(outbox, TRUE);
2995 static gboolean compose_use_attach(Compose *compose) {
2996 return(gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL);
2999 static gint compose_redirect_write_headers_from_headerlist(Compose *compose,
3002 gchar buf[BUFFSIZE];
3004 gboolean first_address;
3006 ComposeHeaderEntry *headerentry;
3007 gchar *headerentryname;
3011 debug_print("Writing redirect header\n");
3013 cc_hdr = prefs_common.trans_hdr ? _("Cc:") : "Cc:";
3014 to_hdr = prefs_common.trans_hdr ? _("To:") : "To:";
3016 first_address = TRUE;
3017 for (list = compose->header_list; list; list = list->next) {
3018 headerentry = ((ComposeHeaderEntry *)list->data);
3019 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
3021 if (g_strcasecmp(headerentryname, cc_hdr) == 0
3022 || g_strcasecmp(headerentryname, to_hdr) == 0) {
3023 str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
3024 Xstrdup_a(str, str, return -1);
3026 if (str[0] != '\0') {
3027 compose_convert_header
3028 (buf, sizeof(buf), str,
3029 strlen("Resent-To") + 2);
3030 if (first_address) {
3031 fprintf(fp, "Resent-To: ");
3032 first_address = FALSE;
3036 fprintf(fp, "%s", buf);
3040 /* if (!first_address) { */
3047 static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
3049 gchar buf[BUFFSIZE];
3051 /* struct utsname utsbuf; */
3053 g_return_val_if_fail(fp != NULL, -1);
3054 g_return_val_if_fail(compose->account != NULL, -1);
3055 g_return_val_if_fail(compose->account->address != NULL, -1);
3058 get_rfc822_date(buf, sizeof(buf));
3059 fprintf(fp, "Resent-Date: %s\n", buf);
3062 if (compose->account->name && *compose->account->name) {
3063 compose_convert_header
3064 (buf, sizeof(buf), compose->account->name,
3066 fprintf(fp, "Resent-From: %s <%s>\n",
3067 buf, compose->account->address);
3069 fprintf(fp, "Resent-From: %s\n", compose->account->address);
3072 str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
3074 Xstrdup_a(str, str, return -1);
3077 compose_convert_header(buf, sizeof(buf), str,
3078 strlen("Subject: "));
3079 fprintf(fp, "Subject: %s\n", buf);
3083 /* Resent-Message-ID */
3084 if (compose->account->gen_msgid) {
3085 compose_generate_msgid(compose, buf, sizeof(buf));
3086 fprintf(fp, "Resent-Message-Id: <%s>\n", buf);
3087 compose->msgid = g_strdup(buf);
3090 compose_redirect_write_headers_from_headerlist(compose, fp);
3092 /* separator between header and body */
3098 static gint compose_redirect_write_to_file(Compose *compose, const gchar *file)
3103 gchar buf[BUFFSIZE];
3105 if ((fp = fopen(compose->redirect_filename, "rb")) == NULL) {
3106 FILE_OP_ERROR(file, "fopen");
3110 if ((fdest = fopen(file, "wb")) == NULL) {
3111 FILE_OP_ERROR(file, "fopen");
3116 /* chmod for security */
3117 if (change_file_mode_rw(fdest, file) < 0) {
3118 FILE_OP_ERROR(file, "chmod");
3119 g_warning(_("can't change file mode\n"));
3122 while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
3123 /* should filter returnpath, delivered-to */
3124 if (g_strncasecmp(buf, "Return-Path:",
3125 strlen("Return-Path:")) == 0 ||
3126 g_strncasecmp(buf, "Delivered-To:",
3127 strlen("Delivered-To:")) == 0 ||
3128 g_strncasecmp(buf, "Received:",
3129 strlen("Received:")) == 0 ||
3130 g_strncasecmp(buf, "Subject:",
3131 strlen("Subject:")) == 0 ||
3132 g_strncasecmp(buf, "X-UIDL:",
3133 strlen("X-UIDL:")) == 0)
3136 if (fputs(buf, fdest) == -1)
3139 if (!prefs_common.redirect_keep_from) {
3140 if (g_strncasecmp(buf, "From:",
3141 strlen("From:")) == 0) {
3142 fputs(" (by way of ", fdest);
3143 if (compose->account->name
3144 && *compose->account->name) {
3145 compose_convert_header
3147 compose->account->name,
3149 fprintf(fdest, "%s <%s>",
3151 compose->account->address);
3153 fprintf(fdest, "%s",
3154 compose->account->address);
3159 if (fputs("\n", fdest) == -1)
3163 compose_redirect_write_headers(compose, fdest);
3165 while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
3166 if (fwrite(buf, sizeof(gchar), len, fdest) != len) {
3167 FILE_OP_ERROR(file, "fwrite");
3173 if (fclose(fdest) == EOF) {
3174 FILE_OP_ERROR(file, "fclose");
3190 /* interfaces to rfc2015 to keep out the prefs stuff there.
3191 * returns 0 on success and -1 on error. */
3192 static gint compose_create_signers_list(Compose *compose, GSList **pkey_list)
3194 const gchar *key_id = NULL;
3197 switch (compose->account->sign_key) {
3198 case SIGN_KEY_DEFAULT:
3201 case SIGN_KEY_BY_FROM:
3202 key_id = compose->account->address;
3204 case SIGN_KEY_CUSTOM:
3205 key_id = compose->account->sign_key_id;
3211 key_list = rfc2015_create_signers_list(key_id);
3214 alertpanel_error(_("Could not find any key associated with "
3215 "currently selected key id `%s'."), key_id);
3219 *pkey_list = key_list;
3223 /* clearsign message body text */
3224 static gint compose_clearsign_text(Compose *compose, gchar **text)
3229 tmp_file = get_tmp_file();
3230 if (str_write_to_file(*text, tmp_file) < 0) {
3235 if (canonicalize_file_replace(tmp_file) < 0 ||
3236 compose_create_signers_list(compose, &key_list) < 0 ||
3237 rfc2015_clearsign(tmp_file, key_list) < 0) {
3244 *text = file_read_to_str(tmp_file);
3252 #endif /* USE_GPGME */
3254 static gint compose_write_to_file(Compose *compose, const gchar *file,
3261 const gchar *out_codeset;
3262 EncodingType encoding;
3264 if ((fp = fopen(file, "wb")) == NULL) {
3265 FILE_OP_ERROR(file, "fopen");
3269 /* chmod for security */
3270 if (change_file_mode_rw(fp, file) < 0) {
3271 FILE_OP_ERROR(file, "chmod");
3272 g_warning(_("can't change file mode\n"));
3275 /* get all composed text */
3276 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
3277 len = strlen(chars);
3278 if (is_ascii_str(chars)) {
3281 out_codeset = CS_US_ASCII;
3282 encoding = ENC_7BIT;
3284 const gchar *src_codeset;
3286 out_codeset = conv_get_outgoing_charset_str();
3287 if (!strcasecmp(out_codeset, CS_US_ASCII))
3288 out_codeset = CS_ISO_8859_1;
3289 encoding = procmime_get_encoding_for_charset(out_codeset);
3293 compose->use_signing && !compose->account->clearsign &&
3294 encoding == ENC_8BIT)
3295 encoding = ENC_BASE64;
3298 src_codeset = conv_get_current_charset_str();
3299 /* if current encoding is US-ASCII, set it the same as
3300 outgoing one to prevent code conversion failure */
3301 if (!strcasecmp(src_codeset, CS_US_ASCII))
3302 src_codeset = out_codeset;
3304 debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
3305 src_codeset, out_codeset, procmime_get_encoding_str(encoding));
3307 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
3313 _("Can't convert the character encoding of the message.\n"
3314 "Send it anyway?"), _("Yes"), _("+No"), NULL);
3315 if (aval != G_ALERTDEFAULT) {
3329 if (!is_draft && compose->use_signing && compose->account->clearsign) {
3330 if (compose_clearsign_text(compose, &buf) < 0) {
3331 g_warning("clearsign failed\n");
3341 if (compose_write_headers
3342 (compose, fp, out_codeset, encoding, is_draft) < 0) {
3343 g_warning(_("can't write headers\n"));
3350 if (compose_use_attach(compose)) {
3352 /* This prolog message is ignored by mime software and
3353 * because it would make our signing/encryption task
3354 * tougher, we don't emit it in that case */
3355 if (!compose->use_signing && !compose->use_encryption)
3357 fputs("This is a multi-part message in MIME format.\n", fp);
3359 fprintf(fp, "\n--%s\n", compose->boundary);
3360 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
3362 fprintf(fp, "Content-Transfer-Encoding: %s\n",
3363 procmime_get_encoding_str(encoding));
3369 if (encoding == ENC_BASE64) {
3370 gchar outbuf[B64_BUFFSIZE];
3373 for (i = 0; i < len; i += B64_LINE_SIZE) {
3374 l = MIN(B64_LINE_SIZE, len - i);
3375 base64_encode(outbuf, buf + i, l);
3379 } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
3380 FILE_OP_ERROR(file, "fwrite");
3388 if (compose_use_attach(compose))
3389 compose_write_attach(compose, fp);
3391 if (fclose(fp) == EOF) {
3392 FILE_OP_ERROR(file, "fclose");
3401 if ((compose->use_signing && !compose->account->clearsign) ||
3402 compose->use_encryption) {
3403 if (canonicalize_file_replace(file) < 0) {
3409 if (compose->use_signing && !compose->account->clearsign) {
3412 if (compose_create_signers_list(compose, &key_list) < 0 ||
3413 rfc2015_sign(file, key_list) < 0) {
3418 if (compose->use_encryption) {
3419 if (rfc2015_encrypt(file, compose->to_list,
3420 compose->account->ascii_armored) < 0) {
3425 #endif /* USE_GPGME */
3430 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
3436 if ((fp = fopen(file, "wb")) == NULL) {
3437 FILE_OP_ERROR(file, "fopen");
3441 /* chmod for security */
3442 if (change_file_mode_rw(fp, file) < 0) {
3443 FILE_OP_ERROR(file, "chmod");
3444 g_warning(_("can't change file mode\n"));
3447 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
3450 len = strlen(chars);
3451 if (fwrite(chars, sizeof(gchar), len, fp) != len) {
3452 FILE_OP_ERROR(file, "fwrite");
3461 if (fclose(fp) == EOF) {
3462 FILE_OP_ERROR(file, "fclose");
3469 static gint compose_remove_reedit_target(Compose *compose)
3472 MsgInfo *msginfo = compose->targetinfo;
3474 g_return_val_if_fail(compose->mode == COMPOSE_REEDIT, -1);
3475 if (!msginfo) return -1;
3477 item = msginfo->folder;
3478 g_return_val_if_fail(item != NULL, -1);
3480 if (procmsg_msg_exist(msginfo) &&
3481 (item->stype == F_DRAFT || item->stype == F_QUEUE)) {
3482 if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
3483 g_warning(_("can't remove the old message\n"));
3491 void compose_remove_draft(Compose *compose)
3494 MsgInfo *msginfo = compose->targetinfo;
3495 drafts = account_get_special_folder(compose->account, F_DRAFT);
3497 if (procmsg_msg_exist(msginfo)) {
3498 folder_item_remove_msg(drafts, msginfo->msgnum);
3499 folderview_update_item(drafts, TRUE);
3504 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
3506 return compose_queue_sub (compose, msgnum, item, FALSE);
3508 static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item, gboolean check_subject)
3514 gchar buf[BUFFSIZE];
3516 static gboolean lock = FALSE;
3517 PrefsAccount *mailac = NULL, *newsac = NULL;
3519 debug_print("queueing message...\n");
3520 g_return_val_if_fail(compose->account != NULL, -1);
3521 g_return_val_if_fail(compose->orig_account != NULL, -1);
3525 if (compose_check_entries(compose, check_subject) == FALSE) {
3530 if (!compose->to_list && !compose->newsgroup_list) {
3531 g_warning(_("can't get recipient list."));
3536 if (compose->to_list) {
3537 if (compose->account->protocol != A_NNTP)
3538 mailac = compose->account;
3539 else if (compose->orig_account->protocol != A_NNTP)
3540 mailac = compose->orig_account;
3541 else if (cur_account && cur_account->protocol != A_NNTP)
3542 mailac = cur_account;
3543 else if (!(mailac = compose_current_mail_account())) {
3545 alertpanel_error(_("No account for sending mails available!"));
3550 if (compose->newsgroup_list) {
3551 if (compose->account->protocol == A_NNTP)
3552 newsac = compose->account;
3553 else if (!(newsac = compose->orig_account) || (newsac->protocol != A_NNTP)) {
3555 alertpanel_error(_("No account for posting news available!"));
3560 if (prefs_common.linewrap_at_send)
3561 compose_wrap_line_all(compose);
3563 /* write to temporary file */
3564 tmp2 = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
3565 G_DIR_SEPARATOR, (gint)compose);
3567 if (compose->redirect_filename != NULL) {
3568 if (compose_redirect_write_to_file(compose, tmp2) < 0) {
3575 if (compose_write_to_file(compose, tmp2, FALSE) < 0) {
3582 /* add queue header */
3583 tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
3584 G_DIR_SEPARATOR, (gint)compose);
3585 if ((fp = fopen(tmp, "wb")) == NULL) {
3586 FILE_OP_ERROR(tmp, "fopen");
3590 if ((src_fp = fopen(tmp2, "rb")) == NULL) {
3591 FILE_OP_ERROR(tmp2, "fopen");
3599 if (change_file_mode_rw(fp, tmp) < 0) {
3600 FILE_OP_ERROR(tmp, "chmod");
3601 g_warning(_("can't change file mode\n"));
3604 /* queueing variables */
3605 fprintf(fp, "AF:\n");
3606 fprintf(fp, "NF:0\n");
3607 fprintf(fp, "PS:10\n");
3608 fprintf(fp, "SRH:1\n");
3609 fprintf(fp, "SFN:\n");
3610 fprintf(fp, "DSR:\n");
3612 fprintf(fp, "MID:<%s>\n", compose->msgid);
3614 fprintf(fp, "MID:\n");
3615 fprintf(fp, "CFG:\n");
3616 fprintf(fp, "PT:0\n");
3617 fprintf(fp, "S:%s\n", compose->account->address);
3618 fprintf(fp, "RQ:\n");
3620 fprintf(fp, "SSV:%s\n", mailac->smtp_server);
3622 fprintf(fp, "SSV:\n");
3624 fprintf(fp, "NSV:%s\n", newsac->nntp_server);
3626 fprintf(fp, "NSV:\n");
3627 fprintf(fp, "SSH:\n");
3628 /* write recepient list */
3630 if (compose->to_list) {
3631 fprintf(fp, "<%s>", (gchar *)compose->to_list->data);
3632 for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
3633 fprintf(fp, ",<%s>", (gchar *)cur->data);
3636 /* write newsgroup list */
3638 if (compose->newsgroup_list) {
3639 fprintf(fp, "%s", (gchar *)compose->newsgroup_list->data);
3640 for (cur = compose->newsgroup_list->next; cur != NULL; cur = cur->next)
3641 fprintf(fp, ",%s", (gchar *)cur->data);
3644 /* Sylpheed account IDs */
3646 fprintf(fp, "MAID:%d\n", mailac->account_id);
3649 fprintf(fp, "NAID:%d\n", newsac->account_id);
3651 /* Save copy folder */
3652 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn))) {
3653 gchar *savefolderid;
3655 savefolderid = gtk_editable_get_chars(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
3656 fprintf(fp, "SCF:%s\n", savefolderid);
3657 g_free(savefolderid);
3659 /* Message-ID of message replying to */
3660 if ((compose->replyinfo != NULL) && (compose->replyinfo->msgid != NULL)) {
3663 folderid = folder_item_get_identifier(compose->replyinfo->folder);
3664 fprintf(fp, "RMID:%s\x7f%d\x7f%s\n", folderid, compose->replyinfo->msgnum, compose->replyinfo->msgid);
3669 while (fgets(buf, sizeof(buf), src_fp) != NULL) {
3670 if (fputs(buf, fp) == EOF) {
3671 FILE_OP_ERROR(tmp, "fputs");
3682 if (fclose(fp) == EOF) {
3683 FILE_OP_ERROR(tmp, "fclose");
3691 queue = account_get_special_folder(compose->account, F_QUEUE);
3693 g_warning(_("can't find queue folder\n"));
3698 folder_item_scan(queue);
3699 if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
3700 g_warning(_("can't queue the message\n"));
3710 if (compose->mode == COMPOSE_REEDIT) {
3711 compose_remove_reedit_target(compose);
3712 if (compose->targetinfo &&
3713 compose->targetinfo->folder != queue)
3714 folderview_update_item
3715 (compose->targetinfo->folder, TRUE);
3718 folderview_update_item(queue, TRUE);
3720 if ((msgnum != NULL) && (item != NULL)) {
3728 static void compose_write_attach(Compose *compose, FILE *fp)
3731 GtkCList *clist = GTK_CLIST(compose->attach_clist);
3734 gchar filename[BUFFSIZE];
3737 for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
3739 gchar buf[BUFFSIZE];
3740 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
3742 if ((attach_fp = fopen(ainfo->file, "rb")) == NULL) {
3743 g_warning(_("Can't open file %s\n"), ainfo->file);
3747 fprintf(fp, "\n--%s\n", compose->boundary);
3749 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
3750 fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
3751 fprintf(fp, "Content-Disposition: inline\n");
3753 conv_encode_header(filename, sizeof(filename),
3755 fprintf(fp, "Content-Type: %s;\n"