* 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_hash_table_lookup(mark_table, GUINT_TO_POINTER(num));
474                 if (flags != NULL)
475                         g_free(flags);
476
477                 flags = g_new0(MsgFlags, 1);
478                 flags->perm_flags = perm_flags;
479     
480                 if(!MSG_IS_REALLY_DELETED(*flags)) {
481                         g_hash_table_insert(mark_table, GUINT_TO_POINTER(num), flags);
482                 } else {
483                         g_hash_table_remove(mark_table, GUINT_TO_POINTER(num));
484                 }
485         }
486
487         fclose(fp);
488         return mark_table;
489 }
490
491 FILE *procmsg_open_mark_file(const gchar *folder, gboolean append)
492 {
493         gchar *markfile;
494         FILE *fp;
495         gint ver;
496
497         markfile = g_strconcat(folder, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
498
499         if ((fp = fopen(markfile, "rb")) == NULL)
500                 debug_print(_("Mark file not found.\n"));
501         else if (fread(&ver, sizeof(ver), 1, fp) != 1 || MARK_VERSION != ver) {
502                 debug_print(_("Mark version is different (%d != %d). "
503                               "Discarding it.\n"), ver, MARK_VERSION);
504                 fclose(fp);
505                 fp = NULL;
506         }
507
508         /* read mode */
509         if (append == FALSE) {
510                 g_free(markfile);
511                 return fp;
512         }
513
514         if (fp) {
515                 /* reopen with append mode */
516                 fclose(fp);
517                 if ((fp = fopen(markfile, "ab")) == NULL)
518                         g_warning(_("Can't open mark file with append mode.\n"));
519         } else {
520                 /* open with overwrite mode if mark file doesn't exist or
521                    version is different */
522                 if ((fp = fopen(markfile, "wb")) == NULL)
523                         g_warning(_("Can't open mark file with write mode.\n"));
524                 else {
525                         ver = MARK_VERSION;
526                         WRITE_CACHE_DATA_INT(ver, fp);
527                 }
528         }
529
530         g_free(markfile);
531         return fp;
532 }
533
534 static gboolean procmsg_ignore_node(GNode *node, gpointer data)
535 {
536         MsgInfo *msginfo = (MsgInfo *)node->data;
537         
538         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
539 }
540
541 /* return the reversed thread tree */
542 GNode *procmsg_get_thread_tree(GSList *mlist)
543 {
544         GNode *root, *parent, *node, *next;
545         GHashTable *msgid_table;
546         GHashTable *subject_table;
547         MsgInfo *msginfo;
548         const gchar *msgid;
549         const gchar *subject;
550         GNode *found_subject;
551
552         root = g_node_new(NULL);
553         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
554         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
555
556         for (; mlist != NULL; mlist = mlist->next) {
557                 msginfo = (MsgInfo *)mlist->data;
558                 parent = root;
559
560                 if (msginfo->inreplyto) {
561                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
562                         if (parent == NULL) {
563                                 parent = root;
564                         } else {
565                                 if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
566                                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
567                                 }
568                         }
569                 }
570                 node = g_node_insert_data_before
571                         (parent, parent == root ? parent->children : NULL,
572                          msginfo);
573                 if ((msgid = msginfo->msgid) &&
574                     g_hash_table_lookup(msgid_table, msgid) == NULL)
575                         g_hash_table_insert(msgid_table, (gchar *)msgid, node);
576
577                 subject = msginfo->subject;
578                 found_subject = subject_table_lookup(subject_table,
579                                                      (gchar *) subject);
580                 if (found_subject == NULL)
581                         subject_table_insert(subject_table, (gchar *) subject,
582                                              node);
583                 else {
584                         /* replace if msg in table is older than current one 
585                          * can add here more stuff.  */
586                         if ( ((MsgInfo*)(found_subject->data))->date_t >
587                              ((MsgInfo*)(node->data))->date_t )  {
588                                 subject_table_remove(subject_table, (gchar *) subject);
589                                 subject_table_insert(subject_table, (gchar *) subject, node);
590                         }       
591                 }
592         }
593
594         /* complete the unfinished threads */
595         for (node = root->children; node != NULL; ) {
596                 next = node->next;
597                 msginfo = (MsgInfo *)node->data;
598                 parent = NULL;
599                 if (msginfo->inreplyto) 
600                         parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
601                 if (parent && parent != node) {
602                         g_node_unlink(node);
603                         g_node_insert_before
604                                 (parent, parent->children, node);
605                         /* CLAWS: ignore thread */
606                         if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
607                                 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
608                         }
609                 }
610                 node = next;
611         }
612
613         /* CLAWS: now see if the first level (below root) still has some nodes that can be
614          * threaded by subject line. we need to handle this in a special way to prevent
615          * circular reference from a node that has already been threaded by IN-REPLY-TO
616          * but is also in the subject line hash table */
617         for (node = root->children; node != NULL; ) {
618                 next = node->next;
619                 msginfo = (MsgInfo *) node->data;
620                 parent = NULL;
621                 if (subject_is_reply(msginfo->subject)) {
622                         parent = subject_table_lookup(subject_table,
623                                                       msginfo->subject);
624                         /* the node may already be threaded by IN-REPLY-TO,
625                            so go up in the tree to find the parent node */
626                         if (parent != NULL) {
627                                 if (g_node_is_ancestor(node, parent))
628                                         parent = NULL;
629                                 if (parent == node)
630                                         parent = NULL;
631                         }
632
633                         if (parent) {
634                                 g_node_unlink(node);
635                                 g_node_append(parent, node);
636                                 /* CLAWS: ignore thread */
637                                 if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
638                                         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
639                                 }
640                         }
641                 }                                       
642                 node = next;
643         }               
644
645         g_hash_table_destroy(subject_table);
646         g_hash_table_destroy(msgid_table);
647
648         return root;
649 }
650
651 void procmsg_move_messages(GSList *mlist)
652 {
653         GSList *cur, *movelist = NULL;
654         MsgInfo *msginfo;
655         FolderItem *dest = NULL;
656
657         if (!mlist) return;
658
659         for (cur = mlist; cur != NULL; cur = cur->next) {
660                 msginfo = (MsgInfo *)cur->data;
661                 if (!dest) {
662                         dest = msginfo->to_folder;
663                         movelist = g_slist_append(movelist, msginfo);
664                 } else if (dest == msginfo->to_folder) {
665                         movelist = g_slist_append(movelist, msginfo);
666                 } else {
667                         folder_item_move_msgs_with_dest(dest, movelist);
668                         g_slist_free(movelist);
669                         movelist = NULL;
670                         dest = msginfo->to_folder;
671                         movelist = g_slist_append(movelist, msginfo);
672                 }
673         }
674
675         if (movelist) {
676                 folder_item_move_msgs_with_dest(dest, movelist);
677                 g_slist_free(movelist);
678         }
679 }
680
681 void procmsg_copy_messages(GSList *mlist)
682 {
683         GSList *cur, *copylist = NULL;
684         MsgInfo *msginfo;
685         FolderItem *dest = NULL;
686
687         if (!mlist) return;
688
689         /* 
690         
691         Horrible: Scanning 2 times for every copy!
692
693         hash = procmsg_to_folder_hash_table_create(mlist);
694         folder_item_scan_foreach(hash);
695         g_hash_table_destroy(hash);
696         */
697
698         for (cur = mlist; cur != NULL; cur = cur->next) {
699                 msginfo = (MsgInfo *)cur->data;
700                 if (!dest) {
701                         dest = msginfo->to_folder;
702                         copylist = g_slist_append(copylist, msginfo);
703                 } else if (dest == msginfo->to_folder) {
704                         copylist = g_slist_append(copylist, msginfo);
705                 } else {
706                         folder_item_copy_msgs_with_dest(dest, copylist);
707                         g_slist_free(copylist);
708                         copylist = NULL;
709                         dest = msginfo->to_folder;
710                         copylist = g_slist_append(copylist, msginfo);
711                 }
712         }
713
714         if (copylist) {
715                 folder_item_copy_msgs_with_dest(dest, copylist);
716                 g_slist_free(copylist);
717         }
718 }
719
720 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
721 {
722         gchar *path, *file;
723
724         g_return_val_if_fail(msginfo != NULL, NULL);
725
726         if (msginfo->plaintext_file)
727                 file = g_strdup(msginfo->plaintext_file);
728         else {
729                 path = folder_item_get_path(msginfo->folder);
730                 file = g_strconcat(path, G_DIR_SEPARATOR_S,
731                                    itos(msginfo->msgnum), NULL);
732                 g_free(path);
733         }
734
735         return file;
736 }
737
738 gchar *procmsg_get_message_file(MsgInfo *msginfo)
739 {
740         gchar *filename = NULL;
741
742         g_return_val_if_fail(msginfo != NULL, NULL);
743
744         filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
745         if (!filename)
746                 g_warning(_("can't fetch message %d\n"), msginfo->msgnum);
747
748         return filename;
749 }
750
751 FILE *procmsg_open_message(MsgInfo *msginfo)
752 {
753         FILE *fp;
754         gchar *file;
755
756         g_return_val_if_fail(msginfo != NULL, NULL);
757
758         file = procmsg_get_message_file_path(msginfo);
759         g_return_val_if_fail(file != NULL, NULL);
760
761         if (!is_file_exist(file)) {
762                 g_free(file);
763                 file = procmsg_get_message_file(msginfo);
764                 g_return_val_if_fail(file != NULL, NULL);
765         }
766
767         if ((fp = fopen(file, "rb")) == NULL) {
768                 FILE_OP_ERROR(file, "fopen");
769                 g_free(file);
770                 return NULL;
771         }
772
773         g_free(file);
774
775         if (MSG_IS_QUEUED(msginfo->flags)) {
776                 gchar buf[BUFFSIZE];
777
778                 while (fgets(buf, sizeof(buf), fp) != NULL)
779                         if (buf[0] == '\r' || buf[0] == '\n') break;
780         }
781
782         return fp;
783 }
784
785 #if USE_GPGME
786 FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
787 {
788         FILE *fp;
789         MimeInfo *mimeinfo_;
790
791         g_return_val_if_fail(msginfo != NULL, NULL);
792
793         if (mimeinfo) *mimeinfo = NULL;
794
795         if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
796
797         mimeinfo_ = procmime_scan_mime_header(fp);
798         if (!mimeinfo_) {
799                 fclose(fp);
800                 return NULL;
801         }
802
803         if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
804             rfc2015_is_encrypted(mimeinfo_)) {
805                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
806         }
807
808         if (MSG_IS_ENCRYPTED(msginfo->flags) &&
809             !msginfo->plaintext_file &&
810             !msginfo->decryption_failed) {
811                 rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
812                 if (msginfo->plaintext_file &&
813                     !msginfo->decryption_failed) {
814                         fclose(fp);
815                         procmime_mimeinfo_free_all(mimeinfo_);
816                         if ((fp = procmsg_open_message(msginfo)) == NULL)
817                                 return NULL;
818                         mimeinfo_ = procmime_scan_mime_header(fp);
819                         if (!mimeinfo_) {
820                                 fclose(fp);
821                                 return NULL;
822                         }
823                 }
824         }
825
826         if (mimeinfo) *mimeinfo = mimeinfo_;
827         return fp;
828 }
829 #endif
830
831 gboolean procmsg_msg_exist(MsgInfo *msginfo)
832 {
833         gchar *path;
834         gboolean ret;
835
836         if (!msginfo) return FALSE;
837
838         path = folder_item_get_path(msginfo->folder);
839         change_dir(path);
840         ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
841         g_free(path);
842
843         return ret;
844 }
845
846 void procmsg_empty_trash(void)
847 {
848         FolderItem *trash;
849         GList *cur;
850
851         for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
852                 trash = FOLDER(cur->data)->trash;
853                 if (trash && trash->total > 0)
854                         folder_item_remove_all_msg(trash);
855         }
856 }
857
858 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
859 {
860         gint ret = 0;
861         GSList *list, *elem;
862
863         if (!queue)
864                 queue = folder_get_default_queue();
865         g_return_val_if_fail(queue != NULL, -1);
866
867         folder_item_scan(queue);
868         list = folder_item_get_msg_list(queue);
869
870
871         for(elem = list; elem != NULL; elem = elem->next) {
872                 gchar *file;
873                 MsgInfo *msginfo;
874                 
875                 msginfo = (MsgInfo *)(elem->data);
876
877                 file = folder_item_fetch_msg(queue, msginfo->msgnum);
878                 if (file) {
879                         if (procmsg_send_message_queue(file) < 0) {
880                                 g_warning(_("Sending queued message %d failed.\n"), msginfo->msgnum);
881                                 ret = -1;
882                         } else {
883                         /* CLAWS: 
884                          * We save in procmsg_send_message_queue because
885                          * we need the destination folder from the queue
886                          * header
887                                                 
888                                 if (save_msgs)
889                                         procmsg_save_to_outbox
890                                                 (queue->folder->outbox,
891                                                  file, TRUE);
892 */
893                                 folder_item_remove_msg(queue, msginfo->msgnum);
894                         }
895                         g_free(file);
896                 }
897                 procmsg_msginfo_free(msginfo);
898         }
899
900         folderview_update_item(queue, FALSE);
901
902         return ret;
903 }
904
905 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
906                             gboolean is_queued)
907 {
908         gint num;
909         FILE *fp;
910         MsgInfo *msginfo;
911
912         debug_print("saving sent message...\n");
913
914         if (!outbox)
915                 outbox = folder_get_default_outbox();
916         g_return_val_if_fail(outbox != NULL, -1);
917
918         /* remove queueing headers */
919         if (is_queued) {
920                 gchar tmp[MAXPATHLEN + 1];
921                 gchar buf[BUFFSIZE];
922                 FILE *outfp;
923
924                 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
925                            get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
926                 if ((fp = fopen(file, "rb")) == NULL) {
927                         FILE_OP_ERROR(file, "fopen");
928                         return -1;
929                 }
930                 if ((outfp = fopen(tmp, "wb")) == NULL) {
931                         FILE_OP_ERROR(tmp, "fopen");
932                         fclose(fp);
933                         return -1;
934                 }
935                 while (fgets(buf, sizeof(buf), fp) != NULL)
936                         if (buf[0] == '\r' || buf[0] == '\n') break;
937                 while (fgets(buf, sizeof(buf), fp) != NULL)
938                         fputs(buf, outfp);
939                 fclose(outfp);
940                 fclose(fp);
941
942                 folder_item_scan(outbox);
943                 if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
944                         g_warning("can't save message\n");
945                         unlink(tmp);
946                         return -1;
947                 }
948         } else {
949                 folder_item_scan(outbox);
950                 if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
951                         g_warning("can't save message\n");
952                         return -1;
953                 }
954                 return -1;
955         }
956         msginfo = folder_item_fetch_msginfo(outbox, num);
957         if(msginfo != NULL) {
958             procmsg_msginfo_unset_flags(msginfo, ~0, ~0);
959             procmsg_msginfo_free(msginfo);
960         }
961
962         if(is_queued) {
963                 unlink(file);
964         }
965
966         return 0;
967 }
968
969 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
970 {
971         static const gchar *def_cmd = "lpr %s";
972         static guint id = 0;
973         gchar *prtmp;
974         FILE *tmpfp, *prfp;
975         gchar buf[1024];
976         gchar *p;
977
978         g_return_if_fail(msginfo);
979
980         if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
981                 g_warning(_("Can't get text part\n"));
982                 return;
983         }
984
985         prtmp = g_strdup_printf("%s%cprinttmp.%08x",
986                                 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
987
988         if ((prfp = fopen(prtmp, "wb")) == NULL) {
989                 FILE_OP_ERROR(prtmp, "fopen");
990                 g_free(prtmp);
991                 fclose(tmpfp);
992                 return;
993         }
994
995         if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
996         if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
997         if (msginfo->to)   fprintf(prfp, "To: %s\n", msginfo->to);
998         if (msginfo->cc)   fprintf(prfp, "Cc: %s\n", msginfo->cc);
999         if (msginfo->newsgroups)
1000                 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
1001         if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
1002         fputc('\n', prfp);
1003
1004         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
1005                 fputs(buf, prfp);
1006
1007         fclose(prfp);
1008         fclose(tmpfp);
1009
1010         if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1011             !strchr(p + 2, '%'))
1012                 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
1013         else {
1014                 if (cmdline)
1015                         g_warning(_("Print command line is invalid: `%s'\n"),
1016                                   cmdline);
1017                 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
1018         }
1019
1020         g_free(prtmp);
1021
1022         g_strchomp(buf);
1023         if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
1024         system(buf);
1025 }
1026
1027 MsgInfo *procmsg_msginfo_new_ref(MsgInfo *msginfo)
1028 {
1029         msginfo->refcnt++;
1030         
1031         return msginfo;
1032 }
1033
1034 MsgInfo *procmsg_msginfo_new()
1035 {
1036         MsgInfo *newmsginfo;
1037
1038         newmsginfo = g_new0(MsgInfo, 1);
1039         newmsginfo->refcnt = 1;
1040         
1041         return newmsginfo;
1042 }
1043
1044 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
1045 {
1046         MsgInfo *newmsginfo;
1047
1048         if (msginfo == NULL) return NULL;
1049
1050         newmsginfo = g_new0(MsgInfo, 1);
1051
1052         newmsginfo->refcnt = 1;
1053
1054 #define MEMBCOPY(mmb)   newmsginfo->mmb = msginfo->mmb
1055 #define MEMBDUP(mmb)    newmsginfo->mmb = msginfo->mmb ? \
1056                         g_strdup(msginfo->mmb) : NULL
1057
1058         MEMBCOPY(msgnum);
1059         MEMBCOPY(size);
1060         MEMBCOPY(mtime);
1061         MEMBCOPY(date_t);
1062         MEMBCOPY(flags);
1063
1064         MEMBDUP(fromname);
1065
1066         MEMBDUP(date);
1067         MEMBDUP(from);
1068         MEMBDUP(to);
1069         MEMBDUP(cc);
1070         MEMBDUP(newsgroups);
1071         MEMBDUP(subject);
1072         MEMBDUP(msgid);
1073         MEMBDUP(inreplyto);
1074         MEMBDUP(xref);
1075
1076         MEMBCOPY(folder);
1077         MEMBCOPY(to_folder);
1078
1079         MEMBDUP(xface);
1080         MEMBDUP(dispositionnotificationto);
1081         MEMBDUP(returnreceiptto);
1082         MEMBDUP(references);
1083
1084         MEMBCOPY(score);
1085         MEMBCOPY(threadscore);
1086
1087         return newmsginfo;
1088 }
1089
1090 void procmsg_msginfo_free(MsgInfo *msginfo)
1091 {
1092         if (msginfo == NULL) return;
1093
1094         msginfo->refcnt--;
1095         if(msginfo->refcnt > 0)
1096                 return;
1097
1098         g_free(msginfo->fromspace);
1099         g_free(msginfo->references);
1100         g_free(msginfo->returnreceiptto);
1101         g_free(msginfo->dispositionnotificationto);
1102         g_free(msginfo->xface);
1103
1104         g_free(msginfo->fromname);
1105
1106         g_free(msginfo->date);
1107         g_free(msginfo->from);
1108         g_free(msginfo->to);
1109         g_free(msginfo->cc);
1110         g_free(msginfo->newsgroups);
1111         g_free(msginfo->subject);
1112         g_free(msginfo->msgid);
1113         g_free(msginfo->inreplyto);
1114         g_free(msginfo->xref);
1115
1116         g_free(msginfo);
1117 }
1118
1119 guint procmsg_msginfo_memusage(MsgInfo *msginfo)
1120 {
1121         guint memusage = 0;
1122         
1123         memusage += sizeof(MsgInfo);
1124         if(msginfo->fromname)
1125                 memusage += strlen(msginfo->fromname);
1126         if(msginfo->date)
1127                 memusage += strlen(msginfo->date);
1128         if(msginfo->from)
1129                 memusage += strlen(msginfo->from);
1130         if(msginfo->to)
1131                 memusage += strlen(msginfo->to);
1132         if(msginfo->cc)
1133                 memusage += strlen(msginfo->cc);
1134         if(msginfo->newsgroups)
1135                 memusage += strlen(msginfo->newsgroups);
1136         if(msginfo->subject)
1137                 memusage += strlen(msginfo->subject);
1138         if(msginfo->msgid)
1139                 memusage += strlen(msginfo->msgid);
1140         if(msginfo->inreplyto)
1141                 memusage += strlen(msginfo->inreplyto);
1142         if(msginfo->xface)
1143                 memusage += strlen(msginfo->xface);
1144         if(msginfo->dispositionnotificationto)
1145                 memusage += strlen(msginfo->dispositionnotificationto);
1146         if(msginfo->returnreceiptto)
1147                 memusage += strlen(msginfo->returnreceiptto);
1148         if(msginfo->references)
1149                 memusage += strlen(msginfo->references);
1150         if(msginfo->fromspace)
1151                 memusage += strlen(msginfo->fromspace);
1152
1153         return memusage;
1154 }
1155
1156 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
1157 {
1158         const MsgInfo *msginfo1 = a;
1159         const MsgInfo *msginfo2 = b;
1160
1161         if (!msginfo1)
1162                 return -1;
1163         if (!msginfo2)
1164                 return -1;
1165
1166         return msginfo1->msgnum - msginfo2->msgnum;
1167 }
1168
1169 enum
1170 {
1171         Q_SENDER           = 0,
1172         Q_SMTPSERVER       = 1,
1173         Q_RECIPIENTS       = 2,
1174         Q_NEWSGROUPS       = 3,
1175         Q_MAIL_ACCOUNT_ID  = 4,
1176         Q_NEWS_ACCOUNT_ID  = 5,
1177         Q_SAVE_COPY_FOLDER = 6,
1178         Q_REPLY_MESSAGE_ID = 7,
1179 };
1180
1181 gint procmsg_send_message_queue(const gchar *file)
1182 {
1183         static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
1184                                        {"SSV:",  NULL, FALSE},
1185                                        {"R:",    NULL, FALSE},
1186                                        {"NG:",   NULL, FALSE},
1187                                        {"MAID:", NULL, FALSE},
1188                                        {"NAID:", NULL, FALSE},
1189                                        {"SCF:",  NULL, FALSE},
1190                                        {"RMID:", NULL, FALSE},
1191                                        {NULL,    NULL, FALSE}};
1192         FILE *fp;
1193         gint filepos;
1194         gint mailval = 0, newsval = 0;
1195         gchar *from = NULL;
1196         gchar *smtpserver = NULL;
1197         GSList *to_list = NULL;
1198         GSList *newsgroup_list = NULL;
1199         gchar *savecopyfolder = NULL;
1200         gchar *replymessageid = NULL;
1201         gchar buf[BUFFSIZE];
1202         gint hnum;
1203         PrefsAccount *mailac = NULL, *newsac = NULL;
1204         int local = 0;
1205
1206         g_return_val_if_fail(file != NULL, -1);
1207
1208         if ((fp = fopen(file, "rb")) == NULL) {
1209                 FILE_OP_ERROR(file, "fopen");
1210                 return -1;
1211         }
1212
1213         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
1214                != -1) {
1215                 gchar *p = buf + strlen(qentry[hnum].name);
1216
1217                 switch (hnum) {
1218                 case Q_SENDER:
1219                         if (!from) from = g_strdup(p);
1220                         break;
1221                 case Q_SMTPSERVER:
1222                         if (!smtpserver) smtpserver = g_strdup(p);
1223                         break;
1224                 case Q_RECIPIENTS:
1225                         to_list = address_list_append(to_list, p);
1226                         break;
1227                 case Q_NEWSGROUPS:
1228                         newsgroup_list = newsgroup_list_append(newsgroup_list, p);
1229                         break;
1230                 case Q_MAIL_ACCOUNT_ID:
1231                         mailac = account_find_from_id(atoi(p));
1232                         break;
1233                 case Q_NEWS_ACCOUNT_ID:
1234                         newsac = account_find_from_id(atoi(p));
1235                         break;
1236                 case Q_SAVE_COPY_FOLDER:
1237                         if (!savecopyfolder) savecopyfolder = g_strdup(p);
1238                         break;
1239                 case Q_REPLY_MESSAGE_ID:
1240                         if (!replymessageid) replymessageid = g_strdup(p);
1241                         break;
1242                 }
1243         }
1244         filepos = ftell(fp);
1245
1246         fseek(fp, filepos, SEEK_SET);
1247         if (to_list) {
1248                 debug_print(_("Sending message by mail\n"));
1249                 if(!from) {
1250                         g_warning(_("Queued message header is broken.\n"));
1251                         mailval = -1;
1252                 } else if (mailac && mailac->use_mail_command &&
1253                            mailac->mail_command && (* mailac->mail_command)) {
1254                         mailval = send_message_local(mailac->mail_command, fp);
1255                         local = 1;
1256                 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
1257                         mailval = send_message_local(prefs_common.extsend_cmd, fp);
1258                         local = 1;
1259                 } else {
1260                         if (!mailac) {
1261                                 mailac = account_find_from_smtp_server(from, smtpserver);
1262                                 if (!mailac) {
1263                                         g_warning(_("Account not found. "
1264                                                     "Using current account...\n"));
1265                                         mailac = cur_account;
1266                                 }
1267                         }
1268
1269                         if (mailac)
1270                                 mailval = send_message_smtp(mailac, to_list, fp);
1271                         else {
1272                                 PrefsAccount tmp_ac;
1273
1274                                 g_warning(_("Account not found.\n"));
1275
1276                                 memset(&tmp_ac, 0, sizeof(PrefsAccount));
1277                                 tmp_ac.address = from;
1278                                 tmp_ac.smtp_server = smtpserver;
1279                                 tmp_ac.smtpport = SMTP_PORT;
1280                                 mailval = send_message_smtp(&tmp_ac, to_list, fp);
1281                         }
1282                 }
1283                 if (mailval < 0) {
1284                         if (!local)
1285                                 alertpanel_error(
1286                                         _("Error occurred while sending the message to `%s'."),
1287                                         mailac ? mailac->smtp_server : smtpserver);
1288                         else
1289                                 alertpanel_error(
1290                                         _("Error occurred while sending the message with command `%s'."),
1291                                         (mailac && mailac->use_mail_command && 
1292                                          mailac->mail_command && (*mailac->mail_command)) ? 
1293                                                 mailac->mail_command : prefs_common.extsend_cmd);
1294                 }
1295         }
1296
1297         if(newsgroup_list && (newsval == 0)) {
1298                 Folder *folder;
1299                 gchar *tmp = NULL;
1300                 FILE *tmpfp;
1301
1302                 /* write to temporary file */
1303                 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1304                             G_DIR_SEPARATOR, (gint)file);
1305                 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
1306                         FILE_OP_ERROR(tmp, "fopen");
1307                         newsval = -1;
1308                         alertpanel_error(_("Could not create temporary file for news sending."));
1309                 } else {
1310                         if (change_file_mode_rw(tmpfp, tmp) < 0) {
1311                                 FILE_OP_ERROR(tmp, "chmod");
1312                                 g_warning(_("can't change file mode\n"));
1313                         }
1314
1315                         while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
1316                                 if (fputs(buf, tmpfp) == EOF) {
1317                                         FILE_OP_ERROR(tmp, "fputs");
1318                                         newsval = -1;
1319                                         alertpanel_error(_("Error when writing temporary file for news sending."));
1320                                 }
1321                         }
1322                         fclose(tmpfp);
1323
1324                         if(newsval == 0) {
1325                                 debug_print(_("Sending message by news\n"));
1326
1327                                 folder = FOLDER(newsac->folder);
1328
1329                                 newsval = news_post(folder, tmp);
1330                                 if (newsval < 0) {
1331                                         alertpanel_error(_("Error occurred while posting the message to %s ."),
1332                                                  newsac->nntp_server);
1333                                 }
1334                         }
1335                         unlink(tmp);
1336                 }
1337                 g_free(tmp);
1338         }
1339
1340         slist_free_strings(to_list);
1341         g_slist_free(to_list);
1342         slist_free_strings(newsgroup_list);
1343         g_slist_free(newsgroup_list);
1344         g_free(from);
1345         g_free(smtpserver);
1346         fclose(fp);
1347
1348         /* save message to outbox */
1349         if (mailval == 0 && newsval == 0 && savecopyfolder) {
1350                 FolderItem *outbox;
1351
1352                 debug_print(_("saving sent message...\n"));
1353
1354                 outbox = folder_find_item_from_identifier(savecopyfolder);
1355                 if(!outbox)
1356                         outbox = folder_get_default_outbox();
1357
1358                 procmsg_save_to_outbox(outbox, file, TRUE);
1359         }
1360
1361         if(replymessageid != NULL) {
1362                 gchar **tokens;
1363                 FolderItem *item;
1364                 
1365                 tokens = g_strsplit(replymessageid, "\x7f", 0);
1366                 item = folder_find_item_from_identifier(tokens[0]);
1367                 if(item != NULL) {
1368                         MsgInfo *msginfo;
1369                         
1370                         msginfo = folder_item_fetch_msginfo(item, atoi(tokens[1]));
1371                         if((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
1372                                 procmsg_msginfo_free(msginfo);
1373                                 msginfo = NULL;
1374                         }
1375                         
1376                         if(msginfo == NULL) {
1377                                 msginfo = folder_item_fetch_msginfo_by_id(item, tokens[2]);
1378                         }
1379                         
1380                         if(msginfo != NULL) {
1381                                 procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
1382                                 procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
1383
1384                                 procmsg_msginfo_free(msginfo);
1385                         }
1386                 }
1387                 g_strfreev(tokens);
1388         }
1389
1390         g_free(savecopyfolder);
1391         g_free(replymessageid);
1392         
1393         return (newsval != 0 ? newsval : mailval);
1394 }
1395
1396 #define CHANGE_FLAGS(msginfo) \
1397 { \
1398 if (msginfo->folder->folder->change_flags != NULL) \
1399 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
1400                                       msginfo->folder, \
1401                                       msginfo); \
1402 }
1403
1404 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1405 {
1406         gboolean changed = FALSE;
1407         FolderItem *item = msginfo->folder;
1408
1409         debug_print(_("Setting flags for message %d in folder %s\n"), msginfo->msgnum, item->path);
1410
1411         /* if new flag is set */
1412         if((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
1413            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1414                 item->new++;
1415                 changed = TRUE;
1416         }
1417
1418         /* if unread flag is set */
1419         if((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
1420            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1421                 item->unread++;
1422                 changed = TRUE;
1423         }
1424
1425         /* if ignore thread flag is set */
1426         if((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1427                 if(MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
1428                         item->new--;
1429                         changed = TRUE;
1430                 }
1431                 if(MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
1432                         item->unread--;
1433                         changed = TRUE;
1434                 }
1435         }
1436
1437         if (MSG_IS_IMAP(msginfo->flags))
1438                 imap_msg_set_perm_flags(msginfo, perm_flags);
1439
1440         msginfo->flags.perm_flags |= perm_flags;
1441         msginfo->flags.tmp_flags |= tmp_flags;
1442
1443         if(changed) {
1444                 folderview_update_item(item, FALSE);
1445         }
1446         CHANGE_FLAGS(msginfo);
1447         procmsg_msginfo_write_flags(msginfo);
1448 }
1449
1450 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
1451 {
1452         gboolean changed = FALSE;
1453         FolderItem *item = msginfo->folder;
1454         
1455         debug_print(_("Unsetting flags for message %d in folder %s\n"), msginfo->msgnum, item->path);
1456
1457         /* if new flag is unset */
1458         if((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
1459            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1460                 item->new--;
1461                 changed = TRUE;
1462         }
1463
1464         /* if unread flag is unset */
1465         if((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
1466            !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1467                 item->unread--;
1468                 changed = TRUE;
1469         }
1470
1471         /* if ignore thread flag is unset */
1472         if((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
1473                 if(MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
1474                         item->new++;
1475                         changed = TRUE;
1476                 }
1477                 if(MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
1478                         item->unread++;
1479                         changed = TRUE;
1480                 }
1481         }
1482
1483         if (MSG_IS_IMAP(msginfo->flags))
1484                 imap_msg_unset_perm_flags(msginfo, perm_flags);
1485
1486         msginfo->flags.perm_flags &= ~perm_flags;
1487         msginfo->flags.tmp_flags &= ~tmp_flags;
1488
1489         if(changed) {
1490                 folderview_update_item(item, FALSE);
1491         }
1492         CHANGE_FLAGS(msginfo);
1493         procmsg_msginfo_write_flags(msginfo);
1494 }
1495
1496 void procmsg_msginfo_write_flags(MsgInfo *msginfo)
1497 {
1498         gchar *destdir;
1499         FILE *fp;
1500
1501         destdir = folder_item_get_path(msginfo->folder);
1502         if (!is_dir_exist(destdir))
1503                 make_dir_hier(destdir);
1504
1505         if ((fp = procmsg_open_mark_file(destdir, TRUE))) {
1506                 procmsg_write_flags(msginfo, fp);
1507                 fclose(fp);
1508         } else {
1509                 g_warning(_("Can't open mark file.\n"));
1510         }
1511         
1512         g_free(destdir);
1513 }