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