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