]> git.notmuchmail.org Git - notmuch/blob - bindings/go/src/notmuch/notmuch.go
debian: build depend on dh-python
[notmuch] / bindings / go / src / notmuch / notmuch.go
1 // Wrapper around the notmuch library
2
3 package notmuch
4
5 /*
6 #cgo LDFLAGS: -lnotmuch
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
11 #include "notmuch.h"
12 */
13 import "C"
14 import "unsafe"
15
16 // Status codes used for the return values of most functions
17 type Status C.notmuch_status_t
18
19 const (
20         STATUS_SUCCESS Status = iota
21         STATUS_OUT_OF_MEMORY
22         STATUS_READ_ONLY_DATABASE
23         STATUS_XAPIAN_EXCEPTION
24         STATUS_FILE_ERROR
25         STATUS_FILE_NOT_EMAIL
26         STATUS_DUPLICATE_MESSAGE_ID
27         STATUS_NULL_POINTER
28         STATUS_TAG_TOO_LONG
29         STATUS_UNBALANCED_FREEZE_THAW
30         STATUS_UNBALANCED_ATOMIC
31
32         STATUS_LAST_STATUS
33 )
34
35 func (self Status) String() string {
36         var p *C.char
37
38         // p is read-only
39         p = C.notmuch_status_to_string(C.notmuch_status_t(self))
40         if p != nil {
41                 s := C.GoString(p)
42                 return s
43         }
44         return ""
45 }
46
47 /* Various opaque data types. For each notmuch_<foo>_t see the various
48  * notmuch_<foo> functions below. */
49
50 type Database struct {
51         db *C.notmuch_database_t
52 }
53
54 type Query struct {
55         query *C.notmuch_query_t
56 }
57
58 type Threads struct {
59         threads *C.notmuch_threads_t
60 }
61
62 type Thread struct {
63         thread *C.notmuch_thread_t
64 }
65
66 type Messages struct {
67         messages *C.notmuch_messages_t
68 }
69
70 type Message struct {
71         message *C.notmuch_message_t
72 }
73
74 type Tags struct {
75         tags *C.notmuch_tags_t
76 }
77
78 type Directory struct {
79         dir *C.notmuch_directory_t
80 }
81
82 type Filenames struct {
83         fnames *C.notmuch_filenames_t
84 }
85
86 type DatabaseMode C.notmuch_database_mode_t
87
88 const (
89         DATABASE_MODE_READ_ONLY DatabaseMode = 0
90         DATABASE_MODE_READ_WRITE
91 )
92
93 // Create a new, empty notmuch database located at 'path'
94 func NewDatabase(path string) (*Database, Status) {
95
96         var c_path *C.char = C.CString(path)
97         defer C.free(unsafe.Pointer(c_path))
98
99         if c_path == nil {
100                 return nil, STATUS_OUT_OF_MEMORY
101         }
102
103         self := &Database{db: nil}
104         st := Status(C.notmuch_database_create(c_path, &self.db))
105         if st != STATUS_SUCCESS {
106                 return nil, st
107         }
108         return self, st
109 }
110
111 /* Open an existing notmuch database located at 'path'.
112  *
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.
118  *
119  * An existing notmuch database can be identified by the presence of a
120  * directory named ".notmuch" below 'path'.
121  *
122  * The caller should call notmuch_database_destroy when finished with
123  * this database.
124  *
125  * In case of any failure, this function returns NULL, (after printing
126  * an error message on stderr).
127  */
128 func OpenDatabase(path string, mode DatabaseMode) (*Database, Status) {
129
130         var c_path *C.char = C.CString(path)
131         defer C.free(unsafe.Pointer(c_path))
132
133         if c_path == nil {
134                 return nil, STATUS_OUT_OF_MEMORY
135         }
136
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 {
140                 return nil, st
141         }
142         return self, st
143 }
144
145 /* Close the given notmuch database, freeing all associated
146  * resources. See notmuch_database_open. */
147 func (self *Database) Close() {
148         C.notmuch_database_destroy(self.db)
149 }
150
151 /* Return the database path of the given database.
152  */
153 func (self *Database) GetPath() string {
154
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)
158         if p != nil {
159                 s := C.GoString(p)
160                 return s
161         }
162         return ""
163 }
164
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))
168 }
169
170 /* Does this database need to be upgraded before writing to it?
171  *
172  * If this function returns TRUE then no functions that modify the
173  * database (notmuch_database_add_message, 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)
178         if do_upgrade == 0 {
179                 return false
180         }
181         return true
182 }
183
184 // TODO: notmuch_database_upgrade
185
186 /* Retrieve a directory object from the database for 'path'.
187  *
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'.
191  *
192  * Can return NULL if a Xapian exception occurs.
193  */
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))
197
198         if c_path == nil {
199                 return nil, STATUS_OUT_OF_MEMORY
200         }
201
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 {
205                 return nil, st
206         }
207         return &Directory{dir: c_dir}, st
208 }
209
210 /* Add a new message to the given notmuch database.
211  *
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
215  * 'database'.
216  *
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.
221  *
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.
227  *
228  * Return value:
229  *
230  * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
231  *
232  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
233  *      message not added.
234  *
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).
239  *
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.
243  *
244  * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
245  *      like an email message. Nothing added to the database.
246  *
247  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
248  *      mode so no message can be added.
249  */
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))
253
254         if c_fname == nil {
255                 return nil, STATUS_OUT_OF_MEMORY
256         }
257
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))
260
261         return &Message{message: c_msg}, st
262 }
263
264 /* Remove a message from the given notmuch database.
265  *
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.
272  *
273  * Return value:
274  *
275  * NOTMUCH_STATUS_SUCCESS: The last filename was removed and the
276  *      message was removed from the database.
277  *
278  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
279  *      message not removed.
280  *
281  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: This filename was removed but
282  *      the message persists in the database with at least one other
283  *      filename.
284  *
285  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
286  *      mode so no message can be removed.
287  */
288 func (self *Database) RemoveMessage(fname string) Status {
289
290         var c_fname *C.char = C.CString(fname)
291         defer C.free(unsafe.Pointer(c_fname))
292
293         if c_fname == nil {
294                 return STATUS_OUT_OF_MEMORY
295         }
296
297         st := C.notmuch_database_remove_message(self.db, c_fname)
298         return Status(st)
299 }
300
301 /* Find a message with the given message_id.
302  *
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.
306  *
307  * This function returns NULL in the following situations:
308  *
309  *      * No message is found with the given message_id
310  *      * An out-of-memory situation occurs
311  *      * A Xapian exception occurs
312  */
313 func (self *Database) FindMessage(message_id string) (*Message, Status) {
314
315         var c_msg_id *C.char = C.CString(message_id)
316         defer C.free(unsafe.Pointer(c_msg_id))
317
318         if c_msg_id == nil {
319                 return nil, STATUS_OUT_OF_MEMORY
320         }
321
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 {
325                 return nil, st
326         }
327         return msg, st
328 }
329
330 /* Return a list of all tags found in the database.
331  *
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.
334  *
335  * On error this function returns NULL.
336  */
337 func (self *Database) GetAllTags() *Tags {
338         tags := C.notmuch_database_get_all_tags(self.db)
339         if tags == nil {
340                 return nil
341         }
342         return &Tags{tags: tags}
343 }
344
345 /* Create a new query for 'database'.
346  *
347  * Here, 'database' should be an open database, (see
348  * notmuch_database_open and notmuch_database_create).
349  *
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:
353  *
354  * http://xapian.org/docs/queryparser.html
355  *
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.
359  *
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.
363  *
364  * User should call notmuch_query_destroy when finished with this
365  * query.
366  *
367  * Will return NULL if insufficient memory is available.
368  */
369 func (self *Database) CreateQuery(query string) *Query {
370
371         var c_query *C.char = C.CString(query)
372         defer C.free(unsafe.Pointer(c_query))
373
374         if c_query == nil {
375                 return nil
376         }
377
378         q := C.notmuch_query_create(self.db, c_query)
379         if q == nil {
380                 return nil
381         }
382         return &Query{query: q}
383 }
384
385 /* Sort values for notmuch_query_set_sort */
386 type Sort C.notmuch_sort_t
387
388 const (
389         SORT_OLDEST_FIRST Sort = 0
390         SORT_NEWEST_FIRST
391         SORT_MESSAGE_ID
392         SORT_UNSORTED
393 )
394
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))
400
401         if q != nil {
402                 s := C.GoString(q)
403                 return s
404         }
405
406         return ""
407 }
408
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))
412 }
413
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))
417 }
418
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.
423  *
424  * Typical usage might be:
425  *
426  *     notmuch_query_t *query;
427  *     notmuch_threads_t *threads;
428  *     notmuch_thread_t *thread;
429  *
430  *     query = notmuch_query_create (database, query_string);
431  *
432  *     for (threads = notmuch_query_search_threads (query);
433  *          notmuch_threads_valid (threads);
434  *          notmuch_threads_move_to_next (threads))
435  *     {
436  *         thread = notmuch_threads_get (threads);
437  *         ....
438  *         notmuch_thread_destroy (thread);
439  *     }
440  *
441  *     notmuch_query_destroy (query);
442  *
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
448  * destroyed.
449  *
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).
454  *
455  * If a Xapian exception occurs this function will return NULL.
456  */
457 func (self *Query) SearchThreads() *Threads {
458         threads := C.notmuch_query_search_threads(self.query)
459         if threads == nil {
460                 return nil
461         }
462         return &Threads{threads: threads}
463 }
464
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.
469  *
470  * Typical usage might be:
471  *
472  *     notmuch_query_t *query;
473  *     notmuch_messages_t *messages;
474  *     notmuch_message_t *message;
475  *
476  *     query = notmuch_query_create (database, query_string);
477  *
478  *     for (messages = notmuch_query_search_messages (query);
479  *          notmuch_messages_valid (messages);
480  *          notmuch_messages_move_to_next (messages))
481  *     {
482  *         message = notmuch_messages_get (messages);
483  *         ....
484  *         notmuch_message_destroy (message);
485  *     }
486  *
487  *     notmuch_query_destroy (query);
488  *
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.
495  *
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).
500  *
501  * If a Xapian exception occurs this function will return NULL.
502  */
503 func (self *Query) SearchMessages() *Messages {
504         msgs := C.notmuch_query_search_messages(self.query)
505         if msgs == nil {
506                 return nil
507         }
508         return &Messages{messages: msgs}
509 }
510
511 /* Destroy a notmuch_query_t along with any associated resources.
512  *
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
517  * destroyed.
518  */
519 func (self *Query) Destroy() {
520         if self.query != nil {
521                 C.notmuch_query_destroy(self.query)
522         }
523 }
524
525 /* Return an estimate of the number of messages matching a search
526  *
527  * This function performs a search and returns Xapian's best
528  * guess as to number of matching messages.
529  *
530  * If a Xapian exception occurs, this function may return 0 (after
531  * printing a message).
532  */
533 func (self *Query) CountMessages() uint {
534         return uint(C.notmuch_query_count_messages(self.query))
535 }
536
537 // TODO: wrap threads and thread
538
539 /* Is the given 'threads' iterator pointing at a valid thread.
540  *
541  * When this function returns TRUE, notmuch_threads_get will return a
542  * valid object. Whereas when this function returns FALSE,
543  * notmuch_threads_get will return NULL.
544  *
545  * See the documentation of notmuch_query_search_threads for example
546  * code showing how to iterate over a notmuch_threads_t object.
547  */
548 func (self *Threads) Valid() bool {
549         if self.threads == nil {
550                 return false
551         }
552         valid := C.notmuch_threads_valid(self.threads)
553         if valid == 0 {
554                 return false
555         }
556         return true
557 }
558
559 /* Destroy a notmuch_threads_t object.
560  *
561  * It's not strictly necessary to call this function. All memory from
562  * the notmuch_threads_t object will be reclaimed when the
563  * containg query object is destroyed.
564  */
565 func (self *Threads) Destroy() {
566         if self.threads != nil {
567                 C.notmuch_threads_destroy(self.threads)
568         }
569 }
570
571 /* Is the given 'messages' iterator pointing at a valid message.
572  *
573  * When this function returns TRUE, notmuch_messages_get will return a
574  * valid object. Whereas when this function returns FALSE,
575  * notmuch_messages_get will return NULL.
576  *
577  * See the documentation of notmuch_query_search_messages for example
578  * code showing how to iterate over a notmuch_messages_t object.
579  */
580 func (self *Messages) Valid() bool {
581         if self.messages == nil {
582                 return false
583         }
584         valid := C.notmuch_messages_valid(self.messages)
585         if valid == 0 {
586                 return false
587         }
588         return true
589 }
590
591 /* Get the current message from 'messages' as a notmuch_message_t.
592  *
593  * Note: The returned message belongs to 'messages' and has a lifetime
594  * identical to it (and the query to which it belongs).
595  *
596  * See the documentation of notmuch_query_search_messages for example
597  * code showing how to iterate over a notmuch_messages_t object.
598  *
599  * If an out-of-memory situation occurs, this function will return
600  * NULL.
601  */
602 func (self *Messages) Get() *Message {
603         if self.messages == nil {
604                 return nil
605         }
606         msg := C.notmuch_messages_get(self.messages)
607         if msg == nil {
608                 return nil
609         }
610         return &Message{message: msg}
611 }
612
613 /* Move the 'messages' iterator to the next message.
614  *
615  * If 'messages' is already pointing at the last message then the
616  * iterator will be moved to a point just beyond that last message,
617  * (where notmuch_messages_valid will return FALSE and
618  * notmuch_messages_get will return NULL).
619  *
620  * See the documentation of notmuch_query_search_messages for example
621  * code showing how to iterate over a notmuch_messages_t object.
622  */
623 func (self *Messages) MoveToNext() {
624         if self.messages == nil {
625                 return
626         }
627         C.notmuch_messages_move_to_next(self.messages)
628 }
629
630 /* Destroy a notmuch_messages_t object.
631  *
632  * It's not strictly necessary to call this function. All memory from
633  * the notmuch_messages_t object will be reclaimed when the containing
634  * query object is destroyed.
635  */
636 func (self *Messages) Destroy() {
637         if self.messages != nil {
638                 C.notmuch_messages_destroy(self.messages)
639         }
640 }
641
642 /* Return a list of tags from all messages.
643  *
644  * The resulting list is guaranteed not to contain duplicated tags.
645  *
646  * WARNING: You can no longer iterate over messages after calling this
647  * function, because the iterator will point at the end of the list.
648  * We do not have a function to reset the iterator yet and the only
649  * way how you can iterate over the list again is to recreate the
650  * message list.
651  *
652  * The function returns NULL on error.
653  */
654 func (self *Messages) CollectTags() *Tags {
655         if self.messages == nil {
656                 return nil
657         }
658         tags := C.notmuch_messages_collect_tags(self.messages)
659         if tags == nil {
660                 return nil
661         }
662         return &Tags{tags: tags}
663 }
664
665 /* Get the message ID of 'message'.
666  *
667  * The returned string belongs to 'message' and as such, should not be
668  * modified by the caller and will only be valid for as long as the
669  * message is valid, (which is until the query from which it derived
670  * is destroyed).
671  *
672  * This function will not return NULL since Notmuch ensures that every
673  * message has a unique message ID, (Notmuch will generate an ID for a
674  * message if the original file does not contain one).
675  */
676 func (self *Message) GetMessageId() string {
677
678         if self.message == nil {
679                 return ""
680         }
681         id := C.notmuch_message_get_message_id(self.message)
682         // we dont own id
683         // defer C.free(unsafe.Pointer(id))
684         if id == nil {
685                 return ""
686         }
687         return C.GoString(id)
688 }
689
690 /* Get the thread ID of 'message'.
691  *
692  * The returned string belongs to 'message' and as such, should not be
693  * modified by the caller and will only be valid for as long as the
694  * message is valid, (for example, until the user calls
695  * notmuch_message_destroy on 'message' or until a query from which it
696  * derived is destroyed).
697  *
698  * This function will not return NULL since Notmuch ensures that every
699  * message belongs to a single thread.
700  */
701 func (self *Message) GetThreadId() string {
702
703         if self.message == nil {
704                 return ""
705         }
706         id := C.notmuch_message_get_thread_id(self.message)
707         // we dont own id
708         // defer C.free(unsafe.Pointer(id))
709
710         if id == nil {
711                 return ""
712         }
713
714         return C.GoString(id)
715 }
716
717 /* Get a notmuch_messages_t iterator for all of the replies to
718  * 'message'.
719  *
720  * Note: This call only makes sense if 'message' was ultimately
721  * obtained from a notmuch_thread_t object, (such as by coming
722  * directly from the result of calling notmuch_thread_get_
723  * toplevel_messages or by any number of subsequent
724  * calls to notmuch_message_get_replies).
725  *
726  * If 'message' was obtained through some non-thread means, (such as
727  * by a call to notmuch_query_search_messages), then this function
728  * will return NULL.
729  *
730  * If there are no replies to 'message', this function will return
731  * NULL. (Note that notmuch_messages_valid will accept that NULL
732  * value as legitimate, and simply return FALSE for it.)
733  */
734 func (self *Message) GetReplies() *Messages {
735         if self.message == nil {
736                 return nil
737         }
738         msgs := C.notmuch_message_get_replies(self.message)
739         if msgs == nil {
740                 return nil
741         }
742         return &Messages{messages: msgs}
743 }
744
745 /* Get a filename for the email corresponding to 'message'.
746  *
747  * The returned filename is an absolute filename, (the initial
748  * component will match notmuch_database_get_path() ).
749  *
750  * The returned string belongs to the message so should not be
751  * modified or freed by the caller (nor should it be referenced after
752  * the message is destroyed).
753  *
754  * Note: If this message corresponds to multiple files in the mail
755  * store, (that is, multiple files contain identical message IDs),
756  * this function will arbitrarily return a single one of those
757  * filenames.
758  */
759 func (self *Message) GetFileName() string {
760         if self.message == nil {
761                 return ""
762         }
763         fname := C.notmuch_message_get_filename(self.message)
764         // we dont own fname
765         // defer C.free(unsafe.Pointer(fname))
766
767         if fname == nil {
768                 return ""
769         }
770
771         return C.GoString(fname)
772 }
773
774 type Flag C.notmuch_message_flag_t
775
776 const (
777         MESSAGE_FLAG_MATCH Flag = 0
778 )
779
780 /* Get a value of a flag for the email corresponding to 'message'. */
781 func (self *Message) GetFlag(flag Flag) bool {
782         if self.message == nil {
783                 return false
784         }
785         v := C.notmuch_message_get_flag(self.message, C.notmuch_message_flag_t(flag))
786         if v == 0 {
787                 return false
788         }
789         return true
790 }
791
792 /* Set a value of a flag for the email corresponding to 'message'. */
793 func (self *Message) SetFlag(flag Flag, value bool) {
794         if self.message == nil {
795                 return
796         }
797         var v C.notmuch_bool_t = 0
798         if value {
799                 v = 1
800         }
801         C.notmuch_message_set_flag(self.message, C.notmuch_message_flag_t(flag), v)
802 }
803
804 // TODO: wrap notmuch_message_get_date
805
806 /* Get the value of the specified header from 'message'.
807  *
808  * The value will be read from the actual message file, not from the
809  * notmuch database. The header name is case insensitive.
810  *
811  * The returned string belongs to the message so should not be
812  * modified or freed by the caller (nor should it be referenced after
813  * the message is destroyed).
814  *
815  * Returns an empty string ("") if the message does not contain a
816  * header line matching 'header'. Returns NULL if any error occurs.
817  */
818 func (self *Message) GetHeader(header string) string {
819         if self.message == nil {
820                 return ""
821         }
822
823         var c_header *C.char = C.CString(header)
824         defer C.free(unsafe.Pointer(c_header))
825
826         /* we dont own value */
827         value := C.notmuch_message_get_header(self.message, c_header)
828         if value == nil {
829                 return ""
830         }
831
832         return C.GoString(value)
833 }
834
835 /* Get the tags for 'message', returning a notmuch_tags_t object which
836  * can be used to iterate over all tags.
837  *
838  * The tags object is owned by the message and as such, will only be
839  * valid for as long as the message is valid, (which is until the
840  * query from which it derived is destroyed).
841  *
842  * Typical usage might be:
843  *
844  *     notmuch_message_t *message;
845  *     notmuch_tags_t *tags;
846  *     const char *tag;
847  *
848  *     message = notmuch_database_find_message (database, message_id);
849  *
850  *     for (tags = notmuch_message_get_tags (message);
851  *          notmuch_tags_valid (tags);
852  *          notmuch_result_move_to_next (tags))
853  *     {
854  *         tag = notmuch_tags_get (tags);
855  *         ....
856  *     }
857  *
858  *     notmuch_message_destroy (message);
859  *
860  * Note that there's no explicit destructor needed for the
861  * notmuch_tags_t object. (For consistency, we do provide a
862  * notmuch_tags_destroy function, but there's no good reason to call
863  * it if the message is about to be destroyed).
864  */
865 func (self *Message) GetTags() *Tags {
866         if self.message == nil {
867                 return nil
868         }
869         tags := C.notmuch_message_get_tags(self.message)
870         if tags == nil {
871                 return nil
872         }
873         return &Tags{tags: tags}
874 }
875
876 /* The longest possible tag value. */
877 const TAG_MAX = 200
878
879 /* Add a tag to the given message.
880  *
881  * Return value:
882  *
883  * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
884  *
885  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
886  *
887  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
888  *      (exceeds NOTMUCH_TAG_MAX)
889  *
890  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
891  *      mode so message cannot be modified.
892  */
893 func (self *Message) AddTag(tag string) Status {
894         if self.message == nil {
895                 return STATUS_NULL_POINTER
896         }
897         c_tag := C.CString(tag)
898         defer C.free(unsafe.Pointer(c_tag))
899
900         return Status(C.notmuch_message_add_tag(self.message, c_tag))
901 }
902
903 /* Remove a tag from the given message.
904  *
905  * Return value:
906  *
907  * NOTMUCH_STATUS_SUCCESS: Tag successfully removed from message
908  *
909  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
910  *
911  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
912  *      (exceeds NOTMUCH_TAG_MAX)
913  *
914  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
915  *      mode so message cannot be modified.
916  */
917 func (self *Message) RemoveTag(tag string) Status {
918         if self.message == nil {
919                 return STATUS_NULL_POINTER
920         }
921         c_tag := C.CString(tag)
922         defer C.free(unsafe.Pointer(c_tag))
923
924         return Status(C.notmuch_message_remove_tag(self.message, c_tag))
925 }
926
927 /* Remove all tags from the given message.
928  *
929  * See notmuch_message_freeze for an example showing how to safely
930  * replace tag values.
931  *
932  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
933  *      mode so message cannot be modified.
934  */
935 func (self *Message) RemoveAllTags() Status {
936         if self.message == nil {
937                 return STATUS_NULL_POINTER
938         }
939         return Status(C.notmuch_message_remove_all_tags(self.message))
940 }
941
942 /* Freeze the current state of 'message' within the database.
943  *
944  * This means that changes to the message state, (via
945  * notmuch_message_add_tag, notmuch_message_remove_tag, and
946  * notmuch_message_remove_all_tags), will not be committed to the
947  * database until the message is thawed with notmuch_message_thaw.
948  *
949  * Multiple calls to freeze/thaw are valid and these calls will
950  * "stack". That is there must be as many calls to thaw as to freeze
951  * before a message is actually thawed.
952  *
953  * The ability to do freeze/thaw allows for safe transactions to
954  * change tag values. For example, explicitly setting a message to
955  * have a given set of tags might look like this:
956  *
957  *    notmuch_message_freeze (message);
958  *
959  *    notmuch_message_remove_all_tags (message);
960  *
961  *    for (i = 0; i < NUM_TAGS; i++)
962  *        notmuch_message_add_tag (message, tags[i]);
963  *
964  *    notmuch_message_thaw (message);
965  *
966  * With freeze/thaw used like this, the message in the database is
967  * guaranteed to have either the full set of original tag values, or
968  * the full set of new tag values, but nothing in between.
969  *
970  * Imagine the example above without freeze/thaw and the operation
971  * somehow getting interrupted. This could result in the message being
972  * left with no tags if the interruption happened after
973  * notmuch_message_remove_all_tags but before notmuch_message_add_tag.
974  *
975  * Return value:
976  *
977  * NOTMUCH_STATUS_SUCCESS: Message successfully frozen.
978  *
979  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
980  *      mode so message cannot be modified.
981  */
982 func (self *Message) Freeze() Status {
983         if self.message == nil {
984                 return STATUS_NULL_POINTER
985         }
986         return Status(C.notmuch_message_freeze(self.message))
987 }
988
989 /* Thaw the current 'message', synchronizing any changes that may have
990  * occurred while 'message' was frozen into the notmuch database.
991  *
992  * See notmuch_message_freeze for an example of how to use this
993  * function to safely provide tag changes.
994  *
995  * Multiple calls to freeze/thaw are valid and these calls with
996  * "stack". That is there must be as many calls to thaw as to freeze
997  * before a message is actually thawed.
998  *
999  * Return value:
1000  *
1001  * NOTMUCH_STATUS_SUCCESS: Message successfully thawed, (or at least
1002  *      its frozen count has successfully been reduced by 1).
1003  *
1004  * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: An attempt was made to thaw
1005  *      an unfrozen message. That is, there have been an unbalanced
1006  *      number of calls to notmuch_message_freeze and
1007  *      notmuch_message_thaw.
1008  */
1009 func (self *Message) Thaw() Status {
1010         if self.message == nil {
1011                 return STATUS_NULL_POINTER
1012         }
1013
1014         return Status(C.notmuch_message_thaw(self.message))
1015 }
1016
1017 /* Destroy a notmuch_message_t object.
1018  *
1019  * It can be useful to call this function in the case of a single
1020  * query object with many messages in the result, (such as iterating
1021  * over the entire database). Otherwise, it's fine to never call this
1022  * function and there will still be no memory leaks. (The memory from
1023  * the messages get reclaimed when the containing query is destroyed.)
1024  */
1025 func (self *Message) Destroy() {
1026         if self.message == nil {
1027                 return
1028         }
1029         C.notmuch_message_destroy(self.message)
1030 }
1031
1032 /* Is the given 'tags' iterator pointing at a valid tag.
1033  *
1034  * When this function returns TRUE, notmuch_tags_get will return a
1035  * valid string. Whereas when this function returns FALSE,
1036  * notmuch_tags_get will return NULL.
1037  *
1038  * See the documentation of notmuch_message_get_tags for example code
1039  * showing how to iterate over a notmuch_tags_t object.
1040  */
1041 func (self *Tags) Valid() bool {
1042         if self.tags == nil {
1043                 return false
1044         }
1045         v := C.notmuch_tags_valid(self.tags)
1046         if v == 0 {
1047                 return false
1048         }
1049         return true
1050 }
1051
1052 /* Get the current tag from 'tags' as a string.
1053  *
1054  * Note: The returned string belongs to 'tags' and has a lifetime
1055  * identical to it (and the query to which it ultimately belongs).
1056  *
1057  * See the documentation of notmuch_message_get_tags for example code
1058  * showing how to iterate over a notmuch_tags_t object.
1059  */
1060 func (self *Tags) Get() string {
1061         if self.tags == nil {
1062                 return ""
1063         }
1064         s := C.notmuch_tags_get(self.tags)
1065         // we dont own 's'
1066
1067         return C.GoString(s)
1068 }
1069 func (self *Tags) String() string {
1070         return self.Get()
1071 }
1072
1073 /* Move the 'tags' iterator to the next tag.
1074  *
1075  * If 'tags' is already pointing at the last tag then the iterator
1076  * will be moved to a point just beyond that last tag, (where
1077  * notmuch_tags_valid will return FALSE and notmuch_tags_get will
1078  * return NULL).
1079  *
1080  * See the documentation of notmuch_message_get_tags for example code
1081  * showing how to iterate over a notmuch_tags_t object.
1082  */
1083 func (self *Tags) MoveToNext() {
1084         if self.tags == nil {
1085                 return
1086         }
1087         C.notmuch_tags_move_to_next(self.tags)
1088 }
1089
1090 /* Destroy a notmuch_tags_t object.
1091  *
1092  * It's not strictly necessary to call this function. All memory from
1093  * the notmuch_tags_t object will be reclaimed when the containing
1094  * message or query objects are destroyed.
1095  */
1096 func (self *Tags) Destroy() {
1097         if self.tags == nil {
1098                 return
1099         }
1100         C.notmuch_tags_destroy(self.tags)
1101 }
1102
1103 // TODO: wrap notmuch_directory_<fct>
1104
1105 /* Destroy a notmuch_directory_t object. */
1106 func (self *Directory) Destroy() {
1107         if self.dir == nil {
1108                 return
1109         }
1110         C.notmuch_directory_destroy(self.dir)
1111 }
1112
1113 // TODO: wrap notmuch_filenames_<fct>
1114
1115 /* Destroy a notmuch_filenames_t object.
1116  *
1117  * It's not strictly necessary to call this function. All memory from
1118  * the notmuch_filenames_t object will be reclaimed when the
1119  * containing directory object is destroyed.
1120  *
1121  * It is acceptable to pass NULL for 'filenames', in which case this
1122  * function will do nothing.
1123  */
1124 func (self *Filenames) Destroy() {
1125         if self.fnames == nil {
1126                 return
1127         }
1128         C.notmuch_filenames_destroy(self.fnames)
1129 }
1130
1131 /* EOF */