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