* src/utils.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
931                 folder_item_scan(outbox);
932                 if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
933                         g_warning("can't save message\n");
934                         unlink(tmp);
935                         return -1;
936                 }
937         } else {
938                 folder_item_scan(outbox);
939                 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
940                         g_warning("can't save message\n");
941                         return -1;
942                 }
943                 return -1;
944         }
945         msginfo = folder_item_fetch_msginfo(outbox, num);
946         if(msginfo != NULL) {
947             procmsg_msginfo_unset_flags(msginfo, ~0, ~0);
948             procmsg_msginfo_free(msginfo);
949         }
950
951         if(is_queued) {
952                 unlink(file);
953         }
954
955         return 0;
956 }
957
958 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
959 {
960         static const gchar *def_cmd = "lpr %s";
961         static guint id = 0;
962         gchar *prtmp;
963         FILE *tmpfp, *prfp;
964         gchar buf[1024];
965         gchar *p;
966
967         g_return_if_fail(msginfo);
968
969         if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
970                 g_warning(_("Can't get text part\n"));
971                 return;
972         }
973
974         prtmp = g_strdup_printf("%s%cprinttmp.%08x",
975                                 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
976
977         if ((prfp = fopen(prtmp, "wb")) == NULL) {
978                 FILE_OP_ERROR(prtmp, "fopen");
979                 g_free(prtmp);
980                 fclose(tmpfp);
981                 return;
982         }
983
984         if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
985         if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
986         if (msginfo->to)   fprintf(prfp, "To: %s\n", msginfo->to);
987         if (msginfo->cc)   fprintf(prfp, "Cc: %s\n", msginfo->cc);
988         if (msginfo->newsgroups)
989                 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
990         if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
991         fputc('\n', prfp);
992
993         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
994                 fputs(buf, prfp);
995
996         fclose(prfp);
997         fclose(tmpfp);
998
999         if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1000             !strchr(p + 2, '%'))
1001                 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
1002         else {
1003                 if (cmdline)
1004                         g_warning(_("Print command line is invalid: `%s'\n"),
1005                                   cmdline);
1006                 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
1007         }
1008
1009         g_free(prtmp);
1010
1011         g_strchomp(buf);
1012         if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
1013         system(buf);
1014 }
1015
1016 MsgInfo *procmsg_msginfo_new_ref(MsgInfo *msginfo)
1017 {
1018         msginfo->refcnt++;
1019         
1020         return msginfo;
1021 }
1022
1023 MsgInfo *procmsg_msginfo_new()
1024 {
1025         MsgInfo *newmsginfo;
1026
1027         newmsginfo = g_new0(MsgInfo, 1);
1028         newmsginfo->refcnt = 1;
1029         
1030         return newmsginfo;
1031 }
1032
1033 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
1034 {
1035         MsgInfo *newmsginfo;
1036
1037         if (msginfo == NULL) return NULL;
1038
1039         newmsginfo = g_new0(MsgInfo, 1);
1040
1041         newmsginfo->refcnt = 1;
1042
1043 #define MEMBCOPY(mmb)   newmsginfo->mmb = msginfo->mmb
1044 #define MEMBDUP(mmb)    newmsginfo->mmb = msginfo->mmb ? \
1045                         g_strdup(msginfo->mmb) : NULL
1046
1047         MEMBCOPY(msgnum);
1048         MEMBCOPY(size);
1049         MEMBCOPY(mtime);
1050         MEMBCOPY(date_t);
1051         MEMBCOPY(flags);
1052
1053         MEMBDUP(fromname);
1054
1055         MEMBDUP(date);
1056         MEMBDUP(from);
1057         MEMBDUP(to);
1058         MEMBDUP(cc);
1059         MEMBDUP(newsgroups);
1060         MEMBDUP(subject);
1061         MEMBDUP(msgid);
1062         MEMBDUP(inreplyto);
1063         MEMBDUP(xref);
1064
1065         MEMBCOPY(folder);
1066         MEMBCOPY(to_folder);
1067
1068         MEMBDUP(xface);
1069         MEMBDUP(dispositionnotificationto);
1070         MEMBDUP(returnreceiptto);
1071         MEMBDUP(references);
1072
1073         MEMBCOPY(score);
1074         MEMBCOPY(threadscore);
1075
1076         return newmsginfo;
1077 }
1078
1079 void procmsg_msginfo_free(MsgInfo *msginfo)
1080 {
1081         if (msginfo == NULL) return;
1082
1083         msginfo->refcnt--;
1084         if(msginfo->refcnt > 0)
1085                 return;
1086
1087         g_free(msginfo->fromspace);
1088         g_free(msginfo->references);
1089         g_free(msginfo->returnreceiptto);
1090         g_free(msginfo->dispositionnotificationto);
1091         g_free(msginfo->xface);
1092
1093         g_free(msginfo->fromname);
1094
1095         g_free(msginfo->date);
1096         g_free(msginfo->from);
1097         g_free(msginfo->to);
1098         g_free(msginfo->cc);
1099         g_free(msginfo->newsgroups);
1100         g_free(msginfo->subject);
1101         g_free(msginfo->msgid);
1102         g_free(msginfo->inreplyto);
1103         g_free(msginfo->xref);
1104
1105         g_free(msginfo);
1106 }
1107
1108 guint procmsg_msginfo_memusage(MsgInfo *msginfo)
1109 {
1110         guint memusage = 0;
1111         
1112         memusage += sizeof(MsgInfo);
1113         if(msginfo->fromname)
1114                 memusage += strlen(msginfo->fromname);
1115         if(msginfo->date)
1116                 memusage += strlen(msginfo->date);
1117         if(msginfo->from)
1118                 memusage += strlen(msginfo->from);
1119         if(msginfo->to)
1120                 memusage += strlen(msginfo->to);
1121         if(msginfo->cc)
1122                 memusage += strlen(msginfo->cc);
1123         if(msginfo->newsgroups)
1124                 memusage += strlen(msginfo->newsgroups);
1125         if(msginfo->subject)
1126                 memusage += strlen(msginfo->subject);
1127         if(msginfo->msgid)
1128                 memusage += strlen(msginfo->msgid);
1129         if(msginfo->inreplyto)
1130                 memusage += strlen(msginfo->inreplyto);
1131         if(msginfo->xface)
1132                 memusage += strlen(msginfo->xface);
1133         if(msginfo->dispositionnotificationto)
1134                 memusage += strlen(msginfo->dispositionnotificationto);
1135         if(msginfo->returnreceiptto)
1136                 memusage += strlen(msginfo->returnreceiptto);
1137         if(msginfo->references)
1138                 memusage += strlen(msginfo->references);
1139         if(msginfo->fromspace)
1140                 memusage += strlen(msginfo->fromspace);
1141
1142         return memusage;
1143 }
1144
1145 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
1146 {
1147         const MsgInfo *msginfo1 = a;
1148         const MsgInfo *msginfo2 = b;
1149
1150         if (!msginfo1)
1151                 return -1;
1152         if (!msginfo2)
1153                 return -1;
1154
1155         return msginfo1->msgnum - msginfo2->msgnum;
1156 }
1157
1158 enum
1159 {
1160         Q_SENDER           = 0,
1161         Q_SMTPSERVER       = 1,
1162         Q_RECIPIENTS       = 2,
1163         Q_NEWSGROUPS       = 3,
1164         Q_MAIL_ACCOUNT_ID  = 4,
1165         Q_NEWS_ACCOUNT_ID  = 5,
1166         Q_SAVE_COPY_FOLDER = 6,
1167         Q_REPLY_MESSAGE_ID = 7,
1168 };
1169
1170 gint procmsg_send_message_queue(const gchar *file)
1171 {
1172         static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
1173                                        {"SSV:",  NULL, FALSE},
1174                                        {"R:",    NULL, FALSE},
1175                                        {"NG:",   NULL, FALSE},
1176                                        {"MAID:", NULL, FALSE},
1177                                        {"NAID:", NULL, FALSE},
1178                                        {"SCF:",  NULL, FALSE},
1179                                        {"RMID:", NULL, FALSE},
1180                                        {NULL,    NULL, FALSE}};
1181         FILE *fp;
1182         gint filepos;
1183         gint mailval = 0, newsval = 0;
1184         gchar *from = NULL;
1185         gchar *smtpserver = NULL;
1186         GSList *to_list = NULL;
1187         GSList *newsgroup_list = NULL;
1188         gchar *savecopyfolder = NULL;
1189         gchar *replymessageid = NULL;
1190         gchar buf[BUFFSIZE];
1191         gint hnum;
1192         PrefsAccount *mailac = NULL, *newsac = NULL;
1193         int local = 0;
1194
1195         g_return_val_if_fail(file != NULL, -1);
1196
1197         if ((fp = fopen(file, "rb")) == NULL) {
1198                 FILE_OP_ERROR(file, "fopen");
1199                 return -1;
1200         }
1201
1202         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
1203                != -1) {
1204                 gchar *p = buf + strlen(qentry[hnum].name);
1205
1206                 switch (hnum) {
1207                 case Q_SENDER:
1208                         if (!from) from = g_strdup(p);
1209                         break;
1210                 case Q_SMTPSERVER:
1211                         if (!smtpserver) smtpserver = g_strdup(p);
1212                         break;
1213                 case Q_RECIPIENTS:
1214                         to_list = address_list_append(to_list, p);
1215                         break;
1216                 case Q_NEWSGROUPS:
1217                         newsgroup_list = newsgroup_list_append(newsgroup_list, p);
1218                         break;
1219                 case Q_MAIL_ACCOUNT_ID:
1220                         mailac = account_find_from_id(atoi(p));
1221                         break;
1222                 case Q_NEWS_ACCOUNT_ID:
1223                         newsac = account_find_from_id(atoi(p));
1224                         break;
1225                 case Q_SAVE_COPY_FOLDER:
1226                         if (!savecopyfolder) savecopyfolder = g_strdup(p);
1227                         break;
1228                 case Q_REPLY_MESSAGE_ID:
1229                         if (!replymessageid) replymessageid = g_strdup(p);
1230                         break;
1231                 }
1232         }
1233         filepos = ftell(fp);
1234
1235         fseek(fp, filepos, SEEK_SET);
1236         if (to_list) {
1237                 debug_print(_("Sending message by mail\n"));
1238                 if(!from) {
1239                         g_warning(_("Queued message header is broken.\n"));
1240                         mailval = -1;
1241                 } else if (mailac && mailac->use_mail_command &&
1242                            mailac->mail_command && (* mailac->mail_command)) {
1243                         mailval = send_message_local(mailac->mail_command, fp);
1244                         local = 1;
1245                 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
1246                         mailval = send_message_local(prefs_common.extsend_cmd, fp);
1247                         local = 1;
1248                 } else {
1249                         if (!mailac) {
1250                                 mailac = account_find_from_smtp_server(from, smtpserver);
1251                                 if (!mailac) {
1252                                         g_warning(_("Account not found. "
1253                                                     "Using current account...\n"));
1254                                         mailac = cur_account;
1255                                 }
1256                         }
1257
1258                         if (mailac)
1259                                 mailval = send_message_smtp(mailac, to_list, fp);
1260                         else {
1261                                 PrefsAccount tmp_ac;
1262
1263                                 g_warning(_("Account not found.\n"));
1264
1265                                 memset(&tmp_ac, 0, sizeof(PrefsAccount));
1266                                 tmp_ac.address = from;
1267                                 tmp_ac.smtp_server = smtpserver;
1268                                 tmp_ac.smtpport = SMTP_PORT;
1269                                 mailval = send_message_smtp(&tmp_ac, to_list, fp);
1270                         }
1271                 }
1272                 if (mailval < 0) {
1273                         if (!local)
1274                                 alertpanel_error(
1275                                         _("Error occurred while sending the message to `%s'."),
1276                                         mailac ? mailac->smtp_server : smtpserver);
1277                         else
1278                                 alertpanel_error(
1279                                         _("Error occurred while sending the message with command `%s'."),
1280                                         (mailac && mailac->use_mail_command && 
1281                                          mailac->mail_command && (*mailac->mail_command)) ? 
1282                                                 mailac->mail_command : prefs_common.extsend_cmd);
1283                 }
1284         }
1285
1286         if(newsgroup_list && (newsval == 0)) {
1287                 Folder *folder;
1288                 gchar *tmp = NULL;
1289                 FILE *tmpfp;
1290
1291                 /* write to temporary file */
1292                 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1293                             G_DIR_SEPARATOR, (gint)file);
1294                 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
1295                         FILE_OP_ERROR(tmp, "fopen");
1296                         newsval = -1;
1297                         alertpanel_error(_("Could not create temporary file for news sending."));
1298                 } else {
1299                         if (change_file_mode_rw(tmpfp, tmp) < 0) {
1300                                 FILE_OP_ERROR(tmp, "chmod");
1301                                 g_warning(_("can't change file mode\n"));
1302                         }
1303
1304                         while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
1305                                 if (fputs(buf, tmpfp) == EOF) {
1306                                         FILE_OP_ERROR(tmp, "fputs");
1307                                         newsval = -1;
1308                                         alertpanel_error(_("Error when writing temporary file for news sending."));
1309                                 }
1310                         }
1311                         fclose(tmpfp);
1312
1313                         if(newsval == 0) {
1314                                 debug_print(_("Sending message by news\n"));
1315
1316                                 folder = FOLDER(newsac->folder);
1317
1318                                 newsval = news_post(folder, tmp);
1319                                 if (newsval < 0) {
1320                                         alertpanel_error(_("Error occurred while posting the message to %s ."),
1321                                                  newsac->nntp_server);
1322                                 }
1323                         }
1324                         unlink(tmp);
1325                 }
1326                 g_free(tmp);
1327         }
1328
1329         slist_free_strings(to_list);
1330         g_slist_free(to_list);
1331         slist_free_strings(newsgroup_list);
1332         g_slist_free(newsgroup_list);
1333         g_free(from);
1334         g_free(smtpserver);
1335         fclose(fp);
1336
1337         /* save message to outbox */
1338         if (mailval == 0 && newsval == 0 && savecopyfolder) {
1339                 FolderItem *outbox;
1340
1341                 debug_print(_("saving sent message...\n"));
1342
1343                 outbox = folder_find_item_from_identifier(savecopyfolder);
1344                 if(!outbox)
1345                         outbox = folder_get_default_outbox();
1346
1347                 procmsg_save_to_outbox(outbox, file, TRUE);
1348         }
1349
1350         if(replymessageid != NULL) {
1351                 gchar **tokens;
1352                 FolderItem *item;
1353                 
1354                 tokens = g_strsplit(replymessageid, "\x7f", 0);
1355                 item = folder_find_item_from_identifier(tokens[0]);
1356                 if(item != NULL) {
1357                         MsgInfo *msginfo;
1358                         
1359                         msginfo = folder_item_fetch_msginfo(item, atoi(tokens[1]));
1360                         if((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1361                                 procmsg_msginfo_free(msginfo);
1362                                 msginfo = NULL;
1363                         }
1364                         
1365                         if(msginfo == NULL) {
1366                                 msginfo = folder_item_fetch_msginfo_by_id(item, tokens[2]);
1367                         }
1368                         
1369                         if(msginfo != NULL) {
1370                                 procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1371                                 procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1372
1373                                 procmsg_msginfo_free(msginfo);
1374                         }
1375                 }
1376                 g_strfreev(tokens);
1377         }
1378
1379         g_free(savecopyfolder);
1380         g_free(replymessageid);
1381         
1382         return (newsval != 0 ? newsval : mailval);
1383 }
1384
1385 #define CHANGE_FLAGS(msginfo) \
1386 { \
1387 if (msginfo->folder->folder->change_flags != NULL) \
1388 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
1389                                       msginfo->folder, \
1390                                       msginfo); \
1391 }
1392
1393 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1394 {
1395         gboolean changed = FALSE;
1396         FolderItem *item = msginfo->folder;
1397
1398         debug_print(_("Setting flags for message %d in folder %s\n"), msginfo->msgnum, item->path);
1399
1400         /* if new flag is set */
1401         if((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
1402            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1403                 item->new++;
1404                 changed = TRUE;
1405         }
1406
1407         /* if unread flag is set */
1408         if((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
1409            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1410                 item->unread++;
1411                 changed = TRUE;
1412         }
1413
1414         /* if ignore thread flag is set */
1415         if((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1416                 if(MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
1417                         item->new--;
1418                         changed = TRUE;
1419                 }
1420                 if(MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
1421                         item->unread--;
1422                         changed = TRUE;
1423                 }
1424         }
1425
1426         if (MSG_IS_IMAP(msginfo->flags))
1427                 imap_msg_set_perm_flags(msginfo, perm_flags);
1428
1429         msginfo->flags.perm_flags |= perm_flags;
1430         msginfo->flags.tmp_flags |= tmp_flags;
1431
1432         if(changed) {
1433                 folderview_update_item(item, FALSE);
1434         }
1435         CHANGE_FLAGS(msginfo);
1436         procmsg_msginfo_write_flags(msginfo);
1437 }
1438
1439 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1440 {
1441         gboolean changed = FALSE;
1442         FolderItem *item = msginfo->folder;
1443         
1444         debug_print(_("Unsetting flags for message %d in folder %s\n"), msginfo->msgnum, item->path);
1445
1446         /* if new flag is unset */
1447         if((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
1448            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1449                 item->new--;
1450                 changed = TRUE;
1451         }
1452
1453         /* if unread flag is unset */
1454         if((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
1455            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1456                 item->unread--;
1457                 changed = TRUE;
1458         }
1459
1460         /* if ignore thread flag is unset */
1461         if((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1462                 if(MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
1463                         item->new++;
1464                         changed = TRUE;
1465                 }
1466                 if(MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
1467                         item->unread++;
1468                         changed = TRUE;
1469                 }
1470         }
1471
1472         if (MSG_IS_IMAP(msginfo->flags))
1473                 imap_msg_unset_perm_flags(msginfo, perm_flags);
1474
1475         msginfo->flags.perm_flags &= ~perm_flags;
1476         msginfo->flags.tmp_flags &= ~tmp_flags;
1477
1478         if(changed) {
1479                 folderview_update_item(item, FALSE);
1480         }
1481         CHANGE_FLAGS(msginfo);
1482         procmsg_msginfo_write_flags(msginfo);
1483 }
1484
1485 void procmsg_msginfo_write_flags(MsgInfo *msginfo)
1486 {
1487         gchar *destdir;
1488         FILE *fp;
1489
1490         destdir = folder_item_get_path(msginfo->folder);
1491         if (!is_dir_exist(destdir))
1492                 make_dir_hier(destdir);
1493
1494         if ((fp = procmsg_open_mark_file(destdir, TRUE))) {
1495                 procmsg_write_flags(msginfo, fp);
1496                 fclose(fp);
1497         } else {
1498                 g_warning(_("Can't open mark file.\n"));
1499         }
1500         
1501         g_free(destdir);
1502 }