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"
31 #include "send_message.h"
33 #include "statusbar.h"
35 #include "prefs_common.h"
40 #include "alertpanel.h"
45 typedef struct _FlagInfo FlagInfo;
53 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
55 GHashTable *msg_table;
57 if (mlist == NULL) return NULL;
59 msg_table = g_hash_table_new(NULL, g_direct_equal);
60 procmsg_msg_hash_table_append(msg_table, mlist);
65 void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
70 if (msg_table == NULL || mlist == NULL) return;
72 for (cur = mlist; cur != NULL; cur = cur->next) {
73 msginfo = (MsgInfo *)cur->data;
75 g_hash_table_insert(msg_table,
76 GUINT_TO_POINTER(msginfo->msgnum),
81 GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
83 GHashTable *msg_table;
87 if (mlist == NULL) return NULL;
89 msg_table = g_hash_table_new(NULL, g_direct_equal);
91 for (cur = mlist; cur != NULL; cur = cur->next) {
92 msginfo = (MsgInfo *)cur->data;
93 g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
99 gint procmsg_get_last_num_in_msg_list(GSList *mlist)
105 for (cur = mlist; cur != NULL; cur = cur->next) {
106 msginfo = (MsgInfo *)cur->data;
107 if (msginfo && msginfo->msgnum > last)
108 last = msginfo->msgnum;
114 void procmsg_msg_list_free(GSList *mlist)
119 for (cur = mlist; cur != NULL; cur = cur->next) {
120 msginfo = (MsgInfo *)cur->data;
121 procmsg_msginfo_free(msginfo);
135 static gboolean procmsg_ignore_node(GNode *node, gpointer data)
137 MsgInfo *msginfo = (MsgInfo *)node->data;
139 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
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_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
171 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
175 node = g_node_insert_data_before
176 (parent, parent == root ? parent->children : NULL,
178 if ((msgid = msginfo->msgid) &&
179 g_hash_table_lookup(msgid_table, msgid) == NULL)
180 g_hash_table_insert(msgid_table, (gchar *)msgid, node);
182 if (prefs_common.thread_by_subject) {
183 subject = msginfo->subject;
184 found_subject = subject_table_lookup(subject_table,
186 if (found_subject == NULL)
187 subject_table_insert(subject_table, (gchar *) subject,
190 /* replace if msg in table is older than current one
191 * can add here more stuff. */
192 if ( ((MsgInfo*)(found_subject->data))->date_t >
193 ((MsgInfo*)(node->data))->date_t ) {
194 subject_table_remove(subject_table, (gchar *) subject);
195 subject_table_insert(subject_table, (gchar *) subject, node);
201 /* complete the unfinished threads */
202 for (node = root->children; node != NULL; ) {
204 msginfo = (MsgInfo *)node->data;
206 if (msginfo->inreplyto)
207 parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
208 if (parent && parent != node) {
211 (parent, parent->children, node);
212 /* CLAWS: ignore thread */
213 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
214 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
220 /* CLAWS: now see if the first level (below root) still has some nodes that can be
221 * threaded by subject line. we need to handle this in a special way to prevent
222 * circular reference from a node that has already been threaded by IN-REPLY-TO
223 * but is also in the subject line hash table */
224 if (prefs_common.thread_by_subject) {
225 for (node = root->children; node != NULL; ) {
227 msginfo = (MsgInfo *) node->data;
229 if (subject_is_reply(msginfo->subject)) {
230 parent = subject_table_lookup(subject_table,
232 /* the node may already be threaded by IN-REPLY-TO,
233 so go up in the tree to find the parent node */
234 if (parent != NULL) {
235 if (g_node_is_ancestor(node, parent))
243 g_node_append(parent, node);
244 /* CLAWS: ignore thread */
245 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
246 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
254 g_hash_table_destroy(subject_table);
255 g_hash_table_destroy(msgid_table);
260 void procmsg_move_messages(GSList *mlist)
262 GSList *cur, *movelist = NULL;
264 FolderItem *dest = NULL;
268 folder_item_update_freeze();
270 for (cur = mlist; cur != NULL; cur = cur->next) {
271 msginfo = (MsgInfo *)cur->data;
273 dest = msginfo->to_folder;
274 movelist = g_slist_append(movelist, msginfo);
275 } else if (dest == msginfo->to_folder) {
276 movelist = g_slist_append(movelist, msginfo);
278 folder_item_move_msgs_with_dest(dest, movelist);
279 g_slist_free(movelist);
281 dest = msginfo->to_folder;
282 movelist = g_slist_append(movelist, msginfo);
284 procmsg_msginfo_set_to_folder(msginfo, NULL);
288 folder_item_move_msgs_with_dest(dest, movelist);
289 g_slist_free(movelist);
292 folder_item_update_thaw();
295 void procmsg_copy_messages(GSList *mlist)
297 GSList *cur, *copylist = NULL;
299 FolderItem *dest = NULL;
303 folder_item_update_freeze();
305 for (cur = mlist; cur != NULL; cur = cur->next) {
306 msginfo = (MsgInfo *)cur->data;
308 dest = msginfo->to_folder;
309 copylist = g_slist_append(copylist, msginfo);
310 } else if (dest == msginfo->to_folder) {
311 copylist = g_slist_append(copylist, msginfo);
313 folder_item_copy_msgs_with_dest(dest, copylist);
314 g_slist_free(copylist);
316 dest = msginfo->to_folder;
317 copylist = g_slist_append(copylist, msginfo);
319 procmsg_msginfo_set_to_folder(msginfo, NULL);
323 folder_item_copy_msgs_with_dest(dest, copylist);
324 g_slist_free(copylist);
327 folder_item_update_thaw();
330 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
334 g_return_val_if_fail(msginfo != NULL, NULL);
336 if (msginfo->plaintext_file)
337 file = g_strdup(msginfo->plaintext_file);
339 path = folder_item_get_path(msginfo->folder);
340 file = g_strconcat(path, G_DIR_SEPARATOR_S,
341 itos(msginfo->msgnum), NULL);
348 gchar *procmsg_get_message_file(MsgInfo *msginfo)
350 gchar *filename = NULL;
352 g_return_val_if_fail(msginfo != NULL, NULL);
354 filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
356 g_warning("can't fetch message %d\n", msginfo->msgnum);
361 FILE *procmsg_open_message(MsgInfo *msginfo)
366 g_return_val_if_fail(msginfo != NULL, NULL);
368 file = procmsg_get_message_file_path(msginfo);
369 g_return_val_if_fail(file != NULL, NULL);
371 if (!is_file_exist(file)) {
373 file = procmsg_get_message_file(msginfo);
374 g_return_val_if_fail(file != NULL, NULL);
377 if ((fp = fopen(file, "rb")) == NULL) {
378 FILE_OP_ERROR(file, "fopen");
385 if (MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags)) {
388 while (fgets(buf, sizeof(buf), fp) != NULL)
389 if (buf[0] == '\r' || buf[0] == '\n') break;
396 FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
401 g_return_val_if_fail(msginfo != NULL, NULL);
403 if (mimeinfo) *mimeinfo = NULL;
405 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
407 mimeinfo_ = procmime_scan_mime_header(fp);
413 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
414 rfc2015_is_encrypted(mimeinfo_)) {
415 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
418 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
419 (!msginfo->plaintext_file || msginfo->decryption_failed)) {
420 rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
421 if (msginfo->plaintext_file &&
422 !msginfo->decryption_failed) {
424 procmime_mimeinfo_free_all(mimeinfo_);
425 if ((fp = procmsg_open_message(msginfo)) == NULL)
427 mimeinfo_ = procmime_scan_mime_header(fp);
435 if (mimeinfo) *mimeinfo = mimeinfo_;
440 gboolean procmsg_msg_exist(MsgInfo *msginfo)
445 if (!msginfo) return FALSE;
447 path = folder_item_get_path(msginfo->folder);
449 ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
455 void procmsg_empty_trash(void)
460 for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
461 trash = FOLDER(cur->data)->trash;
462 if (trash && trash->total > 0)
463 folder_item_remove_all_msg(trash);
468 *\brief Send messages in queue
470 *\param queue Queue folder to process
471 *\param save_msgs Unused
473 *\return Number of messages sent, negative if an error occurred
474 * positive if no error occurred
476 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
478 gint ret = 1, count = 0;
482 queue = folder_get_default_queue();
483 g_return_val_if_fail(queue != NULL, -1);
485 folder_item_scan(queue);
486 list = folder_item_get_msg_list(queue);
488 for (elem = list; elem != NULL; elem = elem->next) {
492 msginfo = (MsgInfo *)(elem->data);
493 if (!MSG_IS_LOCKED(msginfo->flags)) {
494 file = folder_item_fetch_msg(queue, msginfo->msgnum);
496 if (procmsg_send_message_queue(file) < 0) {
497 g_warning("Sending queued message %d failed.\n",
502 * We save in procmsg_send_message_queue because
503 * we need the destination folder from the queue
507 procmsg_save_to_outbox
508 (queue->folder->outbox,
512 folder_item_remove_msg(queue, msginfo->msgnum);
517 /* FIXME: supposedly if only one message is locked, and queue
518 * is being flushed, the following free says something like
519 * "freeing msg ## in folder (nil)". */
520 procmsg_msginfo_free(msginfo);
526 gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
531 if ((fp = fopen(in, "rb")) == NULL) {
532 FILE_OP_ERROR(in, "fopen");
535 if ((outfp = fopen(out, "wb")) == NULL) {
536 FILE_OP_ERROR(out, "fopen");
540 while (fgets(buf, sizeof(buf), fp) != NULL)
541 if (buf[0] == '\r' || buf[0] == '\n') break;
542 while (fgets(buf, sizeof(buf), fp) != NULL)
549 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
555 debug_print("saving sent message...\n");
558 outbox = folder_get_default_outbox();
559 g_return_val_if_fail(outbox != NULL, -1);
561 /* remove queueing headers */
563 gchar tmp[MAXPATHLEN + 1];
565 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
566 get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
568 if (procmsg_remove_special_headers(file, tmp) !=0)
571 folder_item_scan(outbox);
572 if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
573 g_warning("can't save message\n");
578 folder_item_scan(outbox);
579 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
580 g_warning("can't save message\n");
585 msginfo = folder_item_get_msginfo(outbox, num);
586 if (msginfo != NULL) {
587 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
588 procmsg_msginfo_free(msginfo);
590 folder_item_update(outbox, TRUE);
595 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
597 static const gchar *def_cmd = "lpr %s";
604 g_return_if_fail(msginfo);
606 if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
607 g_warning("Can't get text part\n");
611 prtmp = g_strdup_printf("%s%cprinttmp.%08x",
612 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
614 if ((prfp = fopen(prtmp, "wb")) == NULL) {
615 FILE_OP_ERROR(prtmp, "fopen");
621 if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
622 if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
623 if (msginfo->to) fprintf(prfp, "To: %s\n", msginfo->to);
624 if (msginfo->cc) fprintf(prfp, "Cc: %s\n", msginfo->cc);
625 if (msginfo->newsgroups)
626 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
627 if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
630 while (fgets(buf, sizeof(buf), tmpfp) != NULL)
636 if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
638 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
641 g_warning("Print command line is invalid: `%s'\n",
643 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
649 if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
653 MsgInfo *procmsg_msginfo_new_ref(MsgInfo *msginfo)
660 MsgInfo *procmsg_msginfo_new()
664 newmsginfo = g_new0(MsgInfo, 1);
665 newmsginfo->refcnt = 1;
670 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
674 if (msginfo == NULL) return NULL;
676 newmsginfo = g_new0(MsgInfo, 1);
678 newmsginfo->refcnt = 1;
680 #define MEMBCOPY(mmb) newmsginfo->mmb = msginfo->mmb
681 #define MEMBDUP(mmb) newmsginfo->mmb = msginfo->mmb ? \
682 g_strdup(msginfo->mmb) : NULL
706 MEMBDUP(dispositionnotificationto);
707 MEMBDUP(returnreceiptto);
711 MEMBCOPY(threadscore);
716 MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
718 MsgInfo *full_msginfo;
721 if (msginfo == NULL) return NULL;
723 file = procmsg_get_message_file(msginfo);
725 g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
729 full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE, FALSE);
731 if (!full_msginfo) return NULL;
733 full_msginfo->msgnum = msginfo->msgnum;
734 full_msginfo->size = msginfo->size;
735 full_msginfo->mtime = msginfo->mtime;
736 full_msginfo->folder = msginfo->folder;
738 full_msginfo->plaintext_file = g_strdup(msginfo->plaintext_file);
739 full_msginfo->decryption_failed = msginfo->decryption_failed;
741 procmsg_msginfo_set_to_folder(full_msginfo, msginfo->to_folder);
746 void procmsg_msginfo_free(MsgInfo *msginfo)
748 if (msginfo == NULL) return;
751 if (msginfo->refcnt > 0)
754 debug_print("freeing msginfo %d in %s\n", msginfo->msgnum, msginfo->folder ? msginfo->folder->path : "(nil)");
756 if (msginfo->to_folder) {
757 msginfo->to_folder->op_count--;
758 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
761 g_free(msginfo->fromspace);
762 g_free(msginfo->references);
763 g_free(msginfo->returnreceiptto);
764 g_free(msginfo->dispositionnotificationto);
765 g_free(msginfo->xface);
767 g_free(msginfo->fromname);
769 g_free(msginfo->date);
770 g_free(msginfo->from);
773 g_free(msginfo->newsgroups);
774 g_free(msginfo->subject);
775 g_free(msginfo->msgid);
776 g_free(msginfo->inreplyto);
777 g_free(msginfo->xref);
782 guint procmsg_msginfo_memusage(MsgInfo *msginfo)
786 memusage += sizeof(MsgInfo);
787 if (msginfo->fromname)
788 memusage += strlen(msginfo->fromname);
790 memusage += strlen(msginfo->date);
792 memusage += strlen(msginfo->from);
794 memusage += strlen(msginfo->to);
796 memusage += strlen(msginfo->cc);
797 if (msginfo->newsgroups)
798 memusage += strlen(msginfo->newsgroups);
799 if (msginfo->subject)
800 memusage += strlen(msginfo->subject);
802 memusage += strlen(msginfo->msgid);
803 if (msginfo->inreplyto)
804 memusage += strlen(msginfo->inreplyto);
806 memusage += strlen(msginfo->xface);
807 if (msginfo->dispositionnotificationto)
808 memusage += strlen(msginfo->dispositionnotificationto);
809 if (msginfo->returnreceiptto)
810 memusage += strlen(msginfo->returnreceiptto);
811 if (msginfo->references)
812 memusage += strlen(msginfo->references);
813 if (msginfo->fromspace)
814 memusage += strlen(msginfo->fromspace);
819 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
821 const MsgInfo *msginfo1 = a;
822 const MsgInfo *msginfo2 = b;
829 return msginfo1->msgnum - msginfo2->msgnum;
838 Q_MAIL_ACCOUNT_ID = 4,
839 Q_NEWS_ACCOUNT_ID = 5,
840 Q_SAVE_COPY_FOLDER = 6,
841 Q_REPLY_MESSAGE_ID = 7,
845 gint procmsg_send_message_queue(const gchar *file)
847 static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
848 {"SSV:", NULL, FALSE},
850 {"NG:", NULL, FALSE},
851 {"MAID:", NULL, FALSE},
852 {"NAID:", NULL, FALSE},
853 {"SCF:", NULL, FALSE},
854 {"RMID:", NULL, FALSE},
855 {"FMID:", NULL, FALSE},
856 {NULL, NULL, FALSE}};
859 gint mailval = 0, newsval = 0;
861 gchar *smtpserver = NULL;
862 GSList *to_list = NULL;
863 GSList *newsgroup_list = NULL;
864 gchar *savecopyfolder = NULL;
865 gchar *replymessageid = NULL;
866 gchar *fwdmessageid = NULL;
869 PrefsAccount *mailac = NULL, *newsac = NULL;
872 g_return_val_if_fail(file != NULL, -1);
874 if ((fp = fopen(file, "rb")) == NULL) {
875 FILE_OP_ERROR(file, "fopen");
879 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
881 gchar *p = buf + strlen(qentry[hnum].name);
885 if (!from) from = g_strdup(p);
888 if (!smtpserver) smtpserver = g_strdup(p);
891 to_list = address_list_append(to_list, p);
894 newsgroup_list = newsgroup_list_append(newsgroup_list, p);
896 case Q_MAIL_ACCOUNT_ID:
897 mailac = account_find_from_id(atoi(p));
899 case Q_NEWS_ACCOUNT_ID:
900 newsac = account_find_from_id(atoi(p));
902 case Q_SAVE_COPY_FOLDER:
903 if (!savecopyfolder) savecopyfolder = g_strdup(p);
905 case Q_REPLY_MESSAGE_ID:
906 if (!replymessageid) replymessageid = g_strdup(p);
908 case Q_FWD_MESSAGE_ID:
909 if (!fwdmessageid) fwdmessageid = g_strdup(p);
916 debug_print("Sending message by mail\n");
918 g_warning("Queued message header is broken.\n");
920 } else if (mailac && mailac->use_mail_command &&
921 mailac->mail_command && (* mailac->mail_command)) {
922 mailval = send_message_local(mailac->mail_command, fp);
924 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
925 mailval = send_message_local(prefs_common.extsend_cmd, fp);
929 mailac = account_find_from_smtp_server(from, smtpserver);
931 g_warning("Account not found. "
932 "Using current account...\n");
933 mailac = cur_account;
938 mailval = send_message_smtp(mailac, to_list, fp);
942 g_warning("Account not found.\n");
944 memset(&tmp_ac, 0, sizeof(PrefsAccount));
945 tmp_ac.address = from;
946 tmp_ac.smtp_server = smtpserver;
947 tmp_ac.smtpport = SMTP_PORT;
948 mailval = send_message_smtp(&tmp_ac, to_list, fp);
953 alertpanel_error_log(
954 _("Error occurred while sending the message to `%s'."),
955 mailac ? mailac->smtp_server : smtpserver);
957 alertpanel_error_log(
958 _("Error occurred while sending the message with command `%s'."),
959 (mailac && mailac->use_mail_command &&
960 mailac->mail_command && (*mailac->mail_command)) ?
961 mailac->mail_command : prefs_common.extsend_cmd);
965 fseek(fp, filepos, SEEK_SET);
966 if (newsgroup_list && (newsval == 0)) {
971 /* write to temporary file */
972 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
973 G_DIR_SEPARATOR, (gint)file);
974 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
975 FILE_OP_ERROR(tmp, "fopen");
977 alertpanel_error(_("Could not create temporary file for news sending."));
979 if (change_file_mode_rw(tmpfp, tmp) < 0) {
980 FILE_OP_ERROR(tmp, "chmod");
981 g_warning("can't change file mode\n");
984 while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
985 if (fputs(buf, tmpfp) == EOF) {
986 FILE_OP_ERROR(tmp, "fputs");
988 alertpanel_error(_("Error when writing temporary file for news sending."));
994 debug_print("Sending message by news\n");
996 folder = FOLDER(newsac->folder);
998 newsval = news_post(folder, tmp);
1000 alertpanel_error(_("Error occurred while posting the message to %s ."),
1001 newsac->nntp_server);
1009 slist_free_strings(to_list);
1010 g_slist_free(to_list);
1011 slist_free_strings(newsgroup_list);
1012 g_slist_free(newsgroup_list);
1017 /* save message to outbox */
1018 if (mailval == 0 && newsval == 0 && savecopyfolder) {
1021 debug_print("saving sent message...\n");
1023 outbox = folder_find_item_from_identifier(savecopyfolder);
1025 outbox = folder_get_default_outbox();
1027 procmsg_save_to_outbox(outbox, file, TRUE);
1030 if (replymessageid != NULL || fwdmessageid != NULL) {
1034 if (replymessageid != NULL)
1035 tokens = g_strsplit(replymessageid, "\x7f", 0);
1037 tokens = g_strsplit(fwdmessageid, "\x7f", 0);
1038 item = folder_find_item_from_identifier(tokens[0]);
1042 msginfo = folder_item_get_msginfo(item, atoi(tokens[1]));
1043 if ((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1044 procmsg_msginfo_free(msginfo);
1048 if (msginfo == NULL) {
1049 msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]);
1052 if (msginfo != NULL) {
1053 if (replymessageid != NULL) {
1054 procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1055 procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1058 procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
1059 procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0);
1061 procmsg_msginfo_free(msginfo);
1067 g_free(savecopyfolder);
1068 g_free(replymessageid);
1069 g_free(fwdmessageid);
1071 return (newsval != 0 ? newsval : mailval);
1074 static void update_folder_msg_counts(FolderItem *item, MsgInfo *msginfo, MsgPermFlags old)
1076 MsgPermFlags new = msginfo->flags.perm_flags;
1079 if (!(old & MSG_NEW) && (new & MSG_NEW)) {
1083 if ((old & MSG_NEW) && !(new & MSG_NEW)) {
1088 if (!(old & MSG_UNREAD) && (new & MSG_UNREAD)) {
1090 if (procmsg_msg_has_marked_parent(msginfo))
1091 item->unreadmarked++;
1094 if ((old & MSG_UNREAD) && !(new & MSG_UNREAD)) {
1096 if (procmsg_msg_has_marked_parent(msginfo))
1097 item->unreadmarked--;
1101 if (!(old & MSG_MARKED) && (new & MSG_MARKED)) {
1102 procmsg_update_unread_children(msginfo, TRUE);
1105 if ((old & MSG_MARKED) && !(new & MSG_MARKED)) {
1106 procmsg_update_unread_children(msginfo, FALSE);
1110 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1113 MsgInfoUpdate msginfo_update;
1114 MsgPermFlags perm_flags_new, perm_flags_old;
1116 g_return_if_fail(msginfo != NULL);
1117 item = msginfo->folder;
1118 g_return_if_fail(item != NULL);
1120 debug_print("Setting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1122 /* Perm Flags handling */
1123 perm_flags_old = msginfo->flags.perm_flags;
1124 perm_flags_new = msginfo->flags.perm_flags | perm_flags;
1125 if ((perm_flags & MSG_IGNORE_THREAD) || (perm_flags_old & MSG_IGNORE_THREAD)) {
1126 perm_flags_new &= ~(MSG_NEW | MSG_UNREAD);
1129 if (perm_flags_old != perm_flags_new) {
1130 folder_item_change_msg_flags(msginfo->folder, msginfo, perm_flags_new);
1132 update_folder_msg_counts(item, msginfo, perm_flags_old);
1134 msginfo_update.msginfo = msginfo;
1135 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1136 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1139 /* Tmp flags hanlding */
1140 msginfo->flags.tmp_flags |= tmp_flags;
1143 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1146 MsgInfoUpdate msginfo_update;
1147 MsgPermFlags perm_flags_new, perm_flags_old;
1149 g_return_if_fail(msginfo != NULL);
1150 item = msginfo->folder;
1151 g_return_if_fail(item != NULL);
1153 debug_print("Unsetting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1155 /* Perm Flags handling */
1156 perm_flags_old = msginfo->flags.perm_flags;
1157 perm_flags_new = msginfo->flags.perm_flags & ~perm_flags;
1159 if (perm_flags_old != perm_flags_new) {
1160 folder_item_change_msg_flags(msginfo->folder, msginfo, perm_flags_new);
1162 update_folder_msg_counts(item, msginfo, perm_flags_old);
1164 msginfo_update.msginfo = msginfo;
1165 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1166 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1169 /* Tmp flags hanlding */
1170 msginfo->flags.tmp_flags &= ~tmp_flags;
1174 *\brief check for flags (e.g. mark) in prior msgs of current thread
1176 *\param info Current message
1177 *\param perm_flags Flags to be checked
1178 *\param parentmsgs Hash of prior msgs to avoid loops
1180 *\return gboolean TRUE if perm_flags are found
1182 gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
1183 MsgPermFlags perm_flags, GHashTable *parentmsgs)
1187 g_return_val_if_fail(info != NULL, FALSE);
1189 if (info != NULL && info->folder != NULL && info->inreplyto != NULL) {
1190 tmp = folder_item_get_msginfo_by_msgid(info->folder,
1192 if (tmp && (tmp->flags.perm_flags & perm_flags)) {
1193 procmsg_msginfo_free(tmp);
1195 } else if (tmp != NULL) {
1198 if (g_hash_table_lookup(parentmsgs, info)) {
1199 debug_print("loop detected: %s%c%d\n",
1200 folder_item_get_path(info->folder),
1201 G_DIR_SEPARATOR, info->msgnum);
1204 g_hash_table_insert(parentmsgs, info, "1");
1205 result = procmsg_msg_has_flagged_parent_real(
1206 tmp, perm_flags, parentmsgs);
1208 procmsg_msginfo_free(tmp);
1218 *\brief Callback for cleaning up hash of parentmsgs
1220 gboolean parentmsgs_hash_remove(gpointer key,
1228 *\brief Set up list of parentmsgs
1229 * See procmsg_msg_has_flagged_parent_real()
1231 gboolean procmsg_msg_has_flagged_parent(MsgInfo *info, MsgPermFlags perm_flags)
1234 GHashTable *parentmsgs = g_hash_table_new(NULL, NULL);
1236 result = procmsg_msg_has_flagged_parent_real(info, perm_flags, parentmsgs);
1237 g_hash_table_foreach_remove(parentmsgs, parentmsgs_hash_remove, NULL);
1238 g_hash_table_destroy(parentmsgs);
1243 *\brief Check if msgs prior in thread are marked
1244 * See procmsg_msg_has_flagged_parent_real()
1246 gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
1248 return procmsg_msg_has_flagged_parent(info, MSG_MARKED);
1252 GSList *procmsg_find_children_func(MsgInfo *info,
1253 GSList *children, GSList *all)
1257 g_return_val_if_fail(info!=NULL, children);
1258 if (info->msgid == NULL)
1261 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1262 MsgInfo *tmp = (MsgInfo *)cur->data;
1263 if (tmp->inreplyto && !strcmp(tmp->inreplyto, info->msgid)) {
1264 /* Check if message is already in the list */
1265 if ((children == NULL) ||
1266 (g_slist_index(children, tmp) == -1)) {
1267 children = g_slist_prepend(children,
1268 procmsg_msginfo_new_ref(tmp));
1269 children = procmsg_find_children_func(tmp,
1278 GSList *procmsg_find_children (MsgInfo *info)
1283 g_return_val_if_fail(info!=NULL, NULL);
1284 all = folder_item_get_msg_list(info->folder);
1285 children = procmsg_find_children_func(info, NULL, all);
1286 if (children != NULL) {
1287 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1288 /* this will not free the used pointers
1289 created with procmsg_msginfo_new_ref */
1290 procmsg_msginfo_free((MsgInfo *)cur->data);
1298 void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
1300 GSList *children = procmsg_find_children(info);
1302 for (cur = children; cur != NULL; cur = g_slist_next(cur)) {
1303 MsgInfo *tmp = (MsgInfo *)cur->data;
1304 if(MSG_IS_UNREAD(tmp->flags) && !MSG_IS_IGNORE_THREAD(tmp->flags)) {
1306 info->folder->unreadmarked++;
1308 info->folder->unreadmarked--;
1309 folder_item_update(info->folder, F_ITEM_UPDATE_MSGCNT);
1311 procmsg_msginfo_free(tmp);
1313 g_slist_free(children);
1317 * Set the destination folder for a copy or move operation
1319 * \param msginfo The message which's destination folder is changed
1320 * \param to_folder The destination folder for the operation
1322 void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
1324 if(msginfo->to_folder != NULL) {
1325 msginfo->to_folder->op_count--;
1326 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1328 msginfo->to_folder = to_folder;
1329 if(to_folder != NULL) {
1330 to_folder->op_count++;
1331 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);