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