]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/message.c
Merge tag '0.32.2'
[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_rb_object_destroy (self, &notmuch_rb_message_type);
32
33     return Qnil;
34 }
35
36 /*
37  * call-seq: MESSAGE.message_id => String
38  *
39  * Get the message ID of 'message'.
40  */
41 VALUE
42 notmuch_rb_message_get_message_id (VALUE self)
43 {
44     const char *msgid;
45     notmuch_message_t *message;
46
47     Data_Get_Notmuch_Message (self, message);
48
49     msgid = notmuch_message_get_message_id (message);
50
51     return rb_str_new2 (msgid);
52 }
53
54 /*
55  * call-seq: MESSAGE.thread_id => String
56  *
57  * Get the thread ID of 'message'.
58  */
59 VALUE
60 notmuch_rb_message_get_thread_id (VALUE self)
61 {
62     const char *tid;
63     notmuch_message_t *message;
64
65     Data_Get_Notmuch_Message (self, message);
66
67     tid = notmuch_message_get_thread_id (message);
68
69     return rb_str_new2 (tid);
70 }
71
72 /*
73  * call-seq: MESSAGE.replies => MESSAGES
74  *
75  * Get a Notmuch::Messages enumerable for all of the replies to 'message'.
76  */
77 VALUE
78 notmuch_rb_message_get_replies (VALUE self)
79 {
80     notmuch_messages_t *messages;
81     notmuch_message_t *message;
82
83     Data_Get_Notmuch_Message (self, message);
84
85     messages = notmuch_message_get_replies (message);
86
87     return Data_Wrap_Notmuch_Object (notmuch_rb_cMessages, &notmuch_rb_messages_type, messages);
88 }
89
90 /*
91  * call-seq: MESSAGE.filename => String
92  *
93  * Get a filename for the email corresponding to 'message'
94  */
95 VALUE
96 notmuch_rb_message_get_filename (VALUE self)
97 {
98     const char *fname;
99     notmuch_message_t *message;
100
101     Data_Get_Notmuch_Message (self, message);
102
103     fname = notmuch_message_get_filename (message);
104
105     return rb_str_new2 (fname);
106 }
107
108 /*
109  * call-seq: MESSAGE.filenames => FILENAMES
110  *
111  * Get all filenames for the email corresponding to MESSAGE.
112  */
113 VALUE
114 notmuch_rb_message_get_filenames (VALUE self)
115 {
116     notmuch_filenames_t *fnames;
117     notmuch_message_t *message;
118
119     Data_Get_Notmuch_Message (self, message);
120
121     fnames = notmuch_message_get_filenames (message);
122
123     return Data_Wrap_Notmuch_Object (notmuch_rb_cFileNames, &notmuch_rb_filenames_type, fnames);
124 }
125
126 /*
127  * call-seq: MESSAGE.get_flag (flag) => true or false
128  *
129  * Get a value of a flag for the email corresponding to 'message'
130  */
131 VALUE
132 notmuch_rb_message_get_flag (VALUE self, VALUE flagv)
133 {
134     notmuch_message_t *message;
135     notmuch_bool_t is_set;
136     notmuch_status_t status;
137
138     Data_Get_Notmuch_Message (self, message);
139
140     if (!FIXNUM_P (flagv))
141         rb_raise (rb_eTypeError, "Flag not a Fixnum");
142
143     status = notmuch_message_get_flag_st (message, FIX2INT (flagv), &is_set);
144     notmuch_rb_status_raise (status);
145
146     return is_set ? 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     SafeStringValue (headerv);
198     header = RSTRING_PTR (headerv);
199
200     value = notmuch_message_get_header (message, header);
201     if (!value)
202         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
203
204     return rb_str_new2 (value);
205 }
206
207 /*
208  * call-seq: MESSAGE.tags => TAGS
209  *
210  * Get a Notmuch::Tags enumerable for all of the tags of 'message'.
211  */
212 VALUE
213 notmuch_rb_message_get_tags (VALUE self)
214 {
215     notmuch_message_t *message;
216     notmuch_tags_t *tags;
217
218     Data_Get_Notmuch_Message (self, message);
219
220     tags = notmuch_message_get_tags (message);
221     if (!tags)
222         rb_raise (notmuch_rb_eMemoryError, "Out of memory");
223
224     return Data_Wrap_Notmuch_Object (notmuch_rb_cTags, &notmuch_rb_tags_type, tags);
225 }
226
227 /*
228  * call-seq: MESSAGE.add_tag (tag) => true
229  *
230  * Add a tag to the 'message'
231  */
232 VALUE
233 notmuch_rb_message_add_tag (VALUE self, VALUE tagv)
234 {
235     const char *tag;
236     notmuch_status_t ret;
237     notmuch_message_t *message;
238
239     Data_Get_Notmuch_Message (self, message);
240
241     SafeStringValue (tagv);
242     tag = RSTRING_PTR (tagv);
243
244     ret = notmuch_message_add_tag (message, tag);
245     notmuch_rb_status_raise (ret);
246
247     return Qtrue;
248 }
249
250 /*
251  * call-seq: MESSAGE.remove_tag (tag) => true
252  *
253  * Remove a tag from the 'message'
254  */
255 VALUE
256 notmuch_rb_message_remove_tag (VALUE self, VALUE tagv)
257 {
258     const char *tag;
259     notmuch_status_t ret;
260     notmuch_message_t *message;
261
262     Data_Get_Notmuch_Message (self, message);
263
264     SafeStringValue (tagv);
265     tag = RSTRING_PTR (tagv);
266
267     ret = notmuch_message_remove_tag (message, tag);
268     notmuch_rb_status_raise (ret);
269
270     return Qtrue;
271 }
272
273 /*
274  * call-seq: MESSAGE.remove_all_tags => true
275  *
276  * Remove all tags of the 'message'
277  */
278 VALUE
279 notmuch_rb_message_remove_all_tags (VALUE self)
280 {
281     notmuch_status_t ret;
282     notmuch_message_t *message;
283
284     Data_Get_Notmuch_Message (self, message);
285
286     ret = notmuch_message_remove_all_tags (message);
287     notmuch_rb_status_raise (ret);
288
289     return Qtrue;
290 }
291
292 /*
293  * call-seq: MESSAGE.maildir_flags_to_tags => true
294  *
295  * Add/remove tags according to maildir flags in the message filename (s)
296  */
297 VALUE
298 notmuch_rb_message_maildir_flags_to_tags (VALUE self)
299 {
300     notmuch_status_t ret;
301     notmuch_message_t *message;
302
303     Data_Get_Notmuch_Message (self, message);
304
305     ret = notmuch_message_maildir_flags_to_tags (message);
306     notmuch_rb_status_raise (ret);
307
308     return Qtrue;
309 }
310
311 /*
312  * call-seq: MESSAGE.tags_to_maildir_flags => true
313  *
314  * Rename message filename (s) to encode tags as maildir flags
315  */
316 VALUE
317 notmuch_rb_message_tags_to_maildir_flags (VALUE self)
318 {
319     notmuch_status_t ret;
320     notmuch_message_t *message;
321
322     Data_Get_Notmuch_Message (self, message);
323
324     ret = notmuch_message_tags_to_maildir_flags (message);
325     notmuch_rb_status_raise (ret);
326
327     return Qtrue;
328 }
329
330 /*
331  * call-seq: MESSAGE.freeze => true
332  *
333  * Freeze the 'message'
334  */
335 VALUE
336 notmuch_rb_message_freeze (VALUE self)
337 {
338     notmuch_status_t ret;
339     notmuch_message_t *message;
340
341     Data_Get_Notmuch_Message (self, message);
342
343     ret = notmuch_message_freeze (message);
344     notmuch_rb_status_raise (ret);
345
346     return Qtrue;
347 }
348
349 /*
350  * call-seq: MESSAGE.thaw => true
351  *
352  * Thaw a 'message'
353  */
354 VALUE
355 notmuch_rb_message_thaw (VALUE self)
356 {
357     notmuch_status_t ret;
358     notmuch_message_t *message;
359
360     Data_Get_Notmuch_Message (self, message);
361
362     ret = notmuch_message_thaw (message);
363     notmuch_rb_status_raise (ret);
364
365     return Qtrue;
366 }