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"
89 #include "procheader.h"
91 #include "statusbar.h"
98 #include "alertpanel.h"
99 #include "manage_window.h"
100 #include "gtkshruler.h"
102 #include "addr_compl.h"
103 #include "quote_fmt.h"
104 #include "template.h"
106 #include "foldersel.h"
107 #include "prefs_actions.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;
141 #define B64_LINE_SIZE 57
142 #define B64_BUFFSIZE 77
144 #define MAX_REFERENCES_LEN 999
146 static GdkColor quote_color = {0, 0, 0, 0xbfff};
148 static GList *compose_list = NULL;
150 Compose *compose_generic_new (PrefsAccount *account,
154 static Compose *compose_create (PrefsAccount *account,
156 static void compose_toolbar_create (Compose *compose,
157 GtkWidget *container);
158 static GtkWidget *compose_account_option_menu_create
160 static void compose_set_template_menu (Compose *compose);
161 static void compose_template_apply (Compose *compose,
164 static void compose_destroy (Compose *compose);
166 static void compose_entries_set (Compose *compose,
167 const gchar *mailto);
168 static gint compose_parse_header (Compose *compose,
170 static gchar *compose_parse_references (const gchar *ref,
173 static gchar *compose_quote_fmt (Compose *compose,
179 static void compose_reply_set_entry (Compose *compose,
184 followup_and_reply_to);
185 static void compose_reedit_set_entry (Compose *compose,
187 static void compose_insert_sig (Compose *compose);
188 static void compose_insert_file (Compose *compose,
190 static void compose_attach_append (Compose *compose,
193 const gchar *content_type);
194 static void compose_attach_parts (Compose *compose,
196 static void compose_wrap_line (Compose *compose);
197 static void compose_wrap_line_all (Compose *compose);
198 static void compose_set_title (Compose *compose);
200 static PrefsAccount *compose_current_mail_account(void);
201 /* static gint compose_send (Compose *compose); */
202 static gboolean compose_check_for_valid_recipient
204 static gboolean compose_check_entries (Compose *compose,
205 gboolean check_subject);
206 static gint compose_write_to_file (Compose *compose,
209 static gint compose_write_body_to_file (Compose *compose,
211 static gint compose_remove_reedit_target (Compose *compose);
212 static gint compose_queue (Compose *compose,
215 static gint compose_queue_sub (Compose *compose,
218 gboolean check_subject);
219 static void compose_write_attach (Compose *compose,
221 static gint compose_write_headers (Compose *compose,
223 const gchar *charset,
224 EncodingType encoding,
227 static void compose_convert_header (gchar *dest,
231 static void compose_generate_msgid (Compose *compose,
235 static void compose_attach_info_free (AttachInfo *ainfo);
236 static void compose_attach_remove_selected (Compose *compose);
238 static void compose_attach_property (Compose *compose);
239 static void compose_attach_property_create (gboolean *cancelled);
240 static void attach_property_ok (GtkWidget *widget,
241 gboolean *cancelled);
242 static void attach_property_cancel (GtkWidget *widget,
243 gboolean *cancelled);
244 static gint attach_property_delete_event (GtkWidget *widget,
246 gboolean *cancelled);
247 static void attach_property_key_pressed (GtkWidget *widget,
249 gboolean *cancelled);
251 static void compose_exec_ext_editor (Compose *compose);
252 static gint compose_exec_ext_editor_real (const gchar *file);
253 static gboolean compose_ext_editor_kill (Compose *compose);
254 static void compose_input_cb (gpointer data,
256 GdkInputCondition condition);
257 static void compose_set_ext_editor_sensitive (Compose *compose,
260 static void compose_undo_state_changed (UndoMain *undostruct,
265 static gint calc_cursor_xpos (GtkSText *text,
269 static void compose_create_header_entry (Compose *compose);
270 static void compose_add_header_entry (Compose *compose, gchar *header, gchar *text);
272 /* callback functions */
274 static gboolean compose_edit_size_alloc (GtkEditable *widget,
275 GtkAllocation *allocation,
276 GtkSHRuler *shruler);
278 static void toolbar_send_cb (GtkWidget *widget,
280 static void toolbar_send_later_cb (GtkWidget *widget,
282 static void toolbar_draft_cb (GtkWidget *widget,
284 static void toolbar_insert_cb (GtkWidget *widget,
286 static void toolbar_attach_cb (GtkWidget *widget,
288 static void toolbar_sig_cb (GtkWidget *widget,
290 static void toolbar_ext_editor_cb (GtkWidget *widget,
292 static void toolbar_linewrap_cb (GtkWidget *widget,
294 static void toolbar_address_cb (GtkWidget *widget,
297 static void select_account (Compose *compose,
300 static void account_activated (GtkMenuItem *menuitem,
303 static void attach_selected (GtkCList *clist,
308 static void attach_button_pressed (GtkWidget *widget,
309 GdkEventButton *event,
311 static void attach_key_pressed (GtkWidget *widget,
315 static void compose_send_cb (gpointer data,
318 static void compose_send_later_cb (gpointer data,
322 static void compose_draft_cb (gpointer data,
326 static void compose_attach_cb (gpointer data,
329 static void compose_insert_file_cb (gpointer data,
333 static void compose_close_cb (gpointer data,
337 static void compose_address_cb (gpointer data,
340 static void compose_template_activate_cb(GtkWidget *widget,
343 static void compose_ext_editor_cb (gpointer data,
347 static gint compose_delete_cb (GtkWidget *widget,
350 static void compose_destroy_cb (GtkWidget *widget,
353 static void compose_undo_cb (Compose *compose);
354 static void compose_redo_cb (Compose *compose);
355 static void compose_cut_cb (Compose *compose);
356 static void compose_copy_cb (Compose *compose);
357 static void compose_paste_cb (Compose *compose);
358 static void compose_allsel_cb (Compose *compose);
360 static void compose_gtk_stext_action_cb (Compose *compose,
361 ComposeCallGtkSTextAction action);
363 static void compose_grab_focus_cb (GtkWidget *widget,
366 static void compose_changed_cb (GtkEditable *editable,
368 static void compose_button_press_cb (GtkWidget *widget,
369 GdkEventButton *event,
372 static void compose_key_press_cb (GtkWidget *widget,
378 static void compose_toggle_to_cb (gpointer data,
381 static void compose_toggle_cc_cb (gpointer data,
384 static void compose_toggle_bcc_cb (gpointer data,
387 static void compose_toggle_replyto_cb (gpointer data,
390 static void compose_toggle_followupto_cb(gpointer data,
393 static void compose_toggle_attach_cb (gpointer data,
397 static void compose_toggle_ruler_cb (gpointer data,
401 static void compose_toggle_sign_cb (gpointer data,
404 static void compose_toggle_encrypt_cb (gpointer data,
408 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
411 static void compose_attach_drag_received_cb (GtkWidget *widget,
412 GdkDragContext *drag_context,
415 GtkSelectionData *data,
419 static void compose_insert_drag_received_cb (GtkWidget *widget,
420 GdkDragContext *drag_context,
423 GtkSelectionData *data,
429 static void to_activated (GtkWidget *widget,
431 static void newsgroups_activated (GtkWidget *widget,
433 static void subject_activated (GtkWidget *widget,
435 static void cc_activated (GtkWidget *widget,
437 static void bcc_activated (GtkWidget *widget,
439 static void replyto_activated (GtkWidget *widget,
441 static void followupto_activated (GtkWidget *widget,
445 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
447 gboolean ignore_replyto,
448 gboolean followup_and_reply_to,
451 void compose_headerentry_changed_cb (GtkWidget *entry,
452 ComposeHeaderEntry *headerentry);
453 void compose_headerentry_key_press_event_cb(GtkWidget *entry,
455 ComposeHeaderEntry *headerentry);
457 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
460 static void compose_check_all (Compose *compose);
461 static void compose_highlight_all (Compose *compose);
462 static void compose_check_backwards (Compose *compose);
463 static void compose_check_forwards_go (Compose *compose);
466 static gboolean compose_send_control_enter (Compose *compose);
467 static void text_activated (GtkWidget *widget,
470 static GtkItemFactoryEntry compose_popup_entries[] =
472 {N_("/_Add..."), NULL, compose_attach_cb, 0, NULL},
473 {N_("/_Remove"), NULL, compose_attach_remove_selected, 0, NULL},
474 {N_("/---"), NULL, NULL, 0, "<Separator>"},
475 {N_("/_Property..."), NULL, compose_attach_property, 0, NULL}
478 static GtkItemFactoryEntry compose_entries[] =
480 {N_("/_File"), NULL, NULL, 0, "<Branch>"},
481 {N_("/_File/_Attach file"), "<control>M", compose_attach_cb, 0, NULL},
482 {N_("/_File/_Insert file"), "<control>I", compose_insert_file_cb, 0, NULL},
483 {N_("/_File/Insert si_gnature"), "<control>G", compose_insert_sig, 0, NULL},
484 {N_("/_File/---"), NULL, NULL, 0, "<Separator>"},
485 {N_("/_File/_Close"), "<control>W", compose_close_cb, 0, NULL},
487 {N_("/_Edit"), NULL, NULL, 0, "<Branch>"},
488 {N_("/_Edit/_Undo"), "<control>Z", compose_undo_cb, 0, NULL},
489 {N_("/_Edit/_Redo"), "<control>Y", compose_redo_cb, 0, NULL},
490 {N_("/_Edit/---"), NULL, NULL, 0, "<Separator>"},
491 {N_("/_Edit/Cu_t"), "<control>X", compose_cut_cb, 0, NULL},
492 {N_("/_Edit/_Copy"), "<control>C", compose_copy_cb, 0, NULL},
493 {N_("/_Edit/_Paste"), "<control>V", compose_paste_cb, 0, NULL},
494 {N_("/_Edit/Select _all"), "<control>A", compose_allsel_cb, 0, NULL},
495 {N_("/_Edit/A_dvanced"), NULL, NULL, 0, "<Branch>"},
496 {N_("/_Edit/A_dvanced/Move a character backward"),
498 compose_gtk_stext_action_cb,
499 COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_CHARACTER,
501 {N_("/_Edit/A_dvanced/Move a character forward"),
503 compose_gtk_stext_action_cb,
504 COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_CHARACTER,
506 {N_("/_Edit/A_dvanced/Move a word backward"),
508 compose_gtk_stext_action_cb,
509 COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_WORD,
511 {N_("/_Edit/A_dvanced/Move a word forward"),
513 compose_gtk_stext_action_cb,
514 COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_WORD,
516 {N_("/_Edit/A_dvanced/Move to beginning of line"),
517 NULL, /* "<control>A" */
518 compose_gtk_stext_action_cb,
519 COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE,
521 {N_("/_Edit/A_dvanced/Move to end of line"),
523 compose_gtk_stext_action_cb,
524 COMPOSE_CALL_GTK_STEXT_MOVE_END_OF_LINE,
526 {N_("/_Edit/A_dvanced/Move to previous line"),
528 compose_gtk_stext_action_cb,
529 COMPOSE_CALL_GTK_STEXT_MOVE_PREVIOUS_LINE,
531 {N_("/_Edit/A_dvanced/Move to next line"),
533 compose_gtk_stext_action_cb,
534 COMPOSE_CALL_GTK_STEXT_MOVE_NEXT_LINE,
536 {N_("/_Edit/A_dvanced/Delete a character backward"),
538 compose_gtk_stext_action_cb,
539 COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_CHARACTER,
541 {N_("/_Edit/A_dvanced/Delete a character forward"),
543 compose_gtk_stext_action_cb,
544 COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_CHARACTER,
546 {N_("/_Edit/A_dvanced/Delete a word backward"),
547 NULL, /* "<control>W" */
548 compose_gtk_stext_action_cb,
549 COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_WORD,
551 {N_("/_Edit/A_dvanced/Delete a word forward"),
552 NULL, /* "<alt>D", */
553 compose_gtk_stext_action_cb,
554 COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_WORD,
556 {N_("/_Edit/A_dvanced/Delete line"),
558 compose_gtk_stext_action_cb,
559 COMPOSE_CALL_GTK_STEXT_DELETE_LINE,
561 {N_("/_Edit/A_dvanced/Delete entire line"),
563 compose_gtk_stext_action_cb,
564 COMPOSE_CALL_GTK_STEXT_DELETE_LINE_N,
566 {N_("/_Edit/A_dvanced/Delete to end of line"),
568 compose_gtk_stext_action_cb,
569 COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END,
571 {N_("/_Edit/---"), NULL, NULL, 0, "<Separator>"},
572 {N_("/_Edit/_Wrap current paragraph"),
573 "<control>L", compose_wrap_line, 0, NULL},
574 {N_("/_Edit/Wrap all long _lines"),
575 "<control><alt>L", compose_wrap_line_all, 0, NULL},
576 {N_("/_Edit/Edit with e_xternal editor"),
577 "<shift><control>X", compose_ext_editor_cb, 0, NULL},
579 {N_("/_Spelling"), NULL, NULL, 0, "<Branch>"},
580 {N_("/_Spelling/_Check all or check selection"),
581 NULL, compose_check_all, 0, NULL},
582 {N_("/_Spelling/_Highlight all misspelled words"),
583 NULL, compose_highlight_all, 0, NULL},
584 {N_("/_Spelling/Check _backwards misspelled word"),
585 NULL, compose_check_backwards , 0, NULL},
586 {N_("/_Spelling/_Forward to next misspelled word"),
587 NULL, compose_check_forwards_go, 0, NULL},
588 {N_("/_Spelling/---"), NULL, NULL, 0, "<Separator>"},
589 {N_("/_Spelling/_Spelling Configuration"),
590 NULL, NULL, 0, "<Branch>"},
592 #if 0 /* NEW COMPOSE GUI */
593 {N_("/_View"), NULL, NULL, 0, "<Branch>"},
594 {N_("/_View/_To"), NULL, compose_toggle_to_cb , 0, "<ToggleItem>"},
595 {N_("/_View/_Cc"), NULL, compose_toggle_cc_cb , 0, "<ToggleItem>"},
596 {N_("/_View/_Bcc"), NULL, compose_toggle_bcc_cb , 0, "<ToggleItem>"},
597 {N_("/_View/_Reply to"), NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
598 {N_("/_View/---"), NULL, NULL, 0, "<Separator>"},
599 {N_("/_View/_Followup to"), NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
600 {N_("/_View/---"), NULL, NULL, 0, "<Separator>"},
601 {N_("/_View/R_uler"), NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
602 {N_("/_View/---"), NULL, NULL, 0, "<Separator>"},
603 {N_("/_View/_Attachment"), NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
605 {N_("/_Message"), NULL, NULL, 0, "<Branch>"},
606 {N_("/_Message/_Send"), "<control>Return",
607 compose_send_cb, 0, NULL},
608 {N_("/_Message/Send _later"), "<shift><control>S",
609 compose_send_later_cb, 0, NULL},
610 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
611 {N_("/_Message/Save to _draft folder"),
612 "<shift><control>D", compose_draft_cb, 0, NULL},
613 {N_("/_Message/Save and _keep editing"),
614 "<control>S", compose_draft_cb, 1, NULL},
615 #if 0 /* NEW COMPOSE GUI */
616 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
617 {N_("/_Message/_To"), NULL, compose_toggle_to_cb , 0, "<ToggleItem>"},
618 {N_("/_Message/_Cc"), NULL, compose_toggle_cc_cb , 0, "<ToggleItem>"},
619 {N_("/_Message/_Bcc"), NULL, compose_toggle_bcc_cb , 0, "<ToggleItem>"},
620 {N_("/_Message/_Reply to"), NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
621 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
622 {N_("/_Message/_Followup to"), NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
623 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
624 {N_("/_Message/_Attach"), NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
627 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
628 {N_("/_Message/Si_gn"), NULL, compose_toggle_sign_cb , 0, "<ToggleItem>"},
629 {N_("/_Message/_Encrypt"), NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
630 #endif /* USE_GPGME */
631 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
632 {N_("/_Message/_Request Return Receipt"), NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
633 {N_("/_Tools"), NULL, NULL, 0, "<Branch>"},
634 {N_("/_Tools/Show _ruler"), NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
635 {N_("/_Tools/_Address book"), "<shift><control>A", compose_address_cb , 0, NULL},
636 {N_("/_Tools/_Template"), NULL, NULL, 0, "<Branch>"},
637 {N_("/_Tools/Actio_ns"), NULL, NULL, 0, "<Branch>"},
638 {N_("/_Help"), NULL, NULL, 0, "<Branch>"},
639 {N_("/_Help/_About"), NULL, about_show, 0, NULL}
642 static GtkTargetEntry compose_mime_types[] =
644 {"text/uri-list", 0, 0}
647 Compose *compose_new(PrefsAccount *account)
649 return compose_generic_new(account, NULL, NULL);
652 Compose *compose_bounce(PrefsAccount *account, MsgInfo *msginfo)
656 GtkItemFactory *ifactory;
658 c = compose_generic_new(account, NULL, NULL);
660 filename = procmsg_get_message_file(msginfo);
661 if (filename == NULL)
664 c->bounce_filename = filename;
666 if (msginfo->subject)
667 gtk_entry_set_text(GTK_ENTRY(c->subject_entry),
669 gtk_editable_set_editable(GTK_EDITABLE(c->subject_entry), FALSE);
671 compose_quote_fmt(c, msginfo, "%M", NULL, NULL);
672 gtk_editable_set_editable(GTK_EDITABLE(c->text), FALSE);
674 ifactory = gtk_item_factory_from_widget(c->popupmenu);
675 menu_set_sensitive(ifactory, "/Add...", FALSE);
676 menu_set_sensitive(ifactory, "/Remove", FALSE);
677 menu_set_sensitive(ifactory, "/Property...", FALSE);
679 ifactory = gtk_item_factory_from_widget(c->menubar);
680 menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
681 menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
682 menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
683 menu_set_sensitive(ifactory, "/Edit/Paste", FALSE);
684 menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", FALSE);
685 menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", FALSE);
686 menu_set_sensitive(ifactory, "/Edit/Edit with external editor", FALSE);
687 menu_set_sensitive(ifactory, "/Message/Attach", FALSE);
689 menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
690 menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
692 menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
693 menu_set_sensitive(ifactory, "/Tools/Template", FALSE);
695 gtk_widget_set_sensitive(c->insert_btn, FALSE);
696 gtk_widget_set_sensitive(c->attach_btn, FALSE);
697 gtk_widget_set_sensitive(c->sig_btn, FALSE);
698 gtk_widget_set_sensitive(c->exteditor_btn, FALSE);
699 gtk_widget_set_sensitive(c->linewrap_btn, FALSE);
704 Compose *compose_new_with_recipient(PrefsAccount *account, const gchar *mailto)
706 return compose_generic_new(account, mailto, NULL);
709 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
711 return compose_generic_new(account, NULL, item);
714 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item)
718 if (item && item->prefs && item->prefs->enable_default_account)
719 account = account_find_from_id(item->prefs->default_account);
721 if (!account) account = cur_account;
722 g_return_val_if_fail(account != NULL, NULL);
724 compose = compose_create(account, COMPOSE_NEW);
725 compose->replyinfo = NULL;
727 if (prefs_common.auto_sig)
728 compose_insert_sig(compose);
729 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
730 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
732 if (account->protocol != A_NNTP) {
734 compose_entries_set(compose, mailto);
736 } else if(item && item->prefs->enable_default_to) {
737 compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
739 if (item && item->ret_rcpt) {
740 GtkItemFactory *ifactory;
742 ifactory = gtk_item_factory_from_widget(compose->menubar);
743 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
747 compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
750 compose_show_first_last_header(compose, TRUE);
752 /* Set save folder */
753 if(item && item->prefs && item->prefs->save_copy_to_folder) {
754 gchar *folderidentifier;
756 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
757 folderidentifier = folder_item_get_identifier(item);
758 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
759 g_free(folderidentifier);
762 gtk_widget_grab_focus(compose->header_last->entry);
764 if (prefs_common.auto_exteditor)
765 compose_exec_ext_editor(compose);
770 #define CHANGE_FLAGS(msginfo) \
772 if (msginfo->folder->folder->change_flags != NULL) \
773 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
779 Compose *compose_new_followup_and_replyto(PrefsAccount *account,
780 const gchar *followupto, gchar * to)
784 if (!account) account = cur_account;
785 g_return_val_if_fail(account != NULL, NULL);
786 g_return_val_if_fail(account->protocol != A_NNTP, NULL);
788 compose = compose_create(account, COMPOSE_NEW);
790 if (prefs_common.auto_sig)
791 compose_insert_sig(compose);
792 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
793 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
795 compose_entry_append(compose, to, COMPOSE_TO);
796 compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
797 gtk_widget_grab_focus(compose->subject_entry);
803 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
804 gboolean ignore_replyto, const gchar *body)
806 compose_generic_reply(msginfo, quote, to_all, ignore_replyto, FALSE,
810 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
812 gboolean ignore_replyto,
815 compose_generic_reply(msginfo, quote, to_all, ignore_replyto, TRUE,
819 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
821 gboolean ignore_replyto,
822 gboolean followup_and_reply_to,
826 PrefsAccount *account;
827 PrefsAccount *reply_account;
830 g_return_if_fail(msginfo != NULL);
831 g_return_if_fail(msginfo->folder != NULL);
834 /* select the account set in folderitem's property (if enabled) */
835 if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
836 account = account_find_from_id(msginfo->folder->prefs->default_account);
838 /* select the account for the whole folder (IMAP / NNTP) */
840 /* FIXME: this is not right, because folder may be nested. we should
841 * ascend the tree until we find a parent with proper account
843 account = msginfo->folder->folder->account;
845 /* select account by to: and cc: header if enabled */
846 if (prefs_common.reply_account_autosel) {
847 if (!account && msginfo->to) {
849 Xstrdup_a(to, msginfo->to, return);
851 account = account_find_from_address(to);
855 if(!get_header_from_msginfo(msginfo, cc, sizeof(cc), "CC:")) { /* Found a CC header */
857 account = account_find_from_address(cc);
862 /* select current account */
863 if (!account) account = cur_account;
864 g_return_if_fail(account != NULL);
866 if (ignore_replyto && account->protocol == A_NNTP &&
867 !followup_and_reply_to) {
869 account_find_from_address(account->address);
871 reply_account = compose_current_mail_account();
875 reply_account = account;
877 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
878 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
879 if (MSG_IS_IMAP(msginfo->flags))
880 imap_msg_set_perm_flags(msginfo, MSG_REPLIED);
881 CHANGE_FLAGS(msginfo);
883 compose = compose_create(account, COMPOSE_REPLY);
884 compose->replyinfo = procmsg_msginfo_copy(msginfo);
886 #if 0 /* NEW COMPOSE GUI */
887 if (followup_and_reply_to) {
888 gtk_widget_show(compose->to_hbox);
889 gtk_widget_show(compose->to_entry);
890 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
891 compose->use_to = TRUE;
894 if (msginfo->folder && msginfo->folder->ret_rcpt) {
895 GtkItemFactory *ifactory;
897 ifactory = gtk_item_factory_from_widget(compose->menubar);
898 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
901 /* Set save folder */
902 if(msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
903 gchar *folderidentifier;
905 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
906 folderidentifier = folder_item_get_identifier(msginfo->folder);
907 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
908 g_free(folderidentifier);
911 if (compose_parse_header(compose, msginfo) < 0) return;
912 compose_reply_set_entry(compose, msginfo, to_all, ignore_replyto,
913 followup_and_reply_to);
914 compose_show_first_last_header(compose, TRUE);
916 text = GTK_STEXT(compose->text);
917 gtk_stext_freeze(text);
923 if (prefs_common.quotemark && *prefs_common.quotemark)
924 qmark = prefs_common.quotemark;
928 quote_str = compose_quote_fmt(compose, msginfo,
929 prefs_common.quotefmt,
933 if (prefs_common.auto_sig)
934 compose_insert_sig(compose);
936 if (quote && prefs_common.linewrap_quote)
937 compose_wrap_line_all(compose);
939 gtk_editable_set_position(GTK_EDITABLE(text), 0);
940 gtk_stext_set_point(text, 0);
942 gtk_stext_thaw(text);
943 gtk_widget_grab_focus(compose->text);
945 if (prefs_common.auto_exteditor)
946 compose_exec_ext_editor(compose);
949 static void set_toolbar_style(Compose *compose);
951 static gchar *procmime_get_file_name(MimeInfo *mimeinfo)
956 g_return_val_if_fail(mimeinfo != NULL, NULL);
958 base = mimeinfo->filename ? mimeinfo->filename
959 : mimeinfo->name ? mimeinfo->name : NULL;
961 if (MIME_TEXT_HTML == mimeinfo->mime_type && base == NULL){
962 filename = g_strdup_printf("%s%smimetmp.%08x.html",
969 base = base ? base : "";
970 base = g_basename(base);
972 filename = g_strdup_printf("%s%smimetmp.%08x",
980 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
986 static gchar *mime_extract_file(gchar *source, MimeInfo *partinfo)
990 if (!partinfo) return NULL;
992 filename = procmime_get_file_name(partinfo);
994 if (procmime_get_part(filename, source, partinfo) < 0)
996 (_("Can't get the part of multipart message."));
1003 #define INSERT_FW_HEADER(var, hdr) \
1004 if (msginfo->var && *msginfo->var) { \
1005 gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
1006 gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
1007 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
1010 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
1011 gboolean as_attach, const gchar *body)
1014 /* PrefsAccount *account; */
1017 g_return_val_if_fail(msginfo != NULL, NULL);
1018 g_return_val_if_fail(msginfo->folder != NULL, NULL);
1020 account = msginfo->folder->folder->account;
1021 if (!account && msginfo->to && prefs_common.forward_account_autosel) {
1023 Xstrdup_a(to, msginfo->to, return NULL);
1024 extract_address(to);
1025 account = account_find_from_address(to);
1028 if(!account && prefs_common.forward_account_autosel) {
1030 if(!get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
1031 extract_address(cc);
1032 account = account_find_from_address(cc);
1036 if (account == NULL) {
1037 account = cur_account;
1039 account = msginfo->folder->folder->account;
1040 if (!account) account = cur_account;
1043 g_return_val_if_fail(account != NULL, NULL);
1045 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
1046 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
1047 if (MSG_IS_IMAP(msginfo->flags))
1048 imap_msg_unset_perm_flags(msginfo, MSG_REPLIED);
1049 CHANGE_FLAGS(msginfo);
1051 compose = compose_create(account, COMPOSE_FORWARD);
1053 if (msginfo->subject && *msginfo->subject) {
1054 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
1055 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
1059 text = GTK_STEXT(compose->text);
1060 gtk_stext_freeze(text);
1066 msgfile = procmsg_get_message_file_path(msginfo);
1067 if (!is_file_exist(msgfile))
1068 g_warning(_("%s: file not exist\n"), msgfile);
1070 compose_attach_append(compose, msgfile, msgfile,
1078 if (prefs_common.fw_quotemark &&
1079 *prefs_common.fw_quotemark)
1080 qmark = prefs_common.fw_quotemark;
1084 quote_str = compose_quote_fmt(compose, msginfo,
1085 prefs_common.fw_quotefmt,
1087 compose_attach_parts(compose, msginfo);
1090 if (prefs_common.auto_sig)
1091 compose_insert_sig(compose);
1093 if (prefs_common.linewrap_quote)
1094 compose_wrap_line_all(compose);
1096 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1097 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1099 gtk_stext_thaw(text);
1100 #if 0 /* NEW COMPOSE GUI */
1101 if (account->protocol != A_NNTP)
1102 gtk_widget_grab_focus(compose->to_entry);
1104 gtk_widget_grab_focus(compose->newsgroups_entry);
1106 gtk_widget_grab_focus(compose->header_last->entry);
1108 if (prefs_common.auto_exteditor)
1109 compose_exec_ext_editor(compose);
1114 #undef INSERT_FW_HEADER
1116 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1123 g_return_val_if_fail(msginfo_list != NULL, NULL);
1125 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1126 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
1130 if (account == NULL) {
1131 account = cur_account;
1133 account = msginfo->folder->folder->account;
1134 if (!account) account = cur_account;
1137 g_return_val_if_fail(account != NULL, NULL);
1139 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1140 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1141 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1142 CHANGE_FLAGS(((MsgInfo *)msginfo->data));
1145 compose = compose_create(account, COMPOSE_FORWARD);
1147 text = GTK_STEXT(compose->text);
1148 gtk_stext_freeze(text);
1150 for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1151 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1152 if (!is_file_exist(msgfile))
1153 g_warning(_("%s: file not exist\n"), msgfile);
1155 compose_attach_append(compose, msgfile, msgfile,
1160 if (prefs_common.auto_sig)
1161 compose_insert_sig(compose);
1163 if (prefs_common.linewrap_quote)
1164 compose_wrap_line_all(compose);
1166 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1167 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1169 gtk_stext_thaw(text);
1170 #if 0 /* NEW COMPOSE GUI */
1171 if (account->protocol != A_NNTP)
1172 gtk_widget_grab_focus(compose->to_entry);
1174 gtk_widget_grab_focus(compose->newsgroups_entry);
1180 void compose_reedit(MsgInfo *msginfo)
1183 PrefsAccount *account = NULL;
1186 gchar buf[BUFFSIZE];
1188 g_return_if_fail(msginfo != NULL);
1189 g_return_if_fail(msginfo->folder != NULL);
1191 if (msginfo->folder->stype == F_QUEUE) {
1192 gchar queueheader_buf[BUFFSIZE];
1195 /* Select Account from queue headers */
1196 if (!get_header_from_msginfo(msginfo, queueheader_buf,
1197 sizeof(queueheader_buf), "NAID:")) {
1198 id = atoi(&queueheader_buf[5]);
1199 account = account_find_from_id(id);
1201 if (!account && !get_header_from_msginfo(msginfo, queueheader_buf,
1202 sizeof(queueheader_buf), "MAID:")) {
1203 id = atoi(&queueheader_buf[5]);
1204 account = account_find_from_id(id);
1206 if (!account && !get_header_from_msginfo(msginfo, queueheader_buf,
1207 sizeof(queueheader_buf), "S:")) {
1208 account = account_find_from_address(queueheader_buf);
1211 account = msginfo->folder->folder->account;
1213 if (!account && prefs_common.reedit_account_autosel) {
1214 gchar from[BUFFSIZE];
1215 if (!get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
1216 extract_address(from);
1217 account = account_find_from_address(from);
1220 if (!account) account = cur_account;
1221 g_return_if_fail(account != NULL);
1223 compose = compose_create(account, COMPOSE_REEDIT);
1224 compose->targetinfo = procmsg_msginfo_copy(msginfo);
1226 if (msginfo->folder->stype == F_QUEUE) {
1227 gchar queueheader_buf[BUFFSIZE];
1229 /* Set message save folder */
1230 if (!get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1233 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1234 gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1235 gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1239 if (compose_parse_header(compose, msginfo) < 0) return;
1240 compose_reedit_set_entry(compose, msginfo);
1242 text = GTK_STEXT(compose->text);
1243 gtk_stext_freeze(text);
1245 if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1246 g_warning(_("Can't get text part\n"));
1248 while (fgets(buf, sizeof(buf), fp) != NULL)
1249 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1252 compose_attach_parts(compose, msginfo);
1254 gtk_stext_thaw(text);
1255 gtk_widget_grab_focus(compose->text);
1257 if (prefs_common.auto_exteditor)
1258 compose_exec_ext_editor(compose);
1261 GList *compose_get_compose_list(void)
1263 return compose_list;
1266 void compose_entry_append(Compose *compose, const gchar *address,
1267 ComposeEntryType type)
1273 if (!address || *address == '\0') return;
1275 #if 0 /* NEW COMPOSE GUI */
1278 entry = GTK_ENTRY(compose->cc_entry);
1281 entry = GTK_ENTRY(compose->bcc_entry);
1283 case COMPOSE_NEWSGROUPS:
1284 entry = GTK_ENTRY(compose->newsgroups_entry);
1288 entry = GTK_ENTRY(compose->to_entry);
1292 text = gtk_entry_get_text(entry);
1294 gtk_entry_append_text(entry, ", ");
1295 gtk_entry_append_text(entry, address);
1303 header = N_("Bcc:");
1305 case COMPOSE_REPLYTO:
1306 header = N_("Reply-To:");
1308 case COMPOSE_NEWSGROUPS:
1309 header = N_("Newsgroups:");
1311 case COMPOSE_FOLLOWUPTO:
1312 header = N_( "Followup-To:");
1319 header = prefs_common.trans_hdr ? gettext(header) : header;
1321 compose_add_header_entry(compose, header, (gchar *)address);
1324 static void compose_entries_set(Compose *compose, const gchar *mailto)
1326 gchar *subject = NULL;
1334 Xstrdup_a(tmp_mailto, mailto, return);
1338 p = strchr(tmp_mailto, '?');
1345 gchar *field, *value;
1362 if (*value == '\0') continue;
1364 if (!g_strcasecmp(field, "subject")) {
1365 Xalloca(subject, strlen(value) + 1, return);
1366 decode_uri(subject, value);
1367 } else if (!g_strcasecmp(field, "cc")) {
1369 } else if (!g_strcasecmp(field, "bcc")) {
1371 } else if (!g_strcasecmp(field, "body")) {
1372 Xalloca(body, strlen(value) + 1, return);
1373 decode_uri(body, value);
1378 compose_entry_append(compose, to, COMPOSE_TO);
1380 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1382 compose_entry_append(compose, cc, COMPOSE_CC);
1384 compose_entry_append(compose, bcc, COMPOSE_BCC);
1386 gtk_stext_insert(GTK_STEXT(compose->text),
1387 NULL, NULL, NULL, body, -1);
1388 gtk_stext_insert(GTK_STEXT(compose->text),
1389 NULL, NULL, NULL, "\n", 1);
1393 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1395 static HeaderEntry hentry[] = {{"Reply-To:", NULL, TRUE},
1396 {"Cc:", NULL, FALSE},
1397 {"References:", NULL, FALSE},
1398 {"Bcc:", NULL, FALSE},
1399 {"Newsgroups:", NULL, FALSE},
1400 {"Followup-To:", NULL, FALSE},
1401 {"X-Mailing-List:", NULL, FALSE},
1402 {"X-BeenThere:", NULL, FALSE},
1403 {NULL, NULL, FALSE}};
1413 H_X_MAILING_LIST = 6,
1419 g_return_val_if_fail(msginfo != NULL, -1);
1421 if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1422 procheader_get_header_fields(fp, hentry);
1425 if (hentry[H_REPLY_TO].body != NULL) {
1426 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1427 compose->replyto = hentry[H_REPLY_TO].body;
1428 hentry[H_REPLY_TO].body = NULL;
1430 if (hentry[H_CC].body != NULL) {
1431 conv_unmime_header_overwrite(hentry[H_CC].body);
1432 compose->cc = hentry[H_CC].body;
1433 hentry[H_CC].body = NULL;
1435 if (hentry[H_X_MAILING_LIST].body != NULL) {
1436 /* this is good enough to parse debian-devel */
1437 gchar *buf = g_malloc(strlen(hentry[H_X_MAILING_LIST].body) + 1);
1438 g_return_val_if_fail(buf != NULL, -1 );
1439 if (1 == sscanf(hentry[H_X_MAILING_LIST].body, "<%[^>]>", buf))
1440 compose->mailinglist = g_strdup(buf);
1442 g_free(hentry[H_X_MAILING_LIST].body);
1443 hentry[H_X_MAILING_LIST].body = NULL ;
1445 if (hentry[H_X_BEENTHERE].body != NULL) {
1446 /* this is good enough to parse the sylpheed-claws lists */
1447 gchar *buf = g_malloc(strlen(hentry[H_X_BEENTHERE].body) + 1);
1448 g_return_val_if_fail(buf != NULL, -1 );
1449 if (1 == sscanf(hentry[H_X_BEENTHERE].body, "%[^>]", buf))
1450 compose->mailinglist = g_strdup(buf);
1452 g_free(hentry[H_X_BEENTHERE].body);
1453 hentry[H_X_BEENTHERE].body = NULL ;
1455 if (hentry[H_REFERENCES].body != NULL) {
1456 if (compose->mode == COMPOSE_REEDIT)
1457 compose->references = hentry[H_REFERENCES].body;
1459 compose->references = compose_parse_references
1460 (hentry[H_REFERENCES].body, msginfo->msgid);
1461 g_free(hentry[H_REFERENCES].body);
1463 hentry[H_REFERENCES].body = NULL;
1465 if (hentry[H_BCC].body != NULL) {
1466 if (compose->mode == COMPOSE_REEDIT) {
1467 conv_unmime_header_overwrite(hentry[H_BCC].body);
1468 compose->bcc = hentry[H_BCC].body;
1470 g_free(hentry[H_BCC].body);
1471 hentry[H_BCC].body = NULL;
1473 if (hentry[H_NEWSGROUPS].body != NULL) {
1474 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1475 hentry[H_NEWSGROUPS].body = NULL;
1477 if (hentry[H_FOLLOWUP_TO].body != NULL) {
1478 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1479 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1480 hentry[H_FOLLOWUP_TO].body = NULL;
1483 if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1484 compose->inreplyto = g_strdup(msginfo->inreplyto);
1485 else if (compose->mode != COMPOSE_REEDIT &&
1486 msginfo->msgid && *msginfo->msgid) {
1487 compose->inreplyto = g_strdup(msginfo->msgid);
1489 if (!compose->references) {
1490 if (msginfo->inreplyto && *msginfo->inreplyto)
1491 compose->references =
1492 g_strdup_printf("<%s>\n\t<%s>",
1496 compose->references =
1497 g_strconcat("<", msginfo->msgid, ">",
1505 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1507 GSList *ref_id_list, *cur;
1511 ref_id_list = references_list_append(NULL, ref);
1512 if (!ref_id_list) return NULL;
1513 if (msgid && *msgid)
1514 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1519 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1520 /* "<" + Message-ID + ">" + CR+LF+TAB */
1521 len += strlen((gchar *)cur->data) + 5;
1523 if (len > MAX_REFERENCES_LEN) {
1524 /* remove second message-ID */
1525 if (ref_id_list && ref_id_list->next &&
1526 ref_id_list->next->next) {
1527 g_free(ref_id_list->next->data);
1528 ref_id_list = g_slist_remove
1529 (ref_id_list, ref_id_list->next->data);
1531 slist_free_strings(ref_id_list);
1532 g_slist_free(ref_id_list);
1539 new_ref = g_string_new("");
1540 for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1541 if (new_ref->len > 0)
1542 g_string_append(new_ref, "\n\t");
1543 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1546 slist_free_strings(ref_id_list);
1547 g_slist_free(ref_id_list);
1549 new_ref_str = new_ref->str;
1550 g_string_free(new_ref, FALSE);
1555 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1556 const gchar *fmt, const gchar *qmark,
1559 GtkSText *text = GTK_STEXT(compose->text);
1560 gchar *quote_str = NULL;
1565 if (qmark != NULL) {
1566 quote_fmt_init(msginfo, NULL, NULL);
1567 quote_fmt_scan_string(qmark);
1570 buf = quote_fmt_get_buffer();
1572 alertpanel_error(_("Quote mark format error."));
1574 Xstrdup_a(quote_str, buf, return NULL)
1577 if (fmt && *fmt != '\0') {
1578 quote_fmt_init(msginfo, quote_str, body);
1579 quote_fmt_scan_string(fmt);
1582 buf = quote_fmt_get_buffer();
1584 alertpanel_error(_("Message reply/forward format error."));
1590 gtk_stext_freeze(text);
1592 for (p = buf; *p != '\0'; ) {
1593 lastp = strchr(p, '\n');
1594 len = lastp ? lastp - p + 1 : -1;
1595 gtk_stext_insert(text, NULL, NULL, NULL, p, len);
1602 gtk_stext_thaw(text);
1607 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1608 gboolean to_all, gboolean ignore_replyto,
1609 gboolean followup_and_reply_to)
1611 GSList *cc_list = NULL;
1614 gchar *replyto = NULL;
1615 GHashTable *to_table;
1617 g_return_if_fail(compose->account != NULL);
1618 g_return_if_fail(msginfo != NULL);
1620 if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1621 compose_entry_append(compose,
1622 ((compose->replyto && !ignore_replyto)
1624 : (compose->mailinglist && !ignore_replyto)
1625 ? compose->mailinglist
1626 : msginfo->from ? msginfo->from : ""),
1629 if (compose->account->protocol == A_NNTP) {
1631 compose_entry_append
1632 (compose, msginfo->from ? msginfo->from : "",
1635 compose_entry_append
1637 compose->followup_to ? compose->followup_to
1638 : compose->newsgroups ? compose->newsgroups
1640 COMPOSE_NEWSGROUPS);
1643 if (msginfo->subject && *msginfo->subject) {
1644 gchar *buf, *buf2, *p;
1646 buf = g_strdup(msginfo->subject);
1647 while (!strncasecmp(buf, "Re:", 3)) {
1649 while (isspace(*p)) p++;
1650 memmove(buf, p, strlen(p) + 1);
1653 buf2 = g_strdup_printf("Re: %s", buf);
1654 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1658 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1660 if (!to_all || compose->account->protocol == A_NNTP) return;
1662 if (compose->replyto) {
1663 Xstrdup_a(replyto, compose->replyto, return);
1664 extract_address(replyto);
1666 if (msginfo->from) {
1667 Xstrdup_a(from, msginfo->from, return);
1668 extract_address(from);
1671 if (compose->mailinglist && from) {
1672 cc_list = address_list_append(cc_list, from);
1675 if (replyto && from)
1676 cc_list = address_list_append(cc_list, from);
1678 if (!compose->mailinglist)
1679 cc_list = address_list_append(cc_list, msginfo->to);
1681 cc_list = address_list_append(cc_list, compose->cc);
1683 to_table = g_hash_table_new(g_str_hash, g_str_equal);
1685 g_hash_table_insert(to_table, replyto, GINT_TO_POINTER(1));
1686 if (compose->account)
1687 g_hash_table_insert(to_table, compose->account->address,
1688 GINT_TO_POINTER(1));
1690 /* remove address on To: and that of current account */
1691 for (cur = cc_list; cur != NULL; ) {
1692 GSList *next = cur->next;
1694 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1695 cc_list = g_slist_remove(cc_list, cur->data);
1697 g_hash_table_insert(to_table, cur->data, cur);
1701 g_hash_table_destroy(to_table);
1704 for (cur = cc_list; cur != NULL; cur = cur->next)
1705 compose_entry_append(compose, (gchar *)cur->data,
1707 slist_free_strings(cc_list);
1708 g_slist_free(cc_list);
1712 #define SET_ENTRY(entry, str) \
1715 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1718 #define SET_ADDRESS(type, str) \
1721 compose_entry_append(compose, str, type); \
1724 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1726 g_return_if_fail(msginfo != NULL);
1728 SET_ENTRY(subject_entry, msginfo->subject);
1729 SET_ADDRESS(COMPOSE_TO, msginfo->to);
1730 SET_ADDRESS(COMPOSE_CC, compose->cc);
1731 SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1732 SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1734 compose_show_first_last_header(compose, TRUE);
1736 #if 0 /* NEW COMPOSE GUI */
1738 GtkItemFactory *ifactory;
1739 GtkWidget *menuitem;
1741 ifactory = gtk_item_factory_from_widget(compose->menubar);
1742 menuitem = gtk_item_factory_get_item(ifactory, "/View/Bcc");
1743 gtk_check_menu_item_set_active
1744 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1746 if (compose->replyto) {
1747 GtkItemFactory *ifactory;
1748 GtkWidget *menuitem;
1750 ifactory = gtk_item_factory_from_widget(compose->menubar);
1751 menuitem = gtk_item_factory_get_item
1752 (ifactory, "/View/Reply to");
1753 gtk_check_menu_item_set_active
1754 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1762 static void compose_exec_sig(Compose *compose, gchar *sigfile)
1769 size_t buf_len = 128;
1771 if (strlen(sigfile) < 2)
1774 sigprg = popen(sigfile+1, "r");
1777 buf = g_malloc(buf_len);
1780 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1781 "Unable to insert signature (malloc failed)\n", -1);
1787 while (!feof(sigprg)) {
1788 bzero(buf, buf_len);
1789 fread(buf, buf_len-1, 1, sigprg);
1790 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
1798 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1799 "Can't exec file: ", -1);
1800 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1805 static void compose_insert_sig(Compose *compose)
1809 if (compose->account && compose->account->sig_path)
1810 sigfile = g_strdup(compose->account->sig_path);
1812 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1813 DEFAULT_SIGNATURE, NULL);
1815 if (!is_file_or_fifo_exist(sigfile) && sigfile[0] != '|') {
1820 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1821 if (prefs_common.sig_sep) {
1822 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1823 prefs_common.sig_sep, -1);
1824 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1828 if (sigfile[0] == '|')
1829 compose_exec_sig(compose, sigfile);
1831 compose_insert_file(compose, sigfile);
1835 static void compose_insert_file(Compose *compose, const gchar *file)
1837 GtkSText *text = GTK_STEXT(compose->text);
1838 gchar buf[BUFFSIZE];
1842 g_return_if_fail(file != NULL);
1844 if ((fp = fopen(file, "rb")) == NULL) {
1845 FILE_OP_ERROR(file, "fopen");
1849 gtk_stext_freeze(text);
1851 while (fgets(buf, sizeof(buf), fp) != NULL) {
1852 /* strip <CR> if DOS/Windows file,
1853 replace <CR> with <LF> if Macintosh file. */
1856 if (len > 0 && buf[len - 1] != '\n') {
1858 if (buf[len] == '\r') buf[len] = '\n';
1860 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1863 gtk_stext_thaw(text);
1868 static void compose_attach_append(Compose *compose, const gchar *file,
1869 const gchar *filename,
1870 const gchar *content_type)
1873 gchar *text[N_ATTACH_COLS];
1878 if (!is_file_exist(file)) {
1879 g_warning(_("File %s doesn't exist\n"), file);
1882 if ((size = get_file_size(file)) < 0) {
1883 g_warning(_("Can't get file size of %s\n"), file);
1887 alertpanel_notice(_("File %s is empty."), file);
1890 if ((fp = fopen(file, "rb")) == NULL) {
1891 alertpanel_error(_("Can't read %s."), file);
1896 #if 0 /* NEW COMPOSE GUI */
1897 if (!compose->use_attach) {
1898 GtkItemFactory *ifactory;
1899 GtkWidget *menuitem;
1901 ifactory = gtk_item_factory_from_widget(compose->menubar);
1902 menuitem = gtk_item_factory_get_item(ifactory,
1903 "/View/Attachment");
1904 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1908 ainfo = g_new0(AttachInfo, 1);
1909 ainfo->file = g_strdup(file);
1912 ainfo->content_type = g_strdup(content_type);
1913 if (!strcasecmp(content_type, "message/rfc822")) {
1914 ainfo->encoding = ENC_7BIT;
1915 ainfo->name = g_strdup_printf
1917 g_basename(filename ? filename : file));
1919 ainfo->encoding = ENC_BASE64;
1920 ainfo->name = g_strdup
1921 (g_basename(filename ? filename : file));
1924 ainfo->content_type = procmime_get_mime_type(file);
1925 if (!ainfo->content_type)
1926 ainfo->content_type =
1927 g_strdup("application/octet-stream");
1928 ainfo->encoding = ENC_BASE64;
1929 ainfo->name = g_strdup(g_basename(filename ? filename : file));
1933 text[COL_MIMETYPE] = ainfo->content_type;
1934 text[COL_SIZE] = to_human_readable(size);
1935 text[COL_NAME] = ainfo->name;
1937 row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1938 gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1941 #define IS_FIRST_PART_TEXT(info) \
1942 ((info->mime_type == MIME_TEXT || info->mime_type == MIME_TEXT_HTML || \
1943 info->mime_type == MIME_TEXT_ENRICHED) || \
1944 (info->mime_type == MIME_MULTIPART && info->content_type && \
1945 !strcasecmp(info->content_type, "multipart/alternative") && \
1946 (info->children && \
1947 (info->children->mime_type == MIME_TEXT || \
1948 info->children->mime_type == MIME_TEXT_HTML || \
1949 info->children->mime_type == MIME_TEXT_ENRICHED))))
1951 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
1958 mimeinfo = procmime_scan_message(msginfo);
1959 if (!mimeinfo) return;
1961 /* skip first text (presumably message body) */
1962 child = mimeinfo->children;
1963 if (!child || IS_FIRST_PART_TEXT(mimeinfo)) {
1964 procmime_mimeinfo_free_all(mimeinfo);
1967 if (IS_FIRST_PART_TEXT(child))
1968 child = child->next;
1970 infile = procmsg_get_message_file_path(msginfo);
1972 while (child != NULL) {
1973 if (child->children || child->mime_type == MIME_MULTIPART) {
1974 child = procmime_mimeinfo_next(child);
1978 outfile = procmime_get_tmp_file_name(child);
1979 if (procmime_get_part(outfile, infile, child) < 0)
1980 g_warning(_("Can't get the part of multipart message."));
1982 compose_attach_append
1984 child->filename ? child->filename : child->name,
1985 child->content_type);
1987 child = child->next;
1991 procmime_mimeinfo_free_all(mimeinfo);
1994 #undef IS_FIRST_PART_TEXT
1996 #define GET_CHAR(pos, buf, len) \
1998 if (text->use_wchar) \
1999 len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos))); \
2001 buf[0] = GTK_STEXT_INDEX(text, (pos)); \
2006 #define INDENT_CHARS ">|#"
2007 #define SPACE_CHARS " \t"
2009 static void compose_wrap_line(Compose *compose)
2011 GtkSText *text = GTK_STEXT(compose->text);
2012 gint ch_len, last_ch_len;
2013 gchar cbuf[MB_LEN_MAX], last_ch;
2017 gint p_start, p_end;
2018 gint line_pos, cur_pos;
2019 gint line_len, cur_len;
2021 gtk_stext_freeze(text);
2023 text_len = gtk_stext_get_length(text);
2025 /* check to see if the point is on the paragraph mark (empty line). */
2026 cur_pos = gtk_stext_get_point(text);
2027 GET_CHAR(cur_pos, cbuf, ch_len);
2028 if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
2030 goto compose_end; /* on the paragraph mark */
2031 GET_CHAR(cur_pos - 1, cbuf, ch_len);
2032 if (ch_len == 1 && *cbuf == '\n')
2033 goto compose_end; /* on the paragraph mark */
2036 /* find paragraph start. */
2037 line_end = quoted = 0;
2038 for (p_start = cur_pos; p_start >= 0; --p_start) {
2039 GET_CHAR(p_start, cbuf, ch_len);
2040 if (ch_len == 1 && *cbuf == '\n') {
2042 goto compose_end; /* quoted part */
2050 && strchr(prefs_common.quote_chars, *cbuf))
2052 else if (ch_len != 1 || !isspace(*cbuf))
2061 /* find paragraph end. */
2063 for (p_end = cur_pos; p_end < text_len; p_end++) {
2064 GET_CHAR(p_end, cbuf, ch_len);
2065 if (ch_len == 1 && *cbuf == '\n') {
2072 if (line_end && ch_len == 1 &&
2073 strchr(prefs_common.quote_chars, *cbuf))
2074 goto compose_end; /* quoted part */
2079 if (p_end >= text_len)
2082 if (p_start >= p_end)
2085 line_len = cur_len = 0;
2089 for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2092 GET_CHAR(cur_pos, cbuf, ch_len);
2099 if (ch_len == 1 && isspace(*cbuf))
2102 if (ch_len == 1 && *cbuf == '\n') {
2104 if (last_ch_len == 1 && !isspace(last_ch)) {
2105 if (cur_pos + 1 < p_end) {
2106 GET_CHAR(cur_pos + 1, cbuf, ch_len);
2107 if (ch_len == 1 && !isspace(*cbuf))
2111 gtk_stext_set_point(text, cur_pos + 1);
2112 gtk_stext_backward_delete(text, 1);
2114 gtk_stext_set_point(text, cur_pos);
2115 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2125 last_ch_len = ch_len;
2129 line_pos = cur_pos + 1;
2130 line_len = cur_len + ch_len;
2133 if (cur_len + ch_len > prefs_common.linewrap_len &&
2137 GET_CHAR(line_pos - 1, cbuf, ch_len);
2138 if (ch_len == 1 && isspace(*cbuf)) {
2139 gtk_stext_set_point(text, line_pos);
2140 gtk_stext_backward_delete(text, 1);
2149 gtk_stext_set_point(text, line_pos);
2150 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2154 cur_len = cur_len - line_len + ch_len;
2160 line_pos = cur_pos + 1;
2161 line_len = cur_len + ch_len;
2167 gtk_stext_thaw(text);
2172 /* Darko: used when I debug wrapping */
2173 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2178 printf("%d [", pos);
2179 for (i = pos; i < tlen; i++) {
2180 ch = GTK_STEXT_INDEX(text, i);
2181 if (breakoncr && ch == '\n')
2191 WAIT_FOR_INDENT_CHAR,
2192 WAIT_FOR_INDENT_CHAR_OR_SPACE
2195 /* return indent length, we allow:
2196 > followed by spaces/tabs
2197 | followed by spaces/tabs
2198 uppercase characters immediately followed by >,
2199 and the repeating sequences of the above */
2200 /* return indent length */
2201 static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
2204 guint i, ch_len, alnum_cnt = 0;
2205 IndentState state = WAIT_FOR_INDENT_CHAR;
2206 gchar cbuf[MB_LEN_MAX];
2210 for (i = start_pos; i < text_len; i++) {
2211 GET_CHAR(i, cbuf, ch_len);
2215 if (cbuf[0] == '\n')
2218 is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
2219 is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
2222 case WAIT_FOR_SPACE:
2223 if (is_space == FALSE)
2225 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2227 case WAIT_FOR_INDENT_CHAR_OR_SPACE:
2228 if (is_indent == FALSE && is_space == FALSE &&
2231 if (is_space == TRUE) {
2233 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2234 } else if (is_indent == TRUE) {
2236 state = WAIT_FOR_SPACE;
2239 state = WAIT_FOR_INDENT_CHAR;
2242 case WAIT_FOR_INDENT_CHAR:
2243 if (is_indent == FALSE && !isupper(cbuf[0]))
2245 if (is_indent == TRUE) {
2247 && !strchr(prefs_common.quote_chars, cbuf[0]))
2250 state = WAIT_FOR_SPACE;
2261 if ((i_len > 0) && (state == WAIT_FOR_INDENT_CHAR))
2267 /* insert quotation string when line was wrapped */
2268 static guint ins_quote(GtkSText *text, guint indent_len,
2269 guint prev_line_pos, guint text_len,
2276 for (i = 0; i < indent_len; i++) {
2277 ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2278 gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2280 ins_len = indent_len;
2286 /* check if we should join the next line */
2287 static gboolean join_next_line(GtkSText *text, guint start_pos, guint tlen,
2290 guint indent_len, ch_len;
2291 gboolean do_join = FALSE;
2292 gchar cbuf[MB_LEN_MAX];
2294 indent_len = get_indent_length(text, start_pos, tlen);
2296 if ((indent_len > 0) && (indent_len == prev_ilen)) {
2297 GET_CHAR(start_pos + indent_len, cbuf, ch_len);
2298 if (ch_len > 0 && (cbuf[0] != '\n'))
2305 static void compose_wrap_line_all(Compose *compose)
2307 GtkSText *text = GTK_STEXT(compose->text);
2309 guint line_pos = 0, cur_pos = 0, p_pos = 0;
2310 gint line_len = 0, cur_len = 0;
2312 gboolean is_new_line = TRUE, do_delete = FALSE;
2314 gboolean linewrap_quote = TRUE;
2315 guint linewrap_len = prefs_common.linewrap_len;
2316 gchar *qfmt = prefs_common.quotemark;
2317 gchar cbuf[MB_LEN_MAX];
2319 gtk_stext_freeze(text);
2321 /* make text buffer contiguous */
2322 /* gtk_stext_compact_buffer(text); */
2324 tlen = gtk_stext_get_length(text);
2326 for (; cur_pos < tlen; cur_pos++) {
2327 /* mark position of new line - needed for quotation wrap */
2330 i_len = get_indent_length(text, cur_pos, tlen);
2332 is_new_line = FALSE;
2335 printf("new line i_len=%d p_pos=", i_len);
2336 dump_text(text, p_pos, tlen, 1);
2340 GET_CHAR(cur_pos, cbuf, ch_len);
2342 /* fix line length for tabs */
2343 if (ch_len == 1 && *cbuf == '\t') {
2344 guint tab_width = text->default_tab_width;
2345 guint tab_offset = line_len % tab_width;
2348 printf("found tab at pos=%d line_len=%d ", cur_pos,
2352 line_len += tab_width - tab_offset - 1;
2356 printf("new_len=%d\n", line_len);
2360 /* we have encountered line break */
2361 if (ch_len == 1 && *cbuf == '\n') {
2363 gchar cb[MB_CUR_MAX];
2365 /* should we join the next line */
2366 if ((i_len != cur_len) && do_delete &&
2367 join_next_line(text, cur_pos + 1, tlen, i_len))
2373 printf("found CR at %d do_del is %d next line is ",
2374 cur_pos, do_delete);
2375 dump_text(text, cur_pos + 1, tlen, 1);
2378 /* skip delete if it is continuous URL */
2379 if (do_delete && (line_pos - p_pos <= i_len) &&
2380 gtkut_stext_is_uri_string(text, line_pos, tlen))
2384 printf("l_len=%d wrap_len=%d do_del=%d\n",
2385 line_len, linewrap_len, do_delete);
2387 /* should we delete to perform smart wrapping */
2388 if (line_len < linewrap_len && do_delete) {
2389 /* get rid of newline */
2390 gtk_stext_set_point(text, cur_pos);
2391 gtk_stext_forward_delete(text, 1);
2394 /* if text starts with quote fmt or with
2395 indent string, delete them */
2398 ilen = gtkut_stext_str_compare_n
2399 (text, cur_pos, p_pos, i_len,
2402 gtk_stext_forward_delete
2408 GET_CHAR(cur_pos, cb, clen);
2410 /* insert space if it's alphanumeric */
2411 if ((cur_pos != line_pos) &&
2412 ((clen > 1) || isalnum(cb[0]))) {
2413 gtk_stext_insert(text, NULL, NULL,
2415 /* gtk_stext_compact_buffer(text); */
2419 /* and start over with current line */
2420 cur_pos = p_pos - 1;
2422 line_len = cur_len = 0;
2426 printf("after delete l_pos=");
2427 dump_text(text, line_pos, tlen, 1);
2432 /* mark new line beginning */
2433 line_pos = cur_pos + 1;
2434 line_len = cur_len = 0;
2445 /* possible line break */
2446 if (ch_len == 1 && isspace(*cbuf)) {
2447 line_pos = cur_pos + 1;
2448 line_len = cur_len + ch_len;
2451 /* are we over wrapping length set in preferences ? */
2452 if (cur_len + ch_len > linewrap_len) {
2456 printf("should wrap cur_pos=%d ", cur_pos);
2457 dump_text(text, p_pos, tlen, 1);
2458 dump_text(text, line_pos, tlen, 1);
2460 /* force wrapping if it is one long word but not URL */
2461 if (line_pos - p_pos <= i_len)
2462 if (!gtkut_stext_is_uri_string
2463 (text, line_pos, tlen))
2464 line_pos = cur_pos - 1;
2466 printf("new line_pos=%d\n", line_pos);
2469 GET_CHAR(line_pos - 1, cbuf, clen);
2471 /* if next character is space delete it */
2472 if (clen == 1 && isspace(*cbuf)) {
2473 if (p_pos + i_len != line_pos ||
2474 !gtkut_stext_is_uri_string
2475 (text, line_pos, tlen)) {
2476 gtk_stext_set_point(text, line_pos);
2477 gtk_stext_backward_delete(text, 1);
2486 /* if it is URL at beginning of line don't wrap */
2487 if (p_pos + i_len == line_pos &&
2488 gtkut_stext_is_uri_string(text, line_pos, tlen)) {
2490 printf("found URL at ");
2491 dump_text(text, line_pos, tlen, 1);
2497 gtk_stext_set_point(text, line_pos);
2498 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2499 /* gtk_stext_compact_buffer(text); */
2502 /* for loop will increase it */
2503 cur_pos = line_pos - 1;
2504 /* start over with current line */
2513 printf("after CR insert ");
2514 dump_text(text, line_pos, tlen, 1);
2515 dump_text(text, cur_pos, tlen, 1);
2518 /* should we insert quotation ? */
2519 if (linewrap_quote && i_len) {
2520 /* only if line is not already quoted */
2521 if (!gtkut_stext_str_compare
2522 (text, line_pos, tlen, qfmt)) {
2525 if (line_pos - p_pos > i_len) {
2527 (text, i_len, p_pos,
2530 /* gtk_stext_compact_buffer(text); */
2534 printf("after quote insert ");
2535 dump_text(text, line_pos, tlen, 1);
2543 line_pos = cur_pos + 1;
2544 line_len = cur_len + ch_len;
2546 /* advance to next character in buffer */
2550 gtk_stext_thaw(text);
2555 static void compose_set_title(Compose *compose)
2560 edited = compose->modified ? _(" [Edited]") : "";
2561 if (compose->account && compose->account->address)
2562 str = g_strdup_printf(_("%s - Compose message%s"),
2563 compose->account->address, edited);
2565 str = g_strdup_printf(_("Compose message%s"), edited);
2566 gtk_window_set_title(GTK_WINDOW(compose->window), str);
2571 * compose_current_mail_account:
2573 * Find a current mail account (the currently selected account, or the
2574 * default account, if a news account is currently selected). If a
2575 * mail account cannot be found, display an error message.
2577 * Return value: Mail account, or NULL if not found.
2579 static PrefsAccount *
2580 compose_current_mail_account(void)
2584 if (cur_account && cur_account->protocol != A_NNTP)
2587 ac = account_get_default();
2588 if (!ac || ac->protocol == A_NNTP) {
2589 alertpanel_error(_("Account for sending mail is not specified.\n"
2590 "Please select a mail account before sending."));
2597 gboolean compose_check_for_valid_recipient(Compose *compose) {
2598 gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
2599 gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
2600 gboolean recipient_found = FALSE;
2604 /* free to and newsgroup list */
2605 slist_free_strings(compose->to_list);
2606 g_slist_free(compose->to_list);
2607 compose->to_list = NULL;
2609 slist_free_strings(compose->newsgroup_list);
2610 g_slist_free(compose->newsgroup_list);
2611 compose->newsgroup_list = NULL;
2613 /* search header entries for to and newsgroup entries */
2614 for(list = compose->header_list; list; list = list->next) {
2617 header = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry));
2618 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
2620 if(entry[0] != '\0') {
2621 for(strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
2622 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2623 compose->to_list = address_list_append(compose->to_list, entry);
2624 recipient_found = TRUE;
2627 for(strptr = recipient_headers_news; *strptr != NULL; strptr++) {
2628 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2629 compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
2630 recipient_found = TRUE;
2636 return recipient_found;
2639 static gboolean compose_check_entries(Compose *compose, gboolean check_subject)
2643 if (compose_check_for_valid_recipient(compose) == FALSE) {
2644 alertpanel_error(_("Recipient is not specified."));
2648 str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
2649 if (*str == '\0' && check_subject == TRUE) {
2652 aval = alertpanel(_("Send"),
2653 _("Subject is empty. Send it anyway?"),
2654 _("Yes"), _("No"), NULL);
2655 if (aval != G_ALERTDEFAULT)
2662 gint compose_send(Compose *compose)
2668 if (compose_check_entries(compose, TRUE) == FALSE)
2671 val = compose_queue(compose, &msgnum, &folder);
2673 alertpanel_error(_("Could not queue message for sending"));
2677 val = procmsg_send_message_queue(folder_item_fetch_msg(folder, msgnum));
2679 folder_item_remove_msg(folder, msgnum);
2680 folderview_update_item(folder, TRUE);
2685 #if 0 /* compose restructure */
2686 gint compose_send(Compose *compose)
2688 gchar tmp[MAXPATHLEN + 1];
2690 static gboolean lock = FALSE;
2694 g_return_val_if_fail(compose->account != NULL, -1);
2695 g_return_val_if_fail(compose->orig_account != NULL, -1);
2699 if (compose_check_entries(compose, TRUE) == FALSE) {
2704 /* write to temporary file */
2705 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
2706 get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
2708 if (prefs_common.linewrap_at_send)
2709 compose_wrap_line_all(compose);
2711 if (compose_write_to_file(compose, tmp, FALSE) < 0) {
2716 if (!compose->to_list && !compose->newsgroup_list) {
2717 g_warning(_("can't get recipient list."));
2723 if (compose->to_list) {
2726 #if 0 /* NEW COMPOSE GUI */
2727 if (compose->account->protocol != A_NNTP)
2728 ac = compose->account;
2729 else if (compose->orig_account->protocol != A_NNTP)
2730 ac = compose->orig_account;
2731 else if (cur_account && cur_account->protocol != A_NNTP)
2734 ac = compose_current_mail_account();
2742 ac = compose->account;
2744 ok = send_message(tmp, ac, compose->to_list);
2745 statusbar_pop_all();
2748 if (ok == 0 && compose->newsgroup_list) {
2751 if (compose->account->protocol == A_NNTP)
2752 folder = FOLDER(compose->account->folder);
2754 folder = FOLDER(compose->orig_account->folder);
2756 ok = news_post(folder, tmp);
2758 alertpanel_error(_("Error occurred while posting the message to %s ."),
2759 compose->account->nntp_server);
2766 /* queue message if failed to send */
2768 if (prefs_common.queue_msg) {
2773 _("Error occurred while sending the message.\n"
2774 "Put this message into queue folder?"),
2775 _("OK"), _("Cancel"), NULL);
2776 if (G_ALERTDEFAULT == val) {
2777 ok = compose_queue(compose, tmp);
2779 alertpanel_error(_("Can't queue the message."));
2782 alertpanel_error(_("Error occurred while sending the message."));
2784 if (compose->mode == COMPOSE_REEDIT) {
2785 compose_remove_reedit_target(compose);
2786 if (compose->targetinfo)
2787 folderview_update_item
2788 (compose->targetinfo->folder, TRUE);
2790 /* save message to outbox */
2791 if (prefs_common.savemsg) {
2794 outbox = account_get_special_folder
2795 (compose->account, F_OUTBOX);
2796 if (procmsg_save_to_outbox(outbox, tmp, FALSE) < 0)
2798 (_("Can't save the message to outbox."));
2800 folderview_update_item(outbox, TRUE);
2810 static gboolean compose_use_attach(Compose *compose) {
2811 return(gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL);
2814 static gint compose_bounce_write_headers_from_headerlist(Compose *compose,
2817 gchar buf[BUFFSIZE];
2819 gboolean first_address;
2821 ComposeHeaderEntry *headerentry;
2822 gchar *headerentryname;
2823 gchar *header_w_colon;
2827 debug_print(_("Writing bounce header\n"));
2829 header_w_colon = g_strconcat("To:", NULL);
2830 to_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2831 header_w_colon = g_strconcat("Cc:", NULL);
2832 cc_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2834 first_address = TRUE;
2835 for(list = compose->header_list; list; list = list->next) {
2836 headerentry = ((ComposeHeaderEntry *)list->data);
2837 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
2839 if(g_strcasecmp(headerentryname, cc_hdr) == 0
2840 || g_strcasecmp(headerentryname, to_hdr) == 0) {
2841 str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
2842 Xstrdup_a(str, str, return -1);
2844 if(str[0] != '\0') {
2845 compose_convert_header
2846 (buf, sizeof(buf), str,
2847 strlen("Resent-To") + 2);
2849 fprintf(fp, "Resent-To: ");
2850 first_address = FALSE;
2854 fprintf(fp, "%s", buf);
2858 /* if(!first_address) { */
2865 static gint compose_bounce_write_headers(Compose *compose, FILE *fp)
2867 gchar buf[BUFFSIZE];
2869 /* struct utsname utsbuf; */
2871 g_return_val_if_fail(fp != NULL, -1);
2872 g_return_val_if_fail(compose->account != NULL, -1);
2873 g_return_val_if_fail(compose->account->address != NULL, -1);
2876 get_rfc822_date(buf, sizeof(buf));
2877 fprintf(fp, "Resent-Date: %s\n", buf);
2880 if (compose->account->name && *compose->account->name) {
2881 compose_convert_header
2882 (buf, sizeof(buf), compose->account->name,
2884 fprintf(fp, "Resent-From: %s <%s>\n",
2885 buf, compose->account->address);
2887 fprintf(fp, "Resent-From: %s\n", compose->account->address);
2890 compose_bounce_write_headers_from_headerlist(compose, fp);
2892 /* separator between header and body */
2898 static gint compose_bounce_write_to_file(Compose *compose, const gchar *file)
2903 gchar buf[BUFFSIZE];
2905 if ((fp = fopen(compose->bounce_filename, "rb")) == NULL) {
2906 FILE_OP_ERROR(file, "fopen");
2910 if ((fdest = fopen(file, "wb")) == NULL) {
2911 FILE_OP_ERROR(file, "fopen");
2916 /* chmod for security */
2917 if (change_file_mode_rw(fdest, file) < 0) {
2918 FILE_OP_ERROR(file, "chmod");
2919 g_warning(_("can't change file mode\n"));
2922 while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
2923 /* should filter returnpath, delivered-to */
2924 if ((g_strncasecmp(buf, "Return-Path:",
2925 strlen("Return-Path:")) == 0)
2926 || (g_strncasecmp(buf, "Delivered-To:",
2927 strlen("Delivered-To:")) == 0))
2930 if (fputs(buf, fdest) == -1)
2933 if (!prefs_common.bounce_keep_from) {
2934 if (g_strncasecmp(buf, "From:",
2935 strlen("From:")) == 0) {
2936 fputs(" (by way of ", fdest);
2937 if (compose->account->name
2938 && *compose->account->name) {
2939 compose_convert_header
2941 compose->account->name,
2943 fprintf(fdest, "%s <%s>",
2945 compose->account->address);
2947 fprintf(fdest, "%s",
2948 compose->account->address);
2953 if (fputs("\n", fdest) == -1)
2957 compose_bounce_write_headers(compose, fdest);
2959 while ((len = fread(buf, sizeof(gchar), BUFFSIZE, fp)) > 0) {
2960 if (fwrite(buf, sizeof(gchar), len, fdest) == -1)
2978 * interfaces to rfc2015 to keep out the prefs stuff there.
2979 * returns 0 on success and -1 on error. */
2980 static int compose_create_signers_list (Compose *compose, GSList **pkey_list)
2982 const char *keyid = NULL;
2985 switch (compose->account->sign_key) {
2986 case SIGN_KEY_DEFAULT:
2988 return 0; /* nothing to do */
2990 case SIGN_KEY_BY_FROM:
2991 keyid = compose->account->address;
2994 case SIGN_KEY_CUSTOM:
2995 keyid = compose->account->sign_key_id;
2999 g_assert_not_reached ();
3002 key_list = rfc2015_create_signers_list(keyid);
3005 alertpanel_error("Could not find any key associated with currently "
3006 "selected keyid `%s'!", keyid);
3010 *pkey_list = key_list;
3013 #endif /* USE_GPGME */
3015 static gint compose_write_to_file(Compose *compose, const gchar *file,
3022 const gchar *out_codeset;
3023 EncodingType encoding;
3025 if ((fp = fopen(file, "wb")) == NULL) {
3026 FILE_OP_ERROR(file, "fopen");
3030 /* chmod for security */
3031 if (change_file_mode_rw(fp, file) < 0) {
3032 FILE_OP_ERROR(file, "chmod");
3033 g_warning(_("can't change file mode\n"));
3036 /* get all composed text */
3037 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
3038 len = strlen(chars);
3039 if (is_ascii_str(chars)) {
3040 buf = g_strdup(chars);
3041 out_codeset = CS_US_ASCII;
3042 encoding = ENC_7BIT;
3044 const gchar *src_codeset;
3046 out_codeset = conv_get_outgoing_charset_str();
3047 if (!strcasecmp(out_codeset, CS_US_ASCII))
3048 out_codeset = CS_ISO_8859_1;
3049 encoding = procmime_get_encoding_for_charset(out_codeset);
3051 src_codeset = conv_get_current_charset_str();
3052 /* if current encoding is US-ASCII, set it the same as
3053 outgoing one to prevent code conversion failure */
3054 if (!strcasecmp(src_codeset, CS_US_ASCII))
3055 src_codeset = out_codeset;
3057 debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
3058 src_codeset, out_codeset, procmime_get_encoding_str(encoding));
3060 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
3066 _("Can't convert the character encoding of the message.\n"
3067 "Send it anyway?"), _("Yes"), _("+No"), NULL);
3068 if (aval != G_ALERTDEFAULT) {
3074 buf = g_strdup(chars);
3081 if (compose_write_headers
3082 (compose, fp, out_codeset, encoding, is_draft) < 0) {
3083 g_warning(_("can't write headers\n"));
3090 if (compose_use_attach(compose)) {
3092 /* This prolog message is ignored by mime software and
3093 * because it would make our signing/encryption task
3094 * tougher, we don't emit it in that case */
3095 if (!compose->use_signing && !compose->use_encryption)
3097 fputs("This is a multi-part message in MIME format.\n", fp);
3099 fprintf(fp, "\n--%s\n", compose->boundary);
3100 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
3102 fprintf(fp, "Content-Transfer-Encoding: %s\n",
3103 procmime_get_encoding_str(encoding));
3109 if (encoding == ENC_BASE64) {
3110 gchar outbuf[B64_BUFFSIZE];
3113 for (i = 0; i < len; i += B64_LINE_SIZE) {
3114 l = MIN(B64_LINE_SIZE, len - i);
3115 to64frombits(outbuf, buf + i, l);
3119 } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
3120 FILE_OP_ERROR(file, "fwrite");
3128 if (compose_use_attach(compose))
3129 compose_write_attach(compose, fp);
3131 if (fclose(fp) == EOF) {
3132 FILE_OP_ERROR(file, "fclose");
3138 if (compose->use_signing) {
3141 if (compose_create_signers_list(compose, &key_list) == -1 ||
3142 rfc2015_sign(file, key_list) < 0) {
3148 if (compose->use_encryption) {
3149 if (rfc2015_encrypt(file, compose->to_list, compose->account->ascii_armored) < 0) {
3154 #endif /* USE_GPGME */
3159 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
3165 if ((fp = fopen(file, "wb")) == NULL) {
3166 FILE_OP_ERROR(file, "fopen");
3170 /* chmod for security */
3171 if (change_file_mode_rw(fp, file) < 0) {
3172 FILE_OP_ERROR(file, "chmod");
3173 g_warning(_("can't change file mode\n"));
3176 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
3179 len = strlen(chars);
3180 if (fwrite(chars, sizeof(gchar), len, fp) != len) {
3181 FILE_OP_ERROR(file, "fwrite");
3190 if (fclose(fp) == EOF) {
3191 FILE_OP_ERROR(file, "fclose");
3198 static gint compose_remove_reedit_target(Compose *compose)
3201 MsgInfo *msginfo = compose->targetinfo;
3203 g_return_val_if_fail(compose->mode == COMPOSE_REEDIT, -1);
3204 if (!msginfo) return -1;
3206 item = msginfo->folder;
3207 g_return_val_if_fail(item != NULL, -1);
3209 folder_item_scan(item);
3210 if (procmsg_msg_exist(msginfo) &&
3211 (item->stype == F_DRAFT || item->stype == F_QUEUE)) {
3212 if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
3213 g_warning(_("can't remove the old message\n"));
3221 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
3223 return compose_queue_sub (compose, msgnum, item, FALSE);
3225 static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item, gboolean check_subject)
3231 gchar buf[BUFFSIZE];
3233 MsgFlags flag = {0, 0};
3234 static gboolean lock = FALSE;
3235 PrefsAccount *mailac = NULL, *newsac = NULL;
3237 debug_print(_("queueing message...\n"));
3238 g_return_val_if_fail(compose->account != NULL, -1);
3239 g_return_val_if_fail(compose->orig_account != NULL, -1);
3243 if (compose_check_entries(compose, check_subject) == FALSE) {