fixed bug in matching dialog box
[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
238                 MSG_SET_FLAGS(msginfo->flags, default_flags);
239
240                 /* if the message file doesn't exist or is changed,
241                    don't add the data */
242                 if (type == F_MH && scan_file &&
243                     folder_item_is_msg_changed(item, msginfo))
244                         procmsg_msginfo_free(msginfo);
245                 else {
246                         msginfo->folder = item;
247
248                         if (!mlist)
249                                 last = mlist = g_slist_append(NULL, msginfo);
250                         else {
251                                 last = g_slist_append(last, msginfo);
252                                 last = last->next;
253                         }
254                 }
255         }
256
257         fclose(fp);
258         debug_print(_("done.\n"));
259
260         return mlist;
261 }
262
263 #undef READ_CACHE_DATA
264 #undef READ_CACHE_DATA_INT
265
266 void procmsg_set_flags(GSList *mlist, FolderItem *item)
267 {
268         GSList *cur, *tmp;
269         gint newmsg = 0;
270         gint lastnum = 0;
271         gchar *markdir;
272         MsgInfo *msginfo;
273         GHashTable *mark_table;
274         MsgFlags flags;
275
276         if (!mlist) return;
277         g_return_if_fail(item != NULL);
278         g_return_if_fail(item->folder != NULL);
279
280         debug_print(_("\tMarking the messages..."));
281
282         markdir = folder_item_get_path(item);
283         mark_table = procmsg_read_mark_file(markdir);
284         g_free(markdir);
285
286         if (!mark_table) return;
287
288         for (cur = mlist; cur != NULL; cur = cur->next) {
289                 msginfo = (MsgInfo *)cur->data;
290
291                 if (lastnum < msginfo->msgnum)
292                         lastnum = msginfo->msgnum;
293
294                 flags = GPOINTER_TO_UINT(g_hash_table_lookup(mark_table,
295                                          GUINT_TO_POINTER(msginfo->msgnum)));
296
297                 if (flags != 0) {
298                         /* add the permanent flags only */
299                         MSG_UNSET_FLAGS(msginfo->flags,
300                                         MSG_PERMANENT_FLAG_MASK);
301                         MSG_SET_FLAGS(msginfo->flags,
302                                       flags & MSG_PERMANENT_FLAG_MASK);
303                         if (item->folder->type == F_IMAP) {
304                                 MSG_SET_FLAGS(msginfo->flags, MSG_IMAP);
305                         } else if (item->folder->type == F_NEWS) {
306                                 MSG_SET_FLAGS(msginfo->flags, MSG_NEWS);
307                         }
308                 } else {
309                         /* not found (new message) */
310                         if (newmsg == 0) {
311                                 for (tmp = mlist; tmp != cur; tmp = tmp->next)
312                                         MSG_UNSET_FLAGS
313                                                 (((MsgInfo *)tmp->data)->flags,
314                                                  MSG_NEW);
315                         }
316                         newmsg++;
317                 }
318         }
319
320         item->last_num = lastnum;
321
322         debug_print(_("done.\n"));
323         if (newmsg)
324                 debug_print(_("\t%d new message(s)\n"), newmsg);
325
326         g_hash_table_destroy(mark_table);
327 }
328
329 gint procmsg_get_last_num_in_cache(GSList *mlist)
330 {
331         GSList *cur;
332         MsgInfo *msginfo;
333         gint last = 0;
334
335         if (mlist == NULL) return 0;
336
337         for (cur = mlist; cur != NULL; cur = cur->next) {
338                 msginfo = (MsgInfo *)cur->data;
339                 if (msginfo && msginfo->msgnum > last)
340                         last = msginfo->msgnum;
341         }
342
343         return last;
344 }
345
346 void procmsg_write_cache(MsgInfo *msginfo, FILE *fp)
347 {
348         MsgFlags flags = msginfo->flags & MSG_CACHED_FLAG_MASK;
349
350         WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
351         WRITE_CACHE_DATA_INT(msginfo->size, fp);
352         WRITE_CACHE_DATA_INT(msginfo->mtime, fp);
353         WRITE_CACHE_DATA_INT(msginfo->date_t, fp);
354         WRITE_CACHE_DATA_INT(flags, fp);
355
356         WRITE_CACHE_DATA(msginfo->fromname, fp);
357
358         WRITE_CACHE_DATA(msginfo->date, fp);
359         WRITE_CACHE_DATA(msginfo->from, fp);
360         WRITE_CACHE_DATA(msginfo->to, fp);
361         WRITE_CACHE_DATA(msginfo->cc, fp);
362         WRITE_CACHE_DATA(msginfo->newsgroups, fp);
363         WRITE_CACHE_DATA(msginfo->subject, fp);
364         WRITE_CACHE_DATA(msginfo->msgid, fp);
365         WRITE_CACHE_DATA(msginfo->inreplyto, fp);
366 }
367
368 void procmsg_write_flags(MsgInfo *msginfo, FILE *fp)
369 {
370         MsgFlags flags;
371
372         flags = msginfo->flags & MSG_PERMANENT_FLAG_MASK;
373
374         WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
375         WRITE_CACHE_DATA_INT(flags, fp);
376 }
377
378 struct MarkSum {
379         gint *new;
380         gint *unread;
381         gint *total;
382 };
383
384 static void mark_sum_func(gpointer key, gpointer value, gpointer data)
385 {
386         MsgFlags flags = GPOINTER_TO_UINT(value);
387         struct MarkSum *marksum = data;
388
389         if (MSG_IS_NEW(flags)) (*marksum->new)++;
390         if (MSG_IS_UNREAD(flags)) (*marksum->unread)++;
391         (*marksum->total)++;
392 }
393
394 void procmsg_get_mark_sum(const gchar *folder,
395                           gint *new, gint *unread, gint *total)
396 {
397         GHashTable *mark_table;
398         struct MarkSum marksum;
399
400         *new = *unread = *total = 0;
401         marksum.new    = new;
402         marksum.unread = unread;
403         marksum.total  = total;
404
405         mark_table = procmsg_read_mark_file(folder);
406
407         if (mark_table) {
408                 g_hash_table_foreach(mark_table, mark_sum_func, &marksum);
409                 g_hash_table_destroy(mark_table);
410         }
411 }
412
413 static GHashTable *procmsg_read_mark_file(const gchar *folder)
414 {
415         FILE *fp;
416         GHashTable *mark_table = NULL;
417         gint num;
418         MsgFlags flags;
419
420         if ((fp = procmsg_open_mark_file(folder, FALSE)) == NULL)
421                 return NULL;
422
423         mark_table = g_hash_table_new(NULL, g_direct_equal);
424
425         while (fread(&num, sizeof(num), 1, fp) == 1) {
426                 if (fread(&flags, sizeof(flags), 1, fp) != 1) break;
427                 MSG_SET_FLAGS(flags, MSG_CACHED);
428
429                 g_hash_table_insert(mark_table,
430                                     GUINT_TO_POINTER(num),
431                                     GUINT_TO_POINTER(flags));
432         }
433
434         fclose(fp);
435         return mark_table;
436 }
437
438 FILE *procmsg_open_mark_file(const gchar *folder, gboolean append)
439 {
440         gchar *markfile;
441         FILE *fp;
442         gint ver;
443
444         markfile = g_strconcat(folder, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
445
446         if ((fp = fopen(markfile, "r")) == NULL)
447                 debug_print(_("Mark file not found.\n"));
448         else if (fread(&ver, sizeof(ver), 1, fp) != 1 || MARK_VERSION != ver) {
449                 debug_print(_("Mark version is different (%d != %d). "
450                               "Discarding it.\n"), ver, MARK_VERSION);
451                 fclose(fp);
452                 fp = NULL;
453         }
454
455         /* read mode */
456         if (append == FALSE) {
457                 g_free(markfile);
458                 return fp;
459         }
460
461         if (fp) {
462                 /* reopen with append mode */
463                 fclose(fp);
464                 if ((fp = fopen(markfile, "a")) == NULL)
465                         g_warning(_("Can't open mark file with append mode.\n"));
466         } else {
467                 /* open with overwrite mode if mark file doesn't exist or
468                    version is different */
469                 if ((fp = fopen(markfile, "w")) == NULL)
470                         g_warning(_("Can't open mark file with write mode.\n"));
471                 else {
472                         ver = MARK_VERSION;
473                         WRITE_CACHE_DATA_INT(ver, fp);
474                 }
475         }
476
477         g_free(markfile);
478         return fp;
479 }
480
481 void procmsg_move_messages(GSList *mlist)
482 {
483         GSList *cur, *movelist = NULL;
484         MsgInfo *msginfo;
485         FolderItem *dest = NULL;
486         GHashTable *hash;
487
488         if (!mlist) return;
489
490         hash = procmsg_to_folder_hash_table_create(mlist);
491         folder_item_scan_foreach(hash);
492         g_hash_table_destroy(hash);
493
494         for (cur = mlist; cur != NULL; cur = cur->next) {
495                 msginfo = (MsgInfo *)cur->data;
496                 if (!dest) {
497                         dest = msginfo->to_folder;
498                         movelist = g_slist_append(movelist, msginfo);
499                 } else if (dest == msginfo->to_folder) {
500                         movelist = g_slist_append(movelist, msginfo);
501                 } else {
502                         folder_item_move_msgs_with_dest(dest, movelist);
503                         g_slist_free(movelist);
504                         movelist = NULL;
505                         dest = msginfo->to_folder;
506                         movelist = g_slist_append(movelist, msginfo);
507                 }
508         }
509
510         if (movelist) {
511                 folder_item_move_msgs_with_dest(dest, movelist);
512                 g_slist_free(movelist);
513         }
514 }
515
516 void procmsg_copy_messages(GSList *mlist)
517 {
518         GSList *cur, *copylist = NULL;
519         MsgInfo *msginfo;
520         FolderItem *dest = NULL;
521         GHashTable *hash;
522
523         if (!mlist) return;
524
525         hash = procmsg_to_folder_hash_table_create(mlist);
526         folder_item_scan_foreach(hash);
527         g_hash_table_destroy(hash);
528
529         for (cur = mlist; cur != NULL; cur = cur->next) {
530                 msginfo = (MsgInfo *)cur->data;
531                 if (!dest) {
532                         dest = msginfo->to_folder;
533                         copylist = g_slist_append(copylist, msginfo);
534                 } else if (dest == msginfo->to_folder) {
535                         copylist = g_slist_append(copylist, msginfo);
536                 } else {
537                         folder_item_copy_msgs_with_dest(dest, copylist);
538                         g_slist_free(copylist);
539                         copylist = NULL;
540                         dest = msginfo->to_folder;
541                         copylist = g_slist_append(copylist, msginfo);
542                 }
543         }
544
545         if (copylist) {
546                 folder_item_copy_msgs_with_dest(dest, copylist);
547                 g_slist_free(copylist);
548         }
549 }
550
551 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
552 {
553         gchar *path, *file;
554
555         g_return_val_if_fail(msginfo != NULL, NULL);
556
557         if (msginfo->plaintext_file)
558                 file = g_strdup(msginfo->plaintext_file);
559         else {
560                 path = folder_item_get_path(msginfo->folder);
561                 file = g_strconcat(path, G_DIR_SEPARATOR_S,
562                                    itos(msginfo->msgnum), NULL);
563                 g_free(path);
564         }
565
566         return file;
567 }
568
569 gchar *procmsg_get_message_file(MsgInfo *msginfo)
570 {
571         gchar *filename = NULL;
572
573         g_return_val_if_fail(msginfo != NULL, NULL);
574
575         filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
576         if (!filename)
577                 g_warning(_("can't fetch message %d\n"), msginfo->msgnum);
578
579         return filename;
580 }
581
582 FILE *procmsg_open_message(MsgInfo *msginfo)
583 {
584         FILE *fp;
585         gchar *file;
586
587         g_return_val_if_fail(msginfo != NULL, NULL);
588
589         file = procmsg_get_message_file_path(msginfo);
590         g_return_val_if_fail(file != NULL, NULL);
591
592         if ((fp = fopen(file, "r")) == NULL) {
593                 FILE_OP_ERROR(file, "fopen");
594                 g_free(file);
595                 return NULL;
596         }
597
598         g_free(file);
599
600         if (MSG_IS_QUEUED(msginfo->flags)) {
601                 gchar buf[BUFFSIZE];
602
603                 while (fgets(buf, sizeof(buf), fp) != NULL)
604                         if (buf[0] == '\r' || buf[0] == '\n') break;
605         }
606
607         return fp;
608 }
609
610 gboolean procmsg_msg_exist(MsgInfo *msginfo)
611 {
612         gchar *path;
613         gboolean ret;
614
615         if (!msginfo) return FALSE;
616
617         path = procmsg_get_message_file_path(msginfo);
618         change_dir(path);
619         ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
620         g_free(path);
621
622         return ret;
623 }
624
625 void procmsg_empty_trash(void)
626 {
627         FolderItem *trash;
628         GList *cur;
629
630         for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
631                 trash = FOLDER(cur->data)->trash;
632                 if (trash) folder_item_remove_all_msg(trash);
633         }
634 }
635
636 gint procmsg_send_queue(void)
637 {
638         FolderItem *queue;
639         gint i;
640
641         queue = folder_get_default_queue();
642         g_return_val_if_fail(queue != NULL, -1);
643         folder_item_scan(queue);
644         if (queue->last_num < 0) return -1;
645         else if (queue->last_num == 0) return 0;
646
647         for (i = 1; i <= queue->last_num; i++) {
648                 gchar *file;
649
650                 file = folder_item_fetch_msg(queue, i);
651                 if (file) {
652                         if (send_message_queue(file) < 0) {
653                                 g_warning(_("Sending queued message failed.\n"));
654                                 g_free(file);
655                                 return -1;
656                         }
657                         folder_item_remove_msg(queue, i);
658                         g_free(file);
659                 }
660         }
661
662         return 0;
663 }
664
665 void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
666 {
667         static const gchar *def_cmd = "lpr %s";
668         static guint id = 0;
669         gchar *prtmp;
670         FILE *tmpfp, *prfp;
671         gchar buf[1024];
672         gchar *p;
673
674         g_return_if_fail(msginfo);
675
676         if ((tmpfp = procmime_get_text_part(msginfo)) == NULL) {
677                 g_warning(_("Can't get text part\n"));
678                 return;
679         }
680
681         prtmp = g_strdup_printf("%s%cprinttmp.%08x",
682                                 get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
683
684         if ((prfp = fopen(prtmp, "w")) == NULL) {
685                 FILE_OP_ERROR(prtmp, "fopen");
686                 g_free(prtmp);
687                 fclose(tmpfp);
688                 return;
689         }
690
691         if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
692         if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
693         if (msginfo->to)   fprintf(prfp, "To: %s\n", msginfo->to);
694         if (msginfo->cc)   fprintf(prfp, "Cc: %s\n", msginfo->cc);
695         if (msginfo->newsgroups)
696                 fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
697         if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
698         fputc('\n', prfp);
699
700         while (fgets(buf, sizeof(buf), tmpfp) != NULL)
701                 fputs(buf, prfp);
702
703         fclose(prfp);
704         fclose(tmpfp);
705
706         if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
707             !strchr(p + 2, '%'))
708                 g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
709         else {
710                 if (cmdline)
711                         g_warning(_("Print command line is invalid: `%s'\n"),
712                                   cmdline);
713                 g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
714         }
715
716         g_free(prtmp);
717
718         g_strchomp(buf);
719         if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
720         system(buf);
721 }
722
723 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
724 {
725         MsgInfo *newmsginfo;
726
727         if (msginfo == NULL) return NULL;
728
729         newmsginfo = g_new0(MsgInfo, 1);
730
731 #define MEMBCOPY(mmb)   newmsginfo->mmb = msginfo->mmb
732 #define MEMBDUP(mmb)    newmsginfo->mmb = msginfo->mmb ? \
733                         g_strdup(msginfo->mmb) : NULL
734
735         MEMBCOPY(msgnum);
736         MEMBCOPY(size);
737         MEMBCOPY(mtime);
738         MEMBCOPY(date_t);
739         MEMBCOPY(flags);
740
741         MEMBDUP(fromname);
742
743         MEMBDUP(date);
744         MEMBDUP(from);
745         MEMBDUP(to);
746         MEMBDUP(cc);
747         MEMBDUP(newsgroups);
748         MEMBDUP(subject);
749         MEMBDUP(msgid);
750         MEMBDUP(inreplyto);
751
752         MEMBCOPY(folder);
753         MEMBCOPY(to_folder);
754
755         MEMBDUP(xface);
756         MEMBDUP(dispositionnotificationto);
757         MEMBDUP(returnreceiptto);
758
759         MEMBCOPY(score);
760         MEMBCOPY(threadscore);
761
762         return newmsginfo;
763 }
764
765 void procmsg_msginfo_free(MsgInfo *msginfo)
766 {
767         if (msginfo == NULL) return;
768
769         g_free(msginfo->returnreceiptto);
770         g_free(msginfo->dispositionnotificationto);
771         g_free(msginfo->xface);
772
773         g_free(msginfo->fromname);
774
775         g_free(msginfo->date);
776         g_free(msginfo->from);
777         g_free(msginfo->to);
778         g_free(msginfo->cc);
779         g_free(msginfo->newsgroups);
780         g_free(msginfo->subject);
781         g_free(msginfo->msgid);
782         g_free(msginfo->inreplyto);
783
784         g_free(msginfo);
785 }
786
787 static gint procmsg_cmp_msgnum(gconstpointer a, gconstpointer b)
788 {
789         const MsgInfo *msginfo = a;
790         const guint msgnum = GPOINTER_TO_UINT(b);
791
792         if (!msginfo)
793                 return -1;
794
795         return msginfo->msgnum - msgnum;
796 }
797
798 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
799 {
800         const MsgInfo *msginfo1 = a;
801         const MsgInfo *msginfo2 = b;
802
803         if (!msginfo1)
804                 return -1;
805         if (!msginfo2)
806                 return -1;
807
808         return msginfo1->msgnum - msginfo2->msgnum;
809 }
810
811 static gint procmsg_cmp_flag_msgnum(gconstpointer a, gconstpointer b)
812 {
813         const FlagInfo *finfo = a;
814         const guint msgnum = GPOINTER_TO_UINT(b);
815
816         if (!finfo)
817                 return -1;
818
819         return finfo->msgnum - msgnum;
820 }