From 9227c4b55c3d8011539d1a98c175c081e3f8eea7 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Tue, 20 Aug 2019 11:15:28 +0200 Subject: [PATCH] - simplify inbox-connection-guards and returns - make dc_msg_exists safe --- src/chat.rs | 2 +- src/constants.rs | 2 +- src/job.rs | 56 +++++++++++++++++++++--------------------------- src/message.rs | 29 +++++++++++-------------- 4 files changed, 40 insertions(+), 49 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 105a215e7..57bad0b68 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1031,7 +1031,7 @@ pub fn get_chat_msgs(context: &Context, chat_id: u32, flags: u32, marker1before: let curr_local_timestamp = ts + cnv_to_local; let curr_day = (curr_local_timestamp / 86400) as libc::c_int; if curr_day != last_day { - ret.push(DC_MSG_ID_LAST_SPECIAL as u32); + ret.push(DC_MSG_ID_LAST_SPECIAL); last_day = curr_day; } } diff --git a/src/constants.rs b/src/constants.rs index 5c0ed53ae..a98039f52 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -108,7 +108,7 @@ impl Default for Chattype { pub const DC_MSG_ID_MARKER1: usize = 1; const DC_MSG_ID_DAYMARKER: usize = 9; -pub const DC_MSG_ID_LAST_SPECIAL: usize = 9; +pub const DC_MSG_ID_LAST_SPECIAL: u32 = 9; /// approx. max. length returned by dc_msg_get_text() const DC_MAX_GET_TEXT_LEN: usize = 30000; diff --git a/src/job.rs b/src/job.rs index 35072afaf..c607f0d9b 100644 --- a/src/job.rs +++ b/src/job.rs @@ -149,7 +149,7 @@ impl Job { this happends if dc_delete_msgs() was called before the generated mime was sent out */ if 0 != self.foreign_id { - if 0 == unsafe { dc_msg_exists(context, self.foreign_id) } { + if !dc_msg_exists(context, self.foreign_id) { warn!( context, 0, "Message {} for job {} does not exist", self.foreign_id, self.job_id, @@ -197,12 +197,9 @@ impl Job { fn do_DC_JOB_MOVE_MSG(&mut self, context: &Context) { let inbox = context.inbox.read().unwrap(); - if !inbox.is_connected() { - connect_to_inbox(context, &inbox); - if !inbox.is_connected() { - self.try_again_later(Delay::Standard, None); - return; - } + if !connect_to_inbox(context, &inbox) { + self.try_again_later(Delay::Standard, None); + return; } if let Ok(msg) = dc_msg_load_from_db(context, self.foreign_id) { if context @@ -256,12 +253,9 @@ impl Job { return; } /* if this is the last existing part of the message, we delete the message from the server */ - if !inbox.is_connected() { - connect_to_inbox(context, &inbox); - if !inbox.is_connected() { - self.try_again_later(Delay::Standard, None); - return; - } + if !connect_to_inbox(context, &inbox) { + self.try_again_later(Delay::Standard, None); + return; } let mid = unsafe { CStr::from_ptr(msg.rfc724_mid).to_str().unwrap() }; let server_folder = msg.server_folder.as_ref().unwrap(); @@ -281,12 +275,9 @@ impl Job { fn do_DC_JOB_MARKSEEN_MSG_ON_IMAP(&mut self, context: &Context) { let inbox = context.inbox.read().unwrap(); - if !inbox.is_connected() { - connect_to_inbox(context, &inbox); - if !inbox.is_connected() { - self.try_again_later(Delay::Standard, None); - return; - } + if !connect_to_inbox(context, &inbox) { + self.try_again_later(Delay::Standard, None); + return; } if let Ok(msg) = dc_msg_load_from_db(context, self.foreign_id) { let server_folder = msg.server_folder.as_ref().unwrap(); @@ -333,12 +324,9 @@ impl Job { let mut dest_uid = 0; let inbox = context.inbox.read().unwrap(); - if !inbox.is_connected() { - connect_to_inbox(context, &inbox); - if !inbox.is_connected() { - self.try_again_later(Delay::Standard, None); - return; - } + if !connect_to_inbox(context, &inbox) { + self.try_again_later(Delay::Standard, None); + return; } match inbox.set_seen(context, &folder, uid) { @@ -388,7 +376,7 @@ pub fn perform_imap_fetch(context: &Context) { let inbox = context.inbox.read().unwrap(); let start = std::time::Instant::now(); - if 0 == connect_to_inbox(context, &inbox) { + if !connect_to_inbox(context, &inbox) { return; } if context @@ -937,12 +925,18 @@ fn suspend_smtp_thread(context: &Context, suspend: bool) { } } -fn connect_to_inbox(context: &Context, inbox: &Imap) -> libc::c_int { - let ret_connected = dc_connect_to_configured_imap(context, inbox); - if 0 != ret_connected { - inbox.set_watch_folder("INBOX".into()); +fn connect_to_inbox(context: &Context, inbox: &Imap) -> bool { + if inbox.is_connected() { + true + } else { + let ret_connected = dc_connect_to_configured_imap(context, inbox); + if 0 != ret_connected { + inbox.set_watch_folder("INBOX".into()); + true + } else { + false + } } - ret_connected } fn send_mdn(context: &Context, msg_id: uint32_t) { diff --git a/src/message.rs b/src/message.rs index 2bc9558c7..771612117 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1033,25 +1033,22 @@ The value is also used for CC:-summaries */ // Context functions to work with messages -pub unsafe fn dc_msg_exists(context: &Context, msg_id: u32) -> libc::c_int { - if msg_id <= 9 { - return 0; - } +pub fn dc_msg_exists(context: &Context, msg_id: u32) -> bool { + if msg_id > DC_MSG_ID_LAST_SPECIAL { + let chat_id: Option = context.sql.query_row_col( + context, + "SELECT chat_id FROM msgs WHERE id=?;", + params![msg_id as i32], + 0, + ); - let chat_id: Option = context.sql.query_row_col( - context, - "SELECT chat_id FROM msgs WHERE id=?;", - params![msg_id as i32], - 0, - ); - - if let Some(chat_id) = chat_id { - if chat_id != 3 { - return 1; + if let Some(chat_id) = chat_id { + if chat_id != 3 { + return true; + } } } - - 0 + false } pub fn dc_update_msg_move_state(