2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 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.
31 #include "procheader.h"
32 #include "send_message.h"
34 #include "statusbar.h"
35 #include "prefs_filtering.h"
36 #include "filtering.h"
38 #include "prefs_common.h"
40 #include "alertpanel.h"
44 #include "partial_download.h"
46 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
48 GHashTable *msg_table;
50 if (mlist == NULL) return NULL;
52 msg_table = g_hash_table_new(NULL, g_direct_equal);
53 procmsg_msg_hash_table_append(msg_table, mlist);
58 void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
63 if (msg_table == NULL || mlist == NULL) return;
65 for (cur = mlist; cur != NULL; cur = cur->next) {
66 msginfo = (MsgInfo *)cur->data;
68 g_hash_table_insert(msg_table,
69 GUINT_TO_POINTER(msginfo->msgnum),
74 GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
76 GHashTable *msg_table;
80 if (mlist == NULL) return NULL;
82 msg_table = g_hash_table_new(NULL, g_direct_equal);
84 for (cur = mlist; cur != NULL; cur = cur->next) {
85 msginfo = (MsgInfo *)cur->data;
86 g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
92 gint procmsg_get_last_num_in_msg_list(GSList *mlist)
98 for (cur = mlist; cur != NULL; cur = cur->next) {
99 msginfo = (MsgInfo *)cur->data;
100 if (msginfo && msginfo->msgnum > last)
101 last = msginfo->msgnum;
107 void procmsg_msg_list_free(GSList *mlist)
112 for (cur = mlist; cur != NULL; cur = cur->next) {
113 msginfo = (MsgInfo *)cur->data;
114 procmsg_msginfo_free(msginfo);
128 /* CLAWS subject threading:
130 in the first round it inserts subject lines in a
131 relation (subject <-> node)
133 the second round finishes the threads by attaching
134 matching subject lines to the one found in the
135 relation. will use the oldest node with the same
136 subject that is not more then thread_by_subject_max_age
137 days old (see subject_relation_lookup)
140 static void subject_relation_insert(GRelation *relation, GNode *node)
145 g_return_if_fail(relation != NULL);
146 g_return_if_fail(node != NULL);
147 msginfo = (MsgInfo *) node->data;
148 g_return_if_fail(msginfo != NULL);
150 subject = msginfo->subject;
153 subject += subject_get_prefix_length(subject);
155 g_relation_insert(relation, subject, node);
158 static GNode *subject_relation_lookup(GRelation *relation, MsgInfo *msginfo)
165 g_return_val_if_fail(relation != NULL, NULL);
167 subject = msginfo->subject;
170 prefix_length = subject_get_prefix_length(subject);
171 if (prefix_length <= 0)
173 subject += prefix_length;
175 tuples = g_relation_select(relation, subject, 0);
179 if (tuples->len > 0) {
181 GNode *relation_node;
182 MsgInfo *relation_msginfo = NULL, *best_msginfo = NULL;
185 /* check all nodes with the same subject to find the best parent */
186 for (i = 0; i < tuples->len; i++) {
187 relation_node = (GNode *) g_tuples_index(tuples, i, 1);
188 relation_msginfo = (MsgInfo *) relation_node->data;
191 /* best node should be the oldest in the found nodes */
192 /* parent node must not be older then msginfo */
193 if ((relation_msginfo->date_t < msginfo->date_t) &&
194 ((best_msginfo == NULL) ||
195 (best_msginfo->date_t > relation_msginfo->date_t)))
198 /* parent node must not be more then thread_by_subject_max_age
199 days older then msginfo */
200 if (abs(difftime(msginfo->date_t, relation_msginfo->date_t)) >
201 prefs_common.thread_by_subject_max_age * 3600 * 24)
204 /* can add new tests for all matching
205 nodes found by subject */
208 node = relation_node;
209 best_msginfo = relation_msginfo;
214 g_tuples_destroy(tuples);
218 /* return the reversed thread tree */
219 GNode *procmsg_get_thread_tree(GSList *mlist)
221 GNode *root, *parent, *node, *next;
222 GHashTable *msgid_table;
223 GRelation *subject_relation;
227 root = g_node_new(NULL);
228 msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
229 subject_relation = g_relation_new(2);
230 g_relation_index(subject_relation, 0, g_str_hash, g_str_equal);
232 for (; mlist != NULL; mlist = mlist->next) {
233 msginfo = (MsgInfo *)mlist->data;
236 if (msginfo->inreplyto) {
237 parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
238 if (parent == NULL) {
242 node = g_node_insert_data_before
243 (parent, parent == root ? parent->children : NULL,
245 if ((msgid = msginfo->msgid) && g_hash_table_lookup(msgid_table, msgid) == NULL)
246 g_hash_table_insert(msgid_table, (gchar *)msgid, node);
248 /* CLAWS: add subject to relation (without prefix) */
249 if (prefs_common.thread_by_subject) {
250 subject_relation_insert(subject_relation, node);
254 /* complete the unfinished threads */
255 for (node = root->children; node != NULL; ) {
258 msginfo = (MsgInfo *)node->data;
259 if (msginfo->inreplyto) {
260 parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
261 /* node should not be the parent, and node should not
262 be an ancestor of parent (circular reference) */
263 if (parent && parent != node &&
264 !g_node_is_ancestor(node, parent)) {
267 (parent, parent->children, node);
273 if (prefs_common.thread_by_subject) {
274 for (node = root->children; node && node != NULL;) {
276 msginfo = (MsgInfo *) node->data;
278 parent = subject_relation_lookup(subject_relation, msginfo);
280 /* the node may already be threaded by IN-REPLY-TO, so go up
282 find the parent node */
283 if (parent != NULL) {
284 if (g_node_is_ancestor(node, parent))
292 g_node_append(parent, node);
299 g_relation_destroy(subject_relation);
300 g_hash_table_destroy(msgid_table);
305 void procmsg_move_messages(GSList *mlist)
307 GSList *cur, *movelist = NULL;
309 FolderItem *dest = NULL;
313 folder_item_update_freeze();
315 for (cur = mlist; cur != NULL; cur = cur->next) {
316 msginfo = (MsgInfo *)cur->data;
318 dest = msginfo->to_folder;
319 movelist = g_slist_append(movelist, msginfo);
320 } else if (dest == msginfo->to_folder) {
321 movelist = g_slist_append(movelist, msginfo);
323 folder_item_move_msgs(dest, movelist);
324 g_slist_free(movelist);
326 dest = msginfo->to_folder;
327 movelist = g_slist_append(movelist, msginfo);
329 procmsg_msginfo_set_to_folder(msginfo, NULL);
333 folder_item_move_msgs(dest, movelist);
334 g_slist_free(movelist);
337 folder_item_update_thaw();
340 void procmsg_copy_messages(GSList *mlist)
342 GSList *cur, *copylist = NULL;
344 FolderItem *dest = NULL;
348 folder_item_update_freeze();
350 for (cur = mlist; cur != NULL; cur = cur->next) {
351 msginfo = (MsgInfo *)cur->data;
353 dest = msginfo->to_folder;
354 copylist = g_slist_append(copylist, msginfo);
355 } else if (dest == msginfo->to_folder) {
356 copylist = g_slist_append(copylist, msginfo);
358 folder_item_copy_msgs(dest, copylist);
359 g_slist_free(copylist);
361 dest = msginfo->to_folder;
362 copylist = g_slist_append(copylist, msginfo);
364 procmsg_msginfo_set_to_folder(msginfo, NULL);
368 folder_item_copy_msgs(dest, copylist);
369 g_slist_free(copylist);
372 folder_item_update_thaw();
375 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
379 g_return_val_if_fail(msginfo != NULL, NULL);
381 if (msginfo->plaintext_file)
382 file = g_strdup(msginfo->plaintext_file);
384 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
390 gchar *procmsg_get_message_file(MsgInfo *msginfo)
392 gchar *filename = NULL;
394 g_return_val_if_fail(msginfo != NULL, NULL);
396 filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
398 debug_print("can't fetch message %d\n", msginfo->msgnum);
403 GSList *procmsg_get_message_file_list(GSList *mlist)
405 GSList *file_list = NULL;
407 MsgFileInfo *fileinfo;
410 while (mlist != NULL) {
411 msginfo = (MsgInfo *)mlist->data;
412 file = procmsg_get_message_file(msginfo);
414 procmsg_message_file_list_free(file_list);
417 fileinfo = g_new(MsgFileInfo, 1);
418 fileinfo->msginfo = procmsg_msginfo_new_ref(msginfo);
419 fileinfo->file = file;
420 fileinfo->flags = g_new(MsgFlags, 1);
421 *fileinfo->flags = msginfo->flags;
422 file_list = g_slist_prepend(file_list, fileinfo);
426 file_list = g_slist_reverse(file_list);
431 void procmsg_message_file_list_free(MsgInfoList *file_list)
434 MsgFileInfo *fileinfo;
436 for (cur = file_list; cur != NULL; cur = cur->next) {
437 fileinfo = (MsgFileInfo *)cur->data;
438 procmsg_msginfo_free(fileinfo->msginfo);
439 g_free(fileinfo->file);
440 g_free(fileinfo->flags);
444 g_slist_free(file_list);
447 FILE *procmsg_open_message(MsgInfo *msginfo)
452 g_return_val_if_fail(msginfo != NULL, NULL);
454 file = procmsg_get_message_file_path(msginfo);
455 g_return_val_if_fail(file != NULL, NULL);
457 if (!is_file_exist(file)) {
459 file = procmsg_get_message_file(msginfo);
464 if ((fp = fopen(file, "rb")) == NULL) {
465 FILE_OP_ERROR(file, "fopen");
472 if (MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags)) {
475 while (fgets(buf, sizeof(buf), fp) != NULL)
476 if (buf[0] == '\r' || buf[0] == '\n') break;
482 gboolean procmsg_msg_exist(MsgInfo *msginfo)
487 if (!msginfo) return FALSE;
489 path = folder_item_get_path(msginfo->folder);
491 ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
497 void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
498 PrefsFilterType type)
500 static HeaderEntry hentry[] = {{"X-BeenThere:", NULL, TRUE},
501 {"X-ML-Name:", NULL, TRUE},
502 {"X-List:", NULL, TRUE},
503 {"X-Mailing-list:", NULL, TRUE},
504 {"List-Id:", NULL, TRUE},
505 {"X-Sequence:", NULL, TRUE},
506 {NULL, NULL, FALSE}};
512 H_X_MAILING_LIST = 3,
519 g_return_if_fail(msginfo != NULL);
520 g_return_if_fail(header != NULL);
521 g_return_if_fail(key != NULL);
530 if ((fp = procmsg_open_message(msginfo)) == NULL)
532 procheader_get_header_fields(fp, hentry);
535 #define SET_FILTER_KEY(hstr, idx) \
537 *header = g_strdup(hstr); \
538 *key = hentry[idx].body; \
539 hentry[idx].body = NULL; \
542 if (hentry[H_X_BEENTHERE].body != NULL) {
543 SET_FILTER_KEY("header \"X-BeenThere\"", H_X_BEENTHERE);
544 } else if (hentry[H_X_ML_NAME].body != NULL) {
545 SET_FILTER_KEY("header \"X-ML-Name\"", H_X_ML_NAME);
546 } else if (hentry[H_X_LIST].body != NULL) {
547 SET_FILTER_KEY("header \"X-List\"", H_X_LIST);
548 } else if (hentry[H_X_MAILING_LIST].body != NULL) {
549 SET_FILTER_KEY("header \"X-Mailing-List\"", H_X_MAILING_LIST);
550 } else if (hentry[H_LIST_ID].body != NULL) {
551 SET_FILTER_KEY("header \"List-Id\"", H_LIST_ID);
552 extract_list_id_str(*key);
553 } else if (hentry[H_X_SEQUENCE].body != NULL) {
556 SET_FILTER_KEY("X-Sequence", H_X_SEQUENCE);
559 while (*p != '\0' && !isspace(*p)) p++;
560 while (isspace(*p)) p++;
567 } else if (msginfo->subject) {
568 *header = g_strdup("subject");
569 *key = g_strdup(msginfo->subject);
572 #undef SET_FILTER_KEY
574 g_free(hentry[H_X_BEENTHERE].body);
575 hentry[H_X_BEENTHERE].body = NULL;
576 g_free(hentry[H_X_ML_NAME].body);
577 hentry[H_X_ML_NAME].body = NULL;
578 g_free(hentry[H_X_LIST].body);
579 hentry[H_X_LIST].body = NULL;
580 g_free(hentry[H_X_MAILING_LIST].body);
581 hentry[H_X_MAILING_LIST].body = NULL;
582 g_free(hentry[H_LIST_ID].body);
583 hentry[H_LIST_ID].body = NULL;
587 *header = g_strdup("from");
588 *key = g_strdup(msginfo->from);
591 *header = g_strdup("to");
592 *key = g_strdup(msginfo->to);
594 case FILTER_BY_SUBJECT:
595 *header = g_strdup("subject");
596 *key = g_strdup(msginfo->subject);
603 void procmsg_empty_trash(FolderItem *trash)
607 if (trash && trash->total_msgs > 0) {
608 GSList *mlist = folder_item_get_msg_list(trash);
610 for (cur = mlist ; cur != NULL ; cur = cur->next) {
611 MsgInfo * msginfo = (MsgInfo *) cur->data;
612 partial_mark_for_delete(msginfo);
613 procmsg_msginfo_free(msginfo);
616 folder_item_remove_all_msg(trash);
620 void procmsg_empty_all_trash(void)
625 for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
626 trash = FOLDER(cur->data)->trash;
627 procmsg_empty_trash(trash);
632 *\brief Send messages in queue
634 *\param queue Queue folder to process
635 *\param save_msgs Unused
637 *\return Number of messages sent, negative if an error occurred
638 * positive if no error occurred
640 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
642 gint sent = 0, err = 0;
646 queue = folder_get_default_queue();
647 g_return_val_if_fail(queue != NULL, -1);
649 folder_item_scan(queue);
650 list = folder_item_get_msg_list(queue);
652 for (elem = list; elem != NULL; elem = elem->next) {
656 msginfo = (MsgInfo *)(elem->data);
657 if (!MSG_IS_LOCKED(msginfo->flags)) {
658 file = folder_item_fetch_msg(queue, msginfo->msgnum);
660 if (procmsg_send_message_queue(file) < 0) {
661 g_warning("Sending queued message %d failed.\n",
666 * We save in procmsg_send_message_queue because
667 * we need the destination folder from the queue
671 procmsg_save_to_outbox
672 (queue->folder->outbox,
676 folder_item_remove_msg(queue, msginfo->msgnum);
681 /* FIXME: supposedly if only one message is locked, and queue
682 * is being flushed, the following free says something like
683 * "freeing msg ## in folder (nil)". */
684 procmsg_msginfo_free(msginfo);
687 return (err != 0 ? -err : sent);
690 gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
695 if ((fp = fopen(in, "rb")) == NULL) {
696 FILE_OP_ERROR(in, "fopen");
699 if ((outfp = fopen(out, "wb")) == NULL) {
700 FILE_OP_ERROR(out, "fopen");
704 while (fgets(buf, sizeof(buf), fp) != NULL)
705 if (buf[0] == '\r' || buf[0] == '\n') break;
706 while (fgets(buf, sizeof(buf), fp) != NULL)
713 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
717 MsgInfo *msginfo, *tmp_msginfo;
718 MsgFlags flag = {0, 0};
720 debug_print("saving sent message...\n");
723 outbox = folder_get_default_outbox();
724 g_return_val_if_fail(outbox != NULL, -1);
726 /* remove queueing headers */
728 gchar tmp[MAXPATHLEN + 1];
730 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
731 get_rc_dir(), G_DIR_SEPARATOR, (guint) rand());
733 if (procmsg_remove_special_headers(file, tmp) !=0)
736 folder_item_scan(outbox);
737 if ((num = folder_item_add_msg(outbox, tmp, &flag, TRUE)) < 0) {
738 g_warning("can't save message\n");
743 folder_item_scan(outbox);
744 if ((num = folder_item_add_msg
745 (outbox, file, &flag, FALSE)) < 0) {
746 g_warning("can't save message\n");
751 msginfo = folder_item_get_msginfo(outbox, num); /* refcnt++ */
752 tmp_msginfo = procmsg_msginfo_get_full_info(msginfo); /* refcnt++ */
753 if (msginfo != NULL) {
754 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
755 procmsg_msginfo_free(msginfo); /* refcnt-- */
756 /* tmp_msginfo == msginfo */
757 if (tmp_msginfo && (msginfo->dispositionnotificationto ||
758 msginfo->returnreceiptto)) {
759 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
760 procmsg_msginfo_free(msginfo); /* refcnt-- */
767 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
769 static const gchar *def_cmd = "lpr %s";
776 g_return_if_fail(msginfo);
778 if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
779 g_warning("Can't get text part\n");
783 prtmp = g_strdup_printf("%s%cprinttmp.%08x",
784 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
786 if ((prfp = fopen(prtmp, "wb")) == NULL) {
787 FILE_OP_ERROR(prtmp, "fopen");
793 if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
794 if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
795 if (msginfo->to) fprintf(prfp, "To: %s\n", msginfo->to);
796 if (msginfo->cc) fprintf(prfp, "Cc: %s\n", msginfo->cc);
797 if (msginfo->newsgroups)
798 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
799 if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
802 while (fgets(buf, sizeof(buf), tmpfp) != NULL)
808 if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
810 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
813 g_warning("Print command line is invalid: `%s'\n",
815 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
821 if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
825 MsgInfo *procmsg_msginfo_new_ref(MsgInfo *msginfo)
832 MsgInfo *procmsg_msginfo_new(void)
836 newmsginfo = g_new0(MsgInfo, 1);
837 newmsginfo->refcnt = 1;
842 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
846 if (msginfo == NULL) return NULL;
848 newmsginfo = g_new0(MsgInfo, 1);
850 newmsginfo->refcnt = 1;
852 #define MEMBCOPY(mmb) newmsginfo->mmb = msginfo->mmb
853 #define MEMBDUP(mmb) newmsginfo->mmb = msginfo->mmb ? \
854 g_strdup(msginfo->mmb) : NULL
879 MEMBDUP(dispositionnotificationto);
880 MEMBDUP(returnreceiptto);
884 MEMBCOPY(threadscore);
885 MEMBDUP(plaintext_file);
890 MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
892 MsgInfo *full_msginfo;
895 if (msginfo == NULL) return NULL;
897 file = procmsg_get_message_file(msginfo);
899 g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
903 full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE, FALSE);
905 if (!full_msginfo) return NULL;
907 /* CLAWS: make sure we add the missing members; see:
908 * procheader.c::procheader_get_headernames() */
910 msginfo->xface = g_strdup(full_msginfo->xface);
911 if (!msginfo->dispositionnotificationto)
912 msginfo->dispositionnotificationto =
913 g_strdup(full_msginfo->dispositionnotificationto);
914 if (!msginfo->returnreceiptto)
915 msginfo->returnreceiptto = g_strdup
916 (full_msginfo->returnreceiptto);
917 if (!msginfo->partial_recv && full_msginfo->partial_recv)
918 msginfo->partial_recv = g_strdup
919 (full_msginfo->partial_recv);
920 msginfo->total_size = full_msginfo->total_size;
921 if (!msginfo->account_server && full_msginfo->account_server)
922 msginfo->account_server = g_strdup
923 (full_msginfo->account_server);
924 if (!msginfo->account_login && full_msginfo->account_login)
925 msginfo->account_login = g_strdup
926 (full_msginfo->account_login);
927 msginfo->planned_download = full_msginfo->planned_download;
928 procmsg_msginfo_free(full_msginfo);
930 return procmsg_msginfo_new_ref(msginfo);
933 void procmsg_msginfo_free(MsgInfo *msginfo)
935 if (msginfo == NULL) return;
938 if (msginfo->refcnt > 0)
941 if (msginfo->to_folder) {
942 msginfo->to_folder->op_count--;
943 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
946 g_free(msginfo->fromspace);
947 g_free(msginfo->references);
948 g_free(msginfo->returnreceiptto);
949 g_free(msginfo->dispositionnotificationto);
950 g_free(msginfo->xface);
952 g_free(msginfo->fromname);
954 g_free(msginfo->date);
955 g_free(msginfo->from);
958 g_free(msginfo->newsgroups);
959 g_free(msginfo->subject);
960 g_free(msginfo->msgid);
961 g_free(msginfo->inreplyto);
962 g_free(msginfo->xref);
964 g_free(msginfo->partial_recv);
965 g_free(msginfo->account_server);
966 g_free(msginfo->account_login);
968 g_free(msginfo->plaintext_file);
973 guint procmsg_msginfo_memusage(MsgInfo *msginfo)
977 memusage += sizeof(MsgInfo);
978 if (msginfo->fromname)
979 memusage += strlen(msginfo->fromname);
981 memusage += strlen(msginfo->date);
983 memusage += strlen(msginfo->from);
985 memusage += strlen(msginfo->to);
987 memusage += strlen(msginfo->cc);
988 if (msginfo->newsgroups)
989 memusage += strlen(msginfo->newsgroups);
990 if (msginfo->subject)
991 memusage += strlen(msginfo->subject);
993 memusage += strlen(msginfo->msgid);
994 if (msginfo->inreplyto)
995 memusage += strlen(msginfo->inreplyto);
997 memusage += strlen(msginfo->xface);
998 if (msginfo->dispositionnotificationto)
999 memusage += strlen(msginfo->dispositionnotificationto);
1000 if (msginfo->returnreceiptto)
1001 memusage += strlen(msginfo->returnreceiptto);
1002 if (msginfo->references)
1003 memusage += strlen(msginfo->references);
1004 if (msginfo->fromspace)
1005 memusage += strlen(msginfo->fromspace);
1010 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
1012 const MsgInfo *msginfo1 = a;
1013 const MsgInfo *msginfo2 = b;
1020 return msginfo1->msgnum - msginfo2->msgnum;
1029 Q_MAIL_ACCOUNT_ID = 4,
1030 Q_NEWS_ACCOUNT_ID = 5,
1031 Q_SAVE_COPY_FOLDER = 6,
1032 Q_REPLY_MESSAGE_ID = 7,
1033 Q_FWD_MESSAGE_ID = 8,
1034 Q_PRIVACY_SYSTEM = 9,
1036 Q_ENCRYPT_DATA = 11,
1039 gint procmsg_send_message_queue(const gchar *file)
1041 static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
1042 {"SSV:", NULL, FALSE},
1043 {"R:", NULL, FALSE},
1044 {"NG:", NULL, FALSE},
1045 {"MAID:", NULL, FALSE},
1046 {"NAID:", NULL, FALSE},
1047 {"SCF:", NULL, FALSE},
1048 {"RMID:", NULL, FALSE},
1049 {"FMID:", NULL, FALSE},
1050 {"X-Sylpheed-Privacy-System:", NULL, FALSE},
1051 {"X-Sylpheed-Encrypt:", NULL, FALSE},
1052 {"X-Sylpheed-Encrypt-Data:", NULL, FALSE},
1053 {NULL, NULL, FALSE}};
1056 gint mailval = 0, newsval = 0;
1058 gchar *smtpserver = NULL;
1059 GSList *to_list = NULL;
1060 GSList *newsgroup_list = NULL;
1061 gchar *savecopyfolder = NULL;
1062 gchar *replymessageid = NULL;
1063 gchar *fwdmessageid = NULL;
1064 gchar *privacy_system = NULL;
1065 gboolean encrypt = FALSE;
1066 gchar *encrypt_data = NULL;
1067 gchar buf[BUFFSIZE];
1069 PrefsAccount *mailac = NULL, *newsac = NULL;
1070 gboolean save_clear_text = TRUE;
1071 gchar *tmp_enc_file = NULL;
1075 g_return_val_if_fail(file != NULL, -1);
1077 if ((fp = fopen(file, "rb")) == NULL) {
1078 FILE_OP_ERROR(file, "fopen");
1082 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
1084 gchar *p = buf + strlen(qentry[hnum].name);
1092 if (smtpserver == NULL)
1093 smtpserver = g_strdup(p);
1096 to_list = address_list_append(to_list, p);
1099 newsgroup_list = newsgroup_list_append(newsgroup_list, p);
1101 case Q_MAIL_ACCOUNT_ID:
1102 mailac = account_find_from_id(atoi(p));
1104 case Q_NEWS_ACCOUNT_ID:
1105 newsac = account_find_from_id(atoi(p));
1107 case Q_SAVE_COPY_FOLDER:
1108 if (savecopyfolder == NULL)
1109 savecopyfolder = g_strdup(p);
1111 case Q_REPLY_MESSAGE_ID:
1112 if (replymessageid == NULL)
1113 replymessageid = g_strdup(p);
1115 case Q_FWD_MESSAGE_ID:
1116 if (fwdmessageid == NULL)
1117 fwdmessageid = g_strdup(p);
1119 case Q_PRIVACY_SYSTEM:
1120 if (privacy_system == NULL)
1121 privacy_system = g_strdup(p);
1127 case Q_ENCRYPT_DATA:
1128 if (encrypt_data == NULL)
1129 encrypt_data = g_strdup(p);
1133 filepos = ftell(fp);
1138 save_clear_text = (mailac != NULL && mailac->save_encrypted_as_clear_text);
1143 mimeinfo = procmime_scan_queue_file(file);
1144 if (!privacy_encrypt(privacy_system, mimeinfo, encrypt_data)
1145 || (fp = my_tmpfile()) == NULL
1146 || procmime_write_mimeinfo(mimeinfo, fp) < 0) {
1149 procmime_mimeinfo_free_all(mimeinfo);
1152 slist_free_strings(to_list);
1153 g_slist_free(to_list);
1154 slist_free_strings(newsgroup_list);
1155 g_slist_free(newsgroup_list);
1156 g_free(savecopyfolder);
1157 g_free(replymessageid);
1158 g_free(fwdmessageid);
1159 g_free(privacy_system);
1160 g_free(encrypt_data);
1165 if (!save_clear_text) {
1166 gchar *content = NULL;
1167 FILE *tmpfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmp_enc_file);
1171 content = file_read_stream_to_str(fp);
1174 get_tmpfile_in_dir(get_mime_tmp_dir(), &tmp_enc_file);
1175 str_write_to_file(content, tmp_enc_file);
1178 g_warning("couldn't get tempfile\n");
1182 procmime_mimeinfo_free_all(mimeinfo);
1188 debug_print("Sending message by mail\n");
1190 g_warning("Queued message header is broken.\n");
1192 } else if (mailac && mailac->use_mail_command &&
1193 mailac->mail_command && (* mailac->mail_command)) {
1194 mailval = send_message_local(mailac->mail_command, fp);
1198 mailac = account_find_from_smtp_server(from, smtpserver);
1200 g_warning("Account not found. "
1201 "Using current account...\n");
1202 mailac = cur_account;
1207 mailval = send_message_smtp(mailac, to_list, fp);
1209 PrefsAccount tmp_ac;
1211 g_warning("Account not found.\n");
1213 memset(&tmp_ac, 0, sizeof(PrefsAccount));
1214 tmp_ac.address = from;
1215 tmp_ac.smtp_server = smtpserver;
1216 tmp_ac.smtpport = SMTP_PORT;
1217 mailval = send_message_smtp(&tmp_ac, to_list, fp);
1222 fseek(fp, filepos, SEEK_SET);
1223 if (newsgroup_list && (mailval == 0)) {
1228 /* write to temporary file */
1229 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1230 G_DIR_SEPARATOR, (gint)file);
1231 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
1232 FILE_OP_ERROR(tmp, "fopen");
1234 alertpanel_error(_("Could not create temporary file for news sending."));
1236 if (change_file_mode_rw(tmpfp, tmp) < 0) {
1237 FILE_OP_ERROR(tmp, "chmod");
1238 g_warning("can't change file mode\n");
1241 while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
1242 if (fputs(buf, tmpfp) == EOF) {
1243 FILE_OP_ERROR(tmp, "fputs");
1245 alertpanel_error(_("Error when writing temporary file for news sending."));
1251 debug_print("Sending message by news\n");
1253 folder = FOLDER(newsac->folder);
1255 newsval = news_post(folder, tmp);
1257 alertpanel_error(_("Error occurred while posting the message to %s ."),
1258 newsac->nntp_server);
1268 /* save message to outbox */
1269 if (mailval == 0 && newsval == 0 && savecopyfolder) {
1272 debug_print("saving sent message...\n");
1274 outbox = folder_find_item_from_identifier(savecopyfolder);
1276 outbox = folder_get_default_outbox();
1278 if (save_clear_text || tmp_enc_file == NULL) {
1279 procmsg_save_to_outbox(outbox, file, TRUE);
1281 procmsg_save_to_outbox(outbox, tmp_enc_file, FALSE);
1282 unlink(tmp_enc_file);
1287 if (replymessageid != NULL || fwdmessageid != NULL) {
1291 if (replymessageid != NULL)
1292 tokens = g_strsplit(replymessageid, "\x7f", 0);
1294 tokens = g_strsplit(fwdmessageid, "\x7f", 0);
1295 item = folder_find_item_from_identifier(tokens[0]);
1297 /* check if queued message has valid folder and message id */
1298 if (item != NULL && tokens[2] != NULL) {
1301 msginfo = folder_item_get_msginfo(item, atoi(tokens[1]));
1303 /* check if referring message exists and has a message id */
1304 if ((msginfo != NULL) &&
1305 (msginfo->msgid != NULL) &&
1306 (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1307 procmsg_msginfo_free(msginfo);
1311 if (msginfo == NULL) {
1312 msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]);
1315 if (msginfo != NULL) {
1316 if (replymessageid != NULL) {
1317 procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1318 procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1320 procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
1321 procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0);
1323 procmsg_msginfo_free(msginfo);
1331 slist_free_strings(to_list);
1332 g_slist_free(to_list);
1333 slist_free_strings(newsgroup_list);
1334 g_slist_free(newsgroup_list);
1335 g_free(savecopyfolder);
1336 g_free(replymessageid);
1337 g_free(fwdmessageid);
1338 g_free(privacy_system);
1339 g_free(encrypt_data);
1341 return (newsval != 0 ? newsval : mailval);
1344 static void update_folder_msg_counts(FolderItem *item, MsgInfo *msginfo, MsgPermFlags old_flags)
1346 MsgPermFlags new_flags = msginfo->flags.perm_flags;
1349 if (!(old_flags & MSG_NEW) && (new_flags & MSG_NEW)) {
1353 if ((old_flags & MSG_NEW) && !(new_flags & MSG_NEW)) {
1358 if (!(old_flags & MSG_UNREAD) && (new_flags & MSG_UNREAD)) {
1359 item->unread_msgs++;
1360 if (procmsg_msg_has_marked_parent(msginfo))
1361 item->unreadmarked_msgs++;
1364 if ((old_flags & MSG_UNREAD) && !(new_flags & MSG_UNREAD)) {
1365 item->unread_msgs--;
1366 if (procmsg_msg_has_marked_parent(msginfo))
1367 item->unreadmarked_msgs--;
1371 if (!(old_flags & MSG_MARKED) && (new_flags & MSG_MARKED)) {
1372 procmsg_update_unread_children(msginfo, TRUE);
1375 if ((old_flags & MSG_MARKED) && !(new_flags & MSG_MARKED)) {
1376 procmsg_update_unread_children(msginfo, FALSE);
1380 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1383 MsgInfoUpdate msginfo_update;
1384 MsgPermFlags perm_flags_new, perm_flags_old;
1385 MsgTmpFlags tmp_flags_old;
1387 g_return_if_fail(msginfo != NULL);
1388 item = msginfo->folder;
1389 g_return_if_fail(item != NULL);
1391 debug_print("Setting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1393 /* Perm Flags handling */
1394 perm_flags_old = msginfo->flags.perm_flags;
1395 perm_flags_new = msginfo->flags.perm_flags | perm_flags;
1396 if ((perm_flags & MSG_IGNORE_THREAD) || (perm_flags_old & MSG_IGNORE_THREAD)) {
1397 perm_flags_new &= ~(MSG_NEW | MSG_UNREAD);
1400 if (perm_flags_old != perm_flags_new) {
1401 folder_item_change_msg_flags(msginfo->folder, msginfo, perm_flags_new);
1403 update_folder_msg_counts(item, msginfo, perm_flags_old);
1407 /* Tmp flags handling */
1408 tmp_flags_old = msginfo->flags.tmp_flags;
1409 msginfo->flags.tmp_flags |= tmp_flags;
1411 /* update notification */
1412 if ((perm_flags_old != perm_flags_new) || (tmp_flags_old != msginfo->flags.tmp_flags)) {
1413 msginfo_update.msginfo = msginfo;
1414 msginfo_update.flags = MSGINFO_UPDATE_FLAGS;
1415 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1416 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1420 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1423 MsgInfoUpdate msginfo_update;
1424 MsgPermFlags perm_flags_new, perm_flags_old;
1425 MsgTmpFlags tmp_flags_old;
1427 g_return_if_fail(msginfo != NULL);
1428 item = msginfo->folder;
1429 g_return_if_fail(item != NULL);
1431 debug_print("Unsetting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1433 /* Perm Flags handling */
1434 perm_flags_old = msginfo->flags.perm_flags;
1435 perm_flags_new = msginfo->flags.perm_flags & ~perm_flags;
1437 if (perm_flags_old != perm_flags_new) {
1438 folder_item_change_msg_flags(msginfo->folder, msginfo, perm_flags_new);
1440 update_folder_msg_counts(item, msginfo, perm_flags_old);
1442 msginfo_update.msginfo = msginfo;
1443 msginfo_update.flags = MSGINFO_UPDATE_FLAGS;
1444 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1445 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1448 /* Tmp flags hanlding */
1449 tmp_flags_old = msginfo->flags.tmp_flags;
1450 msginfo->flags.tmp_flags &= ~tmp_flags;
1452 /* update notification */
1453 if ((perm_flags_old != perm_flags_new) || (tmp_flags_old != msginfo->flags.tmp_flags)) {
1454 msginfo_update.msginfo = msginfo;
1455 msginfo_update.flags = MSGINFO_UPDATE_FLAGS;
1456 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1457 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1462 *\brief check for flags (e.g. mark) in prior msgs of current thread
1464 *\param info Current message
1465 *\param perm_flags Flags to be checked
1466 *\param parentmsgs Hash of prior msgs to avoid loops
1468 *\return gboolean TRUE if perm_flags are found
1470 gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
1471 MsgPermFlags perm_flags, GHashTable *parentmsgs)
1475 g_return_val_if_fail(info != NULL, FALSE);
1477 if (info != NULL && info->folder != NULL && info->inreplyto != NULL) {
1478 tmp = folder_item_get_msginfo_by_msgid(info->folder,
1480 if (tmp && (tmp->flags.perm_flags & perm_flags)) {
1481 procmsg_msginfo_free(tmp);
1483 } else if (tmp != NULL) {
1486 if (g_hash_table_lookup(parentmsgs, info)) {
1487 debug_print("loop detected: %s%c%d\n",
1488 folder_item_get_path(info->folder),
1489 G_DIR_SEPARATOR, info->msgnum);
1492 g_hash_table_insert(parentmsgs, info, "1");
1493 result = procmsg_msg_has_flagged_parent_real(
1494 tmp, perm_flags, parentmsgs);
1496 procmsg_msginfo_free(tmp);
1506 *\brief Callback for cleaning up hash of parentmsgs
1508 gboolean parentmsgs_hash_remove(gpointer key,
1516 *\brief Set up list of parentmsgs
1517 * See procmsg_msg_has_flagged_parent_real()
1519 gboolean procmsg_msg_has_flagged_parent(MsgInfo *info, MsgPermFlags perm_flags)
1522 GHashTable *parentmsgs = g_hash_table_new(NULL, NULL);
1524 result = procmsg_msg_has_flagged_parent_real(info, perm_flags, parentmsgs);
1525 g_hash_table_foreach_remove(parentmsgs, parentmsgs_hash_remove, NULL);
1526 g_hash_table_destroy(parentmsgs);
1531 *\brief Check if msgs prior in thread are marked
1532 * See procmsg_msg_has_flagged_parent_real()
1534 gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
1536 return procmsg_msg_has_flagged_parent(info, MSG_MARKED);
1540 GSList *procmsg_find_children_func(MsgInfo *info,
1541 GSList *children, GSList *all)
1545 g_return_val_if_fail(info!=NULL, children);
1546 if (info->msgid == NULL)
1549 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1550 MsgInfo *tmp = (MsgInfo *)cur->data;
1551 if (tmp->inreplyto && !strcmp(tmp->inreplyto, info->msgid)) {
1552 /* Check if message is already in the list */
1553 if ((children == NULL) ||
1554 (g_slist_index(children, tmp) == -1)) {
1555 children = g_slist_prepend(children,
1556 procmsg_msginfo_new_ref(tmp));
1557 children = procmsg_find_children_func(tmp,
1566 GSList *procmsg_find_children (MsgInfo *info)
1571 g_return_val_if_fail(info!=NULL, NULL);
1572 all = folder_item_get_msg_list(info->folder);
1573 children = procmsg_find_children_func(info, NULL, all);
1574 if (children != NULL) {
1575 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1576 /* this will not free the used pointers
1577 created with procmsg_msginfo_new_ref */
1578 procmsg_msginfo_free((MsgInfo *)cur->data);
1586 void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
1588 GSList *children = procmsg_find_children(info);
1590 for (cur = children; cur != NULL; cur = g_slist_next(cur)) {
1591 MsgInfo *tmp = (MsgInfo *)cur->data;
1592 if(MSG_IS_UNREAD(tmp->flags) && !MSG_IS_IGNORE_THREAD(tmp->flags)) {
1594 info->folder->unreadmarked_msgs++;
1596 info->folder->unreadmarked_msgs--;
1597 folder_item_update(info->folder, F_ITEM_UPDATE_MSGCNT);
1599 procmsg_msginfo_free(tmp);
1601 g_slist_free(children);
1605 * Set the destination folder for a copy or move operation
1607 * \param msginfo The message which's destination folder is changed
1608 * \param to_folder The destination folder for the operation
1610 void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
1612 if(msginfo->to_folder != NULL) {
1613 msginfo->to_folder->op_count--;
1614 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1616 msginfo->to_folder = to_folder;
1617 if(to_folder != NULL) {
1618 to_folder->op_count++;
1619 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1624 * Apply filtering actions to the msginfo
1626 * \param msginfo The MsgInfo describing the message that should be filtered
1627 * \return TRUE if the message was moved and MsgInfo is now invalid,
1630 gboolean procmsg_msginfo_filter(MsgInfo *msginfo)
1632 MailFilteringData mail_filtering_data;
1634 mail_filtering_data.msginfo = msginfo;
1635 if (hooks_invoke(MAIL_FILTERING_HOOKLIST, &mail_filtering_data))
1638 /* filter if enabled in prefs or move to inbox if not */
1639 if((filtering_rules != NULL) &&
1640 filter_message_by_msginfo(filtering_rules, msginfo))