sync with 0.8.1cvs22
[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 "filter.h"
40 #include "prefs_common.h"
41 #include "prefs_account.h"
42 #include "account.h"
43 #include "utils.h"
44 #include "filtering.h"
45
46 #define MSGBUFSIZE      8192
47
48 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
49 { \
50         if (fputs(s, tmp_fp) == EOF) { \
51                 g_warning(_("can't write to temporary file\n")); \
52                 fclose(tmp_fp); \
53                 fclose(mbox_fp); \
54                 unlink(tmp_file); \
55                 g_free(tmp_file); \
56                 return -1; \
57         } \
58 }
59
60 gint proc_mbox(FolderItem *dest, const gchar *mbox, GHashTable *folder_table)
61 {
62         FILE *mbox_fp;
63         gchar buf[MSGBUFSIZE], from_line[MSGBUFSIZE];
64         gchar *tmp_file;
65         gint msgs = 0;
66         FolderItem *inbox;
67
68         g_return_val_if_fail(dest != NULL, -1);
69         g_return_val_if_fail(mbox != NULL, -1);
70
71         debug_print(_("Getting messages from %s into %s...\n"), mbox, dest->path);
72
73         if ((mbox_fp = fopen(mbox, "rb")) == NULL) {
74                 FILE_OP_ERROR(mbox, "fopen");
75                 return -1;
76         }
77
78         /* ignore empty lines on the head */
79         do {
80                 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
81                         g_warning(_("can't read mbox file.\n"));
82                         fclose(mbox_fp);
83                         return -1;
84                 }
85         } while (buf[0] == '\n' || buf[0] == '\r');
86
87         if (strncmp(buf, "From ", 5) != 0) {
88                 g_warning(_("invalid mbox format: %s\n"), mbox);
89                 fclose(mbox_fp);
90                 return -1;
91         }
92
93         strcpy(from_line, buf);
94         if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
95                 g_warning(_("malformed mbox: %s\n"), mbox);
96                 fclose(mbox_fp);
97                 return -1;
98         }
99
100         tmp_file = get_tmp_file();
101         inbox    = folder_get_default_inbox();
102
103         do {
104                 FILE *tmp_fp;
105                 FolderItem *dropfolder;
106                 gint empty_line;
107                 gint val;
108                 gboolean is_next_msg = FALSE;
109                 gint msgnum;
110
111                 if ((tmp_fp = fopen(tmp_file, "wb")) == NULL) {
112                         FILE_OP_ERROR(tmp_file, "fopen");
113                         g_warning(_("can't open temporary file\n"));
114                         fclose(mbox_fp);
115                         g_free(tmp_file);
116                         return -1;
117                 }
118                 if (change_file_mode_rw(tmp_fp, tmp_file) < 0)
119                         FILE_OP_ERROR(tmp_file, "chmod");
120
121                 /* convert unix From into Return-Path */
122                 /*
123                 startp = from_line + 5;
124                 endp = strchr(startp, ' ');
125                 if (endp == NULL)
126                         rpath = g_strdup(startp);
127                 else
128                         rpath = g_strndup(startp, endp - startp);
129                 g_strstrip(rpath);
130                 g_snprintf(from_line, sizeof(from_line),
131                            "Return-Path: %s\n", rpath);
132                 g_free(rpath);
133                 */
134
135                 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
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                         unlink(tmp_file);
204                         g_free(tmp_file);
205                         return -1;
206                 }
207
208                 if (folder_table) {
209                         if (global_processing == NULL) {
210                                 /* old filtering */
211                                 dropfolder = filter_get_dest_folder
212                                         (prefs_common.fltlist, tmp_file);
213                                 if (!dropfolder ||
214                                     !strcmp(dropfolder->path, FILTER_NOT_RECEIVE))
215                                         dropfolder = dest;
216                                 val = GPOINTER_TO_INT(g_hash_table_lookup
217                                                       (folder_table, dropfolder));
218                                 if (val == 0) {
219                                         g_hash_table_insert(folder_table, dropfolder,
220                                                             GINT_TO_POINTER(1));
221                                 }
222                         }
223                         else {
224                                 /* CLAWS: new filtering */
225                                 dropfolder = folder_get_default_processing();
226                         }
227                 } else
228                         dropfolder = dest;
229
230                         
231                 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, TRUE)) < 0) {
232                         fclose(mbox_fp);
233                         unlink(tmp_file);
234                         g_free(tmp_file);
235                         return -1;
236                 }
237
238                 if (global_processing) {
239                         /* CLAWS: new filtering */
240                         if (folder_table) {
241                                 filter_message(global_processing, inbox,
242                                                msgnum, folder_table);
243                         }
244                 }
245
246                 msgs++;
247         } while (from_line[0] != '\0');
248
249         g_free(tmp_file);
250         fclose(mbox_fp);
251         debug_print(_("%d messages found.\n"), msgs);
252
253         return msgs;
254 }
255
256 gint lock_mbox(const gchar *base, LockType type)
257 {
258         gint retval = 0;
259
260         if (type == LOCK_FILE) {
261                 gchar *lockfile, *locklink;
262                 gint retry = 0;
263                 FILE *lockfp;
264
265                 lockfile = g_strdup_printf("%s.%d", base, getpid());
266                 if ((lockfp = fopen(lockfile, "wb")) == NULL) {
267                         FILE_OP_ERROR(lockfile, "fopen");
268                         g_warning(_("can't create lock file %s\n"), lockfile);
269                         g_warning(_("use 'flock' instead of 'file' if possible.\n"));
270                         g_free(lockfile);
271                         return -1;
272                 }
273
274                 fprintf(lockfp, "%d\n", getpid());
275                 fclose(lockfp);
276
277                 locklink = g_strconcat(base, ".lock", NULL);
278                 while (link(lockfile, locklink) < 0) {
279                         FILE_OP_ERROR(lockfile, "link");
280                         if (retry >= 5) {
281                                 g_warning(_("can't create %s\n"), lockfile);
282                                 unlink(lockfile);
283                                 g_free(lockfile);
284                                 return -1;
285                         }
286                         if (retry == 0)
287                                 g_warning(_("mailbox is owned by another"
288                                             " process, waiting...\n"));
289                         retry++;
290                         sleep(5);
291                 }
292                 unlink(lockfile);
293                 g_free(lockfile);
294         } else if (type == LOCK_FLOCK) {
295                 gint lockfd;
296
297 #if HAVE_FLOCK
298                 if ((lockfd = open(base, O_RDONLY)) < 0) {
299 #else
300                 if ((lockfd = open(base, O_RDWR)) < 0) {
301 #endif
302                         FILE_OP_ERROR(base, "open");
303                         return -1;
304                 }
305 #if HAVE_FLOCK
306                 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
307                         perror("flock");
308 #else
309 #if HAVE_LOCKF
310                 if (lockf(lockfd, F_TLOCK, 0) < 0) {
311                         perror("lockf");
312 #else
313                 {
314 #endif
315 #endif /* HAVE_FLOCK */
316                         g_warning(_("can't lock %s\n"), base);
317                         if (close(lockfd) < 0)
318                                 perror("close");
319                         return -1;
320                 }
321                 retval = lockfd;
322         } else {
323                 g_warning(_("invalid lock type\n"));
324                 return -1;
325         }
326
327         return retval;
328 }
329
330 gint unlock_mbox(const gchar *base, gint fd, LockType type)
331 {
332         if (type == LOCK_FILE) {
333                 gchar *lockfile;
334
335                 lockfile = g_strconcat(base, ".lock", NULL);
336                 if (unlink(lockfile) < 0) {
337                         FILE_OP_ERROR(lockfile, "unlink");
338                         g_free(lockfile);
339                         return -1;
340                 }
341                 g_free(lockfile);
342
343                 return 0;
344         } else if (type == LOCK_FLOCK) {
345 #if HAVE_FLOCK
346                 if (flock(fd, LOCK_UN) < 0) {
347                         perror("flock");
348 #else
349 #if HAVE_LOCKF
350                 if (lockf(fd, F_ULOCK, 0) < 0) {
351                         perror("lockf");
352 #else
353                 {
354 #endif
355 #endif /* HAVE_FLOCK */
356                         g_warning(_("can't unlock %s\n"), base);
357                         if (close(fd) < 0)
358                                 perror("close");
359                         return -1;
360                 }
361
362                 if (close(fd) < 0) {
363                         perror("close");
364                         return -1;
365                 }
366
367                 return 0;
368         }
369
370         g_warning(_("invalid lock type\n"));
371         return -1;
372 }
373
374 gint copy_mbox(const gchar *src, const gchar *dest)
375 {
376         return copy_file(src, dest, TRUE);
377 }
378
379 void empty_mbox(const gchar *mbox)
380 {
381         if (truncate(mbox, 0) < 0) {
382                 FILE *fp;
383
384                 FILE_OP_ERROR(mbox, "truncate");
385                 if ((fp = fopen(mbox, "wb")) == NULL) {
386                         FILE_OP_ERROR(mbox, "fopen");
387                         g_warning(_("can't truncate mailbox to zero.\n"));
388                         return;
389                 }
390                 fclose(fp);
391         }
392 }
393
394 /* read all messages in SRC, and store them into one MBOX file. */
395 gint export_to_mbox(FolderItem *src, const gchar *mbox)
396 {
397         GSList *mlist;
398         GSList *cur;
399         MsgInfo *msginfo;
400         FILE *msg_fp;
401         FILE *mbox_fp;
402         gchar buf[BUFFSIZE];
403
404         g_return_val_if_fail(src != NULL, -1);
405         g_return_val_if_fail(src->folder != NULL, -1);
406         g_return_val_if_fail(mbox != NULL, -1);
407
408         debug_print(_("Exporting messages from %s into %s...\n"),
409                     src->path, mbox);
410
411         if ((mbox_fp = fopen(mbox, "wb")) == NULL) {
412                 FILE_OP_ERROR(mbox, "fopen");
413                 return -1;
414         }
415
416         mlist = folder_item_get_msg_list(src);
417
418         for (cur = mlist; cur != NULL; cur = cur->next) {
419                 msginfo = (MsgInfo *)cur->data;
420
421                 msg_fp = procmsg_open_message(msginfo);
422                 if (!msg_fp) {
423                         procmsg_msginfo_free(msginfo);
424                         continue;
425                 }
426
427                 strncpy2(buf,
428                          msginfo->from ? msginfo->from :
429                          cur_account && cur_account->address ?
430                          cur_account->address : "unknown",
431                          sizeof(buf));
432                 extract_address(buf);
433
434                 fprintf(mbox_fp, "From %s %s",
435                         buf, ctime(&msginfo->date_t));
436
437                 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
438                         if (!strncmp(buf, "From ", 5))
439                                 fputc('>', mbox_fp);
440                         fputs(buf, mbox_fp);
441                 }
442                 fputc('\n', mbox_fp);
443
444                 fclose(msg_fp);
445                 procmsg_msginfo_free(msginfo);
446         }
447
448         g_slist_free(mlist);
449
450         fclose(mbox_fp);
451
452         return 0;
453 }