refactor(location): remove most unsafe usage

This commit is contained in:
dignifiedquire
2019-08-26 21:07:53 +02:00
parent ae17971599
commit bb16849eef
6 changed files with 207 additions and 293 deletions

View File

@@ -1,11 +1,11 @@
use crate::dc_location::dc_location; use crate::dc_location::Location;
use crate::types::*; use crate::types::*;
/* * the structure behind dc_array_t */ /* * the structure behind dc_array_t */
#[derive(Clone)] #[derive(Clone)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub enum dc_array_t { pub enum dc_array_t {
Locations(Vec<dc_location>), Locations(Vec<Location>),
Uint(Vec<u32>), Uint(Vec<u32>),
} }
@@ -27,7 +27,7 @@ impl dc_array_t {
} }
} }
pub fn add_location(&mut self, location: dc_location) { pub fn add_location(&mut self, location: Location) {
if let Self::Locations(array) = self { if let Self::Locations(array) = self {
array.push(location) array.push(location)
} else { } else {
@@ -42,7 +42,7 @@ impl dc_array_t {
} }
} }
pub fn get_location(&self, index: usize) -> &dc_location { pub fn get_location(&self, index: usize) -> &Location {
if let Self::Locations(array) = self { if let Self::Locations(array) = self {
&array[index] &array[index]
} else { } else {
@@ -108,8 +108,8 @@ impl From<Vec<u32>> for dc_array_t {
} }
} }
impl From<Vec<dc_location>> for dc_array_t { impl From<Vec<Location>> for dc_array_t {
fn from(array: Vec<dc_location>) -> Self { fn from(array: Vec<Location>) -> Self {
dc_array_t::Locations(array) dc_array_t::Locations(array)
} }
} }

View File

@@ -6,69 +6,51 @@ use crate::constants::Event;
use crate::constants::*; use crate::constants::*;
use crate::context::*; use crate::context::*;
use crate::dc_tools::*; use crate::dc_tools::*;
use crate::error::Error;
use crate::job::*; use crate::job::*;
use crate::message::*; use crate::message::*;
use crate::param::*; use crate::param::*;
use crate::sql; use crate::sql;
use crate::stock::StockMessage; use crate::stock::StockMessage;
use crate::types::*; use crate::types::*;
use crate::x::*;
// location handling // location handling
#[derive(Clone, Default)] #[derive(Debug, Clone, Default)]
#[allow(non_camel_case_types)] pub struct Location {
pub struct dc_location { pub location_id: u32,
pub location_id: uint32_t, pub latitude: f64,
pub latitude: libc::c_double, pub longitude: f64,
pub longitude: libc::c_double, pub accuracy: f64,
pub accuracy: libc::c_double,
pub timestamp: i64, pub timestamp: i64,
pub contact_id: uint32_t, pub contact_id: u32,
pub msg_id: uint32_t, pub msg_id: u32,
pub chat_id: uint32_t, pub chat_id: u32,
pub marker: Option<String>, pub marker: Option<String>,
pub independent: uint32_t, pub independent: u32,
} }
impl dc_location { impl Location {
pub fn new() -> Self { pub fn new() -> Self {
dc_location { Default::default()
location_id: 0,
latitude: 0.0,
longitude: 0.0,
accuracy: 0.0,
timestamp: 0,
contact_id: 0,
msg_id: 0,
chat_id: 0,
marker: None,
independent: 0,
}
} }
} }
#[derive(Clone)] #[derive(Debug, Clone, Default)]
#[allow(non_camel_case_types)] pub struct Kml {
pub struct dc_kml_t { pub addr: Option<String>,
pub addr: *mut libc::c_char, pub locations: Option<Vec<Location>>,
pub locations: Option<Vec<dc_location>>, pub tag: i32,
pub tag: libc::c_int, pub curr: Location,
pub curr: dc_location,
} }
impl dc_kml_t { impl Kml {
pub fn new() -> Self { pub fn new() -> Self {
dc_kml_t { Default::default()
addr: std::ptr::null_mut(),
locations: None,
tag: 0,
curr: dc_location::new(),
}
} }
} }
// location streaming // location streaming
pub unsafe fn dc_send_locations_to_chat(context: &Context, chat_id: uint32_t, seconds: i64) { pub fn dc_send_locations_to_chat(context: &Context, chat_id: u32, seconds: i64) {
let now = time(); let now = time();
let mut msg: Message; let mut msg: Message;
let is_sending_locations_before: bool; let is_sending_locations_before: bool;
@@ -94,7 +76,7 @@ pub unsafe fn dc_send_locations_to_chat(context: &Context, chat_id: uint32_t, se
msg.text = msg.text =
Some(context.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0)); Some(context.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0));
msg.param.set_int(Param::Cmd, 8); msg.param.set_int(Param::Cmd, 8);
chat::send_msg(context, chat_id, &mut msg).unwrap(); unsafe { chat::send_msg(context, chat_id, &mut msg).unwrap() };
} else if 0 == seconds && is_sending_locations_before { } else if 0 == seconds && is_sending_locations_before {
let stock_str = let stock_str =
context.stock_system_msg(StockMessage::MsgLocationDisabled, "", "", 0); context.stock_system_msg(StockMessage::MsgLocationDisabled, "", "", 0);
@@ -123,7 +105,7 @@ pub unsafe fn dc_send_locations_to_chat(context: &Context, chat_id: uint32_t, se
* job to send locations out to all chats that want them * job to send locations out to all chats that want them
******************************************************************************/ ******************************************************************************/
#[allow(non_snake_case)] #[allow(non_snake_case)]
unsafe fn schedule_MAYBE_SEND_LOCATIONS(context: &Context, flags: libc::c_int) { fn schedule_MAYBE_SEND_LOCATIONS(context: &Context, flags: i32) {
if 0 != flags & 0x1 || !job_action_exists(context, Action::MaybeSendLocations) { if 0 != flags & 0x1 || !job_action_exists(context, Action::MaybeSendLocations) {
job_add(context, Action::MaybeSendLocations, 0, Params::new(), 60); job_add(context, Action::MaybeSendLocations, 0, Params::new(), 60);
}; };
@@ -141,9 +123,9 @@ pub fn dc_is_sending_locations_to_chat(context: &Context, chat_id: u32) -> bool
pub fn dc_set_location( pub fn dc_set_location(
context: &Context, context: &Context,
latitude: libc::c_double, latitude: f64,
longitude: libc::c_double, longitude: f64,
accuracy: libc::c_double, accuracy: f64,
) -> libc::c_int { ) -> libc::c_int {
if latitude == 0.0 && longitude == 0.0 { if latitude == 0.0 && longitude == 0.0 {
return 1; return 1;
@@ -174,7 +156,7 @@ pub fn dc_set_location(
if continue_streaming { if continue_streaming {
context.call_cb(Event::LOCATION_CHANGED, 1, 0); context.call_cb(Event::LOCATION_CHANGED, 1, 0);
}; };
unsafe { schedule_MAYBE_SEND_LOCATIONS(context, 0) }; schedule_MAYBE_SEND_LOCATIONS(context, 0);
Ok(continue_streaming as libc::c_int) Ok(continue_streaming as libc::c_int)
} }
).unwrap_or_default() ).unwrap_or_default()
@@ -182,11 +164,11 @@ pub fn dc_set_location(
pub fn dc_get_locations( pub fn dc_get_locations(
context: &Context, context: &Context,
chat_id: uint32_t, chat_id: u32,
contact_id: uint32_t, contact_id: u32,
timestamp_from: i64, timestamp_from: i64,
mut timestamp_to: i64, mut timestamp_to: i64,
) -> Vec<dc_location> { ) -> Vec<Location> {
if timestamp_to == 0 { if timestamp_to == 0 {
timestamp_to = time() + 10; timestamp_to = time() + 10;
} }
@@ -217,7 +199,7 @@ pub fn dc_get_locations(
None None
}; };
let loc = dc_location { let loc = Location {
location_id: row.get(0)?, location_id: row.get(0)?,
latitude: row.get(1)?, latitude: row.get(1)?,
longitude: row.get(2)?, longitude: row.get(2)?,
@@ -255,22 +237,18 @@ pub fn dc_delete_all_locations(context: &Context) -> bool {
true true
} }
pub fn dc_get_location_kml( pub fn dc_get_location_kml(context: &Context, chat_id: u32) -> Result<(String, u32), Error> {
context: &Context,
chat_id: uint32_t,
last_added_location_id: *mut uint32_t,
) -> *mut libc::c_char {
let mut success: libc::c_int = 0;
let now = time(); let now = time();
let mut location_count: libc::c_int = 0; let mut location_count = 0;
let mut ret = String::new(); let mut ret = String::new();
let mut last_added_location_id = 0;
let self_addr = context let self_addr = context
.sql .sql
.get_config(context, "configured_addr") .get_config(context, "configured_addr")
.unwrap_or_default(); .unwrap_or_default();
if let Ok((locations_send_begin, locations_send_until, locations_last_sent)) = context.sql.query_row( let (locations_send_begin, locations_send_until, locations_last_sent) = context.sql.query_row(
"SELECT locations_send_begin, locations_send_until, locations_last_sent FROM chats WHERE id=?;", "SELECT locations_send_begin, locations_send_until, locations_last_sent FROM chats WHERE id=?;",
params![chat_id as i32], |row| { params![chat_id as i32], |row| {
let send_begin: i64 = row.get(0)?; let send_begin: i64 = row.get(0)?;
@@ -278,8 +256,8 @@ pub fn dc_get_location_kml(
let last_sent: i64 = row.get(2)?; let last_sent: i64 = row.get(2)?;
Ok((send_begin, send_until, last_sent)) Ok((send_begin, send_until, last_sent))
} })?;
) {
if !(locations_send_begin == 0 || now > locations_send_until) { if !(locations_send_begin == 0 || now > locations_send_until) {
ret += &format!( ret += &format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"{}\">\n", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"{}\">\n",
@@ -300,7 +278,7 @@ pub fn dc_get_location_kml(
let latitude: f64 = row.get(1)?; let latitude: f64 = row.get(1)?;
let longitude: f64 = row.get(2)?; let longitude: f64 = row.get(2)?;
let accuracy: f64 = row.get(3)?; let accuracy: f64 = row.get(3)?;
let timestamp = unsafe { get_kml_timestamp(row.get(4)?) }; let timestamp = get_kml_timestamp(row.get(4)?);
Ok((location_id, latitude, longitude, accuracy, timestamp)) Ok((location_id, latitude, longitude, accuracy, timestamp))
}, },
@@ -309,75 +287,47 @@ pub fn dc_get_location_kml(
let (location_id, latitude, longitude, accuracy, timestamp) = row?; let (location_id, latitude, longitude, accuracy, timestamp) = row?;
ret += &format!( ret += &format!(
"<Placemark><Timestamp><when>{}</when></Timestamp><Point><coordinates accuracy=\"{}\">{},{}</coordinates></Point></Placemark>\n\x00", "<Placemark><Timestamp><when>{}</when></Timestamp><Point><coordinates accuracy=\"{}\">{},{}</coordinates></Point></Placemark>\n\x00",
as_str(timestamp), timestamp,
accuracy, accuracy,
longitude, longitude,
latitude latitude
); );
location_count += 1; location_count += 1;
if !last_added_location_id.is_null() { last_added_location_id = location_id as u32;
unsafe { *last_added_location_id = location_id as u32 };
}
unsafe { free(timestamp as *mut libc::c_void) };
} }
Ok(()) Ok(())
} }
).unwrap(); // TODO: better error handling )?;
}
} }
if location_count > 0 { ensure!(location_count > 0, "No locations processed");
ret += "</Document>\n</kml>"; ret += "</Document>\n</kml>";
success = 1;
}
if 0 != success { Ok((ret, last_added_location_id))
unsafe { ret.strdup() }
} else {
std::ptr::null_mut()
}
} }
/******************************************************************************* fn get_kml_timestamp(utc: i64) -> String {
* create kml-files
******************************************************************************/
unsafe fn get_kml_timestamp(utc: i64) -> *mut libc::c_char {
// Returns a string formatted as YYYY-MM-DDTHH:MM:SSZ. The trailing `Z` indicates UTC. // Returns a string formatted as YYYY-MM-DDTHH:MM:SSZ. The trailing `Z` indicates UTC.
let res = chrono::NaiveDateTime::from_timestamp(utc, 0) chrono::NaiveDateTime::from_timestamp(utc, 0)
.format("%Y-%m-%dT%H:%M:%SZ") .format("%Y-%m-%dT%H:%M:%SZ")
.to_string(); .to_string()
res.strdup()
} }
pub unsafe fn dc_get_message_kml( pub fn dc_get_message_kml(timestamp: i64, latitude: f64, longitude: f64) -> String {
timestamp: i64, format!(
latitude: libc::c_double, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
longitude: libc::c_double,
) -> *mut libc::c_char {
let timestamp_str = get_kml_timestamp(timestamp);
let latitude_str = dc_ftoa(latitude);
let longitude_str = dc_ftoa(longitude);
let ret = dc_mprintf(
b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n\ <kml xmlns=\"http://www.opengis.net/kml/2.2\">\n\
<Document>\n\ <Document>\n\
<Placemark>\ <Placemark>\
<Timestamp><when>%s</when></Timestamp>\ <Timestamp><when>{}</when></Timestamp>\
<Point><coordinates>%s,%s</coordinates></Point>\ <Point><coordinates>{:.2},{:.2}</coordinates></Point>\
</Placemark>\n\ </Placemark>\n\
</Document>\n\ </Document>\n\
</kml>\x00" as *const u8 as *const libc::c_char, </kml>",
timestamp_str, get_kml_timestamp(timestamp),
longitude_str, // reverse order! longitude,
latitude_str, latitude,
); )
free(latitude_str as *mut libc::c_void);
free(longitude_str as *mut libc::c_void);
free(timestamp_str as *mut libc::c_void);
ret
} }
pub fn dc_set_kml_sent_timestamp(context: &Context, chat_id: u32, timestamp: i64) -> bool { pub fn dc_set_kml_sent_timestamp(context: &Context, chat_id: u32, timestamp: i64) -> bool {
@@ -400,12 +350,12 @@ pub fn dc_set_msg_location_id(context: &Context, msg_id: u32, location_id: u32)
.is_ok() .is_ok()
} }
pub unsafe fn dc_save_locations( pub fn dc_save_locations(
context: &Context, context: &Context,
chat_id: u32, chat_id: u32,
contact_id: u32, contact_id: u32,
locations_opt: &Option<Vec<dc_location>>, locations_opt: &Option<Vec<Location>>,
independent: libc::c_int, independent: i32,
) -> u32 { ) -> u32 {
if chat_id <= 9 || locations_opt.is_none() { if chat_id <= 9 || locations_opt.is_none() {
return 0; return 0;
@@ -458,26 +408,17 @@ pub unsafe fn dc_save_locations(
.unwrap_or_default() .unwrap_or_default()
} }
pub unsafe fn dc_kml_parse( pub fn dc_kml_parse(context: &Context, content: impl AsRef<str>) -> Result<Kml, Error> {
context: &Context, ensure!(
content: *const libc::c_char, content.as_ref().len() <= (1 * 1024 * 1024),
content_bytes: size_t, "A kml-files with {} bytes is larger than reasonably expected.",
) -> dc_kml_t { content.as_ref().len()
let mut kml = dc_kml_t::new();
if content_bytes > (1 * 1024 * 1024) {
warn!(
context,
0, "A kml-files with {} bytes is larger than reasonably expected.", content_bytes,
); );
return kml;
}
let content_null = dc_null_terminate(content, content_bytes as libc::c_int); let mut reader = quick_xml::Reader::from_str(content.as_ref());
if !content_null.is_null() {
let mut reader = quick_xml::Reader::from_str(as_str(content_null));
reader.trim_text(true); reader.trim_text(true);
let mut kml = Kml::new();
kml.locations = Some(Vec::with_capacity(100)); kml.locations = Some(Vec::with_capacity(100));
let mut buf = Vec::new(); let mut buf = Vec::new();
@@ -501,16 +442,13 @@ pub unsafe fn dc_kml_parse(
} }
buf.clear(); buf.clear();
} }
}
free(content_null.cast()); Ok(kml)
kml
} }
fn kml_text_cb<B: std::io::BufRead>( fn kml_text_cb<B: std::io::BufRead>(
event: &BytesText, event: &BytesText,
kml: &mut dc_kml_t, kml: &mut Kml,
reader: &quick_xml::Reader<B>, reader: &quick_xml::Reader<B>,
) { ) {
if 0 != kml.tag & (0x4 | 0x10) { if 0 != kml.tag & (0x4 | 0x10) {
@@ -546,7 +484,7 @@ fn kml_text_cb<B: std::io::BufRead>(
} }
} }
fn kml_endtag_cb(event: &BytesEnd, kml: &mut dc_kml_t) { fn kml_endtag_cb(event: &BytesEnd, kml: &mut Kml) {
let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase(); let tag = String::from_utf8_lossy(event.name()).trim().to_lowercase();
if tag == "placemark" { if tag == "placemark" {
@@ -556,7 +494,7 @@ fn kml_endtag_cb(event: &BytesEnd, kml: &mut dc_kml_t) {
&& 0. != kml.curr.longitude && 0. != kml.curr.longitude
{ {
if let Some(ref mut locations) = kml.locations { if let Some(ref mut locations) = kml.locations {
locations.push(std::mem::replace(&mut kml.curr, dc_location::new())); locations.push(std::mem::replace(&mut kml.curr, Location::new()));
} }
} }
kml.tag = 0 kml.tag = 0
@@ -565,7 +503,7 @@ fn kml_endtag_cb(event: &BytesEnd, kml: &mut dc_kml_t) {
fn kml_starttag_cb<B: std::io::BufRead>( fn kml_starttag_cb<B: std::io::BufRead>(
event: &BytesStart, event: &BytesStart,
kml: &mut dc_kml_t, kml: &mut Kml,
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();
@@ -575,19 +513,14 @@ fn kml_starttag_cb<B: std::io::BufRead>(
.map(|a| String::from_utf8_lossy(a.key).trim().to_lowercase() == "addr") .map(|a| String::from_utf8_lossy(a.key).trim().to_lowercase() == "addr")
.unwrap_or_default() .unwrap_or_default()
}) { }) {
kml.addr = unsafe { kml.addr = addr.unwrap().unescape_and_decode_value(reader).ok();
addr.unwrap()
.unescape_and_decode_value(reader)
.unwrap_or_default()
.strdup()
};
} }
} else if tag == "placemark" { } else if tag == "placemark" {
kml.tag = 0x1; kml.tag = 0x1;
kml.curr.timestamp = 0; kml.curr.timestamp = 0;
kml.curr.latitude = 0 as libc::c_double; kml.curr.latitude = 0.0;
kml.curr.longitude = 0.0f64; kml.curr.longitude = 0.0;
kml.curr.accuracy = 0.0f64 kml.curr.accuracy = 0.0
} else if tag == "timestamp" && 0 != kml.tag & 0x1 { } else if tag == "timestamp" && 0 != kml.tag & 0x1 {
kml.tag = 0x1 | 0x2 kml.tag = 0x1 | 0x2
} else if tag == "when" && 0 != kml.tag & 0x2 { } else if tag == "when" && 0 != kml.tag & 0x2 {
@@ -611,12 +544,8 @@ fn kml_starttag_cb<B: std::io::BufRead>(
} }
} }
pub unsafe fn dc_kml_unref(kml: &mut dc_kml_t) {
free(kml.addr as *mut libc::c_void);
}
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: &Job) { pub fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: &Job) {
let now = time(); let now = time();
let mut continue_streaming: libc::c_int = 1; let mut continue_streaming: libc::c_int = 1;
info!( info!(
@@ -682,7 +611,7 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: &Jo
msg.hidden = true; msg.hidden = true;
msg.param.set_int(Param::Cmd, 9); msg.param.set_int(Param::Cmd, 9);
// TODO: handle cleanup on error // TODO: handle cleanup on error
chat::send_msg(context, chat_id as u32, &mut msg).unwrap(); unsafe { chat::send_msg(context, chat_id as u32, &mut msg).unwrap() };
} }
Ok(()) Ok(())
}, },
@@ -697,12 +626,12 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: &Jo
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED(context: &Context, job: &mut Job) { pub fn dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED(context: &Context, job: &mut Job) {
// this function is called when location-streaming _might_ have ended for a chat. // this function is called when location-streaming _might_ have ended for a chat.
// the function checks, if location-streaming is really ended; // the function checks, if location-streaming is really ended;
// if so, a device-message is added if not yet done. // if so, a device-message is added if not yet done.
let chat_id = (*job).foreign_id; let chat_id = job.foreign_id;
if let Ok((send_begin, send_until)) = context.sql.query_row( if let Ok((send_begin, send_until)) = context.sql.query_row(
"SELECT locations_send_begin, locations_send_until FROM chats WHERE id=?", "SELECT locations_send_begin, locations_send_until FROM chats WHERE id=?",
@@ -739,17 +668,15 @@ mod tests {
#[test] #[test]
fn test_dc_kml_parse() { fn test_dc_kml_parse() {
unsafe {
let context = dummy_context(); let context = dummy_context();
let xml = let xml =
b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"user@example.org\">\n<Placemark><Timestamp><when>2019-03-06T21:09:57Z</when></Timestamp><Point><coordinates accuracy=\"32.000000\">9.423110,53.790302</coordinates></Point></Placemark>\n<PlaceMARK>\n<Timestamp><WHEN > \n\t2018-12-13T22:11:12Z\t</WHEN></Timestamp><Point><coordinates aCCuracy=\"2.500000\"> 19.423110 \t , \n 63.790302\n </coordinates></Point></PlaceMARK>\n</Document>\n</kml>\x00" "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"user@example.org\">\n<Placemark><Timestamp><when>2019-03-06T21:09:57Z</when></Timestamp><Point><coordinates accuracy=\"32.000000\">9.423110,53.790302</coordinates></Point></Placemark>\n<PlaceMARK>\n<Timestamp><WHEN > \n\t2018-12-13T22:11:12Z\t</WHEN></Timestamp><Point><coordinates aCCuracy=\"2.500000\"> 19.423110 \t , \n 63.790302\n </coordinates></Point></PlaceMARK>\n</Document>\n</kml>";
as *const u8 as *const libc::c_char;
let mut kml = dc_kml_parse(&context.ctx, xml, strlen(xml)); let kml = dc_kml_parse(&context.ctx, &xml).expect("parsing failed");
assert!(!kml.addr.is_null()); assert!(kml.addr.is_some());
assert_eq!(as_str(kml.addr as *const libc::c_char), "user@example.org",); assert_eq!(kml.addr.as_ref().unwrap(), "user@example.org",);
let locations_ref = &kml.locations.as_ref().unwrap(); let locations_ref = &kml.locations.as_ref().unwrap();
assert_eq!(locations_ref.len(), 2); assert_eq!(locations_ref.len(), 2);
@@ -769,8 +696,5 @@ mod tests {
assert!(locations_ref[1].accuracy > 2.4f64); assert!(locations_ref[1].accuracy > 2.4f64);
assert!(locations_ref[1].accuracy < 2.6f64); assert!(locations_ref[1].accuracy < 2.6f64);
assert_eq!(locations_ref[1].timestamp, 1544739072); assert_eq!(locations_ref[1].timestamp, 1544739072);
dc_kml_unref(&mut kml);
}
} }
} }

View File

@@ -885,7 +885,6 @@ pub unsafe fn dc_mimefactory_render(factory: &mut dc_mimefactory_t) -> libc::c_i
.unwrap_or_default(); .unwrap_or_default();
let kml_file = let kml_file =
dc_get_message_kml(factory.msg.timestamp_sort, latitude, longitude); dc_get_message_kml(factory.msg.timestamp_sort, latitude, longitude);
if !kml_file.is_null() {
let content_type = mailmime_content_new_with_str( let content_type = mailmime_content_new_with_str(
b"application/vnd.google-earth.kml+xml\x00" as *const u8 b"application/vnd.google-earth.kml+xml\x00" as *const u8
as *const libc::c_char, as *const libc::c_char,
@@ -896,32 +895,29 @@ pub unsafe fn dc_mimefactory_render(factory: &mut dc_mimefactory_t) -> libc::c_i
MAILMIME_MECHANISM_8BIT as libc::c_int, MAILMIME_MECHANISM_8BIT as libc::c_int,
); );
let kml_mime_part = mailmime_new_empty(content_type, mime_fields); let kml_mime_part = mailmime_new_empty(content_type, mime_fields);
mailmime_set_body_text(kml_mime_part, kml_file, strlen(kml_file)); mailmime_set_body_text(kml_mime_part, kml_file.strdup(), kml_file.len());
mailmime_smart_add_part(message, kml_mime_part); mailmime_smart_add_part(message, kml_mime_part);
} }
}
if dc_is_sending_locations_to_chat(factory.msg.context, factory.msg.chat_id) { if dc_is_sending_locations_to_chat(factory.msg.context, factory.msg.chat_id) {
let mut last_added_location_id: uint32_t = 0 as uint32_t; if let Ok((kml_file, last_added_location_id)) =
let kml_file: *mut libc::c_char = dc_get_location_kml( dc_get_location_kml(factory.msg.context, factory.msg.chat_id)
factory.msg.context, {
factory.msg.chat_id, let content_type = mailmime_content_new_with_str(
&mut last_added_location_id,
);
if !kml_file.is_null() {
let content_type: *mut mailmime_content = mailmime_content_new_with_str(
b"application/vnd.google-earth.kml+xml\x00" as *const u8 b"application/vnd.google-earth.kml+xml\x00" as *const u8
as *const libc::c_char, as *const libc::c_char,
); );
let mime_fields: *mut mailmime_fields = mailmime_fields_new_filename( let mime_fields = mailmime_fields_new_filename(
MAILMIME_DISPOSITION_TYPE_ATTACHMENT as libc::c_int, MAILMIME_DISPOSITION_TYPE_ATTACHMENT as libc::c_int,
dc_strdup(b"location.kml\x00" as *const u8 as *const libc::c_char), dc_strdup(b"location.kml\x00" as *const u8 as *const libc::c_char),
MAILMIME_MECHANISM_8BIT as libc::c_int, MAILMIME_MECHANISM_8BIT as libc::c_int,
); );
let kml_mime_part: *mut mailmime = let kml_mime_part = mailmime_new_empty(content_type, mime_fields);
mailmime_new_empty(content_type, mime_fields); mailmime_set_body_text(
mailmime_set_body_text(kml_mime_part, kml_file, strlen(kml_file)); kml_mime_part,
kml_file.strdup(),
kml_file.len(),
);
mailmime_smart_add_part(message, kml_mime_part); mailmime_smart_add_part(message, kml_mime_part);
if !factory.msg.param.exists(Param::SetLatitude) { if !factory.msg.param.exists(Param::SetLatitude) {
// otherwise, the independent location is already filed // otherwise, the independent location is already filed

View File

@@ -57,8 +57,8 @@ pub struct dc_mimeparser_t<'a> {
pub context: &'a Context, pub context: &'a Context,
pub reports: Vec<*mut mailmime>, pub reports: Vec<*mut mailmime>,
pub is_system_message: libc::c_int, pub is_system_message: libc::c_int,
pub location_kml: Option<dc_kml_t>, pub location_kml: Option<Kml>,
pub message_kml: Option<dc_kml_t>, pub message_kml: Option<Kml>,
} }
// deprecated: flag to switch generation of compound messages on and off. // deprecated: flag to switch generation of compound messages on and off.
@@ -112,14 +112,7 @@ unsafe fn dc_mimeparser_empty(mimeparser: &mut dc_mimeparser_t) {
mimeparser.decrypting_failed = 0i32; mimeparser.decrypting_failed = 0i32;
dc_e2ee_thanks(&mut mimeparser.e2ee_helper); dc_e2ee_thanks(&mut mimeparser.e2ee_helper);
if let Some(location_kml) = mimeparser.location_kml.as_mut() {
dc_kml_unref(location_kml);
}
mimeparser.location_kml = None; mimeparser.location_kml = None;
if let Some(message_kml) = mimeparser.message_kml.as_mut() {
dc_kml_unref(message_kml);
}
mimeparser.message_kml = None; mimeparser.message_kml = None;
} }
@@ -1300,11 +1293,13 @@ unsafe fn dc_mimeparser_add_single_part_if_known(
4, 4,
) == 0i32 ) == 0i32
{ {
mimeparser.location_kml = Some(dc_kml_parse( if !decoded_data.is_null() && decoded_data_bytes > 0 {
mimeparser.context, let d =
decoded_data, dc_null_terminate(decoded_data, decoded_data_bytes as i32);
decoded_data_bytes, mimeparser.location_kml =
)); dc_kml_parse(mimeparser.context, as_str(d)).ok();
free(d.cast());
}
} else if strncmp( } else if strncmp(
desired_filename, desired_filename,
b"message\x00" as *const u8 as *const libc::c_char, b"message\x00" as *const u8 as *const libc::c_char,
@@ -1318,11 +1313,13 @@ unsafe fn dc_mimeparser_add_single_part_if_known(
4, 4,
) == 0i32 ) == 0i32
{ {
mimeparser.message_kml = Some(dc_kml_parse( if !decoded_data.is_null() && decoded_data_bytes > 0 {
mimeparser.context, let d =
decoded_data, dc_null_terminate(decoded_data, decoded_data_bytes as i32);
decoded_data_bytes, mimeparser.message_kml =
)); dc_kml_parse(mimeparser.context, as_str(d)).ok();
free(d.cast());
}
} else { } else {
dc_replace_bad_utf8_chars(desired_filename); dc_replace_bad_utf8_chars(desired_filename);
do_add_single_file_part( do_add_single_file_part(

View File

@@ -953,11 +953,10 @@ unsafe fn save_locations(
} }
if !mime_parser.location_kml.is_none() && chat_id > DC_CHAT_ID_LAST_SPECIAL as libc::c_uint { if !mime_parser.location_kml.is_none() && chat_id > DC_CHAT_ID_LAST_SPECIAL as libc::c_uint {
if !mime_parser.location_kml.as_ref().unwrap().addr.is_null() { if let Some(ref addr) = mime_parser.location_kml.as_ref().unwrap().addr {
if let Ok(contact) = Contact::get_by_id(context, from_id) { if let Ok(contact) = Contact::get_by_id(context, from_id) {
if !contact.get_addr().is_empty() if !contact.get_addr().is_empty()
&& contact.get_addr().to_lowercase() && contact.get_addr().to_lowercase() == addr.to_lowercase()
== as_str(mime_parser.location_kml.as_ref().unwrap().addr).to_lowercase()
{ {
let newest_location_id = dc_save_locations( let newest_location_id = dc_save_locations(
context, context,

View File

@@ -875,12 +875,10 @@ fn job_perform(context: &Context, thread: Thread, probe_network: bool) {
Action::SendMdn => job.do_DC_JOB_SEND(context), Action::SendMdn => job.do_DC_JOB_SEND(context),
Action::ConfigureImap => unsafe { dc_job_do_DC_JOB_CONFIGURE_IMAP(context, &job) }, Action::ConfigureImap => unsafe { dc_job_do_DC_JOB_CONFIGURE_IMAP(context, &job) },
Action::ImexImap => unsafe { dc_job_do_DC_JOB_IMEX_IMAP(context, &job) }, Action::ImexImap => unsafe { dc_job_do_DC_JOB_IMEX_IMAP(context, &job) },
Action::MaybeSendLocations => unsafe { Action::MaybeSendLocations => dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context, &job),
dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context, &job) Action::MaybeSendLocationsEnded => {
},
Action::MaybeSendLocationsEnded => unsafe {
dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED(context, &mut job) dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED(context, &mut job)
}, }
Action::Housekeeping => sql::housekeeping(context), Action::Housekeeping => sql::housekeeping(context),
Action::SendMdnOld => {} Action::SendMdnOld => {}
Action::SendMsgToSmtpOld => {} Action::SendMsgToSmtpOld => {}