1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
3 * Authors: Michael Zucchi <notzed@ximian.com>
4 * Jeffrey Stedfast <fejj@ximian.com>
6 * Copyright (C) 1999, 2003 Ximian Inc.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU Lesser General Public
10 * License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
33 #include <sys/types.h>
36 #include <glib/gi18n-lib.h>
37 #include <glib/gstdio.h>
39 #include "camel/camel-data-wrapper.h"
40 #include "camel/camel-exception.h"
41 #include "camel/camel-mime-filter-from.h"
42 #include "camel/camel-mime-message.h"
43 #include "camel/camel-private.h"
44 #include "camel/camel-stream-filter.h"
45 #include "camel/camel-stream-fs.h"
47 #include "camel-mbox-folder.h"
48 #include "camel-mbox-store.h"
49 #include "camel-mbox-summary.h"
55 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
57 static CamelLocalFolderClass *parent_class = NULL;
59 /* Returns the class for a CamelMboxFolder */
60 #define CMBOXF_CLASS(so) CAMEL_MBOX_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
61 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
62 #define CMBOXS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
64 static int mbox_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex);
65 static void mbox_unlock(CamelLocalFolder *lf);
67 static void mbox_append_message(CamelFolder *folder, CamelMimeMessage * message, const CamelMessageInfo * info, char **appended_uid, CamelException *ex);
68 static CamelMimeMessage *mbox_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex);
69 static CamelLocalSummary *mbox_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index);
71 static void mbox_finalise(CamelObject * object);
74 camel_mbox_folder_class_init(CamelMboxFolderClass * camel_mbox_folder_class)
76 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mbox_folder_class);
77 CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_mbox_folder_class;
79 parent_class = (CamelLocalFolderClass *)camel_type_get_global_classfuncs(camel_local_folder_get_type());
81 /* virtual method definition */
83 /* virtual method overload */
84 camel_folder_class->append_message = mbox_append_message;
85 camel_folder_class->get_message = mbox_get_message;
87 lclass->create_summary = mbox_create_summary;
88 lclass->lock = mbox_lock;
89 lclass->unlock = mbox_unlock;
93 mbox_init(gpointer object, gpointer klass)
95 /*CamelFolder *folder = object;*/
96 CamelMboxFolder *mbox_folder = object;
98 mbox_folder->lockfd = -1;
102 mbox_finalise(CamelObject * object)
104 CamelMboxFolder *mbox_folder = (CamelMboxFolder *)object;
106 g_assert(mbox_folder->lockfd == -1);
109 CamelType camel_mbox_folder_get_type(void)
111 static CamelType camel_mbox_folder_type = CAMEL_INVALID_TYPE;
113 if (camel_mbox_folder_type == CAMEL_INVALID_TYPE) {
114 camel_mbox_folder_type = camel_type_register(CAMEL_LOCAL_FOLDER_TYPE, "CamelMboxFolder",
115 sizeof(CamelMboxFolder),
116 sizeof(CamelMboxFolderClass),
117 (CamelObjectClassInitFunc) camel_mbox_folder_class_init,
119 (CamelObjectInitFunc) mbox_init,
120 (CamelObjectFinalizeFunc) mbox_finalise);
123 return camel_mbox_folder_type;
127 camel_mbox_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
131 d(printf("Creating mbox folder: %s in %s\n", full_name, camel_local_store_get_toplevel_dir((CamelLocalStore *)parent_store)));
133 folder = (CamelFolder *)camel_object_new(CAMEL_MBOX_FOLDER_TYPE);
134 folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
135 parent_store, full_name, flags, ex);
140 static CamelLocalSummary *mbox_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index)
142 return (CamelLocalSummary *)camel_mbox_summary_new((CamelFolder *)lf, path, folder, index);
145 static int mbox_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex)
148 CamelMboxFolder *mf = (CamelMboxFolder *)lf;
150 /* make sure we have matching unlocks for locks, camel-local-folder class should enforce this */
151 g_assert(mf->lockfd == -1);
153 mf->lockfd = open(lf->folder_path, O_RDWR, 0);
154 if (mf->lockfd == -1) {
155 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
156 _("Cannot create folder lock on %s: %s"),
157 lf->folder_path, g_strerror (errno));
161 if (camel_lock_folder(lf->folder_path, mf->lockfd, type, ex) == -1) {
170 static void mbox_unlock(CamelLocalFolder *lf)
173 CamelMboxFolder *mf = (CamelMboxFolder *)lf;
175 g_assert(mf->lockfd != -1);
176 camel_unlock_folder(lf->folder_path, mf->lockfd);
183 mbox_append_message(CamelFolder *folder, CamelMimeMessage * message, const CamelMessageInfo * info, char **appended_uid, CamelException *ex)
185 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
186 CamelStream *output_stream = NULL, *filter_stream = NULL;
187 CamelMimeFilter *filter_from;
188 CamelMboxSummary *mbs = (CamelMboxSummary *)folder->summary;
189 CamelMessageInfo *mi;
190 char *fromline = NULL;
196 /* If we can't lock, dont do anything */
197 if (camel_local_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
200 d(printf("Appending message\n"));
202 /* first, check the summary is correct (updates folder_size too) */
203 retval = camel_local_summary_check ((CamelLocalSummary *)folder->summary, lf->changes, ex);
207 /* add it to the summary/assign the uid, etc */
208 mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
212 d(printf("Appending message: uid is %s\n", camel_message_info_uid(mi)));
214 output_stream = camel_stream_fs_new_with_name(lf->folder_path, O_WRONLY | O_APPEND | O_LARGEFILE, 0666);
215 if (output_stream == NULL) {
216 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
217 _("Cannot open mailbox: %s: %s\n"),
218 lf->folder_path, g_strerror (errno));
222 /* and we need to set the frompos/XEV explicitly */
223 ((CamelMboxMessageInfo *)mi)->frompos = mbs->folder_size;
225 xev = camel_local_summary_encode_x_evolution((CamelLocalSummary *)folder->summary, mi);
227 /* the x-ev header should match the 'current' flags, no problem, so store as much */
228 camel_medium_set_header((CamelMedium *)message, "X-Evolution", xev);
229 mi->flags &= ~ CAMEL_MESSAGE_FOLDER_NOXEV|CAMEL_MESSAGE_FOLDER_FLAGGED;
234 /* we must write this to the non-filtered stream ... */
235 fromline = camel_mime_message_build_mbox_from(message);
236 if (camel_stream_write(output_stream, fromline, strlen(fromline)) == -1)
239 /* and write the content to the filtering stream, that translates '\nFrom' into '\n>From' */
240 filter_stream = (CamelStream *) camel_stream_filter_new_with_stream(output_stream);
241 filter_from = (CamelMimeFilter *) camel_mime_filter_from_new();
242 camel_stream_filter_add((CamelStreamFilter *) filter_stream, filter_from);
243 camel_object_unref (filter_from);
245 if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *) message, filter_stream) == -1 ||
246 camel_stream_write (filter_stream, "\n", 1) == -1 ||
247 camel_stream_flush (filter_stream) == -1)
250 /* filter stream ref's the output stream itself, so we need to unref it too */
251 camel_object_unref (filter_stream);
252 camel_object_unref (output_stream);
255 /* now we 'fudge' the summary to tell it its uptodate, because its idea of uptodate has just changed */
256 /* the stat really shouldn't fail, we just wrote to it */
257 if (g_stat (lf->folder_path, &st) == 0) {
258 ((CamelFolderSummary *) mbs)->time = st.st_mtime;
259 mbs->folder_size = st.st_size;
262 /* unlock as soon as we can */
263 camel_local_folder_unlock(lf);
265 if (camel_folder_change_info_changed(lf->changes)) {
266 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
267 camel_folder_change_info_clear(lf->changes);
271 *appended_uid = g_strdup(camel_message_info_uid(mi));
277 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
278 _("Mail append canceled"));
280 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
281 _("Cannot append message to mbox file: %s: %s"),
282 lf->folder_path, g_strerror (errno));
285 /* reset the file to original size */
287 retval = ftruncate (((CamelStreamFs *) output_stream)->fd, mbs->folder_size);
288 } while (retval == -1 && errno == EINTR);
290 camel_object_unref (output_stream);
294 camel_object_unref (filter_stream);
298 /* remove the summary info so we are not out-of-sync with the mbox */
299 camel_folder_summary_remove_uid (CAMEL_FOLDER_SUMMARY (mbs), camel_message_info_uid (mi));
301 /* and tell the summary it's up-to-date */
302 if (g_stat (lf->folder_path, &st) == 0) {
303 ((CamelFolderSummary *) mbs)->time = st.st_mtime;
304 mbs->folder_size = st.st_size;
308 /* make sure we unlock the folder - before we start triggering events into appland */
309 camel_local_folder_unlock(lf);
311 /* cascade the changes through, anyway, if there are any outstanding */
312 if (camel_folder_change_info_changed(lf->changes)) {
313 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
314 camel_folder_change_info_clear(lf->changes);
318 static CamelMimeMessage *
319 mbox_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex)
321 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
322 CamelMimeMessage *message = NULL;
323 CamelMboxMessageInfo *info;
324 CamelMimeParser *parser = NULL;
329 d(printf("Getting message %s\n", uid));
331 /* lock the folder first, burn if we can't, need write lock for summary check */
332 if (camel_local_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
335 /* check for new messages always */
336 if (camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1) {
337 camel_local_folder_unlock(lf);
342 /* get the message summary info */
343 info = (CamelMboxMessageInfo *) camel_folder_summary_uid(folder->summary, uid);
346 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
347 _("Cannot get message: %s from folder %s\n %s"),
348 uid, lf->folder_path, _("No such message"));
352 /* no frompos, its an error in the library (and we can't do anything with it) */
353 g_assert(info->frompos != -1);
355 frompos = info->frompos;
356 camel_message_info_free((CamelMessageInfo *)info);
358 /* we use an fd instead of a normal stream here - the reason is subtle, camel_mime_part will cache
359 the whole message in memory if the stream is non-seekable (which it is when built from a parser
360 with no stream). This means we dont have to lock the mbox for the life of the message, but only
361 while it is being created. */
363 fd = g_open(lf->folder_path, O_LARGEFILE | O_RDONLY | O_BINARY, 0);
365 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
366 _("Cannot get message: %s from folder %s\n %s"),
367 uid, lf->folder_path, g_strerror (errno));
371 /* we use a parser to verify the message is correct, and in the correct position */
372 parser = camel_mime_parser_new();
373 camel_mime_parser_init_with_fd(parser, fd);
374 camel_mime_parser_scan_from(parser, TRUE);
376 camel_mime_parser_seek(parser, frompos, SEEK_SET);
377 if (camel_mime_parser_step(parser, NULL, NULL) != CAMEL_MIME_PARSER_STATE_FROM
378 || camel_mime_parser_tell_start_from(parser) != frompos) {
380 g_warning("Summary doesn't match the folder contents! eek!\n"
381 " expecting offset %ld got %ld, state = %d", (long int)frompos,
382 (long int)camel_mime_parser_tell_start_from(parser),
383 camel_mime_parser_state(parser));
385 camel_object_unref((CamelObject *)parser);
390 camel_local_summary_check_force((CamelLocalSummary *)folder->summary);
391 retval = camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex);
396 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID,
397 _("Cannot get message: %s from folder %s\n %s"), uid, lf->folder_path,
398 _("The folder appears to be irrecoverably corrupted."));
402 message = camel_mime_message_new();
403 if (camel_mime_part_construct_from_parser((CamelMimePart *)message, parser) == -1) {
404 camel_exception_setv(ex, errno==EINTR?CAMEL_EXCEPTION_USER_CANCEL:CAMEL_EXCEPTION_SYSTEM,
405 _("Cannot get message: %s from folder %s\n %s"), uid, lf->folder_path,
406 _("Message construction failed."));
407 camel_object_unref((CamelObject *)message);
412 camel_medium_remove_header((CamelMedium *)message, "X-Evolution");
414 /* and unlock now we're finished with it */
415 camel_local_folder_unlock(lf);
418 camel_object_unref((CamelObject *)parser);
420 /* use the opportunity to notify of changes (particularly if we had a rebuild) */
421 if (camel_folder_change_info_changed(lf->changes)) {
422 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
423 camel_folder_change_info_clear(lf->changes);