aboutsummaryrefslogtreecommitdiff
path: root/bindings/ruby
diff options
context:
space:
mode:
authorDavid Bremner <bremner@debian.org>2023-12-01 07:51:09 -0400
committerDavid Bremner <bremner@debian.org>2023-12-01 07:51:09 -0400
commit126347b6942dd4b0291beb67b119431ebd750a2a (patch)
tree532c5163cb0972c8b9e6c8b4577b86afb9c6a6a2 /bindings/ruby
Import notmuch_0.38.2.orig.tar.xz
[dgit import orig notmuch_0.38.2.orig.tar.xz]
Diffstat (limited to 'bindings/ruby')
-rw-r--r--bindings/ruby/.gitignore7
-rw-r--r--bindings/ruby/README7
-rw-r--r--bindings/ruby/database.c493
-rw-r--r--bindings/ruby/defs.h369
-rw-r--r--bindings/ruby/directory.c110
-rw-r--r--bindings/ruby/extconf.rb26
-rw-r--r--bindings/ruby/filenames.c11
-rw-r--r--bindings/ruby/init.c377
-rw-r--r--bindings/ruby/message.c366
-rw-r--r--bindings/ruby/messages.c75
-rw-r--r--bindings/ruby/query.c206
-rwxr-xr-xbindings/ruby/rdoc.sh17
-rw-r--r--bindings/ruby/status.c51
-rw-r--r--bindings/ruby/tags.c13
-rw-r--r--bindings/ruby/thread.c208
-rw-r--r--bindings/ruby/threads.c55
16 files changed, 2391 insertions, 0 deletions
diff --git a/bindings/ruby/.gitignore b/bindings/ruby/.gitignore
new file mode 100644
index 00000000..c57ae63f
--- /dev/null
+++ b/bindings/ruby/.gitignore
@@ -0,0 +1,7 @@
+# .gitignore for bindings/ruby
+
+# Generated files
+/Makefile
+/mkmf.log
+/notmuch.so
+*.o
diff --git a/bindings/ruby/README b/bindings/ruby/README
new file mode 100644
index 00000000..a2946b66
--- /dev/null
+++ b/bindings/ruby/README
@@ -0,0 +1,7 @@
+To build the the notmuch ruby extension, run the following commands
+from the *top level* notmuch source directory:
+
+% ./configure
+% make ruby-bindings
+
+The generic documentation about building notmuch also applies.
diff --git a/bindings/ruby/database.c b/bindings/ruby/database.c
new file mode 100644
index 00000000..ed224ef7
--- /dev/null
+++ b/bindings/ruby/database.c
@@ -0,0 +1,493 @@
+/* The Ruby interface to the notmuch mail library
+ *
+ * Copyright © 2010, 2011 Ali Polatel
+ *
+ * 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 3 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, see https://www.gnu.org/licenses/ .
+ *
+ * Author: Ali Polatel <alip@exherbo.org>
+ */
+
+#include "defs.h"
+
+VALUE
+notmuch_rb_database_alloc (VALUE klass)
+{
+ return Data_Wrap_Notmuch_Object (klass, &notmuch_rb_database_type, NULL);
+}
+
+/*
+ * call-seq: DB.destroy => nil
+ *
+ * Destroys the database, freeing all resources allocated for it.
+ */
+VALUE
+notmuch_rb_database_destroy (VALUE self)
+{
+ notmuch_rb_object_destroy (self, &notmuch_rb_database_type);
+
+ return Qnil;
+}
+
+/*
+ * call-seq: Notmuch::Database.new(path [, {:create => false, :mode => Notmuch::MODE_READ_ONLY}]) => DB
+ *
+ * Create or open a notmuch database using the given path.
+ *
+ * If :create is +true+, create the database instead of opening.
+ *
+ * The argument :mode specifies the open mode of the database.
+ */
+VALUE
+notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE self)
+{
+ const char *path;
+ int create, mode;
+ VALUE pathv, hashv;
+ VALUE modev;
+ notmuch_database_t *database;
+ notmuch_status_t ret;
+
+ path = NULL;
+ create = 0;
+ mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
+
+ /* Check arguments */
+ rb_scan_args (argc, argv, "02", &pathv, &hashv);
+
+ if (!NIL_P (pathv)) {
+ SafeStringValue (pathv);
+ path = RSTRING_PTR (pathv);
+ }
+
+ if (!NIL_P (hashv)) {
+ VALUE rmode, rcreate;
+ VALUE kwargs[2];
+ static ID keyword_ids[2];
+
+ if (!keyword_ids[0]) {
+ keyword_ids[0] = rb_intern_const ("mode");
+ keyword_ids[1] = rb_intern_const ("create");
+ }
+
+ rb_get_kwargs (hashv, keyword_ids, 0, 2, kwargs);
+
+ rmode = kwargs[0];
+ rcreate = kwargs[1];
+
+ if (rmode != Qundef) {
+ if (!FIXNUM_P (rmode))
+ rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
+ else {
+ mode = FIX2INT (rmode);
+ switch (mode) {
+ case NOTMUCH_DATABASE_MODE_READ_ONLY:
+ case NOTMUCH_DATABASE_MODE_READ_WRITE:
+ break;
+ default:
+ rb_raise ( rb_eTypeError, "Invalid mode");
+ }
+ }
+ }
+ if (rcreate != Qundef)
+ create = RTEST (rcreate);
+ }
+
+ rb_check_typeddata (self, &notmuch_rb_database_type);
+ if (create)
+ ret = notmuch_database_create (path, &database);
+ else
+ ret = notmuch_database_open_with_config (path, mode, NULL, NULL, &database, NULL);
+ notmuch_rb_status_raise (ret);
+
+ DATA_PTR (self) = notmuch_rb_object_create (database, "notmuch_rb_database");
+
+ return self;
+}
+
+/*
+ * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
+ *
+ * Identical to new, except that when it is called with a block, it yields with
+ * the new instance and closes it, and returns the result which is returned from
+ * the block.
+ */
+VALUE
+notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass)
+{
+ VALUE obj;
+
+ obj = rb_class_new_instance (argc, argv, klass);
+ if (!rb_block_given_p ())
+ return obj;
+
+ return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj);
+}
+
+/*
+ * call-seq: DB.close => nil
+ *
+ * Close the notmuch database.
+ */
+VALUE
+notmuch_rb_database_close (VALUE self)
+{
+ notmuch_database_t *db;
+ notmuch_status_t ret;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ ret = notmuch_database_close (db);
+ notmuch_rb_status_raise (ret);
+
+ return Qnil;
+}
+
+/*
+ * call-seq: DB.path => String
+ *
+ * Return the path of the database
+ */
+VALUE
+notmuch_rb_database_path (VALUE self)
+{
+ notmuch_database_t *db;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ return rb_str_new2 (notmuch_database_get_path (db));
+}
+
+/*
+ * call-seq: DB.version => Fixnum
+ *
+ * Return the version of the database
+ */
+VALUE
+notmuch_rb_database_version (VALUE self)
+{
+ notmuch_database_t *db;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ return INT2FIX (notmuch_database_get_version (db));
+}
+
+/*
+ * call-seq: DB.needs_upgrade? => true or false
+ *
+ * Return the +true+ if the database needs upgrading, +false+ otherwise
+ */
+VALUE
+notmuch_rb_database_needs_upgrade (VALUE self)
+{
+ notmuch_database_t *db;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ return notmuch_database_needs_upgrade (db) ? Qtrue : Qfalse;
+}
+
+static void
+notmuch_rb_upgrade_notify (void *closure, double progress)
+{
+ VALUE *block = (VALUE *) closure;
+ rb_funcall (*block, ID_call, 1, rb_float_new (progress));
+}
+
+/*
+ * call-seq: DB.upgrade! [{|progress| block }] => nil
+ *
+ * Upgrade the database.
+ *
+ * If a block is given the block is called with a progress indicator as a
+ * floating point value in the range of [0.0..1.0].
+ */
+VALUE
+notmuch_rb_database_upgrade (VALUE self)
+{
+ notmuch_status_t ret;
+ void (*pnotify) (void *closure, double progress);
+ notmuch_database_t *db;
+ VALUE block;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ if (rb_block_given_p ()) {
+ pnotify = notmuch_rb_upgrade_notify;
+ block = rb_block_proc ();
+ }
+ else
+ pnotify = NULL;
+
+ ret = notmuch_database_upgrade (db, pnotify, pnotify ? &block : NULL);
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
+
+/*
+ * call-seq: DB.begin_atomic => nil
+ *
+ * Begin an atomic database operation.
+ */
+VALUE
+notmuch_rb_database_begin_atomic (VALUE self)
+{
+ notmuch_status_t ret;
+ notmuch_database_t *db;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ ret = notmuch_database_begin_atomic (db);
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
+
+/*
+ * call-seq: DB.end_atomic => nil
+ *
+ * Indicate the end of an atomic database operation.
+ */
+VALUE
+notmuch_rb_database_end_atomic (VALUE self)
+{
+ notmuch_status_t ret;
+ notmuch_database_t *db;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ ret = notmuch_database_end_atomic (db);
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
+
+/*
+ * call-seq: DB.get_directory(path) => DIR
+ *
+ * Retrieve a directory object from the database for 'path'
+ */
+VALUE
+notmuch_rb_database_get_directory (VALUE self, VALUE pathv)
+{
+ const char *path;
+ notmuch_status_t ret;
+ notmuch_directory_t *dir;
+ notmuch_database_t *db;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ SafeStringValue (pathv);
+ path = RSTRING_PTR (pathv);
+
+ ret = notmuch_database_get_directory (db, path, &dir);
+ notmuch_rb_status_raise (ret);
+ if (dir)
+ return Data_Wrap_Notmuch_Object (notmuch_rb_cDirectory, &notmuch_rb_directory_type, dir);
+ return Qnil;
+}
+
+/*
+ * call-seq: DB.add_message(path) => MESSAGE, isdup
+ *
+ * Add a message to the database and return it.
+ *
+ * +isdup+ is a boolean that specifies whether the added message was a
+ * duplicate.
+ */
+VALUE
+notmuch_rb_database_add_message (VALUE self, VALUE pathv)
+{
+ const char *path;
+ notmuch_status_t ret;
+ notmuch_message_t *message;
+ notmuch_database_t *db;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ SafeStringValue (pathv);
+ path = RSTRING_PTR (pathv);
+
+ ret = notmuch_database_index_file (db, path, NULL, &message);
+ notmuch_rb_status_raise (ret);
+ return rb_assoc_new (Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, &notmuch_rb_message_type, message),
+ (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse);
+}
+
+/*
+ * call-seq: DB.remove_message (path) => isdup
+ *
+ * Remove a message from the database.
+ *
+ * +isdup+ is a boolean that specifies whether the removed message was a
+ * duplicate.
+ */
+VALUE
+notmuch_rb_database_remove_message (VALUE self, VALUE pathv)
+{
+ const char *path;
+ notmuch_status_t ret;
+ notmuch_database_t *db;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ SafeStringValue (pathv);
+ path = RSTRING_PTR (pathv);
+
+ ret = notmuch_database_remove_message (db, path);
+ notmuch_rb_status_raise (ret);
+ return (ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq: DB.find_message(id) => MESSAGE or nil
+ *
+ * Find a message by message id.
+ */
+VALUE
+notmuch_rb_database_find_message (VALUE self, VALUE idv)
+{
+ const char *id;
+ notmuch_status_t ret;
+ notmuch_database_t *db;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ SafeStringValue (idv);
+ id = RSTRING_PTR (idv);
+
+ ret = notmuch_database_find_message (db, id, &message);
+ notmuch_rb_status_raise (ret);
+
+ if (message)
+ return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, &notmuch_rb_message_type, message);
+ return Qnil;
+}
+
+/*
+ * call-seq: DB.find_message_by_filename(path) => MESSAGE or nil
+ *
+ * Find a message by filename.
+ */
+VALUE
+notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv)
+{
+ const char *path;
+ notmuch_status_t ret;
+ notmuch_database_t *db;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ SafeStringValue (pathv);
+ path = RSTRING_PTR (pathv);
+
+ ret = notmuch_database_find_message_by_filename (db, path, &message);
+ notmuch_rb_status_raise (ret);
+
+ if (message)
+ return Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, &notmuch_rb_message_type, message);
+ return Qnil;
+}
+
+/*
+ * call-seq: DB.get_all_tags() => TAGS
+ *
+ * Returns a list of all tags found in the database.
+ */
+VALUE
+notmuch_rb_database_get_all_tags (VALUE self)
+{
+ notmuch_database_t *db;
+ notmuch_tags_t *tags;
+
+ Data_Get_Notmuch_Database (self, db);
+
+ tags = notmuch_database_get_all_tags (db);
+ if (!tags) {
+ const char *msg = notmuch_database_status_string (db);
+ if (!msg)
+ msg = "Unknown notmuch error";
+
+ rb_raise (notmuch_rb_eBaseError, "%s", msg);
+ }
+ return notmuch_rb_tags_get (tags);
+}
+
+/*
+ * call-seq:
+ * DB.query(query) => QUERY
+ * DB.query(query, sort:, excluded_tags:, omit_excluded:) => QUERY
+ *
+ * Retrieve a query object for the query string 'query'. When using keyword
+ * arguments they are passwed to the query object.
+ */
+VALUE
+notmuch_rb_database_query_create (int argc, VALUE *argv, VALUE self)
+{
+ VALUE qstrv;
+ VALUE opts;
+ const char *qstr;
+ notmuch_query_t *query;
+ notmuch_database_t *db;
+
+ rb_scan_args (argc, argv, "1:", &qstrv, &opts);
+
+ Data_Get_Notmuch_Database (self, db);
+
+ SafeStringValue (qstrv);
+ qstr = RSTRING_PTR (qstrv);
+
+ query = notmuch_query_create (db, qstr);
+ if (!query)
+ rb_raise (notmuch_rb_eMemoryError, "Out of memory");
+
+ if (!NIL_P (opts)) {
+ VALUE sort, exclude_tags, omit_excluded;
+ VALUE kwargs[3];
+ static ID keyword_ids[3];
+
+ if (!keyword_ids[0]) {
+ keyword_ids[0] = rb_intern_const ("sort");
+ keyword_ids[1] = rb_intern_const ("exclude_tags");
+ keyword_ids[2] = rb_intern_const ("omit_excluded");
+ }
+
+ rb_get_kwargs (opts, keyword_ids, 0, 3, kwargs);
+
+ sort = kwargs[0];
+ exclude_tags = kwargs[1];
+ omit_excluded = kwargs[2];
+
+ if (sort != Qundef)
+ notmuch_query_set_sort (query, FIX2UINT (sort));
+
+ if (exclude_tags != Qundef) {
+ for (int i = 0; i < RARRAY_LEN (exclude_tags); i++) {
+ VALUE e = RARRAY_AREF (exclude_tags, i);
+ notmuch_query_add_tag_exclude (query, RSTRING_PTR (e));
+ }
+ }
+
+ if (omit_excluded != Qundef) {
+ notmuch_exclude_t omit;
+ omit = FIXNUM_P (omit_excluded) ? FIX2UINT (omit_excluded) : RTEST(omit_excluded);
+ notmuch_query_set_omit_excluded (query, omit);
+ }
+ }
+
+ return Data_Wrap_Notmuch_Object (notmuch_rb_cQuery, &notmuch_rb_query_type, query);
+}
diff --git a/bindings/ruby/defs.h b/bindings/ruby/defs.h
new file mode 100644
index 00000000..a2cb38c8
--- /dev/null
+++ b/bindings/ruby/defs.h
@@ -0,0 +1,369 @@
+/* The Ruby interface to the notmuch mail library
+ *
+ * Copyright © 2010, 2011, 2012 Ali Polatel
+ *
+ * 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 3 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, see https://www.gnu.org/licenses/ .
+ *
+ * Author: Ali Polatel <alip@exherbo.org>
+ */
+
+#ifndef DEFS_H
+#define DEFS_H
+
+#include <notmuch.h>
+#include <ruby.h>
+#include <talloc.h>
+
+extern VALUE notmuch_rb_cDatabase;
+extern VALUE notmuch_rb_cDirectory;
+extern VALUE notmuch_rb_cFileNames;
+extern VALUE notmuch_rb_cQuery;
+extern VALUE notmuch_rb_cThreads;
+extern VALUE notmuch_rb_cThread;
+extern VALUE notmuch_rb_cMessages;
+extern VALUE notmuch_rb_cMessage;
+
+extern VALUE notmuch_rb_eBaseError;
+extern VALUE notmuch_rb_eDatabaseError;
+extern VALUE notmuch_rb_eMemoryError;
+extern VALUE notmuch_rb_eReadOnlyError;
+extern VALUE notmuch_rb_eXapianError;
+extern VALUE notmuch_rb_eFileError;
+extern VALUE notmuch_rb_eFileNotEmailError;
+extern VALUE notmuch_rb_eNullPointerError;
+extern VALUE notmuch_rb_eTagTooLongError;
+extern VALUE notmuch_rb_eUnbalancedFreezeThawError;
+extern VALUE notmuch_rb_eUnbalancedAtomicError;
+
+extern ID ID_call;
+
+/* RSTRING_PTR() is new in ruby-1.9 */
+#if !defined(RSTRING_PTR)
+# define RSTRING_PTR(v) (RSTRING((v))->ptr)
+#endif /* !defined (RSTRING_PTR) */
+
+extern const rb_data_type_t notmuch_rb_object_type;
+extern const rb_data_type_t notmuch_rb_database_type;
+extern const rb_data_type_t notmuch_rb_directory_type;
+extern const rb_data_type_t notmuch_rb_query_type;
+extern const rb_data_type_t notmuch_rb_threads_type;
+extern const rb_data_type_t notmuch_rb_thread_type;
+extern const rb_data_type_t notmuch_rb_messages_type;
+extern const rb_data_type_t notmuch_rb_message_type;
+extern const rb_data_type_t notmuch_rb_tags_type;
+
+#define Data_Get_Notmuch_Rb_Object(obj, type, ptr) \
+ do { \
+ (ptr) = rb_check_typeddata ((obj), (type)); \
+ if (RB_UNLIKELY (!(ptr))) { \
+ VALUE cname = rb_class_name (CLASS_OF ((obj))); \
+ rb_raise (rb_eRuntimeError, "%"PRIsVALUE" object destroyed", cname); \
+ } \
+ } while (0)
+
+#define Data_Get_Notmuch_Object(obj, type, ptr) \
+ do { \
+ notmuch_rb_object_t *rb_wrapper; \
+ Data_Get_Notmuch_Rb_Object ((obj), (type), rb_wrapper); \
+ (ptr) = rb_wrapper->nm_object; \
+ } while (0)
+
+#define Data_Wrap_Notmuch_Object(klass, type, ptr) \
+ TypedData_Wrap_Struct ((klass), (type), notmuch_rb_object_create ((ptr), "notmuch_rb_object: " __location__))
+
+#define Data_Get_Notmuch_Database(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), &notmuch_rb_database_type, (ptr))
+
+#define Data_Get_Notmuch_Directory(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), &notmuch_rb_directory_type, (ptr))
+
+#define Data_Get_Notmuch_Query(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), &notmuch_rb_query_type, (ptr))
+
+#define Data_Get_Notmuch_Threads(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), &notmuch_rb_threads_type, (ptr))
+
+#define Data_Get_Notmuch_Messages(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), &notmuch_rb_messages_type, (ptr))
+
+#define Data_Get_Notmuch_Thread(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), &notmuch_rb_thread_type, (ptr))
+
+#define Data_Get_Notmuch_Message(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), &notmuch_rb_message_type, (ptr))
+
+#define Data_Get_Notmuch_Tags(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), &notmuch_rb_tags_type, (ptr))
+
+typedef struct {
+ void *nm_object;
+} notmuch_rb_object_t;
+
+static inline void *
+notmuch_rb_object_create (void *nm_object, const char *name)
+{
+ notmuch_rb_object_t *rb_wrapper = talloc_named_const (NULL, sizeof (*rb_wrapper), name);
+
+ if (RB_UNLIKELY (!rb_wrapper))
+ return NULL;
+
+ rb_wrapper->nm_object = nm_object;
+ talloc_steal (rb_wrapper, nm_object);
+ return rb_wrapper;
+}
+
+static inline void
+notmuch_rb_object_free (void *rb_wrapper)
+{
+ talloc_free (rb_wrapper);
+}
+
+static inline void
+notmuch_rb_object_destroy (VALUE rb_object, const rb_data_type_t *type)
+{
+ notmuch_rb_object_t *rb_wrapper;
+
+ Data_Get_Notmuch_Rb_Object (rb_object, type, rb_wrapper);
+
+ /* Call the corresponding notmuch_*_destroy function */
+ ((void (*)(void *)) type->data) (rb_wrapper->nm_object);
+ notmuch_rb_object_free (rb_wrapper);
+ DATA_PTR (rb_object) = NULL;
+}
+
+/* status.c */
+void
+notmuch_rb_status_raise (notmuch_status_t status);
+
+/* database.c */
+VALUE
+notmuch_rb_database_alloc (VALUE klass);
+
+VALUE
+notmuch_rb_database_destroy (VALUE self);
+
+VALUE
+notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE klass);
+
+VALUE
+notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass);
+
+VALUE
+notmuch_rb_database_close (VALUE self);
+
+VALUE
+notmuch_rb_database_path (VALUE self);
+
+VALUE
+notmuch_rb_database_version (VALUE self);
+
+VALUE
+notmuch_rb_database_needs_upgrade (VALUE self);
+
+VALUE
+notmuch_rb_database_upgrade (VALUE self);
+
+VALUE
+notmuch_rb_database_begin_atomic (VALUE self);
+
+VALUE
+notmuch_rb_database_end_atomic (VALUE self);
+
+VALUE
+notmuch_rb_database_get_directory (VALUE self, VALUE pathv);
+
+VALUE
+notmuch_rb_database_add_message (VALUE self, VALUE pathv);
+
+VALUE
+notmuch_rb_database_remove_message (VALUE self, VALUE pathv);
+
+VALUE
+notmuch_rb_database_find_message (VALUE self, VALUE idv);
+
+VALUE
+notmuch_rb_database_find_message_by_filename (VALUE self, VALUE pathv);
+
+VALUE
+notmuch_rb_database_get_all_tags (VALUE self);
+
+VALUE
+notmuch_rb_database_query_create (int argc, VALUE *argv, VALUE self);
+
+/* directory.c */
+VALUE
+notmuch_rb_directory_destroy (VALUE self);
+
+VALUE
+notmuch_rb_directory_get_mtime (VALUE self);
+
+VALUE
+notmuch_rb_directory_set_mtime (VALUE self, VALUE mtimev);
+
+VALUE
+notmuch_rb_directory_get_child_files (VALUE self);
+
+VALUE
+notmuch_rb_directory_get_child_directories (VALUE self);
+
+/* filenames.c */
+VALUE
+notmuch_rb_filenames_get (notmuch_filenames_t *fnames);
+
+/* query.c */
+VALUE
+notmuch_rb_query_destroy (VALUE self);
+
+VALUE
+notmuch_rb_query_get_sort (VALUE self);
+
+VALUE
+notmuch_rb_query_set_sort (VALUE self, VALUE sortv);
+
+VALUE
+notmuch_rb_query_get_string (VALUE self);
+
+VALUE
+notmuch_rb_query_add_tag_exclude (VALUE self, VALUE tagv);
+
+VALUE
+notmuch_rb_query_set_omit_excluded (VALUE self, VALUE omitv);
+
+VALUE
+notmuch_rb_query_search_threads (VALUE self);
+
+VALUE
+notmuch_rb_query_search_messages (VALUE self);
+
+VALUE
+notmuch_rb_query_count_messages (VALUE self);
+
+VALUE
+notmuch_rb_query_count_threads (VALUE self);
+
+/* threads.c */
+VALUE
+notmuch_rb_threads_destroy (VALUE self);
+
+VALUE
+notmuch_rb_threads_each (VALUE self);
+
+/* messages.c */
+VALUE
+notmuch_rb_messages_destroy (VALUE self);
+
+VALUE
+notmuch_rb_messages_each (VALUE self);
+
+VALUE
+notmuch_rb_messages_collect_tags (VALUE self);
+
+/* thread.c */
+VALUE
+notmuch_rb_thread_destroy (VALUE self);
+
+VALUE
+notmuch_rb_thread_get_thread_id (VALUE self);
+
+VALUE
+notmuch_rb_thread_get_total_messages (VALUE self);
+
+VALUE
+notmuch_rb_thread_get_toplevel_messages (VALUE self);
+
+VALUE
+notmuch_rb_thread_get_messages (VALUE self);
+
+VALUE
+notmuch_rb_thread_get_matched_messages (VALUE self);
+
+VALUE
+notmuch_rb_thread_get_authors (VALUE self);
+
+VALUE
+notmuch_rb_thread_get_subject (VALUE self);
+
+VALUE
+notmuch_rb_thread_get_oldest_date (VALUE self);
+
+VALUE
+notmuch_rb_thread_get_newest_date (VALUE self);
+
+VALUE
+notmuch_rb_thread_get_tags (VALUE self);
+
+/* message.c */
+VALUE
+notmuch_rb_message_destroy (VALUE self);
+
+VALUE
+notmuch_rb_message_get_message_id (VALUE self);
+
+VALUE
+notmuch_rb_message_get_thread_id (VALUE self);
+
+VALUE
+notmuch_rb_message_get_replies (VALUE self);
+
+VALUE
+notmuch_rb_message_get_filename (VALUE self);
+
+VALUE
+notmuch_rb_message_get_filenames (VALUE self);
+
+VALUE
+notmuch_rb_message_get_flag (VALUE self, VALUE flagv);
+
+VALUE
+notmuch_rb_message_set_flag (VALUE self, VALUE flagv, VALUE valuev);
+
+VALUE
+notmuch_rb_message_get_date (VALUE self);
+
+VALUE
+notmuch_rb_message_get_header (VALUE self, VALUE headerv);
+
+VALUE
+notmuch_rb_message_get_tags (VALUE self);
+
+VALUE
+notmuch_rb_message_add_tag (VALUE self, VALUE tagv);
+
+VALUE
+notmuch_rb_message_remove_tag (VALUE self, VALUE tagv);
+
+VALUE
+notmuch_rb_message_remove_all_tags (VALUE self);
+
+VALUE
+notmuch_rb_message_maildir_flags_to_tags (VALUE self);
+
+VALUE
+notmuch_rb_message_tags_to_maildir_flags (VALUE self);
+
+VALUE
+notmuch_rb_message_freeze (VALUE self);
+
+VALUE
+notmuch_rb_message_thaw (VALUE self);
+
+/* tags.c */
+VALUE
+notmuch_rb_tags_get (notmuch_tags_t *tags);
+
+/* init.c */
+void
+Init_notmuch (void);
+
+#endif
diff --git a/bindings/ruby/directory.c b/bindings/ruby/directory.c
new file mode 100644
index 00000000..f267d82f
--- /dev/null
+++ b/bindings/ruby/directory.c
@@ -0,0 +1,110 @@
+/* The Ruby interface to the notmuch mail library
+ *
+ * Copyright © 2010, 2011 Ali Polatel
+ *
+ * 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 3 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, see https://www.gnu.org/licenses/ .
+ *
+ * Author: Ali Polatel <alip@exherbo.org>
+ */
+
+#include "defs.h"
+
+/*
+ * call-seq: DIR.destroy! => nil
+ *
+ * Destroys the directory, freeing all resources allocated for it.
+ */
+VALUE
+notmuch_rb_directory_destroy (VALUE self)
+{
+ notmuch_rb_object_destroy (self, &notmuch_rb_directory_type);
+
+ return Qnil;
+}
+
+/*
+ * call-seq: DIR.mtime => fixnum
+ *
+ * Returns the mtime of the directory or +0+ if no mtime has been previously
+ * stored.
+ */
+VALUE
+notmuch_rb_directory_get_mtime (VALUE self)
+{
+ notmuch_directory_t *dir;
+
+ Data_Get_Notmuch_Directory (self, dir);
+
+ return UINT2NUM (notmuch_directory_get_mtime (dir));
+}
+
+/*
+ * call-seq: DIR.mtime=(fixnum) => nil
+ *
+ * Store an mtime within the database for the directory object.
+ */
+VALUE
+notmuch_rb_directory_set_mtime (VALUE self, VALUE mtimev)
+{
+ notmuch_status_t ret;
+ notmuch_directory_t *dir;
+
+ Data_Get_Notmuch_Directory (self, dir);
+
+ if (!FIXNUM_P (mtimev))
+ rb_raise (rb_eTypeError, "First argument not a fixnum");
+
+ ret = notmuch_directory_set_mtime (dir, FIX2UINT (mtimev));
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
+
+/*
+ * call-seq: DIR.child_files => FILENAMES
+ *
+ * Return a Notmuch::FileNames object, which is an +Enumerable+ listing all the
+ * filenames of messages in the database within the given directory.
+ */
+VALUE
+notmuch_rb_directory_get_child_files (VALUE self)
+{
+ notmuch_directory_t *dir;
+ notmuch_filenames_t *fnames;
+
+ Data_Get_Notmuch_Directory (self, dir);
+
+ fnames = notmuch_directory_get_child_files (dir);
+
+ return notmuch_rb_filenames_get (fnames);
+}
+
+/*
+ * call-seq: DIR.child_directories => FILENAMES
+ *
+ * Return a Notmuch::FileNames object, which is an +Enumerable+ listing all the
+ * directories in the database within the given directory.
+ */
+VALUE
+notmuch_rb_directory_get_child_directories (VALUE self)
+{
+ notmuch_directory_t *dir;
+ notmuch_filenames_t *fnames;
+
+ Data_Get_Notmuch_Directory (self, dir);
+
+ fnames = notmuch_directory_get_child_directories (dir);
+
+ return notmuch_rb_filenames_get (fnames);
+}
diff --git a/bindings/ruby/extconf.rb b/bindings/ruby/extconf.rb
new file mode 100644
index 00000000..d914537c
--- /dev/null
+++ b/bindings/ruby/extconf.rb
@@ -0,0 +1,26 @@
+#!/usr/bin/env ruby
+# coding: utf-8
+# Copyright 2010, 2011, 2012 Ali Polatel <alip@exherbo.org>
+# Distributed under the terms of the GNU General Public License v3
+
+require 'mkmf'
+
+dir = File.join(ENV['NOTMUCH_SRCDIR'], 'lib')
+
+# includes
+$INCFLAGS = "-I#{dir} #{$INCFLAGS}"
+
+if ENV['EXTRA_LDFLAGS']
+ $LDFLAGS += " " + ENV['EXTRA_LDFLAGS']
+end
+
+if not ENV['LIBNOTMUCH']
+ exit 1
+end
+
+$LOCAL_LIBS += ENV['LIBNOTMUCH']
+$LIBS += " -ltalloc"
+
+# Create Makefile
+dir_config('notmuch')
+create_makefile('notmuch')
diff --git a/bindings/ruby/filenames.c b/bindings/ruby/filenames.c
new file mode 100644
index 00000000..60c3fb8b
--- /dev/null
+++ b/bindings/ruby/filenames.c
@@ -0,0 +1,11 @@
+#include "defs.h"
+
+VALUE
+notmuch_rb_filenames_get (notmuch_filenames_t *fnames)
+{
+ VALUE rb_array = rb_ary_new ();
+
+ for (; notmuch_filenames_valid (fnames); notmuch_filenames_move_to_next (fnames))
+ rb_ary_push (rb_array, rb_str_new2 (notmuch_filenames_get (fnames)));
+ return rb_array;
+}
diff --git a/bindings/ruby/init.c b/bindings/ruby/init.c
new file mode 100644
index 00000000..2d1994af
--- /dev/null
+++ b/bindings/ruby/init.c
@@ -0,0 +1,377 @@
+/* The Ruby interface to the notmuch mail library
+ *
+ * Copyright © 2010, 2011, 2012 Ali Polatel
+ *
+ * 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 3 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, see https://www.gnu.org/licenses/ .
+ *
+ * Author: Ali Polatel <alip@exherbo.org>
+ */
+
+#include "defs.h"
+
+VALUE notmuch_rb_cDatabase;
+VALUE notmuch_rb_cDirectory;
+VALUE notmuch_rb_cQuery;
+VALUE notmuch_rb_cThreads;
+VALUE notmuch_rb_cThread;
+VALUE notmuch_rb_cMessages;
+VALUE notmuch_rb_cMessage;
+
+VALUE notmuch_rb_eBaseError;
+VALUE notmuch_rb_eDatabaseError;
+VALUE notmuch_rb_eMemoryError;
+VALUE notmuch_rb_eReadOnlyError;
+VALUE notmuch_rb_eXapianError;
+VALUE notmuch_rb_eFileError;
+VALUE notmuch_rb_eFileNotEmailError;
+VALUE notmuch_rb_eNullPointerError;
+VALUE notmuch_rb_eTagTooLongError;
+VALUE notmuch_rb_eUnbalancedFreezeThawError;
+VALUE notmuch_rb_eUnbalancedAtomicError;
+
+ID ID_call;
+
+const rb_data_type_t notmuch_rb_object_type = {
+ .wrap_struct_name = "notmuch_object",
+ .function = {
+ .dfree = notmuch_rb_object_free,
+ },
+};
+
+#define define_type(id) \
+ const rb_data_type_t notmuch_rb_ ## id ## _type = { \
+ .wrap_struct_name = "notmuch_" #id, \
+ .parent = &notmuch_rb_object_type, \
+ .data = &notmuch_ ## id ## _destroy, \
+ .function = { \
+ .dfree = notmuch_rb_object_free, \
+ }, \
+ }
+
+define_type (database);
+define_type (directory);
+define_type (query);
+define_type (threads);
+define_type (thread);
+define_type (messages);
+define_type (message);
+
+/*
+ * Document-module: Notmuch
+ *
+ * == Summary
+ *
+ * Ruby extension to the <tt>notmuch</tt> mail library.
+ *
+ * == Classes
+ *
+ * Following are the classes that are most likely to be of interest to
+ * the user:
+ *
+ * - Notmuch::Database
+ * - Notmuch::Query
+ * - Notmuch::Threads
+ * - Notmuch::Messages
+ * - Notmuch::Thread
+ * - Notmuch::Message
+ */
+
+void
+Init_notmuch (void)
+{
+ VALUE mod;
+
+ ID_call = rb_intern ("call");
+
+ mod = rb_define_module ("Notmuch");
+
+ /*
+ * Document-const: Notmuch::MODE_READ_ONLY
+ *
+ * Open the database in read only mode
+ */
+ rb_define_const (mod, "MODE_READ_ONLY", INT2FIX (NOTMUCH_DATABASE_MODE_READ_ONLY));
+ /*
+ * Document-const: Notmuch::MODE_READ_WRITE
+ *
+ * Open the database in read write mode
+ */
+ rb_define_const (mod, "MODE_READ_WRITE", INT2FIX (NOTMUCH_DATABASE_MODE_READ_WRITE));
+ /*
+ * Document-const: Notmuch::SORT_OLDEST_FIRST
+ *
+ * Sort query results by oldest first
+ */
+ rb_define_const (mod, "SORT_OLDEST_FIRST", INT2FIX (NOTMUCH_SORT_OLDEST_FIRST));
+ /*
+ * Document-const: Notmuch::SORT_NEWEST_FIRST
+ *
+ * Sort query results by newest first
+ */
+ rb_define_const (mod, "SORT_NEWEST_FIRST", INT2FIX (NOTMUCH_SORT_NEWEST_FIRST));
+ /*
+ * Document-const: Notmuch::SORT_MESSAGE_ID
+ *
+ * Sort query results by message id
+ */
+ rb_define_const (mod, "SORT_MESSAGE_ID", INT2FIX (NOTMUCH_SORT_MESSAGE_ID));
+ /*
+ * Document-const: Notmuch::SORT_UNSORTED
+ *
+ * Do not sort query results
+ */
+ rb_define_const (mod, "SORT_UNSORTED", INT2FIX (NOTMUCH_SORT_UNSORTED));
+ /*
+ * Document-const: Notmuch::MESSAGE_FLAG_MATCH
+ *
+ * Message flag "match"
+ */
+ rb_define_const (mod, "MESSAGE_FLAG_MATCH", INT2FIX (NOTMUCH_MESSAGE_FLAG_MATCH));
+ /*
+ * Document-const: Notmuch::MESSAGE_FLAG_EXCLUDED
+ *
+ * Message flag "excluded"
+ */
+ rb_define_const (mod, "MESSAGE_FLAG_EXCLUDED", INT2FIX (NOTMUCH_MESSAGE_FLAG_EXCLUDED));
+ /*
+ * Document-const: Notmuch::TAG_MAX
+ *
+ * Maximum allowed length of a tag
+ */
+ rb_define_const (mod, "TAG_MAX", INT2FIX (NOTMUCH_TAG_MAX));
+ /*
+ * Document-const: Notmuch::EXCLUDE_FLAG
+ *
+ * Only flag excluded results
+ */
+ rb_define_const (mod, "EXCLUDE_FLAG", INT2FIX (NOTMUCH_EXCLUDE_FLAG));
+ /*
+ * Document-const: Notmuch::EXCLUDE_TRUE
+ *
+ * Exclude messages from the results
+ */
+ rb_define_const (mod, "EXCLUDE_TRUE", INT2FIX (NOTMUCH_EXCLUDE_TRUE));
+ /*
+ * Document-const: Notmuch::EXCLUDE_FALSE
+ *
+ * Don't exclude anything
+ */
+ rb_define_const (mod, "EXCLUDE_FALSE", INT2FIX (NOTMUCH_EXCLUDE_FALSE));
+ /*
+ * Document-const: Notmuch::EXCLUDE_ALL
+ *
+ * Exclude all results
+ */
+ rb_define_const (mod, "EXCLUDE_ALL", INT2FIX (NOTMUCH_EXCLUDE_ALL));
+
+ /*
+ * Document-class: Notmuch::BaseError
+ *
+ * Base class for all notmuch exceptions
+ */
+ notmuch_rb_eBaseError = rb_define_class_under (mod, "BaseError", rb_eStandardError);
+ /*
+ * Document-class: Notmuch::DatabaseError
+ *
+ * Raised when the database can't be created or opened.
+ */
+ notmuch_rb_eDatabaseError = rb_define_class_under (mod, "DatabaseError", notmuch_rb_eBaseError);
+ /*
+ * Document-class: Notmuch::MemoryError
+ *
+ * Raised when notmuch is out of memory
+ */
+ notmuch_rb_eMemoryError = rb_define_class_under (mod, "MemoryError", notmuch_rb_eBaseError);
+ /*
+ * Document-class: Notmuch::ReadOnlyError
+ *
+ * Raised when an attempt was made to write to a database opened in read-only
+ * mode.
+ */
+ notmuch_rb_eReadOnlyError = rb_define_class_under (mod, "ReadOnlyError", notmuch_rb_eBaseError);
+ /*
+ * Document-class: Notmuch::XapianError
+ *
+ * Raised when a Xapian exception occurs
+ */
+ notmuch_rb_eXapianError = rb_define_class_under (mod, "XapianError", notmuch_rb_eBaseError);
+ /*
+ * Document-class: Notmuch::FileError
+ *
+ * Raised when an error occurs trying to read or write to a file.
+ */
+ notmuch_rb_eFileError = rb_define_class_under (mod, "FileError", notmuch_rb_eBaseError);
+ /*
+ * Document-class: Notmuch::FileNotEmailError
+ *
+ * Raised when a file is presented that doesn't appear to be an email message.
+ */
+ notmuch_rb_eFileNotEmailError = rb_define_class_under (mod, "FileNotEmailError", notmuch_rb_eBaseError);
+ /*
+ * Document-class: Notmuch::NullPointerError
+ *
+ * Raised when the user erroneously passes a +NULL+ pointer to a notmuch
+ * function.
+ */
+ notmuch_rb_eNullPointerError = rb_define_class_under (mod, "NullPointerError", notmuch_rb_eBaseError);
+ /*
+ * Document-class: Notmuch::TagTooLongError
+ *
+ * Raised when a tag value is too long (exceeds Notmuch::TAG_MAX)
+ */
+ notmuch_rb_eTagTooLongError = rb_define_class_under (mod, "TagTooLongError", notmuch_rb_eBaseError);
+ /*
+ * Document-class: Notmuch::UnbalancedFreezeThawError
+ *
+ * Raised when the notmuch_message_thaw function has been called more times
+ * than notmuch_message_freeze.
+ */
+ notmuch_rb_eUnbalancedFreezeThawError = rb_define_class_under (mod, "UnbalancedFreezeThawError",
+ notmuch_rb_eBaseError);
+ /*
+ * Document-class: Notmuch::UnbalancedAtomicError
+ *
+ * Raised when notmuch_database_end_atomic has been called more times than
+ * notmuch_database_begin_atomic
+ */
+ notmuch_rb_eUnbalancedAtomicError = rb_define_class_under (mod, "UnbalancedAtomicError",
+ notmuch_rb_eBaseError);
+ /*
+ * Document-class: Notmuch::Database
+ *
+ * Notmuch database interaction
+ */
+ notmuch_rb_cDatabase = rb_define_class_under (mod, "Database", rb_cObject);
+ rb_define_alloc_func (notmuch_rb_cDatabase, notmuch_rb_database_alloc);
+ rb_define_singleton_method (notmuch_rb_cDatabase, "open", notmuch_rb_database_open, -1); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "initialize", notmuch_rb_database_initialize, -1); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "destroy!", notmuch_rb_database_destroy, 0); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "close", notmuch_rb_database_close, 0); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "path", notmuch_rb_database_path, 0); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "version", notmuch_rb_database_version, 0); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "needs_upgrade?", notmuch_rb_database_needs_upgrade, 0); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "upgrade!", notmuch_rb_database_upgrade, 0); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "begin_atomic", notmuch_rb_database_begin_atomic, 0); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "end_atomic", notmuch_rb_database_end_atomic, 0); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "get_directory", notmuch_rb_database_get_directory, 1); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "add_message", notmuch_rb_database_add_message, 1); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "remove_message", notmuch_rb_database_remove_message, 1); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "find_message",
+ notmuch_rb_database_find_message, 1); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "find_message_by_filename",
+ notmuch_rb_database_find_message_by_filename, 1); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "all_tags", notmuch_rb_database_get_all_tags, 0); /* in database.c */
+ rb_define_method (notmuch_rb_cDatabase, "query", notmuch_rb_database_query_create, -1); /* in database.c */
+
+ /*
+ * Document-class: Notmuch::Directory
+ *
+ * Notmuch directory
+ */
+ notmuch_rb_cDirectory = rb_define_class_under (mod, "Directory", rb_cObject);
+ rb_undef_method (notmuch_rb_cDirectory, "initialize");
+ rb_define_method (notmuch_rb_cDirectory, "destroy!", notmuch_rb_directory_destroy, 0); /* in directory.c */
+ rb_define_method (notmuch_rb_cDirectory, "mtime", notmuch_rb_directory_get_mtime, 0); /* in directory.c */
+ rb_define_method (notmuch_rb_cDirectory, "mtime=", notmuch_rb_directory_set_mtime, 1); /* in directory.c */
+ rb_define_method (notmuch_rb_cDirectory, "child_files", notmuch_rb_directory_get_child_files, 0); /* in directory.c */
+ rb_define_method (notmuch_rb_cDirectory, "child_directories", notmuch_rb_directory_get_child_directories, 0); /* in directory.c */
+
+ /*
+ * Document-class: Notmuch::Query
+ *
+ * Notmuch query
+ */
+ notmuch_rb_cQuery = rb_define_class_under (mod, "Query", rb_cObject);
+ rb_undef_method (notmuch_rb_cQuery, "initialize");
+ rb_define_method (notmuch_rb_cQuery, "destroy!", notmuch_rb_query_destroy, 0); /* in query.c */
+ rb_define_method (notmuch_rb_cQuery, "sort", notmuch_rb_query_get_sort, 0); /* in query.c */
+ rb_define_method (notmuch_rb_cQuery, "sort=", notmuch_rb_query_set_sort, 1); /* in query.c */
+ rb_define_method (notmuch_rb_cQuery, "to_s", notmuch_rb_query_get_string, 0); /* in query.c */
+ rb_define_method (notmuch_rb_cQuery, "add_tag_exclude", notmuch_rb_query_add_tag_exclude, 1); /* in query.c */
+ rb_define_method (notmuch_rb_cQuery, "omit_excluded=", notmuch_rb_query_set_omit_excluded, 1); /* in query.c */
+ rb_define_method (notmuch_rb_cQuery, "search_threads", notmuch_rb_query_search_threads, 0); /* in query.c */
+ rb_define_method (notmuch_rb_cQuery, "search_messages", notmuch_rb_query_search_messages, 0); /* in query.c */
+ rb_define_method (notmuch_rb_cQuery, "count_messages", notmuch_rb_query_count_messages, 0); /* in query.c */
+ rb_define_method (notmuch_rb_cQuery, "count_threads", notmuch_rb_query_count_threads, 0); /* in query.c */
+
+ /*
+ * Document-class: Notmuch::Threads
+ *
+ * Notmuch threads
+ */
+ notmuch_rb_cThreads = rb_define_class_under (mod, "Threads", rb_cObject);
+ rb_undef_method (notmuch_rb_cThreads, "initialize");
+ rb_define_method (notmuch_rb_cThreads, "destroy!", notmuch_rb_threads_destroy, 0); /* in threads.c */
+ rb_define_method (notmuch_rb_cThreads, "each", notmuch_rb_threads_each, 0); /* in threads.c */
+ rb_include_module (notmuch_rb_cThreads, rb_mEnumerable);
+
+ /*
+ * Document-class: Notmuch::Messages
+ *
+ * Notmuch messages
+ */
+ notmuch_rb_cMessages = rb_define_class_under (mod, "Messages", rb_cObject);
+ rb_undef_method (notmuch_rb_cMessages, "initialize");
+ rb_define_method (notmuch_rb_cMessages, "destroy!", notmuch_rb_messages_destroy, 0); /* in messages.c */
+ rb_define_method (notmuch_rb_cMessages, "each", notmuch_rb_messages_each, 0); /* in messages.c */
+ rb_define_method (notmuch_rb_cMessages, "tags", notmuch_rb_messages_collect_tags, 0); /* in messages.c */
+ rb_include_module (notmuch_rb_cMessages, rb_mEnumerable);
+
+ /*
+ * Document-class: Notmuch::Thread
+ *
+ * Notmuch thread
+ */
+ notmuch_rb_cThread = rb_define_class_under (mod, "Thread", rb_cObject);
+ rb_undef_method (notmuch_rb_cThread, "initialize");
+ rb_define_method (notmuch_rb_cThread, "destroy!", notmuch_rb_thread_destroy, 0); /* in thread.c */
+ rb_define_method (notmuch_rb_cThread, "thread_id", notmuch_rb_thread_get_thread_id, 0); /* in thread.c */
+ rb_define_method (notmuch_rb_cThread, "total_messages", notmuch_rb_thread_get_total_messages, 0); /* in thread.c */
+ rb_define_method (notmuch_rb_cThread, "toplevel_messages", notmuch_rb_thread_get_toplevel_messages, 0); /* in thread.c */
+ rb_define_method (notmuch_rb_cThread, "messages", notmuch_rb_thread_get_messages, 0); /* in thread.c */
+ rb_define_method (notmuch_rb_cThread, "matched_messages", notmuch_rb_thread_get_matched_messages, 0); /* in thread.c */
+ rb_define_method (notmuch_rb_cThread, "authors", notmuch_rb_thread_get_authors, 0); /* in thread.c */
+ rb_define_method (notmuch_rb_cThread, "subject", notmuch_rb_thread_get_subject, 0); /* in thread.c */
+ rb_define_method (notmuch_rb_cThread, "oldest_date", notmuch_rb_thread_get_oldest_date, 0); /* in thread.c */
+ rb_define_method (notmuch_rb_cThread, "newest_date", notmuch_rb_thread_get_newest_date, 0); /* in thread.c */
+ rb_define_method (notmuch_rb_cThread, "tags", notmuch_rb_thread_get_tags, 0); /* in thread.c */
+
+ /*
+ * Document-class: Notmuch::Message
+ *
+ * Notmuch message
+ */
+ notmuch_rb_cMessage = rb_define_class_under (mod, "Message", rb_cObject);
+ rb_undef_method (notmuch_rb_cMessage, "initialize");
+ rb_define_method (notmuch_rb_cMessage, "destroy!", notmuch_rb_message_destroy, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "message_id", notmuch_rb_message_get_message_id, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "thread_id", notmuch_rb_message_get_thread_id, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "replies", notmuch_rb_message_get_replies, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "filename", notmuch_rb_message_get_filename, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "filenames", notmuch_rb_message_get_filenames, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "get_flag", notmuch_rb_message_get_flag, 1); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "set_flag", notmuch_rb_message_set_flag, 2); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "date", notmuch_rb_message_get_date, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "header", notmuch_rb_message_get_header, 1); /* in message.c */
+ rb_define_alias (notmuch_rb_cMessage, "[]", "header");
+ rb_define_method (notmuch_rb_cMessage, "tags", notmuch_rb_message_get_tags, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "add_tag", notmuch_rb_message_add_tag, 1); /* in message.c */
+ rb_define_alias (notmuch_rb_cMessage, "<<", "add_tag");
+ rb_define_method (notmuch_rb_cMessage, "remove_tag", notmuch_rb_message_remove_tag, 1); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "remove_all_tags", notmuch_rb_message_remove_all_tags, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "maildir_flags_to_tags", notmuch_rb_message_maildir_flags_to_tags, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "tags_to_maildir_flags", notmuch_rb_message_tags_to_maildir_flags, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "freeze", notmuch_rb_message_freeze, 0); /* in message.c */
+ rb_define_method (notmuch_rb_cMessage, "thaw", notmuch_rb_message_thaw, 0); /* in message.c */
+}
diff --git a/bindings/ruby/message.c b/bindings/ruby/message.c
new file mode 100644
index 00000000..13c182f6
--- /dev/null
+++ b/bindings/ruby/message.c
@@ -0,0 +1,366 @@
+/* The Ruby interface to the notmuch mail library
+ *
+ * Copyright © 2010, 2011 Ali Polatel
+ *
+ * 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 3 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, see https://www.gnu.org/licenses/ .
+ *
+ * Author: Ali Polatel <alip@exherbo.org>
+ */
+
+#include "defs.h"
+
+/*
+ * call-seq: MESSAGE.destroy! => nil
+ *
+ * Destroys the message, freeing all resources allocated for it.
+ */
+VALUE
+notmuch_rb_message_destroy (VALUE self)
+{
+ notmuch_rb_object_destroy (self, &notmuch_rb_message_type);
+
+ return Qnil;
+}
+
+/*
+ * call-seq: MESSAGE.message_id => String
+ *
+ * Get the message ID of 'message'.
+ */
+VALUE
+notmuch_rb_message_get_message_id (VALUE self)
+{
+ const char *msgid;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ msgid = notmuch_message_get_message_id (message);
+
+ return rb_str_new2 (msgid);
+}
+
+/*
+ * call-seq: MESSAGE.thread_id => String
+ *
+ * Get the thread ID of 'message'.
+ */
+VALUE
+notmuch_rb_message_get_thread_id (VALUE self)
+{
+ const char *tid;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ tid = notmuch_message_get_thread_id (message);
+
+ return rb_str_new2 (tid);
+}
+
+/*
+ * call-seq: MESSAGE.replies => MESSAGES
+ *
+ * Get a Notmuch::Messages enumerable for all of the replies to 'message'.
+ */
+VALUE
+notmuch_rb_message_get_replies (VALUE self)
+{
+ notmuch_messages_t *messages;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ messages = notmuch_message_get_replies (message);
+
+ return Data_Wrap_Notmuch_Object (notmuch_rb_cMessages, &notmuch_rb_messages_type, messages);
+}
+
+/*
+ * call-seq: MESSAGE.filename => String
+ *
+ * Get a filename for the email corresponding to 'message'
+ */
+VALUE
+notmuch_rb_message_get_filename (VALUE self)
+{
+ const char *fname;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ fname = notmuch_message_get_filename (message);
+
+ return rb_str_new2 (fname);
+}
+
+/*
+ * call-seq: MESSAGE.filenames => FILENAMES
+ *
+ * Get all filenames for the email corresponding to MESSAGE.
+ */
+VALUE
+notmuch_rb_message_get_filenames (VALUE self)
+{
+ notmuch_filenames_t *fnames;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ fnames = notmuch_message_get_filenames (message);
+
+ return notmuch_rb_filenames_get (fnames);
+}
+
+/*
+ * call-seq: MESSAGE.get_flag (flag) => true or false
+ *
+ * Get a value of a flag for the email corresponding to 'message'
+ */
+VALUE
+notmuch_rb_message_get_flag (VALUE self, VALUE flagv)
+{
+ notmuch_message_t *message;
+ notmuch_bool_t is_set;
+ notmuch_status_t status;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ if (!FIXNUM_P (flagv))
+ rb_raise (rb_eTypeError, "Flag not a Fixnum");
+
+ status = notmuch_message_get_flag_st (message, FIX2INT (flagv), &is_set);
+ notmuch_rb_status_raise (status);
+
+ return is_set ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq: MESSAGE.set_flag (flag, value) => nil
+ *
+ * Set a value of a flag for the email corresponding to 'message'
+ */
+VALUE
+notmuch_rb_message_set_flag (VALUE self, VALUE flagv, VALUE valuev)
+{
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ if (!FIXNUM_P (flagv))
+ rb_raise (rb_eTypeError, "Flag not a Fixnum");
+
+ notmuch_message_set_flag (message, FIX2INT (flagv), RTEST (valuev));
+
+ return Qnil;
+}
+
+/*
+ * call-seq: MESSAGE.date => Fixnum
+ *
+ * Get the date of 'message'
+ */
+VALUE
+notmuch_rb_message_get_date (VALUE self)
+{
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ return UINT2NUM (notmuch_message_get_date (message));
+}
+
+/*
+ * call-seq: MESSAGE.header (name) => String
+ *
+ * Get the value of the specified header from 'message'
+ */
+VALUE
+notmuch_rb_message_get_header (VALUE self, VALUE headerv)
+{
+ const char *header, *value;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ SafeStringValue (headerv);
+ header = RSTRING_PTR (headerv);
+
+ value = notmuch_message_get_header (message, header);
+ if (!value)
+ rb_raise (notmuch_rb_eMemoryError, "Out of memory");
+
+ return rb_str_new2 (value);
+}
+
+/*
+ * call-seq: MESSAGE.tags => TAGS
+ *
+ * Get a Notmuch::Tags enumerable for all of the tags of 'message'.
+ */
+VALUE
+notmuch_rb_message_get_tags (VALUE self)
+{
+ notmuch_message_t *message;
+ notmuch_tags_t *tags;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ tags = notmuch_message_get_tags (message);
+ if (!tags)
+ rb_raise (notmuch_rb_eMemoryError, "Out of memory");
+
+ return notmuch_rb_tags_get (tags);
+}
+
+/*
+ * call-seq: MESSAGE.add_tag (tag) => true
+ *
+ * Add a tag to the 'message'
+ */
+VALUE
+notmuch_rb_message_add_tag (VALUE self, VALUE tagv)
+{
+ const char *tag;
+ notmuch_status_t ret;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ SafeStringValue (tagv);
+ tag = RSTRING_PTR (tagv);
+
+ ret = notmuch_message_add_tag (message, tag);
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
+
+/*
+ * call-seq: MESSAGE.remove_tag (tag) => true
+ *
+ * Remove a tag from the 'message'
+ */
+VALUE
+notmuch_rb_message_remove_tag (VALUE self, VALUE tagv)
+{
+ const char *tag;
+ notmuch_status_t ret;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ SafeStringValue (tagv);
+ tag = RSTRING_PTR (tagv);
+
+ ret = notmuch_message_remove_tag (message, tag);
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
+
+/*
+ * call-seq: MESSAGE.remove_all_tags => true
+ *
+ * Remove all tags of the 'message'
+ */
+VALUE
+notmuch_rb_message_remove_all_tags (VALUE self)
+{
+ notmuch_status_t ret;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ ret = notmuch_message_remove_all_tags (message);
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
+
+/*
+ * call-seq: MESSAGE.maildir_flags_to_tags => true
+ *
+ * Add/remove tags according to maildir flags in the message filename (s)
+ */
+VALUE
+notmuch_rb_message_maildir_flags_to_tags (VALUE self)
+{
+ notmuch_status_t ret;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ ret = notmuch_message_maildir_flags_to_tags (message);
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
+
+/*
+ * call-seq: MESSAGE.tags_to_maildir_flags => true
+ *
+ * Rename message filename (s) to encode tags as maildir flags
+ */
+VALUE
+notmuch_rb_message_tags_to_maildir_flags (VALUE self)
+{
+ notmuch_status_t ret;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ ret = notmuch_message_tags_to_maildir_flags (message);
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
+
+/*
+ * call-seq: MESSAGE.freeze => true
+ *
+ * Freeze the 'message'
+ */
+VALUE
+notmuch_rb_message_freeze (VALUE self)
+{
+ notmuch_status_t ret;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ ret = notmuch_message_freeze (message);
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
+
+/*
+ * call-seq: MESSAGE.thaw => true
+ *
+ * Thaw a 'message'
+ */
+VALUE
+notmuch_rb_message_thaw (VALUE self)
+{
+ notmuch_status_t ret;
+ notmuch_message_t *message;
+
+ Data_Get_Notmuch_Message (self, message);
+
+ ret = notmuch_message_thaw (message);
+ notmuch_rb_status_raise (ret);
+
+ return Qtrue;
+}
diff --git a/bindings/ruby/messages.c b/bindings/ruby/messages.c
new file mode 100644
index 00000000..6369d052
--- /dev/null
+++ b/bindings/ruby/messages.c
@@ -0,0 +1,75 @@
+/* The Ruby interface to the notmuch mail library
+ *
+ * Copyright © 2010, 2011 Ali Polatel
+ *
+ * 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 3 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, see https://www.gnu.org/licenses/ .
+ *
+ * Author: Ali Polatel <alip@exherbo.org>
+ */
+
+#include "defs.h"
+
+/*
+ * call-seq: MESSAGES.destroy! => nil
+ *
+ * Destroys the messages, freeing all resources allocated for it.
+ */
+VALUE
+notmuch_rb_messages_destroy (VALUE self)
+{
+ notmuch_rb_object_destroy (self, &notmuch_rb_messages_type);
+
+ return Qnil;
+}
+
+/* call-seq: MESSAGES.each {|item| block } => MESSAGES
+ *
+ * Calls +block+ once for each message in +self+, passing that element as a
+ * parameter.
+ */
+VALUE
+notmuch_rb_messages_each (VALUE self)
+{
+ notmuch_message_t *message;
+ notmuch_messages_t *messages;
+
+ Data_Get_Notmuch_Messages (self, messages);
+
+ for (; notmuch_messages_valid (messages); notmuch_messages_move_to_next (messages)) {
+ message = notmuch_messages_get (messages);
+ rb_yield (Data_Wrap_Notmuch_Object (notmuch_rb_cMessage, &notmuch_rb_message_type, message));
+ }
+
+ return self;
+}
+
+/*
+ * call-seq: MESSAGES.tags => TAGS
+ *
+ * Collect tags from the messages
+ */
+VALUE
+notmuch_rb_messages_collect_tags (VALUE self)
+{
+ notmuch_tags_t *tags;
+ notmuch_messages_t *messages;
+
+ Data_Get_Notmuch_Messages (self, messages);
+
+ tags = notmuch_messages_collect_tags (messages);
+ if (!tags)
+ rb_raise (notmuch_rb_eMemoryError, "Out of memory");
+
+ return notmuch_rb_tags_get (tags);
+}
diff --git a/bindings/ruby/query.c b/bindings/ruby/query.c
new file mode 100644
index 00000000..077def02
--- /dev/null
+++ b/bindings/ruby/query.c
@@ -0,0 +1,206 @@
+/* The Ruby interface to the notmuch mail library
+ *
+ * Copyright © 2010, 2011, 2012 Ali Polatel
+ *
+ * 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 3 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, see https://www.gnu.org/licenses/ .
+ *
+ * Author: Ali Polatel <alip@exherbo.org>
+ */
+
+#include "defs.h"
+
+/*
+ * call-seq: QUERY.destroy! => nil
+ *
+ * Destroys the query, freeing all resources allocated for it.
+ */
+VALUE
+notmuch_rb_query_destroy (VALUE self)
+{
+ notmuch_rb_object_destroy (self, &notmuch_rb_query_type);
+
+ return Qnil;
+}
+
+/*
+ * call-seq: QUERY.sort => fixnum
+ *
+ * Get sort type of the +QUERY+
+ */
+VALUE
+notmuch_rb_query_get_sort (VALUE self)
+{
+ notmuch_query_t *query;
+
+ Data_Get_Notmuch_Query (self, query);
+
+ return INT2FIX (notmuch_query_get_sort (query));
+}
+
+/*
+ * call-seq: QUERY.sort=(fixnum) => nil
+ *
+ * Set sort type of the +QUERY+
+ */
+VALUE
+notmuch_rb_query_set_sort (VALUE self, VALUE sortv)
+{
+ notmuch_query_t *query;
+
+ Data_Get_Notmuch_Query (self, query);
+
+ if (!FIXNUM_P (sortv))
+ rb_raise (rb_eTypeError, "Not a Fixnum");
+
+ notmuch_query_set_sort (query, FIX2UINT (sortv));
+
+ return Qnil;
+}
+
+/*
+ * call-seq: QUERY.to_s => string
+ *
+ * Get query string of the +QUERY+
+ */
+VALUE
+notmuch_rb_query_get_string (VALUE self)
+{
+ notmuch_query_t *query;
+
+ Data_Get_Notmuch_Query (self, query);
+
+ return rb_str_new2 (notmuch_query_get_query_string (query));
+}
+
+/*
+ * call-seq: QUERY.add_tag_exclude(tag) => nil
+ *
+ * Add a tag that will be excluded from the query results by default.
+ */
+VALUE
+notmuch_rb_query_add_tag_exclude (VALUE self, VALUE tagv)
+{
+ notmuch_query_t *query;
+ const char *tag;
+
+ Data_Get_Notmuch_Query (self, query);
+ tag = RSTRING_PTR(tagv);
+
+ notmuch_query_add_tag_exclude(query, tag);
+ return Qnil;
+}
+
+/*
+ * call-seq: QUERY.omit_excluded=(fixnum) => nil
+ *
+ * Specify whether to omit excluded results or simply flag them.
+ * By default, this is set to +Notmuch::EXCLUDE_TRUE+.
+ */
+VALUE
+notmuch_rb_query_set_omit_excluded (VALUE self, VALUE omitv)
+{
+ notmuch_query_t *query;
+ notmuch_exclude_t value;
+
+ Data_Get_Notmuch_Query (self, query);
+
+ value = FIXNUM_P (omitv) ? FIX2UINT (omitv) : RTEST(omitv);
+ notmuch_query_set_omit_excluded (query, value);
+
+ return Qnil;
+}
+
+/*
+ * call-seq: QUERY.search_threads => THREADS
+ *
+ * Search for threads
+ */
+VALUE
+notmuch_rb_query_search_threads (VALUE self)
+{
+ notmuch_query_t *query;
+ notmuch_threads_t *threads;
+ notmuch_status_t status;
+
+ Data_Get_Notmuch_Query (self, query);
+
+ status = notmuch_query_search_threads (query, &threads);
+ if (status)
+ notmuch_rb_status_raise (status);
+
+ return Data_Wrap_Notmuch_Object (notmuch_rb_cThreads, &notmuch_rb_threads_type, threads);
+}
+
+/*
+ * call-seq: QUERY.search_messages => MESSAGES
+ *
+ * Search for messages
+ */
+VALUE
+notmuch_rb_query_search_messages (VALUE self)
+{
+ notmuch_query_t *query;
+ notmuch_messages_t *messages;
+ notmuch_status_t status;
+
+ Data_Get_Notmuch_Query (self, query);
+
+ status = notmuch_query_search_messages (query, &messages);
+ if (status)
+ notmuch_rb_status_raise (status);
+
+ return Data_Wrap_Notmuch_Object (notmuch_rb_cMessages, &notmuch_rb_messages_type, messages);
+}
+
+/*
+ * call-seq: QUERY.count_messages => Fixnum
+ *
+ * Return an estimate of the number of messages matching a search
+ */
+VALUE
+notmuch_rb_query_count_messages (VALUE self)
+{
+ notmuch_query_t *query;
+ notmuch_status_t status;
+ unsigned int count;
+
+ Data_Get_Notmuch_Query (self, query);
+
+ status = notmuch_query_count_messages (query, &count);
+ if (status)
+ notmuch_rb_status_raise (status);
+
+ return UINT2NUM(count);
+}
+
+/*
+ * call-seq: QUERY.count_threads => Fixnum
+ *
+ * Return an estimate of the number of threads matching a search
+ */
+VALUE
+notmuch_rb_query_count_threads (VALUE self)
+{
+ notmuch_query_t *query;
+ notmuch_status_t status;
+ unsigned int count;
+
+ Data_Get_Notmuch_Query (self, query);
+
+ status = notmuch_query_count_threads (query, &count);
+ if (status)
+ notmuch_rb_status_raise (status);
+
+ return UINT2NUM(count);
+}
diff --git a/bindings/ruby/rdoc.sh b/bindings/ruby/rdoc.sh
new file mode 100755
index 00000000..1e867ff9
--- /dev/null
+++ b/bindings/ruby/rdoc.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+if test -z "$RDOC"; then
+ RDOC=rdoc
+ if which rdoc19 >/dev/null 2>&1; then
+ RDOC=rdoc19
+ fi
+fi
+
+set -e
+set -x
+
+$RDOC --main 'Notmuch' --title 'Notmuch Ruby API' --op ruby *.c
+
+if test "$1" = "--upload"; then
+ rsync -avze ssh --delete --partial --progress ruby bach.exherbo.org:public_html/notmuch/
+fi
diff --git a/bindings/ruby/status.c b/bindings/ruby/status.c
new file mode 100644
index 00000000..a0f88633
--- /dev/null
+++ b/bindings/ruby/status.c
@@ -0,0 +1,51 @@
+/* The Ruby interface to the notmuch mail library
+ *
+ * Copyright © 2010, 2011 Ali Polatel
+ *
+ * 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 3 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, see https://www.gnu.org/licenses/ .
+ *
+ * Author: Ali Polatel <alip@exherbo.org>
+ */
+
+#include "defs.h"
+
+void
+notmuch_rb_status_raise (notmuch_status_t status)
+{
+ switch (status) {
+ case NOTMUCH_STATUS_SUCCESS:
+ case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
+ break;
+ case NOTMUCH_STATUS_OUT_OF_MEMORY:
+ rb_raise (notmuch_rb_eMemoryError, "out of memory");
+ case NOTMUCH_STATUS_READ_ONLY_DATABASE:
+ rb_raise (notmuch_rb_eReadOnlyError, "read-only database");
+ case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
+ rb_raise (notmuch_rb_eXapianError, "xapian exception");
+ case NOTMUCH_STATUS_FILE_ERROR:
+ rb_raise (notmuch_rb_eFileError, "failed to read/write file");
+ case NOTMUCH_STATUS_FILE_NOT_EMAIL:
+ rb_raise (notmuch_rb_eFileNotEmailError, "file not email");
+ case NOTMUCH_STATUS_NULL_POINTER:
+ rb_raise (notmuch_rb_eNullPointerError, "null pointer");
+ case NOTMUCH_STATUS_TAG_TOO_LONG:
+ rb_raise (notmuch_rb_eTagTooLongError, "tag too long");
+ case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
+ rb_raise (notmuch_rb_eUnbalancedFreezeThawError, "unbalanced freeze/thaw");
+ case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
+ rb_raise (notmuch_rb_eUnbalancedAtomicError, "unbalanced atomic");
+ default:
+ rb_raise (notmuch_rb_eBaseError, "unknown notmuch error");
+ }
+}
diff --git a/bindings/ruby/tags.c b/bindings/ruby/tags.c
new file mode 100644
index 00000000..b64874d1
--- /dev/null
+++ b/bindings/ruby/tags.c
@@ -0,0 +1,13 @@
+#include "defs.h"
+
+VALUE
+notmuch_rb_tags_get (notmuch_tags_t *tags)
+{
+ VALUE rb_array = rb_ary_new ();
+
+ for (; notmuch_tags_valid (tags); notmuch_tags_move_to_next (tags)) {
+ const char *tag = notmuch_tags_get (tags);
+ rb_ary_push (rb_array, rb_str_new2 (tag));
+ }
+ return rb_array;
+}
diff --git a/bindings/ruby/thread.c b/bindings/ruby/thread.c
new file mode 100644
index 00000000..b20ed893
--- /dev/null
+++ b/bindings/ruby/thread.c
@@ -0,0 +1,208 @@
+/* The Ruby interface to the notmuch mail library
+ *
+ * Copyright © 2010, 2011 Ali Polatel
+ *
+ * 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 3 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, see https://www.gnu.org/licenses/ .
+ *
+ * Author: Ali Polatel <alip@exherbo.org>
+ */
+
+#include "defs.h"
+
+/*
+ * call-seq: THREAD.destroy! => nil
+ *
+ * Destroys the thread, freeing all resources allocated for it.
+ */
+VALUE
+notmuch_rb_thread_destroy (VALUE self)
+{
+ notmuch_rb_object_destroy (self, &notmuch_rb_thread_type);
+
+ return Qnil;
+}
+
+/*
+ * call-seq: THREAD.thread_id => String
+ *
+ * Returns the thread id
+ */
+VALUE
+notmuch_rb_thread_get_thread_id (VALUE self)
+{
+ const char *tid;
+ notmuch_thread_t *thread;
+
+ Data_Get_Notmuch_Thread (self, thread);
+
+ tid = notmuch_thread_get_thread_id (thread);
+
+ return rb_str_new2 (tid);
+}
+
+/*
+ * call-seq: THREAD.total_messages => fixnum
+ *
+ * Returns the number of total messages
+ */
+VALUE
+notmuch_rb_thread_get_total_messages (VALUE self)
+{
+ notmuch_thread_t *thread;
+
+ Data_Get_Notmuch_Thread (self, thread);
+
+ return INT2FIX (notmuch_thread_get_total_messages (thread));
+}
+
+/*
+ * call-seq: THREAD.toplevel_messages => MESSAGES
+ *
+ * Get a Notmuch::Messages iterator for the top level messages in thread.
+ */
+VALUE
+notmuch_rb_thread_get_toplevel_messages (VALUE self)
+{
+ notmuch_messages_t *messages;
+ notmuch_thread_t *thread;
+
+ Data_Get_Notmuch_Thread (self, thread);
+
+ messages = notmuch_thread_get_toplevel_messages (thread);
+ if (!messages)
+ rb_raise (notmuch_rb_eMemoryError, "Out of memory");
+
+ return Data_Wrap_Notmuch_Object (notmuch_rb_cMessages, &notmuch_rb_messages_type, messages);
+}
+
+/*
+ * call-seq: THREAD.messages => MESSAGES
+ *
+ * Get a Notmuch::Messages iterator for the all messages in thread.
+ */
+VALUE
+notmuch_rb_thread_get_messages (VALUE self)
+{
+ notmuch_messages_t *messages;
+ notmuch_thread_t *thread;
+
+ Data_Get_Notmuch_Thread (self, thread);
+
+ messages = notmuch_thread_get_messages (thread);
+ if (!messages)
+ rb_raise (notmuch_rb_eMemoryError, "Out of memory");
+
+ return Data_Wrap_Notmuch_Object (notmuch_rb_cMessages, &notmuch_rb_messages_type, messages);
+}
+
+/*
+ * call-seq: THREAD.matched_messages => fixnum
+ *
+ * Get the number of messages in thread that matched the search
+ */
+VALUE
+notmuch_rb_thread_get_matched_messages (VALUE self)
+{
+ notmuch_thread_t *thread;
+
+ Data_Get_Notmuch_Thread (self, thread);
+
+ return INT2FIX (notmuch_thread_get_matched_messages (thread));
+}
+
+/*
+ * call-seq: THREAD.authors => String
+ *
+ * Get a comma-separated list of the names of the authors.
+ */
+VALUE
+notmuch_rb_thread_get_authors (VALUE self)
+{
+ const char *authors;
+ notmuch_thread_t *thread;
+
+ Data_Get_Notmuch_Thread (self, thread);
+
+ authors = notmuch_thread_get_authors (thread);
+
+ return rb_str_new2 (authors);
+}
+
+/*
+ * call-seq: THREAD.subject => String
+ *
+ * Returns the subject of the thread
+ */
+VALUE
+notmuch_rb_thread_get_subject (VALUE self)
+{
+ const char *subject;
+ notmuch_thread_t *thread;
+
+ Data_Get_Notmuch_Thread (self, thread);
+
+ subject = notmuch_thread_get_subject (thread);
+
+ return rb_str_new2 (subject);
+}
+
+/*
+ * call-seq: THREAD.oldest_date => Fixnum
+ *
+ * Get the date of the oldest message in thread.
+ */
+VALUE
+notmuch_rb_thread_get_oldest_date (VALUE self)
+{
+ notmuch_thread_t *thread;
+
+ Data_Get_Notmuch_Thread (self, thread);
+
+ return UINT2NUM (notmuch_thread_get_oldest_date (thread));
+}
+
+/*
+ * call-seq: THREAD.newest_date => fixnum
+ *
+ * Get the date of the newest message in thread.
+ */
+VALUE
+notmuch_rb_thread_get_newest_date (VALUE self)
+{
+ notmuch_thread_t *thread;
+
+ Data_Get_Notmuch_Thread (self, thread);
+
+ return UINT2NUM (notmuch_thread_get_newest_date (thread));
+}
+
+/*
+ * call-seq: THREAD.tags => TAGS
+ *
+ * Get a Notmuch::Tags iterator for the tags of the thread
+ */
+VALUE
+notmuch_rb_thread_get_tags (VALUE self)
+{
+ notmuch_thread_t *thread;
+ notmuch_tags_t *tags;
+
+ Data_Get_Notmuch_Thread (self, thread);
+
+ tags = notmuch_thread_get_tags (thread);
+ if (!tags)
+ rb_raise (notmuch_rb_eMemoryError, "Out of memory");
+
+ return notmuch_rb_tags_get (tags);
+}
diff --git a/bindings/ruby/threads.c b/bindings/ruby/threads.c
new file mode 100644
index 00000000..50280260
--- /dev/null
+++ b/bindings/ruby/threads.c
@@ -0,0 +1,55 @@
+/* The Ruby interface to the notmuch mail library
+ *
+ * Copyright © 2010, 2011 Ali Polatel
+ *
+ * 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 3 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, see https://www.gnu.org/licenses/ .
+ *
+ * Author: Ali Polatel <alip@exherbo.org>
+ */
+
+#include "defs.h"
+
+/*
+ * call-seq: THREADS.destroy! => nil
+ *
+ * Destroys the threads, freeing all resources allocated for it.
+ */
+VALUE
+notmuch_rb_threads_destroy (VALUE self)
+{
+ notmuch_rb_object_destroy (self, &notmuch_rb_threads_type);
+
+ return Qnil;
+}
+
+/* call-seq: THREADS.each {|item| block } => THREADS
+ *
+ * Calls +block+ once for each thread in +self+, passing that element as a
+ * parameter.
+ */
+VALUE
+notmuch_rb_threads_each (VALUE self)
+{
+ notmuch_thread_t *thread;
+ notmuch_threads_t *threads;
+
+ Data_Get_Notmuch_Threads (self, threads);
+
+ for (; notmuch_threads_valid (threads); notmuch_threads_move_to_next (threads)) {
+ thread = notmuch_threads_get (threads);
+ rb_yield (Data_Wrap_Notmuch_Object (notmuch_rb_cThread, &notmuch_rb_thread_type, thread));
+ }
+
+ return self;
+}