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