1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use AddressType;
use AutocryptHeader;
use AutocryptHeaderList;
use DecryptFlags;
use Error;
use InternetAddressList;
use Object;
use ffi;
use glib;
use glib::object::IsA;
use glib::translate::*;
use glib_ffi;
use gobject_ffi;
use std::mem;
use std::ptr;

glib_wrapper! {
    /// A MIME Message object.
    ///
    /// # Implements
    ///
    /// [`MessageExt`](trait.MessageExt.html), [`ObjectExt`](trait.ObjectExt.html)
    pub struct Message(Object<ffi::GMimeMessage, ffi::GMimeMessageClass>): Object;

    match fn {
        get_type => || ffi::g_mime_message_get_type(),
    }
}

impl Message {
    /// If `pretty_headers` is `true`, then the standard rfc822 headers are
    /// initialized so as to put headers in a nice friendly order. This is
    /// strictly a cosmetic thing, so if you are unsure, it is safe to say
    /// no (`false`).
    /// ## `pretty_headers`
    /// make pretty headers
    ///
    /// # Returns
    ///
    /// an empty `Message` object.
    pub fn new(pretty_headers: bool) -> Message {
        unsafe {
            from_glib_full(ffi::g_mime_message_new(pretty_headers.to_glib()))
        }
    }
}

/// Trait containing all `Message` methods.
///
/// # Implementors
///
/// [`Message`](struct.Message.html)
pub trait MessageExt {
    /// Add a mailbox of a chosen type to the MIME message.
    ///
    /// Note: The `name` (and `addr`) strings should be in UTF-8.
    /// ## `type_`
    /// A `AddressType`
    /// ## `name`
    /// The name of the mailbox (or `None`)
    /// ## `addr`
    /// The address of the mailbox
    fn add_mailbox(&self, type_: AddressType, name: &str, addr: &str);

    //fn foreach<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&self, callback: /*Unknown conversion*//*Unimplemented*/ObjectForeachFunc, user_data: P);

    /// Gets a list of addresses of the specified `type_` from the `self`.
    /// ## `type_`
    /// A `AddressType`
    ///
    /// # Returns
    ///
    /// a list of addresses of the specified
    /// `type_` from the `self`.
    fn get_addresses(&self, type_: AddressType) -> Option<InternetAddressList>;

    /// Gets the complete list of recipients for `self`.
    ///
    /// # Returns
    ///
    /// a newly allocated `InternetAddressList`
    /// containing all recipients of the message or `None` if no recipients
    /// are set.
    fn get_all_recipients(&self) -> Option<InternetAddressList>;

    /// Creates a new `AutocryptHeaderList` of relevant headers of the
    /// given type based on the recipient(s) of an e-mail message.
    ///
    /// Returns the same object as
    /// `g_mime_message_get_autocrypt_gossip_headers_with_inner_part` , but
    /// handles decryption and cleanup automatically.
    ///
    /// `flags` and `session_key` are passed through to
    /// `MultipartEncryptedExt::decrypt`, as needed.
    ///
    /// If the message is not actually an encrypted message, returns `None`:
    /// it should be ignored for purposes of evaluating gossip.
    ///
    /// If decryption fails, returns `None`. In this case, an exception
    /// will be set on `err` to provide information about the decryption
    /// failure.
    /// ## `now`
    /// a `glib::DateTime` object, or `None`
    /// ## `flags`
    /// a `DecryptFlags`, to be used during decryption
    /// ## `session_key`
    /// session key to use or `None`
    ///
    /// # Returns
    ///
    /// a new `AutocryptHeaderList` object,
    /// or `None` on error.
    fn get_autocrypt_gossip_headers(&self, now: &glib::DateTime, flags: DecryptFlags, session_key: &str) -> Result<AutocryptHeaderList, Error>;

    /// Creates a new `AutocryptHeaderList` of relevant headers of the
    /// given type based on the recipient(s) of an e-mail message.
    ///
    /// You must pass the decrypted inner part of the message to this
    /// function, since Autocrypt-Gossip headers are only stored within the
    /// encrypted layer.
    ///
    /// If you don't already have the decrypted inner part available to
    /// you, you probably want to use
    /// `MessageExt::get_autocrypt_gossip_headers` instead.
    ///
    /// Each header in the returned list will:
    ///
    ///  - have a valid address
    ///  - be of the type requested
    ///  - be complete
    ///
    /// If no Autocrypt header is found for a recipient, no
    /// `AutocryptHeader` will be in the list associated with that e-mail address.
    ///
    /// Note that the following types of Autocrypt headers will not be
    /// returned by this function:
    ///
    ///  - headers of an unrequested type
    ///  - headers that do not match an address in "From:"
    ///  - unparseable headers
    ///  - headers with unknown critical attributes
    ///  - duplicate valid headers for a given address
    ///
    /// On error (e.g. if this version of GMime cannot handle the requested
    /// Autocrypt type, or if a parameter is missing or malformed), returns
    /// `None`
    ///
    /// The returned Autocrypt headers will have their effective_date set
    /// to the earliest of either:
    ///
    /// - the Date: header of the message or
    /// - `now` (or the current time, if `now` is `None`)
    /// ## `now`
    /// a `glib::DateTime` object, or `None`
    /// ## `inner_part`
    /// a `Object` which is the cleartext part of the inner message
    ///
    /// # Returns
    ///
    /// a new `AutocryptHeaderList` object, or `None` on error.
    fn get_autocrypt_gossip_headers_from_inner_part<P: IsA<Object>>(&self, now: &glib::DateTime, inner_part: &P) -> Option<AutocryptHeaderList>;

    /// Creates a new `AutocryptHeader` base on the relevant Autocrypt
    /// header associated with the sender of an e-mail message.
    ///
    /// If the message has no sender in the From: field, or has more than
    /// one sender, then this function will return `None`. Autocrypt should
    /// ignore the message entirely.
    ///
    /// If there is one sender, but no single Autocrypt header is found
    /// that matches that e-mail address, a `AutocryptHeader` will be
    /// returned for the sender, but it will be incomplete (see
    /// `AutocryptHeaderExt::is_complete`).
    ///
    /// Note that the following types of Autocrypt headers will not be
    /// returned by this function:
    ///
    ///  - headers that do not match an address in "From:"
    ///  - unparseable headers
    ///  - headers with unknown critical attributes
    ///  - duplicate valid headers for the sender's address
    ///
    /// The returned Autocrypt headers will have their effective_date set
    /// to the earliest of either:
    ///
    /// - the Date: header of the message or
    /// - `now` (or the current time, if `now` is `None`)
    /// ## `now`
    /// a `glib::DateTime` object, or `None`
    ///
    /// # Returns
    ///
    /// a new `AutocryptHeaderList` object,
    /// or `None` if the message should be ignored for purposes of
    /// Autocrypt.
    fn get_autocrypt_header(&self, now: &glib::DateTime) -> Option<AutocryptHeader>;

    /// Gets combined list of parsed addresses in the Bcc header(s).
    ///
    /// # Returns
    ///
    /// the parsed list of addresses in the Bcc header(s).
    fn get_bcc(&self) -> Option<InternetAddressList>;

    /// Attempts to identify the MIME part containing the body of the
    /// message.
    ///
    /// # Returns
    ///
    /// a `Object` containing the textual
    /// content that appears to be the main body of the message.
    ///
    /// Note: This function is NOT guaranteed to always work as it
    /// makes some assumptions that are not necessarily true. It is
    /// recommended that you traverse the MIME structure yourself.
    fn get_body(&self) -> Option<Object>;

    /// Gets combined list of parsed addresses in the Cc header(s).
    ///
    /// # Returns
    ///
    /// the parsed list of addresses in the Cc header(s).
    fn get_cc(&self) -> Option<InternetAddressList>;

    /// Gets the parsed date and time value from the Date header.
    ///
    /// # Returns
    ///
    /// a `glib::DateTime` on success or `None` if the date could not be parsed.
    fn get_date(&self) -> Option<glib::DateTime>;

    /// Gets the parsed list of addresses in the From header.
    ///
    /// # Returns
    ///
    /// the parsed list of addresses in the From header.
    fn get_from(&self) -> Option<InternetAddressList>;

    /// Gets the Message-Id header of `self`.
    ///
    /// # Returns
    ///
    /// the Message-Id of a message.
    fn get_message_id(&self) -> Option<String>;

    /// Gets the toplevel MIME part contained within `self`.
    ///
    /// # Returns
    ///
    /// the toplevel MIME part of `self`.
    fn get_mime_part(&self) -> Option<Object>;

    /// Gets the parsed list of addresses in the Reply-To header.
    ///
    /// # Returns
    ///
    /// the parsed list of addresses in the Reply-To header.
    fn get_reply_to(&self) -> Option<InternetAddressList>;

    /// Gets the parsed list of addresses in the Sender header.
    ///
    /// # Returns
    ///
    /// the parsed list of addresses in the Sender header.
    fn get_sender(&self) -> Option<InternetAddressList>;

    /// Gets the subject of the `self`.
    ///
    /// # Returns
    ///
    /// the subject of the `self` in a form suitable for display
    /// or `None` if no subject is set. If not `None`, the returned string
    /// will be in UTF-8.
    fn get_subject(&self) -> Option<String>;

    /// Gets combined list of parsed addresses in the To header(s).
    ///
    /// # Returns
    ///
    /// the parsed list of addresses in the To header(s).
    fn get_to(&self) -> Option<InternetAddressList>;

    fn split(&self, max_size: usize) -> Vec<Message>;

    /// Sets the Date header on a MIME Message.
    /// ## `date`
    /// a date to be used in the Date header
    fn set_date(&self, date: &glib::DateTime);

    /// Set the Message-Id on a message.
    /// ## `message_id`
    /// message-id (addr-spec portion)
    fn set_message_id(&self, message_id: &str);

    /// Set the root-level MIME part of the message.
    /// ## `mime_part`
    /// The root-level MIME Part
    fn set_mime_part<P: IsA<Object>>(&self, mime_part: &P);

    /// Set the subject of a `self`.
    ///
    /// Note: The `subject` string should be in UTF-8.
    /// ## `subject`
    /// Subject string
    /// ## `charset`
    /// The charset to use for encoding the subject or `None` to use the default
    fn set_subject(&self, subject: &str, charset: &str);
}

impl<O: IsA<Message>> MessageExt for O {
    fn add_mailbox(&self, type_: AddressType, name: &str, addr: &str) {
        unsafe {
            ffi::g_mime_message_add_mailbox(self.to_glib_none().0, type_.to_glib(), name.to_glib_none().0, addr.to_glib_none().0);
        }
    }

    //fn foreach<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&self, callback: /*Unknown conversion*//*Unimplemented*/ObjectForeachFunc, user_data: P) {
    //    unsafe { TODO: call ffi::g_mime_message_foreach() }
    //}

    fn get_addresses(&self, type_: AddressType) -> Option<InternetAddressList> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_addresses(self.to_glib_none().0, type_.to_glib()))
        }
    }

    fn get_all_recipients(&self) -> Option<InternetAddressList> {
        unsafe {
            from_glib_full(ffi::g_mime_message_get_all_recipients(self.to_glib_none().0))
        }
    }

    fn get_autocrypt_gossip_headers(&self, now: &glib::DateTime, flags: DecryptFlags, session_key: &str) -> Result<AutocryptHeaderList, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_mime_message_get_autocrypt_gossip_headers(self.to_glib_none().0, now.to_glib_none().0, flags.to_glib(), session_key.to_glib_none().0, &mut error);
            if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
        }
    }

    fn get_autocrypt_gossip_headers_from_inner_part<P: IsA<Object>>(&self, now: &glib::DateTime, inner_part: &P) -> Option<AutocryptHeaderList> {
        unsafe {
            from_glib_full(ffi::g_mime_message_get_autocrypt_gossip_headers_from_inner_part(self.to_glib_none().0, now.to_glib_none().0, inner_part.to_glib_none().0))
        }
    }

    fn get_autocrypt_header(&self, now: &glib::DateTime) -> Option<AutocryptHeader> {
        unsafe {
            from_glib_full(ffi::g_mime_message_get_autocrypt_header(self.to_glib_none().0, now.to_glib_none().0))
        }
    }

    fn get_bcc(&self) -> Option<InternetAddressList> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_bcc(self.to_glib_none().0))
        }
    }

    fn get_body(&self) -> Option<Object> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_body(self.to_glib_none().0))
        }
    }

    fn get_cc(&self) -> Option<InternetAddressList> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_cc(self.to_glib_none().0))
        }
    }

    fn get_date(&self) -> Option<glib::DateTime> {
        unsafe {
            from_glib_full(ffi::g_mime_message_get_date(self.to_glib_none().0))
        }
    }

    fn get_from(&self) -> Option<InternetAddressList> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_from(self.to_glib_none().0))
        }
    }

    fn get_message_id(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_message_id(self.to_glib_none().0))
        }
    }

    fn get_mime_part(&self) -> Option<Object> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_mime_part(self.to_glib_none().0))
        }
    }

    fn get_reply_to(&self) -> Option<InternetAddressList> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_reply_to(self.to_glib_none().0))
        }
    }

    fn get_sender(&self) -> Option<InternetAddressList> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_sender(self.to_glib_none().0))
        }
    }

    fn get_subject(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_subject(self.to_glib_none().0))
        }
    }

    fn get_to(&self) -> Option<InternetAddressList> {
        unsafe {
            from_glib_none(ffi::g_mime_message_get_to(self.to_glib_none().0))
        }
    }

    fn split(&self, max_size: usize) -> Vec<Message> {
        unsafe {
            let mut n_parts = mem::uninitialized();
            let parts = ffi::g_mime_message_partial_split_message(self.to_glib_none().0, max_size, &mut n_parts);
            FromGlibContainer::from_glib_full_num(parts, n_parts as usize)
        }
    }

    fn set_date(&self, date: &glib::DateTime) {
        unsafe {
            ffi::g_mime_message_set_date(self.to_glib_none().0, date.to_glib_none().0);
        }
    }

    fn set_message_id(&self, message_id: &str) {
        unsafe {
            ffi::g_mime_message_set_message_id(self.to_glib_none().0, message_id.to_glib_none().0);
        }
    }

    fn set_mime_part<P: IsA<Object>>(&self, mime_part: &P) {
        unsafe {
            ffi::g_mime_message_set_mime_part(self.to_glib_none().0, mime_part.to_glib_none().0);
        }
    }

    fn set_subject(&self, subject: &str, charset: &str) {
        unsafe {
            ffi::g_mime_message_set_subject(self.to_glib_none().0, subject.to_glib_none().0, charset.to_glib_none().0);
        }
    }
}