* src/procmsg.c
[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_msg_list(GSList *mlist)
338 {
339         GSList *cur;
340         MsgInfo *msginfo;
341         gint last = 0;
342
343         for (cur = mlist; cur != NULL; cur = cur->next) {
344                 msginfo = (MsgInfo *)cur->data;
345                 if (msginfo && msginfo->msgnum > last)
346                         last = msginfo->msgnum;
347         }
348
349         return last;
350 }
351
352 void procmsg_msg_list_free(GSList *mlist)
353 {
354         GSList *cur;
355         MsgInfo *msginfo;
356
357         for (cur = mlist; cur != NULL; cur = cur->next) {
358                 msginfo = (MsgInfo *)cur->data;
359                 procmsg_msginfo_free(msginfo);
360         }
361         g_slist_free(mlist);
362 }
363
364 void procmsg_write_cache(MsgInfo *msginfo, FILE *fp)
365 {
366         MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK;
367
368         WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
369         WRITE_CACHE_DATA_INT(msginfo->size, fp);
370         WRITE_CACHE_DATA_INT(msginfo->mtime, fp);
371         WRITE_CACHE_DATA_INT(msginfo->date_t, fp);
372         WRITE_CACHE_DATA_INT(flags, fp);
373
374         WRITE_CACHE_DATA(msginfo->fromname, fp);
375
376         WRITE_CACHE_DATA(msginfo->date, fp);
377         WRITE_CACHE_DATA(msginfo->from, fp);
378         WRITE_CACHE_DATA(msginfo->to, fp);
379         WRITE_CACHE_DATA(msginfo->cc, fp);
380         WRITE_CACHE_DATA(msginfo->newsgroups, fp);
381         WRITE_CACHE_DATA(msginfo->subject, fp);
382         WRITE_CACHE_DATA(msginfo->msgid, fp);
383         WRITE_CACHE_DATA(msginfo->inreplyto, fp);
384         WRITE_CACHE_DATA(msginfo->references, fp);
385         WRITE_CACHE_DATA(msginfo->xref, fp);
386
387 }
388
389 void procmsg_write_flags(MsgInfo *msginfo, FILE *fp)
390 {
391         MsgPermFlags flags = msginfo->flags.perm_flags;
392
393         WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
394         WRITE_CACHE_DATA_INT(flags, fp);
395 }
396
397 void procmsg_flush_mark_queue(FolderItem *item, FILE *fp)
398 {
399         MsgInfo *flaginfo;
400
401         g_return_if_fail(item != NULL);
402         g_return_if_fail(fp != NULL);
403
404         while (item->mark_queue != NULL) {
405                 flaginfo = (MsgInfo *)item->mark_queue->data;
406                 procmsg_write_flags(flaginfo, fp);
407                 procmsg_msginfo_free(flaginfo);
408                 item->mark_queue = g_slist_remove(item->mark_queue, flaginfo);
409         }
410 }
411
412 void procmsg_add_flags(FolderItem *item, gint num, MsgFlags flags)
413 {
414         FILE *fp;
415         gchar *path;
416         MsgInfo msginfo;
417
418         g_return_if_fail(item != NULL);
419
420         if (item->opened) {
421                 MsgInfo *queue_msginfo;
422
423                 queue_msginfo = g_new0(MsgInfo, 1);
424                 queue_msginfo->msgnum = num;
425                 queue_msginfo->flags = flags;
426                 item->mark_queue = g_slist_append
427                         (item->mark_queue, queue_msginfo);
428                 return;
429         }
430
431         path = folder_item_get_path(item);
432         g_return_if_fail(path != NULL);
433
434         if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL) {
435                 g_warning(_("can't open mark file\n"));
436                 g_free(path);
437                 return;
438         }
439         g_free(path);
440
441         msginfo.msgnum = num;
442         msginfo.flags = flags;
443
444         procmsg_write_flags(&msginfo, fp);
445         fclose(fp);
446 }
447
448 struct MarkSum {
449         gint *new;
450         gint *unread;
451         gint *total;
452         gint *min;
453         gint *max;
454         gint first;
455 };
456
457 static GHashTable *procmsg_read_mark_file(const gchar *folder)
458 {
459         FILE *fp;
460         GHashTable *mark_table = NULL;
461         gint num;
462         MsgFlags *flags;
463         MsgPermFlags perm_flags;
464
465         if ((fp = procmsg_open_mark_file(folder, FALSE)) == NULL)
466                 return NULL;
467
468         mark_table = g_hash_table_new(NULL, g_direct_equal);
469
470         while (fread(&num, sizeof(num), 1, fp) == 1) {
471                 if (fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) break;
472
473                 flags = g_new0(MsgFlags, 1);
474                 flags->perm_flags = perm_flags;
475     
476                 if(!MSG_IS_REALLY_DELETED(*flags)) {
477                         g_hash_table_insert(mark_table, GUINT_TO_POINTER(num), flags);
478                 } else {
479                         g_hash_table_remove(mark_table, GUINT_TO_POINTER(num));
480                 }
481         }
482
483         fclose(fp);
484         return mark_table;
485 }
486
487 FILE *procmsg_open_mark_file(const gchar *folder, gboolean append)
488 {
489         gchar *markfile;
490         FILE *fp;
491         gint ver;
492
493         markfile = g_strconcat(folder, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
494
495         if ((fp = fopen(markfile, "rb")) == NULL)
496                 debug_print(_("Mark file not found.\n"));
497         else if (fread(&ver, sizeof(ver), 1, fp) != 1 || MARK_VERSION != ver) {
498                 debug_print(_("Mark version is different (%d != %d). "
499                               "Discarding it.\n"), ver, MARK_VERSION);
500                 fclose(fp);
501                 fp = NULL;
502         }
503
504         /* read mode */
505         if (append == FALSE) {
506                 g_free(markfile);
507                 return fp;
508         }
509
510         if (fp) {
511                 /* reopen with append mode */
512                 fclose(fp);
513                 if ((fp = fopen(markfile, "ab")) == NULL)
514                         g_warning(_("Can't open mark file with append mode.\n"));
515         } else {
516                 /* open with overwrite mode if mark file doesn't exist or
517                    version is different */
518                 if ((fp = fopen(markfile, "wb")) == NULL)
519                         g_warning(_("Can't open mark file with write mode.\n"));
520                 else {
521                         ver = MARK_VERSION;
522                         WRITE_CACHE_DATA_INT(ver, fp);
523                 }
524         }
525
526         g_free(markfile);
527         return fp;
528 }
529
530 /* return the reversed thread tree */
531 GNode *procmsg_get_thread_tree(GSList *mlist)
532 {
533         GNode *root, *parent, *node, *next;
534         GHashTable *msgid_table;
535         GHashTable *subject_table;
536         MsgInfo *msginfo;
537         const gchar *msgid;
538         const gchar *subject;
539         GNode *found_subject;
540
541         root = g_node_new(NULL);
542         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
543         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
544
545         for (; mlist != NULL; mlist = mlist->next) {
546                 msginfo = (MsgInfo *)mlist->data;
547                 parent = root;
548
549                 if (msginfo->inreplyto) {
550                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
551                         if (parent == NULL) {
552                                 parent = root;
553                         } else {
554                                 if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
555                                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
556                                 }
557                         }
558                 }
559                 node = g_node_insert_data_before
560                         (parent, parent == root ? parent->children : NULL,
561                          msginfo);
562                 if ((msgid = msginfo->msgid) &&
563                     g_hash_table_lookup(msgid_table, msgid) == NULL)
564                         g_hash_table_insert(msgid_table, (gchar *)msgid, node);
565
566                 subject = msginfo->subject;
567                 found_subject = subject_table_lookup(subject_table,
568                                                      (gchar *) subject);
569                 if (found_subject == NULL)
570                         subject_table_insert(subject_table, (gchar *) subject,
571                                              node);
572                 else {
573                         /* replace if msg in table is older than current one 
574                          * can add here more stuff.  */
575                         if ( ((MsgInfo*)(found_subject->data))->date_t >
576                              ((MsgInfo*)(node->data))->date_t )  {
577                                 subject_table_remove(subject_table, (gchar *) subject);
578                                 subject_table_insert(subject_table, (gchar *) subject, node);
579                         }       
580                 }
581         }
582
583         /* complete the unfinished threads */
584         for (node = root->children; node != NULL; ) {
585                 next = node->next;
586                 msginfo = (MsgInfo *)node->data;
587                 parent = NULL;
588                 if (msginfo->inreplyto) 
589                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
590                 if (parent && parent != node) {
591                         g_node_unlink(node);
592                         g_node_insert_before
593                                 (parent, parent->children, node);
594                         /* CLAWS: ignore thread */
595                         if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
596                                 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
597                         }
598                 }
599                 node = next;
600         }
601
602         /* CLAWS: now see if the first level (below root) still has some nodes that can be
603          * threaded by subject line. we need to handle this in a special way to prevent
604          * circular reference from a node that has already been threaded by IN-REPLY-TO
605          * but is also in the subject line hash table */
606         for (node = root->children; node != NULL; ) {
607                 next = node->next;
608                 msginfo = (MsgInfo *) node->data;
609                 parent = NULL;
610                 if (subject_is_reply(msginfo->subject)) {
611                         parent = subject_table_lookup(subject_table,
612                                                       msginfo->subject);
613                         /* the node may already be threaded by IN-REPLY-TO,
614                            so go up in the tree to find the parent node */
615                         if (parent != NULL) {
616                                 if (g_node_is_ancestor(node, parent))
617                                         parent = NULL;
618                                 if (parent == node)
619                                         parent = NULL;
620                         }
621
622                         if (parent) {
623                                 g_node_unlink(node);
624                                 g_node_append(parent, node);
625                                 /* CLAWS: ignore thread */
626                                 if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
627                                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
628                                 }
629                         }
630                 }                                       
631                 node = next;
632         }               
633                 
634         g_hash_table_destroy(subject_table);
635         g_hash_table_destroy(msgid_table);
636
637         return root;
638 }
639
640 void procmsg_move_messages(GSList *mlist)
641 {
642         GSList *cur, *movelist = NULL;
643         MsgInfo *msginfo;
644         FolderItem *dest = NULL;
645
646         if (!mlist) return;
647
648         for (cur = mlist; cur != NULL; cur = cur->next) {
649                 msginfo = (MsgInfo *)cur->data;
650                 if (!dest) {
651                         dest = msginfo->to_folder;
652                         movelist = g_slist_append(movelist, msginfo);
653                 } else if (dest == msginfo->to_folder) {
654                         movelist = g_slist_append(movelist, msginfo);
655                 } else {
656                         folder_item_move_msgs_with_dest(dest, movelist);
657                         g_slist_free(movelist);
658                         movelist = NULL;
659                         dest = msginfo->to_folder;
660                         movelist = g_slist_append(movelist, msginfo);
661                 }
662         }
663
664         if (movelist) {
665                 folder_item_move_msgs_with_dest(dest, movelist);
666                 g_slist_free(movelist);
667         }
668 }
669
670 void procmsg_copy_messages(GSList *mlist)
671 {
672         GSList *cur, *copylist = NULL;
673         MsgInfo *msginfo;
674         FolderItem *dest = NULL;
675
676         if (!mlist) return;
677
678         /* 
679         
680         Horrible: Scanning 2 times for every copy!
681
682         hash = procmsg_to_folder_hash_table_create(mlist);
683         folder_item_scan_foreach(hash);
684         g_hash_table_destroy(hash);
685         */
686
687         for (cur = mlist; cur != NULL; cur = cur->next) {
688                 msginfo = (MsgInfo *)cur->data;
689                 if (!dest) {
690                         dest = msginfo->to_folder;
691                         copylist = g_slist_append(copylist, msginfo);
692                 } else if (dest == msginfo->to_folder) {
693                         copylist = g_slist_append(copylist, msginfo);
694                 } else {
695                         folder_item_copy_msgs_with_dest(dest, copylist);
696                         g_slist_free(copylist);
697                         copylist = NULL;
698                         dest = msginfo->to_folder;
699                         copylist = g_slist_append(copylist, msginfo);
700                 }
701         }
702
703         if (copylist) {
704                 folder_item_copy_msgs_with_dest(dest, copylist);
705                 g_slist_free(copylist);
706         }
707 }
708
709 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
710 {
711         gchar *path, *file;
712
713         g_return_val_if_fail(msginfo != NULL, NULL);
714
715         if (msginfo->plaintext_file)
716                 file = g_strdup(msginfo->plaintext_file);
717         else {
718                 path = folder_item_get_path(msginfo->folder);
719                 file = g_strconcat(path, G_DIR_SEPARATOR_S,
720                                    itos(msginfo->msgnum), NULL);
721                 g_free(path);
722         }
723
724         return file;
725 }
726
727 gchar *procmsg_get_message_file(MsgInfo *msginfo)
728 {
729         gchar *filename = NULL;
730
731         g_return_val_if_fail(msginfo != NULL, NULL);
732
733         filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
734         if (!filename)
735                 g_warning(_("can't fetch message %d\n"), msginfo->msgnum);
736
737         return filename;
738 }
739
740 FILE *procmsg_open_message(MsgInfo *msginfo)
741 {
742         FILE *fp;
743         gchar *file;
744
745         g_return_val_if_fail(msginfo != NULL, NULL);
746
747         file = procmsg_get_message_file_path(msginfo);
748         g_return_val_if_fail(file != NULL, NULL);
749
750         if (!is_file_exist(file)) {
751                 g_free(file);
752                 file = procmsg_get_message_file(msginfo);
753                 g_return_val_if_fail(file != NULL, NULL);
754         }
755
756         if ((fp = fopen(file, "rb")) == NULL) {
757                 FILE_OP_ERROR(file, "fopen");
758                 g_free(file);
759                 return NULL;
760         }
761
762         g_free(file);
763
764         if (MSG_IS_QUEUED(msginfo->flags)) {
765                 gchar buf[BUFFSIZE];
766
767                 while (fgets(buf, sizeof(buf), fp) != NULL)
768                         if (buf[0] == '\r' || buf[0] == '\n') break;
769         }
770
771         return fp;
772 }
773
774 #if USE_GPGME
775 FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
776 {
777         FILE *fp;
778         MimeInfo *mimeinfo_;
779
780         g_return_val_if_fail(msginfo != NULL, NULL);
781
782         if (mimeinfo) *mimeinfo = NULL;
783
784         if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
785
786         mimeinfo_ = procmime_scan_mime_header(fp);
787         if (!mimeinfo_) {
788                 fclose(fp);
789                 return NULL;
790         }
791
792         if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
793             rfc2015_is_encrypted(mimeinfo_)) {
794                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
795         }
796
797         if (MSG_IS_ENCRYPTED(msginfo->flags) &&
798             !msginfo->plaintext_file &&
799             !msginfo->decryption_failed) {
800                 rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
801                 if (msginfo->plaintext_file &&
802                     !msginfo->decryption_failed) {
803                         fclose(fp);
804                         procmime_mimeinfo_free_all(mimeinfo_);
805                         if ((fp = procmsg_open_message(msginfo)) == NULL)
806                                 return NULL;
807                         mimeinfo_ = procmime_scan_mime_header(fp);
808                         if (!mimeinfo_) {
809                                 fclose(fp);
810                                 return NULL;
811                         }
812                 }
813         }
814
815         if (mimeinfo) *mimeinfo = mimeinfo_;
816         return fp;
817 }
818 #endif
819
820 gboolean procmsg_msg_exist(MsgInfo *msginfo)
821 {
822         gchar *path;
823         gboolean ret;
824
825         if (!msginfo) return FALSE;
826
827         path = folder_item_get_path(msginfo->folder);
828         change_dir(path);
829         ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
830         g_free(path);
831
832         return ret;
833 }
834
835 void procmsg_empty_trash(void)
836 {
837         FolderItem *trash;
838         GList *cur;
839
840         for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
841                 trash = FOLDER(cur->data)->trash;
842                 if (trash && trash->total > 0)
843                         folder_item_remove_all_msg(trash);
844         }
845 }
846
847 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
848 {
849         gint ret = 0;
850         GSList *list, *elem;
851
852         if (!queue)
853                 queue = folder_get_default_queue();
854         g_return_val_if_fail(queue != NULL, -1);
855
856         folder_item_scan(queue);
857         list = folder_item_get_msg_list(queue);
858
859
860         for(elem = list; elem != NULL; elem = elem->next) {
861                 gchar *file;
862                 MsgInfo *msginfo;
863                 
864                 msginfo = (MsgInfo *)(elem->data);
865
866                 file = folder_item_fetch_msg(queue, msginfo->msgnum);
867                 if (file) {
868                         if (procmsg_send_message_queue(file) < 0) {
869                                 g_warning(_("Sending queued message %d failed.\n"), msginfo->msgnum);
870                                 ret = -1;
871                         } else {
872                         /* CLAWS: 
873                          * We save in procmsg_send_message_queue because
874                          * we need the destination folder from the queue
875                          * header
876                                                 
877                                 if (save_msgs)
878                                         procmsg_save_to_outbox
879                                                 (queue->folder->outbox,
880                                                  file, TRUE);
881 */
882                                 folder_item_remove_msg(queue, msginfo->msgnum);
883                         }
884                         g_free(file);
885                 }
886                 procmsg_msginfo_free(msginfo);
887         }
888
889         folderview_update_item(queue, FALSE);
890
891         return ret;
892 }
893
894 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
895                             gboolean is_queued)
896 {
897         gint num;
898         FILE *fp;
899         MsgInfo *msginfo;
900
901         debug_print(_("saving sent message...\n"));
902
903         if (!outbox)
904                 outbox = folder_get_default_outbox();
905         g_return_val_if_fail(outbox != NULL, -1);
906
907         /* remove queueing headers */
908         if (is_queued) {
909                 gchar tmp[MAXPATHLEN + 1];
910                 gchar buf[BUFFSIZE];
911                 FILE *outfp;
912
913                 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
914                            get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
915                 if ((fp = fopen(file, "rb")) == NULL) {
916                         FILE_OP_ERROR(file, "fopen");
917                         return -1;
918                 }
919                 if ((outfp = fopen(tmp, "wb")) == NULL) {
920                         FILE_OP_ERROR(tmp, "fopen");
921                         fclose(fp);
922                         return -1;
923                 }
924                 while (fgets(buf, sizeof(buf), fp) != NULL)
925                         if (buf[0] == '\r' || buf[0] == '\n') break;
926                 while (fgets(buf, sizeof(buf), fp) != NULL)
927                         fputs(buf, outfp);
928                 fclose(outfp);
929                 fclose(fp);
930                 Xstrdup_a(file, tmp, return -1);
931         }
932
933         if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
934                 g_warning(_("can't save message\n"));
935                 if(is_queued) {
936                         unlink(file);
937                 }
938                 return -1;
939         }
940         msginfo = folder_item_fetch_msginfo(outbox, num);
941         if(msginfo != NULL) {
942             procmsg_msginfo_unset_flags(msginfo, ~0, ~0);
943             procmsg_msginfo_free(msginfo);
944         }
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         Q_REPLY_MESSAGE_ID = 7,
1163 };
1164
1165 gint procmsg_send_message_queue(const gchar *file)
1166 {
1167         static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
1168                                        {"SSV:",  NULL, FALSE},
1169                                        {"R:",    NULL, FALSE},
1170                                        {"NG:",   NULL, FALSE},
1171                                        {"MAID:", NULL, FALSE},
1172                                        {"NAID:", NULL, FALSE},
1173                                        {"SCF:",  NULL, FALSE},
1174                                        {"RMID:", NULL, FALSE},
1175                                        {NULL,    NULL, FALSE}};
1176         FILE *fp;
1177         gint filepos;
1178         gint mailval = 0, newsval = 0;
1179         gchar *from = NULL;
1180         gchar *smtpserver = NULL;
1181         GSList *to_list = NULL;
1182         GSList *newsgroup_list = NULL;
1183         gchar *savecopyfolder = NULL;
1184         gchar *replymessageid = NULL;
1185         gchar buf[BUFFSIZE];
1186         gint hnum;
1187         PrefsAccount *mailac = NULL, *newsac = NULL;
1188         int local = 0;
1189
1190         g_return_val_if_fail(file != NULL, -1);
1191
1192         if ((fp = fopen(file, "rb")) == NULL) {
1193                 FILE_OP_ERROR(file, "fopen");
1194                 return -1;
1195         }
1196
1197         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
1198                != -1) {
1199                 gchar *p = buf + strlen(qentry[hnum].name);
1200
1201                 switch (hnum) {
1202                 case Q_SENDER:
1203                         if (!from) from = g_strdup(p);
1204                         break;
1205                 case Q_SMTPSERVER:
1206                         if (!smtpserver) smtpserver = g_strdup(p);
1207                         break;
1208                 case Q_RECIPIENTS:
1209                         to_list = address_list_append(to_list, p);
1210                         break;
1211                 case Q_NEWSGROUPS:
1212                         newsgroup_list = newsgroup_list_append(newsgroup_list, p);
1213                         break;
1214                 case Q_MAIL_ACCOUNT_ID:
1215                         mailac = account_find_from_id(atoi(p));
1216                         break;
1217                 case Q_NEWS_ACCOUNT_ID:
1218                         newsac = account_find_from_id(atoi(p));
1219                         break;
1220                 case Q_SAVE_COPY_FOLDER:
1221                         if (!savecopyfolder) savecopyfolder = g_strdup(p);
1222                         break;
1223                 case Q_REPLY_MESSAGE_ID:
1224                         if (!replymessageid) replymessageid = g_strdup(p);
1225                         break;
1226                 }
1227         }
1228         filepos = ftell(fp);
1229
1230         fseek(fp, filepos, SEEK_SET);
1231         if (to_list) {
1232                 debug_print(_("Sending message by mail\n"));
1233                 if(!from) {
1234                         g_warning(_("Queued message header is broken.\n"));
1235                         mailval = -1;
1236                 } else if (mailac && mailac->use_mail_command &&
1237                            mailac->mail_command && (* mailac->mail_command)) {
1238                         mailval = send_message_local(mailac->mail_command, fp);
1239                         local = 1;
1240                 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
1241                         mailval = send_message_local(prefs_common.extsend_cmd, fp);
1242                         local = 1;
1243                 } else {
1244                         if (!mailac) {
1245                                 mailac = account_find_from_smtp_server(from, smtpserver);
1246                                 if (!mailac) {
1247                                         g_warning(_("Account not found. "
1248                                                     "Using current account...\n"));
1249                                         mailac = cur_account;
1250                                 }
1251                         }
1252
1253                         if (mailac)
1254                                 mailval = send_message_smtp(mailac, to_list, fp);
1255                         else {
1256                                 PrefsAccount tmp_ac;
1257
1258                                 g_warning(_("Account not found.\n"));
1259
1260                                 memset(&tmp_ac, 0, sizeof(PrefsAccount));
1261                                 tmp_ac.address = from;
1262                                 tmp_ac.smtp_server = smtpserver;
1263                                 tmp_ac.smtpport = SMTP_PORT;
1264                                 mailval = send_message_smtp(&tmp_ac, to_list, fp);
1265                         }
1266                 }
1267                 if (mailval < 0) {
1268                         if (!local)
1269                                 alertpanel_error(
1270                                         _("Error occurred while sending the message to `%s'."),
1271                                         mailac ? mailac->smtp_server : smtpserver);
1272                         else
1273                                 alertpanel_error(
1274                                         _("Error occurred while sending the message with command `%s'."),
1275                                         (mailac && mailac->use_mail_command && 
1276                                          mailac->mail_command && (*mailac->mail_command)) ? 
1277                                                 mailac->mail_command : prefs_common.extsend_cmd);
1278                 }
1279         }
1280
1281         if(newsgroup_list && (newsval == 0)) {
1282                 Folder *folder;
1283                 gchar *tmp = NULL;
1284                 FILE *tmpfp;
1285
1286                 /* write to temporary file */
1287                 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1288                             G_DIR_SEPARATOR, (gint)file);
1289                 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
1290                         FILE_OP_ERROR(tmp, "fopen");
1291                         newsval = -1;
1292                         alertpanel_error(_("Could not create temporary file for news sending."));
1293                 } else {
1294                         if (change_file_mode_rw(tmpfp, tmp) < 0) {
1295                                 FILE_OP_ERROR(tmp, "chmod");
1296                                 g_warning(_("can't change file mode\n"));
1297                         }
1298
1299                         while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
1300                                 if (fputs(buf, tmpfp) == EOF) {
1301                                         FILE_OP_ERROR(tmp, "fputs");
1302                                         newsval = -1;
1303                                         alertpanel_error(_("Error when writing temporary file for news sending."));
1304                                 }
1305                         }
1306                         fclose(tmpfp);
1307
1308                         if(newsval == 0) {
1309                                 debug_print(_("Sending message by news\n"));
1310
1311                                 folder = FOLDER(newsac->folder);
1312
1313                                 newsval = news_post(folder, tmp);
1314                                 if (newsval < 0) {
1315                                         alertpanel_error(_("Error occurred while posting the message to %s ."),
1316                                                  newsac->nntp_server);
1317                                 }
1318                         }
1319                         unlink(tmp);
1320                 }
1321                 g_free(tmp);
1322         }
1323
1324         slist_free_strings(to_list);
1325         g_slist_free(to_list);
1326         slist_free_strings(newsgroup_list);
1327         g_slist_free(newsgroup_list);
1328         g_free(from);
1329         g_free(smtpserver);
1330         fclose(fp);
1331
1332         /* save message to outbox */
1333         if (mailval == 0 && newsval == 0 && savecopyfolder) {
1334                 FolderItem *outbox;
1335
1336                 debug_print(_("saving sent message...\n"));
1337
1338                 outbox = folder_find_item_from_identifier(savecopyfolder);
1339                 if(!outbox)
1340                         outbox = folder_get_default_outbox();
1341
1342                 procmsg_save_to_outbox(outbox, file, TRUE);
1343         }
1344
1345         if(replymessageid != NULL) {
1346                 gchar **tokens;
1347                 FolderItem *item;
1348                 
1349                 tokens = g_strsplit(replymessageid, "\x7f", 0);
1350                 item = folder_find_item_from_identifier(tokens[0]);
1351                 if(item != NULL) {
1352                         MsgInfo *msginfo;
1353                         
1354                         msginfo = folder_item_fetch_msginfo(item, atoi(tokens[1]));
1355                         if((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1356                                 procmsg_msginfo_free(msginfo);
1357                                 msginfo = NULL;
1358                         }
1359                         
1360                         if(msginfo == NULL) {
1361                                 msginfo = folder_item_fetch_msginfo_by_id(item, tokens[2]);
1362                         }
1363                         
1364                         if(msginfo != NULL) {
1365                                 procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1366                                 procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1367
1368                                 procmsg_msginfo_free(msginfo);
1369                         }
1370                 }
1371                 g_strfreev(tokens);
1372         }
1373
1374         g_free(savecopyfolder);
1375         g_free(replymessageid);
1376         
1377         return (newsval != 0 ? newsval : mailval);
1378 }
1379
1380 #define CHANGE_FLAGS(msginfo) \
1381 { \
1382 if (msginfo->folder->folder->change_flags != NULL) \
1383 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
1384                                       msginfo->folder, \
1385                                       msginfo); \
1386 }
1387
1388 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1389 {
1390         gboolean changed = FALSE;
1391         FolderItem *item = msginfo->folder;
1392
1393         debug_print(_("Setting flags for message %d in folder %s\n"), msginfo->msgnum, item->path);
1394
1395         /* if new flag is set */
1396         if((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
1397            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1398                 item->new++;
1399                 changed = TRUE;
1400         }
1401
1402         /* if unread flag is set */
1403         if((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
1404            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1405                 item->unread++;
1406                 changed = TRUE;
1407         }
1408
1409         /* if ignore thread flag is set */
1410         if((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1411                 if(MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
1412                         item->new--;
1413                         changed = TRUE;
1414                 }
1415                 if(MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
1416                         item->unread--;
1417                         changed = TRUE;
1418                 }
1419         }
1420
1421         if (MSG_IS_IMAP(msginfo->flags))
1422                 imap_msg_set_perm_flags(msginfo, perm_flags);
1423
1424         msginfo->flags.perm_flags |= perm_flags;
1425         msginfo->flags.tmp_flags |= tmp_flags;
1426
1427         if(changed) {
1428                 folderview_update_item(item, FALSE);
1429         }
1430         CHANGE_FLAGS(msginfo);
1431         procmsg_msginfo_write_flags(msginfo);
1432 }
1433
1434 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1435 {
1436         gboolean changed = FALSE;
1437         FolderItem *item = msginfo->folder;
1438         
1439         debug_print(_("Unsetting flags for message %d in folder %s\n"), msginfo->msgnum, item->path);
1440
1441         /* if new flag is unset */
1442         if((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
1443            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1444                 item->new--;
1445                 changed = TRUE;
1446         }
1447
1448         /* if unread flag is unset */
1449         if((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
1450            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1451                 item->unread--;
1452                 changed = TRUE;
1453         }
1454
1455         /* if ignore thread flag is unset */
1456         if((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1457                 if(MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
1458                         item->new++;
1459                         changed = TRUE;
1460                 }
1461                 if(MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
1462                         item->unread++;
1463                         changed = TRUE;
1464                 }
1465         }
1466
1467         if (MSG_IS_IMAP(msginfo->flags))
1468                 imap_msg_unset_perm_flags(msginfo, perm_flags);
1469
1470         msginfo->flags.perm_flags &= ~perm_flags;
1471         msginfo->flags.tmp_flags &= ~tmp_flags;
1472
1473         if(changed) {
1474                 folderview_update_item(item, FALSE);
1475         }
1476         CHANGE_FLAGS(msginfo);
1477         procmsg_msginfo_write_flags(msginfo);
1478 }
1479
1480 void procmsg_msginfo_write_flags(MsgInfo *msginfo)
1481 {
1482         gchar *destdir;
1483         FILE *fp;
1484
1485         destdir = folder_item_get_path(msginfo->folder);
1486         if (!is_dir_exist(destdir))
1487                 make_dir_hier(destdir);
1488
1489         if ((fp = procmsg_open_mark_file(destdir, TRUE))) {
1490                 procmsg_write_flags(msginfo, fp);
1491                 fclose(fp);
1492         } else {
1493                 g_warning(_("Can't open mark file.\n"));
1494         }
1495         
1496         g_free(destdir);
1497 }