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
use RfcComplianceMode;
use ffi;
use glib::translate::*;
use glib_ffi;
use gobject_ffi;
use std::mem;
use std::ptr;

glib_wrapper! {
    /// A set of parser options used by `Parser` and various other parsing functions.
    pub struct ParserOptions(Boxed<ffi::GMimeParserOptions>);

    match fn {
        copy => |ptr| gobject_ffi::g_boxed_copy(ffi::g_mime_parser_options_get_type(), ptr as *mut _) as *mut ffi::GMimeParserOptions,
        free => |ptr| gobject_ffi::g_boxed_free(ffi::g_mime_parser_options_get_type(), ptr as *mut _),
        get_type => || ffi::g_mime_parser_options_get_type(),
    }
}

impl ParserOptions {
    /// Creates a new set of `ParserOptions`.
    ///
    /// # Returns
    ///
    /// a newly allocated set of `ParserOptions` with the default values.
    pub fn new() -> ParserOptions {
        unsafe {
            from_glib_full(ffi::g_mime_parser_options_new())
        }
    }

    /// Clones a `ParserOptions`.
    ///
    /// # Returns
    ///
    /// a newly allocated `ParserOptions`.
    pub fn clone(&mut self) -> Option<ParserOptions> {
        unsafe {
            from_glib_full(ffi::g_mime_parser_options_clone(self.to_glib_none_mut().0))
        }
    }

    /// Gets the compliance mode that should be used when parsing rfc822 addresses.
    ///
    /// Note: Even in `RfcComplianceMode::Strict` mode, the address parser is fairly liberal in
    /// what it accepts. Setting it to `RfcComplianceMode::Loose` just makes it try harder to
    /// deal with garbage input.
    ///
    /// # Returns
    ///
    /// the compliance mode that is currently set.
    pub fn get_address_compliance_mode(&mut self) -> RfcComplianceMode {
        unsafe {
            from_glib(ffi::g_mime_parser_options_get_address_compliance_mode(self.to_glib_none_mut().0))
        }
    }

    /// Gets whether or not the rfc822 address parser should allow addresses without a domain.
    ///
    /// In general, you'll probably want this value to be `false` (the default) as it allows
    /// maximum interoperability with existing (broken) mail clients and other mail software
    /// such as sloppily written perl scripts (aka spambots) that do not properly quote the
    /// name when it contains a comma.
    ///
    /// This option exists in order to allow parsing of mailbox addresses that do not have a
    /// domain component. These types of addresses are rare and were typically only used when
    /// sending mail to other users on the same UNIX system.
    ///
    /// # Returns
    ///
    /// `true` if the address parser should allow addresses without a domain.
    pub fn get_allow_addresses_without_domain(&mut self) -> bool {
        unsafe {
            from_glib(ffi::g_mime_parser_options_get_allow_addresses_without_domain(self.to_glib_none_mut().0))
        }
    }

    /// Gets the fallback charsets to try when decoding 8-bit headers.
    ///
    /// # Returns
    ///
    /// a `None`-terminated list of charsets to try when
    /// decoding 8-bit headers.
    pub fn get_fallback_charsets(&mut self) -> Vec<String> {
        unsafe {
            FromGlibPtrContainer::from_glib_none(ffi::g_mime_parser_options_get_fallback_charsets(self.to_glib_none_mut().0))
        }
    }

    /// Gets the compliance mode that should be used when parsing Content-Type and
    /// Content-Disposition parameters.
    ///
    /// Note: Even in `RfcComplianceMode::Strict` mode, the parameter parser is fairly liberal
    /// in what it accepts. Setting it to `RfcComplianceMode::Loose` just makes it try harder
    /// to deal with garbage input.
    ///
    /// # Returns
    ///
    /// the compliance mode that is currently set.
    pub fn get_parameter_compliance_mode(&mut self) -> RfcComplianceMode {
        unsafe {
            from_glib(ffi::g_mime_parser_options_get_parameter_compliance_mode(self.to_glib_none_mut().0))
        }
    }

    /// Gets the compliance mode that should be used when parsing rfc2047 encoded words.
    ///
    /// Note: Even in `RfcComplianceMode::Strict` mode, the rfc2047 parser is fairly liberal
    /// in what it accepts. Setting it to `RfcComplianceMode::Loose` just makes it try harder
    /// to deal with garbage input.
    ///
    /// # Returns
    ///
    /// the compliance mode that is currently set.
    pub fn get_rfc2047_compliance_mode(&mut self) -> RfcComplianceMode {
        unsafe {
            from_glib(ffi::g_mime_parser_options_get_rfc2047_compliance_mode(self.to_glib_none_mut().0))
        }
    }

    //pub fn get_warning_callback(&mut self) -> /*Unknown conversion*//*Unimplemented*/ParserWarningFunc {
    //    unsafe { TODO: call ffi::g_mime_parser_options_get_warning_callback() }
    //}

    /// Sets the compliance mode that should be used when parsing rfc822 addresses.
    ///
    /// In general, you'll probably want this value to be `RfcComplianceMode::Loose`
    /// (the default) as it allows maximum interoperability with existing (broken) mail clients
    /// and other mail software such as sloppily written perl scripts (aka spambots).
    ///
    /// Note: Even in `RfcComplianceMode::Strict` mode, the address parser is fairly liberal in
    /// what it accepts. Setting it to `RfcComplianceMode::Loose` just makes it try harder to
    /// deal with garbage input.
    /// ## `mode`
    /// a `RfcComplianceMode`
    pub fn set_address_compliance_mode(&mut self, mode: RfcComplianceMode) {
        unsafe {
            ffi::g_mime_parser_options_set_address_compliance_mode(self.to_glib_none_mut().0, mode.to_glib());
        }
    }

    /// Sets whether the rfc822 address parser should allow addresses without a domain.
    ///
    /// In general, you'll probably want this value to be `false` (the default) as it allows
    /// maximum interoperability with existing (broken) mail clients and other mail software
    /// such as sloppily written perl scripts (aka spambots) that do not properly quote the
    /// name when it contains a comma.
    ///
    /// This option exists in order to allow parsing of mailbox addresses that do not have a
    /// domain component. These types of addresses are rare and were typically only used when
    /// sending mail to other users on the same UNIX system.
    /// ## `allow`
    /// `true` if the parser should allow addresses without a domain or `false` otherwise
    pub fn set_allow_addresses_without_domain(&mut self, allow: bool) {
        unsafe {
            ffi::g_mime_parser_options_set_allow_addresses_without_domain(self.to_glib_none_mut().0, allow.to_glib());
        }
    }

    /// Sets the fallback charsets to try when decoding 8-bit headers.
    ///
    /// Note: It is recommended that the list of charsets start with utf-8
    /// and end with iso-8859-1.
    /// ## `charsets`
    /// a `None`-terminated list of charsets or `None` for the default list
    pub fn set_fallback_charsets(&mut self, charsets: &Vec<&str>) {
        unsafe {
            ffi::g_mime_parser_options_set_fallback_charsets(self.to_glib_none_mut().0, charsets.to_glib_none().0);
        }
    }

    /// Sets the compliance mode that should be used when parsing Content-Type and
    /// Content-Disposition parameters.
    ///
    /// In general, you'll probably want this value to be `RfcComplianceMode::Loose`
    /// (the default) as it allows maximum interoperability with existing (broken) mail clients
    /// and other mail software such as sloppily written perl scripts (aka spambots).
    ///
    /// Note: Even in `RfcComplianceMode::Strict` mode, the parameter parser is fairly liberal
    /// in what it accepts. Setting it to `RfcComplianceMode::Loose` just makes it try harder
    /// to deal with garbage input.
    /// ## `mode`
    /// a `RfcComplianceMode`
    pub fn set_parameter_compliance_mode(&mut self, mode: RfcComplianceMode) {
        unsafe {
            ffi::g_mime_parser_options_set_parameter_compliance_mode(self.to_glib_none_mut().0, mode.to_glib());
        }
    }

    /// Sets the compliance mode that should be used when parsing rfc2047 encoded words.
    ///
    /// In general, you'll probably want this value to be `RfcComplianceMode::Loose`
    /// (the default) as it allows maximum interoperability with existing (broken) mail clients
    /// and other mail software such as sloppily written perl scripts (aka spambots).
    ///
    /// Note: Even in `RfcComplianceMode::Strict` mode, the parameter parser is fairly liberal
    /// in what it accepts. Setting it to `RfcComplianceMode::Loose` just makes it try harder
    /// to deal with garbage input.
    /// ## `mode`
    /// a `RfcComplianceMode`
    pub fn set_rfc2047_compliance_mode(&mut self, mode: RfcComplianceMode) {
        unsafe {
            ffi::g_mime_parser_options_set_rfc2047_compliance_mode(self.to_glib_none_mut().0, mode.to_glib());
        }
    }

    //pub fn set_warning_callback<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&mut self, warning_cb: /*Unknown conversion*//*Unimplemented*/ParserWarningFunc, user_data: P) {
    //    unsafe { TODO: call ffi::g_mime_parser_options_set_warning_callback() }
    //}

    /// Gets the default parser options.
    ///
    /// # Returns
    ///
    /// the default parser options.
    pub fn get_default() -> Option<ParserOptions> {
        unsafe {
            from_glib_full(ffi::g_mime_parser_options_get_default())
        }
    }
}

impl Default for ParserOptions {
    fn default() -> Self {
        Self::new()
    }
}