filtering dialog box and some changes
[claws.git] / src / procmsg.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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
36 typedef struct _FlagInfo        FlagInfo;
37
38 struct _FlagInfo
39 {
40         guint    msgnum;
41         MsgFlags flags;
42 };
43
44 static void mark_sum_func                       (gpointer        key,
45                                                  gpointer        value,
46                                                  gpointer        data);
47
48 static GHashTable *procmsg_read_mark_file       (const gchar    *folder);
49 static gint procmsg_cmp_msgnum                  (gconstpointer   a,
50                                                  gconstpointer   b);
51 static gint procmsg_cmp_flag_msgnum             (gconstpointer   a,
52                                                  gconstpointer   b);
53
54
55 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
56 {
57         GHashTable *msg_table;
58
59         if (mlist == NULL) return NULL;
60
61         msg_table = g_hash_table_new(NULL, g_direct_equal);
62         procmsg_msg_hash_table_append(msg_table, mlist);
63
64         return msg_table;
65 }
66
67 void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
68 {
69         GSList *cur;
70         MsgInfo *msginfo;
71
72         if (msg_table == NULL || mlist == NULL) return;
73
74         for (cur = mlist; cur != NULL; cur = cur->next) {
75                 msginfo = (MsgInfo *)cur->data;
76
77                 g_hash_table_insert(msg_table,
78                                     GUINT_TO_POINTER(msginfo->msgnum),
79                                     msginfo);
80         }
81 }
82
83 GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
84 {
85         GHashTable *msg_table;
86         GSList *cur;
87         MsgInfo *msginfo;
88
89         if (mlist == NULL) return NULL;
90
91         msg_table = g_hash_table_new(NULL, g_direct_equal);
92
93         for (cur = mlist; cur != NULL; cur = cur->next) {
94                 msginfo = (MsgInfo *)cur->data;
95                 g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
96         }
97
98         return msg_table;
99 }
100
101 static gint procmsg_read_cache_data_str(FILE *fp, gchar **str)
102 {
103         gchar buf[BUFFSIZE];
104         gint ret = 0;
105         size_t len;
106
107         if (fread(&len, sizeof(len), 1, fp) == 1) {
108                 if (len < 0)
109                         ret = -1;
110                 else {
111                         gchar *tmp = NULL;
112
113                         while (len > 0) {
114                                 size_t size = MIN(len, BUFFSIZE - 1);
115
116                                 if (fread(buf, size, 1, fp) != 1) {
117                                         ret = -1;
118                                         if (tmp) g_free(tmp);
119                                         *str = NULL;
120                                         break;
121                                 }
122
123                                 buf[size] = '\0';
124                                 if (tmp) {
125                                         *str = g_strconcat(tmp, buf, NULL);
126                                         g_free(tmp);
127                                         tmp = *str;
128                                 } else
129                                         tmp = *str = g_strdup(buf);
130
131                                 len -= size;
132                         }
133                 }
134         } else
135                 ret = -1;
136
137         if (ret < 0)
138                 g_warning(_("Cache data is corrupted\n"));
139
140         return ret;
141 }
142
143 #define READ_CACHE_DATA(data, fp) \
144 { \
145         if (procmsg_read_cache_data_str(fp, &data) < 0) { \
146                 procmsg_msginfo_free(msginfo); \
147                 break; \
148         } \
149 }
150
151 #define READ_CACHE_DATA_INT(n, fp) \
152 { \
153         if (fread(&n, sizeof(n), 1, fp) != 1) { \
154                 g_warning(_("Cache data is corrupted\n")); \
155                 procmsg_msginfo_free(msginfo); \
156                 break; \
157         } \
158 }
159
160 GSList *procmsg_read_cache(FolderItem *item, gboolean scan_file)
161 {
162         GSList *mlist = NULL;
163         GSList *last = NULL;
164         gchar *cache_file;
165         FILE *fp;
166         MsgInfo *msginfo;
167         MsgFlags default_flags;
168         gchar file_buf[BUFFSIZE];
169         gint ver;
170         guint num;
171         FolderType type;
172
173         g_return_val_if_fail(item != NULL, NULL);
174         g_return_val_if_fail(item->folder != NULL, NULL);
175         type = item->folder->type;
176
177         default_flags = MSG_NEW|MSG_UNREAD|MSG_CACHED;
178         if (type == F_MH) {
179                 if (item->stype == F_QUEUE) {
180                         MSG_SET_FLAGS(default_flags, MSG_QUEUED);
181                 } else if (item->stype == F_DRAFT) {
182                         MSG_SET_FLAGS(default_flags, MSG_DRAFT);
183                 }
184         } else if (type == F_IMAP) {
185                 MSG_SET_FLAGS(default_flags, MSG_IMAP);
186         } else if (type == F_NEWS) {
187                 MSG_SET_FLAGS(default_flags, MSG_NEWS);
188         }
189
190         if (type == F_MH) {
191                 gchar *path;
192
193                 path = folder_item_get_path(item);
194                 if (change_dir(path) < 0) {
195                         g_free(path);
196                         return NULL;
197                 }
198                 g_free(path);
199         }
200         cache_file = folder_item_get_cache_file(item);
201         if ((fp = fopen(cache_file, "r")) == NULL) {
202                 debug_print(_("\tNo cache file\n"));
203                 g_free(cache_file);
204                 return NULL;
205         }
206         setvbuf(fp, file_buf, _IOFBF, sizeof(file_buf));
207         g_free(cache_file);
208
209         debug_print(_("\tReading summary cache..."));
210
211         /* compare cache version */
212         if (fread(&ver, sizeof(ver), 1, fp) != 1 ||
213             CACHE_VERSION != ver) {
214                 debug_print(_("Cache version is different. Discarding it.\n"));
215                 fclose(fp);
216                 return NULL;
217         }
218
219         while (fread(&num, sizeof(num), 1, fp) == 1) {
220                 msginfo = g_new0(MsgInfo, 1);
221                 msginfo->msgnum = num;
222                 READ_CACHE_DATA_INT(msginfo->size, fp);
223                 READ_CACHE_DATA_INT(msginfo->mtime, fp);
224                 READ_CACHE_DATA_INT(msginfo->date_t, fp);
225                 READ_CACHE_DATA_INT(msginfo->flags, fp);
226
227                 READ_CACHE_DATA(msginfo->fromname, fp);
228
229                 READ_CACHE_DATA(msginfo->date, fp);
230                 READ_CACHE_DATA(msginfo->from, fp);
231                 READ_CACHE_DATA(msginfo->to, fp);
232                 READ_CACHE_DATA(msginfo->cc, fp);
233                 READ_CACHE_DATA(msginfo->newsgroups, fp);
234                 READ_CACHE_DATA(msginfo->subject, fp);
235                 READ_CACHE_DATA(msginfo->msgid, fp);
236                 READ_CACHE_DATA(msginfo->inreplyto, fp);
237                 READ_CACHE_DATA(msginfo->references, fp);
238
239                 MSG_SET_FLAGS(msginfo->flags, default_flags);
240
241                 /* if the message file doesn't exist or is changed,
242                    don't add the data */
243                 if (type == F_MH && scan_file &&
244                     folder_item_is_msg_changed(item, msginfo))
245                         procmsg_msginfo_free(msginfo);
246                 else {
247                         msginfo->folder = item;
248
249                         if (!mlist)
250                                 last = mlist = g_slist_append(NULL, msginfo);
251                         else {
252                                 last = g_slist_append(last, msginfo);
253                                 last = last->next;
254                         }
255                 }
256         }
257
258         fclose(fp);
259         debug_print(_("done.\n"));
260
261         return mlist;
262 }
263
264 #undef READ_CACHE_DATA
265 #undef READ_CACHE_DATA_INT
266
267 void procmsg_set_flags(GSList *mlist, FolderItem *item)
268 {
269         GSList *cur, *tmp;
270         gint newmsg = 0;
271         gint lastnum = 0;
272         gchar *markdir;
273         MsgInfo *msginfo;
274         GHashTable *mark_table;
275         MsgFlags flags;
276
277         if (!mlist) return;
278         g_return_if_fail(item != NULL);
279         g_return_if_fail(item->folder != NULL);
280
281         debug_print(_("\tMarking the messages..."));
282
283         markdir = folder_item_get_path(item);
284         mark_table = procmsg_read_mark_file(markdir);
285         g_free(markdir);
286
287         if (!mark_table) return;
288
289         for (cur = mlist; cur != NULL; cur = cur->next) {
290                 msginfo = (MsgInfo *)cur->data;
291
292                 if (lastnum < msginfo->msgnum)
293                         lastnum = msginfo->msgnum;
294
295                 flags = GPOINTER_TO_UINT(g_hash_table_lookup(mark_table,
296                                          GUINT_TO_POINTER(msginfo->msgnum)));
297
298                 if (flags != 0) {
299                         /* add the permanent flags only */
300                         MSG_UNSET_FLAGS(msginfo->flags,
301                                         MSG_PERMANENT_FLAG_MASK);
302                         MSG_SET_FLAGS(msginfo->flags,
303                                       flags & MSG_PERMANENT_FLAG_MASK);
304                         if (item->folder->type == F_IMAP) {
305                                 MSG_SET_FLAGS(msginfo->flags, MSG_IMAP);
306                         } else if (item->folder->type == F_NEWS) {
307                                 MSG_SET_FLAGS(msginfo->flags, MSG_NEWS);
308                         }
309                 } else {
310                         /* not found (new message) */
311                         if (newmsg == 0) {
312                                 for (tmp = mlist; tmp != cur; tmp = tmp->next)
313                                         MSG_UNSET_FLAGS
314                                                 (((MsgInfo *)tmp->data)->flags,
315                                                  MSG_NEW);
316                         }
317                         newmsg++;
318                 }
319         }
320
321         item->last_num = lastnum;
322
323         debug_print(_("done.\n"));
324         if (newmsg)
325                 debug_print(_("\t%d new message(s)\n"), newmsg);
326
327         g_hash_table_destroy(mark_table);
328 }
329
330 gint procmsg_get_last_num_in_cache(GSList *mlist)
331 {
332         GSList *cur;
333         MsgInfo *msginfo;
334         gint last = 0;
335
336         if (mlist == NULL) return 0;
337
338         for (cur = mlist; cur != NULL; cur = cur->next) {
339                 msginfo = (MsgInfo *)cur->data;
340                 if (msginfo && msginfo->msgnum > last)
341                         last = msginfo->msgnum;
342         }
343
344         return last;
345 }
346
347 void procmsg_write_cache(MsgInfo *msginfo, FILE *fp)
348 {
349         MsgFlags flags = msginfo->flags & MSG_CACHED_FLAG_MASK;
350
351         WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
352         WRITE_CACHE_DATA_INT(msginfo->size, fp);
353         WRITE_CACHE_DATA_INT(msginfo->mtime, fp);
354         WRITE_CACHE_DATA_INT(msginfo->date_t, fp);
355         WRITE_CACHE_DATA_INT(flags, fp);
356
357         WRITE_CACHE_DATA(msginfo->fromname, fp);
358
359         WRITE_CACHE_DATA(msginfo->date, fp);
360         WRITE_CACHE_DATA(msginfo->from, fp);
361         WRITE_CACHE_DATA(msginfo->to, fp);
362         WRITE_CACHE_DATA(msginfo->cc, fp);
363         WRITE_CACHE_DATA(msginfo->newsgroups, fp);
364         WRITE_CACHE_DATA(msginfo->subject, fp);
365         WRITE_CACHE_DATA(msginfo->msgid, fp);
366         WRITE_CACHE_DATA(msginfo->inreplyto, fp);
367         WRITE_CACHE_DATA(msginfo->references, fp);
368 }
369
370 void procmsg_write_flags(MsgInfo *msginfo, FILE *fp)
371 {
372         MsgFlags flags;
373
374         flags = msginfo->flags & MSG_PERMANENT_FLAG_MASK;
375
376         WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
377         WRITE_CACHE_DATA_INT(flags, fp);
378 }
379
380 struct MarkSum {
381         gint *new;
382         gint *unread;
383         gint *total;
384 };
385
386 static void mark_sum_func(gpointer key, gpointer value, gpointer data)
387 {
388         MsgFlags flags = GPOINTER_TO_UINT(value);
389         struct MarkSum *marksum = data;
390
391         if (MSG_IS_NEW(flags)) (*marksum->new)++;
392         if (MSG_IS_UNREAD(flags)) (*marksum->unread)++;
393         (*marksum->total)++;
394 }
395
396 void procmsg_get_mark_sum(const gchar *folder,
397                           gint *new, gint *unread, gint *total)
398 {
399         GHashTable *mark_table;
400         struct MarkSum marksum;
401
402         *new = *unread = *total = 0;
403         marksum.new    = new;
404         marksum.unread = unread;
405         marksum.total  = total;
406
407         mark_table = procmsg_read_mark_file(folder);
408
409         if (mark_table) {
410                 g_hash_table_foreach(mark_table, mark_sum_func, &marksum);
411                 g_hash_table_destroy(mark_table);
412         }
413 }
414
415 static GHashTable *procmsg_read_mark_file(const gchar *folder)
416 {
417         FILE *fp;
418         GHashTable *mark_table = NULL;
419         gint num;
420         MsgFlags flags;
421
422         if ((fp = procmsg_open_mark_file(folder, FALSE)) == NULL)
423                 return NULL;
424
425         mark_table = g_hash_table_new(NULL, g_direct_equal);
426
427         while (fread(&num, sizeof(num), 1, fp) == 1) {
428                 if (fread(&flags, sizeof(flags), 1, fp) != 1) break;
429                 MSG_SET_FLAGS(flags, MSG_CACHED);
430
431                 g_hash_table_insert(mark_table,
432                                     GUINT_TO_POINTER(num),
433                                     GUINT_TO_POINTER(flags));
434         }
435
436         fclose(fp);
437         return mark_table;
438 }
439
440 FILE *procmsg_open_mark_file(const gchar *folder, gboolean append)
441 {
442         gchar *markfile;
443         FILE *fp;
444         gint ver;
445
446         markfile = g_strconcat(folder, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
447
448         if ((fp = fopen(markfile, "r")) == NULL)
449                 debug_print(_("Mark file not found.\n"));
450         else if (fread(&ver, sizeof(ver), 1, fp) != 1 || MARK_VERSION != ver) {
451                 debug_print(_("Mark version is different (%d != %d). "
452                               "Discarding it.\n"), ver, MARK_VERSION);
453                 fclose(fp);
454                 fp = NULL;
455         }
456
457         /* read mode */
458         if (append == FALSE) {
459                 g_free(markfile);
460                 return fp;
461         }
462
463         if (fp) {
464                 /* reopen with append mode */
465                 fclose(fp);
466                 if ((fp = fopen(markfile, "a")) == NULL)
467                         g_warning(_("Can't open mark file with append mode.\n"));
468         } else {
469                 /* open with overwrite mode if mark file doesn't exist or
470                    version is different */
471                 if ((fp = fopen(markfile, "w")) == NULL)
472                         g_warning(_("Can't open mark file with write mode.\n"));
473                 else {
474                         ver = MARK_VERSION;
475                         WRITE_CACHE_DATA_INT(ver, fp);
476                 }
477         }
478
479         g_free(markfile);
480         return fp;
481 }
482
483 void procmsg_move_messages(GSList *mlist)
484 {
485         GSList *cur, *movelist = NULL;
486         MsgInfo *msginfo;
487         FolderItem *dest = NULL;
488         GHashTable *hash;
489
490         if (!mlist) return;
491
492         hash = procmsg_to_folder_hash_table_create(mlist);
493         folder_item_scan_foreach(hash);
494         g_hash_table_destroy(hash);
495
496         for (cur = mlist; cur != NULL; cur = cur->next) {
497                 msginfo = (MsgInfo *)cur->data;
498                 if (!dest) {
499                         dest = msginfo->to_folder;
500                         movelist = g_slist_append(movelist, msginfo);
501                 } else if (dest == msginfo->to_folder) {
502                         movelist = g_slist_append(movelist, msginfo);
503                 } else {
504                         folder_item_move_msgs_with_dest(dest, movelist);
505                         g_slist_free(movelist);
506                         movelist = NULL;
507                         dest = msginfo->to_folder;
508                         movelist = g_slist_append(movelist, msginfo);
509                 }
510         }
511
512         if (movelist) {
513                 folder_item_move_msgs_with_dest(dest, movelist);
514                 g_slist_free(movelist);
515         }
516 }
517
518 void procmsg_copy_messages(GSList *mlist)
519 {
520         GSList *cur, *copylist = NULL;
521         MsgInfo *msginfo;
522         FolderItem *dest = NULL;
523         GHashTable *hash;
524
525         if (!mlist) return;
526
527         hash = procmsg_to_folder_hash_table_create(mlist);
528         folder_item_scan_foreach(hash);
529         g_hash_table_destroy(hash);
530
531         for (cur = mlist; cur != NULL; cur = cur->next) {
532                 msginfo = (MsgInfo *)cur->data;
533                 if (!dest) {
534                         dest = msginfo->to_folder;
535                         copylist = g_slist_append(copylist, msginfo);
536                 } else if (dest == msginfo->to_folder) {
537                         copylist = g_slist_append(copylist, msginfo);
538                 } else {
539                         folder_item_copy_msgs_with_dest(dest, copylist);
540                         g_slist_free(copylist);
541                         copylist = NULL;
542                         dest = msginfo->to_folder;
543                         copylist = g_slist_append(copylist, msginfo);
544                 }
545         }
546
547         if (copylist) {
548                 folder_item_copy_msgs_with_dest(dest, copylist);
549                 g_slist_free(copylist);
550         }
551 }
552
553 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
554 {
555         gchar *path, *file;
556
557         g_return_val_if_fail(msginfo != NULL, NULL);
558
559         if (msginfo->plaintext_file)
560                 file = g_strdup(msginfo->plaintext_file);
561         else {
562                 path = folder_item_get_path(msginfo->folder);
563                 file = g_strconcat(path, G_DIR_SEPARATOR_S,
564                                    itos(msginfo->msgnum), NULL);
565                 g_free(path);
566         }
567
568         return file;
569 }
570
571 gchar *procmsg_get_message_file(MsgInfo *msginfo)
572 {
573         gchar *filename = NULL;
574
575         g_return_val_if_fail(msginfo != NULL, NULL);
576
577         filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
578         if (!filename)
579                 g_warning(_("can't fetch message %d\n"), msginfo->msgnum);
580
581         return filename;
582 }
583
584 FILE *procmsg_open_message(MsgInfo *msginfo)
585 {
586         FILE *fp;
587         gchar *file;
588
589         g_return_val_if_fail(msginfo != NULL, NULL);
590
591         file = procmsg_get_message_file_path(msginfo);
592         g_return_val_if_fail(file != NULL, NULL);
593
594         if ((fp = fopen(file, "r")) == NULL) {
595                 FILE_OP_ERROR(file, "fopen");
596                 g_free(file);
597                 return NULL;
598         }
599
600         g_free(file);
601
602         if (MSG_IS_QUEUED(msginfo->flags)) {
603                 gchar buf[BUFFSIZE];
604
605                 while (fgets(buf, sizeof(buf), fp) != NULL)
606                         if (buf[0] == '\r' || buf[0] == '\n') break;
607         }
608
609         return fp;
610 }
611
612 gboolean procmsg_msg_exist(MsgInfo *msginfo)
613 {
614         gchar *path;
615         gboolean ret;
616
617         if (!msginfo) return FALSE;
618
619         path = procmsg_get_message_file_path(msginfo);
620         change_dir(path);
621         ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
622         g_free(path);
623
624         return ret;
625 }
626
627 void procmsg_empty_trash(void)
628 {
629         FolderItem *trash;
630         GList *cur;
631
632         for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
633                 trash = FOLDER(cur->data)->trash;
634                 if (trash) folder_item_remove_all_msg(trash);
635         }
636 }
637
638 gint procmsg_send_queue(void)
639 {
640         FolderItem *queue;
641         gint i;
642
643         queue = folder_get_default_queue();
644         g_return_val_if_fail(queue != NULL, -1);
645         folder_item_scan(queue);
646         if (queue->last_num < 0) return -1;
647         else if (queue->last_num == 0) return 0;
648
649         for (i = 1; i <= queue->last_num; i++) {
650                 gchar *file;
651
652                 file = folder_item_fetch_msg(queue, i);
653                 if (file) {
654                         if (send_message_queue(file) < 0) {
655                                 g_warning(_("Sending queued message failed.\n"));
656                                 g_free(file);
657                                 return -1;
658                         }
659                         folder_item_remove_msg(queue, i);
660                         g_free(file);
661                 }
662         }
663
664         return 0;
665 }
666
667 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
668 {
669         static const gchar *def_cmd = "lpr %s";
670         static guint id = 0;
671         gchar *prtmp;
672         FILE *tmpfp, *prfp;
673         gchar buf[1024];
674         gchar *p;
675
676         g_return_if_fail(msginfo);
677
678         if ((tmpfp = procmime_get_text_part(msginfo)) == NULL) {
679                 g_warning(_("Can't get text part\n"));
680                 return;
681         }
682
683         prtmp = g_strdup_printf("%s%cprinttmp.%08x",
684                                 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
685
686         if ((prfp = fopen(prtmp, "w")) == NULL) {
687                 FILE_OP_ERROR(prtmp, "fopen");
688                 g_free(prtmp);
689                 fclose(tmpfp);
690                 return;
691         }
692
693         if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
694         if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
695         if (msginfo->to)   fprintf(prfp, "To: %s\n", msginfo->to);
696         if (msginfo->cc)   fprintf(prfp, "Cc: %s\n", msginfo->cc);
697         if (msginfo->newsgroups)
698                 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
699         if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
700         fputc('\n', prfp);
701
702         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
703                 fputs(buf, prfp);
704
705         fclose(prfp);
706         fclose(tmpfp);
707
708         if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
709             !strchr(p + 2, '%'))
710                 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
711         else {
712                 if (cmdline)
713                         g_warning(_("Print command line is invalid: `%s'\n"),
714                                   cmdline);
715                 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
716         }
717
718         g_free(prtmp);
719
720         g_strchomp(buf);
721         if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
722         system(buf);
723 }
724
725 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
726 {
727         MsgInfo *newmsginfo;
728
729         if (msginfo == NULL) return NULL;
730
731         newmsginfo = g_new0(MsgInfo, 1);
732
733 #define MEMBCOPY(mmb)   newmsginfo->mmb = msginfo->mmb
734 #define MEMBDUP(mmb)    newmsginfo->mmb = msginfo->mmb ? \
735                         g_strdup(msginfo->mmb) : NULL
736
737         MEMBCOPY(msgnum);
738         MEMBCOPY(size);
739         MEMBCOPY(mtime);
740         MEMBCOPY(date_t);
741         MEMBCOPY(flags);
742
743         MEMBDUP(fromname);
744
745         MEMBDUP(date);
746         MEMBDUP(from);
747         MEMBDUP(to);
748         MEMBDUP(cc);
749         MEMBDUP(newsgroups);
750         MEMBDUP(subject);
751         MEMBDUP(msgid);
752         MEMBDUP(inreplyto);
753
754         MEMBCOPY(folder);
755         MEMBCOPY(to_folder);
756
757         MEMBDUP(xface);
758         MEMBDUP(dispositionnotificationto);
759         MEMBDUP(returnreceiptto);
760         MEMBDUP(references);
761
762         MEMBCOPY(score);
763         MEMBCOPY(threadscore);
764
765         return newmsginfo;
766 }
767
768 void procmsg_msginfo_free(MsgInfo *msginfo)
769 {
770         if (msginfo == NULL) return;
771
772         g_free(msginfo->references);
773         g_free(msginfo->returnreceiptto);
774         g_free(msginfo->dispositionnotificationto);
775         g_free(msginfo->xface);
776
777         g_free(msginfo->fromname);
778
779         g_free(msginfo->date);
780         g_free(msginfo->from);
781         g_free(msginfo->to);
782         g_free(msginfo->cc);
783         g_free(msginfo->newsgroups);
784         g_free(msginfo->subject);
785         g_free(msginfo->msgid);
786         g_free(msginfo->inreplyto);
787
788         g_free(msginfo);
789 }
790
791 static gint procmsg_cmp_msgnum(gconstpointer a, gconstpointer b)
792 {
793         const MsgInfo *msginfo = a;
794         const guint msgnum = GPOINTER_TO_UINT(b);
795
796         if (!msginfo)
797                 return -1;
798
799         return msginfo->msgnum - msgnum;
800 }
801
802 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
803 {
804         const MsgInfo *msginfo1 = a;
805         const MsgInfo *msginfo2 = b;
806
807         if (!msginfo1)
808                 return -1;
809         if (!msginfo2)
810                 return -1;
811
812         return msginfo1->msgnum - msginfo2->msgnum;
813 }
814
815 static gint procmsg_cmp_flag_msgnum(gconstpointer a, gconstpointer b)
816 {
817         const FlagInfo *finfo = a;
818         const guint msgnum = GPOINTER_TO_UINT(b);
819
820         if (!finfo)
821                 return -1;
822
823         return finfo->msgnum - msgnum;
824 }