Remove C pointer manipulation from do_DC_JOB_SEND()

This change removes several `unsafe' blocks by using safe version of
`dc_read_file' function.

Note, that logic is changed slightly: if get(Param::File) returns
Some(""), it no longer triggers "missing filename warnings".
This commit is contained in:
Dmitry Bogatov
2019-09-04 07:07:18 +00:00
committed by holger krekel
parent 46520edd87
commit 0391aebaeb

View File

@@ -112,9 +112,6 @@ impl Job {
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn do_DC_JOB_SEND(&mut self, context: &Context) { fn do_DC_JOB_SEND(&mut self, context: &Context) {
let ok_to_continue; let ok_to_continue;
let mut filename = ptr::null_mut();
let mut buf = ptr::null_mut();
let mut buf_bytes = 0;
/* connect to SMTP server, if not yet done */ /* connect to SMTP server, if not yet done */
if !context.smtp.lock().unwrap().is_connected() { if !context.smtp.lock().unwrap().is_connected() {
@@ -131,88 +128,79 @@ impl Job {
ok_to_continue = true; ok_to_continue = true;
} }
if ok_to_continue { if ok_to_continue {
let filename_s = self.param.get(Param::File).unwrap_or_default(); if let Some(filename) = self.param.get(Param::File) {
filename = unsafe { filename_s.strdup() }; if let Some(body) = dc_read_file_safe(context, filename) {
if unsafe { strlen(filename) } == 0 { if let Some(recipients) = self.param.get(Param::Recipients) {
warn!(context, 0, "Missing file name for job {}", self.job_id,); let recipients_list = recipients
} else if 0 != unsafe { dc_read_file(context, filename, &mut buf, &mut buf_bytes) } { .split("\x1e")
if let Some(recipients) = self.param.get(Param::Recipients) { .filter_map(|addr| match lettre::EmailAddress::new(addr.to_string()) {
let recipients_list = recipients Ok(addr) => Some(addr),
.split("\x1e") Err(err) => {
.filter_map(|addr| match lettre::EmailAddress::new(addr.to_string()) { eprintln!("WARNING: invalid recipient: {} {:?}", addr, err);
Ok(addr) => Some(addr), None
Err(err) => { }
eprintln!("WARNING: invalid recipient: {} {:?}", addr, err); })
None .collect::<Vec<_>>();
/* if there is a msg-id and it does not exist in the db, cancel sending.
this happends if dc_delete_msgs() was called
before the generated mime was sent out */
let ok_to_continue1;
if 0 != self.foreign_id {
if 0 == unsafe { dc_msg_exists(context, self.foreign_id) } {
warn!(
context,
0,
"Message {} for job {} does not exist",
self.foreign_id,
self.job_id,
);
ok_to_continue1 = false;
} else {
ok_to_continue1 = true;
} }
})
.collect::<Vec<_>>();
/* if there is a msg-id and it does not exist in the db, cancel sending.
this happends if dc_delete_msgs() was called
before the generated mime was sent out */
let ok_to_continue1;
if 0 != self.foreign_id {
if 0 == unsafe { dc_msg_exists(context, self.foreign_id) } {
warn!(
context,
0,
"Message {} for job {} does not exist",
self.foreign_id,
self.job_id,
);
ok_to_continue1 = false;
} else { } else {
ok_to_continue1 = true; ok_to_continue1 = true;
} }
} else { if ok_to_continue1 {
ok_to_continue1 = true; // hold the smtp lock during sending of a job and
} // its ok/error response processing. Note that if a message
if ok_to_continue1 { // was sent we need to mark it in the database as we
/* send message */ // otherwise might send it twice.
let body = unsafe { let mut sock = context.smtp.lock().unwrap();
std::slice::from_raw_parts(buf as *const u8, buf_bytes).to_vec() if 0 == sock.send(context, recipients_list, body) {
}; sock.disconnect();
self.try_again_later(-1i32, Some(as_str(sock.error)));
// hold the smtp lock during sending of a job and } else {
// its ok/error response processing. Note that if a message dc_delete_file(context, filename);
// was sent we need to mark it in the database as we if 0 != self.foreign_id {
// otherwise might send it twice. dc_update_msg_state(
let mut sock = context.smtp.lock().unwrap();
if 0 == sock.send(context, recipients_list, body) {
sock.disconnect();
self.try_again_later(-1i32, Some(as_str(sock.error)));
} else {
dc_delete_file(context, filename_s);
if 0 != self.foreign_id {
dc_update_msg_state(
context,
self.foreign_id,
MessageState::OutDelivered,
);
let chat_id: i32 = context
.sql
.query_row_col(
context, context,
"SELECT chat_id FROM msgs WHERE id=?", self.foreign_id,
params![self.foreign_id as i32], MessageState::OutDelivered,
0, );
) let chat_id: i32 = context
.unwrap_or_default(); .sql
context.call_cb( .query_row_col(
Event::MSG_DELIVERED, context,
chat_id as uintptr_t, "SELECT chat_id FROM msgs WHERE id=?",
self.foreign_id as uintptr_t, params![self.foreign_id as i32],
); 0,
)
.unwrap_or_default();
context.call_cb(
Event::MSG_DELIVERED,
chat_id as uintptr_t,
self.foreign_id as uintptr_t,
);
}
} }
} }
} else {
warn!(context, 0, "Missing recipients for job {}", self.job_id,);
} }
} else {
warn!(context, 0, "Missing recipients for job {}", self.job_id,);
} }
} }
} }
unsafe { free(buf) };
unsafe { free(filename.cast()) };
} }
// this value does not increase the number of tries // this value does not increase the number of tries