051ee5e6684697b6f3aa1b5dc81cd952755573fe
[claws.git] / src / mbox.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #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                 return -1; \
55         } \
56 }
57
58 gint proc_mbox(FolderItem *dest, const gchar *mbox, GHashTable *folder_table)
59 {
60         FILE *mbox_fp;
61         gchar buf[MSGBUFSIZE], from_line[MSGBUFSIZE];
62         gchar *tmp_file;
63         gint msgs = 0;
64
65         g_return_val_if_fail(dest != NULL, -1);
66         g_return_val_if_fail(mbox != NULL, -1);
67
68         debug_print(_("Getting messages from %s into %s...\n"), mbox, dest->path);
69
70         if ((mbox_fp = fopen(mbox, "r")) == NULL) {
71                 FILE_OP_ERROR(mbox, "fopen");
72                 return -1;
73         }
74
75         /* ignore empty lines on the head */
76         do {
77                 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
78                         g_warning(_("can't read mbox file.\n"));
79                         fclose(mbox_fp);
80                         return -1;
81                 }
82         } while (buf[0] == '\n' || buf[0] == '\r');
83
84         if (strncmp(buf, "From ", 5) != 0) {
85                 g_warning(_("invalid mbox format: %s\n"), mbox);
86                 fclose(mbox_fp);
87                 return -1;
88         }
89
90         strcpy(from_line, buf);
91         if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
92                 g_warning(_("malformed mbox: %s\n"), mbox);
93                 fclose(mbox_fp);
94                 return -1;
95         }
96
97         tmp_file = get_tmp_file();
98
99         do {
100                 FILE *tmp_fp;
101                 FolderItem *dropfolder;
102                 gchar *startp, *endp, *rpath;
103                 gint empty_line;
104                 gint val;
105                 gboolean is_next_msg = FALSE;
106
107                 if ((tmp_fp = fopen(tmp_file, "w")) == NULL) {
108                         FILE_OP_ERROR(tmp_file, "fopen");
109                         g_warning(_("can't open temporary file\n"));
110                         fclose(mbox_fp);
111                         return -1;
112                 }
113                 if (change_file_mode_rw(tmp_fp, tmp_file) < 0)
114                         FILE_OP_ERROR(tmp_file, "chmod");
115
116                 /* convert unix From into Return-Path */
117                 startp = from_line + 5;
118                 endp = strchr(startp, ' ');
119                 if (endp == NULL)
120                         rpath = g_strdup(startp);
121                 else
122                         rpath = g_strndup(startp, endp - startp);
123                 g_strstrip(rpath);
124                 g_snprintf(from_line, sizeof(from_line),
125                            "Return-Path: %s\n", rpath);
126                 g_free(rpath);
127
128                 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
129                 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
130                 from_line[0] = '\0';
131
132                 empty_line = 0;
133
134                 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
135                         if (buf[0] == '\n' || buf[0] == '\r') {
136                                 empty_line++;
137                                 buf[0] = '\0';
138                                 continue;
139                         }
140
141                         /* From separator */
142                         while (!strncmp(buf, "From ", 5)) {
143                                 strcpy(from_line, buf);
144                                 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
145                                         buf[0] = '\0';
146                                         break;
147                                 }
148
149                                 if (is_header_line(buf)) {
150                                         is_next_msg = TRUE;
151                                         break;
152                                 } else if (!strncmp(buf, "From ", 5)) {
153                                         continue;
154                                 } else if (!strncmp(buf, ">From ", 6)) {
155                                         g_memmove(buf, buf + 1, strlen(buf));
156                                         is_next_msg = TRUE;
157                                         break;
158                                 } else {
159                                         g_warning(_("unescaped From found:\n%s"),
160                                                   from_line);
161                                         break;
162                                 }
163                         }
164                         if (is_next_msg) break;
165
166                         if (empty_line > 0) {
167                                 while (empty_line--)
168                                         FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
169                                 empty_line = 0;
170                         }
171
172                         if (from_line[0] != '\0') {
173                                 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
174                                 from_line[0] = '\0';
175                         }
176
177                         if (buf[0] != '\0') {
178                                 if (!strncmp(buf, ">From ", 6)) {
179                                         FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
180                                 } else
181                                         FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
182
183                                 buf[0] = '\0';
184                         }
185                 }
186
187                 if (empty_line > 0) {
188                         while (--empty_line)
189                                 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
190                 }
191
192                 if (fclose(tmp_fp) == EOF) {
193                         FILE_OP_ERROR(tmp_file, "fclose");
194                         g_warning(_("can't write to temporary file\n"));
195                         fclose(mbox_fp);
196                         unlink(tmp_file);
197                         return -1;
198                 }
199
200                 if (folder_table) {
201                         dropfolder = filter_get_dest_folder
202                                 (prefs_common.fltlist, tmp_file);
203                         if (!dropfolder ||
204                             !strcmp(dropfolder->path, FILTER_NOT_RECEIVE))
205                                 dropfolder = dest;
206                         val = GPOINTER_TO_INT(g_hash_table_lookup
207                                               (folder_table, dropfolder));
208                         if (val == 0) {
209                                 folder_item_scan(dropfolder);
210                                 g_hash_table_insert(folder_table, dropfolder,
211                                                     GINT_TO_POINTER(1));
212                         }
213                 } else
214                         dropfolder = dest;
215
216                 if (folder_item_add_msg(dropfolder, tmp_file) < 0) {
217                         fclose(mbox_fp);
218                         unlink(tmp_file);
219                         return -1;
220                 }
221
222                 msgs++;
223         } while (from_line[0] != '\0');
224
225         fclose(mbox_fp);
226         debug_print(_("%d messages found.\n"), msgs);
227
228         return msgs;
229 }
230
231 gint lock_mbox(const gchar *base, LockType type)
232 {
233         gint retval = 0;
234
235         if (type == LOCK_FILE) {
236                 gchar *lockfile, *locklink;
237                 gint retry = 0;
238                 FILE *lockfp;
239
240                 lockfile = g_strdup_printf("%s.%d", base, getpid());
241                 if ((lockfp = fopen(lockfile, "w")) == NULL) {
242                         FILE_OP_ERROR(lockfile, "fopen");
243                         g_warning(_("can't create lock file %s\n"), lockfile);
244                         g_warning(_("use 'flock' instead of 'file' if possible.\n"));
245                         g_free(lockfile);
246                         return -1;
247                 }
248
249                 fprintf(lockfp, "%d\n", getpid());
250                 fclose(lockfp);
251
252                 locklink = g_strconcat(base, ".lock", NULL);
253                 while (link(lockfile, locklink) < 0) {
254                         FILE_OP_ERROR(lockfile, "link");
255                         if (retry >= 5) {
256                                 g_warning(_("can't create %s\n"), lockfile);
257                                 unlink(lockfile);
258                                 g_free(lockfile);
259                                 return -1;
260                         }
261                         if (retry == 0)
262                                 g_warning(_("mailbox is owned by another"
263                                             " process, waiting...\n"));
264                         retry++;
265                         sleep(5);
266                 }
267                 unlink(lockfile);
268                 g_free(lockfile);
269         } else if (type == LOCK_FLOCK) {
270                 gint lockfd;
271
272 #if HAVE_FLOCK
273                 if ((lockfd = open(base, O_RDONLY)) < 0) {
274 #else
275                 if ((lockfd = open(base, O_RDWR)) < 0) {
276 #endif
277                         FILE_OP_ERROR(base, "open");
278                         return -1;
279                 }
280 #if HAVE_FLOCK
281                 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
282                         perror("flock");
283 #else
284 #if HAVE_LOCKF
285                 if (lockf(lockfd, F_TLOCK, 0) < 0) {
286                         perror("lockf");
287 #else
288                 {
289 #endif
290 #endif /* HAVE_FLOCK */
291                         g_warning(_("can't lock %s\n"), base);
292                         if (close(lockfd) < 0)
293                                 perror("close");
294                         return -1;
295                 }
296                 retval = lockfd;
297         } else {
298                 g_warning(_("invalid lock type\n"));
299                 return -1;
300         }
301
302         return retval;
303 }
304
305 gint unlock_mbox(const gchar *base, gint fd, LockType type)
306 {
307         if (type == LOCK_FILE) {
308                 gchar *lockfile;
309
310                 lockfile = g_strconcat(base, ".lock", NULL);
311                 if (unlink(lockfile) < 0) {
312                         FILE_OP_ERROR(lockfile, "unlink");
313                         g_free(lockfile);
314                         return -1;
315                 }
316                 g_free(lockfile);
317
318                 return 0;
319         } else if (type == LOCK_FLOCK) {
320 #if HAVE_FLOCK
321                 if (flock(fd, LOCK_UN) < 0) {
322                         perror("flock");
323 #else
324 #if HAVE_LOCKF
325                 if (lockf(fd, F_ULOCK, 0) < 0) {
326                         perror("lockf");
327 #else
328                 {
329 #endif
330 #endif /* HAVE_FLOCK */
331                         g_warning(_("can't unlock %s\n"), base);
332                         if (close(fd) < 0)
333                                 perror("close");
334                         return -1;
335                 }
336
337                 if (close(fd) < 0) {
338                         perror("close");
339                         return -1;
340                 }
341
342                 return 0;
343         }
344
345         g_warning(_("invalid lock type\n"));
346         return -1;
347 }
348
349 gint copy_mbox(const gchar *src, const gchar *dest)
350 {
351         return copy_file(src, dest);
352 }
353
354 void empty_mbox(const gchar *mbox)
355 {
356         if (truncate(mbox, 0) < 0) {
357                 FILE *fp;
358
359                 FILE_OP_ERROR(mbox, "truncate");
360                 if ((fp = fopen(mbox, "w")) == NULL) {
361                         FILE_OP_ERROR(mbox, "fopen");
362                         g_warning(_("can't truncate mailbox to zero.\n"));
363                         return;
364                 }
365                 fclose(fp);
366         }
367 }
368
369 /* read all messages in SRC, and store them into one MBOX file. */
370 gint export_mbox(FolderItem *src, const gchar *mbox)
371 {
372         GSList *mlist = NULL;
373         GSList *cur;
374         MsgInfo *msginfo;
375         FILE *msg_fp;
376         FILE *mbox_fp;
377         gchar buf[BUFFSIZE];
378
379         g_return_val_if_fail(src != NULL, -1);
380         g_return_val_if_fail(src->folder != NULL, -1);
381         g_return_val_if_fail(mbox != NULL, -1);
382
383         debug_print(_("Exporting messages from %s into %s...\n"),
384                     src->path, mbox);
385
386         mlist = src->folder->get_msg_list(src->folder, src, TRUE);
387         if (!mlist) return 0;
388
389         if ((mbox_fp = fopen(mbox, "w")) == NULL) {
390                 FILE_OP_ERROR(mbox, "fopen");
391                 return -1;
392         }
393
394         for (cur = mlist; cur != NULL; cur = cur->next) {
395                 msginfo = (MsgInfo *)cur->data;
396
397                 msg_fp = procmsg_open_message(msginfo);
398                 if (!msg_fp) continue;
399
400                 strncpy2(buf,
401                          msginfo->from ? msginfo->from :
402                          cur_account && cur_account->address ?
403                          cur_account->address : "unknown",
404                          sizeof(buf));
405                 extract_address(buf);
406
407                 fprintf(mbox_fp, "From %s %s",
408                         buf, ctime(&msginfo->date_t));
409
410                 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
411                         if (!strncmp(buf, "From ", 5))
412                                 fputc('>', mbox_fp);
413                         fputs(buf, mbox_fp);
414                 }
415                 fputc('\n', mbox_fp);
416
417                 fclose(msg_fp);
418                 procmsg_msginfo_free(msginfo);
419         }
420
421         g_slist_free(mlist);
422
423         fclose(mbox_fp);
424
425         return 0;
426 }