rever to filter from inbox approach
[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                 return -1; \
56         } \
57 }
58
59 gint proc_mbox(FolderItem *dest, const gchar *mbox, GHashTable *folder_table)
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, "r")) == 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                 gchar *startp, *endp, *rpath;
104                 gint empty_line;
105                 gint val;
106                 gboolean is_next_msg = FALSE;
107                 gint msgnum;
108
109                 if ((tmp_fp = fopen(tmp_file, "w")) == NULL) {
110                         FILE_OP_ERROR(tmp_file, "fopen");
111                         g_warning(_("can't open temporary file\n"));
112                         fclose(mbox_fp);
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                         return -1;
202                 }
203
204                 if (folder_table) {
205                         if (global_processing == NULL) {
206                                 /* old filtering */
207                                 dropfolder = filter_get_dest_folder
208                                         (prefs_common.fltlist, tmp_file);
209                                 if (!dropfolder ||
210                                     !strcmp(dropfolder->path, FILTER_NOT_RECEIVE))
211                                         dropfolder = dest;
212                                 val = GPOINTER_TO_INT(g_hash_table_lookup
213                                                       (folder_table, dropfolder));
214                                 if (val == 0) {
215                                         folder_item_scan(dropfolder);
216                                         g_hash_table_insert(folder_table, dropfolder,
217                                                             GINT_TO_POINTER(1));
218                                 }
219                         }
220                         else {
221                                 /* new filtering */
222                                 dropfolder = dest;
223                         }
224                 } else
225                         dropfolder = dest;
226
227                         
228                 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, TRUE)) < 0) {
229                         fclose(mbox_fp);
230                         unlink(tmp_file);
231                         return -1;
232                 }
233                 folder_item_scan(dropfolder);
234
235                 if (global_processing) {
236                         /* new filtering */
237                         if (folder_table) {
238                                 filter_message(global_processing, dropfolder,
239                                                msgnum, folder_table);
240                         }
241                 }
242
243                 msgs++;
244         } while (from_line[0] != '\0');
245
246         fclose(mbox_fp);
247         debug_print(_("%d messages found.\n"), msgs);
248
249         return msgs;
250 }
251
252 gint lock_mbox(const gchar *base, LockType type)
253 {
254         gint retval = 0;
255
256         if (type == LOCK_FILE) {
257                 gchar *lockfile, *locklink;
258                 gint retry = 0;
259                 FILE *lockfp;
260
261                 lockfile = g_strdup_printf("%s.%d", base, getpid());
262                 if ((lockfp = fopen(lockfile, "w")) == NULL) {
263                         FILE_OP_ERROR(lockfile, "fopen");
264                         g_warning(_("can't create lock file %s\n"), lockfile);
265                         g_warning(_("use 'flock' instead of 'file' if possible.\n"));
266                         g_free(lockfile);
267                         return -1;
268                 }
269
270                 fprintf(lockfp, "%d\n", getpid());
271                 fclose(lockfp);
272
273                 locklink = g_strconcat(base, ".lock", NULL);
274                 while (link(lockfile, locklink) < 0) {
275                         FILE_OP_ERROR(lockfile, "link");
276                         if (retry >= 5) {
277                                 g_warning(_("can't create %s\n"), lockfile);
278                                 unlink(lockfile);
279                                 g_free(lockfile);
280                                 return -1;
281                         }
282                         if (retry == 0)
283                                 g_warning(_("mailbox is owned by another"
284                                             " process, waiting...\n"));
285                         retry++;
286                         sleep(5);
287                 }
288                 unlink(lockfile);
289                 g_free(lockfile);
290         } else if (type == LOCK_FLOCK) {
291                 gint lockfd;
292
293 #if HAVE_FLOCK
294                 if ((lockfd = open(base, O_RDONLY)) < 0) {
295 #else
296                 if ((lockfd = open(base, O_RDWR)) < 0) {
297 #endif
298                         FILE_OP_ERROR(base, "open");
299                         return -1;
300                 }
301 #if HAVE_FLOCK
302                 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
303                         perror("flock");
304 #else
305 #if HAVE_LOCKF
306                 if (lockf(lockfd, F_TLOCK, 0) < 0) {
307                         perror("lockf");
308 #else
309                 {
310 #endif
311 #endif /* HAVE_FLOCK */
312                         g_warning(_("can't lock %s\n"), base);
313                         if (close(lockfd) < 0)
314                                 perror("close");
315                         return -1;
316                 }
317                 retval = lockfd;
318         } else {
319                 g_warning(_("invalid lock type\n"));
320                 return -1;
321         }
322
323         return retval;
324 }
325
326 gint unlock_mbox(const gchar *base, gint fd, LockType type)
327 {
328         if (type == LOCK_FILE) {
329                 gchar *lockfile;
330
331                 lockfile = g_strconcat(base, ".lock", NULL);
332                 if (unlink(lockfile) < 0) {
333                         FILE_OP_ERROR(lockfile, "unlink");
334                         g_free(lockfile);
335                         return -1;
336                 }
337                 g_free(lockfile);
338
339                 return 0;
340         } else if (type == LOCK_FLOCK) {
341 #if HAVE_FLOCK
342                 if (flock(fd, LOCK_UN) < 0) {
343                         perror("flock");
344 #else
345 #if HAVE_LOCKF
346                 if (lockf(fd, F_ULOCK, 0) < 0) {
347                         perror("lockf");
348 #else
349                 {
350 #endif
351 #endif /* HAVE_FLOCK */
352                         g_warning(_("can't unlock %s\n"), base);
353                         if (close(fd) < 0)
354                                 perror("close");
355                         return -1;
356                 }
357
358                 if (close(fd) < 0) {
359                         perror("close");
360                         return -1;
361                 }
362
363                 return 0;
364         }
365
366         g_warning(_("invalid lock type\n"));
367         return -1;
368 }
369
370 gint copy_mbox(const gchar *src, const gchar *dest)
371 {
372         return copy_file(src, dest);
373 }
374
375 void empty_mbox(const gchar *mbox)
376 {
377         if (truncate(mbox, 0) < 0) {
378                 FILE *fp;
379
380                 FILE_OP_ERROR(mbox, "truncate");
381                 if ((fp = fopen(mbox, "w")) == NULL) {
382                         FILE_OP_ERROR(mbox, "fopen");
383                         g_warning(_("can't truncate mailbox to zero.\n"));
384                         return;
385                 }
386                 fclose(fp);
387         }
388 }
389
390 /* read all messages in SRC, and store them into one MBOX file. */
391 gint export_to_mbox(FolderItem *src, const gchar *mbox)
392 {
393         GSList *mlist;
394         GSList *cur;
395         MsgInfo *msginfo;
396         FILE *msg_fp;
397         FILE *mbox_fp;
398         gchar buf[BUFFSIZE];
399
400         g_return_val_if_fail(src != NULL, -1);
401         g_return_val_if_fail(src->folder != NULL, -1);
402         g_return_val_if_fail(mbox != NULL, -1);
403
404         debug_print(_("Exporting messages from %s into %s...\n"),
405                     src->path, mbox);
406
407         if ((mbox_fp = fopen(mbox, "w")) == NULL) {
408                 FILE_OP_ERROR(mbox, "fopen");
409                 return -1;
410         }
411
412         mlist = src->folder->get_msg_list(src->folder, src, TRUE);
413
414         for (cur = mlist; cur != NULL; cur = cur->next) {
415                 msginfo = (MsgInfo *)cur->data;
416
417                 msg_fp = procmsg_open_message(msginfo);
418                 if (!msg_fp) {
419                         procmsg_msginfo_free(msginfo);
420                         continue;
421                 }
422
423                 strncpy2(buf,
424                          msginfo->from ? msginfo->from :
425                          cur_account && cur_account->address ?
426                          cur_account->address : "unknown",
427                          sizeof(buf));
428                 extract_address(buf);
429
430                 fprintf(mbox_fp, "From %s %s",
431                         buf, ctime(&msginfo->date_t));
432
433                 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
434                         if (!strncmp(buf, "From ", 5))
435                                 fputc('>', mbox_fp);
436                         fputs(buf, mbox_fp);
437                 }
438                 fputc('\n', mbox_fp);
439
440                 fclose(msg_fp);
441                 procmsg_msginfo_free(msginfo);
442         }
443
444         g_slist_free(mlist);
445
446         fclose(mbox_fp);
447
448         return 0;
449 }