2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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.
30 #include "procheader.h"
33 #include "statusbar.h"
35 #include "prefs_common.h"
40 #include "alertpanel.h"
46 typedef struct _FlagInfo FlagInfo;
54 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
56 GHashTable *msg_table;
58 if (mlist == NULL) return NULL;
60 msg_table = g_hash_table_new(NULL, g_direct_equal);
61 procmsg_msg_hash_table_append(msg_table, mlist);
66 void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
71 if (msg_table == NULL || mlist == NULL) return;
73 for (cur = mlist; cur != NULL; cur = cur->next) {
74 msginfo = (MsgInfo *)cur->data;
76 g_hash_table_insert(msg_table,
77 GUINT_TO_POINTER(msginfo->msgnum),
82 GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
84 GHashTable *msg_table;
88 if (mlist == NULL) return NULL;
90 msg_table = g_hash_table_new(NULL, g_direct_equal);
92 for (cur = mlist; cur != NULL; cur = cur->next) {
93 msginfo = (MsgInfo *)cur->data;
94 g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
100 gint procmsg_get_last_num_in_msg_list(GSList *mlist)
106 for (cur = mlist; cur != NULL; cur = cur->next) {
107 msginfo = (MsgInfo *)cur->data;
108 if (msginfo && msginfo->msgnum > last)
109 last = msginfo->msgnum;
115 void procmsg_msg_list_free(GSList *mlist)
120 for (cur = mlist; cur != NULL; cur = cur->next) {
121 msginfo = (MsgInfo *)cur->data;
122 procmsg_msginfo_free(msginfo);
136 static gboolean procmsg_ignore_node(GNode *node, gpointer data)
138 MsgInfo *msginfo = (MsgInfo *)node->data;
140 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
141 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
146 /* return the reversed thread tree */
147 GNode *procmsg_get_thread_tree(GSList *mlist)
149 GNode *root, *parent, *node, *next;
150 GHashTable *msgid_table;
151 GHashTable *subject_table;
154 const gchar *subject;
155 GNode *found_subject;
157 root = g_node_new(NULL);
158 msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
159 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
161 for (; mlist != NULL; mlist = mlist->next) {
162 msginfo = (MsgInfo *)mlist->data;
165 if (msginfo->inreplyto) {
166 parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
167 if (parent == NULL) {
170 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
171 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
172 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
176 node = g_node_insert_data_before
177 (parent, parent == root ? parent->children : NULL,
179 if ((msgid = msginfo->msgid) &&
180 g_hash_table_lookup(msgid_table, msgid) == NULL)
181 g_hash_table_insert(msgid_table, (gchar *)msgid, node);
183 if (prefs_common.thread_by_subject) {
184 subject = msginfo->subject;
185 found_subject = subject_table_lookup(subject_table,
187 if (found_subject == NULL)
188 subject_table_insert(subject_table, (gchar *) subject,
191 /* replace if msg in table is older than current one
192 * can add here more stuff. */
193 if ( ((MsgInfo*)(found_subject->data))->date_t >
194 ((MsgInfo*)(node->data))->date_t ) {
195 subject_table_remove(subject_table, (gchar *) subject);
196 subject_table_insert(subject_table, (gchar *) subject, node);
202 /* complete the unfinished threads */
203 for (node = root->children; node != NULL; ) {
205 msginfo = (MsgInfo *)node->data;
207 if (msginfo->inreplyto)
208 parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
209 if (parent && parent != node) {
212 (parent, parent->children, node);
213 /* CLAWS: ignore thread */
214 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
215 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
221 /* CLAWS: now see if the first level (below root) still has some nodes that can be
222 * threaded by subject line. we need to handle this in a special way to prevent
223 * circular reference from a node that has already been threaded by IN-REPLY-TO
224 * but is also in the subject line hash table */
225 if (prefs_common.thread_by_subject) {
226 for (node = root->children; node != NULL; ) {
228 msginfo = (MsgInfo *) node->data;
230 if (subject_is_reply(msginfo->subject)) {
231 parent = subject_table_lookup(subject_table,
233 /* the node may already be threaded by IN-REPLY-TO,
234 so go up in the tree to find the parent node */
235 if (parent != NULL) {
236 if (g_node_is_ancestor(node, parent))
244 g_node_append(parent, node);
245 /* CLAWS: ignore thread */
246 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
247 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
255 g_hash_table_destroy(subject_table);
256 g_hash_table_destroy(msgid_table);
261 void procmsg_move_messages(GSList *mlist)
263 GSList *cur, *movelist = NULL;
265 FolderItem *dest = NULL;
269 folder_item_update_freeze();
271 for (cur = mlist; cur != NULL; cur = cur->next) {
272 msginfo = (MsgInfo *)cur->data;
274 dest = msginfo->to_folder;
275 movelist = g_slist_append(movelist, msginfo);
276 } else if (dest == msginfo->to_folder) {
277 movelist = g_slist_append(movelist, msginfo);
279 folder_item_move_msgs_with_dest(dest, movelist);
280 g_slist_free(movelist);
282 dest = msginfo->to_folder;
283 movelist = g_slist_append(movelist, msginfo);
285 procmsg_msginfo_set_to_folder(msginfo, NULL);
289 folder_item_move_msgs_with_dest(dest, movelist);
290 g_slist_free(movelist);
293 folder_item_update_thaw();
296 void procmsg_copy_messages(GSList *mlist)
298 GSList *cur, *copylist = NULL;
300 FolderItem *dest = NULL;
304 folder_item_update_freeze();
306 for (cur = mlist; cur != NULL; cur = cur->next) {
307 msginfo = (MsgInfo *)cur->data;
309 dest = msginfo->to_folder;
310 copylist = g_slist_append(copylist, msginfo);
311 } else if (dest == msginfo->to_folder) {
312 copylist = g_slist_append(copylist, msginfo);
314 folder_item_copy_msgs_with_dest(dest, copylist);
315 g_slist_free(copylist);
317 dest = msginfo->to_folder;
318 copylist = g_slist_append(copylist, msginfo);
320 procmsg_msginfo_set_to_folder(msginfo, NULL);
324 folder_item_copy_msgs_with_dest(dest, copylist);
325 g_slist_free(copylist);
328 folder_item_update_thaw();
331 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
335 g_return_val_if_fail(msginfo != NULL, NULL);
337 if (msginfo->plaintext_file)
338 file = g_strdup(msginfo->plaintext_file);
340 path = folder_item_get_path(msginfo->folder);
341 file = g_strconcat(path, G_DIR_SEPARATOR_S,
342 itos(msginfo->msgnum), NULL);
349 gchar *procmsg_get_message_file(MsgInfo *msginfo)
351 gchar *filename = NULL;
353 g_return_val_if_fail(msginfo != NULL, NULL);
355 filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
357 g_warning("can't fetch message %d\n", msginfo->msgnum);
362 FILE *procmsg_open_message(MsgInfo *msginfo)
367 g_return_val_if_fail(msginfo != NULL, NULL);
369 file = procmsg_get_message_file_path(msginfo);
370 g_return_val_if_fail(file != NULL, NULL);
372 if (!is_file_exist(file)) {
374 file = procmsg_get_message_file(msginfo);
375 g_return_val_if_fail(file != NULL, NULL);
378 if ((fp = fopen(file, "rb")) == NULL) {
379 FILE_OP_ERROR(file, "fopen");
386 if (MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags)) {
389 while (fgets(buf, sizeof(buf), fp) != NULL)
390 if (buf[0] == '\r' || buf[0] == '\n') break;
397 FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
402 g_return_val_if_fail(msginfo != NULL, NULL);
404 if (mimeinfo) *mimeinfo = NULL;
406 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
408 mimeinfo_ = procmime_scan_mime_header(fp);
414 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
415 rfc2015_is_encrypted(mimeinfo_)) {
416 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
419 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
420 (!msginfo->plaintext_file || msginfo->decryption_failed)) {
421 rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
422 if (msginfo->plaintext_file &&
423 !msginfo->decryption_failed) {
425 procmime_mimeinfo_free_all(mimeinfo_);
426 if ((fp = procmsg_open_message(msginfo)) == NULL)
428 mimeinfo_ = procmime_scan_mime_header(fp);
436 if (mimeinfo) *mimeinfo = mimeinfo_;
441 gboolean procmsg_msg_exist(MsgInfo *msginfo)
446 if (!msginfo) return FALSE;
448 path = folder_item_get_path(msginfo->folder);
450 ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
456 void procmsg_empty_trash(void)
461 for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
462 trash = FOLDER(cur->data)->trash;
463 if (trash && trash->total > 0)
464 folder_item_remove_all_msg(trash);
468 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
474 queue = folder_get_default_queue();
475 g_return_val_if_fail(queue != NULL, -1);
477 folder_item_scan(queue);
478 list = folder_item_get_msg_list(queue);
481 for (elem = list; elem != NULL; elem = elem->next) {
485 msginfo = (MsgInfo *)(elem->data);
487 file = folder_item_fetch_msg(queue, msginfo->msgnum);
489 if (procmsg_send_message_queue(file) < 0) {
490 g_warning("Sending queued message %d failed.\n", msginfo->msgnum);
494 * We save in procmsg_send_message_queue because
495 * we need the destination folder from the queue
499 procmsg_save_to_outbox
500 (queue->folder->outbox,
503 folder_item_remove_msg(queue, msginfo->msgnum);
507 procmsg_msginfo_free(msginfo);
513 gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
518 if ((fp = fopen(in, "rb")) == NULL) {
519 FILE_OP_ERROR(in, "fopen");
522 if ((outfp = fopen(out, "wb")) == NULL) {
523 FILE_OP_ERROR(out, "fopen");
527 while (fgets(buf, sizeof(buf), fp) != NULL)
528 if (buf[0] == '\r' || buf[0] == '\n') break;
529 while (fgets(buf, sizeof(buf), fp) != NULL)
536 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
542 debug_print("saving sent message...\n");
545 outbox = folder_get_default_outbox();
546 g_return_val_if_fail(outbox != NULL, -1);
548 /* remove queueing headers */
550 gchar tmp[MAXPATHLEN + 1];
552 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
553 get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
555 if (procmsg_remove_special_headers(file, tmp) !=0)
558 folder_item_scan(outbox);
559 if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
560 g_warning("can't save message\n");
565 folder_item_scan(outbox);
566 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
567 g_warning("can't save message\n");
572 msginfo = folder_item_get_msginfo(outbox, num);
573 if (msginfo != NULL) {
574 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
575 procmsg_msginfo_free(msginfo);
577 folder_item_update(outbox, TRUE);
582 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
584 static const gchar *def_cmd = "lpr %s";
591 g_return_if_fail(msginfo);
593 if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
594 g_warning("Can't get text part\n");
598 prtmp = g_strdup_printf("%s%cprinttmp.%08x",
599 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
601 if ((prfp = fopen(prtmp, "wb")) == NULL) {
602 FILE_OP_ERROR(prtmp, "fopen");
608 if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
609 if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
610 if (msginfo->to) fprintf(prfp, "To: %s\n", msginfo->to);
611 if (msginfo->cc) fprintf(prfp, "Cc: %s\n", msginfo->cc);
612 if (msginfo->newsgroups)
613 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
614 if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
617 while (fgets(buf, sizeof(buf), tmpfp) != NULL)
623 if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
625 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
628 g_warning("Print command line is invalid: `%s'\n",
630 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
636 if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
640 MsgInfo *procmsg_msginfo_new_ref(MsgInfo *msginfo)
647 MsgInfo *procmsg_msginfo_new()
651 newmsginfo = g_new0(MsgInfo, 1);
652 newmsginfo->refcnt = 1;
657 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
661 if (msginfo == NULL) return NULL;
663 newmsginfo = g_new0(MsgInfo, 1);
665 newmsginfo->refcnt = 1;
667 #define MEMBCOPY(mmb) newmsginfo->mmb = msginfo->mmb
668 #define MEMBDUP(mmb) newmsginfo->mmb = msginfo->mmb ? \
669 g_strdup(msginfo->mmb) : NULL
693 MEMBDUP(dispositionnotificationto);
694 MEMBDUP(returnreceiptto);
698 MEMBCOPY(threadscore);
703 MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
705 MsgInfo *full_msginfo;
708 if (msginfo == NULL) return NULL;
710 file = procmsg_get_message_file(msginfo);
712 g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
716 full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE, FALSE);
718 if (!full_msginfo) return NULL;
720 full_msginfo->msgnum = msginfo->msgnum;
721 full_msginfo->size = msginfo->size;
722 full_msginfo->mtime = msginfo->mtime;
723 full_msginfo->folder = msginfo->folder;
725 full_msginfo->plaintext_file = g_strdup(msginfo->plaintext_file);
726 full_msginfo->decryption_failed = msginfo->decryption_failed;
728 procmsg_msginfo_set_to_folder(full_msginfo, msginfo->to_folder);
733 void procmsg_msginfo_free(MsgInfo *msginfo)
735 if (msginfo == NULL) return;
738 if (msginfo->refcnt > 0)
741 debug_print("freeing msginfo %d is %s\n", msginfo->msgnum, msginfo->folder ? msginfo->folder->path : "(nil)");
743 if (msginfo->to_folder) {
744 msginfo->to_folder->op_count--;
745 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
748 g_free(msginfo->fromspace);
749 g_free(msginfo->references);
750 g_free(msginfo->returnreceiptto);
751 g_free(msginfo->dispositionnotificationto);
752 g_free(msginfo->xface);
754 g_free(msginfo->fromname);
756 g_free(msginfo->date);
757 g_free(msginfo->from);
760 g_free(msginfo->newsgroups);
761 g_free(msginfo->subject);
762 g_free(msginfo->msgid);
763 g_free(msginfo->inreplyto);
764 g_free(msginfo->xref);
769 guint procmsg_msginfo_memusage(MsgInfo *msginfo)
773 memusage += sizeof(MsgInfo);
774 if (msginfo->fromname)
775 memusage += strlen(msginfo->fromname);
777 memusage += strlen(msginfo->date);
779 memusage += strlen(msginfo->from);
781 memusage += strlen(msginfo->to);
783 memusage += strlen(msginfo->cc);
784 if (msginfo->newsgroups)
785 memusage += strlen(msginfo->newsgroups);
786 if (msginfo->subject)
787 memusage += strlen(msginfo->subject);
789 memusage += strlen(msginfo->msgid);
790 if (msginfo->inreplyto)
791 memusage += strlen(msginfo->inreplyto);
793 memusage += strlen(msginfo->xface);
794 if (msginfo->dispositionnotificationto)
795 memusage += strlen(msginfo->dispositionnotificationto);
796 if (msginfo->returnreceiptto)
797 memusage += strlen(msginfo->returnreceiptto);
798 if (msginfo->references)
799 memusage += strlen(msginfo->references);
800 if (msginfo->fromspace)
801 memusage += strlen(msginfo->fromspace);
806 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
808 const MsgInfo *msginfo1 = a;
809 const MsgInfo *msginfo2 = b;
816 return msginfo1->msgnum - msginfo2->msgnum;
825 Q_MAIL_ACCOUNT_ID = 4,
826 Q_NEWS_ACCOUNT_ID = 5,
827 Q_SAVE_COPY_FOLDER = 6,
828 Q_REPLY_MESSAGE_ID = 7,
832 gint procmsg_send_message_queue(const gchar *file)
834 static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
835 {"SSV:", NULL, FALSE},
837 {"NG:", NULL, FALSE},
838 {"MAID:", NULL, FALSE},
839 {"NAID:", NULL, FALSE},
840 {"SCF:", NULL, FALSE},
841 {"RMID:", NULL, FALSE},
842 {"FMID:", NULL, FALSE},
843 {NULL, NULL, FALSE}};
846 gint mailval = 0, newsval = 0;
848 gchar *smtpserver = NULL;
849 GSList *to_list = NULL;
850 GSList *newsgroup_list = NULL;
851 gchar *savecopyfolder = NULL;
852 gchar *replymessageid = NULL;
853 gchar *fwdmessageid = NULL;
856 PrefsAccount *mailac = NULL, *newsac = NULL;
859 g_return_val_if_fail(file != NULL, -1);
861 if ((fp = fopen(file, "rb")) == NULL) {
862 FILE_OP_ERROR(file, "fopen");
866 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
868 gchar *p = buf + strlen(qentry[hnum].name);
872 if (!from) from = g_strdup(p);
875 if (!smtpserver) smtpserver = g_strdup(p);
878 to_list = address_list_append(to_list, p);
881 newsgroup_list = newsgroup_list_append(newsgroup_list, p);
883 case Q_MAIL_ACCOUNT_ID:
884 mailac = account_find_from_id(atoi(p));
886 case Q_NEWS_ACCOUNT_ID:
887 newsac = account_find_from_id(atoi(p));
889 case Q_SAVE_COPY_FOLDER:
890 if (!savecopyfolder) savecopyfolder = g_strdup(p);
892 case Q_REPLY_MESSAGE_ID:
893 if (!replymessageid) replymessageid = g_strdup(p);
895 case Q_FWD_MESSAGE_ID:
896 if (!fwdmessageid) fwdmessageid = g_strdup(p);
903 debug_print("Sending message by mail\n");
905 g_warning("Queued message header is broken.\n");
907 } else if (mailac && mailac->use_mail_command &&
908 mailac->mail_command && (* mailac->mail_command)) {
909 mailval = send_message_local(mailac->mail_command, fp);
911 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
912 mailval = send_message_local(prefs_common.extsend_cmd, fp);
916 mailac = account_find_from_smtp_server(from, smtpserver);
918 g_warning("Account not found. "
919 "Using current account...\n");
920 mailac = cur_account;
925 mailval = send_message_smtp(mailac, to_list, fp);
929 g_warning("Account not found.\n");
931 memset(&tmp_ac, 0, sizeof(PrefsAccount));
932 tmp_ac.address = from;
933 tmp_ac.smtp_server = smtpserver;
934 tmp_ac.smtpport = SMTP_PORT;
935 mailval = send_message_smtp(&tmp_ac, to_list, fp);
940 alertpanel_error_log(
941 _("Error occurred while sending the message to `%s'."),
942 mailac ? mailac->smtp_server : smtpserver);
944 alertpanel_error_log(
945 _("Error occurred while sending the message with command `%s'."),
946 (mailac && mailac->use_mail_command &&
947 mailac->mail_command && (*mailac->mail_command)) ?
948 mailac->mail_command : prefs_common.extsend_cmd);
952 fseek(fp, filepos, SEEK_SET);
953 if (newsgroup_list && (newsval == 0)) {
958 /* write to temporary file */
959 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
960 G_DIR_SEPARATOR, (gint)file);
961 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
962 FILE_OP_ERROR(tmp, "fopen");
964 alertpanel_error(_("Could not create temporary file for news sending."));
966 if (change_file_mode_rw(tmpfp, tmp) < 0) {
967 FILE_OP_ERROR(tmp, "chmod");
968 g_warning("can't change file mode\n");
971 while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
972 if (fputs(buf, tmpfp) == EOF) {
973 FILE_OP_ERROR(tmp, "fputs");
975 alertpanel_error(_("Error when writing temporary file for news sending."));
981 debug_print("Sending message by news\n");
983 folder = FOLDER(newsac->folder);
985 newsval = news_post(folder, tmp);
987 alertpanel_error(_("Error occurred while posting the message to %s ."),
988 newsac->nntp_server);
996 slist_free_strings(to_list);
997 g_slist_free(to_list);
998 slist_free_strings(newsgroup_list);
999 g_slist_free(newsgroup_list);
1004 /* save message to outbox */
1005 if (mailval == 0 && newsval == 0 && savecopyfolder) {
1008 debug_print("saving sent message...\n");
1010 outbox = folder_find_item_from_identifier(savecopyfolder);
1012 outbox = folder_get_default_outbox();
1014 procmsg_save_to_outbox(outbox, file, TRUE);
1017 if (replymessageid != NULL || fwdmessageid != NULL) {
1021 if (replymessageid != NULL)
1022 tokens = g_strsplit(replymessageid, "\x7f", 0);
1024 tokens = g_strsplit(fwdmessageid, "\x7f", 0);
1025 item = folder_find_item_from_identifier(tokens[0]);
1029 msginfo = folder_item_get_msginfo(item, atoi(tokens[1]));
1030 if ((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1031 procmsg_msginfo_free(msginfo);
1035 if (msginfo == NULL) {
1036 msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]);
1039 if (msginfo != NULL) {
1040 if (replymessageid != NULL) {
1041 procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1042 procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1045 procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
1046 procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0);
1048 procmsg_msginfo_free(msginfo);
1054 g_free(savecopyfolder);
1055 g_free(replymessageid);
1056 g_free(fwdmessageid);
1058 return (newsval != 0 ? newsval : mailval);
1061 #define CHANGE_FLAGS(msginfo) \
1063 if (msginfo->folder->folder->class->change_flags != NULL) \
1064 msginfo->folder->folder->class->change_flags(msginfo->folder->folder, \
1069 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1072 MsgInfoUpdate msginfo_update;
1074 g_return_if_fail(msginfo != NULL);
1075 item = msginfo->folder;
1076 g_return_if_fail(item != NULL);
1078 debug_print("Setting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1080 /* if new flag is set */
1081 if ((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
1082 !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1086 /* if unread flag is set */
1087 if ((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
1088 !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1092 if (!MSG_IS_UNREAD(msginfo->flags) &&(perm_flags & MSG_UNREAD)
1093 && procmsg_msg_has_marked_parent(msginfo)) {
1094 item->unreadmarked++;
1097 if (!MSG_IS_MARKED(msginfo->flags) && (perm_flags & MSG_MARKED)) {
1098 procmsg_update_unread_children(msginfo, TRUE);
1102 /* if ignore thread flag is set */
1103 if ((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1104 if (MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
1107 if (MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
1110 if ((perm_flags & MSG_UNREAD) || (MSG_IS_UNREAD(msginfo->flags)
1111 && procmsg_msg_has_marked_parent(msginfo))) {
1112 item->unreadmarked--;
1114 if ((perm_flags & MSG_MARKED) || (MSG_IS_MARKED(msginfo->flags)
1115 && !MSG_IS_IGNORE_THREAD(msginfo->flags))) {
1116 procmsg_update_unread_children(msginfo, FALSE);
1121 if (FOLDER_TYPE(msginfo->folder->folder) == F_IMAP)
1122 imap_msg_set_perm_flags(msginfo, perm_flags);
1124 msginfo->flags.perm_flags |= perm_flags;
1125 msginfo->flags.tmp_flags |= tmp_flags;
1127 msginfo_update.msginfo = msginfo;
1128 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1129 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1131 CHANGE_FLAGS(msginfo);
1134 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1137 MsgInfoUpdate msginfo_update;
1139 g_return_if_fail(msginfo != NULL);
1140 item = msginfo->folder;
1141 g_return_if_fail(item != NULL);
1143 debug_print("Unsetting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1145 /* if new flag is unset */
1146 if ((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
1147 !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1151 /* if unread flag is unset */
1152 if ((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
1153 !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1157 if (MSG_IS_UNREAD(msginfo->flags) && (perm_flags & MSG_UNREAD)
1158 && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1159 && procmsg_msg_has_marked_parent(msginfo)) {
1160 item->unreadmarked--;
1163 if (MSG_IS_MARKED(msginfo->flags) && (perm_flags & MSG_MARKED)
1164 && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1165 procmsg_update_unread_children(msginfo, FALSE);
1168 /* if ignore thread flag is unset */
1169 if ((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1170 if (MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
1173 if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
1176 if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)
1177 && procmsg_msg_has_marked_parent(msginfo)) {
1178 item->unreadmarked++;
1180 if (MSG_IS_MARKED(msginfo->flags) && !(perm_flags & MSG_MARKED)) {
1181 procmsg_update_unread_children(msginfo, TRUE);
1186 if (FOLDER_TYPE(msginfo->folder->folder) == F_IMAP)
1187 imap_msg_unset_perm_flags(msginfo, perm_flags);
1189 msginfo->flags.perm_flags &= ~perm_flags;
1190 msginfo->flags.tmp_flags &= ~tmp_flags;
1192 msginfo_update.msginfo = msginfo;
1193 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1194 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1196 CHANGE_FLAGS(msginfo);
1200 *\brief check for flags (e.g. mark) in prior msgs of current thread
1202 *\param info Current message
1203 *\param perm_flags Flags to be checked
1204 *\param parentmsgs Hash of prior msgs to avoid loops
1206 *\return gboolean TRUE if perm_flags are found
1208 gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
1209 MsgPermFlags perm_flags, GHashTable *parentmsgs)
1213 g_return_val_if_fail(info != NULL, FALSE);
1215 if (info != NULL && info->folder != NULL && info->inreplyto != NULL) {
1216 tmp = folder_item_get_msginfo_by_msgid(info->folder,
1218 if (tmp && (tmp->flags.perm_flags & perm_flags)) {
1219 procmsg_msginfo_free(tmp);
1221 } else if (tmp != NULL) {
1224 if (g_hash_table_lookup(parentmsgs, info)) {
1225 debug_print("loop detected: %s%c%d\n",
1226 folder_item_get_path(info->folder),
1227 G_DIR_SEPARATOR, info->msgnum);
1230 g_hash_table_insert(parentmsgs, info, "1");
1231 result = procmsg_msg_has_flagged_parent_real(
1232 tmp, perm_flags, parentmsgs);
1234 procmsg_msginfo_free(tmp);
1244 *\brief Callback for cleaning up hash of parentmsgs
1246 gboolean parentmsgs_hash_remove(gpointer key,
1254 *\brief Set up list of parentmsgs
1255 * See procmsg_msg_has_flagged_parent_real()
1257 gboolean procmsg_msg_has_flagged_parent(MsgInfo *info, MsgPermFlags perm_flags)
1260 GHashTable *parentmsgs = g_hash_table_new(NULL, NULL);
1262 result = procmsg_msg_has_flagged_parent_real(info, perm_flags, parentmsgs);
1263 g_hash_table_foreach_remove(parentmsgs, parentmsgs_hash_remove, NULL);
1264 g_hash_table_destroy(parentmsgs);
1269 *\brief Check if msgs prior in thread are marked
1270 * See procmsg_msg_has_flagged_parent_real()
1272 gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
1274 return procmsg_msg_has_flagged_parent(info, MSG_MARKED);
1278 GSList *procmsg_find_children_func(MsgInfo *info,
1279 GSList *children, GSList *all)
1283 g_return_val_if_fail(info!=NULL, children);
1284 if (info->msgid == NULL)
1287 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1288 MsgInfo *tmp = (MsgInfo *)cur->data;
1289 if (tmp->inreplyto && !strcmp(tmp->inreplyto, info->msgid)) {
1290 /* Check if message is already in the list */
1291 if ((children == NULL) ||
1292 (g_slist_index(children, tmp) == -1)) {
1293 children = g_slist_prepend(children,
1294 procmsg_msginfo_new_ref(tmp));
1295 children = procmsg_find_children_func(tmp,
1304 GSList *procmsg_find_children (MsgInfo *info)
1309 g_return_val_if_fail(info!=NULL, NULL);
1310 all = folder_item_get_msg_list(info->folder);
1311 children = procmsg_find_children_func(info, NULL, all);
1312 if (children != NULL) {
1313 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1314 /* this will not free the used pointers
1315 created with procmsg_msginfo_new_ref */
1316 procmsg_msginfo_free((MsgInfo *)cur->data);
1324 void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
1326 GSList *children = procmsg_find_children(info);
1328 for (cur = children; cur != NULL; cur = g_slist_next(cur)) {
1329 MsgInfo *tmp = (MsgInfo *)cur->data;
1330 if(MSG_IS_UNREAD(tmp->flags) && !MSG_IS_IGNORE_THREAD(tmp->flags)) {
1332 info->folder->unreadmarked++;
1334 info->folder->unreadmarked--;
1335 folder_item_update(info->folder, F_ITEM_UPDATE_MSGCNT);
1337 procmsg_msginfo_free(tmp);
1339 g_slist_free(children);
1343 * Set the destination folder for a copy or move operation
1345 * \param msginfo The message which's destination folder is changed
1346 * \param to_folder The destination folder for the operation
1348 void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
1350 if(msginfo->to_folder != NULL) {
1351 msginfo->to_folder->op_count--;
1352 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1354 msginfo->to_folder = to_folder;
1355 if(to_folder != NULL) {
1356 to_folder->op_count++;
1357 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);