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