1 // Wrapper around the notmuch library
6 #cgo LDFLAGS: -lnotmuch
16 // Status codes used for the return values of most functions
17 type Status C.notmuch_status_t
20 STATUS_SUCCESS Status = iota
22 STATUS_READ_ONLY_DATABASE
23 STATUS_XAPIAN_EXCEPTION
26 STATUS_DUPLICATE_MESSAGE_ID
29 STATUS_UNBALANCED_FREEZE_THAW
30 STATUS_UNBALANCED_ATOMIC
35 func (self Status) String() string {
39 p = C.notmuch_status_to_string(C.notmuch_status_t(self))
47 /* Various opaque data types. For each notmuch_<foo>_t see the various
48 * notmuch_<foo> functions below. */
50 type Database struct {
51 db *C.notmuch_database_t
55 query *C.notmuch_query_t
59 threads *C.notmuch_threads_t
63 thread *C.notmuch_thread_t
66 type Messages struct {
67 messages *C.notmuch_messages_t
71 message *C.notmuch_message_t
75 tags *C.notmuch_tags_t
78 type Directory struct {
79 dir *C.notmuch_directory_t
82 type Filenames struct {
83 fnames *C.notmuch_filenames_t
86 type DatabaseMode C.notmuch_database_mode_t
89 DATABASE_MODE_READ_ONLY DatabaseMode = iota
90 DATABASE_MODE_READ_WRITE
93 // Create a new, empty notmuch database located at 'path'
94 func NewDatabase(path string) (*Database, Status) {
96 var c_path *C.char = C.CString(path)
97 defer C.free(unsafe.Pointer(c_path))
100 return nil, STATUS_OUT_OF_MEMORY
103 self := &Database{db: nil}
104 st := Status(C.notmuch_database_create(c_path, &self.db))
105 if st != STATUS_SUCCESS {
111 /* Open an existing notmuch database located at 'path'.
113 * The database should have been created at some time in the past,
114 * (not necessarily by this process), by calling
115 * notmuch_database_create with 'path'. By default the database should be
116 * opened for reading only. In order to write to the database you need to
117 * pass the NOTMUCH_DATABASE_MODE_READ_WRITE mode.
119 * An existing notmuch database can be identified by the presence of a
120 * directory named ".notmuch" below 'path'.
122 * The caller should call notmuch_database_destroy when finished with
125 * In case of any failure, this function returns NULL, (after printing
126 * an error message on stderr).
128 func OpenDatabase(path string, mode DatabaseMode) (*Database, Status) {
130 var c_path *C.char = C.CString(path)
131 defer C.free(unsafe.Pointer(c_path))
134 return nil, STATUS_OUT_OF_MEMORY
137 self := &Database{db: nil}
138 st := Status(C.notmuch_database_open(c_path, C.notmuch_database_mode_t(mode), &self.db))
139 if st != STATUS_SUCCESS {
145 /* Close the given notmuch database, freeing all associated
146 * resources. See notmuch_database_open. */
147 func (self *Database) Close() Status {
148 return Status(C.notmuch_database_destroy(self.db))
151 /* Return the database path of the given database.
153 func (self *Database) GetPath() string {
155 /* The return value is a string owned by notmuch so should not be
156 * modified nor freed by the caller. */
157 var p *C.char = C.notmuch_database_get_path(self.db)
165 /* Return the database format version of the given database. */
166 func (self *Database) GetVersion() uint {
167 return uint(C.notmuch_database_get_version(self.db))
170 /* Does this database need to be upgraded before writing to it?
172 * If this function returns TRUE then no functions that modify the
173 * database (notmuch_database_index_file, notmuch_message_add_tag,
174 * notmuch_directory_set_mtime, etc.) will work unless the function
175 * notmuch_database_upgrade is called successfully first. */
176 func (self *Database) NeedsUpgrade() bool {
177 do_upgrade := C.notmuch_database_needs_upgrade(self.db)
184 // TODO: notmuch_database_upgrade
186 /* Retrieve a directory object from the database for 'path'.
188 * Here, 'path' should be a path relative to the path of 'database'
189 * (see notmuch_database_get_path), or else should be an absolute path
190 * with initial components that match the path of 'database'.
192 * Can return NULL if a Xapian exception occurs.
194 func (self *Database) GetDirectory(path string) (*Directory, Status) {
195 var c_path *C.char = C.CString(path)
196 defer C.free(unsafe.Pointer(c_path))
199 return nil, STATUS_OUT_OF_MEMORY
202 var c_dir *C.notmuch_directory_t
203 st := Status(C.notmuch_database_get_directory(self.db, c_path, &c_dir))
204 if st != STATUS_SUCCESS || c_dir == nil {
207 return &Directory{dir: c_dir}, st
210 /* Add a new message to the given notmuch database.
212 * Here,'filename' should be a path relative to the path of
213 * 'database' (see notmuch_database_get_path), or else should be an
214 * absolute filename with initial components that match the path of
217 * The file should be a single mail message (not a multi-message mbox)
218 * that is expected to remain at its current location, (since the
219 * notmuch database will reference the filename, and will not copy the
220 * entire contents of the file.
222 * If 'message' is not NULL, then, on successful return '*message'
223 * will be initialized to a message object that can be used for things
224 * such as adding tags to the just-added message. The user should call
225 * notmuch_message_destroy when done with the message. On any failure
226 * '*message' will be set to NULL.
230 * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
232 * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
235 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message
236 * ID as another message already in the database. The new
237 * filename was successfully added to the message in the database
238 * (if not already present).
240 * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
241 * file, (such as permission denied, or file not found,
242 * etc.). Nothing added to the database.
244 * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
245 * like an email message. Nothing added to the database.
247 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
248 * mode so no message can be added.
250 func (self *Database) AddMessage(fname string) (*Message, Status) {
251 var c_fname *C.char = C.CString(fname)
252 defer C.free(unsafe.Pointer(c_fname))
255 return nil, STATUS_OUT_OF_MEMORY
258 var c_msg *C.notmuch_message_t = new(C.notmuch_message_t)
259 st := Status(C.notmuch_database_add_message(self.db, c_fname, &c_msg))
261 return &Message{message: c_msg}, st
264 /* Remove a message from the given notmuch database.
266 * Note that only this particular filename association is removed from
267 * the database. If the same message (as determined by the message ID)
268 * is still available via other filenames, then the message will
269 * persist in the database for those filenames. When the last filename
270 * is removed for a particular message, the database content for that
271 * message will be entirely removed.
275 * NOTMUCH_STATUS_SUCCESS: The last filename was removed and the
276 * message was removed from the database.
278 * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
279 * message not removed.
281 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: This filename was removed but
282 * the message persists in the database with at least one other
285 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
286 * mode so no message can be removed.
288 func (self *Database) RemoveMessage(fname string) Status {
290 var c_fname *C.char = C.CString(fname)
291 defer C.free(unsafe.Pointer(c_fname))
294 return STATUS_OUT_OF_MEMORY
297 st := C.notmuch_database_remove_message(self.db, c_fname)
301 /* Find a message with the given message_id.
303 * If the database contains a message with the given message_id, then
304 * a new notmuch_message_t object is returned. The caller should call
305 * notmuch_message_destroy when done with the message.
307 * This function returns NULL in the following situations:
309 * * No message is found with the given message_id
310 * * An out-of-memory situation occurs
311 * * A Xapian exception occurs
313 func (self *Database) FindMessage(message_id string) (*Message, Status) {
315 var c_msg_id *C.char = C.CString(message_id)
316 defer C.free(unsafe.Pointer(c_msg_id))
319 return nil, STATUS_OUT_OF_MEMORY
322 msg := &Message{message: nil}
323 st := Status(C.notmuch_database_find_message(self.db, c_msg_id, &msg.message))
324 if st != STATUS_SUCCESS {
330 /* Return a list of all tags found in the database.
332 * This function creates a list of all tags found in the database. The
333 * resulting list contains all tags from all messages found in the database.
335 * On error this function returns NULL.
337 func (self *Database) GetAllTags() *Tags {
338 tags := C.notmuch_database_get_all_tags(self.db)
342 return &Tags{tags: tags}
345 /* Create a new query for 'database'.
347 * Here, 'database' should be an open database, (see
348 * notmuch_database_open and notmuch_database_create).
350 * For the query string, we'll document the syntax here more
351 * completely in the future, but it's likely to be a specialized
352 * version of the general Xapian query syntax:
354 * https://xapian.org/docs/queryparser.html
356 * As a special case, passing either a length-zero string, (that is ""),
357 * or a string consisting of a single asterisk (that is "*"), will
358 * result in a query that returns all messages in the database.
360 * See notmuch_query_set_sort for controlling the order of results.
361 * See notmuch_query_search_messages and notmuch_query_search_threads
362 * to actually execute the query.
364 * User should call notmuch_query_destroy when finished with this
367 * Will return NULL if insufficient memory is available.
369 func (self *Database) CreateQuery(query string) *Query {
371 var c_query *C.char = C.CString(query)
372 defer C.free(unsafe.Pointer(c_query))
378 q := C.notmuch_query_create(self.db, c_query)
382 return &Query{query: q}
385 /* Sort values for notmuch_query_set_sort */
386 type Sort C.notmuch_sort_t
389 SORT_OLDEST_FIRST Sort = 0
395 /* Return the query_string of this query. See notmuch_query_create. */
396 func (self *Query) String() string {
397 // FIXME: do we own 'q' or not ?
398 q := C.notmuch_query_get_query_string(self.query)
399 //defer C.free(unsafe.Pointer(q))
409 /* Specify the sorting desired for this query. */
410 func (self *Query) SetSort(sort Sort) {
411 C.notmuch_query_set_sort(self.query, C.notmuch_sort_t(sort))
414 /* Return the sort specified for this query. See notmuch_query_set_sort. */
415 func (self *Query) GetSort() Sort {
416 return Sort(C.notmuch_query_get_sort(self.query))
419 /* Execute a query for threads, returning a notmuch_threads_t object
420 * which can be used to iterate over the results. The returned threads
421 * object is owned by the query and as such, will only be valid until
422 * notmuch_query_destroy.
424 * Typical usage might be:
426 * notmuch_query_t *query;
427 * notmuch_threads_t *threads;
428 * notmuch_thread_t *thread;
430 * query = notmuch_query_create (database, query_string);
432 * for (threads = notmuch_query_search_threads (query);
433 * notmuch_threads_valid (threads);
434 * notmuch_threads_move_to_next (threads))
436 * thread = notmuch_threads_get (threads);
438 * notmuch_thread_destroy (thread);
441 * notmuch_query_destroy (query);
443 * Note: If you are finished with a thread before its containing
444 * query, you can call notmuch_thread_destroy to clean up some memory
445 * sooner (as in the above example). Otherwise, if your thread objects
446 * are long-lived, then you don't need to call notmuch_thread_destroy
447 * and all the memory will still be reclaimed when the query is
450 * Note that there's no explicit destructor needed for the
451 * notmuch_threads_t object. (For consistency, we do provide a
452 * notmuch_threads_destroy function, but there's no good reason
453 * to call it if the query is about to be destroyed).
455 * If a Xapian exception occurs this function will return NULL.
457 func (self *Query) SearchThreads() *Threads {
458 threads := C.notmuch_query_search_threads(self.query)
462 return &Threads{threads: threads}
465 /* Execute a query for messages, returning a notmuch_messages_t object
466 * which can be used to iterate over the results. The returned
467 * messages object is owned by the query and as such, will only be
468 * valid until notmuch_query_destroy.
470 * Typical usage might be:
472 * notmuch_query_t *query;
473 * notmuch_messages_t *messages;
474 * notmuch_message_t *message;
476 * query = notmuch_query_create (database, query_string);
478 * for (messages = notmuch_query_search_messages (query);
479 * notmuch_messages_valid (messages);
480 * notmuch_messages_move_to_next (messages))
482 * message = notmuch_messages_get (messages);
484 * notmuch_message_destroy (message);
487 * notmuch_query_destroy (query);
489 * Note: If you are finished with a message before its containing
490 * query, you can call notmuch_message_destroy to clean up some memory
491 * sooner (as in the above example). Otherwise, if your message
492 * objects are long-lived, then you don't need to call
493 * notmuch_message_destroy and all the memory will still be reclaimed
494 * when the query is destroyed.
496 * Note that there's no explicit destructor needed for the
497 * notmuch_messages_t object. (For consistency, we do provide a
498 * notmuch_messages_destroy function, but there's no good
499 * reason to call it if the query is about to be destroyed).
501 * If a Xapian exception occurs this function will return NULL.
503 func (self *Query) SearchMessages() *Messages {
504 msgs := C.notmuch_query_search_messages(self.query)
508 return &Messages{messages: msgs}
511 /* Destroy a notmuch_query_t along with any associated resources.
513 * This will in turn destroy any notmuch_threads_t and
514 * notmuch_messages_t objects generated by this query, (and in
515 * turn any notmuch_thread_t and notmuch_message_t objects generated
516 * from those results, etc.), if such objects haven't already been
519 func (self *Query) Destroy() {
520 if self.query != nil {
521 C.notmuch_query_destroy(self.query)
525 /* Return an estimate of the number of messages matching a search
527 * This function performs a search and returns Xapian's best
528 * guess as to number of matching messages.
530 * If a Xapian exception occurs, this function may return 0 (after
531 * printing a message).
533 func (self *Query) CountMessages() uint {
534 return uint(C.notmuch_query_count_messages(self.query))
537 /* Is the given 'threads' iterator pointing at a valid thread.
539 * When this function returns TRUE, notmuch_threads_get will return a
540 * valid object. Whereas when this function returns FALSE,
541 * notmuch_threads_get will return NULL.
543 * See the documentation of notmuch_query_search_threads for example
544 * code showing how to iterate over a notmuch_threads_t object.
546 func (self *Threads) Valid() bool {
547 if self.threads == nil {
550 valid := C.notmuch_threads_valid(self.threads)
557 /* Get the current thread from 'threads' as a notmuch_thread_t.
559 * Note: The returned thread belongs to 'threads' and has a lifetime
560 * identical to it (and the query to which it belongs).
562 * See the documentation of notmuch_query_search_threads for example
563 * code showing how to iterate over a notmuch_threads_t object.
565 * If an out-of-memory situation occurs, this function will return
568 func (self *Threads) Get() *Thread {
569 if self.threads == nil {
572 thread := C.notmuch_threads_get(self.threads)
576 return &Thread{thread}
579 /* Move the 'threads' iterator to the next thread.
581 * If 'threads' is already pointing at the last thread then the
582 * iterator will be moved to a point just beyond that last thread,
583 * (where notmuch_threads_valid will return FALSE and
584 * notmuch_threads_get will return NULL).
586 * See the documentation of notmuch_query_search_threads for example
587 * code showing how to iterate over a notmuch_threads_t object.
589 func (self *Threads) MoveToNext() {
590 if self.threads == nil {
593 C.notmuch_threads_move_to_next(self.threads)
596 /* Destroy a notmuch_threads_t object.
598 * It's not strictly necessary to call this function. All memory from
599 * the notmuch_threads_t object will be reclaimed when the
600 * containg query object is destroyed.
602 func (self *Threads) Destroy() {
603 if self.threads != nil {
604 C.notmuch_threads_destroy(self.threads)
609 * Get the thread ID of 'thread'.
611 * The returned string belongs to 'thread' and as such, should not be
612 * modified by the caller and will only be valid for as long as the
613 * thread is valid, (which is until notmuch_thread_destroy or until
614 * the query from which it derived is destroyed).
616 func (self *Thread) GetThreadId() string {
617 if self.thread == nil {
620 id := C.notmuch_thread_get_thread_id(self.thread)
624 return C.GoString(id)
628 * Get the total number of messages in 'thread'.
630 * This count consists of all messages in the database belonging to
631 * this thread. Contrast with notmuch_thread_get_matched_messages() .
633 func (self *Thread) GetTotalMessages() int {
634 if self.thread == nil {
637 return int(C.notmuch_thread_get_total_messages(self.thread))
641 * Get a notmuch_messages_t iterator for the top-level messages in
642 * 'thread' in oldest-first order.
644 * This iterator will not necessarily iterate over all of the messages
645 * in the thread. It will only iterate over the messages in the thread
646 * which are not replies to other messages in the thread.
648 * The returned list will be destroyed when the thread is destroyed.
650 func (self *Thread) GetToplevelMessages() (*Messages, Status) {
651 if self.thread == nil {
652 return nil, STATUS_NULL_POINTER
655 msgs := C.notmuch_thread_get_toplevel_messages(self.thread)
657 return nil, STATUS_NULL_POINTER
659 return &Messages{msgs}, STATUS_SUCCESS
663 * Get a notmuch_thread_t iterator for all messages in 'thread' in
664 * oldest-first order.
666 * The returned list will be destroyed when the thread is destroyed.
668 func (self *Thread) GetMessages() (*Messages, Status) {
669 if self.thread == nil {
670 return nil, STATUS_NULL_POINTER
673 msgs := C.notmuch_thread_get_messages(self.thread)
675 return nil, STATUS_NULL_POINTER
677 return &Messages{msgs}, STATUS_SUCCESS
681 * Get the number of messages in 'thread' that matched the search.
683 * This count includes only the messages in this thread that were
684 * matched by the search from which the thread was created and were
685 * not excluded by any exclude tags passed in with the query (see
686 * notmuch_query_add_tag_exclude). Contrast with
687 * notmuch_thread_get_total_messages() .
689 func (self *Thread) GetMatchedMessages() int {
690 if self.thread == nil {
693 return int(C.notmuch_thread_get_matched_messages(self.thread))
697 * Get the authors of 'thread' as a UTF-8 string.
699 * The returned string is a comma-separated list of the names of the
700 * authors of mail messages in the query results that belong to this
703 * The string contains authors of messages matching the query first, then
704 * non-matched authors (with the two groups separated by '|'). Within
705 * each group, authors are ordered by date.
707 * The returned string belongs to 'thread' and as such, should not be
708 * modified by the caller and will only be valid for as long as the
709 * thread is valid, (which is until notmuch_thread_destroy or until
710 * the query from which it derived is destroyed).
712 func (self *Thread) GetAuthors() string {
713 if self.thread == nil {
716 str := C.notmuch_thread_get_authors(self.thread)
720 return C.GoString(str)
724 * Get the subject of 'thread' as a UTF-8 string.
726 * The subject is taken from the first message (according to the query
727 * order---see notmuch_query_set_sort) in the query results that
728 * belongs to this thread.
730 * The returned string belongs to 'thread' and as such, should not be
731 * modified by the caller and will only be valid for as long as the
732 * thread is valid, (which is until notmuch_thread_destroy or until
733 * the query from which it derived is destroyed).
735 func (self *Thread) GetSubject() string {
736 if self.thread == nil {
739 str := C.notmuch_thread_get_subject(self.thread)
743 return C.GoString(str)
747 * Get the date of the oldest message in 'thread' as a time_t value.
749 func (self *Thread) GetOldestDate() int64 {
750 if self.thread == nil {
753 date := C.notmuch_thread_get_oldest_date(self.thread)
759 * Get the date of the newest message in 'thread' as a time_t value.
761 func (self *Thread) GetNewestDate() int64 {
762 if self.thread == nil {
765 date := C.notmuch_thread_get_newest_date(self.thread)
771 * Get the tags for 'thread', returning a notmuch_tags_t object which
772 * can be used to iterate over all tags.
774 * Note: In the Notmuch database, tags are stored on individual
775 * messages, not on threads. So the tags returned here will be all
776 * tags of the messages which matched the search and which belong to
779 * The tags object is owned by the thread and as such, will only be
780 * valid for as long as the thread is valid, (for example, until
781 * notmuch_thread_destroy or until the query from which it derived is
784 * Typical usage might be:
786 * notmuch_thread_t *thread;
787 * notmuch_tags_t *tags;
790 * thread = notmuch_threads_get (threads);
792 * for (tags = notmuch_thread_get_tags (thread);
793 * notmuch_tags_valid (tags);
794 * notmuch_tags_move_to_next (tags))
796 * tag = notmuch_tags_get (tags);
800 * notmuch_thread_destroy (thread);
802 * Note that there's no explicit destructor needed for the
803 * notmuch_tags_t object. (For consistency, we do provide a
804 * notmuch_tags_destroy function, but there's no good reason to call
805 * it if the message is about to be destroyed).
807 func (self *Thread) GetTags() *Tags {
808 if self.thread == nil {
812 tags := C.notmuch_thread_get_tags(self.thread)
821 * Destroy a notmuch_thread_t object.
823 func (self *Thread) Destroy() {
824 if self.thread != nil {
825 C.notmuch_thread_destroy(self.thread)
829 /* Is the given 'messages' iterator pointing at a valid message.
831 * When this function returns TRUE, notmuch_messages_get will return a
832 * valid object. Whereas when this function returns FALSE,
833 * notmuch_messages_get will return NULL.
835 * See the documentation of notmuch_query_search_messages for example
836 * code showing how to iterate over a notmuch_messages_t object.
838 func (self *Messages) Valid() bool {
839 if self.messages == nil {
842 valid := C.notmuch_messages_valid(self.messages)
849 /* Get the current message from 'messages' as a notmuch_message_t.
851 * Note: The returned message belongs to 'messages' and has a lifetime
852 * identical to it (and the query to which it belongs).
854 * See the documentation of notmuch_query_search_messages for example
855 * code showing how to iterate over a notmuch_messages_t object.
857 * If an out-of-memory situation occurs, this function will return
860 func (self *Messages) Get() *Message {
861 if self.messages == nil {
864 msg := C.notmuch_messages_get(self.messages)
868 return &Message{message: msg}
871 /* Move the 'messages' iterator to the next message.
873 * If 'messages' is already pointing at the last message then the
874 * iterator will be moved to a point just beyond that last message,
875 * (where notmuch_messages_valid will return FALSE and
876 * notmuch_messages_get will return NULL).
878 * See the documentation of notmuch_query_search_messages for example
879 * code showing how to iterate over a notmuch_messages_t object.
881 func (self *Messages) MoveToNext() {
882 if self.messages == nil {
885 C.notmuch_messages_move_to_next(self.messages)
888 /* Destroy a notmuch_messages_t object.
890 * It's not strictly necessary to call this function. All memory from
891 * the notmuch_messages_t object will be reclaimed when the containing
892 * query object is destroyed.
894 func (self *Messages) Destroy() {
895 if self.messages != nil {
896 C.notmuch_messages_destroy(self.messages)
900 /* Return a list of tags from all messages.
902 * The resulting list is guaranteed not to contain duplicated tags.
904 * WARNING: You can no longer iterate over messages after calling this
905 * function, because the iterator will point at the end of the list.
906 * We do not have a function to reset the iterator yet and the only
907 * way how you can iterate over the list again is to recreate the
910 * The function returns NULL on error.
912 func (self *Messages) CollectTags() *Tags {
913 if self.messages == nil {
916 tags := C.notmuch_messages_collect_tags(self.messages)
920 return &Tags{tags: tags}
923 /* Get the message ID of 'message'.
925 * The returned string belongs to 'message' and as such, should not be
926 * modified by the caller and will only be valid for as long as the
927 * message is valid, (which is until the query from which it derived
930 * This function will not return NULL since Notmuch ensures that every
931 * message has a unique message ID, (Notmuch will generate an ID for a
932 * message if the original file does not contain one).
934 func (self *Message) GetMessageId() string {
936 if self.message == nil {
939 id := C.notmuch_message_get_message_id(self.message)
941 // defer C.free(unsafe.Pointer(id))
945 return C.GoString(id)
948 /* Get the thread ID of 'message'.
950 * The returned string belongs to 'message' and as such, should not be
951 * modified by the caller and will only be valid for as long as the
952 * message is valid, (for example, until the user calls
953 * notmuch_message_destroy on 'message' or until a query from which it
954 * derived is destroyed).
956 * This function will not return NULL since Notmuch ensures that every
957 * message belongs to a single thread.
959 func (self *Message) GetThreadId() string {
961 if self.message == nil {
964 id := C.notmuch_message_get_thread_id(self.message)
966 // defer C.free(unsafe.Pointer(id))
972 return C.GoString(id)
975 /* Get a notmuch_messages_t iterator for all of the replies to
978 * Note: This call only makes sense if 'message' was ultimately
979 * obtained from a notmuch_thread_t object, (such as by coming
980 * directly from the result of calling notmuch_thread_get_
981 * toplevel_messages or by any number of subsequent
982 * calls to notmuch_message_get_replies).
984 * If 'message' was obtained through some non-thread means, (such as
985 * by a call to notmuch_query_search_messages), then this function
988 * If there are no replies to 'message', this function will return
989 * NULL. (Note that notmuch_messages_valid will accept that NULL
990 * value as legitimate, and simply return FALSE for it.)
992 func (self *Message) GetReplies() *Messages {
993 if self.message == nil {
996 msgs := C.notmuch_message_get_replies(self.message)
1000 return &Messages{messages: msgs}
1003 /* Get a filename for the email corresponding to 'message'.
1005 * The returned filename is an absolute filename, (the initial
1006 * component will match notmuch_database_get_path() ).
1008 * The returned string belongs to the message so should not be
1009 * modified or freed by the caller (nor should it be referenced after
1010 * the message is destroyed).
1012 * Note: If this message corresponds to multiple files in the mail
1013 * store, (that is, multiple files contain identical message IDs),
1014 * this function will arbitrarily return a single one of those
1017 func (self *Message) GetFileName() string {
1018 if self.message == nil {
1021 fname := C.notmuch_message_get_filename(self.message)
1022 // we dont own fname
1023 // defer C.free(unsafe.Pointer(fname))
1029 return C.GoString(fname)
1032 type Flag C.notmuch_message_flag_t
1035 MESSAGE_FLAG_MATCH Flag = 0
1038 /* Get a value of a flag for the email corresponding to 'message'. */
1039 func (self *Message) GetFlag(flag Flag) bool {
1040 if self.message == nil {
1043 v := C.notmuch_message_get_flag(self.message, C.notmuch_message_flag_t(flag))
1050 /* Set a value of a flag for the email corresponding to 'message'. */
1051 func (self *Message) SetFlag(flag Flag, value bool) {
1052 if self.message == nil {
1055 var v C.notmuch_bool_t = 0
1059 C.notmuch_message_set_flag(self.message, C.notmuch_message_flag_t(flag), v)
1062 /* Get the timestamp (seconds since the epoch) of 'message'.
1066 * NOTMUCH_STATUS_SUCCESS: Timestamp successfully retrieved
1068 * NOTMUCH_STATUS_NULL_POINTER: The 'message' argument is NULL
1071 func (self *Message) GetDate() (int64, Status) {
1072 if self.message == nil {
1073 return -1, STATUS_NULL_POINTER
1075 timestamp := C.notmuch_message_get_date(self.message)
1076 return int64(timestamp), STATUS_SUCCESS
1079 /* Get the value of the specified header from 'message'.
1081 * The value will be read from the actual message file, not from the
1082 * notmuch database. The header name is case insensitive.
1084 * The returned string belongs to the message so should not be
1085 * modified or freed by the caller (nor should it be referenced after
1086 * the message is destroyed).
1088 * Returns an empty string ("") if the message does not contain a
1089 * header line matching 'header'. Returns NULL if any error occurs.
1091 func (self *Message) GetHeader(header string) string {
1092 if self.message == nil {
1096 var c_header *C.char = C.CString(header)
1097 defer C.free(unsafe.Pointer(c_header))
1099 /* we dont own value */
1100 value := C.notmuch_message_get_header(self.message, c_header)
1105 return C.GoString(value)
1108 /* Get the tags for 'message', returning a notmuch_tags_t object which
1109 * can be used to iterate over all tags.
1111 * The tags object is owned by the message and as such, will only be
1112 * valid for as long as the message is valid, (which is until the
1113 * query from which it derived is destroyed).
1115 * Typical usage might be:
1117 * notmuch_message_t *message;
1118 * notmuch_tags_t *tags;
1121 * message = notmuch_database_find_message (database, message_id);
1123 * for (tags = notmuch_message_get_tags (message);
1124 * notmuch_tags_valid (tags);
1125 * notmuch_result_move_to_next (tags))
1127 * tag = notmuch_tags_get (tags);
1131 * notmuch_message_destroy (message);
1133 * Note that there's no explicit destructor needed for the
1134 * notmuch_tags_t object. (For consistency, we do provide a
1135 * notmuch_tags_destroy function, but there's no good reason to call
1136 * it if the message is about to be destroyed).
1138 func (self *Message) GetTags() *Tags {
1139 if self.message == nil {
1142 tags := C.notmuch_message_get_tags(self.message)
1146 return &Tags{tags: tags}
1149 /* The longest possible tag value. */
1152 /* Add a tag to the given message.
1156 * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
1158 * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
1160 * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
1161 * (exceeds NOTMUCH_TAG_MAX)
1163 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1164 * mode so message cannot be modified.
1166 func (self *Message) AddTag(tag string) Status {
1167 if self.message == nil {
1168 return STATUS_NULL_POINTER
1170 c_tag := C.CString(tag)
1171 defer C.free(unsafe.Pointer(c_tag))
1173 return Status(C.notmuch_message_add_tag(self.message, c_tag))
1176 /* Remove a tag from the given message.
1180 * NOTMUCH_STATUS_SUCCESS: Tag successfully removed from message
1182 * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
1184 * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
1185 * (exceeds NOTMUCH_TAG_MAX)
1187 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1188 * mode so message cannot be modified.
1190 func (self *Message) RemoveTag(tag string) Status {
1191 if self.message == nil {
1192 return STATUS_NULL_POINTER
1194 c_tag := C.CString(tag)
1195 defer C.free(unsafe.Pointer(c_tag))
1197 return Status(C.notmuch_message_remove_tag(self.message, c_tag))
1200 /* Remove all tags from the given message.
1202 * See notmuch_message_freeze for an example showing how to safely
1203 * replace tag values.
1205 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1206 * mode so message cannot be modified.
1208 func (self *Message) RemoveAllTags() Status {
1209 if self.message == nil {
1210 return STATUS_NULL_POINTER
1212 return Status(C.notmuch_message_remove_all_tags(self.message))
1215 /* Freeze the current state of 'message' within the database.
1217 * This means that changes to the message state, (via
1218 * notmuch_message_add_tag, notmuch_message_remove_tag, and
1219 * notmuch_message_remove_all_tags), will not be committed to the
1220 * database until the message is thawed with notmuch_message_thaw.
1222 * Multiple calls to freeze/thaw are valid and these calls will
1223 * "stack". That is there must be as many calls to thaw as to freeze
1224 * before a message is actually thawed.
1226 * The ability to do freeze/thaw allows for safe transactions to
1227 * change tag values. For example, explicitly setting a message to
1228 * have a given set of tags might look like this:
1230 * notmuch_message_freeze (message);
1232 * notmuch_message_remove_all_tags (message);
1234 * for (i = 0; i < NUM_TAGS; i++)
1235 * notmuch_message_add_tag (message, tags[i]);
1237 * notmuch_message_thaw (message);
1239 * With freeze/thaw used like this, the message in the database is
1240 * guaranteed to have either the full set of original tag values, or
1241 * the full set of new tag values, but nothing in between.
1243 * Imagine the example above without freeze/thaw and the operation
1244 * somehow getting interrupted. This could result in the message being
1245 * left with no tags if the interruption happened after
1246 * notmuch_message_remove_all_tags but before notmuch_message_add_tag.
1250 * NOTMUCH_STATUS_SUCCESS: Message successfully frozen.
1252 * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1253 * mode so message cannot be modified.
1255 func (self *Message) Freeze() Status {
1256 if self.message == nil {
1257 return STATUS_NULL_POINTER
1259 return Status(C.notmuch_message_freeze(self.message))
1262 /* Thaw the current 'message', synchronizing any changes that may have
1263 * occurred while 'message' was frozen into the notmuch database.
1265 * See notmuch_message_freeze for an example of how to use this
1266 * function to safely provide tag changes.
1268 * Multiple calls to freeze/thaw are valid and these calls with
1269 * "stack". That is there must be as many calls to thaw as to freeze
1270 * before a message is actually thawed.
1274 * NOTMUCH_STATUS_SUCCESS: Message successfully thawed, (or at least
1275 * its frozen count has successfully been reduced by 1).
1277 * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: An attempt was made to thaw
1278 * an unfrozen message. That is, there have been an unbalanced
1279 * number of calls to notmuch_message_freeze and
1280 * notmuch_message_thaw.
1282 func (self *Message) Thaw() Status {
1283 if self.message == nil {
1284 return STATUS_NULL_POINTER
1287 return Status(C.notmuch_message_thaw(self.message))
1290 /* Destroy a notmuch_message_t object.
1292 * It can be useful to call this function in the case of a single
1293 * query object with many messages in the result, (such as iterating
1294 * over the entire database). Otherwise, it's fine to never call this
1295 * function and there will still be no memory leaks. (The memory from
1296 * the messages get reclaimed when the containing query is destroyed.)
1298 func (self *Message) Destroy() {
1299 if self.message == nil {
1302 C.notmuch_message_destroy(self.message)
1305 /* Is the given 'tags' iterator pointing at a valid tag.
1307 * When this function returns TRUE, notmuch_tags_get will return a
1308 * valid string. Whereas when this function returns FALSE,
1309 * notmuch_tags_get will return NULL.
1311 * See the documentation of notmuch_message_get_tags for example code
1312 * showing how to iterate over a notmuch_tags_t object.
1314 func (self *Tags) Valid() bool {
1315 if self.tags == nil {
1318 v := C.notmuch_tags_valid(self.tags)
1325 /* Get the current tag from 'tags' as a string.
1327 * Note: The returned string belongs to 'tags' and has a lifetime
1328 * identical to it (and the query to which it ultimately belongs).
1330 * See the documentation of notmuch_message_get_tags for example code
1331 * showing how to iterate over a notmuch_tags_t object.
1333 func (self *Tags) Get() string {
1334 if self.tags == nil {
1337 s := C.notmuch_tags_get(self.tags)
1340 return C.GoString(s)
1342 func (self *Tags) String() string {
1346 /* Move the 'tags' iterator to the next tag.
1348 * If 'tags' is already pointing at the last tag then the iterator
1349 * will be moved to a point just beyond that last tag, (where
1350 * notmuch_tags_valid will return FALSE and notmuch_tags_get will
1353 * See the documentation of notmuch_message_get_tags for example code
1354 * showing how to iterate over a notmuch_tags_t object.
1356 func (self *Tags) MoveToNext() {
1357 if self.tags == nil {
1360 C.notmuch_tags_move_to_next(self.tags)
1363 /* Destroy a notmuch_tags_t object.
1365 * It's not strictly necessary to call this function. All memory from
1366 * the notmuch_tags_t object will be reclaimed when the containing
1367 * message or query objects are destroyed.
1369 func (self *Tags) Destroy() {
1370 if self.tags == nil {
1373 C.notmuch_tags_destroy(self.tags)
1376 // TODO: wrap notmuch_directory_<fct>
1378 /* Destroy a notmuch_directory_t object. */
1379 func (self *Directory) Destroy() {
1380 if self.dir == nil {
1383 C.notmuch_directory_destroy(self.dir)
1386 // TODO: wrap notmuch_filenames_<fct>
1388 /* Destroy a notmuch_filenames_t object.
1390 * It's not strictly necessary to call this function. All memory from
1391 * the notmuch_filenames_t object will be reclaimed when the
1392 * containing directory object is destroyed.
1394 * It is acceptable to pass NULL for 'filenames', in which case this
1395 * function will do nothing.
1397 func (self *Filenames) Destroy() {
1398 if self.fnames == nil {
1401 C.notmuch_filenames_destroy(self.fnames)