use g_basename rather than basename
[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         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 = 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 = 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                         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                         unlink(tmp_file);
212                         g_free(tmp_file);
213                         return -1;
214                 }
215
216                 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
217                 if (!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         gint retval = 0;
236
237         if (type == LOCK_FILE) {
238                 gchar *lockfile, *locklink;
239                 gint retry = 0;
240                 FILE *lockfp;
241
242                 lockfile = g_strdup_printf("%s.%d", base, getpid());
243                 if ((lockfp = fopen(lockfile, "wb")) == NULL) {
244                         FILE_OP_ERROR(lockfile, "fopen");
245                         g_warning("can't create lock file %s\n", lockfile);
246                         g_warning("use 'flock' instead of 'file' if possible.\n");
247                         g_free(lockfile);
248                         return -1;
249                 }
250
251                 fprintf(lockfp, "%d\n", getpid());
252                 fclose(lockfp);
253
254                 locklink = g_strconcat(base, ".lock", NULL);
255                 while (link(lockfile, locklink) < 0) {
256                         FILE_OP_ERROR(lockfile, "link");
257                         if (retry >= 5) {
258                                 g_warning("can't create %s\n", lockfile);
259                                 unlink(lockfile);
260                                 g_free(lockfile);
261                                 return -1;
262                         }
263                         if (retry == 0)
264                                 g_warning("mailbox is owned by another"
265                                             " process, waiting...\n");
266                         retry++;
267                         sleep(5);
268                 }
269                 unlink(lockfile);
270                 g_free(lockfile);
271         } else if (type == LOCK_FLOCK) {
272                 gint lockfd;
273
274 #if HAVE_FLOCK
275                 if ((lockfd = open(base, O_RDONLY)) < 0) {
276 #else
277                 if ((lockfd = open(base, O_RDWR)) < 0) {
278 #endif
279                         FILE_OP_ERROR(base, "open");
280                         return -1;
281                 }
282 #if HAVE_FLOCK
283                 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
284                         perror("flock");
285 #else
286 #if HAVE_LOCKF
287                 if (lockf(lockfd, F_TLOCK, 0) < 0) {
288                         perror("lockf");
289 #else
290                 {
291 #endif
292 #endif /* HAVE_FLOCK */
293                         g_warning("can't lock %s\n", base);
294                         if (close(lockfd) < 0)
295                                 perror("close");
296                         return -1;
297                 }
298                 retval = lockfd;
299         } else {
300                 g_warning("invalid lock type\n");
301                 return -1;
302         }
303
304         return retval;
305 }
306
307 gint unlock_mbox(const gchar *base, gint fd, LockType type)
308 {
309         if (type == LOCK_FILE) {
310                 gchar *lockfile;
311
312                 lockfile = g_strconcat(base, ".lock", NULL);
313                 if (unlink(lockfile) < 0) {
314                         FILE_OP_ERROR(lockfile, "unlink");
315                         g_free(lockfile);
316                         return -1;
317                 }
318                 g_free(lockfile);
319
320                 return 0;
321         } else if (type == LOCK_FLOCK) {
322 #if HAVE_FLOCK
323                 if (flock(fd, LOCK_UN) < 0) {
324                         perror("flock");
325 #else
326 #if HAVE_LOCKF
327                 if (lockf(fd, F_ULOCK, 0) < 0) {
328                         perror("lockf");
329 #else
330                 {
331 #endif
332 #endif /* HAVE_FLOCK */
333                         g_warning("can't unlock %s\n", base);
334                         if (close(fd) < 0)
335                                 perror("close");
336                         return -1;
337                 }
338
339                 if (close(fd) < 0) {
340                         perror("close");
341                         return -1;
342                 }
343
344                 return 0;
345         }
346
347         g_warning("invalid lock type\n");
348         return -1;
349 }
350
351 gint copy_mbox(const gchar *src, const gchar *dest)
352 {
353         return copy_file(src, dest, TRUE);
354 }
355
356 void empty_mbox(const gchar *mbox)
357 {
358         if (truncate(mbox, 0) < 0) {
359                 FILE *fp;
360
361                 FILE_OP_ERROR(mbox, "truncate");
362                 if ((fp = fopen(mbox, "wb")) == NULL) {
363                         FILE_OP_ERROR(mbox, "fopen");
364                         g_warning("can't truncate mailbox to zero.\n");
365                         return;
366                 }
367                 fclose(fp);
368         }
369 }
370
371 /* read all messages in SRC, and store them into one MBOX file. */
372 gint export_to_mbox(FolderItem *src, const gchar *mbox)
373 {
374         GSList *mlist;
375         GSList *cur;
376         MsgInfo *msginfo;
377         FILE *msg_fp;
378         FILE *mbox_fp;
379         gchar buf[BUFFSIZE];
380
381         g_return_val_if_fail(src != NULL, -1);
382         g_return_val_if_fail(src->folder != NULL, -1);
383         g_return_val_if_fail(mbox != NULL, -1);
384
385         debug_print("Exporting messages from %s into %s...\n",
386                     src->path, mbox);
387
388         if ((mbox_fp = fopen(mbox, "wb")) == NULL) {
389                 FILE_OP_ERROR(mbox, "fopen");
390                 return -1;
391         }
392
393         mlist = folder_item_get_msg_list(src);
394
395         for (cur = mlist; cur != NULL; cur = cur->next) {
396                 msginfo = (MsgInfo *)cur->data;
397
398                 msg_fp = procmsg_open_message(msginfo);
399                 if (!msg_fp) {
400                         procmsg_msginfo_free(msginfo);
401                         continue;
402                 }
403
404                 strncpy2(buf,
405                          msginfo->from ? msginfo->from :
406                          cur_account && cur_account->address ?
407                          cur_account->address : "unknown",
408                          sizeof(buf));
409                 extract_address(buf);
410
411                 fprintf(mbox_fp, "From %s %s",
412                         buf, ctime(&msginfo->date_t));
413
414                 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
415                         if (!strncmp(buf, "From ", 5))
416                                 fputc('>', mbox_fp);
417                         fputs(buf, mbox_fp);
418                 }
419                 fputc('\n', mbox_fp);
420
421                 fclose(msg_fp);
422                 procmsg_msginfo_free(msginfo);
423         }
424
425         g_slist_free(mlist);
426
427         fclose(mbox_fp);
428
429         return 0;
430 }