From 98d6bdb48a725dc391144645ce047c14fceb5174 Mon Sep 17 00:00:00 2001 From: Dmitry Bogatov Date: Wed, 4 Sep 2019 14:10:00 +0000 Subject: [PATCH] Simplify control flow in dc_DC_JOB_SEND function Replace `ok_to_continue' control flow variables with early return from function, since there is no longer need to free memory manually. --- src/job.rs | 135 ++++++++++++++++++++++++----------------------------- 1 file changed, 60 insertions(+), 75 deletions(-) diff --git a/src/job.rs b/src/job.rs index d2b22eb12..2b95df117 100644 --- a/src/job.rs +++ b/src/job.rs @@ -111,8 +111,6 @@ impl Job { #[allow(non_snake_case)] fn do_DC_JOB_SEND(&mut self, context: &Context) { - let ok_to_continue; - /* connect to SMTP server, if not yet done */ if !context.smtp.lock().unwrap().is_connected() { let loginparam = dc_loginparam_read(context, &context.sql, "configured_"); @@ -120,84 +118,71 @@ impl Job { if !connected { self.try_again_later(3i32, None); - ok_to_continue = false; - } else { - ok_to_continue = true; + return; } - } else { - ok_to_continue = true; } - if ok_to_continue { - if let Some(filename) = self.param.get(Param::File) { - if let Some(body) = dc_read_file_safe(context, filename) { - if let Some(recipients) = self.param.get(Param::Recipients) { - let recipients_list = recipients - .split("\x1e") - .filter_map(|addr| match lettre::EmailAddress::new(addr.to_string()) { - Ok(addr) => Some(addr), - Err(err) => { - eprintln!("WARNING: invalid recipient: {} {:?}", addr, err); - None - } - }) - .collect::>(); - /* 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; + + if let Some(filename) = self.param.get(Param::File) { + if let Some(body) = dc_read_file_safe(context, filename) { + if let Some(recipients) = self.param.get(Param::Recipients) { + let recipients_list = recipients + .split("\x1e") + .filter_map(|addr| match lettre::EmailAddress::new(addr.to_string()) { + Ok(addr) => Some(addr), + Err(err) => { + eprintln!("WARNING: invalid recipient: {} {:?}", addr, err); + None } - } else { - ok_to_continue1 = true; - } - if ok_to_continue1 { - // hold the smtp lock during sending of a job and - // its ok/error response processing. Note that if a message - // was sent we need to mark it in the database as we - // otherwise might send it twice. - 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); - 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, - "SELECT chat_id FROM msgs WHERE id=?", - 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, - ); - } - } - } + }) + .collect::>(); + + /* 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 */ + if 0 != self.foreign_id + && 0 == unsafe { dc_msg_exists(context, self.foreign_id) } + { + warn!( + context, + 0, "Message {} for job {} does not exist", self.foreign_id, self.job_id, + ); + return; + }; + + // hold the smtp lock during sending of a job and + // its ok/error response processing. Note that if a message + // was sent we need to mark it in the database as we + // otherwise might send it twice. + 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 { - warn!(context, 0, "Missing recipients for job {}", self.job_id,); + dc_delete_file(context, filename); + 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, + "SELECT chat_id FROM msgs WHERE id=?", + 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,); } } }