From a9309844945b699c7b5162eef47785e44789d2f1 Mon Sep 17 00:00:00 2001 From: Not Zed Date: Tue, 1 Aug 2000 12:46:05 +0000 Subject: Initial cut at mh provider. Well, it already does everything mbox does. 2000-08-01 Not Zed * providers/mh: New mh provider implementation. * providers/Makefile.am (SUBDIRS): Added mh provider. svn path=/trunk/; revision=4444 --- camel/ChangeLog | 6 + camel/providers/Makefile.am | 2 +- camel/providers/mh/Makefile.am | 39 +++ camel/providers/mh/camel-mh-folder.c | 483 +++++++++++++++++++++++++++++++++ camel/providers/mh/camel-mh-folder.h | 73 +++++ camel/providers/mh/camel-mh-provider.c | 54 ++++ camel/providers/mh/camel-mh-store.c | 169 ++++++++++++ camel/providers/mh/camel-mh-store.h | 60 ++++ camel/providers/mh/camel-mh-summary.c | 277 +++++++++++++++++++ camel/providers/mh/camel-mh-summary.h | 62 +++++ camel/providers/mh/libcamelmh.urls | 1 + 11 files changed, 1225 insertions(+), 1 deletion(-) create mode 100644 camel/providers/mh/Makefile.am create mode 100644 camel/providers/mh/camel-mh-folder.c create mode 100644 camel/providers/mh/camel-mh-folder.h create mode 100644 camel/providers/mh/camel-mh-provider.c create mode 100644 camel/providers/mh/camel-mh-store.c create mode 100644 camel/providers/mh/camel-mh-store.h create mode 100644 camel/providers/mh/camel-mh-summary.c create mode 100644 camel/providers/mh/camel-mh-summary.h create mode 100644 camel/providers/mh/libcamelmh.urls (limited to 'camel') diff --git a/camel/ChangeLog b/camel/ChangeLog index ee2cd43dd5..1f07de6188 100644 --- a/camel/ChangeLog +++ b/camel/ChangeLog @@ -1,3 +1,9 @@ +2000-08-01 Not Zed + + * providers/mh: New mh provider implementation. + + * providers/Makefile.am (SUBDIRS): Added mh provider. + 2000-07-31 Jeffrey Stedfast * providers/imap/camel-imap-folder.c (imap_get_message_info_internal): diff --git a/camel/providers/Makefile.am b/camel/providers/Makefile.am index dec31c0cb9..021fe4539b 100644 --- a/camel/providers/Makefile.am +++ b/camel/providers/Makefile.am @@ -1,6 +1,6 @@ ## Process this file with automake to produce Makefile.in -SUBDIRS = mbox pop3 sendmail smtp vee imap +SUBDIRS = mbox pop3 sendmail smtp vee imap mh # these ones are disabled for the moment. # MH maildir nntp smtp diff --git a/camel/providers/mh/Makefile.am b/camel/providers/mh/Makefile.am new file mode 100644 index 0000000000..62f447171b --- /dev/null +++ b/camel/providers/mh/Makefile.am @@ -0,0 +1,39 @@ +## Process this file with automake to produce Makefile.in + +libcamelmhincludedir = $(includedir)/camel + + +providerdir = $(pkglibdir)/camel-providers/$(VERSION) + +provider_LTLIBRARIES = libcamelmh.la +provider_DATA = libcamelmh.urls + +INCLUDES = -I.. \ + -I$(srcdir)/.. \ + -I$(top_srcdir)/camel \ + -I$(top_srcdir)/intl \ + -I$(top_srcdir)/libibex \ + -I$(top_srcdir)/e-util \ + -I$(top_srcdir) \ + -I$(includedir) \ + $(GTK_INCLUDEDIR) \ + -DG_LOG_DOMAIN=\"camel-mh-provider\" + +libcamelmh_la_SOURCES = \ + camel-mh-folder.c \ + camel-mh-provider.c \ + camel-mh-store.c \ + camel-mh-summary.c + +libcamelmhinclude_HEADERS = \ + camel-mh-folder.h \ + camel-mh-store.h \ + camel-mh-summary.h + +libcamelmh_la_LDFLAGS = -version-info 0:0:0 + +libcamelmh_la_LIBADD = $(top_builddir)/e-util/libeutil.la $(top_builddir)/libibex/libibex.la $(UNICODE_LIBS) +#libcamelmh_la_LIBADD = $(top_builddir)/libibex/libibex.la $(UNICODE_LIBS) + +EXTRA_DIST = libcamelmh.urls + diff --git a/camel/providers/mh/camel-mh-folder.c b/camel/providers/mh/camel-mh-folder.c new file mode 100644 index 0000000000..1961c62dd9 --- /dev/null +++ b/camel/providers/mh/camel-mh-folder.c @@ -0,0 +1,483 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */ +/* camel-mh-folder.c : Abstract class for an email folder */ + +/* + * Authors: Michael Zucchi + * + * Copyright (C) 1999, 2000 Helix Code Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "camel-mh-folder.h" +#include "camel-mh-store.h" +#include "string-utils.h" +#include "camel-stream-fs.h" +#include "camel-mh-summary.h" +#include "camel-data-wrapper.h" +#include "camel-mime-message.h" +#include "camel-stream-filter.h" +#include "camel-mime-filter-from.h" +#include "camel-exception.h" + +#define d(x) + +static CamelFolderClass *parent_class = NULL; + +/* Returns the class for a CamelMhFolder */ +#define CMHF_CLASS(so) CAMEL_MH_FOLDER_CLASS (GTK_OBJECT(so)->klass) +#define CF_CLASS(so) CAMEL_FOLDER_CLASS (GTK_OBJECT(so)->klass) +#define CMHS_CLASS(so) CAMEL_STORE_CLASS (GTK_OBJECT(so)->klass) + +static void mh_init(CamelFolder * folder, CamelStore * parent_store, + CamelFolder * parent_folder, const gchar * name, + gchar * separator, gboolean path_begins_with_sep, CamelException * ex); + +static void mh_sync(CamelFolder * folder, gboolean expunge, CamelException * ex); +static gint mh_get_message_count(CamelFolder * folder); +static gint mh_get_unread_message_count(CamelFolder * folder); +static void mh_append_message(CamelFolder * folder, CamelMimeMessage * message, guint32 flags, CamelException * ex); +static GPtrArray *mh_get_uids(CamelFolder * folder); +static GPtrArray *mh_get_subfolder_names(CamelFolder * folder); +static GPtrArray *mh_get_summary(CamelFolder * folder); +static CamelMimeMessage *mh_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex); + +static void mh_expunge(CamelFolder * folder, CamelException * ex); + +static const CamelMessageInfo *mh_get_message_info(CamelFolder * folder, const char *uid); + +static GPtrArray *mh_search_by_expression(CamelFolder * folder, const char *expression, CamelException * ex); + +static guint32 mh_get_message_flags(CamelFolder * folder, const char *uid); +static void mh_set_message_flags(CamelFolder * folder, const char *uid, guint32 flags, guint32 set); +static gboolean mh_get_message_user_flag(CamelFolder * folder, const char *uid, const char *name); +static void mh_set_message_user_flag(CamelFolder * folder, const char *uid, const char *name, gboolean value); + +static void mh_finalize(GtkObject * object); + +static void camel_mh_folder_class_init(CamelMhFolderClass * camel_mh_folder_class) +{ + CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mh_folder_class); + GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS(camel_folder_class); + + parent_class = gtk_type_class(camel_folder_get_type()); + + /* virtual method definition */ + + /* virtual method overload */ + camel_folder_class->init = mh_init; + camel_folder_class->sync = mh_sync; + camel_folder_class->get_message_count = mh_get_message_count; + camel_folder_class->get_unread_message_count = mh_get_unread_message_count; + camel_folder_class->append_message = mh_append_message; + camel_folder_class->get_uids = mh_get_uids; + camel_folder_class->free_uids = camel_folder_free_deep; + camel_folder_class->get_subfolder_names = mh_get_subfolder_names; + camel_folder_class->free_subfolder_names = camel_folder_free_deep; + camel_folder_class->get_summary = mh_get_summary; + camel_folder_class->free_summary = camel_folder_free_nop; + camel_folder_class->expunge = mh_expunge; + + camel_folder_class->get_message = mh_get_message; + + camel_folder_class->search_by_expression = mh_search_by_expression; + + camel_folder_class->get_message_info = mh_get_message_info; + + camel_folder_class->get_message_flags = mh_get_message_flags; + camel_folder_class->set_message_flags = mh_set_message_flags; + camel_folder_class->get_message_user_flag = mh_get_message_user_flag; + camel_folder_class->set_message_user_flag = mh_set_message_user_flag; + + gtk_object_class->finalize = mh_finalize; +} + +static void mh_finalize(GtkObject * object) +{ + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(object); + + g_free(mh_folder->folder_file_path); + g_free(mh_folder->summary_file_path); + g_free(mh_folder->folder_dir_path); + g_free(mh_folder->index_file_path); + + GTK_OBJECT_CLASS(parent_class)->finalize(object); +} + +GtkType camel_mh_folder_get_type(void) +{ + static GtkType camel_mh_folder_type = 0; + + if (!camel_mh_folder_type) { + GtkTypeInfo camel_mh_folder_info = { + "CamelMhFolder", + sizeof(CamelMhFolder), + sizeof(CamelMhFolderClass), + (GtkClassInitFunc) camel_mh_folder_class_init, + (GtkObjectInitFunc) NULL, + /* reserved_1 */ NULL, + /* reserved_2 */ NULL, + (GtkClassInitFunc) NULL, + }; + + camel_mh_folder_type = gtk_type_unique(CAMEL_FOLDER_TYPE, &camel_mh_folder_info); + } + + return camel_mh_folder_type; +} + +static void +mh_init(CamelFolder * folder, CamelStore * parent_store, + CamelFolder * parent_folder, const gchar * name, gchar * separator, + gboolean path_begins_with_sep, CamelException * ex) +{ + CamelMhFolder *mh_folder = (CamelMhFolder *) folder; + const gchar *root_dir_path; + gchar *real_name; + int forceindex; + struct stat st; + + /* call parent method */ + parent_class->init(folder, parent_store, parent_folder, name, separator, path_begins_with_sep, ex); + if (camel_exception_get_id(ex)) + return; + + /* we assume that the parent init method checks for the existance of @folder */ + folder->can_hold_messages = TRUE; + folder->can_hold_folders = TRUE; + folder->has_summary_capability = TRUE; + folder->has_search_capability = TRUE; + + folder->permanent_flags = CAMEL_MESSAGE_ANSWERED | + CAMEL_MESSAGE_DELETED | + CAMEL_MESSAGE_DRAFT | CAMEL_MESSAGE_FLAGGED | CAMEL_MESSAGE_SEEN | CAMEL_MESSAGE_USER; + + mh_folder->summary = NULL; + mh_folder->search = NULL; + + /* now set the name info */ + g_free(mh_folder->folder_file_path); + g_free(mh_folder->folder_dir_path); + g_free(mh_folder->index_file_path); + + root_dir_path = camel_mh_store_get_toplevel_dir(CAMEL_MH_STORE(folder->parent_store)); + + real_name = g_basename(folder->full_name); + mh_folder->folder_file_path = g_strdup_printf("%s/%s", root_dir_path, real_name); + mh_folder->summary_file_path = g_strdup_printf("%s/%s/ev-summary", root_dir_path, real_name); + mh_folder->folder_dir_path = g_strdup_printf("%s/%s", root_dir_path, real_name); + mh_folder->index_file_path = g_strdup_printf("%s/%s/ev-index.ibex", root_dir_path, real_name); + + /* if we have no index file, force it */ + forceindex = stat(mh_folder->index_file_path, &st) == -1; + + mh_folder->index = ibex_open(mh_folder->index_file_path, O_CREAT | O_RDWR, 0600); + if (mh_folder->index == NULL) { + /* yes, this isn't fatal at all */ + g_warning("Could not open/create index file: %s: indexing not performed", strerror(errno)); + } + + /* no summary (disk or memory), and we're proverbially screwed */ + mh_folder->summary = camel_mh_summary_new(mh_folder->summary_file_path, + mh_folder->folder_file_path, + mh_folder->index); + + if (camel_mh_summary_load(mh_folder->summary, forceindex) == -1) { + camel_exception_set(ex, CAMEL_EXCEPTION_FOLDER_INVALID, /* FIXME: right error code */ + "Could not load or create summary"); + return; + } +} + +static void mh_sync(CamelFolder * folder, gboolean expunge, CamelException * ex) +{ + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(folder); + + if (expunge) + mh_expunge(folder, ex); + else + camel_mh_summary_sync(mh_folder->summary, FALSE, ex); + + /* save index */ + if (mh_folder->index) + ibex_save(mh_folder->index); + if (mh_folder->summary) + camel_folder_summary_save(CAMEL_FOLDER_SUMMARY(mh_folder->summary)); +} + +static void mh_expunge(CamelFolder * folder, CamelException * ex) +{ + CamelMhFolder *mh = CAMEL_MH_FOLDER(folder); + + camel_mh_summary_sync(mh->summary, TRUE, ex); + + /* TODO: check it actually changed */ + gtk_signal_emit_by_name(GTK_OBJECT(folder), "folder_changed", 0); +} + +static gint mh_get_message_count(CamelFolder * folder) +{ + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(folder); + + g_return_val_if_fail(mh_folder->summary != NULL, -1); + + return camel_folder_summary_count(CAMEL_FOLDER_SUMMARY(mh_folder->summary)); +} + +static gint mh_get_unread_message_count(CamelFolder * folder) +{ + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(folder); + CamelMessageInfo *info; + GPtrArray *infolist; + gint i, count = 0; + + g_return_val_if_fail(mh_folder->summary != NULL, -1); + + infolist = mh_get_summary(folder); + + for (i = 0; i < infolist->len; i++) { + info = (CamelMessageInfo *) g_ptr_array_index(infolist, i); + if (!(info->flags & CAMEL_MESSAGE_SEEN)) + count++; + } + + return count; +} + +/* FIXME: this may need some tweaking for performance? */ +static void mh_append_message(CamelFolder * folder, CamelMimeMessage * message, guint32 flags, CamelException * ex) +{ + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(folder); + CamelStream *output_stream = NULL; + char *name = NULL; + char *uid = NULL; + + /* FIXME: probably needs additional locking */ + + /* keep trying uid's until we find one thats ok */ + do { + g_free(uid); + g_free(name); + uid = camel_folder_summary_next_uid_string((CamelFolderSummary *)mh_folder->summary); + name = g_strdup_printf("%s/%s", mh_folder->folder_file_path, uid); + output_stream = camel_stream_fs_new_with_name(name, O_WRONLY|O_CREAT|O_EXCL, 0600); + } while (output_stream == NULL || errno != EEXIST); + + if (output_stream == NULL) + goto fail; + + /* write the message */ + if (camel_data_wrapper_write_to_stream(CAMEL_DATA_WRAPPER(message), output_stream) == -1) + goto fail; + + if (camel_stream_close(output_stream) == -1) + goto fail; + + /* index/summarise the message. Yes this re-reads it, its just simpler */ + camel_mh_summary_add(mh_folder->summary, uid, TRUE); + gtk_signal_emit_by_name(GTK_OBJECT(folder), "folder_changed", 0); + g_free(name); + g_free(uid); + return; + +fail: + if (camel_exception_is_set(ex)) { + camel_exception_setv(ex, camel_exception_get_id(ex), + "Cannot append message to mh file: %s", camel_exception_get_description(ex)); + } else { + camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, + "Cannot append message to mh file: %s", g_strerror(errno)); + } + if (output_stream) + gtk_object_unref(GTK_OBJECT(output_stream)); + if (name) { + unlink(name); + g_free(name); + } + g_free(uid); +} + +static GPtrArray *mh_get_uids(CamelFolder * folder) +{ + GPtrArray *array; + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(folder); + int i, count; + + count = camel_folder_summary_count(CAMEL_FOLDER_SUMMARY(mh_folder->summary)); + array = g_ptr_array_new(); + g_ptr_array_set_size(array, count); + for (i = 0; i < count; i++) { + CamelMessageInfo *info = + camel_folder_summary_index(CAMEL_FOLDER_SUMMARY(mh_folder->summary), i); + + array->pdata[i] = g_strdup(info->uid); + } + + return array; +} + +static GPtrArray *mh_get_subfolder_names(CamelFolder * folder) +{ + /* FIXME: scan for sub-folders */ + /* No subfolders. */ + return g_ptr_array_new(); +} + +static CamelMimeMessage *mh_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex) +{ + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(folder); + CamelStream *message_stream = NULL; + CamelMimeMessage *message = NULL; + CamelMessageInfo *info; + char *name; + + name = g_strdup_printf("%s/%s", mh_folder->folder_file_path, uid); + + /* get the message summary info */ + info = camel_folder_summary_uid(CAMEL_FOLDER_SUMMARY(mh_folder->summary), uid); + + if (info == NULL) { + errno = ENOENT; + goto fail; + } + + message_stream = camel_stream_fs_new_with_name(name, O_RDONLY, 0); + + /* where we read from */ + if (message_stream == NULL) + goto fail; + + message = camel_mime_message_new(); + if (camel_data_wrapper_construct_from_stream(CAMEL_DATA_WRAPPER(message), message_stream) == -1) { + g_warning("Construction failed"); + errno = EINVAL; + goto fail; + } + gtk_object_unref(GTK_OBJECT(message_stream)); + g_free(name); + + return message; + +fail: + camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID, "Cannot get message: %s\n %s", + name, g_strerror(errno)); + + if (message_stream) + gtk_object_unref(GTK_OBJECT(message_stream)); + + if (message) + gtk_object_unref(GTK_OBJECT(message)); + + g_free(name); + + return NULL; +} + +GPtrArray *mh_get_summary(CamelFolder * folder) +{ + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(folder); + + return CAMEL_FOLDER_SUMMARY(mh_folder->summary)->messages; +} + +/* get a single message info, by uid */ +static const CamelMessageInfo *mh_get_message_info(CamelFolder * folder, const char *uid) +{ + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(folder); + + return camel_folder_summary_uid(CAMEL_FOLDER_SUMMARY(mh_folder->summary), uid); +} + +static GPtrArray *mh_search_by_expression(CamelFolder * folder, const char *expression, CamelException * ex) +{ + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(folder); + + if (mh_folder->search == NULL) { + mh_folder->search = camel_folder_search_new(); + } + + camel_folder_search_set_folder(mh_folder->search, folder); + if (mh_folder->summary) { + /* FIXME: dont access summary array directly? */ + camel_folder_search_set_summary(mh_folder->search, + CAMEL_FOLDER_SUMMARY(mh_folder->summary)->messages); + } + + camel_folder_search_set_body_index(mh_folder->search, mh_folder->index); + + return camel_folder_search_execute_expression(mh_folder->search, expression, ex); +} + +static guint32 mh_get_message_flags(CamelFolder * folder, const char *uid) +{ + CamelMessageInfo *info; + CamelMhFolder *mf = CAMEL_MH_FOLDER(folder); + + info = camel_folder_summary_uid(CAMEL_FOLDER_SUMMARY(mf->summary), uid); + g_return_val_if_fail(info != NULL, 0); + + return info->flags; +} + +static void mh_set_message_flags(CamelFolder * folder, const char *uid, guint32 flags, guint32 set) +{ + CamelMessageInfo *info; + CamelMhFolder *mf = CAMEL_MH_FOLDER(folder); + + info = camel_folder_summary_uid(CAMEL_FOLDER_SUMMARY(mf->summary), uid); + g_return_if_fail(info != NULL); + + info->flags = (info->flags & ~flags) | (set & flags) | CAMEL_MESSAGE_FOLDER_FLAGGED; + camel_folder_summary_touch(CAMEL_FOLDER_SUMMARY(mf->summary)); + + gtk_signal_emit_by_name(GTK_OBJECT(folder), "message_changed", uid); +} + +static gboolean mh_get_message_user_flag(CamelFolder * folder, const char *uid, const char *name) +{ + CamelMessageInfo *info; + CamelMhFolder *mf = CAMEL_MH_FOLDER(folder); + + info = camel_folder_summary_uid(CAMEL_FOLDER_SUMMARY(mf->summary), uid); + g_return_val_if_fail(info != NULL, FALSE); + + return camel_flag_get(&info->user_flags, name); +} + +static void mh_set_message_user_flag(CamelFolder * folder, const char *uid, const char *name, gboolean value) +{ + CamelMessageInfo *info; + CamelMhFolder *mf = CAMEL_MH_FOLDER(folder); + + info = camel_folder_summary_uid(CAMEL_FOLDER_SUMMARY(mf->summary), uid); + g_return_if_fail(info != NULL); + + camel_flag_set(&info->user_flags, name, value); + info->flags |= CAMEL_MESSAGE_FOLDER_FLAGGED; + camel_folder_summary_touch(CAMEL_FOLDER_SUMMARY(mf->summary)); + gtk_signal_emit_by_name(GTK_OBJECT(folder), "message_changed", uid); +} diff --git a/camel/providers/mh/camel-mh-folder.h b/camel/providers/mh/camel-mh-folder.h new file mode 100644 index 0000000000..e014197442 --- /dev/null +++ b/camel/providers/mh/camel-mh-folder.h @@ -0,0 +1,73 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-mh-folder.h : MH folder. */ + +/* + * + * Authors: + * Michael Zucchi + * + * Copyright (C) 1999 Helix Code Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef CAMEL_MH_FOLDER_H +#define CAMEL_MH_FOLDER_H 1 + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus } */ +#include +#include +#include +#include +#include "camel-mh-summary.h" + +#define CAMEL_MH_FOLDER_TYPE (camel_mh_folder_get_type ()) +#define CAMEL_MH_FOLDER(obj) (GTK_CHECK_CAST((obj), CAMEL_MH_FOLDER_TYPE, CamelMhFolder)) +#define CAMEL_MH_FOLDER_CLASS(k) (GTK_CHECK_CLASS_CAST ((k), CAMEL_MH_FOLDER_TYPE, CamelMhFolderClass)) +#define IS_CAMEL_MH_FOLDER(o) (GTK_CHECK_TYPE((o), CAMEL_MH_FOLDER_TYPE)) + +typedef struct { + CamelFolder parent_object; + + gchar *folder_file_path; /* contains the messages */ + gchar *summary_file_path; /* contains the messages summary */ + gchar *folder_dir_path; /* contains the subfolders */ + gchar *index_file_path; /* index of body contents */ + + ibex *index; /* index for this folder */ + CamelMhSummary *summary; + CamelFolderSearch *search; /* used to run searches, we just use the real thing (tm) */ +} CamelMhFolder; + +typedef struct { + CamelFolderClass parent_class; + + /* Virtual methods */ + +} CamelMhFolderClass; + +/* public methods */ + +/* Standard Gtk function */ +GtkType camel_mh_folder_get_type(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* CAMEL_MH_FOLDER_H */ diff --git a/camel/providers/mh/camel-mh-provider.c b/camel/providers/mh/camel-mh-provider.c new file mode 100644 index 0000000000..6a0aaac492 --- /dev/null +++ b/camel/providers/mh/camel-mh-provider.c @@ -0,0 +1,54 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-mbox-provider.c: mbox provider registration code */ + +/* + * Authors : + * Bertrand Guiheneuf + * + * Copyright (C) 2000 HelixCode (www.helixcode.com). + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include "config.h" +#include "camel-mh-store.h" +#include "camel-provider.h" +#include "camel-session.h" +#include "camel-url.h" + +static CamelProvider mh_provider = { + "mh", + "UNIX mh-format mail files", + + "For reading mail delivered by the local system, and for " "storing mail on local disk.", + + "mail", + + 0, + + {0, 0}, + + NULL +}; + +void camel_provider_module_init(CamelSession * session) +{ + mh_provider.object_types[CAMEL_PROVIDER_STORE] = camel_mh_store_get_type(); + + mh_provider.service_cache = g_hash_table_new(camel_url_hash, camel_url_equal); + + camel_session_register_provider(session, &mh_provider); +} diff --git a/camel/providers/mh/camel-mh-store.c b/camel/providers/mh/camel-mh-store.c new file mode 100644 index 0000000000..6ae885ac27 --- /dev/null +++ b/camel/providers/mh/camel-mh-store.c @@ -0,0 +1,169 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-mbox-store.c : class for an mbox store */ + +/* + * + * Copyright (C) 2000 Helix Code, Inc. + * + * Authors: Michael Zucchi + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include + +#include +#include +#include +#include + +#include "camel-mh-store.h" +#include "camel-mh-folder.h" +#include "camel-exception.h" +#include "camel-url.h" + +/* Returns the class for a CamelMhStore */ +#define CMHS_CLASS(so) CAMEL_MH_STORE_CLASS (GTK_OBJECT(so)->klass) +#define CF_CLASS(so) CAMEL_FOLDER_CLASS (GTK_OBJECT(so)->klass) +#define CMHF_CLASS(so) CAMEL_MH_FOLDER_CLASS (GTK_OBJECT(so)->klass) + +static char *get_name(CamelService * service, gboolean brief); +static CamelFolder *get_folder(CamelStore * store, const char *folder_name, gboolean create, CamelException * ex); +static void delete_folder(CamelStore * store, const char *folder_name, CamelException * ex); +static char *get_folder_name(CamelStore * store, const char *folder_name, CamelException * ex); + +static void camel_mh_store_class_init(CamelMhStoreClass * camel_mh_store_class) +{ + CamelStoreClass *camel_store_class = CAMEL_STORE_CLASS(camel_mh_store_class); + CamelServiceClass *camel_service_class = CAMEL_SERVICE_CLASS(camel_mh_store_class); + + /* virtual method overload */ + camel_service_class->get_name = get_name; + + camel_store_class->get_folder = get_folder; + camel_store_class->delete_folder = delete_folder; + camel_store_class->get_folder_name = get_folder_name; +} + +static void camel_mh_store_init(gpointer object, gpointer klass) +{ + CamelService *service = CAMEL_SERVICE(object); + CamelStore *store = CAMEL_STORE(object); + + service->url_flags = CAMEL_SERVICE_URL_NEED_PATH; + + /* mh names are filenames, so they are case-sensitive. */ + store->folders = g_hash_table_new(g_str_hash, g_str_equal); +} + +GtkType camel_mh_store_get_type(void) +{ + static GtkType camel_mh_store_type = 0; + + if (!camel_mh_store_type) { + GtkTypeInfo camel_mh_store_info = { + "CamelMhStore", + sizeof(CamelMhStore), + sizeof(CamelMhStoreClass), + (GtkClassInitFunc) camel_mh_store_class_init, + (GtkObjectInitFunc) camel_mh_store_init, + /* reserved_1 */ NULL, + /* reserved_2 */ NULL, + (GtkClassInitFunc) NULL, + }; + + camel_mh_store_type = gtk_type_unique(CAMEL_STORE_TYPE, &camel_mh_store_info); + } + + return camel_mh_store_type; +} + +const gchar *camel_mh_store_get_toplevel_dir(CamelMhStore * store) +{ + CamelURL *url = CAMEL_SERVICE(store)->url; + + g_assert(url != NULL); + return url->path; +} + +static CamelFolder *get_folder(CamelStore * store, const char *folder_name, gboolean create, CamelException * ex) +{ + CamelFolder *new_folder = NULL; + char *name; + struct stat st; + + name = g_strdup_printf("%s%s", CAMEL_SERVICE(store)->url->path, folder_name); + + if (stat(name, &st) == -1) { + int fd; + + if (errno != ENOENT) { + camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, + "Could not open folder `%s':" "\n%s", folder_name, g_strerror(errno)); + goto done; + } + if (!create) { + camel_exception_setv(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER, + "Folder `%s' does not exist.", folder_name); + goto done; + } + + if (mkdir (name, 0600) != 0) { + camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, + "Could not create folder `%s':" "\n%s", folder_name, g_strerror(errno)); + goto done; + } + } else if (!S_ISDIR(st.st_mode)) { + camel_exception_setv(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER, "`%s' is not a directory.", name); + goto done; + } + + new_folder = gtk_type_new(CAMEL_MH_FOLDER_TYPE); + + CF_CLASS(new_folder)->init(new_folder, store, NULL, folder_name, "/", TRUE, ex); +done: + g_free(name); + return new_folder; +} + +static void delete_folder(CamelStore * store, const char *folder_name, CamelException * ex) +{ + char *name; + + name = g_strdup_printf("%s%s", CAMEL_SERVICE(store)->url->path, folder_name); + camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, + "Could not delete folder `%s':\n%s", folder_name, "not implemented"); + g_free(name); +} + +static char *get_folder_name(CamelStore * store, const char *folder_name, CamelException * ex) +{ + /* For now, we don't allow hieararchy. FIXME. */ + if (strchr(folder_name + 1, '/')) { + camel_exception_set(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER, "Mh folders may not be nested."); + return NULL; + } + + return *folder_name == '/' ? g_strdup(folder_name) : g_strdup_printf("/%s", folder_name); +} + +static char *get_name(CamelService * service, gboolean brief) +{ + if (brief) + return g_strdup(service->url->path); + else + return g_strdup_printf("Local mail file %s", service->url->path); +} diff --git a/camel/providers/mh/camel-mh-store.h b/camel/providers/mh/camel-mh-store.h new file mode 100644 index 0000000000..7040b27ffe --- /dev/null +++ b/camel/providers/mh/camel-mh-store.h @@ -0,0 +1,60 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-mh-store.h : class for an mh store */ + +/* + * + * Copyright (C) 2000 Helix Code, Inc. + * + * Authors: Michael Zucchi + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef CAMEL_MH_STORE_H +#define CAMEL_MH_STORE_H 1 + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus } */ +#include +#include "camel-store.h" +#define CAMEL_MH_STORE_TYPE (camel_mh_store_get_type ()) +#define CAMEL_MH_STORE(obj) (GTK_CHECK_CAST((obj), CAMEL_MH_STORE_TYPE, CamelMhStore)) +#define CAMEL_MH_STORE_CLASS(k) (GTK_CHECK_CLASS_CAST ((k), CAMEL_MH_STORE_TYPE, CamelMhStoreClass)) +#define IS_CAMEL_MH_STORE(o) (GTK_CHECK_TYPE((o), CAMEL_MH_STORE_TYPE)) + +typedef struct { + CamelStore parent_object; + +} CamelMhStore; + +typedef struct { + CamelStoreClass parent_class; + +} CamelMhStoreClass; + +/* public methods */ + +/* Standard Gtk function */ +GtkType camel_mh_store_get_type(void); + +const gchar *camel_mh_store_get_toplevel_dir(CamelMhStore * store); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* CAMEL_MH_STORE_H */ diff --git a/camel/providers/mh/camel-mh-summary.c b/camel/providers/mh/camel-mh-summary.c new file mode 100644 index 0000000000..83f2113445 --- /dev/null +++ b/camel/providers/mh/camel-mh-summary.c @@ -0,0 +1,277 @@ +/* + * Copyright (C) 2000 Helix Code Inc. + * + * Authors: Not Zed + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public License + * as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include + +#include "camel-mh-summary.h" +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#define d(x) + +#define CAMEL_MH_SUMMARY_VERSION (0x2000) + +static CamelMessageInfo *message_info_new(CamelFolderSummary *, struct _header_raw *); + +static void camel_mh_summary_class_init (CamelMhSummaryClass *class); +static void camel_mh_summary_init (CamelMhSummary *gspaper); +static void camel_mh_summary_finalise (GtkObject *obj); + +#define _PRIVATE(x) (((CamelMhSummary *)(x))->priv) + +struct _CamelMhSummaryPrivate { + char *current_uid; +}; + +static CamelFolderSummaryClass *parent_class; + +guint +camel_mh_summary_get_type (void) +{ + static guint type = 0; + + if (!type) { + GtkTypeInfo type_info = { + "CamelMhSummary", + sizeof(CamelMhSummary), + sizeof(CamelMhSummaryClass), + (GtkClassInitFunc)camel_mh_summary_class_init, + (GtkObjectInitFunc)camel_mh_summary_init, + (GtkArgSetFunc)NULL, + (GtkArgGetFunc)NULL + }; + + type = gtk_type_unique(camel_folder_summary_get_type (), &type_info); + } + + return type; +} + +static void +camel_mh_summary_class_init (CamelMhSummaryClass *class) +{ + GtkObjectClass *object_class; + CamelFolderSummaryClass *sklass = (CamelFolderSummaryClass *) class; + + object_class = (GtkObjectClass *)class; + parent_class = gtk_type_class(camel_folder_summary_get_type ()); + + object_class->finalize = camel_mh_summary_finalise; + + /* override methods */ + sklass->message_info_new = message_info_new; + +} + +static void +camel_mh_summary_init (CamelMhSummary *o) +{ + struct _CamelFolderSummary *s = (CamelFolderSummary *) o; + + o->priv = g_malloc0(sizeof(*o->priv)); + + /* set unique file version */ + s->version += CAMEL_MH_SUMMARY_VERSION; +} + +static void +camel_mh_summary_finalise(GtkObject *obj) +{ + CamelMhSummary *o = (CamelMhSummary *)obj; + + g_free(o->mh_path); + + ((GtkObjectClass *)(parent_class))->finalize(obj); +} + +/** + * camel_mh_summary_new: + * + * Create a new CamelMhSummary object. + * + * Return value: A new #CamelMhSummary object. + **/ +CamelMhSummary *camel_mh_summary_new (const char *filename, const char *mhdir, ibex *index) +{ + CamelMhSummary *o = (CamelMhSummary *)gtk_type_new(camel_mh_summary_get_type ()); + + camel_folder_summary_set_build_content((CamelFolderSummary *)o, TRUE); + camel_folder_summary_set_filename((CamelFolderSummary *)o, filename); + o->mh_path = g_strdup(mhdir); + o->index = index; + return o; +} + +static CamelMessageInfo *message_info_new(CamelFolderSummary * s, struct _header_raw *h) +{ + CamelMessageInfo *mi; + CamelMhSummary *mhs = (CamelMhSummary *)s; + + mi = ((CamelFolderSummaryClass *) parent_class)->message_info_new(s, h); + if (mi) { + /* it only ever indexes 1 message at a time */ + mi->uid = g_strdup(mhs->priv->current_uid); + } + + return mi; +} + +int camel_mh_summary_load(CamelMhSummary * mhs, int forceindex) +{ + CamelFolderSummary *s = CAMEL_FOLDER_SUMMARY(mhs); + + d(printf("loading summary ...\n")); + + if (forceindex || camel_folder_summary_load(s) == -1) { + camel_folder_summary_clear(s); + } + return camel_mh_summary_check(mhs, forceindex); +} + +int camel_mh_summary_add(CamelMhSummary * mhs, const char *name, int forceindex) +{ + char *filename = g_strdup_printf("%s/%s", mhs->mh_path, name); + int fd; + CamelMimeParser *mp; + + d(printf("summarising: %s\n", name)); + + fd = open(filename, O_RDONLY); + if (fd == -1) { + g_warning("Cannot summarise/index: %s: %s", filename, strerror(errno)); + g_free(filename); + return -1; + } + mp = camel_mime_parser_new(); + camel_mime_parser_scan_from(mp, FALSE); + camel_mime_parser_init_with_fd(mp, fd); + if (forceindex || !ibex_contains_name(mhs->index, (char *)name)) { + d(printf("forcing indexing of message content\n")); + camel_folder_summary_set_index((CamelFolderSummary *)mhs, mhs->index); + } else { + camel_folder_summary_set_index((CamelFolderSummary *)mhs, NULL); + } + mhs->priv->current_uid = (char *)name; + camel_folder_summary_add_from_parser((CamelFolderSummary *)mhs, mp); + gtk_object_unref((GtkObject *)mp); + mhs->priv->current_uid = NULL; + camel_folder_summary_set_index((CamelFolderSummary *)mhs, NULL); + g_free(filename); + return 0; +} + +static void +remove_summary(char *key, CamelMessageInfo *info, CamelMhSummary *mhs) +{ + d(printf("removing message %s from summary\n", key)); + ibex_unindex(mhs->index, info->uid); + camel_folder_summary_remove((CamelFolderSummary *)mhs, info); +} + +int camel_mh_summary_check(CamelMhSummary * mhs, int forceindex) +{ + DIR *dir; + struct dirent *d; + char *p, c; + CamelMessageInfo *info; + GHashTable *left; + int i, count; + + d(printf("checking summary ...\n")); + + /* scan the directory, check for mail files not in the index, or index entries that + no longer exist */ + dir = opendir(mhs->mh_path); + if (dir == NULL) + return -1; + + /* keeps track of all uid's that have not been processed */ + left = g_hash_table_new(g_str_hash, g_str_equal); + count = camel_folder_summary_count((CamelFolderSummary *)mhs); + for (i=0;iuid, info); + } + } + while ( (d = readdir(dir)) ) { + /* FIXME: also run stat to check for regular file */ + p = d->d_name; + while ( (c = *p++) ) { + if (!isdigit(c)) + break; + } + if (c==0) { + info = camel_folder_summary_uid((CamelFolderSummary *)mhs, d->d_name); + if (info == NULL || (!ibex_contains_name(mhs->index, d->d_name))) { + /* need to add this file to the summary */ + if (info != NULL) { + g_hash_table_remove(left, info->uid); + camel_folder_summary_remove((CamelFolderSummary *)mhs, info); + } + camel_mh_summary_add(mhs, d->d_name, forceindex); + } else { + g_hash_table_remove(left, info->uid); + } + } + } + closedir(dir); + g_hash_table_foreach(left, (GHFunc)remove_summary, mhs); + g_hash_table_destroy(left); + + return 0; +} + +/* sync the summary with the ondisk files. + It doesnt store the state in the file, the summary only, == MUCH faster */ +int camel_mh_summary_sync(CamelMhSummary * mhs, int expunge, CamelException *ex) +{ + int count, i; + CamelMessageInfo *info; + char *name; + + if (!expunge) + return 0; + + count = camel_folder_summary_count((CamelFolderSummary *)mhs); + for (i=0;iflags & CAMEL_MESSAGE_DELETED) { + name = g_strdup_printf("%s/%s", mhs->mh_path, info->uid); + d(printf("deleting %s\n", name)); + if (unlink(name) == 0 || errno==ENOENT) { + camel_folder_summary_remove((CamelFolderSummary *)mhs, info); + } + } + } + return 0; +} + diff --git a/camel/providers/mh/camel-mh-summary.h b/camel/providers/mh/camel-mh-summary.h new file mode 100644 index 0000000000..b7c8867959 --- /dev/null +++ b/camel/providers/mh/camel-mh-summary.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2000 Helix Code Inc. + * + * Authors: Not Zed + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public License + * as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef _CAMEL_MH_SUMMARY_H +#define _CAMEL_MH_SUMMARY_H + +#include +#include +#include +#include + +#define CAMEL_MH_SUMMARY(obj) GTK_CHECK_CAST (obj, camel_mh_summary_get_type (), CamelMhSummary) +#define CAMEL_MH_SUMMARY_CLASS(klass) GTK_CHECK_CLASS_CAST (klass, camel_mh_summary_get_type (), CamelMhSummaryClass) +#define IS_CAMEL_MH_SUMMARY(obj) GTK_CHECK_TYPE (obj, camel_mh_summary_get_type ()) + +typedef struct _CamelMhSummary CamelMhSummary; +typedef struct _CamelMhSummaryClass CamelMhSummaryClass; + +struct _CamelMhSummary { + CamelFolderSummary parent; + struct _CamelMhSummaryPrivate *priv; + + char *mh_path; + ibex *index; +}; + +struct _CamelMhSummaryClass { + CamelFolderSummaryClass parent_class; + + /* virtual methods */ + + /* signals */ +}; + +guint camel_mh_summary_get_type (void); +CamelMhSummary *camel_mh_summary_new (const char *filename, const char *mhdir, ibex *index); + +/* methods */ +int camel_mh_summary_load(CamelMhSummary * mhs, int forceindex); +int camel_mh_summary_check(CamelMhSummary * mhs, int forceindex); +int camel_mh_summary_add(CamelMhSummary * mhs, const char *name, int forceindex); +int camel_mh_summary_sync(CamelMhSummary * mhs, int expunge, CamelException *ex); + +#endif /* ! _CAMEL_MH_SUMMARY_H */ + diff --git a/camel/providers/mh/libcamelmh.urls b/camel/providers/mh/libcamelmh.urls new file mode 100644 index 0000000000..372daaa98f --- /dev/null +++ b/camel/providers/mh/libcamelmh.urls @@ -0,0 +1 @@ +mh -- cgit