mirror of
https://github.com/chatmail/core.git
synced 2026-05-02 04:46:29 +03:00
majorly de-indent code structure in secure_join by introducing cleanup function, also majorly reducing unsafety in several places
This commit is contained in:
@@ -854,7 +854,7 @@ pub fn unarchive(context: &Context, chat_id: u32) -> Result<(), Error> {
|
|||||||
/// However, this does not imply, the message really reached the recipient -
|
/// However, this does not imply, the message really reached the recipient -
|
||||||
/// sending may be delayed eg. due to network problems. However, from your
|
/// sending may be delayed eg. due to network problems. However, from your
|
||||||
/// view, you're done with the message. Sooner or later it will find its way.
|
/// view, you're done with the message. Sooner or later it will find its way.
|
||||||
pub unsafe fn send_msg<'a>(
|
pub fn send_msg<'a>(
|
||||||
context: &'a Context,
|
context: &'a Context,
|
||||||
chat_id: u32,
|
chat_id: u32,
|
||||||
msg: &mut Message<'a>,
|
msg: &mut Message<'a>,
|
||||||
@@ -872,7 +872,7 @@ pub unsafe fn send_msg<'a>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ensure!(
|
ensure!(
|
||||||
job_send_msg(context, msg.id) != 0,
|
unsafe { job_send_msg(context, msg.id) } != 0,
|
||||||
"Failed to initiate send job"
|
"Failed to initiate send job"
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -1394,7 +1394,7 @@ pub unsafe fn add_contact_to_chat(context: &Context, chat_id: u32, contact_id: u
|
|||||||
|
|
||||||
// TODO should return bool /rtn
|
// TODO should return bool /rtn
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
pub unsafe fn add_contact_to_chat_ex(
|
pub fn add_contact_to_chat_ex(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
chat_id: u32,
|
chat_id: u32,
|
||||||
contact_id: u32,
|
contact_id: u32,
|
||||||
@@ -1407,7 +1407,7 @@ pub unsafe fn add_contact_to_chat_ex(
|
|||||||
if contact.is_err() || chat_id <= DC_CHAT_ID_LAST_SPECIAL {
|
if contact.is_err() || chat_id <= DC_CHAT_ID_LAST_SPECIAL {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
let mut msg = dc_msg_new_untyped(context);
|
let mut msg = unsafe { dc_msg_new_untyped(context) };
|
||||||
|
|
||||||
reset_gossiped_timestamp(context, chat_id);
|
reset_gossiped_timestamp(context, chat_id);
|
||||||
let contact = contact.unwrap();
|
let contact = contact.unwrap();
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ macro_rules! progress {
|
|||||||
|
|
||||||
// connect
|
// connect
|
||||||
pub unsafe fn configure(context: &Context) {
|
pub unsafe fn configure(context: &Context) {
|
||||||
if 0 != dc_has_ongoing(context) {
|
if dc_has_ongoing(context) {
|
||||||
warn!(
|
warn!(
|
||||||
context,
|
context,
|
||||||
0, "There is already another ongoing process running.",
|
0, "There is already another ongoing process running.",
|
||||||
@@ -43,6 +43,7 @@ pub unsafe fn configure(context: &Context) {
|
|||||||
job_kill_action(context, Action::ConfigureImap);
|
job_kill_action(context, Action::ConfigureImap);
|
||||||
job_add(context, Action::ConfigureImap, 0, Params::new(), 0);
|
job_add(context, Action::ConfigureImap, 0, Params::new(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if the context is already configured.
|
/// Check if the context is already configured.
|
||||||
pub fn dc_is_configured(context: &Context) -> libc::c_int {
|
pub fn dc_is_configured(context: &Context) -> libc::c_int {
|
||||||
if context
|
if context
|
||||||
@@ -56,17 +57,6 @@ pub fn dc_is_configured(context: &Context) -> libc::c_int {
|
|||||||
0
|
0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Check if there is an ongoing process.
|
|
||||||
unsafe fn dc_has_ongoing(context: &Context) -> libc::c_int {
|
|
||||||
let s_a = context.running_state.clone();
|
|
||||||
let s = s_a.read().unwrap();
|
|
||||||
|
|
||||||
if s.ongoing_running || !s.shall_stop_ongoing {
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Configure JOB
|
* Configure JOB
|
||||||
@@ -80,7 +70,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: &Job) {
|
|||||||
let mut ongoing_allocated_here = false;
|
let mut ongoing_allocated_here = false;
|
||||||
|
|
||||||
let mut param_autoconfig: Option<dc_loginparam_t> = None;
|
let mut param_autoconfig: Option<dc_loginparam_t> = None;
|
||||||
if !(0 == dc_alloc_ongoing(context)) {
|
if dc_alloc_ongoing(context) {
|
||||||
ongoing_allocated_here = true;
|
ongoing_allocated_here = true;
|
||||||
if !context.sql.is_open() {
|
if !context.sql.is_open() {
|
||||||
error!(context, 0, "Cannot configure, database not opened.",);
|
error!(context, 0, "Cannot configure, database not opened.",);
|
||||||
@@ -591,7 +581,45 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: &Job) {
|
|||||||
progress!(context, (if success { 1000 } else { 0 }));
|
progress!(context, (if success { 1000 } else { 0 }));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* File Structure like in C: */
|
/*******************************************************************************
|
||||||
|
* Ongoing process allocation/free/check
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
pub fn dc_alloc_ongoing(context: &Context) -> bool {
|
||||||
|
if dc_has_ongoing(context) {
|
||||||
|
warn!(
|
||||||
|
context,
|
||||||
|
0, "There is already another ongoing process running.",
|
||||||
|
);
|
||||||
|
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
let s_a = context.running_state.clone();
|
||||||
|
let mut s = s_a.write().unwrap();
|
||||||
|
|
||||||
|
s.ongoing_running = true;
|
||||||
|
s.shall_stop_ongoing = false;
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dc_free_ongoing(context: &Context) {
|
||||||
|
let s_a = context.running_state.clone();
|
||||||
|
let mut s = s_a.write().unwrap();
|
||||||
|
|
||||||
|
s.ongoing_running = false;
|
||||||
|
s.shall_stop_ongoing = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn dc_has_ongoing(context: &Context) -> bool {
|
||||||
|
let s_a = context.running_state.clone();
|
||||||
|
let s = s_a.read().unwrap();
|
||||||
|
|
||||||
|
s.ongoing_running || !s.shall_stop_ongoing
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Connect to configured account
|
* Connect to configured account
|
||||||
@@ -624,35 +652,6 @@ pub fn dc_connect_to_configured_imap(context: &Context, imap: &Imap) -> libc::c_
|
|||||||
* Configure a Context
|
* Configure a Context
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
/// Request an ongoing process to start.
|
|
||||||
/// Returns 0=process started, 1=not started, there is running another process
|
|
||||||
pub unsafe fn dc_alloc_ongoing(context: &Context) -> libc::c_int {
|
|
||||||
if 0 != dc_has_ongoing(context) {
|
|
||||||
warn!(
|
|
||||||
context,
|
|
||||||
0, "There is already another ongoing process running.",
|
|
||||||
);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
let s_a = context.running_state.clone();
|
|
||||||
let mut s = s_a.write().unwrap();
|
|
||||||
|
|
||||||
s.ongoing_running = true;
|
|
||||||
s.shall_stop_ongoing = false;
|
|
||||||
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Frees the process allocated with dc_alloc_ongoing() - independently of dc_shall_stop_ongoing.
|
|
||||||
/// If dc_alloc_ongoing() fails, this function MUST NOT be called.
|
|
||||||
pub unsafe fn dc_free_ongoing(context: &Context) {
|
|
||||||
let s_a = context.running_state.clone();
|
|
||||||
let mut s = s_a.write().unwrap();
|
|
||||||
|
|
||||||
s.ongoing_running = false;
|
|
||||||
s.shall_stop_ongoing = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Signal an ongoing process to stop.
|
/// Signal an ongoing process to stop.
|
||||||
pub fn dc_stop_ongoing_process(context: &Context) {
|
pub fn dc_stop_ongoing_process(context: &Context) {
|
||||||
let s_a = context.running_state.clone();
|
let s_a = context.running_state.clone();
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ pub unsafe fn dc_imex_has_backup(
|
|||||||
pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
|
pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
|
||||||
let mut setup_file_name: *mut libc::c_char = ptr::null_mut();
|
let mut setup_file_name: *mut libc::c_char = ptr::null_mut();
|
||||||
let mut msg: Message;
|
let mut msg: Message;
|
||||||
if dc_alloc_ongoing(context) == 0 {
|
if !dc_alloc_ongoing(context) {
|
||||||
return std::ptr::null_mut();
|
return std::ptr::null_mut();
|
||||||
}
|
}
|
||||||
let setup_code = dc_create_setup_code(context);
|
let setup_code = dc_create_setup_code(context);
|
||||||
@@ -502,11 +502,9 @@ pub unsafe fn dc_normalize_setup_code(
|
|||||||
pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: &Job) {
|
pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: &Job) {
|
||||||
let mut ok_to_continue = true;
|
let mut ok_to_continue = true;
|
||||||
let mut success: libc::c_int = 0;
|
let mut success: libc::c_int = 0;
|
||||||
let mut ongoing_allocated_here: libc::c_int = 0;
|
|
||||||
let what: libc::c_int;
|
let what: libc::c_int;
|
||||||
|
|
||||||
if !(0 == dc_alloc_ongoing(context)) {
|
if dc_alloc_ongoing(context) {
|
||||||
ongoing_allocated_here = 1;
|
|
||||||
what = job.param.get_int(Param::Cmd).unwrap_or_default();
|
what = job.param.get_int(Param::Cmd).unwrap_or_default();
|
||||||
let param1_s = job.param.get(Param::Arg).unwrap_or_default();
|
let param1_s = job.param.get(Param::Arg).unwrap_or_default();
|
||||||
let param1 = CString::yolo(param1_s);
|
let param1 = CString::yolo(param1_s);
|
||||||
@@ -564,9 +562,6 @@ pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: &Job) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if 0 != ongoing_allocated_here {
|
|
||||||
dc_free_ongoing(context);
|
dc_free_ongoing(context);
|
||||||
}
|
}
|
||||||
context.call_cb(
|
context.call_cb(
|
||||||
|
|||||||
1107
src/dc_securejoin.rs
1107
src/dc_securejoin.rs
File diff suppressed because it is too large
Load Diff
@@ -220,7 +220,7 @@ pub fn send_locations_to_chat(context: &Context, chat_id: u32, seconds: i64) {
|
|||||||
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);
|
||||||
unsafe { chat::send_msg(context, chat_id, &mut msg).unwrap() };
|
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);
|
||||||
@@ -607,7 +607,7 @@ pub fn job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: &Job) {
|
|||||||
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
|
||||||
unsafe { chat::send_msg(context, chat_id as u32, &mut msg).unwrap() };
|
chat::send_msg(context, chat_id as u32, &mut msg).unwrap();
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user