7b7635320287b2bdafac5d35f371b0e1b7625aaf
[claws.git] / src / procmsg.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26
27 #include "intl.h"
28 #include "main.h"
29 #include "utils.h"
30 #include "procmsg.h"
31 #include "procheader.h"
32 #include "send_message.h"
33 #include "procmime.h"
34 #include "statusbar.h"
35 #include "prefs_filtering.h"
36 #include "filtering.h"
37 #include "folder.h"
38 #include "prefs_common.h"
39 #include "account.h"
40 #if USE_GPGME
41 #  include "rfc2015.h"
42 #endif
43 #include "alertpanel.h"
44 #include "news.h"
45 #include "hooks.h"
46 #include "msgcache.h"
47
48 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
49 {
50         GHashTable *msg_table;
51
52         if (mlist == NULL) return NULL;
53
54         msg_table = g_hash_table_new(NULL, g_direct_equal);
55         procmsg_msg_hash_table_append(msg_table, mlist);
56
57         return msg_table;
58 }
59
60 void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
61 {
62         GSList *cur;
63         MsgInfo *msginfo;
64
65         if (msg_table == NULL || mlist == NULL) return;
66
67         for (cur = mlist; cur != NULL; cur = cur->next) {
68                 msginfo = (MsgInfo *)cur->data;
69
70                 g_hash_table_insert(msg_table,
71                                     GUINT_TO_POINTER(msginfo->msgnum),
72                                     msginfo);
73         }
74 }
75
76 GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
77 {
78         GHashTable *msg_table;
79         GSList *cur;
80         MsgInfo *msginfo;
81
82         if (mlist == NULL) return NULL;
83
84         msg_table = g_hash_table_new(NULL, g_direct_equal);
85
86         for (cur = mlist; cur != NULL; cur = cur->next) {
87                 msginfo = (MsgInfo *)cur->data;
88                 g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
89         }
90
91         return msg_table;
92 }
93
94 gint procmsg_get_last_num_in_msg_list(GSList *mlist)
95 {
96         GSList *cur;
97         MsgInfo *msginfo;
98         gint last = 0;
99
100         for (cur = mlist; cur != NULL; cur = cur->next) {
101                 msginfo = (MsgInfo *)cur->data;
102                 if (msginfo && msginfo->msgnum > last)
103                         last = msginfo->msgnum;
104         }
105
106         return last;
107 }
108
109 void procmsg_msg_list_free(GSList *mlist)
110 {
111         GSList *cur;
112         MsgInfo *msginfo;
113
114         for (cur = mlist; cur != NULL; cur = cur->next) {
115                 msginfo = (MsgInfo *)cur->data;
116                 procmsg_msginfo_free(msginfo);
117         }
118         g_slist_free(mlist);
119 }
120
121 struct MarkSum {
122         gint *new_msgs;
123         gint *unread_msgs;
124         gint *total_msgs;
125         gint *min;
126         gint *max;
127         gint first;
128 };
129
130 static gboolean procmsg_ignore_node(GNode *node, gpointer data)
131 {
132         MsgInfo *msginfo = (MsgInfo *)node->data;
133         
134         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
135         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
136
137         return FALSE;
138 }
139
140 /* return the reversed thread tree */
141 GNode *procmsg_get_thread_tree(GSList *mlist)
142 {
143         GNode *root, *parent, *node, *next;
144         GHashTable *msgid_table;
145         GHashTable *subject_table;
146         MsgInfo *msginfo;
147         const gchar *msgid;
148         const gchar *subject;
149         GNode *found_subject;
150
151         root = g_node_new(NULL);
152         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
153         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
154
155         for (; mlist != NULL; mlist = mlist->next) {
156                 msginfo = (MsgInfo *)mlist->data;
157                 parent = root;
158
159                 if (msginfo->inreplyto) {
160                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
161                         if (parent == NULL) {
162                                 parent = root;
163                         } else {
164                                 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
165                                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
166                                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
167                                 }
168                         }
169                 }
170                 node = g_node_insert_data_before
171                         (parent, parent == root ? parent->children : NULL,
172                          msginfo);
173                 if ((msgid = msginfo->msgid) &&
174                     g_hash_table_lookup(msgid_table, msgid) == NULL)
175                         g_hash_table_insert(msgid_table, (gchar *)msgid, node);
176
177                 if (prefs_common.thread_by_subject) {
178                         subject = msginfo->subject;
179                         found_subject = subject_table_lookup(subject_table,
180                                                              (gchar *) subject);
181                         if (found_subject == NULL)
182                                 subject_table_insert(subject_table, (gchar *) subject,
183                                                      node);
184                         else {
185                                 /* replace if msg in table is older than current one 
186                                  * can add here more stuff. */
187                                 if ( ((MsgInfo*)(found_subject->data))->date_t >
188                                      ((MsgInfo*)(node->data))->date_t )  {
189                                         subject_table_remove(subject_table, (gchar *) subject);
190                                         subject_table_insert(subject_table, (gchar *) subject, node);
191                                 }       
192                         }
193                 }
194         }
195
196         /* complete the unfinished threads */
197         for (node = root->children; node != NULL; ) {
198                 next = node->next;
199                 msginfo = (MsgInfo *)node->data;
200                 parent = NULL;
201                 if (msginfo->inreplyto) 
202                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
203                 if (parent && parent != node) {
204                         g_node_unlink(node);
205                         g_node_insert_before
206                                 (parent, parent->children, node);
207                         /* CLAWS: ignore thread */
208                         if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
209                                 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
210                         }
211                 }
212                 node = next;
213         }
214
215         /* CLAWS: now see if the first level (below root) still has some nodes that can be
216          * threaded by subject line. we need to handle this in a special way to prevent
217          * circular reference from a node that has already been threaded by IN-REPLY-TO
218          * but is also in the subject line hash table */
219         if (prefs_common.thread_by_subject) {
220                 for (node = root->children; node != NULL; ) {
221                         next = node->next;
222                         msginfo = (MsgInfo *) node->data;
223                         parent = NULL;
224                         if (subject_is_reply(msginfo->subject)) {
225                                 parent = subject_table_lookup(subject_table,
226                                                               msginfo->subject);
227                                 /* the node may already be threaded by IN-REPLY-TO,
228                                    so go up in the tree to find the parent node */
229                                 if (parent != NULL) {
230                                         if (g_node_is_ancestor(node, parent))
231                                                 parent = NULL;
232                                         if (parent == node)
233                                                 parent = NULL;
234                                 }
235
236                                 if (parent) {
237                                         g_node_unlink(node);
238                                         g_node_append(parent, node);
239                                         /* CLAWS: ignore thread */
240                                         if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
241                                                 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
242                                         }
243                                 }
244                         }                                       
245                         node = next;
246                 }       
247         }
248         
249         g_hash_table_destroy(subject_table);
250         g_hash_table_destroy(msgid_table);
251
252         return root;
253 }
254
255 void procmsg_move_messages(GSList *mlist)
256 {
257         GSList *cur, *movelist = NULL;
258         MsgInfo *msginfo;
259         FolderItem *dest = NULL;
260
261         if (!mlist) return;
262
263         folder_item_update_freeze();
264
265         for (cur = mlist; cur != NULL; cur = cur->next) {
266                 msginfo = (MsgInfo *)cur->data;
267                 if (!dest) {
268                         dest = msginfo->to_folder;
269                         movelist = g_slist_append(movelist, msginfo);
270                 } else if (dest == msginfo->to_folder) {
271                         movelist = g_slist_append(movelist, msginfo);
272                 } else {
273                         folder_item_move_msgs_with_dest(dest, movelist);
274                         g_slist_free(movelist);
275                         movelist = NULL;
276                         dest = msginfo->to_folder;
277                         movelist = g_slist_append(movelist, msginfo);
278                 }
279                 procmsg_msginfo_set_to_folder(msginfo, NULL);
280         }
281
282         if (movelist) {
283                 folder_item_move_msgs_with_dest(dest, movelist);
284                 g_slist_free(movelist);
285         }
286
287         folder_item_update_thaw();
288 }
289
290 void procmsg_copy_messages(GSList *mlist)
291 {
292         GSList *cur, *copylist = NULL;
293         MsgInfo *msginfo;
294         FolderItem *dest = NULL;
295
296         if (!mlist) return;
297
298         folder_item_update_freeze();
299
300         for (cur = mlist; cur != NULL; cur = cur->next) {
301                 msginfo = (MsgInfo *)cur->data;
302                 if (!dest) {
303                         dest = msginfo->to_folder;
304                         copylist = g_slist_append(copylist, msginfo);
305                 } else if (dest == msginfo->to_folder) {
306                         copylist = g_slist_append(copylist, msginfo);
307                 } else {
308                         folder_item_copy_msgs_with_dest(dest, copylist);
309                         g_slist_free(copylist);
310                         copylist = NULL;
311                         dest = msginfo->to_folder;
312                         copylist = g_slist_append(copylist, msginfo);
313                 }
314                 procmsg_msginfo_set_to_folder(msginfo, NULL);
315         }
316
317         if (copylist) {
318                 folder_item_copy_msgs_with_dest(dest, copylist);
319                 g_slist_free(copylist);
320         }
321
322         folder_item_update_thaw();
323 }
324
325 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
326 {
327         gchar *path, *file;
328
329         g_return_val_if_fail(msginfo != NULL, NULL);
330
331         if (msginfo->plaintext_file)
332                 file = g_strdup(msginfo->plaintext_file);
333         else {
334                 path = folder_item_get_path(msginfo->folder);
335                 file = g_strconcat(path, G_DIR_SEPARATOR_S,
336                                    itos(msginfo->msgnum), NULL);
337                 g_free(path);
338         }
339
340         return file;
341 }
342
343 gchar *procmsg_get_message_file(MsgInfo *msginfo)
344 {
345         gchar *filename = NULL;
346
347         g_return_val_if_fail(msginfo != NULL, NULL);
348
349         filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
350         if (!filename)
351                 g_warning("can't fetch message %d\n", msginfo->msgnum);
352
353         return filename;
354 }
355
356 FILE *procmsg_open_message(MsgInfo *msginfo)
357 {
358         FILE *fp;
359         gchar *file;
360
361         g_return_val_if_fail(msginfo != NULL, NULL);
362
363         file = procmsg_get_message_file_path(msginfo);
364         g_return_val_if_fail(file != NULL, NULL);
365
366         if (!is_file_exist(file)) {
367                 g_free(file);
368                 file = procmsg_get_message_file(msginfo);
369                 g_return_val_if_fail(file != NULL, NULL);
370         }
371
372         if ((fp = fopen(file, "rb")) == NULL) {
373                 FILE_OP_ERROR(file, "fopen");
374                 g_free(file);
375                 return NULL;
376         }
377
378         g_free(file);
379
380         if (MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags)) {
381                 gchar buf[BUFFSIZE];
382
383                 while (fgets(buf, sizeof(buf), fp) != NULL)
384                         if (buf[0] == '\r' || buf[0] == '\n') break;
385         }
386
387         return fp;
388 }
389
390 #if USE_GPGME
391 FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
392 {
393         FILE *fp;
394         MimeInfo *mimeinfo_;
395         glong fpos;
396
397         g_return_val_if_fail(msginfo != NULL, NULL);
398
399         if (mimeinfo) *mimeinfo = NULL;
400
401         if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
402
403         mimeinfo_ = procmime_scan_mime_header(fp);
404         if (!mimeinfo_) {
405                 fclose(fp);
406                 return NULL;
407         }
408
409         if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
410             rfc2015_is_encrypted(mimeinfo_)) {
411                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
412         }
413
414         if (MSG_IS_ENCRYPTED(msginfo->flags) &&
415             !msginfo->plaintext_file &&
416             !msginfo->decryption_failed) {
417                 fpos = ftell(fp);
418                 rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
419                 if (msginfo->plaintext_file &&
420                     !msginfo->decryption_failed) {
421                         fclose(fp);
422                         procmime_mimeinfo_free_all(mimeinfo_);
423                         if ((fp = procmsg_open_message(msginfo)) == NULL)
424                                 return NULL;
425                         mimeinfo_ = procmime_scan_mime_header(fp);
426                         if (!mimeinfo_) {
427                                 fclose(fp);
428                                 return NULL;
429                         }
430                 } else {
431                         if (fseek(fp, fpos, SEEK_SET) < 0)
432                                 perror("fseek");
433                 }
434         }
435
436         if (mimeinfo) *mimeinfo = mimeinfo_;
437         return fp;
438 }
439 #endif
440
441 gboolean procmsg_msg_exist(MsgInfo *msginfo)
442 {
443         gchar *path;
444         gboolean ret;
445
446         if (!msginfo) return FALSE;
447
448         path = folder_item_get_path(msginfo->folder);
449         change_dir(path);
450         ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
451         g_free(path);
452
453         return ret;
454 }
455
456 void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
457                                 PrefsFilterType type)
458 {
459         static HeaderEntry hentry[] = {{"X-BeenThere:",    NULL, TRUE},
460                                        {"X-ML-Name:",      NULL, TRUE},
461                                        {"X-List:",         NULL, TRUE},
462                                        {"X-Mailing-list:", NULL, TRUE},
463                                        {"List-Id:",        NULL, TRUE},
464                                        {"X-Sequence:",     NULL, TRUE},
465                                        {NULL,              NULL, FALSE}};
466         enum
467         {
468                 H_X_BEENTHERE    = 0,
469                 H_X_ML_NAME      = 1,
470                 H_X_LIST         = 2,
471                 H_X_MAILING_LIST = 3,
472                 H_LIST_ID        = 4,
473                 H_X_SEQUENCE     = 5
474         };
475
476         FILE *fp;
477
478         g_return_if_fail(msginfo != NULL);
479         g_return_if_fail(header != NULL);
480         g_return_if_fail(key != NULL);
481
482         *header = NULL;
483         *key = NULL;
484
485         switch (type) {
486         case FILTER_BY_NONE:
487                 return;
488         case FILTER_BY_AUTO:
489                 if ((fp = procmsg_open_message(msginfo)) == NULL)
490                         return;
491                 procheader_get_header_fields(fp, hentry);
492                 fclose(fp);
493
494 #define SET_FILTER_KEY(hstr, idx)       \
495 {                                       \
496         *header = g_strdup(hstr);       \
497         *key = hentry[idx].body;        \
498         hentry[idx].body = NULL;        \
499 }
500
501                 if (hentry[H_X_BEENTHERE].body != NULL) {
502                         SET_FILTER_KEY("header \"X-BeenThere\"", H_X_BEENTHERE);
503                 } else if (hentry[H_X_ML_NAME].body != NULL) {
504                         SET_FILTER_KEY("header \"X-ML-Name\"", H_X_ML_NAME);
505                 } else if (hentry[H_X_LIST].body != NULL) {
506                         SET_FILTER_KEY("header \"X-List\"", H_X_LIST);
507                 } else if (hentry[H_X_MAILING_LIST].body != NULL) {
508                         SET_FILTER_KEY("header \"X-Mailing-List\"", H_X_MAILING_LIST);
509                 } else if (hentry[H_LIST_ID].body != NULL) {
510                         SET_FILTER_KEY("header \"List-Id\"", H_LIST_ID);
511                         extract_list_id_str(*key);
512                 } else if (hentry[H_X_SEQUENCE].body != NULL) {
513                         gchar *p;
514
515                         SET_FILTER_KEY("X-Sequence", H_X_SEQUENCE);
516                         p = *key;
517                         while (*p != '\0') {
518                                 while (*p != '\0' && !isspace(*p)) p++;
519                                 while (isspace(*p)) p++;
520                                 if (isdigit(*p)) {
521                                         *p = '\0';
522                                         break;
523                                 }
524                         }
525                         g_strstrip(*key);
526                 } else if (msginfo->subject) {
527                         *header = g_strdup("subject");
528                         *key = g_strdup(msginfo->subject);
529                 }
530
531 #undef SET_FILTER_KEY
532
533                 g_free(hentry[H_X_BEENTHERE].body);
534                 hentry[H_X_BEENTHERE].body = NULL;
535                 g_free(hentry[H_X_ML_NAME].body);
536                 hentry[H_X_ML_NAME].body = NULL;
537                 g_free(hentry[H_X_LIST].body);
538                 hentry[H_X_LIST].body = NULL;
539                 g_free(hentry[H_X_MAILING_LIST].body);
540                 hentry[H_X_MAILING_LIST].body = NULL;
541                 g_free(hentry[H_LIST_ID].body);
542                 hentry[H_LIST_ID].body = NULL;
543
544                 break;
545         case FILTER_BY_FROM:
546                 *header = g_strdup("from");
547                 *key = g_strdup(msginfo->from);
548                 break;
549         case FILTER_BY_TO:
550                 *header = g_strdup("to");
551                 *key = g_strdup(msginfo->to);
552                 break;
553         case FILTER_BY_SUBJECT:
554                 *header = g_strdup("subject");
555                 *key = g_strdup(msginfo->subject);
556                 break;
557         default:
558                 break;
559         }
560 }
561
562 void procmsg_empty_trash(void)
563 {
564         FolderItem *trash;
565         GList *cur;
566
567         for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
568                 trash = FOLDER(cur->data)->trash;
569                 if (trash && trash->total_msgs > 0)
570                         folder_item_remove_all_msg(trash);
571         }
572 }
573
574 /*!
575  *\brief        Send messages in queue
576  *
577  *\param        queue Queue folder to process
578  *\param        save_msgs Unused
579  *
580  *\return       Number of messages sent, negative if an error occurred
581  *              positive if no error occurred
582  */
583 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
584 {
585         gint ret = 1, count = 0;
586         GSList *list, *elem;
587
588         if (!queue)
589                 queue = folder_get_default_queue();
590         g_return_val_if_fail(queue != NULL, -1);
591
592         folder_item_scan(queue);
593         list = folder_item_get_msg_list(queue);
594
595         for (elem = list; elem != NULL; elem = elem->next) {
596                 gchar *file;
597                 MsgInfo *msginfo;
598                 
599                 msginfo = (MsgInfo *)(elem->data);
600                 if (!MSG_IS_LOCKED(msginfo->flags)) {
601                         file = folder_item_fetch_msg(queue, msginfo->msgnum);
602                         if (file) {
603                                 if (procmsg_send_message_queue(file) < 0) {
604                                         g_warning("Sending queued message %d failed.\n", 
605                                                   msginfo->msgnum);
606                                         ret = -1;
607                                 } else {
608                                         /* CLAWS: 
609                                          * We save in procmsg_send_message_queue because
610                                          * we need the destination folder from the queue
611                                          * header
612                                                         
613                                         if (save_msgs)
614                                                 procmsg_save_to_outbox
615                                                         (queue->folder->outbox,
616                                                          file, TRUE);
617                                          */
618                                         count++; 
619                                         folder_item_remove_msg(queue, msginfo->msgnum);
620                                 }
621                                 g_free(file);
622                         }
623                 }
624                 /* FIXME: supposedly if only one message is locked, and queue
625                  * is being flushed, the following free says something like 
626                  * "freeing msg ## in folder (nil)". */
627                 procmsg_msginfo_free(msginfo);
628         }
629
630         return ret * count;
631 }
632
633 gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
634 {
635         FILE *fp, *outfp;
636         gchar buf[BUFFSIZE];
637         
638         if ((fp = fopen(in, "rb")) == NULL) {
639                 FILE_OP_ERROR(in, "fopen");
640                 return -1;
641         }
642         if ((outfp = fopen(out, "wb")) == NULL) {
643                 FILE_OP_ERROR(out, "fopen");
644                 fclose(fp);
645                 return -1;
646         }
647         while (fgets(buf, sizeof(buf), fp) != NULL)
648                 if (buf[0] == '\r' || buf[0] == '\n') break;
649         while (fgets(buf, sizeof(buf), fp) != NULL)
650                 fputs(buf, outfp);
651         fclose(outfp);
652         fclose(fp);
653         return 0;
654
655 }
656 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
657                             gboolean is_queued)
658 {
659         gint num;
660         MsgInfo *msginfo;
661
662         debug_print("saving sent message...\n");
663
664         if (!outbox)
665                 outbox = folder_get_default_outbox();
666         g_return_val_if_fail(outbox != NULL, -1);
667
668         /* remove queueing headers */
669         if (is_queued) {
670                 gchar tmp[MAXPATHLEN + 1];
671
672                 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
673                            get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
674                 
675                 if (procmsg_remove_special_headers(file, tmp) !=0)
676                         return -1;
677
678                 folder_item_scan(outbox);
679                 if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
680                         g_warning("can't save message\n");
681                         unlink(tmp);
682                         return -1;
683                 }
684         } else {
685                 folder_item_scan(outbox);
686                 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
687                         g_warning("can't save message\n");
688                         return -1;
689                 }
690                 return -1;
691         }
692         msginfo = folder_item_get_msginfo(outbox, num);
693         if (msginfo != NULL) {
694             procmsg_msginfo_unset_flags(msginfo, ~0, 0);
695             procmsg_msginfo_free(msginfo);
696         }
697         folder_item_update(outbox, TRUE);
698
699         return 0;
700 }
701
702 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
703 {
704         static const gchar *def_cmd = "lpr %s";
705         static guint id = 0;
706         gchar *prtmp;
707         FILE *tmpfp, *prfp;
708         gchar buf[1024];
709         gchar *p;
710
711         g_return_if_fail(msginfo);
712
713         if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
714                 g_warning("Can't get text part\n");
715                 return;
716         }
717
718         prtmp = g_strdup_printf("%s%cprinttmp.%08x",
719                                 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
720
721         if ((prfp = fopen(prtmp, "wb")) == NULL) {
722                 FILE_OP_ERROR(prtmp, "fopen");
723                 g_free(prtmp);
724                 fclose(tmpfp);
725                 return;
726         }
727
728         if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
729         if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
730         if (msginfo->to)   fprintf(prfp, "To: %s\n", msginfo->to);
731         if (msginfo->cc)   fprintf(prfp, "Cc: %s\n", msginfo->cc);
732         if (msginfo->newsgroups)
733                 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
734         if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
735         fputc('\n', prfp);
736
737         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
738                 fputs(buf, prfp);
739
740         fclose(prfp);
741         fclose(tmpfp);
742
743         if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
744             !strchr(p + 2, '%'))
745                 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
746         else {
747                 if (cmdline)
748                         g_warning("Print command line is invalid: `%s'\n",
749                                   cmdline);
750                 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
751         }
752
753         g_free(prtmp);
754
755         g_strchomp(buf);
756         if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
757         system(buf);
758 }
759
760 MsgInfo *procmsg_msginfo_new_ref(MsgInfo *msginfo)
761 {
762         msginfo->refcnt++;
763         
764         return msginfo;
765 }
766
767 MsgInfo *procmsg_msginfo_new(void)
768 {
769         MsgInfo *newmsginfo;
770
771         newmsginfo = g_new0(MsgInfo, 1);
772         newmsginfo->refcnt = 1;
773         
774         return newmsginfo;
775 }
776
777 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
778 {
779         MsgInfo *newmsginfo;
780
781         if (msginfo == NULL) return NULL;
782
783         newmsginfo = g_new0(MsgInfo, 1);
784
785         newmsginfo->refcnt = 1;
786
787 #define MEMBCOPY(mmb)   newmsginfo->mmb = msginfo->mmb
788 #define MEMBDUP(mmb)    newmsginfo->mmb = msginfo->mmb ? \
789                         g_strdup(msginfo->mmb) : NULL
790
791         MEMBCOPY(msgnum);
792         MEMBCOPY(size);
793         MEMBCOPY(mtime);
794         MEMBCOPY(date_t);
795         MEMBCOPY(flags);
796
797         MEMBDUP(fromname);
798
799         MEMBDUP(date);
800         MEMBDUP(from);
801         MEMBDUP(to);
802         MEMBDUP(cc);
803         MEMBDUP(newsgroups);
804         MEMBDUP(subject);
805         MEMBDUP(msgid);
806         MEMBDUP(inreplyto);
807         MEMBDUP(xref);
808
809         MEMBCOPY(folder);
810         MEMBCOPY(to_folder);
811
812         MEMBDUP(xface);
813         MEMBDUP(dispositionnotificationto);
814         MEMBDUP(returnreceiptto);
815         MEMBDUP(references);
816
817         MEMBCOPY(score);
818         MEMBCOPY(threadscore);
819
820         return newmsginfo;
821 }
822
823 MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
824 {
825         MsgInfo *full_msginfo;
826         gchar *file;
827
828         if (msginfo == NULL) return NULL;
829
830         file = procmsg_get_message_file(msginfo);
831         if (!file) {
832                 g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
833                 return NULL;
834         }
835
836         full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE, FALSE);
837         g_free(file);
838         if (!full_msginfo) return NULL;
839
840         full_msginfo->msgnum = msginfo->msgnum;
841         full_msginfo->size = msginfo->size;
842         full_msginfo->mtime = msginfo->mtime;
843         full_msginfo->folder = msginfo->folder;
844 #if USE_GPGME
845         full_msginfo->plaintext_file = g_strdup(msginfo->plaintext_file);
846         full_msginfo->decryption_failed = msginfo->decryption_failed;
847 #endif
848         procmsg_msginfo_set_to_folder(full_msginfo, msginfo->to_folder);
849
850         return full_msginfo;
851 }
852
853 void procmsg_msginfo_free(MsgInfo *msginfo)
854 {
855         if (msginfo == NULL) return;
856
857         msginfo->refcnt--;
858         if (msginfo->refcnt > 0)
859                 return;
860
861         debug_print("freeing msginfo %d in %s\n", msginfo->msgnum, msginfo->folder ? msginfo->folder->path : "(nil)");
862
863         if (msginfo->to_folder) {
864                 msginfo->to_folder->op_count--;
865                 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
866         }
867
868         g_free(msginfo->fromspace);
869         g_free(msginfo->references);
870         g_free(msginfo->returnreceiptto);
871         g_free(msginfo->dispositionnotificationto);
872         g_free(msginfo->xface);
873
874         g_free(msginfo->fromname);
875
876         g_free(msginfo->date);
877         g_free(msginfo->from);
878         g_free(msginfo->to);
879         g_free(msginfo->cc);
880         g_free(msginfo->newsgroups);
881         g_free(msginfo->subject);
882         g_free(msginfo->msgid);
883         g_free(msginfo->inreplyto);
884         g_free(msginfo->xref);
885
886         g_free(msginfo);
887 }
888
889 guint procmsg_msginfo_memusage(MsgInfo *msginfo)
890 {
891         guint memusage = 0;
892         
893         memusage += sizeof(MsgInfo);
894         if (msginfo->fromname)
895                 memusage += strlen(msginfo->fromname);
896         if (msginfo->date)
897                 memusage += strlen(msginfo->date);
898         if (msginfo->from)
899                 memusage += strlen(msginfo->from);
900         if (msginfo->to)
901                 memusage += strlen(msginfo->to);
902         if (msginfo->cc)
903                 memusage += strlen(msginfo->cc);
904         if (msginfo->newsgroups)
905                 memusage += strlen(msginfo->newsgroups);
906         if (msginfo->subject)
907                 memusage += strlen(msginfo->subject);
908         if (msginfo->msgid)
909                 memusage += strlen(msginfo->msgid);
910         if (msginfo->inreplyto)
911                 memusage += strlen(msginfo->inreplyto);
912         if (msginfo->xface)
913                 memusage += strlen(msginfo->xface);
914         if (msginfo->dispositionnotificationto)
915                 memusage += strlen(msginfo->dispositionnotificationto);
916         if (msginfo->returnreceiptto)
917                 memusage += strlen(msginfo->returnreceiptto);
918         if (msginfo->references)
919                 memusage += strlen(msginfo->references);
920         if (msginfo->fromspace)
921                 memusage += strlen(msginfo->fromspace);
922
923         return memusage;
924 }
925
926 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
927 {
928         const MsgInfo *msginfo1 = a;
929         const MsgInfo *msginfo2 = b;
930
931         if (!msginfo1)
932                 return -1;
933         if (!msginfo2)
934                 return -1;
935
936         return msginfo1->msgnum - msginfo2->msgnum;
937 }
938
939 enum
940 {
941         Q_SENDER           = 0,
942         Q_SMTPSERVER       = 1,
943         Q_RECIPIENTS       = 2,
944         Q_NEWSGROUPS       = 3,
945         Q_MAIL_ACCOUNT_ID  = 4,
946         Q_NEWS_ACCOUNT_ID  = 5,
947         Q_SAVE_COPY_FOLDER = 6,
948         Q_REPLY_MESSAGE_ID = 7,
949         Q_FWD_MESSAGE_ID   = 8
950 };
951
952 gint procmsg_send_message_queue(const gchar *file)
953 {
954         static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
955                                        {"SSV:",  NULL, FALSE},
956                                        {"R:",    NULL, FALSE},
957                                        {"NG:",   NULL, FALSE},
958                                        {"MAID:", NULL, FALSE},
959                                        {"NAID:", NULL, FALSE},
960                                        {"SCF:",  NULL, FALSE},
961                                        {"RMID:", NULL, FALSE},
962                                        {"FMID:", NULL, FALSE},
963                                        {NULL,    NULL, FALSE}};
964         FILE *fp;
965         gint filepos;
966         gint mailval = 0, newsval = 0;
967         gchar *from = NULL;
968         gchar *smtpserver = NULL;
969         GSList *to_list = NULL;
970         GSList *newsgroup_list = NULL;
971         gchar *savecopyfolder = NULL;
972         gchar *replymessageid = NULL;
973         gchar *fwdmessageid = NULL;
974         gchar buf[BUFFSIZE];
975         gint hnum;
976         PrefsAccount *mailac = NULL, *newsac = NULL;
977         int local = 0;
978
979         g_return_val_if_fail(file != NULL, -1);
980
981         if ((fp = fopen(file, "rb")) == NULL) {
982                 FILE_OP_ERROR(file, "fopen");
983                 return -1;
984         }
985
986         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
987                != -1) {
988                 gchar *p = buf + strlen(qentry[hnum].name);
989
990                 switch (hnum) {
991                 case Q_SENDER:
992                         if (!from) from = g_strdup(p);
993                         break;
994                 case Q_SMTPSERVER:
995                         if (!smtpserver) smtpserver = g_strdup(p);
996                         break;
997                 case Q_RECIPIENTS:
998                         to_list = address_list_append(to_list, p);
999                         break;
1000                 case Q_NEWSGROUPS:
1001                         newsgroup_list = newsgroup_list_append(newsgroup_list, p);
1002                         break;
1003                 case Q_MAIL_ACCOUNT_ID:
1004                         mailac = account_find_from_id(atoi(p));
1005                         break;
1006                 case Q_NEWS_ACCOUNT_ID:
1007                         newsac = account_find_from_id(atoi(p));
1008                         break;
1009                 case Q_SAVE_COPY_FOLDER:
1010                         if (!savecopyfolder) savecopyfolder = g_strdup(p);
1011                         break;
1012                 case Q_REPLY_MESSAGE_ID:
1013                         if (!replymessageid) replymessageid = g_strdup(p);
1014                         break;
1015                 case Q_FWD_MESSAGE_ID:
1016                         if (!fwdmessageid) fwdmessageid = g_strdup(p);
1017                         break;
1018                 }
1019         }
1020         filepos = ftell(fp);
1021
1022         if (to_list) {
1023                 debug_print("Sending message by mail\n");
1024                 if (!from) {
1025                         g_warning("Queued message header is broken.\n");
1026                         mailval = -1;
1027                 } else if (mailac && mailac->use_mail_command &&
1028                            mailac->mail_command && (* mailac->mail_command)) {
1029                         mailval = send_message_local(mailac->mail_command, fp);
1030                         local = 1;
1031                 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
1032                         mailval = send_message_local(prefs_common.extsend_cmd, fp);
1033                         local = 1;
1034                 } else {
1035                         if (!mailac) {
1036                                 mailac = account_find_from_smtp_server(from, smtpserver);
1037                                 if (!mailac) {
1038                                         g_warning("Account not found. "
1039                                                     "Using current account...\n");
1040                                         mailac = cur_account;
1041                                 }
1042                         }
1043
1044                         if (mailac)
1045                                 mailval = send_message_smtp(mailac, to_list, fp);
1046                         else {
1047                                 PrefsAccount tmp_ac;
1048
1049                                 g_warning("Account not found.\n");
1050
1051                                 memset(&tmp_ac, 0, sizeof(PrefsAccount));
1052                                 tmp_ac.address = from;
1053                                 tmp_ac.smtp_server = smtpserver;
1054                                 tmp_ac.smtpport = SMTP_PORT;
1055                                 mailval = send_message_smtp(&tmp_ac, to_list, fp);
1056                         }
1057                 }
1058         }
1059
1060         fseek(fp, filepos, SEEK_SET);
1061         if (newsgroup_list && (newsval == 0)) {
1062                 Folder *folder;
1063                 gchar *tmp = NULL;
1064                 FILE *tmpfp;
1065
1066                 /* write to temporary file */
1067                 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1068                             G_DIR_SEPARATOR, (gint)file);
1069                 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
1070                         FILE_OP_ERROR(tmp, "fopen");
1071                         newsval = -1;
1072                         alertpanel_error(_("Could not create temporary file for news sending."));
1073                 } else {
1074                         if (change_file_mode_rw(tmpfp, tmp) < 0) {
1075                                 FILE_OP_ERROR(tmp, "chmod");
1076                                 g_warning("can't change file mode\n");
1077                         }
1078
1079                         while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
1080                                 if (fputs(buf, tmpfp) == EOF) {
1081                                         FILE_OP_ERROR(tmp, "fputs");
1082                                         newsval = -1;
1083                                         alertpanel_error(_("Error when writing temporary file for news sending."));
1084                                 }
1085                         }
1086                         fclose(tmpfp);
1087
1088                         if (newsval == 0) {
1089                                 debug_print("Sending message by news\n");
1090
1091                                 folder = FOLDER(newsac->folder);
1092
1093                                 newsval = news_post(folder, tmp);
1094                                 if (newsval < 0) {
1095                                         alertpanel_error(_("Error occurred while posting the message to %s ."),
1096                                                  newsac->nntp_server);
1097                                 }
1098                         }
1099                         unlink(tmp);
1100                 }
1101                 g_free(tmp);
1102         }
1103
1104         slist_free_strings(to_list);
1105         g_slist_free(to_list);
1106         slist_free_strings(newsgroup_list);
1107         g_slist_free(newsgroup_list);
1108         g_free(from);
1109         g_free(smtpserver);
1110         fclose(fp);
1111
1112         /* save message to outbox */
1113         if (mailval == 0 && newsval == 0 && savecopyfolder) {
1114                 FolderItem *outbox;
1115
1116                 debug_print("saving sent message...\n");
1117
1118                 outbox = folder_find_item_from_identifier(savecopyfolder);
1119                 if (!outbox)
1120                         outbox = folder_get_default_outbox();
1121
1122                 procmsg_save_to_outbox(outbox, file, TRUE);
1123         }
1124
1125         if (replymessageid != NULL || fwdmessageid != NULL) {
1126                 gchar **tokens;
1127                 FolderItem *item;
1128                 
1129                 if (replymessageid != NULL)
1130                         tokens = g_strsplit(replymessageid, "\x7f", 0);
1131                 else
1132                         tokens = g_strsplit(fwdmessageid, "\x7f", 0);
1133                 item = folder_find_item_from_identifier(tokens[0]);
1134                 if (item != NULL) {
1135                         MsgInfo *msginfo;
1136                         
1137                         msginfo = folder_item_get_msginfo(item, atoi(tokens[1]));
1138                         
1139                         /*!< note that if the message has no msgid (maybe it was invalid), 
1140                         * we also refuse to do something with the reply to flag */
1141                         if ((msginfo != NULL) && 
1142                             (msginfo->msgid != NULL) &&
1143                             (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1144                                 procmsg_msginfo_free(msginfo);
1145                                 msginfo = NULL;
1146                         }
1147                         
1148                         if (msginfo == NULL) {
1149                                 msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]);
1150                         }
1151                         
1152                         if (msginfo != NULL) {
1153                                 if (replymessageid != NULL) {
1154                                         procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1155                                         procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1156                                 } 
1157                                 else {
1158                                         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
1159                                         procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0);
1160                                 }
1161                                 procmsg_msginfo_free(msginfo);
1162                         }
1163                 }
1164                 g_strfreev(tokens);
1165         }
1166
1167         g_free(savecopyfolder);
1168         g_free(replymessageid);
1169         g_free(fwdmessageid);
1170         
1171         return (newsval != 0 ? newsval : mailval);
1172 }
1173
1174 static void update_folder_msg_counts(FolderItem *item, MsgInfo *msginfo, MsgPermFlags old_flags)
1175 {
1176         MsgPermFlags new_flags = msginfo->flags.perm_flags;
1177
1178         /* NEW flag */
1179         if (!(old_flags & MSG_NEW) && (new_flags & MSG_NEW)) {
1180                 item->new_msgs++;
1181         }
1182
1183         if ((old_flags & MSG_NEW) && !(new_flags & MSG_NEW)) {
1184                 item->new_msgs--;
1185         }
1186
1187         /* UNREAD flag */
1188         if (!(old_flags & MSG_UNREAD) && (new_flags & MSG_UNREAD)) {
1189                 item->unread_msgs++;
1190                 if (procmsg_msg_has_marked_parent(msginfo))
1191                         item->unreadmarked_msgs++;
1192         }
1193
1194         if ((old_flags & MSG_UNREAD) && !(new_flags & MSG_UNREAD)) {
1195                 item->unread_msgs--;
1196                 if (procmsg_msg_has_marked_parent(msginfo))
1197                         item->unreadmarked_msgs--;
1198         }
1199         
1200         /* MARK flag */
1201         if (!(old_flags & MSG_MARKED) && (new_flags & MSG_MARKED)) {
1202                 procmsg_update_unread_children(msginfo, TRUE);
1203         }
1204
1205         if ((old_flags & MSG_MARKED) && !(new_flags & MSG_MARKED)) {
1206                 procmsg_update_unread_children(msginfo, FALSE);
1207         }
1208 }
1209
1210 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1211 {
1212         FolderItem *item;
1213         MsgInfoUpdate msginfo_update;
1214         MsgPermFlags perm_flags_new, perm_flags_old;
1215
1216         g_return_if_fail(msginfo != NULL);
1217         item = msginfo->folder;
1218         g_return_if_fail(item != NULL);
1219         
1220         debug_print("Setting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1221
1222         /* Perm Flags handling */
1223         perm_flags_old = msginfo->flags.perm_flags;
1224         perm_flags_new = msginfo->flags.perm_flags | perm_flags;
1225         if ((perm_flags & MSG_IGNORE_THREAD) || (perm_flags_old & MSG_IGNORE_THREAD)) {
1226                 perm_flags_new &= ~(MSG_NEW | MSG_UNREAD);
1227         }
1228
1229         if (perm_flags_old != perm_flags_new) {
1230                 folder_item_change_msg_flags(msginfo->folder, msginfo, perm_flags_new);
1231
1232                 update_folder_msg_counts(item, msginfo, perm_flags_old);
1233
1234                 msginfo_update.msginfo = msginfo;
1235                 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1236                 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1237         }
1238
1239         /* Tmp flags hanlding */
1240         msginfo->flags.tmp_flags |= tmp_flags;
1241 }
1242
1243 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1244 {
1245         FolderItem *item;
1246         MsgInfoUpdate msginfo_update;
1247         MsgPermFlags perm_flags_new, perm_flags_old;
1248
1249         g_return_if_fail(msginfo != NULL);
1250         item = msginfo->folder;
1251         g_return_if_fail(item != NULL);
1252         
1253         debug_print("Unsetting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1254
1255         /* Perm Flags handling */
1256         perm_flags_old = msginfo->flags.perm_flags;
1257         perm_flags_new = msginfo->flags.perm_flags & ~perm_flags;
1258         
1259         if (perm_flags_old != perm_flags_new) {
1260                 folder_item_change_msg_flags(msginfo->folder, msginfo, perm_flags_new);
1261
1262                 update_folder_msg_counts(item, msginfo, perm_flags_old);
1263
1264                 msginfo_update.msginfo = msginfo;
1265                 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1266                 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1267         }
1268
1269         /* Tmp flags hanlding */
1270         msginfo->flags.tmp_flags &= ~tmp_flags;
1271 }
1272
1273 /*!
1274  *\brief        check for flags (e.g. mark) in prior msgs of current thread
1275  *
1276  *\param        info Current message
1277  *\param        perm_flags Flags to be checked
1278  *\param        parentmsgs Hash of prior msgs to avoid loops
1279  *
1280  *\return       gboolean TRUE if perm_flags are found
1281  */
1282 gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
1283                 MsgPermFlags perm_flags, GHashTable *parentmsgs)
1284 {
1285         MsgInfo *tmp;
1286
1287         g_return_val_if_fail(info != NULL, FALSE);
1288
1289         if (info != NULL && info->folder != NULL && info->inreplyto != NULL) {
1290                 tmp = folder_item_get_msginfo_by_msgid(info->folder,
1291                                 info->inreplyto);
1292                 if (tmp && (tmp->flags.perm_flags & perm_flags)) {
1293                         procmsg_msginfo_free(tmp);
1294                         return TRUE;
1295                 } else if (tmp != NULL) {
1296                         gboolean result;
1297
1298                         if (g_hash_table_lookup(parentmsgs, info)) {
1299                                 debug_print("loop detected: %s%c%d\n",
1300                                         folder_item_get_path(info->folder),
1301                                         G_DIR_SEPARATOR, info->msgnum);
1302                                 result = FALSE;
1303                         } else {
1304                                 g_hash_table_insert(parentmsgs, info, "1");
1305                                 result = procmsg_msg_has_flagged_parent_real(
1306                                     tmp, perm_flags, parentmsgs);
1307                         }
1308                         procmsg_msginfo_free(tmp);
1309                         return result;
1310                 } else {
1311                         return FALSE;
1312                 }
1313         } else
1314                 return FALSE;
1315 }
1316
1317 /*!
1318  *\brief        Callback for cleaning up hash of parentmsgs
1319  */
1320 gboolean parentmsgs_hash_remove(gpointer key,
1321                             gpointer value,
1322                             gpointer user_data)
1323 {
1324         return TRUE;
1325 }
1326
1327 /*!
1328  *\brief        Set up list of parentmsgs
1329  *              See procmsg_msg_has_flagged_parent_real()
1330  */
1331 gboolean procmsg_msg_has_flagged_parent(MsgInfo *info, MsgPermFlags perm_flags)
1332 {
1333         gboolean result;
1334         GHashTable *parentmsgs = g_hash_table_new(NULL, NULL); 
1335
1336         result = procmsg_msg_has_flagged_parent_real(info, perm_flags, parentmsgs);
1337         g_hash_table_foreach_remove(parentmsgs, parentmsgs_hash_remove, NULL);
1338         g_hash_table_destroy(parentmsgs);
1339         return result;
1340 }
1341
1342 /*!
1343  *\brief        Check if msgs prior in thread are marked
1344  *              See procmsg_msg_has_flagged_parent_real()
1345  */
1346 gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
1347 {
1348         return procmsg_msg_has_flagged_parent(info, MSG_MARKED);
1349 }
1350
1351
1352 GSList *procmsg_find_children_func(MsgInfo *info, 
1353                                    GSList *children, GSList *all)
1354 {
1355         GSList *cur;
1356
1357         g_return_val_if_fail(info!=NULL, children);
1358         if (info->msgid == NULL)
1359                 return children;
1360
1361         for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1362                 MsgInfo *tmp = (MsgInfo *)cur->data;
1363                 if (tmp->inreplyto && !strcmp(tmp->inreplyto, info->msgid)) {
1364                         /* Check if message is already in the list */
1365                         if ((children == NULL) || 
1366                             (g_slist_index(children, tmp) == -1)) {
1367                                 children = g_slist_prepend(children,
1368                                                 procmsg_msginfo_new_ref(tmp));
1369                                 children = procmsg_find_children_func(tmp, 
1370                                                         children, 
1371                                                         all);
1372                         }
1373                 }
1374         }
1375         return children;
1376 }
1377
1378 GSList *procmsg_find_children (MsgInfo *info)
1379 {
1380         GSList *children;
1381         GSList *all, *cur;
1382
1383         g_return_val_if_fail(info!=NULL, NULL);
1384         all = folder_item_get_msg_list(info->folder);
1385         children = procmsg_find_children_func(info, NULL, all);
1386         if (children != NULL) {
1387                 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1388                         /* this will not free the used pointers
1389                            created with procmsg_msginfo_new_ref */
1390                         procmsg_msginfo_free((MsgInfo *)cur->data);
1391                 }
1392         }
1393         g_slist_free(all);
1394
1395         return children;
1396 }
1397
1398 void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
1399 {
1400         GSList *children = procmsg_find_children(info);
1401         GSList *cur;
1402         for (cur = children; cur != NULL; cur = g_slist_next(cur)) {
1403                 MsgInfo *tmp = (MsgInfo *)cur->data;
1404                 if(MSG_IS_UNREAD(tmp->flags) && !MSG_IS_IGNORE_THREAD(tmp->flags)) {
1405                         if(newly_marked) 
1406                                 info->folder->unreadmarked_msgs++;
1407                         else
1408                                 info->folder->unreadmarked_msgs--;
1409                         folder_item_update(info->folder, F_ITEM_UPDATE_MSGCNT);
1410                 }
1411                 procmsg_msginfo_free(tmp);
1412         }
1413         g_slist_free(children);
1414 }
1415
1416 /**
1417  * Set the destination folder for a copy or move operation
1418  *
1419  * \param msginfo The message which's destination folder is changed
1420  * \param to_folder The destination folder for the operation
1421  */
1422 void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
1423 {
1424         if(msginfo->to_folder != NULL) {
1425                 msginfo->to_folder->op_count--;
1426                 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1427         }
1428         msginfo->to_folder = to_folder;
1429         if(to_folder != NULL) {
1430                 to_folder->op_count++;
1431                 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1432         }
1433 }
1434
1435 /**
1436  * Apply filtering actions to the msginfo
1437  *
1438  * \param msginfo The MsgInfo describing the message that should be filtered
1439  * \return TRUE if the message was moved and MsgInfo is now invalid,
1440  *         FALSE otherwise
1441  */
1442 gboolean procmsg_msginfo_filter(MsgInfo *msginfo)
1443 {
1444         MailFilteringData mail_filtering_data;
1445                         
1446         mail_filtering_data.msginfo = msginfo;                  
1447         if (hooks_invoke(MAIL_FILTERING_HOOKLIST, &mail_filtering_data))
1448                 return TRUE;
1449
1450         /* filter if enabled in prefs or move to inbox if not */
1451         if((global_processing != NULL) &&
1452            filter_message_by_msginfo(global_processing, msginfo))
1453                 return TRUE;
1454
1455         return FALSE;
1456 }