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