]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/message.c
python: Improve documentation
[notmuch] / bindings / ruby / message.c
1 /* The Ruby interface to the notmuch mail library
2  *
3  * Copyright © 2010 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 http://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.filanames => 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
141     Data_Get_Notmuch_Message(self, message);
142
143     if (!FIXNUM_P(flagv))
144         rb_raise(rb_eTypeError, "Flag not a Fixnum");
145
146     return notmuch_message_get_flag(message, FIX2INT(flagv)) ? Qtrue : Qfalse;
147 }
148
149 /*
150  * call-seq: MESSAGE.set_flag(flag, value) => nil
151  *
152  * Set a value of a flag for the email corresponding to 'message'
153  */
154 VALUE
155 notmuch_rb_message_set_flag(VALUE self, VALUE flagv, VALUE valuev)
156 {
157     notmuch_message_t *message;
158
159     Data_Get_Notmuch_Message(self, message);
160
161     if (!FIXNUM_P(flagv))
162         rb_raise(rb_eTypeError, "Flag not a Fixnum");
163
164     notmuch_message_set_flag(message, FIX2INT(flagv), RTEST(valuev));
165
166     return Qnil;
167 }
168
169 /*
170  * call-seq: MESSAGE.date => Fixnum
171  *
172  * Get the date of 'message'
173  */
174 VALUE
175 notmuch_rb_message_get_date(VALUE self)
176 {
177     notmuch_message_t *message;
178
179     Data_Get_Notmuch_Message(self, message);
180
181     return UINT2NUM(notmuch_message_get_date(message));
182 }
183
184 /*
185  * call-seq: MESSAGE.header(name) => String
186  *
187  * Get the value of the specified header from 'message'
188  */
189 VALUE
190 notmuch_rb_message_get_header(VALUE self, VALUE headerv)
191 {
192     const char *header, *value;
193     notmuch_message_t *message;
194
195     Data_Get_Notmuch_Message(self, message);
196
197 #if !defined(RSTRING_PTR)
198 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
199 #endif /* !defined(RSTRING_PTR) */
200
201     SafeStringValue(headerv);
202     header = RSTRING_PTR(headerv);
203
204     value = notmuch_message_get_header(message, header);
205     if (!value)
206         rb_raise(notmuch_rb_eMemoryError, "Out of memory");
207
208     return rb_str_new2(value);
209 }
210
211 /*
212  * call-seq: MESSAGE.tags => TAGS
213  *
214  * Get a Notmuch::Tags enumerable for all of the tags of 'message'.
215  */
216 VALUE
217 notmuch_rb_message_get_tags(VALUE self)
218 {
219     notmuch_message_t *message;
220     notmuch_tags_t *tags;
221
222     Data_Get_Notmuch_Message(self, message);
223
224     tags = notmuch_message_get_tags(message);
225     if (!tags)
226         rb_raise(notmuch_rb_eMemoryError, "Out of memory");
227
228     return Data_Wrap_Struct(notmuch_rb_cTags, NULL, NULL, tags);
229 }
230
231 /*
232  * call-seq: MESSAGE.add_tag(tag) => true
233  *
234  * Add a tag to the 'message'
235  */
236 VALUE
237 notmuch_rb_message_add_tag(VALUE self, VALUE tagv)
238 {
239     const char *tag;
240     notmuch_status_t ret;
241     notmuch_message_t *message;
242
243     Data_Get_Notmuch_Message(self, message);
244
245 #if !defined(RSTRING_PTR)
246 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
247 #endif /* !defined(RSTRING_PTR) */
248
249     SafeStringValue(tagv);
250     tag = RSTRING_PTR(tagv);
251
252     ret = notmuch_message_add_tag(message, tag);
253     notmuch_rb_status_raise(ret);
254
255     return Qtrue;
256 }
257
258 /*
259  * call-seq: MESSAGE.remove_tag(tag) => true
260  *
261  * Remove a tag from the 'message'
262  */
263 VALUE
264 notmuch_rb_message_remove_tag(VALUE self, VALUE tagv)
265 {
266     const char *tag;
267     notmuch_status_t ret;
268     notmuch_message_t *message;
269
270     Data_Get_Notmuch_Message(self, message);
271
272 #if !defined(RSTRING_PTR)
273 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
274 #endif /* !defined(RSTRING_PTR) */
275
276     SafeStringValue(tagv);
277     tag = RSTRING_PTR(tagv);
278
279     ret = notmuch_message_remove_tag(message, tag);
280     notmuch_rb_status_raise(ret);
281
282     return Qtrue;
283 }
284
285 /*
286  * call-seq: MESSAGE.remove_all_tags => true
287  *
288  * Remove all tags of the 'message'
289  */
290 VALUE
291 notmuch_rb_message_remove_all_tags(VALUE self)
292 {
293     notmuch_status_t ret;
294     notmuch_message_t *message;
295
296     Data_Get_Notmuch_Message(self, message);
297
298     ret = notmuch_message_remove_all_tags(message);
299     notmuch_rb_status_raise(ret);
300
301     return Qtrue;
302 }
303
304 /*
305  * call-seq: MESSAGE.maildir_flags_to_tags => true
306  *
307  * Add/remove tags according to maildir flags in the message filename(s)
308  */
309 VALUE
310 notmuch_rb_message_maildir_flags_to_tags(VALUE self)
311 {
312     notmuch_status_t ret;
313     notmuch_message_t *message;
314
315     Data_Get_Notmuch_Message(self, message);
316
317     ret = notmuch_message_maildir_flags_to_tags(message);
318     notmuch_rb_status_raise(ret);
319
320     return Qtrue;
321 }
322
323 /*
324  * call-seq: MESSAGE.tags_to_maildir_flags => true
325  *
326  * Rename message filename(s) to encode tags as maildir flags
327  */
328 VALUE
329 notmuch_rb_message_tags_to_maildir_flags(VALUE self)
330 {
331     notmuch_status_t ret;
332     notmuch_message_t *message;
333
334     Data_Get_Notmuch_Message(self, message);
335
336     ret = notmuch_message_tags_to_maildir_flags(message);
337     notmuch_rb_status_raise(ret);
338
339     return Qtrue;
340 }
341
342 /*
343  * call-seq: MESSAGE.freeze => true
344  *
345  * Freeze the 'message'
346  */
347 VALUE
348 notmuch_rb_message_freeze(VALUE self)
349 {
350     notmuch_status_t ret;
351     notmuch_message_t *message;
352
353     Data_Get_Notmuch_Message(self, message);
354
355     ret = notmuch_message_freeze(message);
356     notmuch_rb_status_raise(ret);
357
358     return Qtrue;
359 }
360
361 /*
362  * call-seq: MESSAGE.thaw => true
363  *
364  * Thaw a 'message'
365  */
366 VALUE
367 notmuch_rb_message_thaw(VALUE self)
368 {
369     notmuch_status_t ret;
370     notmuch_message_t *message;
371
372     Data_Get_Notmuch_Message(self, message);
373
374     ret = notmuch_message_thaw(message);
375     notmuch_rb_status_raise(ret);
376
377     return Qtrue;
378 }