Statistics
| Revision:

root / src / mbox.c @ 1

History | View | Annotate | Download (9.82 KB)

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
#ifdef HAVE_CONFIG_H
21
#  include "config.h"
22
#endif
23

    
24
#include "defs.h"
25

    
26
#include <glib.h>
27
#include <stdio.h>
28
#include <unistd.h>
29
#include <string.h>
30
#include <fcntl.h>
31
#include <sys/file.h>
32
#include <ctype.h>
33
#include <time.h>
34

    
35
#include "intl.h"
36
#include "mbox.h"
37
#include "procmsg.h"
38
#include "folder.h"
39
#include "filter.h"
40
#include "prefs_common.h"
41
#include "prefs_account.h"
42
#include "account.h"
43
#include "utils.h"
44

    
45
#define MSGBUFSIZE        8192
46

    
47
#define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
48
{ \
49
        if (fputs(s, tmp_fp) == EOF) { \
50
                g_warning(_("can't write to temporary file\n")); \
51
                fclose(tmp_fp); \
52
                fclose(mbox_fp); \
53
                unlink(tmp_file); \
54
                g_free(tmp_file); \
55
                return -1; \
56
        } \
57
}
58

    
59
gint proc_mbox(FolderItem *dest, const gchar *mbox, GHashTable *folder_table)
60
{
61
        FILE *mbox_fp;
62
        gchar buf[MSGBUFSIZE], from_line[MSGBUFSIZE];
63
        gchar *tmp_file;
64
        gint msgs = 0;
65

    
66
        g_return_val_if_fail(dest != NULL, -1);
67
        g_return_val_if_fail(mbox != NULL, -1);
68

    
69
        debug_print(_("Getting messages from %s into %s...\n"), mbox, dest->path);
70

    
71
        if ((mbox_fp = fopen(mbox, "rb")) == NULL) {
72
                FILE_OP_ERROR(mbox, "fopen");
73
                return -1;
74
        }
75

    
76
        /* ignore empty lines on the head */
77
        do {
78
                if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
79
                        g_warning(_("can't read mbox file.\n"));
80
                        fclose(mbox_fp);
81
                        return -1;
82
                }
83
        } while (buf[0] == '\n' || buf[0] == '\r');
84

    
85
        if (strncmp(buf, "From ", 5) != 0) {
86
                g_warning(_("invalid mbox format: %s\n"), mbox);
87
                fclose(mbox_fp);
88
                return -1;
89
        }
90

    
91
        strcpy(from_line, buf);
92
        if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
93
                g_warning(_("malformed mbox: %s\n"), mbox);
94
                fclose(mbox_fp);
95
                return -1;
96
        }
97

    
98
        tmp_file = get_tmp_file();
99

    
100
        do {
101
                FILE *tmp_fp;
102
                GSList *cur;
103
                gchar *startp, *endp, *rpath;
104
                gint empty_line;
105
                gboolean is_next_msg = FALSE;
106
                FilterInfo *fltinfo;
107

    
108
                if ((tmp_fp = fopen(tmp_file, "wb")) == NULL) {
109
                        FILE_OP_ERROR(tmp_file, "fopen");
110
                        g_warning(_("can't open temporary file\n"));
111
                        g_free(tmp_file);
112
                        fclose(mbox_fp);
113
                        return -1;
114
                }
115
                if (change_file_mode_rw(tmp_fp, tmp_file) < 0)
116
                        FILE_OP_ERROR(tmp_file, "chmod");
117

    
118
                /* convert unix From into Return-Path */
119
                startp = from_line + 5;
120
                endp = strchr(startp, ' ');
121
                if (endp == NULL)
122
                        rpath = g_strdup(startp);
123
                else
124
                        rpath = g_strndup(startp, endp - startp);
125
                g_strstrip(rpath);
126
                g_snprintf(from_line, sizeof(from_line),
127
                           "Return-Path: %s\n", rpath);
128
                g_free(rpath);
129

    
130
                FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
131
                FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
132
                from_line[0] = '\0';
133

    
134
                empty_line = 0;
135

    
136
                while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
137
                        if (buf[0] == '\n' || buf[0] == '\r') {
138
                                empty_line++;
139
                                buf[0] = '\0';
140
                                continue;
141
                        }
142

    
143
                        /* From separator */
144
                        while (!strncmp(buf, "From ", 5)) {
145
                                strcpy(from_line, buf);
146
                                if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
147
                                        buf[0] = '\0';
148
                                        break;
149
                                }
150

    
151
                                if (is_header_line(buf)) {
152
                                        is_next_msg = TRUE;
153
                                        break;
154
                                } else if (!strncmp(buf, "From ", 5)) {
155
                                        continue;
156
                                } else if (!strncmp(buf, ">From ", 6)) {
157
                                        g_memmove(buf, buf + 1, strlen(buf));
158
                                        is_next_msg = TRUE;
159
                                        break;
160
                                } else {
161
                                        g_warning(_("unescaped From found:\n%s"),
162
                                                  from_line);
163
                                        break;
164
                                }
165
                        }
166
                        if (is_next_msg) break;
167

    
168
                        if (empty_line > 0) {
169
                                while (empty_line--)
170
                                        FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
171
                                empty_line = 0;
172
                        }
173

    
174
                        if (from_line[0] != '\0') {
175
                                FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
176
                                from_line[0] = '\0';
177
                        }
178

    
179
                        if (buf[0] != '\0') {
180
                                if (!strncmp(buf, ">From ", 6)) {
181
                                        FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
182
                                } else
183
                                        FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
184

    
185
                                buf[0] = '\0';
186
                        }
187
                }
188

    
189
                if (empty_line > 0) {
190
                        while (--empty_line)
191
                                FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
192
                }
193

    
194
                if (fclose(tmp_fp) == EOF) {
195
                        FILE_OP_ERROR(tmp_file, "fclose");
196
                        g_warning(_("can't write to temporary file\n"));
197
                        unlink(tmp_file);
198
                        g_free(tmp_file);
199
                        fclose(mbox_fp);
200
                        return -1;
201
                }
202

    
203
                fltinfo = filter_info_new();
204
                fltinfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
205
                fltinfo->flags.tmp_flags = MSG_RECEIVED;
206

    
207
                if (folder_table)
208
                        filter_apply(prefs_common.fltlist, tmp_file, fltinfo);
209

    
210
                if (fltinfo->actions[FLT_ACTION_MOVE] == FALSE &&
211
                    fltinfo->actions[FLT_ACTION_DELETE] == FALSE) {
212
                        if (folder_item_add_msg(dest, tmp_file, &fltinfo->flags,
213
                                                FALSE) < 0) {
214
                                filter_info_free(fltinfo);
215
                                unlink(tmp_file);
216
                                g_free(tmp_file);
217
                                fclose(mbox_fp);
218
                                return -1;
219
                        }
220
                        fltinfo->dest_list = g_slist_append(fltinfo->dest_list,
221
                                                            dest);
222
                }
223

    
224
                for (cur = fltinfo->dest_list; cur != NULL; cur = cur->next) {
225
                        FolderItem *drop_folder = (FolderItem *)cur->data;
226
                        gint val = 0;
227

    
228
                        if (folder_table) {
229
                                val = GPOINTER_TO_INT(g_hash_table_lookup
230
                                                      (folder_table,
231
                                                       drop_folder));
232
                        }
233
                        if (val == 0) {
234
                                /* force updating */
235
                                if (FOLDER_IS_LOCAL(drop_folder->folder))
236
                                        drop_folder->mtime = 0;
237
                                if (folder_table) {
238
                                        g_hash_table_insert(folder_table,
239
                                                            drop_folder,
240
                                                            GINT_TO_POINTER(1));
241
                                }
242
                        }
243
                }
244

    
245
                filter_info_free(fltinfo);
246
                unlink(tmp_file);
247

    
248
                msgs++;
249
        } while (from_line[0] != '\0');
250

    
251
        g_free(tmp_file);
252
        fclose(mbox_fp);
253
        debug_print(_("%d messages found.\n"), msgs);
254

    
255
        return msgs;
256
}
257

    
258
gint lock_mbox(const gchar *base, LockType type)
259
{
260
        gint retval = 0;
261

    
262
        if (type == LOCK_FILE) {
263
                gchar *lockfile, *locklink;
264
                gint retry = 0;
265
                FILE *lockfp;
266

    
267
                lockfile = g_strdup_printf("%s.%d", base, getpid());
268
                if ((lockfp = fopen(lockfile, "wb")) == NULL) {
269
                        FILE_OP_ERROR(lockfile, "fopen");
270
                        g_warning(_("can't create lock file %s\n"), lockfile);
271
                        g_warning(_("use 'flock' instead of 'file' if possible.\n"));
272
                        g_free(lockfile);
273
                        return -1;
274
                }
275

    
276
                fprintf(lockfp, "%d\n", getpid());
277
                fclose(lockfp);
278

    
279
                locklink = g_strconcat(base, ".lock", NULL);
280
                while (link(lockfile, locklink) < 0) {
281
                        FILE_OP_ERROR(lockfile, "link");
282
                        if (retry >= 5) {
283
                                g_warning(_("can't create %s\n"), lockfile);
284
                                unlink(lockfile);
285
                                g_free(lockfile);
286
                                return -1;
287
                        }
288
                        if (retry == 0)
289
                                g_warning(_("mailbox is owned by another"
290
                                            " process, waiting...\n"));
291
                        retry++;
292
                        sleep(5);
293
                }
294
                unlink(lockfile);
295
                g_free(lockfile);
296
        } else if (type == LOCK_FLOCK) {
297
                gint lockfd;
298

    
299
#if HAVE_FLOCK
300
                if ((lockfd = open(base, O_RDONLY)) < 0) {
301
#else
302
                if ((lockfd = open(base, O_RDWR)) < 0) {
303
#endif
304
                        FILE_OP_ERROR(base, "open");
305
                        return -1;
306
                }
307
#if HAVE_FLOCK
308
                if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
309
                        perror("flock");
310
#else
311
#if HAVE_LOCKF
312
                if (lockf(lockfd, F_TLOCK, 0) < 0) {
313
                        perror("lockf");
314
#else
315
                {
316
#endif
317
#endif /* HAVE_FLOCK */
318
                        g_warning(_("can't lock %s\n"), base);
319
                        if (close(lockfd) < 0)
320
                                perror("close");
321
                        return -1;
322
                }
323
                retval = lockfd;
324
        } else {
325
                g_warning(_("invalid lock type\n"));
326
                return -1;
327
        }
328

    
329
        return retval;
330
}
331

    
332
gint unlock_mbox(const gchar *base, gint fd, LockType type)
333
{
334
        if (type == LOCK_FILE) {
335
                gchar *lockfile;
336

    
337
                lockfile = g_strconcat(base, ".lock", NULL);
338
                if (unlink(lockfile) < 0) {
339
                        FILE_OP_ERROR(lockfile, "unlink");
340
                        g_free(lockfile);
341
                        return -1;
342
                }
343
                g_free(lockfile);
344

    
345
                return 0;
346
        } else if (type == LOCK_FLOCK) {
347
#if HAVE_FLOCK
348
                if (flock(fd, LOCK_UN) < 0) {
349
                        perror("flock");
350
#else
351
#if HAVE_LOCKF
352
                if (lockf(fd, F_ULOCK, 0) < 0) {
353
                        perror("lockf");
354
#else
355
                {
356
#endif
357
#endif /* HAVE_FLOCK */
358
                        g_warning(_("can't unlock %s\n"), base);
359
                        if (close(fd) < 0)
360
                                perror("close");
361
                        return -1;
362
                }
363

    
364
                if (close(fd) < 0) {
365
                        perror("close");
366
                        return -1;
367
                }
368

    
369
                return 0;
370
        }
371

    
372
        g_warning(_("invalid lock type\n"));
373
        return -1;
374
}
375

    
376
gint copy_mbox(const gchar *src, const gchar *dest)
377
{
378
        return copy_file(src, dest, TRUE);
379
}
380

    
381
void empty_mbox(const gchar *mbox)
382
{
383
        if (truncate(mbox, 0) < 0) {
384
                FILE *fp;
385

    
386
                FILE_OP_ERROR(mbox, "truncate");
387
                if ((fp = fopen(mbox, "wb")) == NULL) {
388
                        FILE_OP_ERROR(mbox, "fopen");
389
                        g_warning(_("can't truncate mailbox to zero.\n"));
390
                        return;
391
                }
392
                fclose(fp);
393
        }
394
}
395

    
396
/* read all messages in SRC, and store them into one MBOX file. */
397
gint export_to_mbox(FolderItem *src, const gchar *mbox)
398
{
399
        GSList *mlist;
400
        GSList *cur;
401
        MsgInfo *msginfo;
402
        FILE *msg_fp;
403
        FILE *mbox_fp;
404
        gchar buf[BUFFSIZE];
405

    
406
        g_return_val_if_fail(src != NULL, -1);
407
        g_return_val_if_fail(src->folder != NULL, -1);
408
        g_return_val_if_fail(mbox != NULL, -1);
409

    
410
        debug_print(_("Exporting messages from %s into %s...\n"),
411
                    src->path, mbox);
412

    
413
        if ((mbox_fp = fopen(mbox, "wb")) == NULL) {
414
                FILE_OP_ERROR(mbox, "fopen");
415
                return -1;
416
        }
417

    
418
        mlist = folder_item_get_msg_list(src, TRUE);
419

    
420
        for (cur = mlist; cur != NULL; cur = cur->next) {
421
                msginfo = (MsgInfo *)cur->data;
422

    
423
                msg_fp = procmsg_open_message(msginfo);
424
                if (!msg_fp) {
425
                        procmsg_msginfo_free(msginfo);
426
                        continue;
427
                }
428

    
429
                strncpy2(buf,
430
                         msginfo->from ? msginfo->from :
431
                         cur_account && cur_account->address ?
432
                         cur_account->address : "unknown",
433
                         sizeof(buf));
434
                extract_address(buf);
435

    
436
                fprintf(mbox_fp, "From %s %s",
437
                        buf, ctime(&msginfo->date_t));
438

    
439
                while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
440
                        if (!strncmp(buf, "From ", 5))
441
                                fputc('>', mbox_fp);
442
                        fputs(buf, mbox_fp);
443
                }
444
                fputc('\n', mbox_fp);
445

    
446
                fclose(msg_fp);
447
                procmsg_msginfo_free(msginfo);
448
        }
449

    
450
        g_slist_free(mlist);
451

    
452
        fclose(mbox_fp);
453

    
454
        return 0;
455
}