Statistics
| Revision:

root / src / procmsg.c @ 1

History | View | Annotate | Download (34.8 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2004 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
#include <ctype.h>
26

    
27
#include "intl.h"
28
#include "main.h"
29
#include "utils.h"
30
#include "procmsg.h"
31
#include "procheader.h"
32
#include "account.h"
33
#include "send_message.h"
34
#include "procmime.h"
35
#include "statusbar.h"
36
#include "prefs_filter.h"
37
#include "filter.h"
38
#include "folder.h"
39
#if USE_GPGME
40
#  include "rfc2015.h"
41
#endif
42

    
43
static void mark_sum_func                        (gpointer         key,
44
                                                 gpointer         value,
45
                                                 gpointer         data);
46

    
47
static GHashTable *procmsg_read_mark_file        (FolderItem        *item);
48
static void procmsg_write_mark_file                (FolderItem        *item,
49
                                                 GHashTable        *mark_table);
50

    
51
static FILE *procmsg_open_data_file                (const gchar        *file,
52
                                                 guint                 version,
53
                                                 DataOpenMode         mode,
54
                                                 gchar                *buf,
55
                                                 size_t                 buf_size);
56
static FILE *procmsg_open_cache_file_with_buffer(FolderItem        *item,
57
                                                 DataOpenMode         mode,
58
                                                 gchar                *buf,
59
                                                 size_t                 buf_size);
60

    
61
static gint procmsg_cmp_by_mark                        (gconstpointer         a,
62
                                                 gconstpointer         b);
63
static gint procmsg_cmp_by_unread                (gconstpointer         a,
64
                                                 gconstpointer         b);
65
static gint procmsg_cmp_by_mime                        (gconstpointer         a,
66
                                                 gconstpointer         b);
67
static gint procmsg_cmp_by_label                (gconstpointer         a,
68
                                                 gconstpointer         b);
69
static gint procmsg_cmp_by_number                (gconstpointer         a,
70
                                                 gconstpointer         b);
71
static gint procmsg_cmp_by_size                        (gconstpointer         a,
72
                                                 gconstpointer         b);
73
static gint procmsg_cmp_by_date                        (gconstpointer         a,
74
                                                 gconstpointer         b);
75
static gint procmsg_cmp_by_from                        (gconstpointer         a,
76
                                                 gconstpointer         b);
77
static gint procmsg_cmp_by_to                        (gconstpointer         a,
78
                                                 gconstpointer         b);
79
static gint procmsg_cmp_by_subject                (gconstpointer         a,
80
                                                 gconstpointer         b);
81

    
82

    
83
GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
84
{
85
        GHashTable *msg_table;
86

    
87
        if (mlist == NULL) return NULL;
88

    
89
        msg_table = g_hash_table_new(NULL, g_direct_equal);
90
        procmsg_msg_hash_table_append(msg_table, mlist);
91

    
92
        return msg_table;
93
}
94

    
95
void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
96
{
97
        GSList *cur;
98
        MsgInfo *msginfo;
99

    
100
        if (msg_table == NULL || mlist == NULL) return;
101

    
102
        for (cur = mlist; cur != NULL; cur = cur->next) {
103
                msginfo = (MsgInfo *)cur->data;
104

    
105
                g_hash_table_insert(msg_table,
106
                                    GUINT_TO_POINTER(msginfo->msgnum),
107
                                    msginfo);
108
        }
109
}
110

    
111
GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
112
{
113
        GHashTable *msg_table;
114
        GSList *cur;
115
        MsgInfo *msginfo;
116

    
117
        if (mlist == NULL) return NULL;
118

    
119
        msg_table = g_hash_table_new(NULL, g_direct_equal);
120

    
121
        for (cur = mlist; cur != NULL; cur = cur->next) {
122
                msginfo = (MsgInfo *)cur->data;
123
                g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
124
        }
125

    
126
        return msg_table;
127
}
128

    
129
static gint procmsg_read_cache_data_str(FILE *fp, gchar **str)
130
{
131
        gchar buf[BUFFSIZE];
132
        gint ret = 0;
133
        guint32 len;
134

    
135
        if (fread(&len, sizeof(len), 1, fp) == 1) {
136
                if (len > G_MAXINT)
137
                        ret = -1;
138
                else {
139
                        gchar *tmp = NULL;
140

    
141
                        while (len > 0) {
142
                                size_t size = MIN(len, BUFFSIZE - 1);
143

    
144
                                if (fread(buf, size, 1, fp) != 1) {
145
                                        ret = -1;
146
                                        if (tmp) g_free(tmp);
147
                                        *str = NULL;
148
                                        break;
149
                                }
150

    
151
                                buf[size] = '\0';
152
                                if (tmp) {
153
                                        *str = g_strconcat(tmp, buf, NULL);
154
                                        g_free(tmp);
155
                                        tmp = *str;
156
                                } else
157
                                        tmp = *str = g_strdup(buf);
158

    
159
                                len -= size;
160
                        }
161
                }
162
        } else
163
                ret = -1;
164

    
165
        if (ret < 0)
166
                g_warning("Cache data is corrupted\n");
167

    
168
        return ret;
169
}
170

    
171
#define READ_CACHE_DATA(data, fp)                                \
172
{                                                                \
173
        if (procmsg_read_cache_data_str(fp, &data) < 0) {        \
174
                procmsg_msginfo_free(msginfo);                        \
175
                procmsg_msg_list_free(mlist);                        \
176
                mlist = NULL;                                        \
177
                break;                                                \
178
        }                                                        \
179
}
180

    
181
#define READ_CACHE_DATA_INT(n, fp)                                \
182
{                                                                \
183
        guint32 idata;                                                \
184
                                                                \
185
        if (fread(&idata, sizeof(idata), 1, fp) != 1) {                \
186
                g_warning("Cache data is corrupted\n");                \
187
                procmsg_msginfo_free(msginfo);                        \
188
                procmsg_msg_list_free(mlist);                        \
189
                mlist = NULL;                                        \
190
                break;                                                \
191
        } else                                                        \
192
                n = idata;                                        \
193
}
194

    
195
GSList *procmsg_read_cache(FolderItem *item, gboolean scan_file)
196
{
197
        GSList *mlist = NULL;
198
        GSList *last = NULL;
199
        FILE *fp;
200
        MsgInfo *msginfo;
201
        MsgFlags default_flags;
202
        gchar file_buf[BUFFSIZE];
203
        guint32 num;
204
        FolderType type;
205

    
206
        g_return_val_if_fail(item != NULL, NULL);
207
        g_return_val_if_fail(item->folder != NULL, NULL);
208
        type = FOLDER_TYPE(item->folder);
209

    
210
        default_flags.perm_flags = MSG_NEW|MSG_UNREAD;
211
        default_flags.tmp_flags = 0;
212
        if (type == F_MH || type == F_IMAP) {
213
                if (item->stype == F_QUEUE) {
214
                        MSG_SET_TMP_FLAGS(default_flags, MSG_QUEUED);
215
                } else if (item->stype == F_DRAFT) {
216
                        MSG_SET_TMP_FLAGS(default_flags, MSG_DRAFT);
217
                }
218
        }
219
        if (type == F_IMAP) {
220
                MSG_SET_TMP_FLAGS(default_flags, MSG_IMAP);
221
        } else if (type == F_NEWS) {
222
                MSG_SET_TMP_FLAGS(default_flags, MSG_NEWS);
223
        }
224

    
225
        if (type == F_MH) {
226
                gchar *path;
227

    
228
                path = folder_item_get_path(item);
229
                if (change_dir(path) < 0) {
230
                        g_free(path);
231
                        return NULL;
232
                }
233
                g_free(path);
234
        }
235

    
236
        if ((fp = procmsg_open_cache_file_with_buffer
237
                (item, DATA_READ, file_buf, sizeof(file_buf))) == NULL)
238
                return NULL;
239

    
240
        debug_print("Reading summary cache...");
241

    
242
        while (fread(&num, sizeof(num), 1, fp) == 1) {
243
                msginfo = g_new0(MsgInfo, 1);
244
                msginfo->msgnum = num;
245
                READ_CACHE_DATA_INT(msginfo->size, fp);
246
                READ_CACHE_DATA_INT(msginfo->mtime, fp);
247
                READ_CACHE_DATA_INT(msginfo->date_t, fp);
248
                READ_CACHE_DATA_INT(msginfo->flags.tmp_flags, fp);
249

    
250
                READ_CACHE_DATA(msginfo->fromname, fp);
251

    
252
                READ_CACHE_DATA(msginfo->date, fp);
253
                READ_CACHE_DATA(msginfo->from, fp);
254
                READ_CACHE_DATA(msginfo->to, fp);
255
                READ_CACHE_DATA(msginfo->newsgroups, fp);
256
                READ_CACHE_DATA(msginfo->subject, fp);
257
                READ_CACHE_DATA(msginfo->msgid, fp);
258
                READ_CACHE_DATA(msginfo->inreplyto, fp);
259

    
260
                MSG_SET_PERM_FLAGS(msginfo->flags, default_flags.perm_flags);
261
                MSG_SET_TMP_FLAGS(msginfo->flags, default_flags.tmp_flags);
262

    
263
                /* if the message file doesn't exist or is changed,
264
                   don't add the data */
265
                if (type == F_MH && scan_file &&
266
                    folder_item_is_msg_changed(item, msginfo))
267
                        procmsg_msginfo_free(msginfo);
268
                else {
269
                        msginfo->folder = item;
270

    
271
                        if (!mlist)
272
                                last = mlist = g_slist_append(NULL, msginfo);
273
                        else {
274
                                last = g_slist_append(last, msginfo);
275
                                last = last->next;
276
                        }
277
                }
278
        }
279

    
280
        fclose(fp);
281

    
282
        debug_print("done.\n");
283

    
284
        return mlist;
285
}
286

    
287
#undef READ_CACHE_DATA
288
#undef READ_CACHE_DATA_INT
289

    
290
static void mark_unset_new_func(gpointer key, gpointer value, gpointer data)
291
{
292
        MSG_UNSET_PERM_FLAGS(*((MsgFlags *)value), MSG_NEW);
293
}
294

    
295
void procmsg_set_flags(GSList *mlist, FolderItem *item)
296
{
297
        GSList *cur;
298
        gint new = 0, unread = 0, total = 0;
299
        gint lastnum = 0;
300
        gint unflagged = 0;
301
        gboolean mark_queue_exist;
302
        MsgInfo *msginfo;
303
        GHashTable *mark_table;
304
        MsgFlags *flags;
305

    
306
        g_return_if_fail(item != NULL);
307
        g_return_if_fail(item->folder != NULL);
308

    
309
        debug_print("Marking the messages...\n");
310

    
311
        mark_queue_exist = (item->mark_queue != NULL);
312
        mark_table = procmsg_read_mark_file(item);
313
        if (!mark_table) {
314
                item->new = item->unread = item->total = g_slist_length(mlist);
315
                item->updated = TRUE;
316
                return;
317
        }
318

    
319
        /* unset new flags if new (unflagged) messages exist */
320
        if (!mark_queue_exist) {
321
                for (cur = mlist; cur != NULL; cur = cur->next) {
322
                        msginfo = (MsgInfo *)cur->data;
323
                        flags = g_hash_table_lookup
324
                                (mark_table, GUINT_TO_POINTER(msginfo->msgnum));
325
                        if (!flags) {
326
                                g_hash_table_foreach(mark_table,
327
                                                     mark_unset_new_func, NULL);
328
                                break;
329
                        }
330
                }
331
        }
332

    
333
        for (cur = mlist; cur != NULL; cur = cur->next) {
334
                msginfo = (MsgInfo *)cur->data;
335

    
336
                if (lastnum < msginfo->msgnum)
337
                        lastnum = msginfo->msgnum;
338

    
339
                flags = g_hash_table_lookup
340
                        (mark_table, GUINT_TO_POINTER(msginfo->msgnum));
341

    
342
                if (flags != NULL) {
343
                        /* add the permanent flags only */
344
                        msginfo->flags.perm_flags = flags->perm_flags;
345
                        if (MSG_IS_NEW(*flags))
346
                                ++new;
347
                        if (MSG_IS_UNREAD(*flags))
348
                                ++unread;
349
                        if (FOLDER_TYPE(item->folder) == F_IMAP) {
350
                                MSG_SET_TMP_FLAGS(msginfo->flags, MSG_IMAP);
351
                        } else if (FOLDER_TYPE(item->folder) == F_NEWS) {
352
                                MSG_SET_TMP_FLAGS(msginfo->flags, MSG_NEWS);
353
                        }
354
                } else {
355
                        ++unflagged;
356
                        ++new;
357
                        ++unread;
358
                }
359

    
360
                ++total;
361
        }
362

    
363
        item->new = new;
364
        item->unread = unread;
365
        item->total = total;
366
        item->unmarked_num = unflagged;
367
        item->last_num = lastnum;
368
        item->updated = TRUE;
369

    
370
        debug_print("new: %d unread: %d unflagged: %d total: %d\n",
371
                    new, unread, unflagged, total);
372

    
373
        hash_free_value_mem(mark_table);
374
        g_hash_table_destroy(mark_table);
375
}
376

    
377
static FolderSortType cmp_func_sort_type;
378

    
379
GSList *procmsg_sort_msg_list(GSList *mlist, FolderSortKey sort_key,
380
                              FolderSortType sort_type)
381
{
382
        GCompareFunc cmp_func;
383

    
384
        switch (sort_key) {
385
        case SORT_BY_MARK:
386
                cmp_func = procmsg_cmp_by_mark; break;
387
        case SORT_BY_UNREAD:
388
                cmp_func = procmsg_cmp_by_unread; break;
389
        case SORT_BY_MIME:
390
                cmp_func = procmsg_cmp_by_mime; break;
391
        case SORT_BY_LABEL:
392
                cmp_func = procmsg_cmp_by_label; break;
393
        case SORT_BY_NUMBER:
394
                cmp_func = procmsg_cmp_by_number; break;
395
        case SORT_BY_SIZE:
396
                cmp_func = procmsg_cmp_by_size; break;
397
        case SORT_BY_DATE:
398
                cmp_func = procmsg_cmp_by_date; break;
399
        case SORT_BY_FROM:
400
                cmp_func = procmsg_cmp_by_from; break;
401
        case SORT_BY_SUBJECT:
402
                cmp_func = procmsg_cmp_by_subject; break;
403
        case SORT_BY_TO:
404
                cmp_func = procmsg_cmp_by_to; break;
405
        default:
406
                return mlist;
407
        }
408

    
409
        cmp_func_sort_type = sort_type;
410

    
411
        mlist = g_slist_sort(mlist, cmp_func);
412

    
413
        return mlist;
414
}
415

    
416
gint procmsg_get_last_num_in_msg_list(GSList *mlist)
417
{
418
        GSList *cur;
419
        MsgInfo *msginfo;
420
        gint last = 0;
421

    
422
        for (cur = mlist; cur != NULL; cur = cur->next) {
423
                msginfo = (MsgInfo *)cur->data;
424
                if (msginfo && msginfo->msgnum > last)
425
                        last = msginfo->msgnum;
426
        }
427

    
428
        return last;
429
}
430

    
431
void procmsg_msg_list_free(GSList *mlist)
432
{
433
        GSList *cur;
434
        MsgInfo *msginfo;
435

    
436
        for (cur = mlist; cur != NULL; cur = cur->next) {
437
                msginfo = (MsgInfo *)cur->data;
438
                procmsg_msginfo_free(msginfo);
439
        }
440
        g_slist_free(mlist);
441
}
442

    
443
void procmsg_write_cache(MsgInfo *msginfo, FILE *fp)
444
{
445
        MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK;
446

    
447
        WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
448
        WRITE_CACHE_DATA_INT(msginfo->size, fp);
449
        WRITE_CACHE_DATA_INT(msginfo->mtime, fp);
450
        WRITE_CACHE_DATA_INT(msginfo->date_t, fp);
451
        WRITE_CACHE_DATA_INT(flags, fp);
452

    
453
        WRITE_CACHE_DATA(msginfo->fromname, fp);
454

    
455
        WRITE_CACHE_DATA(msginfo->date, fp);
456
        WRITE_CACHE_DATA(msginfo->from, fp);
457
        WRITE_CACHE_DATA(msginfo->to, fp);
458
        WRITE_CACHE_DATA(msginfo->newsgroups, fp);
459
        WRITE_CACHE_DATA(msginfo->subject, fp);
460
        WRITE_CACHE_DATA(msginfo->msgid, fp);
461
        WRITE_CACHE_DATA(msginfo->inreplyto, fp);
462
}
463

    
464
void procmsg_write_flags(MsgInfo *msginfo, FILE *fp)
465
{
466
        MsgPermFlags flags = msginfo->flags.perm_flags;
467

    
468
        WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
469
        WRITE_CACHE_DATA_INT(flags, fp);
470
}
471

    
472
void procmsg_flush_mark_queue(FolderItem *item, FILE *fp)
473
{
474
        MsgInfo *flaginfo;
475

    
476
        g_return_if_fail(item != NULL);
477
        g_return_if_fail(fp != NULL);
478

    
479
        if (item->mark_queue)
480
                debug_print("flushing mark_queue...\n");
481

    
482
        while (item->mark_queue != NULL) {
483
                flaginfo = (MsgInfo *)item->mark_queue->data;
484
                procmsg_write_flags(flaginfo, fp);
485
                procmsg_msginfo_free(flaginfo);
486
                item->mark_queue = g_slist_remove(item->mark_queue, flaginfo);
487
        }
488
}
489

    
490
void procmsg_add_mark_queue(FolderItem *item, gint num, MsgFlags flags)
491
{
492
        MsgInfo *queue_msginfo;
493

    
494
        queue_msginfo = g_new0(MsgInfo, 1);
495
        queue_msginfo->msgnum = num;
496
        queue_msginfo->flags = flags;
497
        item->mark_queue = g_slist_append
498
                (item->mark_queue, queue_msginfo);
499
        return;
500
}
501

    
502
void procmsg_add_flags(FolderItem *item, gint num, MsgFlags flags)
503
{
504
        FILE *fp;
505
        MsgInfo msginfo;
506

    
507
        g_return_if_fail(item != NULL);
508

    
509
        if (item->opened) {
510
                procmsg_add_mark_queue(item, num, flags);
511
                return;
512
        }
513

    
514
        if ((fp = procmsg_open_mark_file(item, DATA_APPEND)) == NULL) {
515
                g_warning(_("can't open mark file\n"));
516
                return;
517
        }
518

    
519
        msginfo.msgnum = num;
520
        msginfo.flags = flags;
521

    
522
        procmsg_write_flags(&msginfo, fp);
523
        fclose(fp);
524
}
525

    
526
struct MarkSum {
527
        gint *new;
528
        gint *unread;
529
        gint *total;
530
        gint *min;
531
        gint *max;
532
        gint first;
533
};
534

    
535
static void mark_sum_func(gpointer key, gpointer value, gpointer data)
536
{
537
        MsgFlags *flags = value;
538
        gint num = GPOINTER_TO_INT(key);
539
        struct MarkSum *marksum = data;
540

    
541
        if (marksum->first <= num) {
542
                if (MSG_IS_NEW(*flags)) (*marksum->new)++;
543
                if (MSG_IS_UNREAD(*flags)) (*marksum->unread)++;
544
                if (num > *marksum->max) *marksum->max = num;
545
                if (num < *marksum->min || *marksum->min == 0) *marksum->min = num;
546
                (*marksum->total)++;
547
        }
548

    
549
        g_free(flags);
550
}
551

    
552
void procmsg_get_mark_sum(FolderItem *item,
553
                          gint *new, gint *unread, gint *total,
554
                          gint *min, gint *max,
555
                          gint first)
556
{
557
        GHashTable *mark_table;
558
        struct MarkSum marksum;
559

    
560
        *new = *unread = *total = *min = *max = 0;
561
        marksum.new    = new;
562
        marksum.unread = unread;
563
        marksum.total  = total;
564
        marksum.min    = min;
565
        marksum.max    = max;
566
        marksum.first  = first;
567

    
568
        mark_table = procmsg_read_mark_file(item);
569

    
570
        if (mark_table) {
571
                g_hash_table_foreach(mark_table, mark_sum_func, &marksum);
572
                g_hash_table_destroy(mark_table);
573
        }
574
}
575

    
576
static GHashTable *procmsg_read_mark_file(FolderItem *item)
577
{
578
        FILE *fp;
579
        GHashTable *mark_table = NULL;
580
        guint32 idata;
581
        guint num;
582
        MsgFlags *flags;
583
        MsgPermFlags perm_flags;
584
        GSList *cur;
585

    
586
        if ((fp = procmsg_open_mark_file(item, DATA_READ)) == NULL)
587
                return NULL;
588

    
589
        mark_table = g_hash_table_new(NULL, g_direct_equal);
590

    
591
        while (fread(&idata, sizeof(idata), 1, fp) == 1) {
592
                num = idata;
593
                if (fread(&idata, sizeof(idata), 1, fp) != 1) break;
594
                perm_flags = idata;
595

    
596
                flags = g_hash_table_lookup(mark_table, GUINT_TO_POINTER(num));
597
                if (flags != NULL)
598
                        g_free(flags);
599

    
600
                flags = g_new0(MsgFlags, 1);
601
                flags->perm_flags = perm_flags;
602

    
603
                g_hash_table_insert(mark_table, GUINT_TO_POINTER(num), flags);
604
        }
605

    
606
        fclose(fp);
607

    
608
        if (item->mark_queue) {
609
                g_hash_table_foreach(mark_table, mark_unset_new_func, NULL);
610
        }
611

    
612
        for (cur = item->mark_queue; cur != NULL; cur = cur->next) {
613
                MsgInfo *msginfo = (MsgInfo *)cur->data;
614

    
615
                flags = g_hash_table_lookup(mark_table,
616
                                            GUINT_TO_POINTER(msginfo->msgnum));
617
                if (flags != NULL)
618
                        g_free(flags);
619

    
620
                flags = g_new0(MsgFlags, 1);
621
                flags->perm_flags = msginfo->flags.perm_flags;
622

    
623
                g_hash_table_insert(mark_table,
624
                                    GUINT_TO_POINTER(msginfo->msgnum), flags);
625
                                    
626
        }
627

    
628
        if (item->mark_queue && !item->opened) {
629
                procmsg_write_mark_file(item, mark_table);
630
                procmsg_msg_list_free(item->mark_queue);
631
                item->mark_queue = NULL;
632
        }
633

    
634
        return mark_table;
635
}
636

    
637
static void write_mark_func(gpointer key, gpointer value, gpointer data)
638
{
639
        MsgInfo msginfo;
640

    
641
        msginfo.msgnum = GPOINTER_TO_UINT(key);
642
        msginfo.flags.perm_flags = ((MsgFlags *)value)->perm_flags;
643
        procmsg_write_flags(&msginfo, (FILE *)data);
644
}
645

    
646
static void procmsg_write_mark_file(FolderItem *item, GHashTable *mark_table)
647
{
648
        FILE *fp;
649

    
650
        fp = procmsg_open_mark_file(item, DATA_WRITE);
651
        g_hash_table_foreach(mark_table, write_mark_func, fp);
652
        fclose(fp);
653
}
654

    
655
static FILE *procmsg_open_data_file(const gchar *file, guint version,
656
                                    DataOpenMode mode,
657
                                    gchar *buf, size_t buf_size)
658
{
659
        FILE *fp;
660
        guint32 data_ver;
661

    
662
        g_return_val_if_fail(file != NULL, NULL);
663

    
664
        if (mode == DATA_WRITE) {
665
                if ((fp = fopen(file, "wb")) == NULL) {
666
                        FILE_OP_ERROR(file, "fopen");
667
                        return NULL;
668
                }
669
                if (change_file_mode_rw(fp, file) < 0)
670
                        FILE_OP_ERROR(file, "chmod");
671

    
672
                WRITE_CACHE_DATA_INT(version, fp);
673
                return fp;
674
        }
675

    
676
        /* check version */
677
        if ((fp = fopen(file, "rb")) == NULL)
678
                debug_print("Mark/Cache file not found\n");
679
        else {
680
                if (buf && buf_size > 0)
681
                        setvbuf(fp, buf, _IOFBF, buf_size);
682
                if (fread(&data_ver, sizeof(data_ver), 1, fp) != 1 ||
683
                    version != data_ver) {
684
                        debug_print("Mark/Cache version is different (%u != %u). "
685
                                    "Discarding it.\n", data_ver, version);
686
                        fclose(fp);
687
                        fp = NULL;
688
                }
689
        }
690

    
691
        if (mode == DATA_READ)
692
                return fp;
693

    
694
        if (fp) {
695
                /* reopen with append mode */
696
                fclose(fp);
697
                if ((fp = fopen(file, "ab")) == NULL)
698
                        FILE_OP_ERROR(file, "fopen");
699
        } else {
700
                /* open with overwrite mode if mark file doesn't exist or
701
                   version is different */
702
                fp = procmsg_open_data_file(file, version, DATA_WRITE, buf,
703
                                            buf_size);
704
        }
705

    
706
        return fp;
707
}
708

    
709
static FILE *procmsg_open_cache_file_with_buffer(FolderItem *item,
710
                                                 DataOpenMode mode,
711
                                                 gchar *buf, size_t buf_size)
712
{
713
        gchar *cachefile;
714
        FILE *fp;
715

    
716
        cachefile = folder_item_get_cache_file(item);
717
        fp = procmsg_open_data_file(cachefile, CACHE_VERSION, mode, buf,
718
                                    buf_size);
719
        g_free(cachefile);
720

    
721
        return fp;
722
}
723

    
724
FILE *procmsg_open_cache_file(FolderItem *item, DataOpenMode mode)
725
{
726
        gchar *cachefile;
727
        FILE *fp;
728

    
729
        cachefile = folder_item_get_cache_file(item);
730
        fp = procmsg_open_data_file(cachefile, CACHE_VERSION, mode, NULL, 0);
731
        g_free(cachefile);
732

    
733
        return fp;
734
}
735

    
736
FILE *procmsg_open_mark_file(FolderItem *item, DataOpenMode mode)
737
{
738
        gchar *markfile;
739
        FILE *fp;
740

    
741
        markfile = folder_item_get_mark_file(item);
742
        fp = procmsg_open_data_file(markfile, MARK_VERSION, mode, NULL, 0);
743
        g_free(markfile);
744

    
745
        return fp;
746
}
747

    
748
/* return the reversed thread tree */
749
GNode *procmsg_get_thread_tree(GSList *mlist)
750
{
751
        GNode *root, *parent, *node, *next;
752
        GHashTable *table;
753
        MsgInfo *msginfo;
754
        const gchar *msgid;
755

    
756
        root = g_node_new(NULL);
757
        table = g_hash_table_new(g_str_hash, g_str_equal);
758

    
759
        for (; mlist != NULL; mlist = mlist->next) {
760
                msginfo = (MsgInfo *)mlist->data;
761
                parent = root;
762

    
763
                if (msginfo->inreplyto) {
764
                        parent = g_hash_table_lookup(table, msginfo->inreplyto);
765
                        if (parent == NULL)
766
                                parent = root;
767
                }
768
                node = g_node_insert_data_before
769
                        (parent, parent == root ? parent->children : NULL,
770
                         msginfo);
771
                if ((msgid = msginfo->msgid) &&
772
                    g_hash_table_lookup(table, msgid) == NULL)
773
                        g_hash_table_insert(table, (gchar *)msgid, node);
774
        }
775

    
776
        /* complete the unfinished threads */
777
        for (node = root->children; node != NULL; ) {
778
                next = node->next;
779
                msginfo = (MsgInfo *)node->data;
780
                if (msginfo->inreplyto) {
781
                        parent = g_hash_table_lookup(table, msginfo->inreplyto);
782
                        /* node should not be the parent, and node should not
783
                           be an ancestor of parent (circular reference) */
784
                        if (parent && parent != node &&
785
                            !g_node_is_ancestor(node, parent)) {
786
                                g_node_unlink(node);
787
                                g_node_insert_before
788
                                        (parent, parent->children, node);
789
                        }
790
                }
791
                node = next;
792
        }
793

    
794
        g_hash_table_destroy(table);
795

    
796
        return root;
797
}
798

    
799
gint procmsg_move_messages(GSList *mlist)
800
{
801
        GSList *cur, *movelist = NULL;
802
        MsgInfo *msginfo;
803
        FolderItem *dest = NULL;
804
        GHashTable *hash;
805
        gint val = 0;
806

    
807
        if (!mlist) return 0;
808

    
809
        hash = procmsg_to_folder_hash_table_create(mlist);
810
        folder_item_scan_foreach(hash);
811
        g_hash_table_destroy(hash);
812

    
813
        for (cur = mlist; cur != NULL; cur = cur->next) {
814
                msginfo = (MsgInfo *)cur->data;
815
                if (!dest) {
816
                        dest = msginfo->to_folder;
817
                        movelist = g_slist_append(movelist, msginfo);
818
                } else if (dest == msginfo->to_folder) {
819
                        movelist = g_slist_append(movelist, msginfo);
820
                } else {
821
                        val = folder_item_move_msgs(dest, movelist);
822
                        g_slist_free(movelist);
823
                        movelist = NULL;
824
                        if (val == -1)
825
                                return val;
826
                        dest = msginfo->to_folder;
827
                        movelist = g_slist_append(movelist, msginfo);
828
                }
829
        }
830

    
831
        if (movelist) {
832
                val = folder_item_move_msgs(dest, movelist);
833
                g_slist_free(movelist);
834
        }
835

    
836
        return val == -1 ? -1 : 0;
837
}
838

    
839
gint procmsg_copy_messages(GSList *mlist)
840
{
841
        GSList *cur, *copylist = NULL;
842
        MsgInfo *msginfo;
843
        FolderItem *dest = NULL;
844
        GHashTable *hash;
845
        gint val = 0;
846

    
847
        if (!mlist) return 0;
848

    
849
        hash = procmsg_to_folder_hash_table_create(mlist);
850
        folder_item_scan_foreach(hash);
851
        g_hash_table_destroy(hash);
852

    
853
        for (cur = mlist; cur != NULL; cur = cur->next) {
854
                msginfo = (MsgInfo *)cur->data;
855
                if (!dest) {
856
                        dest = msginfo->to_folder;
857
                        copylist = g_slist_append(copylist, msginfo);
858
                } else if (dest == msginfo->to_folder) {
859
                        copylist = g_slist_append(copylist, msginfo);
860
                } else {
861
                        val = folder_item_copy_msgs(dest, copylist);
862
                        g_slist_free(copylist);
863
                        copylist = NULL;
864
                        if (val == -1)
865
                                return val;
866
                        dest = msginfo->to_folder;
867
                        copylist = g_slist_append(copylist, msginfo);
868
                }
869
        }
870

    
871
        if (copylist) {
872
                val = folder_item_copy_msgs(dest, copylist);
873
                g_slist_free(copylist);
874
        }
875

    
876
        return val == -1 ? -1 : 0;
877
}
878

    
879
gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
880
{
881
        gchar *path, *file;
882

    
883
        g_return_val_if_fail(msginfo != NULL, NULL);
884

    
885
        if (msginfo->plaintext_file)
886
                file = g_strdup(msginfo->plaintext_file);
887
        else if (msginfo->file_path)
888
                return g_strdup(msginfo->file_path);
889
        else {
890
                path = folder_item_get_path(msginfo->folder);
891
                file = g_strconcat(path, G_DIR_SEPARATOR_S,
892
                                   itos(msginfo->msgnum), NULL);
893
                g_free(path);
894
        }
895

    
896
        return file;
897
}
898

    
899
gchar *procmsg_get_message_file(MsgInfo *msginfo)
900
{
901
        gchar *filename = NULL;
902

    
903
        g_return_val_if_fail(msginfo != NULL, NULL);
904

    
905
        if (msginfo->file_path)
906
                return g_strdup(msginfo->file_path);
907

    
908
        filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
909
        if (!filename)
910
                debug_print(_("can't fetch message %d\n"), msginfo->msgnum);
911

    
912
        return filename;
913
}
914

    
915
GSList *procmsg_get_message_file_list(GSList *mlist)
916
{
917
        GSList *file_list = NULL;
918
        MsgInfo *msginfo;
919
        MsgFileInfo *fileinfo;
920
        gchar *file;
921

    
922
        while (mlist != NULL) {
923
                msginfo = (MsgInfo *)mlist->data;
924
                file = procmsg_get_message_file(msginfo);
925
                if (!file) {
926
                        procmsg_message_file_list_free(file_list);
927
                        return NULL;
928
                }
929
                fileinfo = g_new(MsgFileInfo, 1);
930
                fileinfo->file = file;
931
                fileinfo->flags = g_new(MsgFlags, 1);
932
                *fileinfo->flags = msginfo->flags;
933
                file_list = g_slist_prepend(file_list, fileinfo);
934
                mlist = mlist->next;
935
        }
936

    
937
        file_list = g_slist_reverse(file_list);
938

    
939
        return file_list;
940
}
941

    
942
void procmsg_message_file_list_free(GSList *file_list)
943
{
944
        GSList *cur;
945
        MsgFileInfo *fileinfo;
946

    
947
        for (cur = file_list; cur != NULL; cur = cur->next) {
948
                fileinfo = (MsgFileInfo *)cur->data;
949
                g_free(fileinfo->file);
950
                g_free(fileinfo->flags);
951
                g_free(fileinfo);
952
        }
953

    
954
        g_slist_free(file_list);
955
}
956

    
957
FILE *procmsg_open_message(MsgInfo *msginfo)
958
{
959
        FILE *fp;
960
        gchar *file;
961

    
962
        g_return_val_if_fail(msginfo != NULL, NULL);
963

    
964
        file = procmsg_get_message_file_path(msginfo);
965
        g_return_val_if_fail(file != NULL, NULL);
966

    
967
        if (!is_file_exist(file)) {
968
                g_free(file);
969
                file = procmsg_get_message_file(msginfo);
970
                if (!file)
971
                        return NULL;
972
        }
973

    
974
        if ((fp = fopen(file, "rb")) == NULL) {
975
                FILE_OP_ERROR(file, "fopen");
976
                g_free(file);
977
                return NULL;
978
        }
979

    
980
        g_free(file);
981

    
982
        if (MSG_IS_QUEUED(msginfo->flags)) {
983
                gchar buf[BUFFSIZE];
984

    
985
                while (fgets(buf, sizeof(buf), fp) != NULL)
986
                        if (buf[0] == '\r' || buf[0] == '\n') break;
987
        }
988

    
989
        return fp;
990
}
991

    
992
#if USE_GPGME
993
FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
994
{
995
        FILE *fp;
996
        MimeInfo *mimeinfo_;
997
        glong fpos;
998

    
999
        g_return_val_if_fail(msginfo != NULL, NULL);
1000

    
1001
        if (mimeinfo) *mimeinfo = NULL;
1002

    
1003
        if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
1004

    
1005
        mimeinfo_ = procmime_scan_mime_header(fp);
1006
        if (!mimeinfo_) {
1007
                fclose(fp);
1008
                return NULL;
1009
        }
1010

    
1011
        if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
1012
            rfc2015_is_encrypted(mimeinfo_)) {
1013
                MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
1014
        }
1015

    
1016
        if (MSG_IS_ENCRYPTED(msginfo->flags) &&
1017
            !msginfo->plaintext_file &&
1018
            !msginfo->decryption_failed) {
1019
                fpos = ftell(fp);
1020
                rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
1021
                if (msginfo->plaintext_file &&
1022
                    !msginfo->decryption_failed) {
1023
                        fclose(fp);
1024
                        procmime_mimeinfo_free_all(mimeinfo_);
1025
                        if ((fp = procmsg_open_message(msginfo)) == NULL)
1026
                                return NULL;
1027
                        mimeinfo_ = procmime_scan_mime_header(fp);
1028
                        if (!mimeinfo_) {
1029
                                fclose(fp);
1030
                                return NULL;
1031
                        }
1032
                } else {
1033
                        if (fseek(fp, fpos, SEEK_SET) < 0)
1034
                                perror("fseek");
1035
                }
1036
        }
1037

    
1038
        if (mimeinfo) *mimeinfo = mimeinfo_;
1039
        return fp;
1040
}
1041
#endif
1042

    
1043
gboolean procmsg_msg_exist(MsgInfo *msginfo)
1044
{
1045
        gchar *path;
1046
        gboolean ret;
1047

    
1048
        if (!msginfo) return FALSE;
1049

    
1050
        path = folder_item_get_path(msginfo->folder);
1051
        change_dir(path);
1052
        ret = !folder_item_is_msg_changed(msginfo->folder, msginfo);
1053
        g_free(path);
1054

    
1055
        return ret;
1056
}
1057

    
1058
void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
1059
                                PrefsFilterType type)
1060
{
1061
        static HeaderEntry hentry[] = {{"List-Id:",           NULL, TRUE},
1062
                                       {"X-ML-Name:",           NULL, TRUE},
1063
                                       {"X-List:",           NULL, TRUE},
1064
                                       {"X-Mailing-list:", NULL, TRUE},
1065
                                       {"X-Sequence:",           NULL, TRUE},
1066
                                       {NULL,                   NULL, FALSE}};
1067
        enum
1068
        {
1069
                H_LIST_ID         = 0,
1070
                H_X_ML_NAME         = 1,
1071
                H_X_LIST         = 2,
1072
                H_X_MAILING_LIST = 3,
1073
                H_X_SEQUENCE         = 4
1074
        };
1075

    
1076
        FILE *fp;
1077

    
1078
        g_return_if_fail(msginfo != NULL);
1079
        g_return_if_fail(header != NULL);
1080
        g_return_if_fail(key != NULL);
1081

    
1082
        *header = NULL;
1083
        *key = NULL;
1084

    
1085
        switch (type) {
1086
        case FILTER_BY_NONE:
1087
                return;
1088
        case FILTER_BY_AUTO:
1089
                if ((fp = procmsg_open_message(msginfo)) == NULL)
1090
                        return;
1091
                procheader_get_header_fields(fp, hentry);
1092
                fclose(fp);
1093

    
1094
#define SET_FILTER_KEY(hstr, idx)        \
1095
{                                        \
1096
        *header = g_strdup(hstr);        \
1097
        *key = hentry[idx].body;        \
1098
        hentry[idx].body = NULL;        \
1099
}
1100

    
1101
                if (hentry[H_LIST_ID].body != NULL) {
1102
                        SET_FILTER_KEY("List-Id", H_LIST_ID);
1103
                        extract_list_id_str(*key);
1104
                } else if (hentry[H_X_ML_NAME].body != NULL) {
1105
                        SET_FILTER_KEY("X-ML-Name", H_X_ML_NAME);
1106
                } else if (hentry[H_X_LIST].body != NULL) {
1107
                        SET_FILTER_KEY("X-List", H_X_LIST);
1108
                } else if (hentry[H_X_MAILING_LIST].body != NULL) {
1109
                        SET_FILTER_KEY("X-Mailing-list", H_X_MAILING_LIST);
1110
                } else if (hentry[H_X_SEQUENCE].body != NULL) {
1111
                        guchar *p;
1112

    
1113
                        SET_FILTER_KEY("X-Sequence", H_X_SEQUENCE);
1114
                        p = *key;
1115
                        while (*p != '\0') {
1116
                                while (*p != '\0' && !isspace(*p)) p++;
1117
                                while (isspace(*p)) p++;
1118
                                if (isdigit(*p)) {
1119
                                        *p = '\0';
1120
                                        break;
1121
                                }
1122
                        }
1123
                        g_strstrip(*key);
1124
                } else if (msginfo->subject) {
1125
                        *header = g_strdup("Subject");
1126
                        *key = g_strdup(msginfo->subject);
1127
                }
1128

    
1129
#undef SET_FILTER_KEY
1130

    
1131
                g_free(hentry[H_LIST_ID].body);
1132
                hentry[H_LIST_ID].body = NULL;
1133
                g_free(hentry[H_X_ML_NAME].body);
1134
                hentry[H_X_ML_NAME].body = NULL;
1135
                g_free(hentry[H_X_LIST].body);
1136
                hentry[H_X_LIST].body = NULL;
1137
                g_free(hentry[H_X_MAILING_LIST].body);
1138
                hentry[H_X_MAILING_LIST].body = NULL;
1139

    
1140
                break;
1141
        case FILTER_BY_FROM:
1142
                *header = g_strdup("From");
1143
                *key = g_strdup(msginfo->from);
1144
                break;
1145
        case FILTER_BY_TO:
1146
                *header = g_strdup("To");
1147
                *key = g_strdup(msginfo->to);
1148
                break;
1149
        case FILTER_BY_SUBJECT:
1150
                *header = g_strdup("Subject");
1151
                *key = g_strdup(msginfo->subject);
1152
                break;
1153
        default:
1154
                break;
1155
        }
1156
}
1157

    
1158
void procmsg_empty_trash(FolderItem *trash)
1159
{
1160
        FILE *fp;
1161

    
1162
        if (trash && trash->total > 0) {
1163
                debug_print("Emptying messages in %s ...\n", trash->path);
1164

    
1165
                folder_item_remove_all_msg(trash);
1166
                fp = procmsg_open_cache_file(trash, DATA_WRITE);
1167
                if (fp) fclose(fp);
1168
                fp = procmsg_open_mark_file(trash, DATA_WRITE);
1169
                if (fp) fclose(fp);
1170
        }
1171
}
1172

    
1173
void procmsg_empty_all_trash(void)
1174
{
1175
        FolderItem *trash;
1176
        GList *cur;
1177

    
1178
        for (cur = folder_get_list(); cur != NULL; cur = cur->next) {
1179
                trash = FOLDER(cur->data)->trash;
1180
                procmsg_empty_trash(trash);
1181
        }
1182
}
1183

    
1184
gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
1185
{
1186
        gint ret = 0;
1187
        GSList *mlist = NULL;
1188
        GSList *cur;
1189

    
1190
        if (!queue)
1191
                queue = folder_get_default_queue();
1192
        g_return_val_if_fail(queue != NULL, -1);
1193

    
1194
        mlist = folder_item_get_msg_list(queue, FALSE);
1195
        mlist = procmsg_sort_msg_list(mlist, SORT_BY_NUMBER, SORT_ASCENDING);
1196

    
1197
        for (cur = mlist; cur != NULL; cur = cur->next) {
1198
                gchar *file;
1199
                MsgInfo *msginfo = (MsgInfo *)cur->data;
1200

    
1201
                file = procmsg_get_message_file(msginfo);
1202
                if (file) {
1203
                        QueueInfo *qinfo;
1204

    
1205
                        qinfo = send_get_queue_info(file);
1206
                        if (!qinfo || send_message_queue(qinfo) < 0) {
1207
                                g_warning(_("Sending queued message %d failed.\n"),
1208
                                          msginfo->msgnum);
1209
                        } else {
1210
                                ret++;
1211
                                if (save_msgs) {
1212
                                        FolderItem *outbox;
1213
                                        outbox = account_get_special_folder
1214
                                                (qinfo->ac, F_OUTBOX);
1215
                                        procmsg_save_to_outbox(outbox, file,
1216
                                                               TRUE);
1217
                                }
1218
                                folder_item_remove_msg(queue, msginfo);
1219
                        }
1220
                        send_queue_info_free(qinfo);
1221
                        g_free(file);
1222
                }
1223

    
1224
                procmsg_msginfo_free(msginfo);
1225
        }
1226

    
1227
        g_slist_free(mlist);
1228

    
1229
        queue->mtime = 0;
1230

    
1231
        return ret;
1232
}
1233

    
1234
gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
1235
                            gboolean is_queued)
1236
{
1237
        gint num;
1238
        FILE *fp;
1239
        MsgFlags flag = {0, 0};
1240

    
1241
        debug_print("saving sent message...\n");
1242

    
1243
        if (!outbox)
1244
                outbox = folder_get_default_outbox();
1245
        g_return_val_if_fail(outbox != NULL, -1);
1246

    
1247
        /* remove queueing headers */
1248
        if (is_queued) {
1249
                gchar tmp[MAXPATHLEN + 1];
1250
                gchar buf[BUFFSIZE];
1251
                FILE *outfp;
1252

    
1253
                g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
1254
                           get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
1255
                if ((fp = fopen(file, "rb")) == NULL) {
1256
                        FILE_OP_ERROR(file, "fopen");
1257
                        return -1;
1258
                }
1259
                if ((outfp = fopen(tmp, "wb")) == NULL) {
1260
                        FILE_OP_ERROR(tmp, "fopen");
1261
                        fclose(fp);
1262
                        return -1;
1263
                }
1264
                while (fgets(buf, sizeof(buf), fp) != NULL)
1265
                        if (buf[0] == '\r' || buf[0] == '\n') break;
1266
                while (fgets(buf, sizeof(buf), fp) != NULL)
1267
                        fputs(buf, outfp);
1268
                fclose(outfp);
1269
                fclose(fp);
1270

    
1271
                folder_item_scan(outbox);
1272
                if ((num = folder_item_add_msg(outbox, tmp, &flag, TRUE)) < 0) {
1273
                        g_warning("can't save message\n");
1274
                        unlink(tmp);
1275
                        return -1;
1276
                }
1277
        } else {
1278
                folder_item_scan(outbox);
1279
                if ((num = folder_item_add_msg
1280
                        (outbox, file, &flag, FALSE)) < 0) {
1281
                        g_warning("can't save message\n");
1282
                        return -1;
1283
                }
1284
        }
1285

    
1286
        return 0;
1287
}
1288

    
1289
void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
1290
{
1291
        static const gchar *def_cmd = "lpr %s";
1292
        static guint id = 0;
1293
        gchar *prtmp;
1294
        FILE *tmpfp, *prfp;
1295
        gchar buf[1024];
1296
        gchar *p;
1297

    
1298
        g_return_if_fail(msginfo);
1299

    
1300
        if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
1301
                g_warning(_("Can't get text part\n"));
1302
                return;
1303
        }
1304

    
1305
        prtmp = g_strdup_printf("%s%cprinttmp.%08x",
1306
                                get_mime_tmp_dir(), G_DIR_SEPARATOR, id++);
1307

    
1308
        if ((prfp = fopen(prtmp, "wb")) == NULL) {
1309
                FILE_OP_ERROR(prtmp, "fopen");
1310
                g_free(prtmp);
1311
                fclose(tmpfp);
1312
                return;
1313
        }
1314

    
1315
        if (msginfo->date) fprintf(prfp, "Date: %s\n", msginfo->date);
1316
        if (msginfo->from) fprintf(prfp, "From: %s\n", msginfo->from);
1317
        if (msginfo->to)   fprintf(prfp, "To: %s\n", msginfo->to);
1318
        if (msginfo->newsgroups)
1319
                fprintf(prfp, "Newsgroups: %s\n", msginfo->newsgroups);
1320
        if (msginfo->subject) fprintf(prfp, "Subject: %s\n", msginfo->subject);
1321
        fputc('\n', prfp);
1322

    
1323
        while (fgets(buf, sizeof(buf), tmpfp) != NULL)
1324
                fputs(buf, prfp);
1325

    
1326
        fclose(prfp);
1327
        fclose(tmpfp);
1328

    
1329
        if (cmdline && (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
1330
            !strchr(p + 2, '%'))
1331
                g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
1332
        else {
1333
                if (cmdline)
1334
                        g_warning(_("Print command line is invalid: `%s'\n"),
1335
                                  cmdline);
1336
                g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
1337
        }
1338

    
1339
        g_free(prtmp);
1340

    
1341
        g_strchomp(buf);
1342
        if (buf[strlen(buf) - 1] != '&') strcat(buf, "&");
1343
        system(buf);
1344
}
1345

    
1346
MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
1347
{
1348
        MsgInfo *newmsginfo;
1349

    
1350
        if (msginfo == NULL) return NULL;
1351

    
1352
        newmsginfo = g_new0(MsgInfo, 1);
1353

    
1354
#define MEMBCOPY(mmb)        newmsginfo->mmb = msginfo->mmb
1355
#define MEMBDUP(mmb)        newmsginfo->mmb = msginfo->mmb ? \
1356
                        g_strdup(msginfo->mmb) : NULL
1357

    
1358
        MEMBCOPY(msgnum);
1359
        MEMBCOPY(size);
1360
        MEMBCOPY(mtime);
1361
        MEMBCOPY(date_t);
1362

    
1363
        MEMBCOPY(flags);
1364

    
1365
        MEMBDUP(fromname);
1366

    
1367
        MEMBDUP(date);
1368
        MEMBDUP(from);
1369
        MEMBDUP(to);
1370
        MEMBDUP(cc);
1371
        MEMBDUP(newsgroups);
1372
        MEMBDUP(subject);
1373
        MEMBDUP(msgid);
1374
        MEMBDUP(inreplyto);
1375

    
1376
        MEMBCOPY(folder);
1377
        MEMBCOPY(to_folder);
1378

    
1379
        MEMBDUP(xface);
1380

    
1381
        MEMBDUP(file_path);
1382

    
1383
        MEMBDUP(plaintext_file);
1384
        MEMBCOPY(decryption_failed);
1385

    
1386
        return newmsginfo;
1387
}
1388

    
1389
MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
1390
{
1391
        MsgInfo *full_msginfo;
1392
        gchar *file;
1393

    
1394
        if (msginfo == NULL) return NULL;
1395

    
1396
        file = procmsg_get_message_file(msginfo);
1397
        if (!file) {
1398
                g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
1399
                return NULL;
1400
        }
1401

    
1402
        full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE);
1403
        g_free(file);
1404
        if (!full_msginfo) return NULL;
1405

    
1406
        full_msginfo->msgnum = msginfo->msgnum;
1407
        full_msginfo->size = msginfo->size;
1408
        full_msginfo->mtime = msginfo->mtime;
1409
        full_msginfo->folder = msginfo->folder;
1410
        full_msginfo->to_folder = msginfo->to_folder;
1411

    
1412
        full_msginfo->file_path = g_strdup(msginfo->file_path);
1413

    
1414
#if USE_GPGME
1415
        full_msginfo->plaintext_file = g_strdup(msginfo->plaintext_file);
1416
        full_msginfo->decryption_failed = msginfo->decryption_failed;
1417
#endif
1418

    
1419
        return full_msginfo;
1420
}
1421

    
1422
void procmsg_msginfo_free(MsgInfo *msginfo)
1423
{
1424
        if (msginfo == NULL) return;
1425

    
1426
        g_free(msginfo->xface);
1427

    
1428
        g_free(msginfo->fromname);
1429

    
1430
        g_free(msginfo->date);
1431
        g_free(msginfo->from);
1432
        g_free(msginfo->to);
1433
        g_free(msginfo->cc);
1434
        g_free(msginfo->newsgroups);
1435
        g_free(msginfo->subject);
1436
        g_free(msginfo->msgid);
1437
        g_free(msginfo->inreplyto);
1438

    
1439
        g_free(msginfo->file_path);
1440

    
1441
        g_free(msginfo->plaintext_file);
1442

    
1443
        g_free(msginfo);
1444
}
1445

    
1446
gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
1447
{
1448
        const MsgInfo *msginfo1 = a;
1449
        const MsgInfo *msginfo2 = b;
1450

    
1451
        if (!msginfo1)
1452
                return -1;
1453
        if (!msginfo2)
1454
                return -1;
1455

    
1456
        return msginfo1->msgnum - msginfo2->msgnum;
1457
}
1458

    
1459
#define CMP_FUNC_DEF(func_name, val)                                        \
1460
static gint func_name(gconstpointer a, gconstpointer b)                        \
1461
{                                                                        \
1462
        const MsgInfo *msginfo1 = a;                                        \
1463
        const MsgInfo *msginfo2 = b;                                        \
1464
                                                                        \
1465
        if (!msginfo1 || !msginfo2)                                        \
1466
                return -1;                                                \
1467
                                                                        \
1468
        return (val) * (cmp_func_sort_type == SORT_ASCENDING ? 1 : -1);        \
1469
}
1470

    
1471
CMP_FUNC_DEF(procmsg_cmp_by_mark,
1472
             MSG_IS_MARKED(msginfo1->flags) - MSG_IS_MARKED(msginfo2->flags))
1473
CMP_FUNC_DEF(procmsg_cmp_by_unread,
1474
             MSG_IS_UNREAD(msginfo1->flags) - MSG_IS_UNREAD(msginfo2->flags))
1475
CMP_FUNC_DEF(procmsg_cmp_by_mime,
1476
             MSG_IS_MIME(msginfo1->flags) - MSG_IS_MIME(msginfo2->flags))
1477
CMP_FUNC_DEF(procmsg_cmp_by_label,
1478
             MSG_GET_COLORLABEL(msginfo1->flags) -
1479
             MSG_GET_COLORLABEL(msginfo2->flags))
1480

    
1481
CMP_FUNC_DEF(procmsg_cmp_by_number, msginfo1->msgnum - msginfo2->msgnum)
1482
CMP_FUNC_DEF(procmsg_cmp_by_size, msginfo1->size - msginfo2->size)
1483
CMP_FUNC_DEF(procmsg_cmp_by_date, msginfo1->date_t - msginfo2->date_t)
1484

    
1485
#undef CMP_FUNC_DEF
1486
#define CMP_FUNC_DEF(func_name, var_name)                                \
1487
static gint func_name(gconstpointer a, gconstpointer b)                        \
1488
{                                                                        \
1489
        const MsgInfo *msginfo1 = a;                                        \
1490
        const MsgInfo *msginfo2 = b;                                        \
1491
                                                                        \
1492
        if (!msginfo1->var_name)                                        \
1493
                return (msginfo2->var_name != NULL);                        \
1494
        if (!msginfo2->var_name)                                        \
1495
                return -1;                                                \
1496
                                                                        \
1497
        return strcasecmp(msginfo1->var_name, msginfo2->var_name) *        \
1498
                (cmp_func_sort_type == SORT_ASCENDING ? 1 : -1);        \
1499
}
1500

    
1501
CMP_FUNC_DEF(procmsg_cmp_by_from, fromname)
1502
CMP_FUNC_DEF(procmsg_cmp_by_to, to)
1503

    
1504
#undef CMP_FUNC_DEF
1505

    
1506
static gint procmsg_cmp_by_subject(gconstpointer a, gconstpointer b)        \
1507
{                                                                        \
1508
        const MsgInfo *msginfo1 = a;                                        \
1509
        const MsgInfo *msginfo2 = b;                                        \
1510
                                                                        \
1511
        if (!msginfo1->subject)                                                \
1512
                return (msginfo2->subject != NULL);                        \
1513
        if (!msginfo2->subject)                                                \
1514
                return -1;                                                \
1515
                                                                        \
1516
        return subject_compare_for_sort                                        \
1517
                (msginfo1->subject, msginfo2->subject) *                \
1518
                (cmp_func_sort_type == SORT_ASCENDING ? 1 : -1);        \
1519
}