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