2005-09-08 [colin] 1.9.14cvs8
[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 <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 "folder.h"
39 #include "prefs_common.h"
40 #include "prefs_account.h"
41 #include "account.h"
42 #include "utils.h"
43 #include "filtering.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                 g_unlink(tmp_file); \
54                 g_free(tmp_file); \
55                 return -1; \
56         } \
57 }
58
59 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter)
60 {
61         FILE *mbox_fp;
62         gchar buf[MSGBUFSIZE], from_line[MSGBUFSIZE];
63         gchar *tmp_file;
64         gint msgs = 0;
65         MsgInfo *msginfo;
66
67         g_return_val_if_fail(dest != NULL, -1);
68         g_return_val_if_fail(mbox != NULL, -1);
69
70         debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
71
72         if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
73                 FILE_OP_ERROR(mbox, "fopen");
74                 return -1;
75         }
76
77         /* ignore empty lines on the head */
78         do {
79                 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
80                         g_warning("can't read mbox file.\n");
81                         fclose(mbox_fp);
82                         return -1;
83                 }
84         } while (buf[0] == '\n' || buf[0] == '\r');
85
86         if (strncmp(buf, "From ", 5) != 0) {
87                 g_warning("invalid mbox format: %s\n", mbox);
88                 fclose(mbox_fp);
89                 return -1;
90         }
91
92         strcpy(from_line, buf);
93         if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
94                 g_warning("malformed mbox: %s\n", mbox);
95                 fclose(mbox_fp);
96                 return -1;
97         }
98
99         tmp_file = get_tmp_file();
100
101         folder_item_update_freeze();
102         
103         do {
104                 FILE *tmp_fp;
105                 FolderItem *dropfolder;
106                 gint empty_line;
107                 gboolean is_next_msg = FALSE;
108                 gint msgnum;
109
110                 if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
111                         FILE_OP_ERROR(tmp_file, "fopen");
112                         g_warning("can't open temporary file\n");
113                         fclose(mbox_fp);
114                         g_free(tmp_file);
115                         return -1;
116                 }
117                 if (change_file_mode_rw(tmp_fp, tmp_file) < 0)
118                         FILE_OP_ERROR(tmp_file, "chmod");
119
120                 /* convert unix From into Return-Path */
121                 /*
122                 startp = from_line + 5;
123                 endp = strchr(startp, ' ');
124                 if (endp == NULL)
125                         rpath = g_strdup(startp);
126                 else
127                         rpath = g_strndup(startp, endp - startp);
128                 g_strstrip(rpath);
129                 g_snprintf(from_line, sizeof(from_line),
130                            "Return-Path: %s\n", rpath);
131                 g_free(rpath);
132                 */
133
134                 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
135                 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
136                 from_line[0] = '\0';
137
138                 empty_line = 0;
139
140                 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
141                         if (buf[0] == '\n' || buf[0] == '\r') {
142                                 empty_line++;
143                                 buf[0] = '\0';
144                                 continue;
145                         }
146
147                         /* From separator */
148                         while (!strncmp(buf, "From ", 5)) {
149                                 strcpy(from_line, buf);
150                                 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
151                                         buf[0] = '\0';
152                                         break;
153                                 }
154
155                                 if (is_header_line(buf)) {
156                                         is_next_msg = TRUE;
157                                         break;
158                                 } else if (!strncmp(buf, "From ", 5)) {
159                                         continue;
160                                 } else if (!strncmp(buf, ">From ", 6)) {
161                                         g_memmove(buf, buf + 1, strlen(buf));
162                                         is_next_msg = TRUE;
163                                         break;
164                                 } else {
165                                         g_warning("unescaped From found:\n%s",
166                                                   from_line);
167                                         break;
168                                 }
169                         }
170                         if (is_next_msg) break;
171
172                         if (empty_line > 0) {
173                                 while (empty_line--)
174                                         FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
175                                 empty_line = 0;
176                         }
177
178                         if (from_line[0] != '\0') {
179                                 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
180                                 from_line[0] = '\0';
181                         }
182
183                         if (buf[0] != '\0') {
184                                 if (!strncmp(buf, ">From ", 6)) {
185                                         FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
186                                 } else
187                                         FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
188
189                                 buf[0] = '\0';
190                         }
191                 }
192
193                 if (empty_line > 0) {
194                         while (--empty_line)
195                                 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
196                 }
197
198                 if (fclose(tmp_fp) == EOF) {
199                         FILE_OP_ERROR(tmp_file, "fclose");
200                         g_warning("can't write to temporary file\n");
201                         fclose(mbox_fp);
202                         g_unlink(tmp_file);
203                         g_free(tmp_file);
204                         return -1;
205                 }
206
207                 dropfolder = folder_get_default_processing();
208                         
209                 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
210                         fclose(mbox_fp);
211                         g_unlink(tmp_file);
212                         g_free(tmp_file);
213                         return -1;
214                 }
215
216                 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
217                 if (!apply_filter || !procmsg_msginfo_filter(msginfo))
218                         folder_item_move_msg(dest, msginfo);
219                 procmsg_msginfo_free(msginfo);
220
221                 msgs++;
222         } while (from_line[0] != '\0');
223
224         folder_item_update_thaw();
225         
226         g_free(tmp_file);
227         fclose(mbox_fp);
228         debug_print("%d messages found.\n", msgs);
229
230         return msgs;
231 }
232
233 gint lock_mbox(const gchar *base, LockType type)
234 {
235 #ifdef G_OS_UNIX
236         gint retval = 0;
237
238         if (type == LOCK_FILE) {
239                 gchar *lockfile, *locklink;
240                 gint retry = 0;
241                 FILE *lockfp;
242
243                 lockfile = g_strdup_printf("%s.%d", base, getpid());
244                 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
245                         FILE_OP_ERROR(lockfile, "fopen");
246                         g_warning("can't create lock file %s\n", lockfile);
247                         g_warning("use 'flock' instead of 'file' if possible.\n");
248                         g_free(lockfile);
249                         return -1;
250                 }
251
252                 fprintf(lockfp, "%d\n", getpid());
253                 fclose(lockfp);
254
255                 locklink = g_strconcat(base, ".lock", NULL);
256                 while (link(lockfile, locklink) < 0) {
257                         FILE_OP_ERROR(lockfile, "link");
258                         if (retry >= 5) {
259                                 g_warning("can't create %s\n", lockfile);
260                                 g_unlink(lockfile);
261                                 g_free(lockfile);
262                                 return -1;
263                         }
264                         if (retry == 0)
265                                 g_warning("mailbox is owned by another"
266                                             " process, waiting...\n");
267                         retry++;
268                         sleep(5);
269                 }
270                 g_unlink(lockfile);
271                 g_free(lockfile);
272         } else if (type == LOCK_FLOCK) {
273                 gint lockfd;
274
275 #if HAVE_FLOCK
276                 if ((lockfd = open(base, O_RDONLY)) < 0) {
277 #else
278                 if ((lockfd = open(base, O_RDWR)) < 0) {
279 #endif
280                         FILE_OP_ERROR(base, "open");
281                         return -1;
282                 }
283 #if HAVE_FLOCK
284                 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
285                         perror("flock");
286 #else
287 #if HAVE_LOCKF
288                 if (lockf(lockfd, F_TLOCK, 0) < 0) {
289                         perror("lockf");
290 #else
291                 {
292 #endif
293 #endif /* HAVE_FLOCK */
294                         g_warning("can't lock %s\n", base);
295                         if (close(lockfd) < 0)
296                                 perror("close");
297                         return -1;
298                 }
299                 retval = lockfd;
300         } else {
301                 g_warning("invalid lock type\n");
302                 return -1;
303         }
304
305         return retval;
306 #else
307         return -1;
308 #endif /* G_OS_UNIX */
309 }
310
311 gint unlock_mbox(const gchar *base, gint fd, LockType type)
312 {
313         if (type == LOCK_FILE) {
314                 gchar *lockfile;
315
316                 lockfile = g_strconcat(base, ".lock", NULL);
317                 if (g_unlink(lockfile) < 0) {
318                         FILE_OP_ERROR(lockfile, "unlink");
319                         g_free(lockfile);
320                         return -1;
321                 }
322                 g_free(lockfile);
323
324                 return 0;
325         } else if (type == LOCK_FLOCK) {
326 #if HAVE_FLOCK
327                 if (flock(fd, LOCK_UN) < 0) {
328                         perror("flock");
329 #else
330 #if HAVE_LOCKF
331                 if (lockf(fd, F_ULOCK, 0) < 0) {
332                         perror("lockf");
333 #else
334                 {
335 #endif
336 #endif /* HAVE_FLOCK */
337                         g_warning("can't unlock %s\n", base);
338                         if (close(fd) < 0)
339                                 perror("close");
340                         return -1;
341                 }
342
343                 if (close(fd) < 0) {
344                         perror("close");
345                         return -1;
346                 }
347
348                 return 0;
349         }
350
351         g_warning("invalid lock type\n");
352         return -1;
353 }
354
355 gint copy_mbox(const gchar *src, const gchar *dest)
356 {
357         return copy_file(src, dest, TRUE);
358 }
359
360 void empty_mbox(const gchar *mbox)
361 {
362         FILE *fp;
363
364         if ((fp = g_fopen(mbox, "wb")) == NULL) {
365                 FILE_OP_ERROR(mbox, "fopen");
366                 g_warning("can't truncate mailbox to zero.\n");
367                 return;
368         }
369         fclose(fp);
370 }
371
372 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
373 {
374         GSList *cur;
375         MsgInfo *msginfo;
376         FILE *msg_fp;
377         FILE *mbox_fp;
378         gchar buf[BUFFSIZE];
379
380         if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
381                 FILE_OP_ERROR(mbox, "fopen");
382                 return -1;
383         }
384
385         for (cur = mlist; cur != NULL; cur = cur->next) {
386                 msginfo = (MsgInfo *)cur->data;
387
388                 msg_fp = procmsg_open_message(msginfo);
389                 if (!msg_fp) {
390                         procmsg_msginfo_free(msginfo);
391                         continue;
392                 }
393
394                 strncpy2(buf,
395                          msginfo->from ? msginfo->from :
396                          cur_account && cur_account->address ?
397                          cur_account->address : "unknown",
398                          sizeof(buf));
399                 extract_address(buf);
400
401                 fprintf(mbox_fp, "From %s %s",
402                         buf, ctime(&msginfo->date_t));
403
404                 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
405                         if (!strncmp(buf, "From ", 5))
406                                 fputc('>', mbox_fp);
407                         fputs(buf, mbox_fp);
408                 }
409                 fputc('\n', mbox_fp);
410
411                 fclose(msg_fp);
412                 procmsg_msginfo_free(msginfo);
413         }
414         
415         fclose(mbox_fp);
416
417         return 0;
418 }
419
420 /* read all messages in SRC, and store them into one MBOX file. */
421 gint export_to_mbox(FolderItem *src, const gchar *mbox)
422 {
423         GSList *mlist;
424         
425         g_return_val_if_fail(src != NULL, -1);
426         g_return_val_if_fail(src->folder != NULL, -1);
427         g_return_val_if_fail(mbox != NULL, -1);
428
429         debug_print("Exporting messages from %s into %s...\n",
430                     src->path, mbox);
431
432         mlist = folder_item_get_msg_list(src);
433
434         export_list_to_mbox(mlist, mbox);
435
436         procmsg_msg_list_free(mlist);
437
438         return 0;
439 }