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