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_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
145 /* return the reversed thread tree */
146 GNode *procmsg_get_thread_tree(GSList *mlist)
148 GNode *root, *parent, *node, *next;
149 GHashTable *msgid_table;
150 GHashTable *subject_table;
153 const gchar *subject;
154 GNode *found_subject;
156 root = g_node_new(NULL);
157 msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
158 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
160 for (; mlist != NULL; mlist = mlist->next) {
161 msginfo = (MsgInfo *)mlist->data;
164 if (msginfo->inreplyto) {
165 parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
166 if (parent == NULL) {
169 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
170 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
174 node = g_node_insert_data_before
175 (parent, parent == root ? parent->children : NULL,
177 if ((msgid = msginfo->msgid) &&
178 g_hash_table_lookup(msgid_table, msgid) == NULL)
179 g_hash_table_insert(msgid_table, (gchar *)msgid, node);
181 if (prefs_common.thread_by_subject) {
182 subject = msginfo->subject;
183 found_subject = subject_table_lookup(subject_table,
185 if (found_subject == NULL)
186 subject_table_insert(subject_table, (gchar *) subject,
189 /* replace if msg in table is older than current one
190 * can add here more stuff. */
191 if ( ((MsgInfo*)(found_subject->data))->date_t >
192 ((MsgInfo*)(node->data))->date_t ) {
193 subject_table_remove(subject_table, (gchar *) subject);
194 subject_table_insert(subject_table, (gchar *) subject, node);
200 /* complete the unfinished threads */
201 for (node = root->children; node != NULL; ) {
203 msginfo = (MsgInfo *)node->data;
205 if (msginfo->inreplyto)
206 parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
207 if (parent && parent != node) {
210 (parent, parent->children, node);
211 /* CLAWS: ignore thread */
212 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
213 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
219 /* CLAWS: now see if the first level (below root) still has some nodes that can be
220 * threaded by subject line. we need to handle this in a special way to prevent
221 * circular reference from a node that has already been threaded by IN-REPLY-TO
222 * but is also in the subject line hash table */
223 if (prefs_common.thread_by_subject) {
224 for (node = root->children; node != NULL; ) {
226 msginfo = (MsgInfo *) node->data;
228 if (subject_is_reply(msginfo->subject)) {
229 parent = subject_table_lookup(subject_table,
231 /* the node may already be threaded by IN-REPLY-TO,
232 so go up in the tree to find the parent node */
233 if (parent != NULL) {
234 if (g_node_is_ancestor(node, parent))
242 g_node_append(parent, node);
243 /* CLAWS: ignore thread */
244 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
245 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
253 g_hash_table_destroy(subject_table);
254 g_hash_table_destroy(msgid_table);
259 void procmsg_move_messages(GSList *mlist)
261 GSList *cur, *movelist = NULL;
263 FolderItem *dest = NULL;
267 folder_item_update_freeze();
269 for (cur = mlist; cur != NULL; cur = cur->next) {
270 msginfo = (MsgInfo *)cur->data;
272 dest = msginfo->to_folder;
273 movelist = g_slist_append(movelist, msginfo);
274 } else if (dest == msginfo->to_folder) {
275 movelist = g_slist_append(movelist, msginfo);
277 folder_item_move_msgs_with_dest(dest, movelist);
278 g_slist_free(movelist);
280 dest = msginfo->to_folder;
281 movelist = g_slist_append(movelist, msginfo);
283 procmsg_msginfo_set_to_folder(msginfo, NULL);
287 folder_item_move_msgs_with_dest(dest, movelist);
288 g_slist_free(movelist);
291 folder_item_update_thaw();
294 void procmsg_copy_messages(GSList *mlist)
296 GSList *cur, *copylist = NULL;
298 FolderItem *dest = NULL;
302 folder_item_update_freeze();
304 for (cur = mlist; cur != NULL; cur = cur->next) {
305 msginfo = (MsgInfo *)cur->data;
307 dest = msginfo->to_folder;
308 copylist = g_slist_append(copylist, msginfo);
309 } else if (dest == msginfo->to_folder) {
310 copylist = g_slist_append(copylist, msginfo);
312 folder_item_copy_msgs_with_dest(dest, copylist);
313 g_slist_free(copylist);
315 dest = msginfo->to_folder;
316 copylist = g_slist_append(copylist, msginfo);
318 procmsg_msginfo_set_to_folder(msginfo, NULL);
322 folder_item_copy_msgs_with_dest(dest, copylist);
323 g_slist_free(copylist);
326 folder_item_update_thaw();
329 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
333 g_return_val_if_fail(msginfo != NULL, NULL);
335 if (msginfo->plaintext_file)
336 file = g_strdup(msginfo->plaintext_file);
338 path = folder_item_get_path(msginfo->folder);
339 file = g_strconcat(path, G_DIR_SEPARATOR_S,
340 itos(msginfo->msgnum), NULL);
347 gchar *procmsg_get_message_file(MsgInfo *msginfo)
349 gchar *filename = NULL;
351 g_return_val_if_fail(msginfo != NULL, NULL);
353 filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
355 g_warning("can't fetch message %d\n", msginfo->msgnum);
360 FILE *procmsg_open_message(MsgInfo *msginfo)
365 g_return_val_if_fail(msginfo != NULL, NULL);
367 file = procmsg_get_message_file_path(msginfo);
368 g_return_val_if_fail(file != NULL, NULL);
370 if (!is_file_exist(file)) {
372 file = procmsg_get_message_file(msginfo);
373 g_return_val_if_fail(file != NULL, NULL);
376 if ((fp = fopen(file, "rb")) == NULL) {
377 FILE_OP_ERROR(file, "fopen");
384 if (MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags)) {
387 while (fgets(buf, sizeof(buf), fp) != NULL)
388 if (buf[0] == '\r' || buf[0] == '\n') break;
395 FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
400 g_return_val_if_fail(msginfo != NULL, NULL);
402 if (mimeinfo) *mimeinfo = NULL;
404 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
406 mimeinfo_ = procmime_scan_mime_header(fp);
412 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
413 rfc2015_is_encrypted(mimeinfo_)) {
414 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
417 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
418 (!msginfo->plaintext_file || msginfo->decryption_failed)) {
419 rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
420 if (msginfo->plaintext_file &&
421 !msginfo->decryption_failed) {
423 procmime_mimeinfo_free_all(mimeinfo_);
424 if ((fp = procmsg_open_message(msginfo)) == NULL)
426 mimeinfo_ = procmime_scan_mime_header(fp);
434 if (mimeinfo) *mimeinfo = mimeinfo_;
439 gboolean procmsg_msg_exist(MsgInfo *msginfo)
444 if (!msginfo) return FALSE;
446 path = folder_item_get_path(msginfo->folder);
448 ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
454 void procmsg_empty_trash(void)
459 for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
460 trash = FOLDER(cur->data)->trash;
461 if (trash && trash->total > 0)
462 folder_item_remove_all_msg(trash);
466 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
472 queue = folder_get_default_queue();
473 g_return_val_if_fail(queue != NULL, -1);
475 folder_item_scan(queue);
476 list = folder_item_get_msg_list(queue);
479 for (elem = list; elem != NULL; elem = elem->next) {
483 msginfo = (MsgInfo *)(elem->data);
485 file = folder_item_fetch_msg(queue, msginfo->msgnum);
487 if (procmsg_send_message_queue(file) < 0) {
488 g_warning("Sending queued message %d failed.\n", msginfo->msgnum);
492 * We save in procmsg_send_message_queue because
493 * we need the destination folder from the queue
497 procmsg_save_to_outbox
498 (queue->folder->outbox,
501 folder_item_remove_msg(queue, msginfo->msgnum);
505 procmsg_msginfo_free(msginfo);
511 gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
516 if ((fp = fopen(in, "rb")) == NULL) {
517 FILE_OP_ERROR(in, "fopen");
520 if ((outfp = fopen(out, "wb")) == NULL) {
521 FILE_OP_ERROR(out, "fopen");
525 while (fgets(buf, sizeof(buf), fp) != NULL)
526 if (buf[0] == '\r' || buf[0] == '\n') break;
527 while (fgets(buf, sizeof(buf), fp) != NULL)
534 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
540 debug_print("saving sent message...\n");
543 outbox = folder_get_default_outbox();
544 g_return_val_if_fail(outbox != NULL, -1);
546 /* remove queueing headers */
548 gchar tmp[MAXPATHLEN + 1];
550 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
551 get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
553 if (procmsg_remove_special_headers(file, tmp) !=0)
556 folder_item_scan(outbox);
557 if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
558 g_warning("can't save message\n");
563 folder_item_scan(outbox);
564 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
565 g_warning("can't save message\n");
570 msginfo = folder_item_get_msginfo(outbox, num);
571 if (msginfo != NULL) {
572 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
573 procmsg_msginfo_free(msginfo);
575 folder_item_update(outbox, TRUE);
580 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
582 static const gchar *def_cmd = "lpr %s";
589 g_return_if_fail(msginfo);
591 if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
592 g_warning("Can't get text part\n");
596 prtmp = g_strdup_printf("%s%cprinttmp.%08x",
597 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
599 if ((prfp = fopen(prtmp, "wb")) == NULL) {
600 FILE_OP_ERROR(prtmp, "fopen");
606 if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
607 if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
608 if (msginfo->to) fprintf(prfp, "To: %s\n", msginfo->to);
609 if (msginfo->cc) fprintf(prfp, "Cc: %s\n", msginfo->cc);
610 if (msginfo->newsgroups)
611 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
612 if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
615 while (fgets(buf, sizeof(buf), tmpfp) != NULL)
621 if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
623 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
626 g_warning("Print command line is invalid: `%s'\n",
628 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
634 if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
638 MsgInfo *procmsg_msginfo_new_ref(MsgInfo *msginfo)
645 MsgInfo *procmsg_msginfo_new()
649 newmsginfo = g_new0(MsgInfo, 1);
650 newmsginfo->refcnt = 1;
655 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
659 if (msginfo == NULL) return NULL;
661 newmsginfo = g_new0(MsgInfo, 1);
663 newmsginfo->refcnt = 1;
665 #define MEMBCOPY(mmb) newmsginfo->mmb = msginfo->mmb
666 #define MEMBDUP(mmb) newmsginfo->mmb = msginfo->mmb ? \
667 g_strdup(msginfo->mmb) : NULL
691 MEMBDUP(dispositionnotificationto);
692 MEMBDUP(returnreceiptto);
696 MEMBCOPY(threadscore);
701 MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
703 MsgInfo *full_msginfo;
706 if (msginfo == NULL) return NULL;
708 file = procmsg_get_message_file(msginfo);
710 g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
714 full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE, FALSE);
716 if (!full_msginfo) return NULL;
718 full_msginfo->msgnum = msginfo->msgnum;
719 full_msginfo->size = msginfo->size;
720 full_msginfo->mtime = msginfo->mtime;
721 full_msginfo->folder = msginfo->folder;
723 full_msginfo->plaintext_file = g_strdup(msginfo->plaintext_file);
724 full_msginfo->decryption_failed = msginfo->decryption_failed;
726 procmsg_msginfo_set_to_folder(full_msginfo, msginfo->to_folder);
731 void procmsg_msginfo_free(MsgInfo *msginfo)
733 if (msginfo == NULL) return;
736 if (msginfo->refcnt > 0)
739 debug_print("freeing msginfo %d is %s\n", msginfo->msgnum, msginfo->folder ? msginfo->folder->path : "(nil)");
741 if (msginfo->to_folder) {
742 msginfo->to_folder->op_count--;
743 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
746 g_free(msginfo->fromspace);
747 g_free(msginfo->references);
748 g_free(msginfo->returnreceiptto);
749 g_free(msginfo->dispositionnotificationto);
750 g_free(msginfo->xface);
752 g_free(msginfo->fromname);
754 g_free(msginfo->date);
755 g_free(msginfo->from);
758 g_free(msginfo->newsgroups);
759 g_free(msginfo->subject);
760 g_free(msginfo->msgid);
761 g_free(msginfo->inreplyto);
762 g_free(msginfo->xref);
767 guint procmsg_msginfo_memusage(MsgInfo *msginfo)
771 memusage += sizeof(MsgInfo);
772 if (msginfo->fromname)
773 memusage += strlen(msginfo->fromname);
775 memusage += strlen(msginfo->date);
777 memusage += strlen(msginfo->from);
779 memusage += strlen(msginfo->to);
781 memusage += strlen(msginfo->cc);
782 if (msginfo->newsgroups)
783 memusage += strlen(msginfo->newsgroups);
784 if (msginfo->subject)
785 memusage += strlen(msginfo->subject);
787 memusage += strlen(msginfo->msgid);
788 if (msginfo->inreplyto)
789 memusage += strlen(msginfo->inreplyto);
791 memusage += strlen(msginfo->xface);
792 if (msginfo->dispositionnotificationto)
793 memusage += strlen(msginfo->dispositionnotificationto);
794 if (msginfo->returnreceiptto)
795 memusage += strlen(msginfo->returnreceiptto);
796 if (msginfo->references)
797 memusage += strlen(msginfo->references);
798 if (msginfo->fromspace)
799 memusage += strlen(msginfo->fromspace);
804 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
806 const MsgInfo *msginfo1 = a;
807 const MsgInfo *msginfo2 = b;
814 return msginfo1->msgnum - msginfo2->msgnum;
823 Q_MAIL_ACCOUNT_ID = 4,
824 Q_NEWS_ACCOUNT_ID = 5,
825 Q_SAVE_COPY_FOLDER = 6,
826 Q_REPLY_MESSAGE_ID = 7,
830 gint procmsg_send_message_queue(const gchar *file)
832 static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
833 {"SSV:", NULL, FALSE},
835 {"NG:", NULL, FALSE},
836 {"MAID:", NULL, FALSE},
837 {"NAID:", NULL, FALSE},
838 {"SCF:", NULL, FALSE},
839 {"RMID:", NULL, FALSE},
840 {"FMID:", NULL, FALSE},
841 {NULL, NULL, FALSE}};
844 gint mailval = 0, newsval = 0;
846 gchar *smtpserver = NULL;
847 GSList *to_list = NULL;
848 GSList *newsgroup_list = NULL;
849 gchar *savecopyfolder = NULL;
850 gchar *replymessageid = NULL;
851 gchar *fwdmessageid = NULL;
854 PrefsAccount *mailac = NULL, *newsac = NULL;
857 g_return_val_if_fail(file != NULL, -1);
859 if ((fp = fopen(file, "rb")) == NULL) {
860 FILE_OP_ERROR(file, "fopen");
864 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
866 gchar *p = buf + strlen(qentry[hnum].name);
870 if (!from) from = g_strdup(p);
873 if (!smtpserver) smtpserver = g_strdup(p);
876 to_list = address_list_append(to_list, p);
879 newsgroup_list = newsgroup_list_append(newsgroup_list, p);
881 case Q_MAIL_ACCOUNT_ID:
882 mailac = account_find_from_id(atoi(p));
884 case Q_NEWS_ACCOUNT_ID:
885 newsac = account_find_from_id(atoi(p));
887 case Q_SAVE_COPY_FOLDER:
888 if (!savecopyfolder) savecopyfolder = g_strdup(p);
890 case Q_REPLY_MESSAGE_ID:
891 if (!replymessageid) replymessageid = g_strdup(p);
893 case Q_FWD_MESSAGE_ID:
894 if (!fwdmessageid) fwdmessageid = g_strdup(p);
901 debug_print("Sending message by mail\n");
903 g_warning("Queued message header is broken.\n");
905 } else if (mailac && mailac->use_mail_command &&
906 mailac->mail_command && (* mailac->mail_command)) {
907 mailval = send_message_local(mailac->mail_command, fp);
909 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
910 mailval = send_message_local(prefs_common.extsend_cmd, fp);
914 mailac = account_find_from_smtp_server(from, smtpserver);
916 g_warning("Account not found. "
917 "Using current account...\n");
918 mailac = cur_account;
923 mailval = send_message_smtp(mailac, to_list, fp);
927 g_warning("Account not found.\n");
929 memset(&tmp_ac, 0, sizeof(PrefsAccount));
930 tmp_ac.address = from;
931 tmp_ac.smtp_server = smtpserver;
932 tmp_ac.smtpport = SMTP_PORT;
933 mailval = send_message_smtp(&tmp_ac, to_list, fp);
938 alertpanel_error_log(
939 _("Error occurred while sending the message to `%s'."),
940 mailac ? mailac->smtp_server : smtpserver);
942 alertpanel_error_log(
943 _("Error occurred while sending the message with command `%s'."),
944 (mailac && mailac->use_mail_command &&
945 mailac->mail_command && (*mailac->mail_command)) ?
946 mailac->mail_command : prefs_common.extsend_cmd);
950 fseek(fp, filepos, SEEK_SET);
951 if (newsgroup_list && (newsval == 0)) {
956 /* write to temporary file */
957 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
958 G_DIR_SEPARATOR, (gint)file);
959 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
960 FILE_OP_ERROR(tmp, "fopen");
962 alertpanel_error(_("Could not create temporary file for news sending."));
964 if (change_file_mode_rw(tmpfp, tmp) < 0) {
965 FILE_OP_ERROR(tmp, "chmod");
966 g_warning("can't change file mode\n");
969 while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
970 if (fputs(buf, tmpfp) == EOF) {
971 FILE_OP_ERROR(tmp, "fputs");
973 alertpanel_error(_("Error when writing temporary file for news sending."));
979 debug_print("Sending message by news\n");
981 folder = FOLDER(newsac->folder);
983 newsval = news_post(folder, tmp);
985 alertpanel_error(_("Error occurred while posting the message to %s ."),
986 newsac->nntp_server);
994 slist_free_strings(to_list);
995 g_slist_free(to_list);
996 slist_free_strings(newsgroup_list);
997 g_slist_free(newsgroup_list);
1002 /* save message to outbox */
1003 if (mailval == 0 && newsval == 0 && savecopyfolder) {
1006 debug_print("saving sent message...\n");
1008 outbox = folder_find_item_from_identifier(savecopyfolder);
1010 outbox = folder_get_default_outbox();
1012 procmsg_save_to_outbox(outbox, file, TRUE);
1015 if (replymessageid != NULL || fwdmessageid != NULL) {
1019 if (replymessageid != NULL)
1020 tokens = g_strsplit(replymessageid, "\x7f", 0);
1022 tokens = g_strsplit(fwdmessageid, "\x7f", 0);
1023 item = folder_find_item_from_identifier(tokens[0]);
1027 msginfo = folder_item_get_msginfo(item, atoi(tokens[1]));
1028 if ((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1029 procmsg_msginfo_free(msginfo);
1033 if (msginfo == NULL) {
1034 msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]);
1037 if (msginfo != NULL) {
1038 if (replymessageid != NULL) {
1039 procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1040 procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1043 procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
1044 procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0);
1046 procmsg_msginfo_free(msginfo);
1052 g_free(savecopyfolder);
1053 g_free(replymessageid);
1054 g_free(fwdmessageid);
1056 return (newsval != 0 ? newsval : mailval);
1059 #define CHANGE_FLAGS(msginfo) \
1061 if (msginfo->folder->folder->class->change_flags != NULL) \
1062 msginfo->folder->folder->class->change_flags(msginfo->folder->folder, \
1067 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1070 MsgInfoUpdate msginfo_update;
1072 g_return_if_fail(msginfo != NULL);
1073 item = msginfo->folder;
1074 g_return_if_fail(item != NULL);
1076 debug_print("Setting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1078 /* if new flag is set */
1079 if ((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
1080 !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1084 /* if unread flag is set */
1085 if ((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
1086 !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1090 if (!MSG_IS_UNREAD(msginfo->flags) &&(perm_flags & MSG_UNREAD)
1091 && procmsg_msg_has_marked_parent(msginfo)) {
1092 item->unreadmarked++;
1095 if (!MSG_IS_MARKED(msginfo->flags) && (perm_flags & MSG_MARKED)) {
1096 procmsg_update_unread_children(msginfo, TRUE);
1100 /* if ignore thread flag is set */
1101 if ((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1102 if (MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
1105 if (MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
1108 if ((perm_flags & MSG_UNREAD) || (MSG_IS_UNREAD(msginfo->flags)
1109 && procmsg_msg_has_marked_parent(msginfo))) {
1110 item->unreadmarked--;
1112 if ((perm_flags & MSG_MARKED) || (MSG_IS_MARKED(msginfo->flags)
1113 && !MSG_IS_IGNORE_THREAD(msginfo->flags))) {
1114 procmsg_update_unread_children(msginfo, FALSE);
1119 if (FOLDER_TYPE(msginfo->folder->folder) == F_IMAP)
1120 imap_msg_set_perm_flags(msginfo, perm_flags);
1122 msginfo->flags.perm_flags |= perm_flags;
1123 msginfo->flags.tmp_flags |= tmp_flags;
1125 msginfo_update.msginfo = msginfo;
1126 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1127 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1129 CHANGE_FLAGS(msginfo);
1132 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1135 MsgInfoUpdate msginfo_update;
1137 g_return_if_fail(msginfo != NULL);
1138 item = msginfo->folder;
1139 g_return_if_fail(item != NULL);
1141 debug_print("Unsetting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1143 /* if new flag is unset */
1144 if ((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
1145 !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1149 /* if unread flag is unset */
1150 if ((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
1151 !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1155 if (MSG_IS_UNREAD(msginfo->flags) && (perm_flags & MSG_UNREAD)
1156 && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1157 && procmsg_msg_has_marked_parent(msginfo)) {
1158 item->unreadmarked--;
1161 if (MSG_IS_MARKED(msginfo->flags) && (perm_flags & MSG_MARKED)
1162 && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1163 procmsg_update_unread_children(msginfo, FALSE);
1166 /* if ignore thread flag is unset */
1167 if ((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1168 if (MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
1171 if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
1174 if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)
1175 && procmsg_msg_has_marked_parent(msginfo)) {
1176 item->unreadmarked++;
1178 if (MSG_IS_MARKED(msginfo->flags) && !(perm_flags & MSG_MARKED)) {
1179 procmsg_update_unread_children(msginfo, TRUE);
1184 if (FOLDER_TYPE(msginfo->folder->folder) == F_IMAP)
1185 imap_msg_unset_perm_flags(msginfo, perm_flags);
1187 msginfo->flags.perm_flags &= ~perm_flags;
1188 msginfo->flags.tmp_flags &= ~tmp_flags;
1190 msginfo_update.msginfo = msginfo;
1191 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1192 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1194 CHANGE_FLAGS(msginfo);
1198 *\brief check for flags (e.g. mark) in prior msgs of current thread
1200 *\param info Current message
1201 *\param perm_flags Flags to be checked
1202 *\param parentmsgs Hash of prior msgs to avoid loops
1204 *\return gboolean TRUE if perm_flags are found
1206 gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
1207 MsgPermFlags perm_flags, GHashTable *parentmsgs)
1211 g_return_val_if_fail(info != NULL, FALSE);
1213 if (info != NULL && info->folder != NULL && info->inreplyto != NULL) {
1214 tmp = folder_item_get_msginfo_by_msgid(info->folder,
1216 if (tmp && (tmp->flags.perm_flags & perm_flags)) {
1217 procmsg_msginfo_free(tmp);
1219 } else if (tmp != NULL) {
1222 if (g_hash_table_lookup(parentmsgs, info)) {
1223 debug_print("loop detected: %s%c%d\n",
1224 folder_item_get_path(info->folder),
1225 G_DIR_SEPARATOR, info->msgnum);
1228 g_hash_table_insert(parentmsgs, info, "1");
1229 result = procmsg_msg_has_flagged_parent_real(
1230 tmp, perm_flags, parentmsgs);
1232 procmsg_msginfo_free(tmp);
1242 *\brief Callback for cleaning up hash of parentmsgs
1244 gboolean parentmsgs_hash_remove(gpointer key,
1252 *\brief Set up list of parentmsgs
1253 * See procmsg_msg_has_flagged_parent_real()
1255 gboolean procmsg_msg_has_flagged_parent(MsgInfo *info, MsgPermFlags perm_flags)
1258 GHashTable *parentmsgs = g_hash_table_new(NULL, NULL);
1260 result = procmsg_msg_has_flagged_parent_real(info, perm_flags, parentmsgs);
1261 g_hash_table_foreach_remove(parentmsgs, parentmsgs_hash_remove, NULL);
1262 g_hash_table_destroy(parentmsgs);
1267 *\brief Check if msgs prior in thread are marked
1268 * See procmsg_msg_has_flagged_parent_real()
1270 gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
1272 return procmsg_msg_has_flagged_parent(info, MSG_MARKED);
1276 GSList *procmsg_find_children_func(MsgInfo *info,
1277 GSList *children, GSList *all)
1281 g_return_val_if_fail(info!=NULL, children);
1282 if (info->msgid == NULL)
1285 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1286 MsgInfo *tmp = (MsgInfo *)cur->data;
1287 if (tmp->inreplyto && !strcmp(tmp->inreplyto, info->msgid)) {
1288 /* Check if message is already in the list */
1289 if ((children == NULL) ||
1290 (g_slist_index(children, tmp) == -1)) {
1291 children = g_slist_prepend(children,
1292 procmsg_msginfo_new_ref(tmp));
1293 children = procmsg_find_children_func(tmp,
1302 GSList *procmsg_find_children (MsgInfo *info)
1307 g_return_val_if_fail(info!=NULL, NULL);
1308 all = folder_item_get_msg_list(info->folder);
1309 children = procmsg_find_children_func(info, NULL, all);
1310 if (children != NULL) {
1311 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1312 /* this will not free the used pointers
1313 created with procmsg_msginfo_new_ref */
1314 procmsg_msginfo_free((MsgInfo *)cur->data);
1322 void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
1324 GSList *children = procmsg_find_children(info);
1326 for (cur = children; cur != NULL; cur = g_slist_next(cur)) {
1327 MsgInfo *tmp = (MsgInfo *)cur->data;
1328 if(MSG_IS_UNREAD(tmp->flags) && !MSG_IS_IGNORE_THREAD(tmp->flags)) {
1330 info->folder->unreadmarked++;
1332 info->folder->unreadmarked--;
1333 folder_item_update(info->folder, F_ITEM_UPDATE_MSGCNT);
1335 procmsg_msginfo_free(tmp);
1337 g_slist_free(children);
1341 * Set the destination folder for a copy or move operation
1343 * \param msginfo The message which's destination folder is changed
1344 * \param to_folder The destination folder for the operation
1346 void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
1348 if(msginfo->to_folder != NULL) {
1349 msginfo->to_folder->op_count--;
1350 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1352 msginfo->to_folder = to_folder;
1353 if(to_folder != NULL) {
1354 to_folder->op_count++;
1355 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);