2005-09-15 [paul] 1.9.14cvs31
[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                 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
134                 */
135
136                 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
137                 from_line[0] = '\0';
138
139                 empty_line = 0;
140
141                 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
142                         if (buf[0] == '\n' || buf[0] == '\r') {
143                                 empty_line++;
144                                 buf[0] = '\0';
145                                 continue;
146                         }
147
148                         /* From separator */
149                         while (!strncmp(buf, "From ", 5)) {
150                                 strcpy(from_line, buf);
151                                 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
152                                         buf[0] = '\0';
153                                         break;
154                                 }
155
156                                 if (is_header_line(buf)) {
157                                         is_next_msg = TRUE;
158                                         break;
159                                 } else if (!strncmp(buf, "From ", 5)) {
160                                         continue;
161                                 } else if (!strncmp(buf, ">From ", 6)) {
162                                         g_memmove(buf, buf + 1, strlen(buf));
163                                         is_next_msg = TRUE;
164                                         break;
165                                 } else {
166                                         g_warning("unescaped From found:\n%s",
167                                                   from_line);
168                                         break;
169                                 }
170                         }
171                         if (is_next_msg) break;
172
173                         if (empty_line > 0) {
174                                 while (empty_line--)
175                                         FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
176                                 empty_line = 0;
177                         }
178
179                         if (from_line[0] != '\0') {
180                                 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
181                                 from_line[0] = '\0';
182                         }
183
184                         if (buf[0] != '\0') {
185                                 if (!strncmp(buf, ">From ", 6)) {
186                                         FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
187                                 } else
188                                         FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
189
190                                 buf[0] = '\0';
191                         }
192                 }
193
194                 if (empty_line > 0) {
195                         while (--empty_line)
196                                 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
197                 }
198
199                 if (fclose(tmp_fp) == EOF) {
200                         FILE_OP_ERROR(tmp_file, "fclose");
201                         g_warning("can't write to temporary file\n");
202                         fclose(mbox_fp);
203                         g_unlink(tmp_file);
204                         g_free(tmp_file);
205                         return -1;
206                 }
207
208                 dropfolder = folder_get_default_processing();
209                         
210                 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
211                         fclose(mbox_fp);
212                         g_unlink(tmp_file);
213                         g_free(tmp_file);
214                         return -1;
215                 }
216
217                 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
218                 if (!apply_filter || !procmsg_msginfo_filter(msginfo))
219                         folder_item_move_msg(dest, msginfo);
220                 procmsg_msginfo_free(msginfo);
221
222                 msgs++;
223         } while (from_line[0] != '\0');
224
225         folder_item_update_thaw();
226         
227         g_free(tmp_file);
228         fclose(mbox_fp);
229         debug_print("%d messages found.\n", msgs);
230
231         return msgs;
232 }
233
234 gint lock_mbox(const gchar *base, LockType type)
235 {
236 #ifdef G_OS_UNIX
237         gint retval = 0;
238
239         if (type == LOCK_FILE) {
240                 gchar *lockfile, *locklink;
241                 gint retry = 0;
242                 FILE *lockfp;
243
244                 lockfile = g_strdup_printf("%s.%d", base, getpid());
245                 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
246                         FILE_OP_ERROR(lockfile, "fopen");
247                         g_warning("can't create lock file %s\n", lockfile);
248                         g_warning("use 'flock' instead of 'file' if possible.\n");
249                         g_free(lockfile);
250                         return -1;
251                 }
252
253                 fprintf(lockfp, "%d\n", getpid());
254                 fclose(lockfp);
255
256                 locklink = g_strconcat(base, ".lock", NULL);
257                 while (link(lockfile, locklink) < 0) {
258                         FILE_OP_ERROR(lockfile, "link");
259                         if (retry >= 5) {
260                                 g_warning("can't create %s\n", lockfile);
261                                 g_unlink(lockfile);
262                                 g_free(lockfile);
263                                 return -1;
264                         }
265                         if (retry == 0)
266                                 g_warning("mailbox is owned by another"
267                                             " process, waiting...\n");
268                         retry++;
269                         sleep(5);
270                 }
271                 g_unlink(lockfile);
272                 g_free(lockfile);
273         } else if (type == LOCK_FLOCK) {
274                 gint lockfd;
275
276 #if HAVE_FLOCK
277                 if ((lockfd = open(base, O_RDONLY)) < 0) {
278 #else
279                 if ((lockfd = open(base, O_RDWR)) < 0) {
280 #endif
281                         FILE_OP_ERROR(base, "open");
282                         return -1;
283                 }
284 #if HAVE_FLOCK
285                 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
286                         perror("flock");
287 #else
288 #if HAVE_LOCKF
289                 if (lockf(lockfd, F_TLOCK, 0) < 0) {
290                         perror("lockf");
291 #else
292                 {
293 #endif
294 #endif /* HAVE_FLOCK */
295                         g_warning("can't lock %s\n", base);
296                         if (close(lockfd) < 0)
297                                 perror("close");
298                         return -1;
299                 }
300                 retval = lockfd;
301         } else {
302                 g_warning("invalid lock type\n");
303                 return -1;
304         }
305
306         return retval;
307 #else
308         return -1;
309 #endif /* G_OS_UNIX */
310 }
311
312 gint unlock_mbox(const gchar *base, gint fd, LockType type)
313 {
314         if (type == LOCK_FILE) {
315                 gchar *lockfile;
316
317                 lockfile = g_strconcat(base, ".lock", NULL);
318                 if (g_unlink(lockfile) < 0) {
319                         FILE_OP_ERROR(lockfile, "unlink");
320                         g_free(lockfile);
321                         return -1;
322                 }
323                 g_free(lockfile);
324
325                 return 0;
326         } else if (type == LOCK_FLOCK) {
327 #if HAVE_FLOCK
328                 if (flock(fd, LOCK_UN) < 0) {
329                         perror("flock");
330 #else
331 #if HAVE_LOCKF
332                 if (lockf(fd, F_ULOCK, 0) < 0) {
333                         perror("lockf");
334 #else
335                 {
336 #endif
337 #endif /* HAVE_FLOCK */
338                         g_warning("can't unlock %s\n", base);
339                         if (close(fd) < 0)
340                                 perror("close");
341                         return -1;
342                 }
343
344                 if (close(fd) < 0) {
345                         perror("close");
346                         return -1;
347                 }
348
349                 return 0;
350         }
351
352         g_warning("invalid lock type\n");
353         return -1;
354 }
355
356 gint copy_mbox(const gchar *src, const gchar *dest)
357 {
358         return copy_file(src, dest, TRUE);
359 }
360
361 void empty_mbox(const gchar *mbox)
362 {
363         FILE *fp;
364
365         if ((fp = g_fopen(mbox, "wb")) == NULL) {
366                 FILE_OP_ERROR(mbox, "fopen");
367                 g_warning("can't truncate mailbox to zero.\n");
368                 return;
369         }
370         fclose(fp);
371 }
372
373 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
374 {
375         GSList *cur;
376         MsgInfo *msginfo;
377         FILE *msg_fp;
378         FILE *mbox_fp;
379         gchar buf[BUFFSIZE];
380
381         if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
382                 FILE_OP_ERROR(mbox, "fopen");
383                 return -1;
384         }
385
386         for (cur = mlist; cur != NULL; cur = cur->next) {
387                 msginfo = (MsgInfo *)cur->data;
388
389                 msg_fp = procmsg_open_message(msginfo);
390                 if (!msg_fp) {
391                         procmsg_msginfo_free(msginfo);
392                         continue;
393                 }
394
395                 strncpy2(buf,
396                          msginfo->from ? msginfo->from :
397                          cur_account && cur_account->address ?
398                          cur_account->address : "unknown",
399                          sizeof(buf));
400                 extract_address(buf);
401
402                 fprintf(mbox_fp, "From %s %s",
403                         buf, ctime(&msginfo->date_t));
404
405                 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
406                         if (!strncmp(buf, "From ", 5))
407                                 fputc('>', mbox_fp);
408                         fputs(buf, mbox_fp);
409                 }
410                 fputc('\n', mbox_fp);
411
412                 fclose(msg_fp);
413                 procmsg_msginfo_free(msginfo);
414         }
415         
416         fclose(mbox_fp);
417
418         return 0;
419 }
420
421 /* read all messages in SRC, and store them into one MBOX file. */
422 gint export_to_mbox(FolderItem *src, const gchar *mbox)
423 {
424         GSList *mlist;
425         
426         g_return_val_if_fail(src != NULL, -1);
427         g_return_val_if_fail(src->folder != NULL, -1);
428         g_return_val_if_fail(mbox != NULL, -1);
429
430         debug_print("Exporting messages from %s into %s...\n",
431                     src->path, mbox);
432
433         mlist = folder_item_get_msg_list(src);
434
435         export_list_to_mbox(mlist, mbox);
436
437         procmsg_msg_list_free(mlist);
438
439         return 0;
440 }