1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
3 * Authors: Michael Zucchi <notzed@ximian.com>
5 * Copyright (C) 1999, 2000 Ximian Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/types.h>
35 #include "camel-mbox-folder.h"
36 #include "camel-mbox-store.h"
37 #include "string-utils.h"
38 #include "camel-stream-fs.h"
39 #include "camel-mbox-summary.h"
40 #include "camel-data-wrapper.h"
41 #include "camel-mime-message.h"
42 #include "camel-stream-filter.h"
43 #include "camel-mime-filter-from.h"
44 #include "camel-exception.h"
46 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
48 static CamelLocalFolderClass *parent_class = NULL;
50 /* Returns the class for a CamelMboxFolder */
51 #define CMBOXF_CLASS(so) CAMEL_MBOX_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
52 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
53 #define CMBOXS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
55 static int mbox_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex);
56 static void mbox_unlock(CamelLocalFolder *lf);
59 static void mbox_set_message_flags(CamelFolder *folder, const char *uid, guint32 flags, guint32 set);
62 static void mbox_set_message_user_flag(CamelFolder *folder, const char *uid, const char *name, gboolean value);
63 static void mbox_set_message_user_tag(CamelFolder *folder, const char *uid, const char *name, const char *value);
65 static void mbox_append_message(CamelFolder *folder, CamelMimeMessage * message, const CamelMessageInfo * info, char **appended_uid, CamelException *ex);
66 static CamelMimeMessage *mbox_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex);
67 static CamelLocalSummary *mbox_create_summary(const char *path, const char *folder, CamelIndex *index);
69 static void mbox_finalise(CamelObject * object);
72 camel_mbox_folder_class_init(CamelMboxFolderClass * camel_mbox_folder_class)
74 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mbox_folder_class);
75 CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_mbox_folder_class;
77 parent_class = (CamelLocalFolderClass *)camel_type_get_global_classfuncs(camel_local_folder_get_type());
79 /* virtual method definition */
81 /* virtual method overload */
82 camel_folder_class->append_message = mbox_append_message;
83 camel_folder_class->get_message = mbox_get_message;
86 camel_folder_class->set_message_flags = mbox_set_message_flags;
88 camel_folder_class->set_message_user_flag = mbox_set_message_user_flag;
89 camel_folder_class->set_message_user_tag = mbox_set_message_user_tag;
91 lclass->create_summary = mbox_create_summary;
92 lclass->lock = mbox_lock;
93 lclass->unlock = mbox_unlock;
97 mbox_init(gpointer object, gpointer klass)
99 /*CamelFolder *folder = object;*/
100 CamelMboxFolder *mbox_folder = object;
102 mbox_folder->lockfd = -1;
106 mbox_finalise(CamelObject * object)
108 CamelMboxFolder *mbox_folder = (CamelMboxFolder *)object;
110 g_assert(mbox_folder->lockfd == -1);
113 CamelType camel_mbox_folder_get_type(void)
115 static CamelType camel_mbox_folder_type = CAMEL_INVALID_TYPE;
117 if (camel_mbox_folder_type == CAMEL_INVALID_TYPE) {
118 camel_mbox_folder_type = camel_type_register(CAMEL_LOCAL_FOLDER_TYPE, "CamelMboxFolder",
119 sizeof(CamelMboxFolder),
120 sizeof(CamelMboxFolderClass),
121 (CamelObjectClassInitFunc) camel_mbox_folder_class_init,
123 (CamelObjectInitFunc) mbox_init,
124 (CamelObjectFinalizeFunc) mbox_finalise);
127 return camel_mbox_folder_type;
131 camel_mbox_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
135 d(printf("Creating mbox folder: %s in %s\n", full_name, camel_local_store_get_toplevel_dir((CamelLocalStore *)parent_store)));
137 folder = (CamelFolder *)camel_object_new(CAMEL_MBOX_FOLDER_TYPE);
138 folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
139 parent_store, full_name, flags, ex);
144 static CamelLocalSummary *mbox_create_summary(const char *path, const char *folder, CamelIndex *index)
146 return (CamelLocalSummary *)camel_mbox_summary_new(path, folder, index);
149 static int mbox_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex)
151 CamelMboxFolder *mf = (CamelMboxFolder *)lf;
153 /* make sure we have matching unlocks for locks, camel-local-folder class should enforce this */
154 g_assert(mf->lockfd == -1);
156 mf->lockfd = open(lf->folder_path, O_RDWR, 0);
157 if (mf->lockfd == -1) {
158 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
159 _("Cannot create folder lock on %s: %s"),
160 lf->folder_path, g_strerror (errno));
164 if (camel_lock_folder(lf->folder_path, mf->lockfd, type, ex) == -1) {
173 static void mbox_unlock(CamelLocalFolder *lf)
175 CamelMboxFolder *mf = (CamelMboxFolder *)lf;
177 g_assert(mf->lockfd != -1);
178 camel_unlock_folder(lf->folder_path, mf->lockfd);
184 mbox_append_message(CamelFolder *folder, CamelMimeMessage * message, const CamelMessageInfo * info, char **appended_uid, CamelException *ex)
186 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
187 CamelStream *output_stream = NULL, *filter_stream = NULL;
188 CamelMimeFilter *filter_from = NULL;
189 CamelMboxSummary *mbs = (CamelMboxSummary *)folder->summary;
190 CamelMessageInfo *mi;
191 char *fromline = NULL;
197 /* If we can't lock, dont do anything */
198 if (camel_local_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
201 d(printf("Appending message\n"));
203 /* first, check the summary is correct (updates folder_size too) */
204 retval = camel_local_summary_check ((CamelLocalSummary *)folder->summary, lf->changes, ex);
208 /* add it to the summary/assign the uid, etc */
209 mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
213 d(printf("Appending message: uid is %s\n", camel_message_info_uid(mi)));
215 output_stream = camel_stream_fs_new_with_name(lf->folder_path, O_WRONLY|O_APPEND, 0600);
216 if (output_stream == NULL) {
217 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
218 _("Cannot open mailbox: %s: %s\n"),
219 lf->folder_path, g_strerror (errno));
223 /* and we need to set the frompos/XEV explicitly */
224 ((CamelMboxMessageInfo *)mi)->frompos = mbs->folder_size;
226 xev = camel_local_summary_encode_x_evolution((CamelLocalSummary *)folder->summary, mi);
228 /* the x-ev header should match the 'current' flags, no problem, so store as much */
229 camel_medium_set_header((CamelMedium *)message, "X-Evolution", xev);
230 mi->flags &= ~ CAMEL_MESSAGE_FOLDER_NOXEV|CAMEL_MESSAGE_FOLDER_FLAGGED;
235 /* we must write this to the non-filtered stream ... */
236 fromline = camel_mime_message_build_mbox_from(message);
237 if (camel_stream_write(output_stream, fromline, strlen(fromline)) == -1)
240 /* and write the content to the filtering stream, that translates '\nFrom' into '\n>From' */
241 filter_stream = (CamelStream *) camel_stream_filter_new_with_stream(output_stream);
242 filter_from = (CamelMimeFilter *) camel_mime_filter_from_new();
243 camel_stream_filter_add((CamelStreamFilter *) filter_stream, filter_from);
244 if (camel_data_wrapper_write_to_stream((CamelDataWrapper *)message, filter_stream) == -1
245 || camel_stream_write(filter_stream, "\n", 1) == -1
246 || camel_stream_close(filter_stream) == -1)
249 /* filter stream ref's the output stream itself, so we need to unref it too */
250 camel_object_unref((CamelObject *)filter_from);
251 camel_object_unref((CamelObject *)filter_stream);
252 camel_object_unref((CamelObject *)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 (stat(lf->folder_path, &st) == 0) {
258 mbs->folder_size = st.st_size;
259 ((CamelFolderSummary *)mbs)->time = st.st_mtime;
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 cancelled"));
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 camel_object_unref(CAMEL_OBJECT(filter_stream));
288 camel_object_unref(CAMEL_OBJECT(output_stream));
291 camel_object_unref(CAMEL_OBJECT(filter_from));
295 /* reset the file to original size */
296 fd = open(lf->folder_path, O_WRONLY, 0600);
298 ftruncate(fd, mbs->folder_size);
302 /* remove the summary info so we are not out-of-sync with the mbox */
303 camel_folder_summary_remove_uid (CAMEL_FOLDER_SUMMARY (mbs), camel_message_info_uid (mi));
305 /* and tell the summary its uptodate */
306 if (stat(lf->folder_path, &st) == 0) {
307 mbs->folder_size = st.st_size;
308 ((CamelFolderSummary *)mbs)->time = st.st_mtime;
312 /* make sure we unlock the folder - before we start triggering events into appland */
313 camel_local_folder_unlock(lf);
315 /* cascade the changes through, anyway, if there are any outstanding */
316 if (camel_folder_change_info_changed(lf->changes)) {
317 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
318 camel_folder_change_info_clear(lf->changes);
322 static CamelMimeMessage *
323 mbox_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex)
325 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
326 CamelMimeMessage *message = NULL;
327 CamelMboxMessageInfo *info;
328 CamelMimeParser *parser = NULL;
333 d(printf("Getting message %s\n", uid));
335 /* lock the folder first, burn if we can't, need write lock for summary check */
336 if (camel_local_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
339 /* check for new messages always */
340 if (camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1) {
341 camel_local_folder_unlock(lf);
346 /* get the message summary info */
347 info = (CamelMboxMessageInfo *) camel_folder_summary_uid(folder->summary, uid);
350 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
351 _("Cannot get message: %s\n %s"), uid, _("No such message"));
355 /* no frompos, its an error in the library (and we can't do anything with it) */
356 g_assert(info->frompos != -1);
358 frompos = info->frompos;
359 camel_folder_summary_info_free(folder->summary, (CamelMessageInfo *)info);
361 /* we use an fd instead of a normal stream here - the reason is subtle, camel_mime_part will cache
362 the whole message in memory if the stream is non-seekable (which it is when built from a parser
363 with no stream). This means we dont have to lock the mbox for the life of the message, but only
364 while it is being created. */
366 fd = open(lf->folder_path, O_RDONLY);
368 camel_exception_setv (ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
369 _("Cannot get message: %s from folder %s\n %s"),
370 uid, lf->folder_path, g_strerror (errno));
374 /* we use a parser to verify the message is correct, and in the correct position */
375 parser = camel_mime_parser_new();
376 camel_mime_parser_init_with_fd(parser, fd);
377 camel_mime_parser_scan_from(parser, TRUE);
379 camel_mime_parser_seek(parser, frompos, SEEK_SET);
380 if (camel_mime_parser_step(parser, NULL, NULL) != HSCAN_FROM
381 || camel_mime_parser_tell_start_from(parser) != frompos) {
383 g_warning("Summary doesn't match the folder contents! eek!\n"
384 " expecting offset %ld got %ld, state = %d", (long int)frompos,
385 (long int)camel_mime_parser_tell_start_from(parser),
386 camel_mime_parser_state(parser));
388 camel_object_unref((CamelObject *)parser);
393 camel_local_summary_check_force((CamelLocalSummary *)folder->summary);
394 retval = camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex);
399 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
400 _("Cannot get message: %s from folder %s\n %s"), uid, lf->folder_path,
401 _("The folder appears to be irrecoverably corrupted."));
405 message = camel_mime_message_new();
406 if (camel_mime_part_construct_from_parser((CamelMimePart *)message, parser) == -1) {
407 camel_exception_setv(ex, errno==EINTR?CAMEL_EXCEPTION_USER_CANCEL:CAMEL_EXCEPTION_FOLDER_INVALID_UID,
408 _("Cannot get message: %s from folder %s\n %s"), uid, lf->folder_path,
409 _("Message construction failed: Corrupt mailbox?"));
410 camel_object_unref((CamelObject *)message);
415 camel_medium_remove_header((CamelMedium *)message, "X-Evolution");
417 /* and unlock now we're finished with it */
418 camel_local_folder_unlock(lf);
421 camel_object_unref((CamelObject *)parser);
423 /* use the opportunity to notify of changes (particularly if we had a rebuild) */
424 if (camel_folder_change_info_changed(lf->changes)) {
425 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
426 camel_folder_change_info_clear(lf->changes);
434 mbox_set_message_flags(CamelFolder *folder, const char *uid, guint32 flags, guint32 set)
436 /* Basically, if anything could change the Status line, presume it does */
437 if (((CamelMboxSummary *)folder->summary)->xstatus
438 && (flags & (CAMEL_MESSAGE_SEEN|CAMEL_MESSAGE_FLAGGED|CAMEL_MESSAGE_ANSWERED|CAMEL_MESSAGE_DELETED))) {
439 flags |= CAMEL_MESSAGE_FOLDER_XEVCHANGE|CAMEL_MESSAGE_FOLDER_FLAGGED;
440 set |= CAMEL_MESSAGE_FOLDER_XEVCHANGE|CAMEL_MESSAGE_FOLDER_FLAGGED;
443 ((CamelFolderClass *)parent_class)->set_message_flags(folder, uid, flags, set);
448 mbox_set_message_user_flag(CamelFolder *folder, const char *uid, const char *name, gboolean value)
450 CamelMessageInfo *info;
452 g_return_if_fail(folder->summary != NULL);
454 info = camel_folder_summary_uid(folder->summary, uid);
458 if (camel_flag_set(&info->user_flags, name, value)) {
459 info->flags |= CAMEL_MESSAGE_FOLDER_FLAGGED|CAMEL_MESSAGE_FOLDER_XEVCHANGE;
460 camel_folder_summary_touch(folder->summary);
461 camel_object_trigger_event(CAMEL_OBJECT(folder), "message_changed", (char *) uid);
463 camel_folder_summary_info_free(folder->summary, info);
467 mbox_set_message_user_tag(CamelFolder *folder, const char *uid, const char *name, const char *value)
469 CamelMessageInfo *info;
471 g_return_if_fail(folder->summary != NULL);
473 info = camel_folder_summary_uid(folder->summary, uid);
477 if (camel_tag_set(&info->user_tags, name, value)) {
478 info->flags |= CAMEL_MESSAGE_FOLDER_FLAGGED|CAMEL_MESSAGE_FOLDER_XEVCHANGE;
479 camel_folder_summary_touch(folder->summary);
480 camel_object_trigger_event(CAMEL_OBJECT(folder), "message_changed", (char *) uid);
482 camel_folder_summary_info_free(folder->summary, info);