From 184c58bf36d861065125e0a36c7518bf6c2e8f75 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Sun, 18 Aug 2019 20:12:19 +0200 Subject: [PATCH 1/5] move main file making this step by step in hope that git can then better work with this --- src/{dc_configure.rs => dc_configure/mod.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{dc_configure.rs => dc_configure/mod.rs} (100%) diff --git a/src/dc_configure.rs b/src/dc_configure/mod.rs similarity index 100% rename from src/dc_configure.rs rename to src/dc_configure/mod.rs From 51b54fce64b04a0acc0374415006e975b0ced4dd Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Sun, 18 Aug 2019 20:49:58 +0200 Subject: [PATCH 2/5] move out the autoconfigure functions --- src/dc_configure/auto_moz.rs | 224 ++++++++++++++++ src/dc_configure/auto_outlk.rs | 213 +++++++++++++++ src/dc_configure/mod.rs | 461 ++------------------------------- 3 files changed, 459 insertions(+), 439 deletions(-) create mode 100644 src/dc_configure/auto_moz.rs create mode 100644 src/dc_configure/auto_outlk.rs diff --git a/src/dc_configure/auto_moz.rs b/src/dc_configure/auto_moz.rs new file mode 100644 index 000000000..0da737fcd --- /dev/null +++ b/src/dc_configure/auto_moz.rs @@ -0,0 +1,224 @@ +use quick_xml; +use quick_xml::events::{BytesEnd, BytesStart, BytesText}; + +use crate::context::Context; +use crate::dc_loginparam::*; +use crate::dc_tools::*; +use crate::x::*; + +use super::read_autoconf_file; +/* ****************************************************************************** + * Thunderbird's Autoconfigure + ******************************************************************************/ +/* documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration */ +#[repr(C)] +struct moz_autoconfigure_t<'a> { + pub in_0: &'a dc_loginparam_t, + pub in_emaildomain: *mut libc::c_char, + pub in_emaillocalpart: *mut libc::c_char, + pub out: dc_loginparam_t, + pub out_imap_set: libc::c_int, + pub out_smtp_set: libc::c_int, + pub tag_server: libc::c_int, + pub tag_config: libc::c_int, +} + +pub unsafe fn moz_autoconfigure( + context: &Context, + url: &str, + param_in: &dc_loginparam_t, +) -> Option { + let mut moz_ac = moz_autoconfigure_t { + in_0: param_in, + in_emaildomain: std::ptr::null_mut(), + in_emaillocalpart: std::ptr::null_mut(), + out: dc_loginparam_new(), + out_imap_set: 0, + out_smtp_set: 0, + tag_server: 0, + tag_config: 0, + }; + + let url_c = url.strdup(); + let xml_raw = read_autoconf_file(context, url_c); + free(url_c as *mut libc::c_void); + if xml_raw.is_null() { + return None; + } + + moz_ac.in_emaillocalpart = param_in.addr.strdup(); + let p = strchr(moz_ac.in_emaillocalpart, '@' as i32); + + if p.is_null() { + free(xml_raw as *mut libc::c_void); + free(moz_ac.in_emaildomain as *mut libc::c_void); + free(moz_ac.in_emaillocalpart as *mut libc::c_void); + return None; + } + + *p = 0 as libc::c_char; + moz_ac.in_emaildomain = dc_strdup(p.offset(1isize)); + + let mut reader = quick_xml::Reader::from_str(as_str(xml_raw)); + reader.trim_text(true); + + let mut buf = Vec::new(); + + loop { + match reader.read_event(&mut buf) { + Ok(quick_xml::events::Event::Start(ref e)) => { + moz_autoconfigure_starttag_cb(e, &mut moz_ac, &reader) + } + Ok(quick_xml::events::Event::End(ref e)) => moz_autoconfigure_endtag_cb(e, &mut moz_ac), + Ok(quick_xml::events::Event::Text(ref e)) => { + moz_autoconfigure_text_cb(e, &mut moz_ac, &reader) + } + Err(e) => { + error!( + context, + 0, + "Configure xml: Error at position {}: {:?}", + reader.buffer_position(), + e + ); + } + Ok(quick_xml::events::Event::Eof) => break, + _ => (), + } + buf.clear(); + } + + if moz_ac.out.mail_server.is_empty() + || moz_ac.out.mail_port == 0 + || moz_ac.out.send_server.is_empty() + || moz_ac.out.send_port == 0 + { + let r = dc_loginparam_get_readable(&moz_ac.out); + warn!(context, 0, "Bad or incomplete autoconfig: {}", r,); + free(xml_raw as *mut libc::c_void); + free(moz_ac.in_emaildomain as *mut libc::c_void); + free(moz_ac.in_emaillocalpart as *mut libc::c_void); + return None; + } + + free(xml_raw as *mut libc::c_void); + free(moz_ac.in_emaildomain as *mut libc::c_void); + free(moz_ac.in_emaillocalpart as *mut libc::c_void); + Some(moz_ac.out) +} + +fn moz_autoconfigure_text_cb( + event: &BytesText, + moz_ac: &mut moz_autoconfigure_t, + reader: &quick_xml::Reader, +) { + let val = event.unescape_and_decode(reader).unwrap_or_default(); + + let addr = &moz_ac.in_0.addr; + let email_local = as_str(moz_ac.in_emaillocalpart); + let email_domain = as_str(moz_ac.in_emaildomain); + + let val = val + .trim() + .replace("%EMAILADDRESS%", addr) + .replace("%EMAILLOCALPART%", email_local) + .replace("%EMAILDOMAIN%", email_domain); + + if moz_ac.tag_server == 1 { + match moz_ac.tag_config { + 10 => moz_ac.out.mail_server = val, + 11 => moz_ac.out.mail_port = val.parse().unwrap_or_default(), + 12 => moz_ac.out.mail_user = val, + 13 => { + let val_lower = val.to_lowercase(); + if val_lower == "ssl" { + moz_ac.out.server_flags |= 0x200 + } + if val_lower == "starttls" { + moz_ac.out.server_flags |= 0x100 + } + if val_lower == "plain" { + moz_ac.out.server_flags |= 0x400 + } + } + _ => {} + } + } else if moz_ac.tag_server == 2 { + match moz_ac.tag_config { + 10 => moz_ac.out.send_server = val, + 11 => moz_ac.out.send_port = val.parse().unwrap_or_default(), + 12 => moz_ac.out.send_user = val, + 13 => { + let val_lower = val.to_lowercase(); + if val_lower == "ssl" { + moz_ac.out.server_flags |= 0x20000 + } + if val_lower == "starttls" { + moz_ac.out.server_flags |= 0x10000 + } + if val_lower == "plain" { + moz_ac.out.server_flags |= 0x40000 + } + } + _ => {} + } + } +} + +fn moz_autoconfigure_endtag_cb(event: &BytesEnd, moz_ac: &mut moz_autoconfigure_t) { + let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); + + if tag == "incomingserver" { + moz_ac.tag_server = 0; + moz_ac.tag_config = 0; + moz_ac.out_imap_set = 1; + } else if tag == "outgoingserver" { + moz_ac.tag_server = 0; + moz_ac.tag_config = 0; + moz_ac.out_smtp_set = 1; + } else { + moz_ac.tag_config = 0; + } +} + +fn moz_autoconfigure_starttag_cb( + event: &BytesStart, + moz_ac: &mut moz_autoconfigure_t, + reader: &quick_xml::Reader, +) { + let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); + + if tag == "incomingserver" { + moz_ac.tag_server = if let Some(typ) = event.attributes().find(|attr| { + attr.as_ref() + .map(|a| String::from_utf8_lossy(a.key).trim().to_lowercase() == "type") + .unwrap_or_default() + }) { + let typ = typ + .unwrap() + .unescape_and_decode_value(reader) + .unwrap_or_default() + .to_lowercase(); + + if typ == "imap" && moz_ac.out_imap_set == 0 { + 1 + } else { + 0 + } + } else { + 0 + }; + moz_ac.tag_config = 0; + } else if tag == "outgoingserver" { + moz_ac.tag_server = if moz_ac.out_smtp_set == 0 { 2 } else { 0 }; + moz_ac.tag_config = 0; + } else if tag == "hostname" { + moz_ac.tag_config = 10; + } else if tag == "port" { + moz_ac.tag_config = 11; + } else if tag == "sockettype" { + moz_ac.tag_config = 13; + } else if tag == "username" { + moz_ac.tag_config = 12; + } +} diff --git a/src/dc_configure/auto_outlk.rs b/src/dc_configure/auto_outlk.rs new file mode 100644 index 000000000..c476b09dc --- /dev/null +++ b/src/dc_configure/auto_outlk.rs @@ -0,0 +1,213 @@ +use quick_xml; +use quick_xml::events::{BytesEnd, BytesStart, BytesText}; + +use crate::context::Context; +use crate::dc_loginparam::*; +use crate::dc_tools::*; +use crate::x::*; +use std::ptr; + +use super::read_autoconf_file; +/* ****************************************************************************** + * Outlook's Autodiscover + ******************************************************************************/ +#[repr(C)] +struct outlk_autodiscover_t<'a> { + pub in_0: &'a dc_loginparam_t, + pub out: dc_loginparam_t, + pub out_imap_set: libc::c_int, + pub out_smtp_set: libc::c_int, + pub tag_config: libc::c_int, + pub config: [*mut libc::c_char; 6], + pub redirect: *mut libc::c_char, +} + +pub unsafe fn outlk_autodiscover( + context: &Context, + url__: &str, + param_in: &dc_loginparam_t, +) -> Option { + let mut xml_raw: *mut libc::c_char = ptr::null_mut(); + let mut url = url__.strdup(); + let mut outlk_ad = outlk_autodiscover_t { + in_0: param_in, + out: dc_loginparam_new(), + out_imap_set: 0, + out_smtp_set: 0, + tag_config: 0, + config: [ptr::null_mut(); 6], + redirect: ptr::null_mut(), + }; + let ok_to_continue; + let mut i = 0; + loop { + if !(i < 10) { + ok_to_continue = true; + break; + } + memset( + &mut outlk_ad as *mut outlk_autodiscover_t as *mut libc::c_void, + 0, + ::std::mem::size_of::(), + ); + xml_raw = read_autoconf_file(context, url); + if xml_raw.is_null() { + ok_to_continue = false; + break; + } + + let mut reader = quick_xml::Reader::from_str(as_str(xml_raw)); + reader.trim_text(true); + + let mut buf = Vec::new(); + + loop { + match reader.read_event(&mut buf) { + Ok(quick_xml::events::Event::Start(ref e)) => { + outlk_autodiscover_starttag_cb(e, &mut outlk_ad) + } + Ok(quick_xml::events::Event::End(ref e)) => { + outlk_autodiscover_endtag_cb(e, &mut outlk_ad) + } + Ok(quick_xml::events::Event::Text(ref e)) => { + outlk_autodiscover_text_cb(e, &mut outlk_ad, &reader) + } + Err(e) => { + error!( + context, + 0, + "Configure xml: Error at position {}: {:?}", + reader.buffer_position(), + e + ); + } + Ok(quick_xml::events::Event::Eof) => break, + _ => (), + } + buf.clear(); + } + + if !(!outlk_ad.config[5].is_null() + && 0 != *outlk_ad.config[5usize].offset(0isize) as libc::c_int) + { + ok_to_continue = true; + break; + } + free(url as *mut libc::c_void); + url = dc_strdup(outlk_ad.config[5usize]); + + outlk_clean_config(&mut outlk_ad); + free(xml_raw as *mut libc::c_void); + xml_raw = ptr::null_mut(); + i += 1; + } + + if ok_to_continue { + if outlk_ad.out.mail_server.is_empty() + || outlk_ad.out.mail_port == 0 + || outlk_ad.out.send_server.is_empty() + || outlk_ad.out.send_port == 0 + { + let r = dc_loginparam_get_readable(&outlk_ad.out); + warn!(context, 0, "Bad or incomplete autoconfig: {}", r,); + free(url as *mut libc::c_void); + free(xml_raw as *mut libc::c_void); + outlk_clean_config(&mut outlk_ad); + + return None; + } + } + free(url as *mut libc::c_void); + free(xml_raw as *mut libc::c_void); + outlk_clean_config(&mut outlk_ad); + Some(outlk_ad.out) +} + +unsafe fn outlk_clean_config(mut outlk_ad: *mut outlk_autodiscover_t) { + for i in 0..6 { + free((*outlk_ad).config[i] as *mut libc::c_void); + (*outlk_ad).config[i] = 0 as *mut libc::c_char; + } +} + +fn outlk_autodiscover_text_cb( + event: &BytesText, + outlk_ad: &mut outlk_autodiscover_t, + reader: &quick_xml::Reader, +) { + let val = event.unescape_and_decode(reader).unwrap_or_default(); + + unsafe { + free(outlk_ad.config[outlk_ad.tag_config as usize].cast()); + outlk_ad.config[outlk_ad.tag_config as usize] = val.trim().strdup(); + } +} + +unsafe fn outlk_autodiscover_endtag_cb(event: &BytesEnd, outlk_ad: &mut outlk_autodiscover_t) { + let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); + + if tag == "protocol" { + if !outlk_ad.config[1].is_null() { + let port = dc_atoi_null_is_0(outlk_ad.config[3]); + let ssl_on = (!outlk_ad.config[4].is_null() + && strcasecmp( + outlk_ad.config[4], + b"on\x00" as *const u8 as *const libc::c_char, + ) == 0) as libc::c_int; + let ssl_off = (!outlk_ad.config[4].is_null() + && strcasecmp( + outlk_ad.config[4], + b"off\x00" as *const u8 as *const libc::c_char, + ) == 0) as libc::c_int; + if strcasecmp( + outlk_ad.config[1], + b"imap\x00" as *const u8 as *const libc::c_char, + ) == 0 + && outlk_ad.out_imap_set == 0 + { + outlk_ad.out.mail_server = to_string(outlk_ad.config[2]); + outlk_ad.out.mail_port = port; + if 0 != ssl_on { + outlk_ad.out.server_flags |= 0x200 + } else if 0 != ssl_off { + outlk_ad.out.server_flags |= 0x400 + } + outlk_ad.out_imap_set = 1 + } else if strcasecmp( + outlk_ad.config[1usize], + b"smtp\x00" as *const u8 as *const libc::c_char, + ) == 0 + && outlk_ad.out_smtp_set == 0 + { + outlk_ad.out.send_server = to_string(outlk_ad.config[2]); + outlk_ad.out.send_port = port; + if 0 != ssl_on { + outlk_ad.out.server_flags |= 0x20000 + } else if 0 != ssl_off { + outlk_ad.out.server_flags |= 0x40000 + } + outlk_ad.out_smtp_set = 1 + } + } + outlk_clean_config(outlk_ad); + } + outlk_ad.tag_config = 0; +} + +fn outlk_autodiscover_starttag_cb(event: &BytesStart, outlk_ad: &mut outlk_autodiscover_t) { + let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); + + if tag == "protocol" { + unsafe { outlk_clean_config(outlk_ad) }; + } else if tag == "type" { + outlk_ad.tag_config = 1 + } else if tag == "server" { + outlk_ad.tag_config = 2 + } else if tag == "port" { + outlk_ad.tag_config = 3 + } else if tag == "ssl" { + outlk_ad.tag_config = 4 + } else if tag == "redirecturl" { + outlk_ad.tag_config = 5 + }; +} diff --git a/src/dc_configure/mod.rs b/src/dc_configure/mod.rs index ef7860092..2caf44513 100644 --- a/src/dc_configure/mod.rs +++ b/src/dc_configure/mod.rs @@ -1,6 +1,4 @@ use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; -use quick_xml; -use quick_xml::events::{BytesEnd, BytesStart, BytesText}; use crate::constants::Event; use crate::context::Context; @@ -12,8 +10,11 @@ use crate::job::*; use crate::oauth2::*; use crate::param::Params; use crate::types::*; -use crate::x::*; -use std::ptr; + +mod auto_outlk; +use auto_outlk::outlk_autodiscover; +mod auto_moz; +use auto_moz::moz_autoconfigure; macro_rules! progress { ($context:tt, $progress:expr) => { @@ -39,35 +40,7 @@ struct dc_imapfolder_t { pub name_utf8: *mut libc::c_char, pub meaning: libc::c_int, } -/* ****************************************************************************** - * Thunderbird's Autoconfigure - ******************************************************************************/ -/* documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration */ -#[repr(C)] -struct moz_autoconfigure_t<'a> { - pub in_0: &'a dc_loginparam_t, - pub in_emaildomain: *mut libc::c_char, - pub in_emaillocalpart: *mut libc::c_char, - pub out: dc_loginparam_t, - pub out_imap_set: libc::c_int, - pub out_smtp_set: libc::c_int, - pub tag_server: libc::c_int, - pub tag_config: libc::c_int, -} -/* ****************************************************************************** - * Outlook's Autodiscover - ******************************************************************************/ -#[repr(C)] -struct outlk_autodiscover_t<'a> { - pub in_0: &'a dc_loginparam_t, - pub out: dc_loginparam_t, - pub out_imap_set: libc::c_int, - pub out_smtp_set: libc::c_int, - pub tag_config: libc::c_int, - pub config: [*mut libc::c_char; 6], - pub redirect: *mut libc::c_char, -} // connect pub unsafe fn dc_configure(context: &Context) { if 0 != dc_has_ongoing(context) { @@ -692,413 +665,6 @@ pub unsafe fn dc_free_ongoing(context: &Context) { s.shall_stop_ongoing = true; } -unsafe fn moz_autoconfigure( - context: &Context, - url: &str, - param_in: &dc_loginparam_t, -) -> Option { - let mut moz_ac = moz_autoconfigure_t { - in_0: param_in, - in_emaildomain: std::ptr::null_mut(), - in_emaillocalpart: std::ptr::null_mut(), - out: dc_loginparam_new(), - out_imap_set: 0, - out_smtp_set: 0, - tag_server: 0, - tag_config: 0, - }; - - let url_c = url.strdup(); - let xml_raw = read_autoconf_file(context, url_c); - free(url_c as *mut libc::c_void); - if xml_raw.is_null() { - return None; - } - - moz_ac.in_emaillocalpart = param_in.addr.strdup(); - let p = strchr(moz_ac.in_emaillocalpart, '@' as i32); - - if p.is_null() { - free(xml_raw as *mut libc::c_void); - free(moz_ac.in_emaildomain as *mut libc::c_void); - free(moz_ac.in_emaillocalpart as *mut libc::c_void); - return None; - } - - *p = 0 as libc::c_char; - moz_ac.in_emaildomain = dc_strdup(p.offset(1isize)); - - let mut reader = quick_xml::Reader::from_str(as_str(xml_raw)); - reader.trim_text(true); - - let mut buf = Vec::new(); - - loop { - match reader.read_event(&mut buf) { - Ok(quick_xml::events::Event::Start(ref e)) => { - moz_autoconfigure_starttag_cb(e, &mut moz_ac, &reader) - } - Ok(quick_xml::events::Event::End(ref e)) => moz_autoconfigure_endtag_cb(e, &mut moz_ac), - Ok(quick_xml::events::Event::Text(ref e)) => { - moz_autoconfigure_text_cb(e, &mut moz_ac, &reader) - } - Err(e) => { - error!( - context, - 0, - "Configure xml: Error at position {}: {:?}", - reader.buffer_position(), - e - ); - } - Ok(quick_xml::events::Event::Eof) => break, - _ => (), - } - buf.clear(); - } - - if moz_ac.out.mail_server.is_empty() - || moz_ac.out.mail_port == 0 - || moz_ac.out.send_server.is_empty() - || moz_ac.out.send_port == 0 - { - let r = dc_loginparam_get_readable(&moz_ac.out); - warn!(context, 0, "Bad or incomplete autoconfig: {}", r,); - free(xml_raw as *mut libc::c_void); - free(moz_ac.in_emaildomain as *mut libc::c_void); - free(moz_ac.in_emaillocalpart as *mut libc::c_void); - return None; - } - - free(xml_raw as *mut libc::c_void); - free(moz_ac.in_emaildomain as *mut libc::c_void); - free(moz_ac.in_emaillocalpart as *mut libc::c_void); - Some(moz_ac.out) -} - -fn moz_autoconfigure_text_cb( - event: &BytesText, - moz_ac: &mut moz_autoconfigure_t, - reader: &quick_xml::Reader, -) { - let val = event.unescape_and_decode(reader).unwrap_or_default(); - - let addr = &moz_ac.in_0.addr; - let email_local = as_str(moz_ac.in_emaillocalpart); - let email_domain = as_str(moz_ac.in_emaildomain); - - let val = val - .trim() - .replace("%EMAILADDRESS%", addr) - .replace("%EMAILLOCALPART%", email_local) - .replace("%EMAILDOMAIN%", email_domain); - - if moz_ac.tag_server == 1 { - match moz_ac.tag_config { - 10 => moz_ac.out.mail_server = val, - 11 => moz_ac.out.mail_port = val.parse().unwrap_or_default(), - 12 => moz_ac.out.mail_user = val, - 13 => { - let val_lower = val.to_lowercase(); - if val_lower == "ssl" { - moz_ac.out.server_flags |= 0x200 - } - if val_lower == "starttls" { - moz_ac.out.server_flags |= 0x100 - } - if val_lower == "plain" { - moz_ac.out.server_flags |= 0x400 - } - } - _ => {} - } - } else if moz_ac.tag_server == 2 { - match moz_ac.tag_config { - 10 => moz_ac.out.send_server = val, - 11 => moz_ac.out.send_port = val.parse().unwrap_or_default(), - 12 => moz_ac.out.send_user = val, - 13 => { - let val_lower = val.to_lowercase(); - if val_lower == "ssl" { - moz_ac.out.server_flags |= 0x20000 - } - if val_lower == "starttls" { - moz_ac.out.server_flags |= 0x10000 - } - if val_lower == "plain" { - moz_ac.out.server_flags |= 0x40000 - } - } - _ => {} - } - } -} - -fn moz_autoconfigure_endtag_cb(event: &BytesEnd, moz_ac: &mut moz_autoconfigure_t) { - let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); - - if tag == "incomingserver" { - moz_ac.tag_server = 0; - moz_ac.tag_config = 0; - moz_ac.out_imap_set = 1; - } else if tag == "outgoingserver" { - moz_ac.tag_server = 0; - moz_ac.tag_config = 0; - moz_ac.out_smtp_set = 1; - } else { - moz_ac.tag_config = 0; - } -} - -fn moz_autoconfigure_starttag_cb( - event: &BytesStart, - moz_ac: &mut moz_autoconfigure_t, - reader: &quick_xml::Reader, -) { - let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); - - if tag == "incomingserver" { - moz_ac.tag_server = if let Some(typ) = event.attributes().find(|attr| { - attr.as_ref() - .map(|a| String::from_utf8_lossy(a.key).trim().to_lowercase() == "type") - .unwrap_or_default() - }) { - let typ = typ - .unwrap() - .unescape_and_decode_value(reader) - .unwrap_or_default() - .to_lowercase(); - - if typ == "imap" && moz_ac.out_imap_set == 0 { - 1 - } else { - 0 - } - } else { - 0 - }; - moz_ac.tag_config = 0; - } else if tag == "outgoingserver" { - moz_ac.tag_server = if moz_ac.out_smtp_set == 0 { 2 } else { 0 }; - moz_ac.tag_config = 0; - } else if tag == "hostname" { - moz_ac.tag_config = 10; - } else if tag == "port" { - moz_ac.tag_config = 11; - } else if tag == "sockettype" { - moz_ac.tag_config = 13; - } else if tag == "username" { - moz_ac.tag_config = 12; - } -} - -fn read_autoconf_file(context: &Context, url: *const libc::c_char) -> *mut libc::c_char { - info!(context, 0, "Testing {} ...", to_string(url)); - - match reqwest::Client::new() - .get(as_str(url)) - .send() - .and_then(|mut res| res.text()) - { - Ok(res) => unsafe { res.strdup() }, - Err(_err) => { - info!(context, 0, "Can\'t read file.",); - - std::ptr::null_mut() - } - } -} - -unsafe fn outlk_autodiscover( - context: &Context, - url__: &str, - param_in: &dc_loginparam_t, -) -> Option { - let mut xml_raw: *mut libc::c_char = ptr::null_mut(); - let mut url = url__.strdup(); - let mut outlk_ad = outlk_autodiscover_t { - in_0: param_in, - out: dc_loginparam_new(), - out_imap_set: 0, - out_smtp_set: 0, - tag_config: 0, - config: [ptr::null_mut(); 6], - redirect: ptr::null_mut(), - }; - let ok_to_continue; - let mut i = 0; - loop { - if !(i < 10) { - ok_to_continue = true; - break; - } - memset( - &mut outlk_ad as *mut outlk_autodiscover_t as *mut libc::c_void, - 0, - ::std::mem::size_of::(), - ); - xml_raw = read_autoconf_file(context, url); - if xml_raw.is_null() { - ok_to_continue = false; - break; - } - - let mut reader = quick_xml::Reader::from_str(as_str(xml_raw)); - reader.trim_text(true); - - let mut buf = Vec::new(); - - loop { - match reader.read_event(&mut buf) { - Ok(quick_xml::events::Event::Start(ref e)) => { - outlk_autodiscover_starttag_cb(e, &mut outlk_ad) - } - Ok(quick_xml::events::Event::End(ref e)) => { - outlk_autodiscover_endtag_cb(e, &mut outlk_ad) - } - Ok(quick_xml::events::Event::Text(ref e)) => { - outlk_autodiscover_text_cb(e, &mut outlk_ad, &reader) - } - Err(e) => { - error!( - context, - 0, - "Configure xml: Error at position {}: {:?}", - reader.buffer_position(), - e - ); - } - Ok(quick_xml::events::Event::Eof) => break, - _ => (), - } - buf.clear(); - } - - if !(!outlk_ad.config[5].is_null() - && 0 != *outlk_ad.config[5usize].offset(0isize) as libc::c_int) - { - ok_to_continue = true; - break; - } - free(url as *mut libc::c_void); - url = dc_strdup(outlk_ad.config[5usize]); - - outlk_clean_config(&mut outlk_ad); - free(xml_raw as *mut libc::c_void); - xml_raw = ptr::null_mut(); - i += 1; - } - - if ok_to_continue { - if outlk_ad.out.mail_server.is_empty() - || outlk_ad.out.mail_port == 0 - || outlk_ad.out.send_server.is_empty() - || outlk_ad.out.send_port == 0 - { - let r = dc_loginparam_get_readable(&outlk_ad.out); - warn!(context, 0, "Bad or incomplete autoconfig: {}", r,); - free(url as *mut libc::c_void); - free(xml_raw as *mut libc::c_void); - outlk_clean_config(&mut outlk_ad); - - return None; - } - } - free(url as *mut libc::c_void); - free(xml_raw as *mut libc::c_void); - outlk_clean_config(&mut outlk_ad); - Some(outlk_ad.out) -} - -unsafe fn outlk_clean_config(mut outlk_ad: *mut outlk_autodiscover_t) { - for i in 0..6 { - free((*outlk_ad).config[i] as *mut libc::c_void); - (*outlk_ad).config[i] = 0 as *mut libc::c_char; - } -} - -fn outlk_autodiscover_text_cb( - event: &BytesText, - outlk_ad: &mut outlk_autodiscover_t, - reader: &quick_xml::Reader, -) { - let val = event.unescape_and_decode(reader).unwrap_or_default(); - - unsafe { - free(outlk_ad.config[outlk_ad.tag_config as usize].cast()); - outlk_ad.config[outlk_ad.tag_config as usize] = val.trim().strdup(); - } -} - -unsafe fn outlk_autodiscover_endtag_cb(event: &BytesEnd, outlk_ad: &mut outlk_autodiscover_t) { - let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); - - if tag == "protocol" { - if !outlk_ad.config[1].is_null() { - let port = dc_atoi_null_is_0(outlk_ad.config[3]); - let ssl_on = (!outlk_ad.config[4].is_null() - && strcasecmp( - outlk_ad.config[4], - b"on\x00" as *const u8 as *const libc::c_char, - ) == 0) as libc::c_int; - let ssl_off = (!outlk_ad.config[4].is_null() - && strcasecmp( - outlk_ad.config[4], - b"off\x00" as *const u8 as *const libc::c_char, - ) == 0) as libc::c_int; - if strcasecmp( - outlk_ad.config[1], - b"imap\x00" as *const u8 as *const libc::c_char, - ) == 0 - && outlk_ad.out_imap_set == 0 - { - outlk_ad.out.mail_server = to_string(outlk_ad.config[2]); - outlk_ad.out.mail_port = port; - if 0 != ssl_on { - outlk_ad.out.server_flags |= 0x200 - } else if 0 != ssl_off { - outlk_ad.out.server_flags |= 0x400 - } - outlk_ad.out_imap_set = 1 - } else if strcasecmp( - outlk_ad.config[1usize], - b"smtp\x00" as *const u8 as *const libc::c_char, - ) == 0 - && outlk_ad.out_smtp_set == 0 - { - outlk_ad.out.send_server = to_string(outlk_ad.config[2]); - outlk_ad.out.send_port = port; - if 0 != ssl_on { - outlk_ad.out.server_flags |= 0x20000 - } else if 0 != ssl_off { - outlk_ad.out.server_flags |= 0x40000 - } - outlk_ad.out_smtp_set = 1 - } - } - outlk_clean_config(outlk_ad); - } - outlk_ad.tag_config = 0; -} - -fn outlk_autodiscover_starttag_cb(event: &BytesStart, outlk_ad: &mut outlk_autodiscover_t) { - let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); - - if tag == "protocol" { - unsafe { outlk_clean_config(outlk_ad) }; - } else if tag == "type" { - outlk_ad.tag_config = 1 - } else if tag == "server" { - outlk_ad.tag_config = 2 - } else if tag == "port" { - outlk_ad.tag_config = 3 - } else if tag == "ssl" { - outlk_ad.tag_config = 4 - } else if tag == "redirecturl" { - outlk_ad.tag_config = 5 - }; -} - pub unsafe fn dc_alloc_ongoing(context: &Context) -> libc::c_int { if 0 != dc_has_ongoing(context) { warn!( @@ -1139,3 +705,20 @@ pub fn dc_connect_to_configured_imap(context: &Context, imap: &Imap) -> libc::c_ ret_connected } + +pub fn read_autoconf_file(context: &Context, url: *const libc::c_char) -> *mut libc::c_char { + info!(context, 0, "Testing {} ...", to_string(url)); + + match reqwest::Client::new() + .get(as_str(url)) + .send() + .and_then(|mut res| res.text()) + { + Ok(res) => unsafe { res.strdup() }, + Err(_err) => { + info!(context, 0, "Can\'t read file.",); + + std::ptr::null_mut() + } + } +} From e3b2a7a69b6b7c08a3ac6712079562973dfeff7d Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Sun, 18 Aug 2019 20:51:05 +0200 Subject: [PATCH 3/5] remove unused struct --- src/dc_configure/mod.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/dc_configure/mod.rs b/src/dc_configure/mod.rs index 2caf44513..0f63e68cf 100644 --- a/src/dc_configure/mod.rs +++ b/src/dc_configure/mod.rs @@ -30,17 +30,6 @@ macro_rules! progress { }; } -/* ****************************************************************************** - * Configure folders - ******************************************************************************/ -#[derive(Copy, Clone)] -#[repr(C)] -struct dc_imapfolder_t { - pub name_to_select: *mut libc::c_char, - pub name_utf8: *mut libc::c_char, - pub meaning: libc::c_int, -} - // connect pub unsafe fn dc_configure(context: &Context) { if 0 != dc_has_ongoing(context) { From 491826556b01e543466661ebebc43158b4e0fbf1 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Sun, 18 Aug 2019 21:21:43 +0200 Subject: [PATCH 4/5] renaming dc_configure to configure and renaming the autoconfigure modules --- deltachat-ffi/src/lib.rs | 6 +++--- examples/repl/cmdline.rs | 2 +- examples/repl/main.rs | 4 ++-- examples/simple.rs | 4 ++-- .../auto_moz.rs => configure/auto_mozilla.rs} | 0 .../auto_outlk.rs => configure/auto_outlook.rs} | 0 src/{dc_configure => configure}/mod.rs | 10 +++++----- src/constants.rs | 6 +++--- src/dc_imex.rs | 2 +- src/dc_securejoin.rs | 2 +- src/job.rs | 2 +- src/job_thread.rs | 2 +- src/lib.rs | 2 +- 13 files changed, 21 insertions(+), 21 deletions(-) rename src/{dc_configure/auto_moz.rs => configure/auto_mozilla.rs} (100%) rename src/{dc_configure/auto_outlk.rs => configure/auto_outlook.rs} (100%) rename src/{dc_configure => configure}/mod.rs (99%) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 5d65e75d7..47101f523 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -173,7 +173,7 @@ pub unsafe extern "C" fn dc_configure(context: *mut dc_context_t) { assert!(!context.is_null()); let context = &*context; - dc_configure::dc_configure(context) + configure::configure(context) } #[no_mangle] @@ -181,7 +181,7 @@ pub unsafe extern "C" fn dc_is_configured(context: *mut dc_context_t) -> libc::c assert!(!context.is_null()); let context = &*context; - dc_configure::dc_is_configured(context) + configure::dc_is_configured(context) } #[no_mangle] @@ -986,7 +986,7 @@ pub unsafe extern "C" fn dc_stop_ongoing_process(context: *mut dc_context_t) { assert!(!context.is_null()); let context = &*context; - dc_configure::dc_stop_ongoing_process(context) + configure::dc_stop_ongoing_process(context) } #[no_mangle] diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index d1454e0e3..7738393bf 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -7,7 +7,7 @@ use deltachat::config; use deltachat::constants::*; use deltachat::contact::*; use deltachat::context::*; -use deltachat::dc_configure::*; +use deltachat::configure::*; use deltachat::dc_imex::*; use deltachat::dc_location::*; use deltachat::dc_msg::*; diff --git a/examples/repl/main.rs b/examples/repl/main.rs index 358ffeaf4..5055c290f 100644 --- a/examples/repl/main.rs +++ b/examples/repl/main.rs @@ -20,7 +20,7 @@ use std::sync::{Arc, Mutex, RwLock}; use deltachat::config; use deltachat::constants::*; use deltachat::context::*; -use deltachat::dc_configure::*; +use deltachat::configure::*; use deltachat::dc_securejoin::*; use deltachat::dc_tools::*; use deltachat::job::*; @@ -495,7 +495,7 @@ unsafe fn handle_cmd(line: &str, ctx: Arc>) -> Result { start_threads(ctx.clone()); - dc_configure(&ctx.read().unwrap()); + configure(&ctx.read().unwrap()); } "oauth2" => { if let Some(addr) = ctx.read().unwrap().get_config(config::Config::Addr) { diff --git a/examples/simple.rs b/examples/simple.rs index 9db186533..3bb90fdcf 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -11,7 +11,7 @@ use deltachat::config; use deltachat::constants::Event; use deltachat::contact::*; use deltachat::context::*; -use deltachat::dc_configure::*; +use deltachat::configure::*; use deltachat::job::{ perform_imap_fetch, perform_imap_idle, perform_imap_jobs, perform_smtp_idle, perform_smtp_jobs, }; @@ -87,7 +87,7 @@ fn main() { ctx.set_config(config::Config::Addr, Some("d@testrun.org")) .unwrap(); ctx.set_config(config::Config::MailPw, Some(&pw)).unwrap(); - dc_configure(&ctx); + configure(&ctx); thread::sleep(duration); diff --git a/src/dc_configure/auto_moz.rs b/src/configure/auto_mozilla.rs similarity index 100% rename from src/dc_configure/auto_moz.rs rename to src/configure/auto_mozilla.rs diff --git a/src/dc_configure/auto_outlk.rs b/src/configure/auto_outlook.rs similarity index 100% rename from src/dc_configure/auto_outlk.rs rename to src/configure/auto_outlook.rs diff --git a/src/dc_configure/mod.rs b/src/configure/mod.rs similarity index 99% rename from src/dc_configure/mod.rs rename to src/configure/mod.rs index 0f63e68cf..a347dac30 100644 --- a/src/dc_configure/mod.rs +++ b/src/configure/mod.rs @@ -11,10 +11,10 @@ use crate::oauth2::*; use crate::param::Params; use crate::types::*; -mod auto_outlk; -use auto_outlk::outlk_autodiscover; -mod auto_moz; -use auto_moz::moz_autoconfigure; +mod auto_outlook; +use auto_outlook::outlk_autodiscover; +mod auto_mozilla; +use auto_mozilla::moz_autoconfigure; macro_rules! progress { ($context:tt, $progress:expr) => { @@ -31,7 +31,7 @@ macro_rules! progress { } // connect -pub unsafe fn dc_configure(context: &Context) { +pub unsafe fn configure(context: &Context) { if 0 != dc_has_ongoing(context) { warn!( context, diff --git a/src/constants.rs b/src/constants.rs index 062632fdf..8089fcb33 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -128,7 +128,7 @@ pub const DC_CREATE_MVBOX: usize = 1; // via dc_set_config() using the key "server_flags". /// Force OAuth2 authorization. This flag does not skip automatic configuration. -/// Before calling dc_configure() with DC_LP_AUTH_OAUTH2 set, +/// Before calling configure() with DC_LP_AUTH_OAUTH2 set, /// the user has to confirm access at the URL returned by dc_get_oauth2_url(). pub const DC_LP_AUTH_OAUTH2: usize = 0x2; @@ -282,7 +282,7 @@ pub enum Event { /// As most things are asynchronous, things may go wrong at any time and the user /// should not be disturbed by a dialog or so. Instead, use a bubble or so. /// - /// However, for ongoing processes (eg. dc_configure()) + /// However, for ongoing processes (eg. configure()) /// or for functions that are expected to fail (eg. dc_continue_key_transfer()) /// it might be better to delay showing these events until the function has really /// failed (returned false). It should be sufficient to report only the _last_ error @@ -400,7 +400,7 @@ pub enum Event { /// @return 0 LOCATION_CHANGED = 2035, - /// Inform about the configuration progress started by dc_configure(). + /// Inform about the configuration progress started by configure(). /// /// @param data1 (int) 0=error, 1-999=progress in permille, 1000=success and done /// @param data2 0 diff --git a/src/dc_imex.rs b/src/dc_imex.rs index 55858e759..11c2a8de5 100644 --- a/src/dc_imex.rs +++ b/src/dc_imex.rs @@ -10,7 +10,7 @@ use crate::chat; use crate::config::Config; use crate::constants::*; use crate::context::Context; -use crate::dc_configure::*; +use crate::configure::*; use crate::dc_e2ee::*; use crate::dc_msg::*; use crate::dc_tools::*; diff --git a/src/dc_securejoin.rs b/src/dc_securejoin.rs index 1910411c8..9ff018fbb 100644 --- a/src/dc_securejoin.rs +++ b/src/dc_securejoin.rs @@ -6,7 +6,7 @@ use crate::chat::{self, Chat}; use crate::constants::*; use crate::contact::*; use crate::context::Context; -use crate::dc_configure::*; +use crate::configure::*; use crate::dc_e2ee::*; use crate::dc_mimeparser::*; use crate::dc_msg::*; diff --git a/src/job.rs b/src/job.rs index ea7faa5a7..1cf0a72e1 100644 --- a/src/job.rs +++ b/src/job.rs @@ -8,7 +8,7 @@ use rand::{thread_rng, Rng}; use crate::chat; use crate::constants::*; use crate::context::Context; -use crate::dc_configure::*; +use crate::configure::*; use crate::dc_imex::*; use crate::dc_location::*; use crate::dc_loginparam::*; diff --git a/src/job_thread.rs b/src/job_thread.rs index eb62de8ba..bf34a5e46 100644 --- a/src/job_thread.rs +++ b/src/job_thread.rs @@ -1,7 +1,7 @@ use std::sync::{Arc, Condvar, Mutex}; use crate::context::Context; -use crate::dc_configure::*; +use crate::configure::*; use crate::imap::Imap; pub struct JobThread { diff --git a/src/lib.rs b/src/lib.rs index bdebfb851..0d9bd7453 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,7 @@ mod aheader; pub mod chat; pub mod chatlist; pub mod config; +pub mod configure; pub mod constants; pub mod contact; pub mod context; @@ -47,7 +48,6 @@ pub mod types; pub mod x; pub mod dc_array; -pub mod dc_configure; mod dc_dehtml; mod dc_e2ee; pub mod dc_imex; From 91481caf8983d8e01cc0f50928b564f6c44047d7 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Mon, 19 Aug 2019 19:04:12 +0200 Subject: [PATCH 5/5] cargo fmt --- examples/repl/cmdline.rs | 2 +- examples/repl/main.rs | 2 +- examples/simple.rs | 2 +- src/dc_imex.rs | 2 +- src/dc_securejoin.rs | 2 +- src/job.rs | 2 +- src/job_thread.rs | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index 7738393bf..641810652 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -4,10 +4,10 @@ use std::str::FromStr; use deltachat::chat::{self, Chat}; use deltachat::chatlist::*; use deltachat::config; +use deltachat::configure::*; use deltachat::constants::*; use deltachat::contact::*; use deltachat::context::*; -use deltachat::configure::*; use deltachat::dc_imex::*; use deltachat::dc_location::*; use deltachat::dc_msg::*; diff --git a/examples/repl/main.rs b/examples/repl/main.rs index 5055c290f..5b5409dd8 100644 --- a/examples/repl/main.rs +++ b/examples/repl/main.rs @@ -18,9 +18,9 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, RwLock}; use deltachat::config; +use deltachat::configure::*; use deltachat::constants::*; use deltachat::context::*; -use deltachat::configure::*; use deltachat::dc_securejoin::*; use deltachat::dc_tools::*; use deltachat::job::*; diff --git a/examples/simple.rs b/examples/simple.rs index 3bb90fdcf..e5d31ea06 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -8,10 +8,10 @@ use tempfile::tempdir; use deltachat::chat; use deltachat::chatlist::*; use deltachat::config; +use deltachat::configure::*; use deltachat::constants::Event; use deltachat::contact::*; use deltachat::context::*; -use deltachat::configure::*; use deltachat::job::{ perform_imap_fetch, perform_imap_idle, perform_imap_jobs, perform_smtp_idle, perform_smtp_jobs, }; diff --git a/src/dc_imex.rs b/src/dc_imex.rs index 11c2a8de5..33b8717f8 100644 --- a/src/dc_imex.rs +++ b/src/dc_imex.rs @@ -8,9 +8,9 @@ use rand::{thread_rng, Rng}; use crate::chat; use crate::config::Config; +use crate::configure::*; use crate::constants::*; use crate::context::Context; -use crate::configure::*; use crate::dc_e2ee::*; use crate::dc_msg::*; use crate::dc_tools::*; diff --git a/src/dc_securejoin.rs b/src/dc_securejoin.rs index 9ff018fbb..de7de9d06 100644 --- a/src/dc_securejoin.rs +++ b/src/dc_securejoin.rs @@ -3,10 +3,10 @@ use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use crate::aheader::EncryptPreference; use crate::chat::{self, Chat}; +use crate::configure::*; use crate::constants::*; use crate::contact::*; use crate::context::Context; -use crate::configure::*; use crate::dc_e2ee::*; use crate::dc_mimeparser::*; use crate::dc_msg::*; diff --git a/src/job.rs b/src/job.rs index 1cf0a72e1..45e86f90d 100644 --- a/src/job.rs +++ b/src/job.rs @@ -6,9 +6,9 @@ use deltachat_derive::{FromSql, ToSql}; use rand::{thread_rng, Rng}; use crate::chat; +use crate::configure::*; use crate::constants::*; use crate::context::Context; -use crate::configure::*; use crate::dc_imex::*; use crate::dc_location::*; use crate::dc_loginparam::*; diff --git a/src/job_thread.rs b/src/job_thread.rs index bf34a5e46..d1059fd53 100644 --- a/src/job_thread.rs +++ b/src/job_thread.rs @@ -1,7 +1,7 @@ use std::sync::{Arc, Condvar, Mutex}; -use crate::context::Context; use crate::configure::*; +use crate::context::Context; use crate::imap::Imap; pub struct JobThread {