4515dfa681570d72b75190058b3291c64b9801cc
[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         glong fpos;
404
405         g_return_val_if_fail(msginfo != NULL, NULL);
406
407         if (mimeinfo) *mimeinfo = NULL;
408
409         if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
410
411         mimeinfo_ = procmime_scan_mime_header(fp);
412         if (!mimeinfo_) {
413                 fclose(fp);
414                 return NULL;
415         }
416
417         if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
418             rfc2015_is_encrypted(mimeinfo_)) {
419                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
420         }
421
422         if (MSG_IS_ENCRYPTED(msginfo->flags) &&
423             !msginfo->plaintext_file &&
424             !msginfo->decryption_failed) {
425                 fpos = ftell(fp);
426                 rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
427                 if (msginfo->plaintext_file &&
428                     !msginfo->decryption_failed) {
429                         fclose(fp);
430                         procmime_mimeinfo_free_all(mimeinfo_);
431                         if ((fp = procmsg_open_message(msginfo)) == NULL)
432                                 return NULL;
433                         mimeinfo_ = procmime_scan_mime_header(fp);
434                         if (!mimeinfo_) {
435                                 fclose(fp);
436                                 return NULL;
437                         }
438                 } else {
439                         if (fseek(fp, fpos, SEEK_SET) < 0)
440                                 perror("fseek");
441                 }
442         }
443
444         if (mimeinfo) *mimeinfo = mimeinfo_;
445         return fp;
446 }
447 #endif
448
449 gboolean procmsg_msg_exist(MsgInfo *msginfo)
450 {
451         gchar *path;
452         gboolean ret;
453
454         if (!msginfo) return FALSE;
455
456         path = folder_item_get_path(msginfo->folder);
457         change_dir(path);
458         ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
459         g_free(path);
460
461         return ret;
462 }
463
464 void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
465                                 PrefsFilterType type)
466 {
467         static HeaderEntry hentry[] = {{"X-BeenThere:",    NULL, TRUE},
468                                        {"X-ML-Name:",      NULL, TRUE},
469                                        {"X-List:",         NULL, TRUE},
470                                        {"X-Mailing-list:", NULL, TRUE},
471                                        {"List-Id:",        NULL, TRUE},
472                                        {"X-Sequence:",     NULL, TRUE},
473                                        {NULL,              NULL, FALSE}};
474         enum
475         {
476                 H_X_BEENTHERE    = 0,
477                 H_X_ML_NAME      = 1,
478                 H_X_LIST         = 2,
479                 H_X_MAILING_LIST = 3,
480                 H_LIST_ID        = 4,
481                 H_X_SEQUENCE     = 5
482         };
483
484         FILE *fp;
485
486         g_return_if_fail(msginfo != NULL);
487         g_return_if_fail(header != NULL);
488         g_return_if_fail(key != NULL);
489
490         *header = NULL;
491         *key = NULL;
492
493         switch (type) {
494         case FILTER_BY_NONE:
495                 return;
496         case FILTER_BY_AUTO:
497                 if ((fp = procmsg_open_message(msginfo)) == NULL)
498                         return;
499                 procheader_get_header_fields(fp, hentry);
500                 fclose(fp);
501
502 #define SET_FILTER_KEY(hstr, idx)       \
503 {                                       \
504         *header = g_strdup(hstr);       \
505         *key = hentry[idx].body;        \
506         hentry[idx].body = NULL;        \
507 }
508
509                 if (hentry[H_X_BEENTHERE].body != NULL) {
510                         SET_FILTER_KEY("header \"X-BeenThere\"", H_X_BEENTHERE);
511                 } else if (hentry[H_X_ML_NAME].body != NULL) {
512                         SET_FILTER_KEY("header \"X-ML-Name\"", H_X_ML_NAME);
513                 } else if (hentry[H_X_LIST].body != NULL) {
514                         SET_FILTER_KEY("header \"X-List\"", H_X_LIST);
515                 } else if (hentry[H_X_MAILING_LIST].body != NULL) {
516                         SET_FILTER_KEY("header \"X-Mailing-List\"", H_X_MAILING_LIST);
517                 } else if (hentry[H_LIST_ID].body != NULL) {
518                         SET_FILTER_KEY("header \"List-Id\"", H_LIST_ID);
519                         extract_list_id_str(*key);
520                 } else if (hentry[H_X_SEQUENCE].body != NULL) {
521                         gchar *p;
522
523                         SET_FILTER_KEY("X-Sequence", H_X_SEQUENCE);
524                         p = *key;
525                         while (*p != '\0') {
526                                 while (*p != '\0' && !isspace(*p)) p++;
527                                 while (isspace(*p)) p++;
528                                 if (isdigit(*p)) {
529                                         *p = '\0';
530                                         break;
531                                 }
532                         }
533                         g_strstrip(*key);
534                 } else if (msginfo->subject) {
535                         *header = g_strdup("subject");
536                         *key = g_strdup(msginfo->subject);
537                 }
538
539 #undef SET_FILTER_KEY
540
541                 g_free(hentry[H_X_BEENTHERE].body);
542                 hentry[H_X_BEENTHERE].body = NULL;
543                 g_free(hentry[H_X_ML_NAME].body);
544                 hentry[H_X_ML_NAME].body = NULL;
545                 g_free(hentry[H_X_LIST].body);
546                 hentry[H_X_LIST].body = NULL;
547                 g_free(hentry[H_X_MAILING_LIST].body);
548                 hentry[H_X_MAILING_LIST].body = NULL;
549                 g_free(hentry[H_LIST_ID].body);
550                 hentry[H_LIST_ID].body = NULL;
551
552                 break;
553         case FILTER_BY_FROM:
554                 *header = g_strdup("from");
555                 *key = g_strdup(msginfo->from);
556                 break;
557         case FILTER_BY_TO:
558                 *header = g_strdup("to");
559                 *key = g_strdup(msginfo->to);
560                 break;
561         case FILTER_BY_SUBJECT:
562                 *header = g_strdup("subject");
563                 *key = g_strdup(msginfo->subject);
564                 break;
565         default:
566                 break;
567         }
568 }
569
570 void procmsg_empty_trash(void)
571 {
572         FolderItem *trash;
573         GList *cur;
574
575         for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
576                 trash = FOLDER(cur->data)->trash;
577                 if (trash && trash->total_msgs > 0)
578                         folder_item_remove_all_msg(trash);
579         }
580 }
581
582 /*!
583  *\brief        Send messages in queue
584  *
585  *\param        queue Queue folder to process
586  *\param        save_msgs Unused
587  *
588  *\return       Number of messages sent, negative if an error occurred
589  *              positive if no error occurred
590  */
591 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
592 {
593         gint ret = 1, count = 0;
594         GSList *list, *elem;
595
596         if (!queue)
597                 queue = folder_get_default_queue();
598         g_return_val_if_fail(queue != NULL, -1);
599
600         folder_item_scan(queue);
601         list = folder_item_get_msg_list(queue);
602
603         for (elem = list; elem != NULL; elem = elem->next) {
604                 gchar *file;
605                 MsgInfo *msginfo;
606                 
607                 msginfo = (MsgInfo *)(elem->data);
608                 if (!MSG_IS_LOCKED(msginfo->flags)) {
609                         file = folder_item_fetch_msg(queue, msginfo->msgnum);
610                         if (file) {
611                                 if (procmsg_send_message_queue(file) < 0) {
612                                         g_warning("Sending queued message %d failed.\n", 
613                                                   msginfo->msgnum);
614                                         ret = -1;
615                                 } else {
616                                         /* CLAWS: 
617                                          * We save in procmsg_send_message_queue because
618                                          * we need the destination folder from the queue
619                                          * header
620                                                         
621                                         if (save_msgs)
622                                                 procmsg_save_to_outbox
623                                                         (queue->folder->outbox,
624                                                          file, TRUE);
625                                          */
626                                         count++; 
627                                         folder_item_remove_msg(queue, msginfo->msgnum);
628                                 }
629                                 g_free(file);
630                         }
631                 }
632                 /* FIXME: supposedly if only one message is locked, and queue
633                  * is being flushed, the following free says something like 
634                  * "freeing msg ## in folder (nil)". */
635                 procmsg_msginfo_free(msginfo);
636         }
637
638         return ret * count;
639 }
640
641 gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
642 {
643         FILE *fp, *outfp;
644         gchar buf[BUFFSIZE];
645         
646         if ((fp = fopen(in, "rb")) == NULL) {
647                 FILE_OP_ERROR(in, "fopen");
648                 return -1;
649         }
650         if ((outfp = fopen(out, "wb")) == NULL) {
651                 FILE_OP_ERROR(out, "fopen");
652                 fclose(fp);
653                 return -1;
654         }
655         while (fgets(buf, sizeof(buf), fp) != NULL)
656                 if (buf[0] == '\r' || buf[0] == '\n') break;
657         while (fgets(buf, sizeof(buf), fp) != NULL)
658                 fputs(buf, outfp);
659         fclose(outfp);
660         fclose(fp);
661         return 0;
662
663 }
664 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
665                             gboolean is_queued)
666 {
667         gint num;
668         MsgInfo *msginfo;
669
670         debug_print("saving sent message...\n");
671
672         if (!outbox)
673                 outbox = folder_get_default_outbox();
674         g_return_val_if_fail(outbox != NULL, -1);
675
676         /* remove queueing headers */
677         if (is_queued) {
678                 gchar tmp[MAXPATHLEN + 1];
679
680                 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
681                            get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
682                 
683                 if (procmsg_remove_special_headers(file, tmp) !=0)
684                         return -1;
685
686                 folder_item_scan(outbox);
687                 if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
688                         g_warning("can't save message\n");
689                         unlink(tmp);
690                         return -1;
691                 }
692         } else {
693                 folder_item_scan(outbox);
694                 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
695                         g_warning("can't save message\n");
696                         return -1;
697                 }
698                 return -1;
699         }
700         msginfo = folder_item_get_msginfo(outbox, num);
701         if (msginfo != NULL) {
702             procmsg_msginfo_unset_flags(msginfo, ~0, 0);
703             procmsg_msginfo_free(msginfo);
704         }
705         folder_item_update(outbox, TRUE);
706
707         return 0;
708 }
709
710 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
711 {
712         static const gchar *def_cmd = "lpr %s";
713         static guint id = 0;
714         gchar *prtmp;
715         FILE *tmpfp, *prfp;
716         gchar buf[1024];
717         gchar *p;
718
719         g_return_if_fail(msginfo);
720
721         if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
722                 g_warning("Can't get text part\n");
723                 return;
724         }
725
726         prtmp = g_strdup_printf("%s%cprinttmp.%08x",
727                                 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
728
729         if ((prfp = fopen(prtmp, "wb")) == NULL) {
730                 FILE_OP_ERROR(prtmp, "fopen");
731                 g_free(prtmp);
732                 fclose(tmpfp);
733                 return;
734         }
735
736         if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
737         if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
738         if (msginfo->to)   fprintf(prfp, "To: %s\n", msginfo->to);
739         if (msginfo->cc)   fprintf(prfp, "Cc: %s\n", msginfo->cc);
740         if (msginfo->newsgroups)
741                 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
742         if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
743         fputc('\n', prfp);
744
745         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
746                 fputs(buf, prfp);
747
748         fclose(prfp);
749         fclose(tmpfp);
750
751         if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
752             !strchr(p + 2, '%'))
753                 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
754         else {
755                 if (cmdline)
756                         g_warning("Print command line is invalid: `%s'\n",
757                                   cmdline);
758                 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
759         }
760
761         g_free(prtmp);
762
763         g_strchomp(buf);
764         if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
765         system(buf);
766 }
767
768 MsgInfo *procmsg_msginfo_new_ref(MsgInfo *msginfo)
769 {
770         msginfo->refcnt++;
771         
772         return msginfo;
773 }
774
775 MsgInfo *procmsg_msginfo_new(void)
776 {
777         MsgInfo *newmsginfo;
778
779         newmsginfo = g_new0(MsgInfo, 1);
780         newmsginfo->refcnt = 1;
781         
782         return newmsginfo;
783 }
784
785 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
786 {
787         MsgInfo *newmsginfo;
788
789         if (msginfo == NULL) return NULL;
790
791         newmsginfo = g_new0(MsgInfo, 1);
792
793         newmsginfo->refcnt = 1;
794
795 #define MEMBCOPY(mmb)   newmsginfo->mmb = msginfo->mmb
796 #define MEMBDUP(mmb)    newmsginfo->mmb = msginfo->mmb ? \
797                         g_strdup(msginfo->mmb) : NULL
798
799         MEMBCOPY(msgnum);
800         MEMBCOPY(size);
801         MEMBCOPY(mtime);
802         MEMBCOPY(date_t);
803         MEMBCOPY(flags);
804
805         MEMBDUP(fromname);
806
807         MEMBDUP(date);
808         MEMBDUP(from);
809         MEMBDUP(to);
810         MEMBDUP(cc);
811         MEMBDUP(newsgroups);
812         MEMBDUP(subject);
813         MEMBDUP(msgid);
814         MEMBDUP(inreplyto);
815         MEMBDUP(xref);
816
817         MEMBCOPY(folder);
818         MEMBCOPY(to_folder);
819
820         MEMBDUP(xface);
821         MEMBDUP(dispositionnotificationto);
822         MEMBDUP(returnreceiptto);
823         MEMBDUP(references);
824
825         MEMBCOPY(score);
826         MEMBCOPY(threadscore);
827
828         return newmsginfo;
829 }
830
831 MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
832 {
833         MsgInfo *full_msginfo;
834         gchar *file;
835
836         if (msginfo == NULL) return NULL;
837
838         file = procmsg_get_message_file(msginfo);
839         if (!file) {
840                 g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
841                 return NULL;
842         }
843
844         full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE, FALSE);
845         g_free(file);
846         if (!full_msginfo) return NULL;
847
848         full_msginfo->msgnum = msginfo->msgnum;
849         full_msginfo->size = msginfo->size;
850         full_msginfo->mtime = msginfo->mtime;
851         full_msginfo->folder = msginfo->folder;
852 #if USE_GPGME
853         full_msginfo->plaintext_file = g_strdup(msginfo->plaintext_file);
854         full_msginfo->decryption_failed = msginfo->decryption_failed;
855 #endif
856         procmsg_msginfo_set_to_folder(full_msginfo, msginfo->to_folder);
857
858         return full_msginfo;
859 }
860
861 void procmsg_msginfo_free(MsgInfo *msginfo)
862 {
863         if (msginfo == NULL) return;
864
865         msginfo->refcnt--;
866         if (msginfo->refcnt > 0)
867                 return;
868
869         debug_print("freeing msginfo %d in %s\n", msginfo->msgnum, msginfo->folder ? msginfo->folder->path : "(nil)");
870
871         if (msginfo->to_folder) {
872                 msginfo->to_folder->op_count--;
873                 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
874         }
875
876         g_free(msginfo->fromspace);
877         g_free(msginfo->references);
878         g_free(msginfo->returnreceiptto);
879         g_free(msginfo->dispositionnotificationto);
880         g_free(msginfo->xface);
881
882         g_free(msginfo->fromname);
883
884         g_free(msginfo->date);
885         g_free(msginfo->from);
886         g_free(msginfo->to);
887         g_free(msginfo->cc);
888         g_free(msginfo->newsgroups);
889         g_free(msginfo->subject);
890         g_free(msginfo->msgid);
891         g_free(msginfo->inreplyto);
892         g_free(msginfo->xref);
893
894         g_free(msginfo);
895 }
896
897 guint procmsg_msginfo_memusage(MsgInfo *msginfo)
898 {
899         guint memusage = 0;
900         
901         memusage += sizeof(MsgInfo);
902         if (msginfo->fromname)
903                 memusage += strlen(msginfo->fromname);
904         if (msginfo->date)
905                 memusage += strlen(msginfo->date);
906         if (msginfo->from)
907                 memusage += strlen(msginfo->from);
908         if (msginfo->to)
909                 memusage += strlen(msginfo->to);
910         if (msginfo->cc)
911                 memusage += strlen(msginfo->cc);
912         if (msginfo->newsgroups)
913                 memusage += strlen(msginfo->newsgroups);
914         if (msginfo->subject)
915                 memusage += strlen(msginfo->subject);
916         if (msginfo->msgid)
917                 memusage += strlen(msginfo->msgid);
918         if (msginfo->inreplyto)
919                 memusage += strlen(msginfo->inreplyto);
920         if (msginfo->xface)
921                 memusage += strlen(msginfo->xface);
922         if (msginfo->dispositionnotificationto)
923                 memusage += strlen(msginfo->dispositionnotificationto);
924         if (msginfo->returnreceiptto)
925                 memusage += strlen(msginfo->returnreceiptto);
926         if (msginfo->references)
927                 memusage += strlen(msginfo->references);
928         if (msginfo->fromspace)
929                 memusage += strlen(msginfo->fromspace);
930
931         return memusage;
932 }
933
934 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
935 {
936         const MsgInfo *msginfo1 = a;
937         const MsgInfo *msginfo2 = b;
938
939         if (!msginfo1)
940                 return -1;
941         if (!msginfo2)
942                 return -1;
943
944         return msginfo1->msgnum - msginfo2->msgnum;
945 }
946
947 enum
948 {
949         Q_SENDER           = 0,
950         Q_SMTPSERVER       = 1,
951         Q_RECIPIENTS       = 2,
952         Q_NEWSGROUPS       = 3,
953         Q_MAIL_ACCOUNT_ID  = 4,
954         Q_NEWS_ACCOUNT_ID  = 5,
955         Q_SAVE_COPY_FOLDER = 6,
956         Q_REPLY_MESSAGE_ID = 7,
957         Q_FWD_MESSAGE_ID   = 8
958 };
959
960 gint procmsg_send_message_queue(const gchar *file)
961 {
962         static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
963                                        {"SSV:",  NULL, FALSE},
964                                        {"R:",    NULL, FALSE},
965                                        {"NG:",   NULL, FALSE},
966                                        {"MAID:", NULL, FALSE},
967                                        {"NAID:", NULL, FALSE},
968                                        {"SCF:",  NULL, FALSE},
969                                        {"RMID:", NULL, FALSE},
970                                        {"FMID:", NULL, FALSE},
971                                        {NULL,    NULL, FALSE}};
972         FILE *fp;
973         gint filepos;
974         gint mailval = 0, newsval = 0;
975         gchar *from = NULL;
976         gchar *smtpserver = NULL;
977         GSList *to_list = NULL;
978         GSList *newsgroup_list = NULL;
979         gchar *savecopyfolder = NULL;
980         gchar *replymessageid = NULL;
981         gchar *fwdmessageid = NULL;
982         gchar buf[BUFFSIZE];
983         gint hnum;
984         PrefsAccount *mailac = NULL, *newsac = NULL;
985         int local = 0;
986
987         g_return_val_if_fail(file != NULL, -1);
988
989         if ((fp = fopen(file, "rb")) == NULL) {
990                 FILE_OP_ERROR(file, "fopen");
991                 return -1;
992         }
993
994         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
995                != -1) {
996                 gchar *p = buf + strlen(qentry[hnum].name);
997
998                 switch (hnum) {
999                 case Q_SENDER:
1000                         if (!from) from = g_strdup(p);
1001                         break;
1002                 case Q_SMTPSERVER:
1003                         if (!smtpserver) smtpserver = g_strdup(p);
1004                         break;
1005                 case Q_RECIPIENTS:
1006                         to_list = address_list_append(to_list, p);
1007                         break;
1008                 case Q_NEWSGROUPS:
1009                         newsgroup_list = newsgroup_list_append(newsgroup_list, p);
1010                         break;
1011                 case Q_MAIL_ACCOUNT_ID:
1012                         mailac = account_find_from_id(atoi(p));
1013                         break;
1014                 case Q_NEWS_ACCOUNT_ID:
1015                         newsac = account_find_from_id(atoi(p));
1016                         break;
1017                 case Q_SAVE_COPY_FOLDER:
1018                         if (!savecopyfolder) savecopyfolder = g_strdup(p);
1019                         break;
1020                 case Q_REPLY_MESSAGE_ID:
1021                         if (!replymessageid) replymessageid = g_strdup(p);
1022                         break;
1023                 case Q_FWD_MESSAGE_ID:
1024                         if (!fwdmessageid) fwdmessageid = g_strdup(p);
1025                         break;
1026                 }
1027         }
1028         filepos = ftell(fp);
1029
1030         if (to_list) {
1031                 debug_print("Sending message by mail\n");
1032                 if (!from) {
1033                         g_warning("Queued message header is broken.\n");
1034                         mailval = -1;
1035                 } else if (mailac && mailac->use_mail_command &&
1036                            mailac->mail_command && (* mailac->mail_command)) {
1037                         mailval = send_message_local(mailac->mail_command, fp);
1038                         local = 1;
1039                 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
1040                         mailval = send_message_local(prefs_common.extsend_cmd, fp);
1041                         local = 1;
1042                 } else {
1043                         if (!mailac) {
1044                                 mailac = account_find_from_smtp_server(from, smtpserver);
1045                                 if (!mailac) {
1046                                         g_warning("Account not found. "
1047                                                     "Using current account...\n");
1048                                         mailac = cur_account;
1049                                 }
1050                         }
1051
1052                         if (mailac)
1053                                 mailval = send_message_smtp(mailac, to_list, fp);
1054                         else {
1055                                 PrefsAccount tmp_ac;
1056
1057                                 g_warning("Account not found.\n");
1058
1059                                 memset(&tmp_ac, 0, sizeof(PrefsAccount));
1060                                 tmp_ac.address = from;
1061                                 tmp_ac.smtp_server = smtpserver;
1062                                 tmp_ac.smtpport = SMTP_PORT;
1063                                 mailval = send_message_smtp(&tmp_ac, to_list, fp);
1064                         }
1065                 }
1066         }
1067
1068         fseek(fp, filepos, SEEK_SET);
1069         if (newsgroup_list && (newsval == 0)) {
1070                 Folder *folder;
1071                 gchar *tmp = NULL;
1072                 FILE *tmpfp;
1073
1074                 /* write to temporary file */
1075                 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1076                             G_DIR_SEPARATOR, (gint)file);
1077                 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
1078                         FILE_OP_ERROR(tmp, "fopen");
1079                         newsval = -1;
1080                         alertpanel_error(_("Could not create temporary file for news sending."));
1081                 } else {
1082                         if (change_file_mode_rw(tmpfp, tmp) < 0) {
1083                                 FILE_OP_ERROR(tmp, "chmod");
1084                                 g_warning("can't change file mode\n");
1085                         }
1086
1087                         while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
1088                                 if (fputs(buf, tmpfp) == EOF) {
1089                                         FILE_OP_ERROR(tmp, "fputs");
1090                                         newsval = -1;
1091                                         alertpanel_error(_("Error when writing temporary file for news sending."));
1092                                 }
1093                         }
1094                         fclose(tmpfp);
1095
1096                         if (newsval == 0) {
1097                                 debug_print("Sending message by news\n");
1098
1099                                 folder = FOLDER(newsac->folder);
1100
1101                                 newsval = news_post(folder, tmp);
1102                                 if (newsval < 0) {
1103                                         alertpanel_error(_("Error occurred while posting the message to %s ."),
1104                                                  newsac->nntp_server);
1105                                 }
1106                         }
1107                         unlink(tmp);
1108                 }
1109                 g_free(tmp);
1110         }
1111
1112         slist_free_strings(to_list);
1113         g_slist_free(to_list);
1114         slist_free_strings(newsgroup_list);
1115         g_slist_free(newsgroup_list);
1116         g_free(from);
1117         g_free(smtpserver);
1118         fclose(fp);
1119
1120         /* save message to outbox */
1121         if (mailval == 0 && newsval == 0 && savecopyfolder) {
1122                 FolderItem *outbox;
1123
1124                 debug_print("saving sent message...\n");
1125
1126                 outbox = folder_find_item_from_identifier(savecopyfolder);
1127                 if (!outbox)
1128                         outbox = folder_get_default_outbox();
1129
1130                 procmsg_save_to_outbox(outbox, file, TRUE);
1131         }
1132
1133         if (replymessageid != NULL || fwdmessageid != NULL) {
1134                 gchar **tokens;
1135                 FolderItem *item;
1136                 
1137                 if (replymessageid != NULL)
1138                         tokens = g_strsplit(replymessageid, "\x7f", 0);
1139                 else
1140                         tokens = g_strsplit(fwdmessageid, "\x7f", 0);
1141                 item = folder_find_item_from_identifier(tokens[0]);
1142                 if (item != NULL) {
1143                         MsgInfo *msginfo;
1144                         
1145                         msginfo = folder_item_get_msginfo(item, atoi(tokens[1]));
1146                         if ((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1147                                 procmsg_msginfo_free(msginfo);
1148                                 msginfo = NULL;
1149                         }
1150                         
1151                         if (msginfo == NULL) {
1152                                 msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]);
1153                         }
1154                         
1155                         if (msginfo != NULL) {
1156                                 if (replymessageid != NULL) {
1157                                         procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1158                                         procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1159                                 } 
1160                                 else {
1161                                         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
1162                                         procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0);
1163                                 }
1164                                 procmsg_msginfo_free(msginfo);
1165                         }
1166                 }
1167                 g_strfreev(tokens);
1168         }
1169
1170         g_free(savecopyfolder);
1171         g_free(replymessageid);
1172         g_free(fwdmessageid);
1173         
1174         return (newsval != 0 ? newsval : mailval);
1175 }
1176
1177 static void update_folder_msg_counts(FolderItem *item, MsgInfo *msginfo, MsgPermFlags old_flags)
1178 {
1179         MsgPermFlags new_flags = msginfo->flags.perm_flags;
1180
1181         /* NEW flag */
1182         if (!(old_flags & MSG_NEW) && (new_flags & MSG_NEW)) {
1183                 item->new_msgs++;
1184         }
1185
1186         if ((old_flags & MSG_NEW) && !(new_flags & MSG_NEW)) {
1187                 item->new_msgs--;
1188         }
1189
1190         /* UNREAD flag */
1191         if (!(old_flags & MSG_UNREAD) && (new_flags & MSG_UNREAD)) {
1192                 item->unread_msgs++;
1193                 if (procmsg_msg_has_marked_parent(msginfo))
1194                         item->unreadmarked_msgs++;
1195         }
1196
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         /* MARK flag */
1204         if (!(old_flags & MSG_MARKED) && (new_flags & MSG_MARKED)) {
1205                 procmsg_update_unread_children(msginfo, TRUE);
1206         }
1207
1208         if ((old_flags & MSG_MARKED) && !(new_flags & MSG_MARKED)) {
1209                 procmsg_update_unread_children(msginfo, FALSE);
1210         }
1211 }
1212
1213 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1214 {
1215         FolderItem *item;
1216         MsgInfoUpdate msginfo_update;
1217         MsgPermFlags perm_flags_new, perm_flags_old;
1218
1219         g_return_if_fail(msginfo != NULL);
1220         item = msginfo->folder;
1221         g_return_if_fail(item != NULL);
1222         
1223         debug_print("Setting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1224
1225         /* Perm Flags handling */
1226         perm_flags_old = msginfo->flags.perm_flags;
1227         perm_flags_new = msginfo->flags.perm_flags | perm_flags;
1228         if ((perm_flags & MSG_IGNORE_THREAD) || (perm_flags_old & MSG_IGNORE_THREAD)) {
1229                 perm_flags_new &= ~(MSG_NEW | MSG_UNREAD);
1230         }
1231
1232         if (perm_flags_old != perm_flags_new) {
1233                 folder_item_change_msg_flags(msginfo->folder, msginfo, perm_flags_new);
1234
1235                 update_folder_msg_counts(item, msginfo, perm_flags_old);
1236
1237                 msginfo_update.msginfo = msginfo;
1238                 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1239                 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1240         }
1241
1242         /* Tmp flags hanlding */
1243         msginfo->flags.tmp_flags |= tmp_flags;
1244 }
1245
1246 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1247 {
1248         FolderItem *item;
1249         MsgInfoUpdate msginfo_update;
1250         MsgPermFlags perm_flags_new, perm_flags_old;
1251
1252         g_return_if_fail(msginfo != NULL);
1253         item = msginfo->folder;
1254         g_return_if_fail(item != NULL);
1255         
1256         debug_print("Unsetting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1257
1258         /* Perm Flags handling */
1259         perm_flags_old = msginfo->flags.perm_flags;
1260         perm_flags_new = msginfo->flags.perm_flags & ~perm_flags;
1261         
1262         if (perm_flags_old != perm_flags_new) {
1263                 folder_item_change_msg_flags(msginfo->folder, msginfo, perm_flags_new);
1264
1265                 update_folder_msg_counts(item, msginfo, perm_flags_old);
1266
1267                 msginfo_update.msginfo = msginfo;
1268                 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1269                 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1270         }
1271
1272         /* Tmp flags hanlding */
1273         msginfo->flags.tmp_flags &= ~tmp_flags;
1274 }
1275
1276 /*!
1277  *\brief        check for flags (e.g. mark) in prior msgs of current thread
1278  *
1279  *\param        info Current message
1280  *\param        perm_flags Flags to be checked
1281  *\param        parentmsgs Hash of prior msgs to avoid loops
1282  *
1283  *\return       gboolean TRUE if perm_flags are found
1284  */
1285 gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
1286                 MsgPermFlags perm_flags, GHashTable *parentmsgs)
1287 {
1288         MsgInfo *tmp;
1289
1290         g_return_val_if_fail(info != NULL, FALSE);
1291
1292         if (info != NULL && info->folder != NULL && info->inreplyto != NULL) {
1293                 tmp = folder_item_get_msginfo_by_msgid(info->folder,
1294                                 info->inreplyto);
1295                 if (tmp && (tmp->flags.perm_flags & perm_flags)) {
1296                         procmsg_msginfo_free(tmp);
1297                         return TRUE;
1298                 } else if (tmp != NULL) {
1299                         gboolean result;
1300
1301                         if (g_hash_table_lookup(parentmsgs, info)) {
1302                                 debug_print("loop detected: %s%c%d\n",
1303                                         folder_item_get_path(info->folder),
1304                                         G_DIR_SEPARATOR, info->msgnum);
1305                                 result = FALSE;
1306                         } else {
1307                                 g_hash_table_insert(parentmsgs, info, "1");
1308                                 result = procmsg_msg_has_flagged_parent_real(
1309                                     tmp, perm_flags, parentmsgs);
1310                         }
1311                         procmsg_msginfo_free(tmp);
1312                         return result;
1313                 } else {
1314                         return FALSE;
1315                 }
1316         } else
1317                 return FALSE;
1318 }
1319
1320 /*!
1321  *\brief        Callback for cleaning up hash of parentmsgs
1322  */
1323 gboolean parentmsgs_hash_remove(gpointer key,
1324                             gpointer value,
1325                             gpointer user_data)
1326 {
1327         return TRUE;
1328 }
1329
1330 /*!
1331  *\brief        Set up list of parentmsgs
1332  *              See procmsg_msg_has_flagged_parent_real()
1333  */
1334 gboolean procmsg_msg_has_flagged_parent(MsgInfo *info, MsgPermFlags perm_flags)
1335 {
1336         gboolean result;
1337         GHashTable *parentmsgs = g_hash_table_new(NULL, NULL); 
1338
1339         result = procmsg_msg_has_flagged_parent_real(info, perm_flags, parentmsgs);
1340         g_hash_table_foreach_remove(parentmsgs, parentmsgs_hash_remove, NULL);
1341         g_hash_table_destroy(parentmsgs);
1342         return result;
1343 }
1344
1345 /*!
1346  *\brief        Check if msgs prior in thread are marked
1347  *              See procmsg_msg_has_flagged_parent_real()
1348  */
1349 gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
1350 {
1351         return procmsg_msg_has_flagged_parent(info, MSG_MARKED);
1352 }
1353
1354
1355 GSList *procmsg_find_children_func(MsgInfo *info, 
1356                                    GSList *children, GSList *all)
1357 {
1358         GSList *cur;
1359
1360         g_return_val_if_fail(info!=NULL, children);
1361         if (info->msgid == NULL)
1362                 return children;
1363
1364         for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1365                 MsgInfo *tmp = (MsgInfo *)cur->data;
1366                 if (tmp->inreplyto && !strcmp(tmp->inreplyto, info->msgid)) {
1367                         /* Check if message is already in the list */
1368                         if ((children == NULL) || 
1369                             (g_slist_index(children, tmp) == -1)) {
1370                                 children = g_slist_prepend(children,
1371                                                 procmsg_msginfo_new_ref(tmp));
1372                                 children = procmsg_find_children_func(tmp, 
1373                                                         children, 
1374                                                         all);
1375                         }
1376                 }
1377         }
1378         return children;
1379 }
1380
1381 GSList *procmsg_find_children (MsgInfo *info)
1382 {
1383         GSList *children;
1384         GSList *all, *cur;
1385
1386         g_return_val_if_fail(info!=NULL, NULL);
1387         all = folder_item_get_msg_list(info->folder);
1388         children = procmsg_find_children_func(info, NULL, all);
1389         if (children != NULL) {
1390                 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1391                         /* this will not free the used pointers
1392                            created with procmsg_msginfo_new_ref */
1393                         procmsg_msginfo_free((MsgInfo *)cur->data);
1394                 }
1395         }
1396         g_slist_free(all);
1397
1398         return children;
1399 }
1400
1401 void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
1402 {
1403         GSList *children = procmsg_find_children(info);
1404         GSList *cur;
1405         for (cur = children; cur != NULL; cur = g_slist_next(cur)) {
1406                 MsgInfo *tmp = (MsgInfo *)cur->data;
1407                 if(MSG_IS_UNREAD(tmp->flags) && !MSG_IS_IGNORE_THREAD(tmp->flags)) {
1408                         if(newly_marked) 
1409                                 info->folder->unreadmarked_msgs++;
1410                         else
1411                                 info->folder->unreadmarked_msgs--;
1412                         folder_item_update(info->folder, F_ITEM_UPDATE_MSGCNT);
1413                 }
1414                 procmsg_msginfo_free(tmp);
1415         }
1416         g_slist_free(children);
1417 }
1418
1419 /**
1420  * Set the destination folder for a copy or move operation
1421  *
1422  * \param msginfo The message which's destination folder is changed
1423  * \param to_folder The destination folder for the operation
1424  */
1425 void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
1426 {
1427         if(msginfo->to_folder != NULL) {
1428                 msginfo->to_folder->op_count--;
1429                 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1430         }
1431         msginfo->to_folder = to_folder;
1432         if(to_folder != NULL) {
1433                 to_folder->op_count++;
1434                 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1435         }
1436 }
1437
1438 /**
1439  * Apply filtering actions to the msginfo
1440  *
1441  * \param msginfo The MsgInfo describing the message that should be filtered
1442  * \return TRUE if the message was moved and MsgInfo is now invalid,
1443  *         FALSE otherwise
1444  */
1445 gboolean procmsg_msginfo_filter(MsgInfo *msginfo)
1446 {
1447         MailFilteringData mail_filtering_data;
1448                         
1449         mail_filtering_data.msginfo = msginfo;                  
1450         if (hooks_invoke(MAIL_FILTERING_HOOKLIST, &mail_filtering_data))
1451                 return TRUE;
1452
1453         /* filter if enabled in prefs or move to inbox if not */
1454         if((global_processing != NULL) &&
1455            filter_message_by_msginfo(global_processing, msginfo))
1456                 return TRUE;
1457
1458         return FALSE;
1459 }