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