melib: add fqdn to MID
parent
87adc6dd19
commit
f348cc9a55
|
@ -81,6 +81,16 @@ impl Address {
|
|||
Address::Group(_) => String::new(),
|
||||
}
|
||||
}
|
||||
pub fn get_fqdn(&self) -> Option<String> {
|
||||
match self {
|
||||
Address::Mailbox(m) => {
|
||||
let raw_address = m.address_spec.display_bytes(&m.raw);
|
||||
let fqdn_pos = raw_address.iter().position(|&b| b == b'@')? + 1;
|
||||
Some(String::from_utf8_lossy(&raw_address[fqdn_pos..]).into())
|
||||
}
|
||||
Address::Group(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Address {}
|
||||
|
@ -157,7 +167,6 @@ impl StrBuilder {
|
|||
let length = self.length;
|
||||
String::from_utf8(s[offset..offset + length].to_vec()).unwrap()
|
||||
}
|
||||
#[cfg(test)]
|
||||
fn display_bytes<'a>(&self, b: &'a [u8]) -> &'a [u8] {
|
||||
&b[self.offset..(self.offset + self.length)]
|
||||
}
|
||||
|
@ -190,7 +199,7 @@ impl StrBuild for MessageID {
|
|||
|
||||
#[test]
|
||||
fn test_strbuilder() {
|
||||
let m_id = b"<20170825132332.6734-1-el13635@mail.ntua.gr>";
|
||||
let m_id = b"<20170825132332.6734-1@el13635@mail.ntua.gr>";
|
||||
let (_, slice) = parser::message_id(m_id).unwrap();
|
||||
assert_eq!(
|
||||
MessageID::new(m_id, slice),
|
||||
|
|
|
@ -33,15 +33,13 @@ impl Default for Draft {
|
|||
let now: DateTime<Local> = Local::now();
|
||||
headers.insert("Date".into(), now.to_rfc2822());
|
||||
headers.insert("Subject".into(), "".into());
|
||||
headers.insert("Message-ID".into(), random::gen_message_id());
|
||||
headers.insert("User-Agent".into(), "meli".into());
|
||||
headers.insert("User-Agent".into(), "meli 0.0".into());
|
||||
header_order.push("Date".into());
|
||||
header_order.push("From".into());
|
||||
header_order.push("To".into());
|
||||
header_order.push("Cc".into());
|
||||
header_order.push("Bcc".into());
|
||||
header_order.push("Subject".into());
|
||||
header_order.push("Message-ID".into());
|
||||
header_order.push("User-Agent".into());
|
||||
Draft {
|
||||
headers,
|
||||
|
@ -78,6 +76,26 @@ impl str::FromStr for Draft {
|
|||
ret.header_order.push(String::from_utf8(k.to_vec())?);
|
||||
}
|
||||
}
|
||||
if ret.headers.contains_key("From") && !ret.headers.contains_key("Message-ID") {
|
||||
if let super::parser::IResult::Done(_, addr) =
|
||||
super::parser::mailbox(ret.headers["From"].as_bytes())
|
||||
{
|
||||
if let Some(fqdn) = addr.get_fqdn() {
|
||||
if ret
|
||||
.headers
|
||||
.insert("Message-ID".into(), random::gen_message_id(&fqdn))
|
||||
.is_none()
|
||||
{
|
||||
let pos = ret
|
||||
.header_order
|
||||
.iter()
|
||||
.position(|h| h == "Subject")
|
||||
.unwrap();
|
||||
ret.header_order.insert(pos, "Message-ID".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let body = Envelope::new(0).body_bytes(s.as_bytes());
|
||||
|
||||
|
@ -192,6 +210,7 @@ impl Draft {
|
|||
let v = &self.headers[k];
|
||||
ret.extend(format!("{}: {}\n", k, v).chars());
|
||||
}
|
||||
ret.push_str("MIME-Version: 1.0\n");
|
||||
|
||||
if self.body.is_ascii() {
|
||||
ret.push('\n');
|
||||
|
|
|
@ -43,9 +43,9 @@ fn base36(mut m: u64) -> String {
|
|||
ret
|
||||
}
|
||||
|
||||
pub fn gen_message_id() -> String {
|
||||
pub fn gen_message_id(fqdn: &str) -> String {
|
||||
let clock = base36(clock());
|
||||
let rand = base36(random_u64());
|
||||
|
||||
format!("<{}.{}@meli>", clock, rand)
|
||||
format!("<{}.{}@{}>", clock, rand, fqdn)
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ use chrono;
|
|||
use data_encoding::BASE64_MIME;
|
||||
use encoding::{DecoderTrap, Encoding};
|
||||
use nom::{is_hex_digit, le_u8};
|
||||
use nom::{ErrorKind, IResult, Needed};
|
||||
pub(super) use nom::{ErrorKind, IResult, Needed};
|
||||
|
||||
use encoding::all::*;
|
||||
use std;
|
||||
|
@ -406,7 +406,7 @@ fn addr_spec(input: &[u8]) -> IResult<&[u8], Address> {
|
|||
}
|
||||
|
||||
named!(
|
||||
mailbox<Address>,
|
||||
pub mailbox<Address>,
|
||||
ws!(alt_complete!(display_addr | addr_spec))
|
||||
);
|
||||
named!(mailbox_list<Vec<Address>>, many0!(mailbox));
|
||||
|
|
Loading…
Reference in New Issue