]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/message.c
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / bindings / ruby / message.c
1 /* The Ruby interface to the notmuch mail library
2  *
3  * Copyright © 2010, 2011 Ali Polatel
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Author: Ali Polatel <alip@exherbo.org>
19  */
20
21 #include "defs.h"
22
23 /*
24  * call-seq: MESSAGE.destroy! => nil
25  *
26  * Destroys the message, freeing all resources allocated for it.
27  */
28 VALUE
29 notmuch_rb_message_destroy (VALUE self)
30 {
31     notmuch_message_t *message;
32
33     Data_Get_Notmuch_Message (self, message);
34
35     notmuch_message_destroy (message);
36     DATA_PTR (self) = NULL;
37
38     return Qnil;
39 }
40
41 /*
42  * call-seq: MESSAGE.message_id => String
43  *
44  * Get the message ID of 'message'.
45  */
46 VALUE
47 notmuch_rb_message_get_message_id (VALUE self)
48 {
49     const char *msgid;
50     notmuch_message_t *message;
51
52     Data_Get_Notmuch_Message (self, message);
53
54     msgid = notmuch_message_get_message_id (message);
55
56     return rb_str_new2 (msgid);
57 }
58
59 /*
60  * call-seq: MESSAGE.thread_id => String
61  *
62  * Get the thread ID of 'message'.
63  */
64 VALUE
65 notmuch_rb_message_get_thread_id (VALUE self)
66 {
67     const char *tid;
68     notmuch_message_t *message;
69
70     Data_Get_Notmuch_Message (self, message);
71
72     tid = notmuch_message_get_thread_id (message);
73
74     return rb_str_new2 (tid);
75 }
76
77 /*
78  * call-seq: MESSAGE.replies => MESSAGES
79  *
80  * Get a Notmuch::Messages enumerable for all of the replies to 'message'.
81  */
82 VALUE
83 notmuch_rb_message_get_replies (VALUE self)
84 {
85     notmuch_messages_t *messages;
86     notmuch_message_t *message;
87
88     Data_Get_Notmuch_Message (self, message);
89
90     messages = notmuch_message_get_replies (message);
91
92     return Data_Wrap_Struct (notmuch_rb_cMessages, NULL, NULL, messages);
93 }
94
95 /*
96  * call-seq: MESSAGE.filename => String
97  *
98  * Get a filename for the email corresponding to 'message'
99  */
100 VALUE
101 notmuch_rb_message_get_filename (VALUE self)
102 {
103     const char *fname;
104     notmuch_message_t *message;
105
106     Data_Get_Notmuch_Message (self, message);
107
108     fname = notmuch_message_get_filename (message);
109
110     return rb_str_new2 (fname);
111 }
112
113 /*
114  * call-seq: MESSAGE.filenames => FILENAMES
115  *
116  * Get all filenames for the email corresponding to MESSAGE.
117  */
118 VALUE
119 notmuch_rb_message_get_filenames (VALUE self)
120 {
121     notmuch_filenames_t *fnames;
122     notmuch_message_t *message;
123
124     Data_Get_Notmuch_Message (self, message);
125
126     fnames = notmuch_message_get_filenames (message);
127
128     return Data_Wrap_Struct (notmuch_rb_cFileNames, NULL, NULL, fnames);
129 }
130
131 /*
132  * call-seq: MESSAGE.get_flag (flag) => true or false
133  *
134  * Get a value of a flag for the email corresponding to 'message'
135  */
136 VALUE
137 notmuch_rb_message_get_flag (VALUE self, VALUE flagv)
138 {
139     notmuch_message_t *message;
140     notmuch_bool_t is_set;
141     notmuch_status_t status;
142
143     Data_Get_Notmuch_Message (self, message);
144
145     if (!FIXNUM_P (flagv))
146         rb_raise (rb_eTypeError, "Flag not a Fixnum");
147
148     status = notmuch_message_get_flag_st (message, FIX2INT (flagv), &is_set);
149     notmuch_rb_status_raise (status);
150
151     return is_set ? Qtrue : Qfalse;
152 }
153
154 /*
155  * call-seq: MESSAGE.set_flag (flag, value) => nil
156  *
157  * Set a value of a flag for the email corresponding to 'message'
158  */
159 VALUE
160 notmuch_rb_message_set_flag (VALUE self, VALUE flagv, VALUE valuev)
161 {
162     notmuch_message_t *message;
163
164     Data_Get_Notmuch_Message (self, message);
165
166     if (!FIXNUM_P (flagv))
167         rb_raise (rb_eTypeError, "Flag not a Fixnum");
168
169     notmuch_message_set_flag (message, FIX2INT (flagv), RTEST (valuev));
170
171     return Qnil;
172 }
173
174 /*
175  * call-seq: MESSAGE.date => Fixnum
176  *
177  * Get the date of 'message'
178  */
179 VALUE
180 notmuch_rb_message_get_date (VALUE self)
181 {
182     notmuch_message_t *message;
183
184     Data_Get_Notmuch_Message (self, message);
185
186     return UINT2NUM (notmuch_message_get_date (message));
187 }
188
189 /*
190  * call-seq: MESSAGE.header (name) => String
191  *
192  * Get the value of the specified header from 'message'
193  */
194 VALUE
195 notmuch_rb_message_get_header (VALUE self, VALUE headerv)
196 {
197     const char *header, *value;
198     notmuch_message_t *message;
199
200     Data_Get_Notmuch_Message (self, message);
201
202     SafeStringValue (headerv);
203     header = RSTRING_PTR (headerv);
204
205     value = notmuch_message_get_header (message, header);
206     if (!value)
207         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
208
209     return rb_str_new2 (value);
210 }
211
212 /*
213  * call-seq: MESSAGE.tags => TAGS
214  *
215  * Get a Notmuch::Tags enumerable for all of the tags of 'message'.
216  */
217 VALUE
218 notmuch_rb_message_get_tags (VALUE self)
219 {
220     notmuch_message_t *message;
221     notmuch_tags_t *tags;
222
223     Data_Get_Notmuch_Message (self, message);
224
225     tags = notmuch_message_get_tags (message);
226     if (!tags)
227         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
228
229     return Data_Wrap_Struct (notmuch_rb_cTags, NULL, NULL, tags);
230 }
231
232 /*
233  * call-seq: MESSAGE.add_tag (tag) => true
234  *
235  * Add a tag to the 'message'
236  */
237 VALUE
238 notmuch_rb_message_add_tag (VALUE self, VALUE tagv)
239 {
240     const char *tag;
241     notmuch_status_t ret;
242     notmuch_message_t *message;
243
244     Data_Get_Notmuch_Message (self, message);
245
246     SafeStringValue (tagv);
247     tag = RSTRING_PTR (tagv);
248
249     ret = notmuch_message_add_tag (message, tag);
250     notmuch_rb_status_raise (ret);
251
252     return Qtrue;
253 }
254
255 /*
256  * call-seq: MESSAGE.remove_tag (tag) => true
257  *
258  * Remove a tag from the 'message'
259  */
260 VALUE
261 notmuch_rb_message_remove_tag (VALUE self, VALUE tagv)
262 {
263     const char *tag;
264     notmuch_status_t ret;
265     notmuch_message_t *message;
266
267     Data_Get_Notmuch_Message (self, message);
268
269     SafeStringValue (tagv);
270     tag = RSTRING_PTR (tagv);
271
272     ret = notmuch_message_remove_tag (message, tag);
273     notmuch_rb_status_raise (ret);
274
275     return Qtrue;
276 }
277
278 /*
279  * call-seq: MESSAGE.remove_all_tags => true
280  *
281  * Remove all tags of the 'message'
282  */
283 VALUE
284 notmuch_rb_message_remove_all_tags (VALUE self)
285 {
286     notmuch_status_t ret;
287     notmuch_message_t *message;
288
289     Data_Get_Notmuch_Message (self, message);
290
291     ret = notmuch_message_remove_all_tags (message);
292     notmuch_rb_status_raise (ret);
293
294     return Qtrue;
295 }
296
297 /*
298  * call-seq: MESSAGE.maildir_flags_to_tags => true
299  *
300  * Add/remove tags according to maildir flags in the message filename (s)
301  */
302 VALUE
303 notmuch_rb_message_maildir_flags_to_tags (VALUE self)
304 {
305     notmuch_status_t ret;
306     notmuch_message_t *message;
307
308     Data_Get_Notmuch_Message (self, message);
309
310     ret = notmuch_message_maildir_flags_to_tags (message);
311     notmuch_rb_status_raise (ret);
312
313     return Qtrue;
314 }
315
316 /*
317  * call-seq: MESSAGE.tags_to_maildir_flags => true
318  *
319  * Rename message filename (s) to encode tags as maildir flags
320  */
321 VALUE
322 notmuch_rb_message_tags_to_maildir_flags (VALUE self)
323 {
324     notmuch_status_t ret;
325     notmuch_message_t *message;
326
327     Data_Get_Notmuch_Message (self, message);
328
329     ret = notmuch_message_tags_to_maildir_flags (message);
330     notmuch_rb_status_raise (ret);
331
332     return Qtrue;
333 }
334
335 /*
336  * call-seq: MESSAGE.freeze => true
337  *
338  * Freeze the 'message'
339  */
340 VALUE
341 notmuch_rb_message_freeze (VALUE self)
342 {
343     notmuch_status_t ret;
344     notmuch_message_t *message;
345
346     Data_Get_Notmuch_Message (self, message);
347
348     ret = notmuch_message_freeze (message);
349     notmuch_rb_status_raise (ret);
350
351     return Qtrue;
352 }
353
354 /*
355  * call-seq: MESSAGE.thaw => true
356  *
357  * Thaw a 'message'
358  */
359 VALUE
360 notmuch_rb_message_thaw (VALUE self)
361 {
362     notmuch_status_t ret;
363     notmuch_message_t *message;
364
365     Data_Get_Notmuch_Message (self, message);
366
367     ret = notmuch_message_thaw (message);
368     notmuch_rb_status_raise (ret);
369
370     return Qtrue;
371 }