4f438a8900732c69456009d682b4f207396a79a7
[claws.git] / src / mbox.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include "alertpanel.h"
45
46 #define MSGBUFSIZE      8192
47
48 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
49 { \
50         lines++; \
51         if (fputs(s, tmp_fp) == EOF) { \
52                 g_warning("can't write to temporary file\n"); \
53                 fclose(tmp_fp); \
54                 fclose(mbox_fp); \
55                 g_unlink(tmp_file); \
56                 g_free(tmp_file); \
57                 return -1; \
58         } \
59 }
60
61 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter)
62 {
63         FILE *mbox_fp;
64         gchar buf[MSGBUFSIZE];
65         gchar *tmp_file;
66         gint msgs = 0;
67         gint lines;
68         MsgInfo *msginfo;
69         gboolean more;
70
71         g_return_val_if_fail(dest != NULL, -1);
72         g_return_val_if_fail(mbox != NULL, -1);
73
74         debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
75
76         if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
77                 FILE_OP_ERROR(mbox, "fopen");
78                 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
79                 return -1;
80         }
81
82         /* ignore empty lines on the head */
83         do {
84                 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
85                         g_warning("can't read mbox file.\n");
86                         fclose(mbox_fp);
87                         return -1;
88                 }
89         } while (buf[0] == '\n' || buf[0] == '\r');
90
91         if (strncmp(buf, "From ", 5) != 0) {
92                 g_warning("invalid mbox format: %s\n", mbox);
93                 fclose(mbox_fp);
94                 return -1;
95         }
96
97         tmp_file = get_tmp_file();
98
99         folder_item_update_freeze();
100         
101         do {
102                 FILE *tmp_fp;
103                 FolderItem *dropfolder;
104                 gint empty_lines;
105                 gint msgnum;
106
107                 if ((tmp_fp = g_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
118                 empty_lines = 0;
119                 lines = 0;
120                 more = FALSE;
121
122                 /* process all lines from mboxrc file */
123                 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
124                         int offset;
125
126                         /* eof not reached, expect more lines */
127                         more = TRUE;
128
129                         /* eat empty lines */
130                         if (buf[0] == '\n' || buf[0] == '\r') {
131                                 empty_lines++;
132                                 continue;
133                         }
134
135                         /* From separator or quoted From */
136                         offset = 0;
137                         /* detect leading '>' char(s) */
138                         while ((buf[offset] == '>')) {
139                                 offset++;
140                         }
141                         if (!strncmp(buf+offset, "From ", 5)) {
142                                 /* From separator: */
143                                 if (offset == 0) {
144                                         /* expect next mbox item */
145                                         break;
146                                 }
147
148                                 /* quoted From: */
149                                 /* flush any eaten empty line */
150                                 if (empty_lines > 0) {
151                                         while (empty_lines-- > 0) {
152                                                 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
153                                 }
154                                         empty_lines = 0;
155                                 }
156                                 /* store the unquoted line */
157                                 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
158                                 continue;
159                         }
160
161                         /* other line */
162                         /* flush any eaten empty line */
163                         if (empty_lines > 0) {                  
164                                 while (empty_lines-- > 0) {
165                                         FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
166                         }
167                                 empty_lines = 0;
168                         }
169                         /* store the line itself */
170                                         FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
171                 }
172                 /* end of mbox item or end of mbox */
173
174                 /* flush any eaten empty line (but the last one) */
175                 if (empty_lines > 0) {
176                         while (--empty_lines > 0) {
177                                 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
178                         }
179                 }
180
181                 /* more emails to expect? */
182                 more = !feof(mbox_fp);
183
184                 /* warn if email part is empty (it's the minimum check 
185                    we can do */
186                 if (lines == 0) {
187                         g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs);
188                         fclose(tmp_fp);
189                         fclose(mbox_fp);
190                         g_unlink(tmp_file);
191                         return -1;
192                 }
193
194                 if (fclose(tmp_fp) == EOF) {
195                         FILE_OP_ERROR(tmp_file, "fclose");
196                         g_warning("can't write to temporary file\n");
197                         fclose(mbox_fp);
198                         g_unlink(tmp_file);
199                         g_free(tmp_file);
200                         return -1;
201                 }
202
203                 dropfolder = folder_get_default_processing();
204                         
205                 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
206                         fclose(mbox_fp);
207                         g_unlink(tmp_file);
208                         g_free(tmp_file);
209                         return -1;
210                 }
211
212                 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
213                 if (!apply_filter || !procmsg_msginfo_filter(msginfo))
214                         folder_item_move_msg(dest, msginfo);
215                 procmsg_msginfo_free(msginfo);
216
217                 msgs++;
218         } while (more);
219
220         folder_item_update_thaw();
221         
222         g_free(tmp_file);
223         fclose(mbox_fp);
224         debug_print("%d messages found.\n", msgs);
225
226         return msgs;
227 }
228
229 gint lock_mbox(const gchar *base, LockType type)
230 {
231 #ifdef G_OS_UNIX
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 = g_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                                 g_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                 g_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 #else
303         return -1;
304 #endif /* G_OS_UNIX */
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 (g_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         FILE *fp;
359
360         if ((fp = g_fopen(mbox, "wb")) == NULL) {
361                 FILE_OP_ERROR(mbox, "fopen");
362                 g_warning("can't truncate mailbox to zero.\n");
363                 return;
364         }
365         fclose(fp);
366 }
367
368 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
369 {
370         GSList *cur;
371         MsgInfo *msginfo;
372         FILE *msg_fp;
373         FILE *mbox_fp;
374         gchar buf[BUFFSIZE];
375
376         if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
377                 if (alertpanel_full(_("Overwrite mbox file"),
378                                                         _("This file already exists. Do you want to overwrite it?"),
379                                                         _("Overwrite"), GTK_STOCK_CANCEL, NULL, FALSE,
380                                                         NULL, ALERT_WARNING, G_ALERTALTERNATE)
381                         == G_ALERTALTERNATE) {
382                 return -1;
383         }
384         }
385
386         if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
387                 FILE_OP_ERROR(mbox, "fopen");
388                 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
389                 return -1;
390         }
391
392         for (cur = mlist; cur != NULL; cur = cur->next) {
393                 msginfo = (MsgInfo *)cur->data;
394                 int len;
395
396                 msg_fp = procmsg_open_message(msginfo);
397                 if (!msg_fp) {
398                         procmsg_msginfo_free(msginfo);
399                         continue;
400                 }
401
402                 strncpy2(buf,
403                          msginfo->from ? msginfo->from :
404                          cur_account && cur_account->address ?
405                          cur_account->address : "unknown",
406                          sizeof(buf));
407                 extract_address(buf);
408
409                 fprintf(mbox_fp, "From %s %s",
410                         buf, ctime(&msginfo->date_t));
411
412                 buf[0] = '\0';
413                 
414                 /* write email to mboxrc */
415                 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
416                         /* quote any From, >From, >>From, etc., according to mbox format specs */
417                         int offset;
418
419                         offset = 0;
420                         /* detect leading '>' char(s) */
421                         while ((buf[offset] == '>')) {
422                                 offset++;
423                         }
424                         if (!strncmp(buf+offset, "From ", 5))
425                                 fputc('>', mbox_fp);
426                         fputs(buf, mbox_fp);
427                 }
428
429                 /* force last line to end w/ a newline */
430                 len = strlen(buf);
431                 if (len > 0) {
432                         len--;
433                         if ((buf[len] != '\n') && (buf[len] != '\r'))
434                                 fputc('\n', mbox_fp);
435                 }
436
437                 /* add a trailing empty line */
438                 fputc('\n', mbox_fp);
439
440                 fclose(msg_fp);
441                 procmsg_msginfo_free(msginfo);
442         }
443         
444         fclose(mbox_fp);
445
446         return 0;
447 }
448
449 /* read all messages in SRC, and store them into one MBOX file. */
450 gint export_to_mbox(FolderItem *src, const gchar *mbox)
451 {
452         GSList *mlist;
453         
454         g_return_val_if_fail(src != NULL, -1);
455         g_return_val_if_fail(src->folder != NULL, -1);
456         g_return_val_if_fail(mbox != NULL, -1);
457
458         debug_print("Exporting messages from %s into %s...\n",
459                     src->path, mbox);
460
461         mlist = folder_item_get_msg_list(src);
462
463         export_list_to_mbox(mlist, mbox);
464
465         procmsg_msg_list_free(mlist);
466
467         return 0;
468 }