Merge pull request #693 from deltachat/mozautoconfig

Use Rust types in auto_mozilla.rs
This commit is contained in:
Alexander Krotov
2019-10-07 23:03:17 +00:00
committed by GitHub

View File

@@ -10,16 +10,29 @@ use super::read_autoconf_file;
* Thunderbird's Autoconfigure * Thunderbird's Autoconfigure
******************************************************************************/ ******************************************************************************/
/* documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration */ /* documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration */
#[repr(C)] struct MozAutoconfigure<'a> {
struct moz_autoconfigure_t<'a> {
pub in_0: &'a LoginParam, pub in_0: &'a LoginParam,
pub in_emaildomain: &'a str, pub in_emaildomain: &'a str,
pub in_emaillocalpart: &'a str, pub in_emaillocalpart: &'a str,
pub out: LoginParam, pub out: LoginParam,
pub out_imap_set: libc::c_int, pub out_imap_set: bool,
pub out_smtp_set: libc::c_int, pub out_smtp_set: bool,
pub tag_server: libc::c_int, pub tag_server: MozServer,
pub tag_config: libc::c_int, pub tag_config: MozConfigTag,
}
enum MozServer {
Undefined,
Imap,
Smtp,
}
enum MozConfigTag {
Undefined,
Hostname,
Port,
Sockettype,
Username,
} }
pub fn moz_autoconfigure( pub fn moz_autoconfigure(
@@ -30,11 +43,8 @@ pub fn moz_autoconfigure(
let xml_raw = read_autoconf_file(context, url)?; let xml_raw = read_autoconf_file(context, url)?;
// Split address into local part and domain part. // Split address into local part and domain part.
let p = param_in.addr.find("@"); let p = param_in.addr.find('@')?;
if p.is_none() { let (in_emaillocalpart, in_emaildomain) = param_in.addr.split_at(p);
return None;
}
let (in_emaillocalpart, in_emaildomain) = param_in.addr.split_at(p.unwrap());
let in_emaildomain = &in_emaildomain[1..]; let in_emaildomain = &in_emaildomain[1..];
let mut reader = quick_xml::Reader::from_str(&xml_raw); let mut reader = quick_xml::Reader::from_str(&xml_raw);
@@ -42,15 +52,15 @@ pub fn moz_autoconfigure(
let mut buf = Vec::new(); let mut buf = Vec::new();
let mut moz_ac = moz_autoconfigure_t { let mut moz_ac = MozAutoconfigure {
in_0: param_in, in_0: param_in,
in_emaildomain, in_emaildomain,
in_emaillocalpart, in_emaillocalpart,
out: LoginParam::new(), out: LoginParam::new(),
out_imap_set: 0, out_imap_set: false,
out_smtp_set: 0, out_smtp_set: false,
tag_server: 0, tag_server: MozServer::Undefined,
tag_config: 0, tag_config: MozConfigTag::Undefined,
}; };
loop { loop {
match reader.read_event(&mut buf) { match reader.read_event(&mut buf) {
@@ -90,7 +100,7 @@ pub fn moz_autoconfigure(
fn moz_autoconfigure_text_cb<B: std::io::BufRead>( fn moz_autoconfigure_text_cb<B: std::io::BufRead>(
event: &BytesText, event: &BytesText,
moz_ac: &mut moz_autoconfigure_t, moz_ac: &mut MozAutoconfigure,
reader: &quick_xml::Reader<B>, reader: &quick_xml::Reader<B>,
) { ) {
let val = event.unescape_and_decode(reader).unwrap_or_default(); let val = event.unescape_and_decode(reader).unwrap_or_default();
@@ -105,12 +115,12 @@ fn moz_autoconfigure_text_cb<B: std::io::BufRead>(
.replace("%EMAILLOCALPART%", email_local) .replace("%EMAILLOCALPART%", email_local)
.replace("%EMAILDOMAIN%", email_domain); .replace("%EMAILDOMAIN%", email_domain);
if moz_ac.tag_server == 1 { match moz_ac.tag_server {
match moz_ac.tag_config { MozServer::Imap => match moz_ac.tag_config {
10 => moz_ac.out.mail_server = val, MozConfigTag::Hostname => moz_ac.out.mail_server = val,
11 => moz_ac.out.mail_port = val.parse().unwrap_or_default(), MozConfigTag::Port => moz_ac.out.mail_port = val.parse().unwrap_or_default(),
12 => moz_ac.out.mail_user = val, MozConfigTag::Username => moz_ac.out.mail_user = val,
13 => { MozConfigTag::Sockettype => {
let val_lower = val.to_lowercase(); let val_lower = val.to_lowercase();
if val_lower == "ssl" { if val_lower == "ssl" {
moz_ac.out.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32 moz_ac.out.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32
@@ -123,13 +133,12 @@ fn moz_autoconfigure_text_cb<B: std::io::BufRead>(
} }
} }
_ => {} _ => {}
} },
} else if moz_ac.tag_server == 2 { MozServer::Smtp => match moz_ac.tag_config {
match moz_ac.tag_config { MozConfigTag::Hostname => moz_ac.out.send_server = val,
10 => moz_ac.out.send_server = val, MozConfigTag::Port => moz_ac.out.send_port = val.parse().unwrap_or_default(),
11 => moz_ac.out.send_port = val.parse().unwrap_or_default(), MozConfigTag::Username => moz_ac.out.send_user = val,
12 => moz_ac.out.send_user = val, MozConfigTag::Sockettype => {
13 => {
let val_lower = val.to_lowercase(); let val_lower = val.to_lowercase();
if val_lower == "ssl" { if val_lower == "ssl" {
moz_ac.out.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32 moz_ac.out.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32
@@ -142,29 +151,30 @@ fn moz_autoconfigure_text_cb<B: std::io::BufRead>(
} }
} }
_ => {} _ => {}
} },
MozServer::Undefined => {}
} }
} }
fn moz_autoconfigure_endtag_cb(event: &BytesEnd, moz_ac: &mut moz_autoconfigure_t) { fn moz_autoconfigure_endtag_cb(event: &BytesEnd, moz_ac: &mut MozAutoconfigure) {
let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase();
if tag == "incomingserver" { if tag == "incomingserver" {
moz_ac.tag_server = 0; moz_ac.tag_server = MozServer::Undefined;
moz_ac.tag_config = 0; moz_ac.tag_config = MozConfigTag::Undefined;
moz_ac.out_imap_set = 1; moz_ac.out_imap_set = true;
} else if tag == "outgoingserver" { } else if tag == "outgoingserver" {
moz_ac.tag_server = 0; moz_ac.tag_server = MozServer::Undefined;
moz_ac.tag_config = 0; moz_ac.tag_config = MozConfigTag::Undefined;
moz_ac.out_smtp_set = 1; moz_ac.out_smtp_set = true;
} else { } else {
moz_ac.tag_config = 0; moz_ac.tag_config = MozConfigTag::Undefined;
} }
} }
fn moz_autoconfigure_starttag_cb<B: std::io::BufRead>( fn moz_autoconfigure_starttag_cb<B: std::io::BufRead>(
event: &BytesStart, event: &BytesStart,
moz_ac: &mut moz_autoconfigure_t, moz_ac: &mut MozAutoconfigure,
reader: &quick_xml::Reader<B>, reader: &quick_xml::Reader<B>,
) { ) {
let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase();
@@ -181,25 +191,29 @@ fn moz_autoconfigure_starttag_cb<B: std::io::BufRead>(
.unwrap_or_default() .unwrap_or_default()
.to_lowercase(); .to_lowercase();
if typ == "imap" && moz_ac.out_imap_set == 0 { if typ == "imap" && !moz_ac.out_imap_set {
1 MozServer::Imap
} else { } else {
0 MozServer::Undefined
} }
} else { } else {
0 MozServer::Undefined
}; };
moz_ac.tag_config = 0; moz_ac.tag_config = MozConfigTag::Undefined;
} else if tag == "outgoingserver" { } else if tag == "outgoingserver" {
moz_ac.tag_server = if moz_ac.out_smtp_set == 0 { 2 } else { 0 }; moz_ac.tag_server = if !moz_ac.out_smtp_set {
moz_ac.tag_config = 0; MozServer::Smtp
} else {
MozServer::Undefined
};
moz_ac.tag_config = MozConfigTag::Undefined;
} else if tag == "hostname" { } else if tag == "hostname" {
moz_ac.tag_config = 10; moz_ac.tag_config = MozConfigTag::Hostname;
} else if tag == "port" { } else if tag == "port" {
moz_ac.tag_config = 11; moz_ac.tag_config = MozConfigTag::Port;
} else if tag == "sockettype" { } else if tag == "sockettype" {
moz_ac.tag_config = 13; moz_ac.tag_config = MozConfigTag::Sockettype;
} else if tag == "username" { } else if tag == "username" {
moz_ac.tag_config = 12; moz_ac.tag_config = MozConfigTag::Username;
} }
} }