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