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 451 452 453 454
/*
* meli - parser module
*
* Copyright 2019 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
//! Parsing of `mailto` addresses.
//!
//! Conforming to [RFC6068](https://www.rfc-editor.org/rfc/rfc6068) which obsoletes
//! [RFC2368](https://www.rfc-editor.org/rfc/rfc2368).
use std::convert::TryFrom;
use super::*;
use crate::{
email::headers::HeaderMap,
utils::percent_encoding::{AsciiSet, CONTROLS},
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Mailto {
pub address: Vec<Address>,
pub body: Option<String>,
pub headers: HeaderMap,
}
impl Mailto {
pub const IGNORE_HEADERS: &'static [HeaderName] = &[
HeaderName::FROM,
HeaderName::DATE,
HeaderName::MESSAGE_ID,
HeaderName::APPARENTLY_TO,
HeaderName::ARC_AUTHENTICATION_RESULTS,
HeaderName::ARC_MESSAGE_SIGNATURE,
HeaderName::ARC_SEAL,
HeaderName::AUTHENTICATION_RESULTS,
HeaderName::AUTOFORWARDED,
HeaderName::AUTO_SUBMITTED,
HeaderName::AUTOSUBMITTED,
HeaderName::BASE,
HeaderName::CONTENT_ALTERNATIVE,
HeaderName::CONTENT_BASE,
HeaderName::CONTENT_DESCRIPTION,
HeaderName::CONTENT_DISPOSITION,
HeaderName::CONTENT_DURATION,
HeaderName::CONTENT_FEATURES,
HeaderName::CONTENT_ID,
HeaderName::CONTENT_IDENTIFIER,
HeaderName::CONTENT_LANGUAGE,
HeaderName::CONTENT_LENGTH,
HeaderName::CONTENT_LOCATION,
HeaderName::CONTENT_MD5,
HeaderName::CONTENT_RETURN,
HeaderName::CONTENT_TRANSFER_ENCODING,
HeaderName::CONTENT_TRANSLATION_TYPE,
HeaderName::CONTENT_TYPE,
HeaderName::DELIVERED_TO,
HeaderName::DKIM_SIGNATURE,
HeaderName::ENCRYPTED,
HeaderName::FORWARDED,
HeaderName::MAIL_FOLLOWUP_TO,
HeaderName::MAIL_REPLY_TO,
HeaderName::MIME_VERSION,
HeaderName::ORIGINAL_ENCODED_INFORMATION_TYPES,
HeaderName::ORIGINAL_FROM,
HeaderName::ORIGINAL_MESSAGE_ID,
HeaderName::ORIGINAL_RECIPIENT,
HeaderName::ORIGINAL_SUBJECT,
HeaderName::ORIGINATOR_RETURN_ADDRESS,
HeaderName::RECEIVED,
HeaderName::RECEIVED_SPF,
HeaderName::RESENT_BCC,
HeaderName::RESENT_CC,
HeaderName::RESENT_DATE,
HeaderName::RESENT_FROM,
HeaderName::RESENT_MESSAGE_ID,
HeaderName::RESENT_REPLY_TO,
HeaderName::RESENT_SENDER,
HeaderName::RESENT_TO,
HeaderName::RETURN_PATH,
HeaderName::SENDER,
HeaderName::USER_AGENT,
];
pub const MAILTO_CHARSET: &'static AsciiSet = &CONTROLS
.add(b' ')
.add(b'"')
.add(b'"')
.add(b'#')
.add(b'%')
.add(b'/')
.add(b'<')
.add(b'>')
.add(b'?')
.add(b'`')
.add(b'{')
.add(b'}');
}
impl From<Mailto> for Draft {
fn from(val: Mailto) -> Self {
let mut ret = Self::default();
let Mailto {
address: _,
body,
headers,
} = val;
for (hdr, val) in headers.into_inner() {
ret.set_header(hdr, val);
}
ret.set_body(body.unwrap_or_default());
ret
}
}
impl From<&Mailto> for Draft {
fn from(val: &Mailto) -> Self {
Self::from(val.clone())
}
}
impl TryFrom<&[u8]> for Mailto {
type Error = String;
fn try_from(value: &[u8]) -> std::result::Result<Self, Self::Error> {
super::parser::generic::mailto(value)
.map(|(_, v)| v)
.map_err(|err| {
log::debug!(
"parser::mailto returned error while parsing {}:\n{:?}",
String::from_utf8_lossy(value),
&err,
);
format!("{:?}", err)
})
}
}
impl TryFrom<&str> for Mailto {
type Error = String;
fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
super::parser::generic::mailto(value.as_bytes())
.map(|(_, v)| v)
.map_err(|err| {
log::debug!(
"parser::mailto returned error while parsing {}:\n{:?}",
value,
&err
);
format!("{:?}", err)
})
}
}
#[cfg(test)]
mod tests {
use HeaderName as HDR;
use super::*;
#[test]
fn test_mailto() {
macro_rules! addr {
($lit:literal) => {
Address::try_from($lit).unwrap()
};
}
macro_rules! mlt {
($lit:literal) => {
Mailto::try_from($lit).expect("Could not parse mailto link.")
};
}
macro_rules! hdr {
($lit:literal) => {
HeaderName::try_from($lit).expect("Could not parse header name.")
};
}
macro_rules! hdrmap {
($(($field:literal, $val:literal)),+) => {{
let mut m = HeaderMap::empty();
$(
m.insert(hdr!($field), $val.into());
)+
m
}};
}
macro_rules! test_case {
($mailto:literal, addresses => $($addr:literal),*; body => $body:expr; $(($field:literal, $val:literal)),+) => {{
let addresses = &[
$(
addr!($addr)
),*
];
let Mailto {
address,
body,
headers,
} = mlt!($mailto);
assert_eq!(
(address.as_slice(), body.as_ref().map(|b| b.as_str()), headers),
(addresses.as_slice(), $body, hdrmap!($(($field, $val)),*))
);
}}
}
test_case!("mailto:info@example.com?subject=email%20subject",
addresses=> "info@example.com";
body => None;
("To", "info@example.com"), ("Subject", "email subject")
);
test_case!("mailto:info@example.com?cc=8cc9@example.com",
addresses=> "info@example.com";
body => None;
("To", "info@example.com"), ("Cc", "8cc9@example.com")
);
test_case!("mailto:info@example.com?bcc=7bcc8@example.com&body=line%20first%0Abut%20not%0Alast",
addresses=> "info@example.com";
body => Some("line first\nbut not\nlast");
("To", "info@example.com"), ("Bcc", "7bcc8@example.com")
);
test_case!("mailto:info@example.com?In-Reply-To=%3C20230526204845.673031-1-manos.pitsidianakis@linaro.org%3E&Cc=kraxel%40redhat.com%2Cqemu-devel%40nongnu.org&Subject=Re%3A%20%5BPATCH%5D%20Add%20virtio-sound%20and%20virtio-sound-pci%20devices",
addresses=> "info@example.com";
body => None;
("To", "info@example.com"), ("Subject", "Re: [PATCH] Add virtio-sound and virtio-sound-pci devices"), ("Cc", "kraxel@redhat.com,qemu-devel@nongnu.org"), ("In-Reply-To", "<20230526204845.673031-1-manos.pitsidianakis@linaro.org>")
);
assert_eq!(
mlt!("mailto:chris@example.com%2C%20tony@example.com"),
mlt!("mailto:?to=chris@example.com%2C%20tony@example.com")
);
/* address plus to= should be ignored */
assert!(
Mailto::try_from("mailto:?to=chris@example.com%2C%20tony@example.com")
!= Mailto::try_from("mailto:chris@example.com?to=tony@example.com"),
"{:?} == {:?}",
Mailto::try_from("mailto:?to=chris@example.com%2C%20tony@example.com"),
Mailto::try_from("mailto:chris@example.com?to=tony@example.com")
);
// URLs for an ordinary individual mailing address:
test_case!("mailto:chris@example.com",
addresses=> "chris@example.com";
body => None;
("To", "chris@example.com")
);
// A URL for a mail response system that requires the name of the file in the
// subject:
test_case!("mailto:infobot@example.com?subject=current-issue",
addresses => "infobot@example.com";
body => None;
("To", "infobot@example.com"), ("Subject", "current-issue")
);
// A mail response system that requires a "send" request in the body:
test_case!("mailto:infobot@example.com?body=send%20current-issue",
addresses => "infobot@example.com";
body => Some("send current-issue");
("To", "infobot@example.com")
);
//A similar URL could have two lines with different "send" requests (in this
// case, "send current-issue" and, on the next line, "send index".)
test_case!("mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index",
addresses => "infobot@example.com";
body => Some("send current-issue\r\nsend index");
("To", "infobot@example.com")
);
// An interesting use of your mailto URL is when browsing archives of messages.
// Each browsed message might contain a mailto URL like:
test_case!("mailto:foobar@example.com?In-Reply-To=%3c3469A91.D10AF4C@example.com%3e",
addresses => "foobar@example.com";
body => None;
("To", "foobar@example.com"), ("In-Reply-To", "<3469A91.D10AF4C@example.com>")
);
// A request to subscribe to a mailing list:
test_case!("mailto:majordomo@example.com?body=subscribe%20bamboo-l",
addresses => "majordomo@example.com";
body => Some("subscribe bamboo-l");
("To", "majordomo@example.com")
);
// A URL for a single user which includes a CC of another user:
test_case!("mailto:joe@example.com?cc=bob@example.com&body=hello",
addresses => "joe@example.com";
body => Some("hello");
("To", "joe@example.com"), ("Cc", "bob@example.com")
);
// Another way of expressing the same thing:
test_case!("mailto:?to=joe@example.com&cc=bob@example.com&body=hello",
addresses => "joe@example.com";
body => Some("hello");
("To", "joe@example.com"), ("Cc", "bob@example.com")
);
// Note the use of the "&" reserved character, above. The following example,
// by using "?" twice, is incorrect: <mailto:joe@example.com?cc=bob@
// example.com?body=hello> ; WRONG!
Mailto::try_from("mailto:joe@example.com?cc=bob@example.com?body=hello").unwrap_err();
// <a href="mailto:?to=joe@xyz.com&cc=bob@xyz.com&body=hello"> assert
// these are equal
test_case!("mailto:?to=joe@example.com&cc=bob@example.com&body=hello",
addresses => "joe@example.com";
body => Some("hello");
("To", "joe@example.com"), ("Cc", "bob@example.com")
);
// To indicate the address "gorby%kremvax@example.com" one would do:
// <mailto:gorby%25kremvax@example.com>
test_case!("mailto:gorby%25kremvax@example.com",
addresses => "gorby%kremvax@example.com";
body => None;
("To", "gorby%kremvax@example.com")
);
// Custom header is ignored
// <mailto:address@example.com?blat=foop>
test_case!("mailto:address@example.com?blat=foop",
addresses => "address@example.com";
body => None;
("To", "address@example.com")
);
// 6.2. Examples of Complicated Email Addresses
assert_eq!(
mlt!("mailto:%22not%40me%22@example.org").address,
vec![addr!(r#""not@me"@example.org"#)]
);
// Email address: "oh\\no"@example.org; corresponding 'mailto' URI:
// <mailto:%22oh%5C%5Cno%22@example.org>.
assert_eq!(
mlt!("mailto:%22not%40me%22@example.org").address,
vec![addr!(r#""not@me"@example.org"#)]
);
// Email address: "\\\"it's\ ugly\\\""@example.org; corresponding
// 'mailto' URI:
// <mailto:%22%5C%5C%5C%22it's%5C%20ugly%5C%5C%5C%22%22@example.org>.
assert_eq!(
mlt!("mailto:%22%5C%5C%5C%22it's%5C%20ugly%5C%5C%5C%22%22@example.org").address,
vec![addr!(r#""\\\"it's\ ugly\\\""@example.org"#)]
);
// When an email address itself includes an "&" (ampersand) character, that
// character has to be percent-encoded. For example, the 'mailto' URI
// to send mail to "Mike&family@example.org" is
// <mailto:Mike%26family@example.org>.
assert_eq!(
mlt!("mailto:Mike%26family@example.org").address,
vec![addr!("Mike&family@example.org")]
);
// Sending a mail with the subject "coffee" in French, i.e., "cafe" where the
// final e is an e-acute, using UTF-8 and percent-encoding:
// <mailto:user@example.org?subject=caf%C3%A9>
assert_eq!(
&mlt!("mailto:user@example.org?subject=caf%C3%A9").headers[HDR::SUBJECT],
"café"
);
// The same subject, this time using an encoded-word (escaping the "="
// and "?" characters used in the encoded-word syntax, because they are
// reserved):
// <mailto:user@example.org?subject=%3D%3Futf-8%3FQ%3Fcaf%3DC3%3DA9%3F%3D>
assert_eq!(
&mlt!("mailto:user@example.org?subject=%3D%3Futf-8%3FQ%3Fcaf%3DC3%3DA9%3F%3D").headers
[HDR::SUBJECT],
"=?utf-8?Q?caf=C3=A9?="
);
// The same subject, this time encoded as iso-8859-1:
// <mailto:user@example.org?subject=%3D%3Fiso-8859-1%3FQ%3Fcaf%3DE9%3F%3D>
assert_eq!(
&mlt!("mailto:user@example.org?subject=%3D%3Fiso-8859-1%3FQ%3Fcaf%3DE9%3F%3D").headers
[HDR::SUBJECT],
"=?iso-8859-1?Q?caf=E9?="
);
// Going back to straight UTF-8 and adding a body with the same value:
//
// <mailto:user@example.org?subject=caf%C3%A9&body=caf%C3%A9>
test_case!("mailto:user@example.org?subject=caf%C3%A9&body=caf%C3%A9",
addresses => "user@example.org";
body => Some("café");
("To", "user@example.org"),
("Subject", "café")
);
// The following example uses the Japanese word "natto" (Unicode
// characters U+7D0D U+8C46) as a domain name label, sending a mail to a
// user at "natto".example.org:
// <mailto:user@%E7%B4%8D%E8%B1%86.example.org?subject=Test&body=NATTO>
// When constructing the email, the domain name label is converted to
// punycode. The resulting message may look as follows:
// From: sender@example.net
// To: user@xn--99zt52a.example.org
// Subject: Test
// Content-Type: text/plain
// Content-Transfer-Encoding: 7bit
//
// NATTO
test_case!("mailto:user@%E7%B4%8D%E8%B1%86.example.org?subject=Test&body=NATTO",
addresses => "user@納豆.example.org";
body => Some("NATTO");
("To", "user@納豆.example.org"),
("Subject", "Test")
);
}
}