0.8.11claws22
[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
26 #include "intl.h"
27 #include "main.h"
28 #include "utils.h"
29 #include "procmsg.h"
30 #include "procheader.h"
31 #include "send.h"
32 #include "procmime.h"
33 #include "statusbar.h"
34 #include "folder.h"
35 #include "prefs_common.h"
36 #include "account.h"
37 #if USE_GPGME
38 #  include "rfc2015.h"
39 #endif
40 #include "alertpanel.h"
41 #include "news.h"
42 #include "imap.h"
43 #include "hooks.h"
44 #include "msgcache.h"
45
46 typedef struct _FlagInfo        FlagInfo;
47
48 struct _FlagInfo
49 {
50         guint    msgnum;
51         MsgFlags flags;
52 };
53
54 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
55 {
56         GHashTable *msg_table;
57
58         if (mlist == NULL) return NULL;
59
60         msg_table = g_hash_table_new(NULL, g_direct_equal);
61         procmsg_msg_hash_table_append(msg_table, mlist);
62
63         return msg_table;
64 }
65
66 void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
67 {
68         GSList *cur;
69         MsgInfo *msginfo;
70
71         if (msg_table == NULL || mlist == NULL) return;
72
73         for (cur = mlist; cur != NULL; cur = cur->next) {
74                 msginfo = (MsgInfo *)cur->data;
75
76                 g_hash_table_insert(msg_table,
77                                     GUINT_TO_POINTER(msginfo->msgnum),
78                                     msginfo);
79         }
80 }
81
82 GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
83 {
84         GHashTable *msg_table;
85         GSList *cur;
86         MsgInfo *msginfo;
87
88         if (mlist == NULL) return NULL;
89
90         msg_table = g_hash_table_new(NULL, g_direct_equal);
91
92         for (cur = mlist; cur != NULL; cur = cur->next) {
93                 msginfo = (MsgInfo *)cur->data;
94                 g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
95         }
96
97         return msg_table;
98 }
99
100 gint procmsg_get_last_num_in_msg_list(GSList *mlist)
101 {
102         GSList *cur;
103         MsgInfo *msginfo;
104         gint last = 0;
105
106         for (cur = mlist; cur != NULL; cur = cur->next) {
107                 msginfo = (MsgInfo *)cur->data;
108                 if (msginfo && msginfo->msgnum > last)
109                         last = msginfo->msgnum;
110         }
111
112         return last;
113 }
114
115 void procmsg_msg_list_free(GSList *mlist)
116 {
117         GSList *cur;
118         MsgInfo *msginfo;
119
120         for (cur = mlist; cur != NULL; cur = cur->next) {
121                 msginfo = (MsgInfo *)cur->data;
122                 procmsg_msginfo_free(msginfo);
123         }
124         g_slist_free(mlist);
125 }
126
127 struct MarkSum {
128         gint *new;
129         gint *unread;
130         gint *total;
131         gint *min;
132         gint *max;
133         gint first;
134 };
135
136 static gboolean procmsg_ignore_node(GNode *node, gpointer data)
137 {
138         MsgInfo *msginfo = (MsgInfo *)node->data;
139         
140         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
141
142         return FALSE;
143 }
144
145 /* return the reversed thread tree */
146 GNode *procmsg_get_thread_tree(GSList *mlist)
147 {
148         GNode *root, *parent, *node, *next;
149         GHashTable *msgid_table;
150         GHashTable *subject_table;
151         MsgInfo *msginfo;
152         const gchar *msgid;
153         const gchar *subject;
154         GNode *found_subject;
155
156         root = g_node_new(NULL);
157         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
158         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
159
160         for (; mlist != NULL; mlist = mlist->next) {
161                 msginfo = (MsgInfo *)mlist->data;
162                 parent = root;
163
164                 if (msginfo->inreplyto) {
165                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
166                         if (parent == NULL) {
167                                 parent = root;
168                         } else {
169                                 if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
170                                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
171                                 }
172                         }
173                 }
174                 node = g_node_insert_data_before
175                         (parent, parent == root ? parent->children : NULL,
176                          msginfo);
177                 if ((msgid = msginfo->msgid) &&
178                     g_hash_table_lookup(msgid_table, msgid) == NULL)
179                         g_hash_table_insert(msgid_table, (gchar *)msgid, node);
180
181                 if (prefs_common.thread_by_subject) {
182                         subject = msginfo->subject;
183                         found_subject = subject_table_lookup(subject_table,
184                                                              (gchar *) subject);
185                         if (found_subject == NULL)
186                                 subject_table_insert(subject_table, (gchar *) subject,
187                                                      node);
188                         else {
189                                 /* replace if msg in table is older than current one 
190                                  * can add here more stuff. */
191                                 if ( ((MsgInfo*)(found_subject->data))->date_t >
192                                      ((MsgInfo*)(node->data))->date_t )  {
193                                         subject_table_remove(subject_table, (gchar *) subject);
194                                         subject_table_insert(subject_table, (gchar *) subject, node);
195                                 }       
196                         }
197                 }
198         }
199
200         /* complete the unfinished threads */
201         for (node = root->children; node != NULL; ) {
202                 next = node->next;
203                 msginfo = (MsgInfo *)node->data;
204                 parent = NULL;
205                 if (msginfo->inreplyto) 
206                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
207                 if (parent && parent != node) {
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 = NULL;
228                         if (subject_is_reply(msginfo->subject)) {
229                                 parent = subject_table_lookup(subject_table,
230                                                               msginfo->subject);
231                                 /* the node may already be threaded by IN-REPLY-TO,
232                                    so go up in the tree to find the parent node */
233                                 if (parent != NULL) {
234                                         if (g_node_is_ancestor(node, parent))
235                                                 parent = NULL;
236                                         if (parent == node)
237                                                 parent = NULL;
238                                 }
239
240                                 if (parent) {
241                                         g_node_unlink(node);
242                                         g_node_append(parent, node);
243                                         /* CLAWS: ignore thread */
244                                         if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
245                                                 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
246                                         }
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
400         g_return_val_if_fail(msginfo != NULL, NULL);
401
402         if (mimeinfo) *mimeinfo = NULL;
403
404         if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
405
406         mimeinfo_ = procmime_scan_mime_header(fp);
407         if (!mimeinfo_) {
408                 fclose(fp);
409                 return NULL;
410         }
411
412         if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
413             rfc2015_is_encrypted(mimeinfo_)) {
414                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
415         }
416
417         if (MSG_IS_ENCRYPTED(msginfo->flags) &&
418             (!msginfo->plaintext_file || msginfo->decryption_failed)) {
419                 rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
420                 if (msginfo->plaintext_file &&
421                     !msginfo->decryption_failed) {
422                         fclose(fp);
423                         procmime_mimeinfo_free_all(mimeinfo_);
424                         if ((fp = procmsg_open_message(msginfo)) == NULL)
425                                 return NULL;
426                         mimeinfo_ = procmime_scan_mime_header(fp);
427                         if (!mimeinfo_) {
428                                 fclose(fp);
429                                 return NULL;
430                         }
431                 }
432         }
433
434         if (mimeinfo) *mimeinfo = mimeinfo_;
435         return fp;
436 }
437 #endif
438
439 gboolean procmsg_msg_exist(MsgInfo *msginfo)
440 {
441         gchar *path;
442         gboolean ret;
443
444         if (!msginfo) return FALSE;
445
446         path = folder_item_get_path(msginfo->folder);
447         change_dir(path);
448         ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
449         g_free(path);
450
451         return ret;
452 }
453
454 void procmsg_empty_trash(void)
455 {
456         FolderItem *trash;
457         GList *cur;
458
459         for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
460                 trash = FOLDER(cur->data)->trash;
461                 if (trash && trash->total > 0)
462                         folder_item_remove_all_msg(trash);
463         }
464 }
465
466 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
467 {
468         gint ret = 0;
469         GSList *list, *elem;
470
471         if (!queue)
472                 queue = folder_get_default_queue();
473         g_return_val_if_fail(queue != NULL, -1);
474
475         folder_item_scan(queue);
476         list = folder_item_get_msg_list(queue);
477
478
479         for (elem = list; elem != NULL; elem = elem->next) {
480                 gchar *file;
481                 MsgInfo *msginfo;
482                 
483                 msginfo = (MsgInfo *)(elem->data);
484
485                 file = folder_item_fetch_msg(queue, msginfo->msgnum);
486                 if (file) {
487                         if (procmsg_send_message_queue(file) < 0) {
488                                 g_warning("Sending queued message %d failed.\n", msginfo->msgnum);
489                                 ret = -1;
490                         } else {
491                         /* CLAWS: 
492                          * We save in procmsg_send_message_queue because
493                          * we need the destination folder from the queue
494                          * header
495                                                 
496                                 if (save_msgs)
497                                         procmsg_save_to_outbox
498                                                 (queue->folder->outbox,
499                                                  file, TRUE);
500 */
501                                 folder_item_remove_msg(queue, msginfo->msgnum);
502                         }
503                         g_free(file);
504                 }
505                 procmsg_msginfo_free(msginfo);
506         }
507
508         return ret;
509 }
510
511 gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
512 {
513         FILE *fp, *outfp;
514         gchar buf[BUFFSIZE];
515         
516         if ((fp = fopen(in, "rb")) == NULL) {
517                 FILE_OP_ERROR(in, "fopen");
518                 return -1;
519         }
520         if ((outfp = fopen(out, "wb")) == NULL) {
521                 FILE_OP_ERROR(out, "fopen");
522                 fclose(fp);
523                 return -1;
524         }
525         while (fgets(buf, sizeof(buf), fp) != NULL)
526                 if (buf[0] == '\r' || buf[0] == '\n') break;
527         while (fgets(buf, sizeof(buf), fp) != NULL)
528                 fputs(buf, outfp);
529         fclose(outfp);
530         fclose(fp);
531         return 0;
532
533 }
534 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
535                             gboolean is_queued)
536 {
537         gint num;
538         MsgInfo *msginfo;
539
540         debug_print("saving sent message...\n");
541
542         if (!outbox)
543                 outbox = folder_get_default_outbox();
544         g_return_val_if_fail(outbox != NULL, -1);
545
546         /* remove queueing headers */
547         if (is_queued) {
548                 gchar tmp[MAXPATHLEN + 1];
549
550                 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
551                            get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
552                 
553                 if (procmsg_remove_special_headers(file, tmp) !=0)
554                         return -1;
555
556                 folder_item_scan(outbox);
557                 if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
558                         g_warning("can't save message\n");
559                         unlink(tmp);
560                         return -1;
561                 }
562         } else {
563                 folder_item_scan(outbox);
564                 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
565                         g_warning("can't save message\n");
566                         return -1;
567                 }
568                 return -1;
569         }
570         msginfo = folder_item_get_msginfo(outbox, num);
571         if (msginfo != NULL) {
572             procmsg_msginfo_unset_flags(msginfo, ~0, 0);
573             procmsg_msginfo_free(msginfo);
574         }
575         folder_item_update(outbox, TRUE);
576
577         return 0;
578 }
579
580 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
581 {
582         static const gchar *def_cmd = "lpr %s";
583         static guint id = 0;
584         gchar *prtmp;
585         FILE *tmpfp, *prfp;
586         gchar buf[1024];
587         gchar *p;
588
589         g_return_if_fail(msginfo);
590
591         if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
592                 g_warning("Can't get text part\n");
593                 return;
594         }
595
596         prtmp = g_strdup_printf("%s%cprinttmp.%08x",
597                                 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
598
599         if ((prfp = fopen(prtmp, "wb")) == NULL) {
600                 FILE_OP_ERROR(prtmp, "fopen");
601                 g_free(prtmp);
602                 fclose(tmpfp);
603                 return;
604         }
605
606         if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
607         if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
608         if (msginfo->to)   fprintf(prfp, "To: %s\n", msginfo->to);
609         if (msginfo->cc)   fprintf(prfp, "Cc: %s\n", msginfo->cc);
610         if (msginfo->newsgroups)
611                 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
612         if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
613         fputc('\n', prfp);
614
615         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
616                 fputs(buf, prfp);
617
618         fclose(prfp);
619         fclose(tmpfp);
620
621         if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
622             !strchr(p + 2, '%'))
623                 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
624         else {
625                 if (cmdline)
626                         g_warning("Print command line is invalid: `%s'\n",
627                                   cmdline);
628                 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
629         }
630
631         g_free(prtmp);
632
633         g_strchomp(buf);
634         if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
635         system(buf);
636 }
637
638 MsgInfo *procmsg_msginfo_new_ref(MsgInfo *msginfo)
639 {
640         msginfo->refcnt++;
641         
642         return msginfo;
643 }
644
645 MsgInfo *procmsg_msginfo_new()
646 {
647         MsgInfo *newmsginfo;
648
649         newmsginfo = g_new0(MsgInfo, 1);
650         newmsginfo->refcnt = 1;
651         
652         return newmsginfo;
653 }
654
655 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
656 {
657         MsgInfo *newmsginfo;
658
659         if (msginfo == NULL) return NULL;
660
661         newmsginfo = g_new0(MsgInfo, 1);
662
663         newmsginfo->refcnt = 1;
664
665 #define MEMBCOPY(mmb)   newmsginfo->mmb = msginfo->mmb
666 #define MEMBDUP(mmb)    newmsginfo->mmb = msginfo->mmb ? \
667                         g_strdup(msginfo->mmb) : NULL
668
669         MEMBCOPY(msgnum);
670         MEMBCOPY(size);
671         MEMBCOPY(mtime);
672         MEMBCOPY(date_t);
673         MEMBCOPY(flags);
674
675         MEMBDUP(fromname);
676
677         MEMBDUP(date);
678         MEMBDUP(from);
679         MEMBDUP(to);
680         MEMBDUP(cc);
681         MEMBDUP(newsgroups);
682         MEMBDUP(subject);
683         MEMBDUP(msgid);
684         MEMBDUP(inreplyto);
685         MEMBDUP(xref);
686
687         MEMBCOPY(folder);
688         MEMBCOPY(to_folder);
689
690         MEMBDUP(xface);
691         MEMBDUP(dispositionnotificationto);
692         MEMBDUP(returnreceiptto);
693         MEMBDUP(references);
694
695         MEMBCOPY(score);
696         MEMBCOPY(threadscore);
697
698         return newmsginfo;
699 }
700
701 MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
702 {
703         MsgInfo *full_msginfo;
704         gchar *file;
705
706         if (msginfo == NULL) return NULL;
707
708         file = procmsg_get_message_file(msginfo);
709         if (!file) {
710                 g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
711                 return NULL;
712         }
713
714         full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE, FALSE);
715         g_free(file);
716         if (!full_msginfo) return NULL;
717
718         full_msginfo->msgnum = msginfo->msgnum;
719         full_msginfo->size = msginfo->size;
720         full_msginfo->mtime = msginfo->mtime;
721         full_msginfo->folder = msginfo->folder;
722 #if USE_GPGME
723         full_msginfo->plaintext_file = g_strdup(msginfo->plaintext_file);
724         full_msginfo->decryption_failed = msginfo->decryption_failed;
725 #endif
726         procmsg_msginfo_set_to_folder(full_msginfo, msginfo->to_folder);
727
728         return full_msginfo;
729 }
730
731 void procmsg_msginfo_free(MsgInfo *msginfo)
732 {
733         if (msginfo == NULL) return;
734
735         msginfo->refcnt--;
736         if (msginfo->refcnt > 0)
737                 return;
738
739         debug_print("freeing msginfo %d is %s\n", msginfo->msgnum, msginfo->folder ? msginfo->folder->path : "(nil)");
740
741         if (msginfo->to_folder) {
742                 msginfo->to_folder->op_count--;
743                 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
744         }
745
746         g_free(msginfo->fromspace);
747         g_free(msginfo->references);
748         g_free(msginfo->returnreceiptto);
749         g_free(msginfo->dispositionnotificationto);
750         g_free(msginfo->xface);
751
752         g_free(msginfo->fromname);
753
754         g_free(msginfo->date);
755         g_free(msginfo->from);
756         g_free(msginfo->to);
757         g_free(msginfo->cc);
758         g_free(msginfo->newsgroups);
759         g_free(msginfo->subject);
760         g_free(msginfo->msgid);
761         g_free(msginfo->inreplyto);
762         g_free(msginfo->xref);
763
764         g_free(msginfo);
765 }
766
767 guint procmsg_msginfo_memusage(MsgInfo *msginfo)
768 {
769         guint memusage = 0;
770         
771         memusage += sizeof(MsgInfo);
772         if (msginfo->fromname)
773                 memusage += strlen(msginfo->fromname);
774         if (msginfo->date)
775                 memusage += strlen(msginfo->date);
776         if (msginfo->from)
777                 memusage += strlen(msginfo->from);
778         if (msginfo->to)
779                 memusage += strlen(msginfo->to);
780         if (msginfo->cc)
781                 memusage += strlen(msginfo->cc);
782         if (msginfo->newsgroups)
783                 memusage += strlen(msginfo->newsgroups);
784         if (msginfo->subject)
785                 memusage += strlen(msginfo->subject);
786         if (msginfo->msgid)
787                 memusage += strlen(msginfo->msgid);
788         if (msginfo->inreplyto)
789                 memusage += strlen(msginfo->inreplyto);
790         if (msginfo->xface)
791                 memusage += strlen(msginfo->xface);
792         if (msginfo->dispositionnotificationto)
793                 memusage += strlen(msginfo->dispositionnotificationto);
794         if (msginfo->returnreceiptto)
795                 memusage += strlen(msginfo->returnreceiptto);
796         if (msginfo->references)
797                 memusage += strlen(msginfo->references);
798         if (msginfo->fromspace)
799                 memusage += strlen(msginfo->fromspace);
800
801         return memusage;
802 }
803
804 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
805 {
806         const MsgInfo *msginfo1 = a;
807         const MsgInfo *msginfo2 = b;
808
809         if (!msginfo1)
810                 return -1;
811         if (!msginfo2)
812                 return -1;
813
814         return msginfo1->msgnum - msginfo2->msgnum;
815 }
816
817 enum
818 {
819         Q_SENDER           = 0,
820         Q_SMTPSERVER       = 1,
821         Q_RECIPIENTS       = 2,
822         Q_NEWSGROUPS       = 3,
823         Q_MAIL_ACCOUNT_ID  = 4,
824         Q_NEWS_ACCOUNT_ID  = 5,
825         Q_SAVE_COPY_FOLDER = 6,
826         Q_REPLY_MESSAGE_ID = 7,
827         Q_FWD_MESSAGE_ID   = 8
828 };
829
830 gint procmsg_send_message_queue(const gchar *file)
831 {
832         static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
833                                        {"SSV:",  NULL, FALSE},
834                                        {"R:",    NULL, FALSE},
835                                        {"NG:",   NULL, FALSE},
836                                        {"MAID:", NULL, FALSE},
837                                        {"NAID:", NULL, FALSE},
838                                        {"SCF:",  NULL, FALSE},
839                                        {"RMID:", NULL, FALSE},
840                                        {"FMID:", NULL, FALSE},
841                                        {NULL,    NULL, FALSE}};
842         FILE *fp;
843         gint filepos;
844         gint mailval = 0, newsval = 0;
845         gchar *from = NULL;
846         gchar *smtpserver = NULL;
847         GSList *to_list = NULL;
848         GSList *newsgroup_list = NULL;
849         gchar *savecopyfolder = NULL;
850         gchar *replymessageid = NULL;
851         gchar *fwdmessageid = NULL;
852         gchar buf[BUFFSIZE];
853         gint hnum;
854         PrefsAccount *mailac = NULL, *newsac = NULL;
855         int local = 0;
856
857         g_return_val_if_fail(file != NULL, -1);
858
859         if ((fp = fopen(file, "rb")) == NULL) {
860                 FILE_OP_ERROR(file, "fopen");
861                 return -1;
862         }
863
864         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
865                != -1) {
866                 gchar *p = buf + strlen(qentry[hnum].name);
867
868                 switch (hnum) {
869                 case Q_SENDER:
870                         if (!from) from = g_strdup(p);
871                         break;
872                 case Q_SMTPSERVER:
873                         if (!smtpserver) smtpserver = g_strdup(p);
874                         break;
875                 case Q_RECIPIENTS:
876                         to_list = address_list_append(to_list, p);
877                         break;
878                 case Q_NEWSGROUPS:
879                         newsgroup_list = newsgroup_list_append(newsgroup_list, p);
880                         break;
881                 case Q_MAIL_ACCOUNT_ID:
882                         mailac = account_find_from_id(atoi(p));
883                         break;
884                 case Q_NEWS_ACCOUNT_ID:
885                         newsac = account_find_from_id(atoi(p));
886                         break;
887                 case Q_SAVE_COPY_FOLDER:
888                         if (!savecopyfolder) savecopyfolder = g_strdup(p);
889                         break;
890                 case Q_REPLY_MESSAGE_ID:
891                         if (!replymessageid) replymessageid = g_strdup(p);
892                         break;
893                 case Q_FWD_MESSAGE_ID:
894                         if (!fwdmessageid) fwdmessageid = g_strdup(p);
895                         break;
896                 }
897         }
898         filepos = ftell(fp);
899
900         if (to_list) {
901                 debug_print("Sending message by mail\n");
902                 if (!from) {
903                         g_warning("Queued message header is broken.\n");
904                         mailval = -1;
905                 } else if (mailac && mailac->use_mail_command &&
906                            mailac->mail_command && (* mailac->mail_command)) {
907                         mailval = send_message_local(mailac->mail_command, fp);
908                         local = 1;
909                 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
910                         mailval = send_message_local(prefs_common.extsend_cmd, fp);
911                         local = 1;
912                 } else {
913                         if (!mailac) {
914                                 mailac = account_find_from_smtp_server(from, smtpserver);
915                                 if (!mailac) {
916                                         g_warning("Account not found. "
917                                                     "Using current account...\n");
918                                         mailac = cur_account;
919                                 }
920                         }
921
922                         if (mailac)
923                                 mailval = send_message_smtp(mailac, to_list, fp);
924                         else {
925                                 PrefsAccount tmp_ac;
926
927                                 g_warning("Account not found.\n");
928
929                                 memset(&tmp_ac, 0, sizeof(PrefsAccount));
930                                 tmp_ac.address = from;
931                                 tmp_ac.smtp_server = smtpserver;
932                                 tmp_ac.smtpport = SMTP_PORT;
933                                 mailval = send_message_smtp(&tmp_ac, to_list, fp);
934                         }
935                 }
936                 if (mailval < 0) {
937                         if (!local)
938                                 alertpanel_error_log(
939                                         _("Error occurred while sending the message to `%s'."),
940                                         mailac ? mailac->smtp_server : smtpserver);
941                         else
942                                 alertpanel_error_log(
943                                         _("Error occurred while sending the message with command `%s'."),
944                                         (mailac && mailac->use_mail_command && 
945                                          mailac->mail_command && (*mailac->mail_command)) ? 
946                                                 mailac->mail_command : prefs_common.extsend_cmd);
947                 }
948         }
949
950         fseek(fp, filepos, SEEK_SET);
951         if (newsgroup_list && (newsval == 0)) {
952                 Folder *folder;
953                 gchar *tmp = NULL;
954                 FILE *tmpfp;
955
956                 /* write to temporary file */
957                 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
958                             G_DIR_SEPARATOR, (gint)file);
959                 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
960                         FILE_OP_ERROR(tmp, "fopen");
961                         newsval = -1;
962                         alertpanel_error(_("Could not create temporary file for news sending."));
963                 } else {
964                         if (change_file_mode_rw(tmpfp, tmp) < 0) {
965                                 FILE_OP_ERROR(tmp, "chmod");
966                                 g_warning("can't change file mode\n");
967                         }
968
969                         while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
970                                 if (fputs(buf, tmpfp) == EOF) {
971                                         FILE_OP_ERROR(tmp, "fputs");
972                                         newsval = -1;
973                                         alertpanel_error(_("Error when writing temporary file for news sending."));
974                                 }
975                         }
976                         fclose(tmpfp);
977
978                         if (newsval == 0) {
979                                 debug_print("Sending message by news\n");
980
981                                 folder = FOLDER(newsac->folder);
982
983                                 newsval = news_post(folder, tmp);
984                                 if (newsval < 0) {
985                                         alertpanel_error(_("Error occurred while posting the message to %s ."),
986                                                  newsac->nntp_server);
987                                 }
988                         }
989                         unlink(tmp);
990                 }
991                 g_free(tmp);
992         }
993
994         slist_free_strings(to_list);
995         g_slist_free(to_list);
996         slist_free_strings(newsgroup_list);
997         g_slist_free(newsgroup_list);
998         g_free(from);
999         g_free(smtpserver);
1000         fclose(fp);
1001
1002         /* save message to outbox */
1003         if (mailval == 0 && newsval == 0 && savecopyfolder) {
1004                 FolderItem *outbox;
1005
1006                 debug_print("saving sent message...\n");
1007
1008                 outbox = folder_find_item_from_identifier(savecopyfolder);
1009                 if (!outbox)
1010                         outbox = folder_get_default_outbox();
1011
1012                 procmsg_save_to_outbox(outbox, file, TRUE);
1013         }
1014
1015         if (replymessageid != NULL || fwdmessageid != NULL) {
1016                 gchar **tokens;
1017                 FolderItem *item;
1018                 
1019                 if (replymessageid != NULL)
1020                         tokens = g_strsplit(replymessageid, "\x7f", 0);
1021                 else
1022                         tokens = g_strsplit(fwdmessageid, "\x7f", 0);
1023                 item = folder_find_item_from_identifier(tokens[0]);
1024                 if (item != NULL) {
1025                         MsgInfo *msginfo;
1026                         
1027                         msginfo = folder_item_get_msginfo(item, atoi(tokens[1]));
1028                         if ((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1029                                 procmsg_msginfo_free(msginfo);
1030                                 msginfo = NULL;
1031                         }
1032                         
1033                         if (msginfo == NULL) {
1034                                 msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]);
1035                         }
1036                         
1037                         if (msginfo != NULL) {
1038                                 if (replymessageid != NULL) {
1039                                         procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1040                                         procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1041                                 } 
1042                                 else {
1043                                         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
1044                                         procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0);
1045                                 }
1046                                 procmsg_msginfo_free(msginfo);
1047                         }
1048                 }
1049                 g_strfreev(tokens);
1050         }
1051
1052         g_free(savecopyfolder);
1053         g_free(replymessageid);
1054         g_free(fwdmessageid);
1055         
1056         return (newsval != 0 ? newsval : mailval);
1057 }
1058
1059 #define CHANGE_FLAGS(msginfo) \
1060 { \
1061 if (msginfo->folder->folder->class->change_flags != NULL) \
1062 msginfo->folder->folder->class->change_flags(msginfo->folder->folder, \
1063                                              msginfo->folder, \
1064                                              msginfo); \
1065 }
1066
1067 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1068 {
1069         FolderItem *item;
1070         MsgInfoUpdate msginfo_update;
1071
1072         g_return_if_fail(msginfo != NULL);
1073         item = msginfo->folder;
1074         g_return_if_fail(item != NULL);
1075         
1076         debug_print("Setting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1077
1078         /* if new flag is set */
1079         if ((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
1080            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1081                 item->new++;
1082         }
1083
1084         /* if unread flag is set */
1085         if ((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
1086            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1087                 item->unread++;
1088         }
1089
1090         if (!MSG_IS_UNREAD(msginfo->flags) &&(perm_flags & MSG_UNREAD)
1091         && procmsg_msg_has_marked_parent(msginfo)) {
1092                 item->unreadmarked++;
1093         }
1094         
1095         if (!MSG_IS_MARKED(msginfo->flags) && (perm_flags & MSG_MARKED)) {
1096                 procmsg_update_unread_children(msginfo, TRUE);
1097         }
1098
1099
1100         /* if ignore thread flag is set */
1101         if ((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1102                 if (MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
1103                         item->new--;
1104                 }
1105                 if (MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
1106                         item->unread--;
1107                 }
1108                 if ((perm_flags & MSG_UNREAD) || (MSG_IS_UNREAD(msginfo->flags)
1109                 && procmsg_msg_has_marked_parent(msginfo))) {
1110                         item->unreadmarked--;
1111                 }
1112                 if ((perm_flags & MSG_MARKED) || (MSG_IS_MARKED(msginfo->flags)
1113                 && !MSG_IS_IGNORE_THREAD(msginfo->flags))) {
1114                         procmsg_update_unread_children(msginfo, FALSE);
1115                 }
1116
1117         }
1118
1119         if (FOLDER_TYPE(msginfo->folder->folder) == F_IMAP)
1120                 imap_msg_set_perm_flags(msginfo, perm_flags);
1121
1122         msginfo->flags.perm_flags |= perm_flags;
1123         msginfo->flags.tmp_flags |= tmp_flags;
1124
1125         msginfo_update.msginfo = msginfo;
1126         hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1127         folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1128
1129         CHANGE_FLAGS(msginfo);
1130 }
1131
1132 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1133 {
1134         FolderItem *item;
1135         MsgInfoUpdate msginfo_update;
1136
1137         g_return_if_fail(msginfo != NULL);
1138         item = msginfo->folder;
1139         g_return_if_fail(item != NULL); 
1140         
1141         debug_print("Unsetting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
1142
1143         /* if new flag is unset */
1144         if ((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
1145            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1146                 item->new--;
1147         }
1148
1149         /* if unread flag is unset */
1150         if ((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
1151            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1152                 item->unread--;
1153         }
1154         
1155         if (MSG_IS_UNREAD(msginfo->flags) && (perm_flags & MSG_UNREAD)
1156         && !MSG_IS_IGNORE_THREAD(msginfo->flags)
1157         && procmsg_msg_has_marked_parent(msginfo)) {
1158                 item->unreadmarked--;
1159         }
1160
1161         if (MSG_IS_MARKED(msginfo->flags) && (perm_flags & MSG_MARKED)
1162         && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1163                 procmsg_update_unread_children(msginfo, FALSE);
1164         }
1165
1166         /* if ignore thread flag is unset */
1167         if ((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1168                 if (MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
1169                         item->new++;
1170                 }
1171                 if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
1172                         item->unread++;
1173                 }
1174                 if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)
1175                 && procmsg_msg_has_marked_parent(msginfo)) {
1176                         item->unreadmarked++;
1177                 }
1178                 if (MSG_IS_MARKED(msginfo->flags) && !(perm_flags & MSG_MARKED)) {
1179                         procmsg_update_unread_children(msginfo, TRUE);
1180                 }
1181
1182         }
1183
1184         if (FOLDER_TYPE(msginfo->folder->folder) == F_IMAP)
1185                 imap_msg_unset_perm_flags(msginfo, perm_flags);
1186
1187         msginfo->flags.perm_flags &= ~perm_flags;
1188         msginfo->flags.tmp_flags &= ~tmp_flags;
1189
1190         msginfo_update.msginfo = msginfo;
1191         hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1192         folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
1193
1194         CHANGE_FLAGS(msginfo);
1195 }
1196
1197 /*!
1198  *\brief        check for flags (e.g. mark) in prior msgs of current thread
1199  *
1200  *\param        info Current message
1201  *\param        perm_flags Flags to be checked
1202  *\param        parentmsgs Hash of prior msgs to avoid loops
1203  *
1204  *\return       gboolean TRUE if perm_flags are found
1205  */
1206 gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
1207                 MsgPermFlags perm_flags, GHashTable *parentmsgs)
1208 {
1209         MsgInfo *tmp;
1210
1211         g_return_val_if_fail(info != NULL, FALSE);
1212
1213         if (info != NULL && info->folder != NULL && info->inreplyto != NULL) {
1214                 tmp = folder_item_get_msginfo_by_msgid(info->folder,
1215                                 info->inreplyto);
1216                 if (tmp && (tmp->flags.perm_flags & perm_flags)) {
1217                         procmsg_msginfo_free(tmp);
1218                         return TRUE;
1219                 } else if (tmp != NULL) {
1220                         gboolean result;
1221
1222                         if (g_hash_table_lookup(parentmsgs, info)) {
1223                                 debug_print("loop detected: %s%c%d\n",
1224                                         folder_item_get_path(info->folder),
1225                                         G_DIR_SEPARATOR, info->msgnum);
1226                                 result = FALSE;
1227                         } else {
1228                                 g_hash_table_insert(parentmsgs, info, "1");
1229                                 result = procmsg_msg_has_flagged_parent_real(
1230                                     tmp, perm_flags, parentmsgs);
1231                         }
1232                         procmsg_msginfo_free(tmp);
1233                         return result;
1234                 } else {
1235                         return FALSE;
1236                 }
1237         } else
1238                 return FALSE;
1239 }
1240
1241 /*!
1242  *\brief        Callback for cleaning up hash of parentmsgs
1243  */
1244 gboolean parentmsgs_hash_remove(gpointer key,
1245                             gpointer value,
1246                             gpointer user_data)
1247 {
1248         return TRUE;
1249 }
1250
1251 /*!
1252  *\brief        Set up list of parentmsgs
1253  *              See procmsg_msg_has_flagged_parent_real()
1254  */
1255 gboolean procmsg_msg_has_flagged_parent(MsgInfo *info, MsgPermFlags perm_flags)
1256 {
1257         gboolean result;
1258         GHashTable *parentmsgs = g_hash_table_new(NULL, NULL); 
1259
1260         result = procmsg_msg_has_flagged_parent_real(info, perm_flags, parentmsgs);
1261         g_hash_table_foreach_remove(parentmsgs, parentmsgs_hash_remove, NULL);
1262         g_hash_table_destroy(parentmsgs);
1263         return result;
1264 }
1265
1266 /*!
1267  *\brief        Check if msgs prior in thread are marked
1268  *              See procmsg_msg_has_flagged_parent_real()
1269  */
1270 gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
1271 {
1272         return procmsg_msg_has_flagged_parent(info, MSG_MARKED);
1273 }
1274
1275
1276 GSList *procmsg_find_children_func(MsgInfo *info, 
1277                                    GSList *children, GSList *all)
1278 {
1279         GSList *cur;
1280
1281         g_return_val_if_fail(info!=NULL, children);
1282         if (info->msgid == NULL)
1283                 return children;
1284
1285         for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1286                 MsgInfo *tmp = (MsgInfo *)cur->data;
1287                 if (tmp->inreplyto && !strcmp(tmp->inreplyto, info->msgid)) {
1288                         /* Check if message is already in the list */
1289                         if ((children == NULL) || 
1290                             (g_slist_index(children, tmp) == -1)) {
1291                                 children = g_slist_prepend(children,
1292                                                 procmsg_msginfo_new_ref(tmp));
1293                                 children = procmsg_find_children_func(tmp, 
1294                                                         children, 
1295                                                         all);
1296                         }
1297                 }
1298         }
1299         return children;
1300 }
1301
1302 GSList *procmsg_find_children (MsgInfo *info)
1303 {
1304         GSList *children;
1305         GSList *all, *cur;
1306
1307         g_return_val_if_fail(info!=NULL, NULL);
1308         all = folder_item_get_msg_list(info->folder);
1309         children = procmsg_find_children_func(info, NULL, all);
1310         if (children != NULL) {
1311                 for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
1312                         /* this will not free the used pointers
1313                            created with procmsg_msginfo_new_ref */
1314                         procmsg_msginfo_free((MsgInfo *)cur->data);
1315                 }
1316         }
1317         g_slist_free(all);
1318
1319         return children;
1320 }
1321
1322 void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
1323 {
1324         GSList *children = procmsg_find_children(info);
1325         GSList *cur;
1326         for (cur = children; cur != NULL; cur = g_slist_next(cur)) {
1327                 MsgInfo *tmp = (MsgInfo *)cur->data;
1328                 if(MSG_IS_UNREAD(tmp->flags) && !MSG_IS_IGNORE_THREAD(tmp->flags)) {
1329                         if(newly_marked) 
1330                                 info->folder->unreadmarked++;
1331                         else
1332                                 info->folder->unreadmarked--;
1333                         folder_item_update(info->folder, F_ITEM_UPDATE_MSGCNT);
1334                 }
1335                 procmsg_msginfo_free(tmp);
1336         }
1337         g_slist_free(children);
1338 }
1339
1340 /**
1341  * Set the destination folder for a copy or move operation
1342  *
1343  * \param msginfo The message which's destination folder is changed
1344  * \param to_folder The destination folder for the operation
1345  */
1346 void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
1347 {
1348         if(msginfo->to_folder != NULL) {
1349                 msginfo->to_folder->op_count--;
1350                 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1351         }
1352         msginfo->to_folder = to_folder;
1353         if(to_folder != NULL) {
1354                 to_folder->op_count++;
1355                 folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
1356         }
1357 }