Statistics
| Revision:

root / libsylph / mbox.c @ 3141

History | View | Annotate | Download (12.4 kB)

1
/*
2
 * LibSylph -- E-Mail client library
3
 * Copyright (C) 1999-2010 Hiroyuki Yamamoto
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library 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 GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
#ifdef HAVE_CONFIG_H
21
#  include "config.h"
22
#endif
23
24
#include "defs.h"
25
26
#include <glib.h>
27
#include <glib/gi18n.h>
28
#include <stdio.h>
29
#include <unistd.h>
30
#include <string.h>
31
#include <fcntl.h>
32
#include <sys/file.h>
33
#include <ctype.h>
34
#include <time.h>
35
36
#include "mbox.h"
37
#include "procmsg.h"
38
#include "procheader.h"
39
#include "folder.h"
40
#include "filter.h"
41
#include "prefs_common.h"
42
#include "prefs_account.h"
43
#include "account.h"
44
#include "utils.h"
45
46
#define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
47
{ \
48
        if (fputs(s, tmp_fp) == EOF) { \
49
                g_warning(_("can't write to temporary file\n")); \
50
                fclose(tmp_fp); \
51
                fclose(mbox_fp); \
52
                g_unlink(tmp_file); \
53
                g_free(tmp_file); \
54
                return -1; \
55
        } \
56
}
57
58
gint proc_mbox(FolderItem *dest, const gchar *mbox, GHashTable *folder_table)
59
{
60
        return proc_mbox_full(dest, mbox, folder_table,
61
                              folder_table ? TRUE : FALSE,
62
                              folder_table && prefs_common.enable_junk &&
63
                              prefs_common.filter_junk_on_recv ? TRUE : FALSE);
64
}
65
66
gint proc_mbox_full(FolderItem *dest, const gchar *mbox,
67
                    GHashTable *folder_table, gboolean apply_filter,
68
                    gboolean filter_junk)
69
{
70
        FILE *mbox_fp;
71
        gchar buf[BUFFSIZE], from_line[BUFFSIZE];
72
        gchar *tmp_file;
73
        gint new_msgs = 0;
74
        gint count = 0;
75
        Folder *folder;
76
        FilterRule *junk_rule = NULL;
77
        GSList junk_fltlist = {NULL, NULL};
78
        FolderItem *junk;
79
80
        g_return_val_if_fail(dest != NULL, -1);
81
        g_return_val_if_fail(dest->folder != NULL, -1);
82
        g_return_val_if_fail(mbox != NULL, -1);
83
84
        debug_print(_("Getting messages from %s into %s...\n"), mbox, dest->path);
85
86
        folder = dest->folder;
87
88
        if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
89
                FILE_OP_ERROR(mbox, "fopen");
90
                return -1;
91
        }
92
93
        /* ignore empty lines on the head */
94
        do {
95
                if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
96
                        g_warning(_("can't read mbox file.\n"));
97
                        fclose(mbox_fp);
98
                        return -1;
99
                }
100
        } while (buf[0] == '\n' || buf[0] == '\r');
101
102
        if (strncmp(buf, "From ", 5) != 0) {
103
                g_warning(_("invalid mbox format: %s\n"), mbox);
104
                fclose(mbox_fp);
105
                return -1;
106
        }
107
108
        strcpy(from_line, buf);
109
        if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
110
                g_warning(_("malformed mbox: %s\n"), mbox);
111
                fclose(mbox_fp);
112
                return -1;
113
        }
114
115
        tmp_file = get_tmp_file();
116
117
        if (filter_junk) {
118
                junk = folder_get_junk(folder);
119
                junk_rule = filter_junk_rule_create(NULL, junk, FALSE);
120
                junk_fltlist.data = junk_rule;
121
        }
122
123
        do {
124
                FILE *tmp_fp;
125
                GSList *cur;
126
                gchar *startp, *endp, *rpath;
127
                gint empty_line;
128
                gboolean is_next_msg = FALSE;
129
                gboolean is_junk = FALSE;
130
                FilterInfo *fltinfo;
131
                MsgInfo *msginfo;
132
133
                count++;
134
                if (folder->ui_func)
135
                        folder->ui_func(folder, dest, folder->ui_func_data ? dest->folder->ui_func_data : GINT_TO_POINTER(count));
136
137
                if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
138
                        FILE_OP_ERROR(tmp_file, "fopen");
139
                        g_warning(_("can't open temporary file\n"));
140
                        filter_rule_free(junk_rule);
141
                        g_free(tmp_file);
142
                        fclose(mbox_fp);
143
                        return -1;
144
                }
145
                if (change_file_mode_rw(tmp_fp, tmp_file) < 0)
146
                        FILE_OP_ERROR(tmp_file, "chmod");
147
148
                /* convert unix From into Return-Path */
149
                startp = from_line + 5;
150
                endp = strchr(startp, ' ');
151
                if (endp == NULL)
152
                        rpath = g_strdup(startp);
153
                else
154
                        rpath = g_strndup(startp, endp - startp);
155
                g_strstrip(rpath);
156
                g_snprintf(from_line, sizeof(from_line),
157
                           "Return-Path: %s\n", rpath);
158
                g_free(rpath);
159
160
                FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
161
                FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
162
                from_line[0] = '\0';
163
164
                empty_line = 0;
165
166
                while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
167
                        if (buf[0] == '\n' || buf[0] == '\r') {
168
                                empty_line++;
169
                                buf[0] = '\0';
170
                                continue;
171
                        }
172
173
                        /* From separator */
174
                        while (!strncmp(buf, "From ", 5)) {
175
                                strcpy(from_line, buf);
176
                                if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
177
                                        buf[0] = '\0';
178
                                        break;
179
                                }
180
181
                                if (is_header_line(buf)) {
182
                                        is_next_msg = TRUE;
183
                                        break;
184
                                } else if (!strncmp(buf, "From ", 5)) {
185
                                        continue;
186
                                } else if (!strncmp(buf, ">From ", 6)) {
187
                                        g_memmove(buf, buf + 1, strlen(buf));
188
                                        is_next_msg = TRUE;
189
                                        break;
190
                                } else {
191
                                        g_warning(_("unescaped From found:\n%s"),
192
                                                  from_line);
193
                                        break;
194
                                }
195
                        }
196
                        if (is_next_msg) break;
197
198
                        if (empty_line > 0) {
199
                                while (empty_line--)
200
                                        FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
201
                                empty_line = 0;
202
                        }
203
204
                        if (from_line[0] != '\0') {
205
                                FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
206
                                from_line[0] = '\0';
207
                        }
208
209
                        if (buf[0] != '\0') {
210
                                if (!strncmp(buf, ">From ", 6)) {
211
                                        FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
212
                                } else
213
                                        FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
214
215
                                buf[0] = '\0';
216
                        }
217
                }
218
219
                if (empty_line > 0) {
220
                        while (--empty_line)
221
                                FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
222
                }
223
224
                if (fclose(tmp_fp) == EOF) {
225
                        FILE_OP_ERROR(tmp_file, "fclose");
226
                        g_warning(_("can't write to temporary file\n"));
227
                        filter_rule_free(junk_rule);
228
                        g_unlink(tmp_file);
229
                        g_free(tmp_file);
230
                        fclose(mbox_fp);
231
                        return -1;
232
                }
233
234
                fltinfo = filter_info_new();
235
                fltinfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
236
                fltinfo->flags.tmp_flags = MSG_RECEIVED;
237
238
                msginfo = procheader_parse_file(tmp_file, fltinfo->flags,
239
                                                FALSE);
240
                if (!msginfo) {
241
                        g_warning("proc_mbox_full: procheader_parse_file failed");
242
                        filter_info_free(fltinfo);
243
                        filter_rule_free(junk_rule);
244
                        g_unlink(tmp_file);
245
                        g_free(tmp_file);
246
                        fclose(mbox_fp);
247
                        return -1;
248
                }
249
                fltinfo->flags = msginfo->flags;
250
                msginfo->file_path = g_strdup(tmp_file);
251
252
                if (filter_junk && prefs_common.enable_junk &&
253
                    prefs_common.filter_junk_before && junk_rule) {
254
                        filter_apply_msginfo(&junk_fltlist, msginfo, fltinfo);
255
                        if (fltinfo->drop_done)
256
                                is_junk = TRUE;
257
                }
258
259
                if (!fltinfo->drop_done && apply_filter)
260
                        filter_apply_msginfo(prefs_common.fltlist, msginfo,
261
                                             fltinfo);
262
263
                if (!fltinfo->drop_done &&
264
                    filter_junk && prefs_common.enable_junk &&
265
                    !prefs_common.filter_junk_before && junk_rule) {
266
                        filter_apply_msginfo(&junk_fltlist, msginfo, fltinfo);
267
                        if (fltinfo->drop_done)
268
                                is_junk = TRUE;
269
                }
270
271
                if (fltinfo->actions[FLT_ACTION_MOVE] == FALSE &&
272
                    fltinfo->actions[FLT_ACTION_DELETE] == FALSE) {
273
                        msginfo->flags = fltinfo->flags;
274
                        if (folder_item_add_msg_msginfo(dest, msginfo, FALSE) < 0) {
275
                                procmsg_msginfo_free(msginfo);
276
                                filter_info_free(fltinfo);
277
                                filter_rule_free(junk_rule);
278
                                g_unlink(tmp_file);
279
                                g_free(tmp_file);
280
                                fclose(mbox_fp);
281
                                return -1;
282
                        }
283
                        fltinfo->dest_list = g_slist_append(fltinfo->dest_list,
284
                                                            dest);
285
                }
286
287
                for (cur = fltinfo->dest_list; cur != NULL; cur = cur->next) {
288
                        FolderItem *drop_folder = (FolderItem *)cur->data;
289
                        gint val = 0;
290
291
                        if (folder_table) {
292
                                val = GPOINTER_TO_INT(g_hash_table_lookup
293
                                                      (folder_table,
294
                                                       drop_folder));
295
                        }
296
                        if (val == 0) {
297
                                if (folder_table) {
298
                                        g_hash_table_insert(folder_table,
299
                                                            drop_folder,
300
                                                            GINT_TO_POINTER(1));
301
                                }
302
                        }
303
                }
304
305
                if (!is_junk &&
306
                    fltinfo->actions[FLT_ACTION_DELETE] == FALSE &&
307
                    fltinfo->actions[FLT_ACTION_MARK_READ] == FALSE)
308
                        new_msgs++;
309
310
                procmsg_msginfo_free(msginfo);
311
                filter_info_free(fltinfo);
312
                g_unlink(tmp_file);
313
        } while (from_line[0] != '\0');
314
315
        if (junk_rule)
316
                filter_rule_free(junk_rule);
317
318
        g_free(tmp_file);
319
        fclose(mbox_fp);
320
        debug_print("%d new messages found.\n", new_msgs);
321
322
        return new_msgs;
323
}
324
325
gint lock_mbox(const gchar *base, LockType type)
326
{
327
#ifdef G_OS_UNIX
328
        gint retval = 0;
329
330
        if (type == LOCK_FILE) {
331
                gchar *lockfile, *locklink;
332
                gint retry = 0;
333
                FILE *lockfp;
334
335
                lockfile = g_strdup_printf("%s.%d", base, getpid());
336
                if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
337
                        FILE_OP_ERROR(lockfile, "fopen");
338
                        g_warning(_("can't create lock file %s\n"), lockfile);
339
                        g_warning(_("use 'flock' instead of 'file' if possible.\n"));
340
                        g_free(lockfile);
341
                        return -1;
342
                }
343
344
                fprintf(lockfp, "%d\n", getpid());
345
                fclose(lockfp);
346
347
                locklink = g_strconcat(base, ".lock", NULL);
348
                while (link(lockfile, locklink) < 0) {
349
                        FILE_OP_ERROR(lockfile, "link");
350
                        if (retry >= 5) {
351
                                g_warning(_("can't create %s\n"), lockfile);
352
                                g_unlink(lockfile);
353
                                g_free(lockfile);
354
                                return -1;
355
                        }
356
                        if (retry == 0)
357
                                g_warning(_("mailbox is owned by another"
358
                                            " process, waiting...\n"));
359
                        retry++;
360
                        sleep(5);
361
                }
362
                g_unlink(lockfile);
363
                g_free(lockfile);
364
        } else if (type == LOCK_FLOCK) {
365
                gint lockfd;
366
367
#if HAVE_FLOCK
368
                if ((lockfd = open(base, O_RDONLY)) < 0) {
369
#else
370
                if ((lockfd = open(base, O_RDWR)) < 0) {
371
#endif
372
                        FILE_OP_ERROR(base, "open");
373
                        return -1;
374
                }
375
#if HAVE_FLOCK
376
                if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
377
                        perror("flock");
378
#else
379
#if HAVE_LOCKF
380
                if (lockf(lockfd, F_TLOCK, 0) < 0) {
381
                        perror("lockf");
382
#else
383
                {
384
#endif
385
#endif /* HAVE_FLOCK */
386
                        g_warning(_("can't lock %s\n"), base);
387
                        if (close(lockfd) < 0)
388
                                perror("close");
389
                        return -1;
390
                }
391
                retval = lockfd;
392
        } else {
393
                g_warning(_("invalid lock type\n"));
394
                return -1;
395
        }
396
397
        return retval;
398
#else
399
        return -1;
400
#endif /* G_OS_UNIX */
401
}
402
403
gint unlock_mbox(const gchar *base, gint fd, LockType type)
404
{
405
        if (type == LOCK_FILE) {
406
                gchar *lockfile;
407
408
                lockfile = g_strconcat(base, ".lock", NULL);
409
                if (g_unlink(lockfile) < 0) {
410
                        FILE_OP_ERROR(lockfile, "unlink");
411
                        g_free(lockfile);
412
                        return -1;
413
                }
414
                g_free(lockfile);
415
416
                return 0;
417
        } else if (type == LOCK_FLOCK) {
418
#if HAVE_FLOCK
419
                if (flock(fd, LOCK_UN) < 0) {
420
                        perror("flock");
421
#else
422
#if HAVE_LOCKF
423
                if (lockf(fd, F_ULOCK, 0) < 0) {
424
                        perror("lockf");
425
#else
426
                {
427
#endif
428
#endif /* HAVE_FLOCK */
429
                        g_warning(_("can't unlock %s\n"), base);
430
                        if (close(fd) < 0)
431
                                perror("close");
432
                        return -1;
433
                }
434
435
                if (close(fd) < 0) {
436
                        perror("close");
437
                        return -1;
438
                }
439
440
                return 0;
441
        }
442
443
        g_warning(_("invalid lock type\n"));
444
        return -1;
445
}
446
447
gint copy_mbox(const gchar *src, const gchar *dest)
448
{
449
        return copy_file(src, dest, TRUE);
450
}
451
452
void empty_mbox(const gchar *mbox)
453
{
454
#if HAVE_TRUNCATE
455
        if (truncate(mbox, 0) < 0) {
456
#endif
457
                FILE *fp;
458
459
#if HAVE_TRUNCATE
460
                FILE_OP_ERROR(mbox, "truncate");
461
#endif
462
                if ((fp = g_fopen(mbox, "wb")) == NULL) {
463
                        FILE_OP_ERROR(mbox, "fopen");
464
                        g_warning(_("can't truncate mailbox to zero.\n"));
465
                        return;
466
                }
467
                fclose(fp);
468
#if HAVE_TRUNCATE
469
        }
470
#endif
471
}
472
473
/* read all messages in SRC, and store them into one MBOX file. */
474
gint export_to_mbox(FolderItem *src, const gchar *mbox)
475
{
476
        GSList *mlist;
477
        gint ret = 0;
478
479
        mlist = folder_item_get_msg_list(src, TRUE);
480
        if (mlist) {
481
                ret = export_msgs_to_mbox(src, mlist, mbox);
482
                procmsg_msg_list_free(mlist);
483
        }
484
485
        return ret;
486
}
487
488
/* store MLIST into one MBOX file. */
489
gint export_msgs_to_mbox(FolderItem *src, GSList *mlist, const gchar *mbox)
490
{
491
        GSList *cur;
492
        MsgInfo *msginfo;
493
        FILE *msg_fp;
494
        FILE *mbox_fp;
495
        gchar buf[BUFFSIZE];
496
        PrefsAccount *cur_ac;
497
        gint count = 0, length;
498
499
        g_return_val_if_fail(src != NULL, -1);
500
        g_return_val_if_fail(src->folder != NULL, -1);
501
        g_return_val_if_fail(mlist != NULL, -1);
502
        g_return_val_if_fail(mbox != NULL, -1);
503
504
        debug_print(_("Exporting messages from %s into %s...\n"),
505
                    src->path, mbox);
506
507
        if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
508
                FILE_OP_ERROR(mbox, "fopen");
509
                return -1;
510
        }
511
512
        cur_ac = account_get_current_account();
513
514
        length = g_slist_length(mlist);
515
516
        for (cur = mlist; cur != NULL; cur = cur->next) {
517
                msginfo = (MsgInfo *)cur->data;
518
519
                count++;
520
                if (src->folder->ui_func)
521
                        src->folder->ui_func(src->folder, src, src->folder->ui_func_data ? src->folder->ui_func_data : GINT_TO_POINTER(count));
522
523
                msg_fp = procmsg_open_message(msginfo);
524
                if (!msg_fp)
525
                        continue;
526
527
                strncpy2(buf,
528
                         msginfo->from ? msginfo->from :
529
                         cur_ac && cur_ac->address ?
530
                         cur_ac->address : "unknown",
531
                         sizeof(buf));
532
                extract_address(buf);
533
534
                fprintf(mbox_fp, "From %s %s",
535
                        buf, ctime(&msginfo->date_t));
536
537
                while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
538
                        if (!strncmp(buf, "From ", 5))
539
                                fputc('>', mbox_fp);
540
                        fputs(buf, mbox_fp);
541
                }
542
                fputc('\n', mbox_fp);
543
544
                fclose(msg_fp);
545
        }
546
547
        fclose(mbox_fp);
548
549
        return 0;
550
}