2006-02-06 [colin] 2.0.0cvs21
[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         GSList *to_filter = NULL, *cur;
71
72         g_return_val_if_fail(dest != NULL, -1);
73         g_return_val_if_fail(mbox != NULL, -1);
74
75         debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
76
77         if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
78                 FILE_OP_ERROR(mbox, "fopen");
79                 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
80                 return -1;
81         }
82
83         /* ignore empty lines on the head */
84         do {
85                 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
86                         g_warning("can't read mbox file.\n");
87                         fclose(mbox_fp);
88                         return -1;
89                 }
90         } while (buf[0] == '\n' || buf[0] == '\r');
91
92         if (strncmp(buf, "From ", 5) != 0) {
93                 g_warning("invalid mbox format: %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_lines;
106                 gint msgnum;
107
108                 if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
109                         FILE_OP_ERROR(tmp_file, "fopen");
110                         g_warning("can't open temporary file\n");
111                         fclose(mbox_fp);
112                         g_free(tmp_file);
113                         return -1;
114                 }
115                 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
116                         FILE_OP_ERROR(tmp_file, "chmod");
117                 }
118
119                 empty_lines = 0;
120                 lines = 0;
121                 more = FALSE;
122
123                 /* process all lines from mboxrc file */
124                 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
125                         int offset;
126
127                         /* eof not reached, expect more lines */
128                         more = TRUE;
129
130                         /* eat empty lines */
131                         if (buf[0] == '\n' || buf[0] == '\r') {
132                                 empty_lines++;
133                                 continue;
134                         }
135
136                         /* From separator or quoted From */
137                         offset = 0;
138                         /* detect leading '>' char(s) */
139                         while ((buf[offset] == '>')) {
140                                 offset++;
141                         }
142                         if (!strncmp(buf+offset, "From ", 5)) {
143                                 /* From separator: */
144                                 if (offset == 0) {
145                                         /* expect next mbox item */
146                                         break;
147                                 }
148
149                                 /* quoted From: */
150                                 /* flush any eaten empty line */
151                                 if (empty_lines > 0) {
152                                         while (empty_lines-- > 0) {
153                                                 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
154                                 }
155                                         empty_lines = 0;
156                                 }
157                                 /* store the unquoted line */
158                                 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
159                                 continue;
160                         }
161
162                         /* other line */
163                         /* flush any eaten empty line */
164                         if (empty_lines > 0) {                  
165                                 while (empty_lines-- > 0) {
166                                         FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
167                         }
168                                 empty_lines = 0;
169                         }
170                         /* store the line itself */
171                                         FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
172                 }
173                 /* end of mbox item or end of mbox */
174
175                 /* flush any eaten empty line (but the last one) */
176                 if (empty_lines > 0) {
177                         while (--empty_lines > 0) {
178                                 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
179                         }
180                 }
181
182                 /* more emails to expect? */
183                 more = !feof(mbox_fp);
184
185                 /* warn if email part is empty (it's the minimum check 
186                    we can do */
187                 if (lines == 0) {
188                         g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs);
189                         fclose(tmp_fp);
190                         fclose(mbox_fp);
191                         g_unlink(tmp_file);
192                         return -1;
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                         g_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, NULL, TRUE)) < 0) {
207                         fclose(mbox_fp);
208                         g_unlink(tmp_file);
209                         g_free(tmp_file);
210                         return -1;
211                 }
212
213                 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
214                 if (!apply_filter || !procmsg_msginfo_filter(msginfo)) {
215                         folder_item_move_msg(dest, msginfo);
216                         procmsg_msginfo_free(msginfo);
217                 } else
218                         to_filter = g_slist_append(to_filter, msginfo);
219
220                 msgs++;
221         } while (more);
222
223         filtering_move_and_copy_msgs(to_filter);
224         for (cur = to_filter; cur; cur = g_slist_next(cur)) {
225                 MsgInfo *info = (MsgInfo *)cur->data;
226                 procmsg_msginfo_free(info);
227         }
228
229         folder_item_update_thaw();
230         
231         g_free(tmp_file);
232         fclose(mbox_fp);
233         debug_print("%d messages found.\n", msgs);
234
235         return msgs;
236 }
237
238 gint lock_mbox(const gchar *base, LockType type)
239 {
240 #ifdef G_OS_UNIX
241         gint retval = 0;
242
243         if (type == LOCK_FILE) {
244                 gchar *lockfile, *locklink;
245                 gint retry = 0;
246                 FILE *lockfp;
247
248                 lockfile = g_strdup_printf("%s.%d", base, getpid());
249                 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
250                         FILE_OP_ERROR(lockfile, "fopen");
251                         g_warning("can't create lock file %s\n", lockfile);
252                         g_warning("use 'flock' instead of 'file' if possible.\n");
253                         g_free(lockfile);
254                         return -1;
255                 }
256
257                 fprintf(lockfp, "%d\n", getpid());
258                 fclose(lockfp);
259
260                 locklink = g_strconcat(base, ".lock", NULL);
261                 while (link(lockfile, locklink) < 0) {
262                         FILE_OP_ERROR(lockfile, "link");
263                         if (retry >= 5) {
264                                 g_warning("can't create %s\n", lockfile);
265                                 g_unlink(lockfile);
266                                 g_free(lockfile);
267                                 return -1;
268                         }
269                         if (retry == 0)
270                                 g_warning("mailbox is owned by another"
271                                             " process, waiting...\n");
272                         retry++;
273                         sleep(5);
274                 }
275                 g_unlink(lockfile);
276                 g_free(lockfile);
277         } else if (type == LOCK_FLOCK) {
278                 gint lockfd;
279
280 #if HAVE_FLOCK
281                 if ((lockfd = open(base, O_RDONLY)) < 0) {
282 #else
283                 if ((lockfd = open(base, O_RDWR)) < 0) {
284 #endif
285                         FILE_OP_ERROR(base, "open");
286                         return -1;
287                 }
288 #if HAVE_FLOCK
289                 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
290                         perror("flock");
291 #else
292 #if HAVE_LOCKF
293                 if (lockf(lockfd, F_TLOCK, 0) < 0) {
294                         perror("lockf");
295 #else
296                 {
297 #endif
298 #endif /* HAVE_FLOCK */
299                         g_warning("can't lock %s\n", base);
300                         if (close(lockfd) < 0)
301                                 perror("close");
302                         return -1;
303                 }
304                 retval = lockfd;
305         } else {
306                 g_warning("invalid lock type\n");
307                 return -1;
308         }
309
310         return retval;
311 #else
312         return -1;
313 #endif /* G_OS_UNIX */
314 }
315
316 gint unlock_mbox(const gchar *base, gint fd, LockType type)
317 {
318         if (type == LOCK_FILE) {
319                 gchar *lockfile;
320
321                 lockfile = g_strconcat(base, ".lock", NULL);
322                 if (g_unlink(lockfile) < 0) {
323                         FILE_OP_ERROR(lockfile, "unlink");
324                         g_free(lockfile);
325                         return -1;
326                 }
327                 g_free(lockfile);
328
329                 return 0;
330         } else if (type == LOCK_FLOCK) {
331 #if HAVE_FLOCK
332                 if (flock(fd, LOCK_UN) < 0) {
333                         perror("flock");
334 #else
335 #if HAVE_LOCKF
336                 if (lockf(fd, F_ULOCK, 0) < 0) {
337                         perror("lockf");
338 #else
339                 {
340 #endif
341 #endif /* HAVE_FLOCK */
342                         g_warning("can't unlock %s\n", base);
343                         if (close(fd) < 0)
344                                 perror("close");
345                         return -1;
346                 }
347
348                 if (close(fd) < 0) {
349                         perror("close");
350                         return -1;
351                 }
352
353                 return 0;
354         }
355
356         g_warning("invalid lock type\n");
357         return -1;
358 }
359
360 gint copy_mbox(const gchar *src, const gchar *dest)
361 {
362         return copy_file(src, dest, TRUE);
363 }
364
365 void empty_mbox(const gchar *mbox)
366 {
367         FILE *fp;
368
369         if ((fp = g_fopen(mbox, "wb")) == NULL) {
370                 FILE_OP_ERROR(mbox, "fopen");
371                 g_warning("can't truncate mailbox to zero.\n");
372                 return;
373         }
374         fclose(fp);
375 }
376
377 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
378 {
379         GSList *cur;
380         MsgInfo *msginfo;
381         FILE *msg_fp;
382         FILE *mbox_fp;
383         gchar buf[BUFFSIZE];
384
385         if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
386                 if (alertpanel_full(_("Overwrite mbox file"),
387                                                         _("This file already exists. Do you want to overwrite it?"),
388                                                         _("Overwrite"), GTK_STOCK_CANCEL, NULL, FALSE,
389                                                         NULL, ALERT_WARNING, G_ALERTALTERNATE)
390                         == G_ALERTALTERNATE) {
391                 return -1;
392         }
393         }
394
395         if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
396                 FILE_OP_ERROR(mbox, "fopen");
397                 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
398                 return -1;
399         }
400
401         for (cur = mlist; cur != NULL; cur = cur->next) {
402                 msginfo = (MsgInfo *)cur->data;
403                 int len;
404
405                 msg_fp = procmsg_open_message(msginfo);
406                 if (!msg_fp) {
407                         procmsg_msginfo_free(msginfo);
408                         continue;
409                 }
410
411                 strncpy2(buf,
412                          msginfo->from ? msginfo->from :
413                          cur_account && cur_account->address ?
414                          cur_account->address : "unknown",
415                          sizeof(buf));
416                 extract_address(buf);
417
418                 fprintf(mbox_fp, "From %s %s",
419                         buf, ctime(&msginfo->date_t));
420
421                 buf[0] = '\0';
422                 
423                 /* write email to mboxrc */
424                 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
425                         /* quote any From, >From, >>From, etc., according to mbox format specs */
426                         int offset;
427
428                         offset = 0;
429                         /* detect leading '>' char(s) */
430                         while ((buf[offset] == '>')) {
431                                 offset++;
432                         }
433                         if (!strncmp(buf+offset, "From ", 5))
434                                 fputc('>', mbox_fp);
435                         fputs(buf, mbox_fp);
436                 }
437
438                 /* force last line to end w/ a newline */
439                 len = strlen(buf);
440                 if (len > 0) {
441                         len--;
442                         if ((buf[len] != '\n') && (buf[len] != '\r'))
443                                 fputc('\n', mbox_fp);
444                 }
445
446                 /* add a trailing empty line */
447                 fputc('\n', mbox_fp);
448
449                 fclose(msg_fp);
450                 procmsg_msginfo_free(msginfo);
451         }
452         
453         fclose(mbox_fp);
454
455         return 0;
456 }
457
458 /* read all messages in SRC, and store them into one MBOX file. */
459 gint export_to_mbox(FolderItem *src, const gchar *mbox)
460 {
461         GSList *mlist;
462         
463         g_return_val_if_fail(src != NULL, -1);
464         g_return_val_if_fail(src->folder != NULL, -1);
465         g_return_val_if_fail(mbox != NULL, -1);
466
467         debug_print("Exporting messages from %s into %s...\n",
468                     src->path, mbox);
469
470         mlist = folder_item_get_msg_list(src);
471
472         export_list_to_mbox(mlist, mbox);
473
474         procmsg_msg_list_free(mlist);
475
476         return 0;
477 }