diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index 2930ca3b3..b92920657 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -597,10 +597,7 @@ unsafe extern "C" fn chat_prefix(mut chat: *const dc_chat_t) -> *const libc::c_c }; } #[no_mangle] -pub unsafe extern "C" fn dc_cmdline( - mut context: *mut dc_context_t, - cmdline: &str, -) -> *mut libc::c_char { +pub unsafe extern "C" fn dc_cmdline(context: &dc_context_t, cmdline: &str) -> *mut libc::c_char { let mut cmd: &libc::c_char = 0 as *mut libc::c_char; let mut arg1: *mut libc::c_char = 0 as *mut libc::c_char; let mut ret: *mut libc::c_char = 1i32 as *mut libc::c_char; diff --git a/src/dc_apeerstate.rs b/src/dc_apeerstate.rs index bc5bc84ef..ba20123b5 100644 --- a/src/dc_apeerstate.rs +++ b/src/dc_apeerstate.rs @@ -17,8 +17,8 @@ use crate::x::*; */ #[derive(Copy, Clone)] #[repr(C)] -pub struct dc_apeerstate_t { - pub context: &dc_context_t, +pub struct dc_apeerstate_t<'a> { + pub context: &'a dc_context_t, pub addr: *mut libc::c_char, pub last_seen: time_t, pub last_seen_autocrypt: time_t, @@ -35,13 +35,14 @@ pub struct dc_apeerstate_t { } /* the returned pointer is ref'd and must be unref'd after usage */ -pub unsafe fn dc_apeerstate_new(mut context: &dc_context_t) -> *mut dc_apeerstate_t { +pub unsafe fn dc_apeerstate_new<'a>(context: &'a dc_context_t) -> *mut dc_apeerstate_t<'a> { let mut peerstate: *mut dc_apeerstate_t = 0 as *mut dc_apeerstate_t; peerstate = calloc(1, ::std::mem::size_of::()) as *mut dc_apeerstate_t; if peerstate.is_null() { exit(43i32); } (*peerstate).context = context; + return peerstate; } pub unsafe fn dc_apeerstate_unref(mut peerstate: *mut dc_apeerstate_t) { @@ -311,12 +312,12 @@ pub unsafe fn dc_apeerstate_set_verified( } pub unsafe fn dc_apeerstate_load_by_addr( mut peerstate: *mut dc_apeerstate_t, - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, mut addr: *const libc::c_char, ) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(peerstate.is_null() || sql.is_null() || addr.is_null()) { + if !(peerstate.is_null() || addr.is_null()) { dc_apeerstate_empty(peerstate); stmt = dc_sqlite3_prepare(sql, @@ -361,12 +362,12 @@ unsafe fn dc_apeerstate_set_from_stmt( } pub unsafe fn dc_apeerstate_load_by_fingerprint( mut peerstate: *mut dc_apeerstate_t, - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, mut fingerprint: *const libc::c_char, ) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(peerstate.is_null() || sql.is_null() || fingerprint.is_null()) { + if !(peerstate.is_null() || fingerprint.is_null()) { dc_apeerstate_empty(peerstate); stmt = dc_sqlite3_prepare(sql, @@ -385,13 +386,13 @@ pub unsafe fn dc_apeerstate_load_by_fingerprint( } pub unsafe fn dc_apeerstate_save_to_db( mut peerstate: *const dc_apeerstate_t, - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, mut create: libc::c_int, ) -> libc::c_int { let mut current_block: u64; let mut success: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if peerstate.is_null() || sql.is_null() || (*peerstate).addr.is_null() { + if peerstate.is_null() || (*peerstate).addr.is_null() { return 0i32; } if 0 != create { diff --git a/src/dc_array.rs b/src/dc_array.rs index 560d6313b..4ce144bba 100644 --- a/src/dc_array.rs +++ b/src/dc_array.rs @@ -268,7 +268,7 @@ pub unsafe fn dc_array_new(initsize: size_t) -> *mut dc_array_t { pub unsafe extern "C" fn dc_array_new_typed( mut type_0: libc::c_int, mut initsize: size_t, -) -> &dc_array_t { +) -> *mut dc_array_t { let mut array: *mut dc_array_t = 0 as *mut dc_array_t; array = calloc(1, ::std::mem::size_of::()) as *mut dc_array_t; if array.is_null() { @@ -286,8 +286,9 @@ pub unsafe extern "C" fn dc_array_new_typed( if (*array).array.is_null() { exit(48i32); } - return array; + array } + pub unsafe fn dc_array_empty(mut array: *mut dc_array_t) { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint { return; diff --git a/src/dc_chat.rs b/src/dc_chat.rs index e838432e1..4041ddf5b 100644 --- a/src/dc_chat.rs +++ b/src/dc_chat.rs @@ -19,13 +19,13 @@ use crate::x::*; /* * the structure behind dc_chat_t */ #[derive(Copy, Clone)] #[repr(C)] -pub struct dc_chat_t { +pub struct dc_chat_t<'a> { pub magic: uint32_t, pub id: uint32_t, pub type_0: libc::c_int, pub name: *mut libc::c_char, pub archived: libc::c_int, - pub context: *mut dc_context_t, + pub context: &'a dc_context_t, pub grpid: *mut libc::c_char, pub blocked: libc::c_int, pub param: *mut dc_param_t, @@ -42,23 +42,22 @@ pub unsafe fn dc_create_chat_by_msg_id( let mut send_event: libc::c_int = 0i32; let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); let mut chat: *mut dc_chat_t = dc_chat_new(context); - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - if !(0 == dc_msg_load_from_db(msg, context, msg_id) - || 0 == dc_chat_load_from_db(chat, (*msg).chat_id) - || (*chat).id <= 9i32 as libc::c_uint) - { - chat_id = (*chat).id; - if 0 != (*chat).blocked { - dc_unblock_chat(context, (*chat).id); - send_event = 1i32 - } - dc_scaleup_contact_origin(context, (*msg).from_id, 0x800i32); + if !(0 == dc_msg_load_from_db(msg, context, msg_id) + || 0 == dc_chat_load_from_db(chat, (*msg).chat_id) + || (*chat).id <= 9i32 as libc::c_uint) + { + chat_id = (*chat).id; + if 0 != (*chat).blocked { + dc_unblock_chat(context, (*chat).id); + send_event = 1i32 } + dc_scaleup_contact_origin(context, (*msg).from_id, 0x800i32); } + dc_msg_unref(msg); dc_chat_unref(chat); if 0 != send_event { - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -82,14 +81,9 @@ pub unsafe fn dc_create_chat_by_msg_id( // only an indicator in a chatlist // only an indicator in a chatlist // larger chat IDs are "real" chats, their messages are "real" messages. -pub unsafe fn dc_chat_new(mut context: &dc_context_t) -> *mut dc_chat_t { +pub unsafe fn dc_chat_new<'a>(context: &'a dc_context_t) -> *mut dc_chat_t<'a> { let mut chat: *mut dc_chat_t = 0 as *mut dc_chat_t; - if context.is_null() || { - chat = calloc(1, ::std::mem::size_of::()) as *mut dc_chat_t; - chat.is_null() - } { - exit(14i32); - } + chat = calloc(1, ::std::mem::size_of::()) as *mut dc_chat_t; (*chat).magic = 0xc4a7c4a7u32; (*chat).context = context; (*chat).type_0 = 0i32; @@ -128,11 +122,8 @@ pub unsafe fn dc_block_chat( mut new_blocking: libc::c_int, ) { let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return; - } stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"UPDATE chats SET blocked=? WHERE id=?;\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_int(stmt, 1i32, new_blocking); @@ -146,9 +137,11 @@ pub unsafe fn dc_chat_load_from_db(mut chat: *mut dc_chat_t, mut chat_id: uint32 if !(chat.is_null() || (*chat).magic != 0xc4a7c4a7u32) { dc_chat_empty(chat); stmt = - dc_sqlite3_prepare((*(*chat).context).sql, - b"SELECT c.id,c.type,c.name, c.grpid,c.param,c.archived, c.blocked, c.gossiped_timestamp, c.locations_send_until FROM chats c WHERE c.id=?;\x00" - as *const u8 as *const libc::c_char); + dc_sqlite3_prepare( + &mut (*chat).context.sql.lock().unwrap(), + b"SELECT c.id,c.type,c.name, c.grpid,c.param,c.archived, c.blocked, c.gossiped_timestamp, c.locations_send_until FROM chats c WHERE c.id=?;\x00" + as *const u8 as *const libc::c_char + ); sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); if !(sqlite3_step(stmt) != 100i32) { if !(0 == set_from_stmt(chat, stmt)) { @@ -225,9 +218,6 @@ pub unsafe fn dc_create_chat_by_contact_id( let mut chat_id: uint32_t = 0i32 as uint32_t; let mut chat_blocked: libc::c_int = 0i32; let mut send_event: libc::c_int = 0i32; - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return 0i32 as uint32_t; - } dc_lookup_real_nchat_by_contact_id(context, contact_id, &mut chat_id, &mut chat_blocked); if 0 != chat_id { if 0 != chat_blocked { @@ -258,7 +248,7 @@ pub unsafe fn dc_create_chat_by_contact_id( dc_scaleup_contact_origin(context, contact_id, 0x800i32); } if 0 != send_event { - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -286,10 +276,7 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id( if !ret_chat_blocked.is_null() { *ret_chat_blocked = 0i32 } - if context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || (*(*context).sql).cobj.is_null() - { + if context.sql.lock().unwrap().cobj.is_null() { return; } if contact_id == 0i32 as libc::c_uint { @@ -306,7 +293,7 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id( return; } contact = dc_contact_new(context); - if !(0 == dc_contact_load_from_db(contact, (*context).sql, contact_id)) { + if !(0 == dc_contact_load_from_db(contact, &mut context.sql.lock().unwrap(), contact_id)) { chat_name = if !(*contact).name.is_null() && 0 != *(*contact).name.offset(0isize) as libc::c_int { (*contact).name @@ -338,11 +325,11 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id( ); } else { }; - stmt = dc_sqlite3_prepare((*context).sql, q); + stmt = dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), q); if !stmt.is_null() { if !(sqlite3_step(stmt) != 101i32) { chat_id = dc_sqlite3_get_rowid( - (*context).sql, + &mut context.sql.lock().unwrap(), b"chats\x00" as *const u8 as *const libc::c_char, b"grpid\x00" as *const u8 as *const libc::c_char, (*contact).addr, @@ -357,7 +344,7 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id( chat_id, contact_id, ); - stmt = dc_sqlite3_prepare((*context).sql, q); + stmt = dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), q); if !(sqlite3_step(stmt) != 101i32) { sqlite3_free(q as *mut libc::c_void); q = 0 as *mut libc::c_char; @@ -391,15 +378,13 @@ pub unsafe fn dc_lookup_real_nchat_by_contact_id( if !ret_chat_blocked.is_null() { *ret_chat_blocked = 0i32 } - if context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || (*(*context).sql).cobj.is_null() - { + if context.sql.lock().unwrap().cobj.is_null() { return; } stmt = - dc_sqlite3_prepare((*context).sql, - b"SELECT c.id, c.blocked FROM chats c INNER JOIN chats_contacts j ON c.id=j.chat_id WHERE c.type=100 AND c.id>9 AND j.contact_id=?;\x00" + dc_sqlite3_prepare( + &mut context.sql.lock().unwrap(), + b"SELECT c.id, c.blocked FROM chats c INNER JOIN chats_contacts j ON c.id=j.chat_id WHERE c.type=100 AND c.id>9 AND j.contact_id=?;\x00" as *const u8 as *const libc::c_char); sqlite3_bind_int(stmt, 1i32, contact_id as libc::c_int); if sqlite3_step(stmt) == 100i32 { @@ -418,9 +403,6 @@ pub unsafe fn dc_get_chat_id_by_contact_id( ) -> uint32_t { let mut chat_id: uint32_t = 0i32 as uint32_t; let mut chat_id_blocked: libc::c_int = 0i32; - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return 0i32 as uint32_t; - } dc_lookup_real_nchat_by_contact_id(context, contact_id, &mut chat_id, &mut chat_id_blocked); return if 0 != chat_id_blocked { 0i32 as libc::c_uint @@ -428,21 +410,17 @@ pub unsafe fn dc_get_chat_id_by_contact_id( chat_id }; } -pub unsafe fn dc_prepare_msg( - mut context: &dc_context_t, +pub unsafe fn dc_prepare_msg<'a>( + mut context: &'a dc_context_t, mut chat_id: uint32_t, - mut msg: *mut dc_msg_t, + mut msg: *mut dc_msg_t<'a>, ) -> uint32_t { - if context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || msg.is_null() - || chat_id <= 9i32 as libc::c_uint - { + if msg.is_null() || chat_id <= 9i32 as libc::c_uint { return 0i32 as uint32_t; } (*msg).state = 18i32; let mut msg_id: uint32_t = prepare_msg_common(context, chat_id, msg); - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, (*msg).chat_id as uintptr_t, @@ -450,10 +428,10 @@ pub unsafe fn dc_prepare_msg( ); return msg_id; } -unsafe fn prepare_msg_common( - mut context: &dc_context_t, +unsafe fn prepare_msg_common<'a>( + mut context: &'a dc_context_t, mut chat_id: uint32_t, - mut msg: *mut dc_msg_t, + mut msg: *mut dc_msg_t<'a>, ) -> uint32_t { let mut current_block: u64; let mut pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char; @@ -531,7 +509,7 @@ unsafe fn prepare_msg_common( match current_block { 17281240262373992796 => { dc_unarchive_chat(context, chat_id); - (*(*context).smtp).log_connect_errors = 1i32; + context.smtp.lock().unwrap().log_connect_errors = 1i32; chat = dc_chat_new(context); if 0 != dc_chat_load_from_db(chat, chat_id) { if (*msg).state != 18i32 { @@ -587,7 +565,7 @@ unsafe fn prepare_msg_raw( ); } else { let mut from: *mut libc::c_char = dc_sqlite3_get_config( - (*context).sql, + &mut context.sql.lock().unwrap(), b"configured_addr\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); @@ -609,7 +587,7 @@ unsafe fn prepare_msg_raw( free(from as *mut libc::c_void); if (*chat).type_0 == 100i32 { stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"SELECT contact_id FROM chats_contacts WHERE chat_id=?;\x00" as *const u8 as *const libc::c_char, ); @@ -647,7 +625,7 @@ unsafe fn prepare_msg_raw( we do not send the message out at all */ do_guarantee_e2ee = 0i32; e2ee_enabled = dc_sqlite3_get_config_int( - (*context).sql, + &mut context.sql.lock().unwrap(), b"e2ee_enabled\x00" as *const u8 as *const libc::c_char, 1i32, ); @@ -656,7 +634,7 @@ unsafe fn prepare_msg_raw( let mut can_encrypt: libc::c_int = 1i32; let mut all_mutual: libc::c_int = 1i32; stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), b"SELECT ps.prefer_encrypted, c.addr FROM chats_contacts cc LEFT JOIN contacts c ON cc.contact_id=c.id LEFT JOIN acpeerstates ps ON c.addr=ps.addr WHERE cc.chat_id=? AND cc.contact_id>9;\x00" as *const u8 as *const libc::c_char); @@ -697,7 +675,12 @@ unsafe fn prepare_msg_raw( if 0 != can_encrypt { if 0 != all_mutual { do_guarantee_e2ee = 1i32 - } else if 0 != last_msg_in_chat_encrypted((*context).sql, (*chat).id) { + } else if 0 + != last_msg_in_chat_encrypted( + &mut context.sql.lock().unwrap(), + (*chat).id, + ) + { do_guarantee_e2ee = 1i32 } } @@ -761,7 +744,7 @@ unsafe fn prepare_msg_raw( if 0 != dc_param_exists((*msg).param, DC_PARAM_SET_LATITUDE as libc::c_int) { stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"INSERT INTO locations \ (timestamp,from_id,chat_id, latitude,longitude,independent)\ VALUES (?,?,?, ?,?,1);\x00" as *const u8 @@ -793,7 +776,7 @@ unsafe fn prepare_msg_raw( stmt = 0 as *mut sqlite3_stmt; location_id = dc_sqlite3_get_rowid2( - (*context).sql, + &mut context.sql.lock().unwrap(), b"locations\x00" as *const u8 as *const libc::c_char, b"timestamp\x00" as *const u8 as *const libc::c_char, timestamp as u64, @@ -805,7 +788,7 @@ unsafe fn prepare_msg_raw( // add message to the database stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), b"INSERT INTO msgs (rfc724_mid, chat_id, from_id, to_id, timestamp, type, state, txt, param, hidden, mime_in_reply_to, mime_references, location_id) VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?);\x00" as *const u8 as *const libc::c_char); @@ -842,7 +825,7 @@ unsafe fn prepare_msg_raw( ); } else { msg_id = dc_sqlite3_get_rowid( - (*context).sql, + &mut context.sql.lock().unwrap(), b"msgs\x00" as *const u8 as *const libc::c_char, b"rfc724_mid\x00" as *const u8 as *const libc::c_char, new_rfc724_mid, @@ -875,7 +858,7 @@ unsafe fn get_parent_mime_headers( || parent_references.is_null()) { stmt = - dc_sqlite3_prepare((*(*chat).context).sql, + dc_sqlite3_prepare(&mut (*chat).context.sql.lock().unwrap(), b"SELECT rfc724_mid, mime_in_reply_to, mime_references FROM msgs WHERE timestamp=(SELECT max(timestamp) FROM msgs WHERE chat_id=? AND from_id!=?);\x00" as *const u8 as *const libc::c_char); sqlite3_bind_int(stmt, 1i32, (*chat).id as libc::c_int); @@ -890,7 +873,7 @@ unsafe fn get_parent_mime_headers( stmt = 0 as *mut sqlite3_stmt; if 0 == success { stmt = - dc_sqlite3_prepare((*(*chat).context).sql, + dc_sqlite3_prepare(&mut (*chat).context.sql.lock().unwrap(), b"SELECT rfc724_mid, mime_in_reply_to, mime_references FROM msgs WHERE timestamp=(SELECT min(timestamp) FROM msgs WHERE chat_id=? AND from_id==?);\x00" as *const u8 as *const libc::c_char); sqlite3_bind_int(stmt, 1i32, (*chat).id as libc::c_int); @@ -919,7 +902,7 @@ pub unsafe fn dc_chat_is_self_talk(mut chat: *const dc_chat_t) -> libc::c_int { * Sending messages ******************************************************************************/ unsafe fn last_msg_in_chat_encrypted( - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, mut chat_id: uint32_t, ) -> libc::c_int { let mut last_is_encrypted: libc::c_int = 0i32; @@ -945,7 +928,7 @@ unsafe fn last_msg_in_chat_encrypted( pub unsafe fn dc_chat_update_param(mut chat: *mut dc_chat_t) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*(*chat).context).sql, + &mut (*chat).context.sql.lock().unwrap(), b"UPDATE chats SET param=? WHERE id=?\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_text(stmt, 1i32, (*(*chat).param).packed, -1i32, None); @@ -967,38 +950,39 @@ pub unsafe fn dc_is_contact_in_chat( DC_CONTACT_ID_SELF may be used to check, if the user itself is in a group chat (DC_CONTACT_ID_SELF is not added to normal chats) */ let mut ret: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - stmt = dc_sqlite3_prepare( - (*context).sql, - b"SELECT contact_id FROM chats_contacts WHERE chat_id=? AND contact_id=?;\x00" - as *const u8 as *const libc::c_char, - ); - sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); - sqlite3_bind_int(stmt, 2i32, contact_id as libc::c_int); - ret = if sqlite3_step(stmt) == 100i32 { - 1i32 - } else { - 0i32 - } - } + + stmt = dc_sqlite3_prepare( + &mut context.sql.lock().unwrap(), + b"SELECT contact_id FROM chats_contacts WHERE chat_id=? AND contact_id=?;\x00" as *const u8 + as *const libc::c_char, + ); + sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); + sqlite3_bind_int(stmt, 2i32, contact_id as libc::c_int); + ret = if sqlite3_step(stmt) == 100i32 { + 1i32 + } else { + 0i32 + }; + sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_unarchive_chat(mut context: &dc_context_t, mut chat_id: uint32_t) { let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"UPDATE chats SET archived=0 WHERE id=?\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); sqlite3_step(stmt); sqlite3_finalize(stmt); } -pub unsafe fn dc_send_msg( - mut context: &dc_context_t, +pub unsafe fn dc_send_msg<'a>( + mut context: &'a dc_context_t, mut chat_id: uint32_t, - mut msg: *mut dc_msg_t, + mut msg: *mut dc_msg_t<'a>, ) -> uint32_t { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint || msg.is_null() { + if msg.is_null() { return 0i32 as uint32_t; } if (*msg).state != 18i32 { @@ -1014,7 +998,7 @@ pub unsafe fn dc_send_msg( if 0 == dc_job_send_msg(context, (*msg).id) { return 0i32 as uint32_t; } - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, (*msg).chat_id as uintptr_t, @@ -1022,7 +1006,7 @@ pub unsafe fn dc_send_msg( ); if 0 != dc_param_exists((*msg).param, DC_PARAM_SET_LATITUDE as libc::c_int) { - ((*context).cb)(context, Event::LOCATION_CHANGED, DC_CONTACT_ID_SELF, 0); + (context.cb)(context, Event::LOCATION_CHANGED, DC_CONTACT_ID_SELF, 0); } if 0 == chat_id { @@ -1057,11 +1041,7 @@ pub unsafe fn dc_send_text_msg( ) -> uint32_t { let mut msg: *mut dc_msg_t = dc_msg_new(context, 10i32); let mut ret: uint32_t = 0i32 as uint32_t; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || chat_id <= 9i32 as libc::c_uint - || text_to_send.is_null()) - { + if !(chat_id <= 9i32 as libc::c_uint || text_to_send.is_null()) { (*msg).text = dc_strdup(text_to_send); ret = dc_send_msg(context, chat_id, msg) } @@ -1073,14 +1053,11 @@ pub unsafe fn dc_set_draft( mut chat_id: uint32_t, mut msg: *mut dc_msg_t, ) { - if context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || chat_id <= 9i32 as libc::c_uint - { + if chat_id <= 9i32 as libc::c_uint { return; } if 0 != set_draft_raw(context, chat_id, msg) { - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, @@ -1139,7 +1116,7 @@ unsafe fn set_draft_raw( 14513523936503887211 => {} _ => { stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), b"INSERT INTO msgs (chat_id, from_id, timestamp, type, state, txt, param, hidden) VALUES (?,?,?, ?,?,?,?,?);\x00" as *const u8 as *const libc::c_char); @@ -1174,7 +1151,7 @@ unsafe fn set_draft_raw( unsafe fn get_draft_msg_id(mut context: &dc_context_t, mut chat_id: uint32_t) -> uint32_t { let mut draft_msg_id: uint32_t = 0i32 as uint32_t; let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"SELECT id FROM msgs WHERE chat_id=? AND state=?;\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); @@ -1188,10 +1165,7 @@ unsafe fn get_draft_msg_id(mut context: &dc_context_t, mut chat_id: uint32_t) -> pub unsafe fn dc_get_draft(mut context: &dc_context_t, mut chat_id: uint32_t) -> *mut dc_msg_t { let mut draft_msg_id: uint32_t = 0i32 as uint32_t; let mut draft_msg: *mut dc_msg_t = 0 as *mut dc_msg_t; - if context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || chat_id <= 9i32 as libc::c_uint - { + if chat_id <= 9i32 as libc::c_uint { return 0 as *mut dc_msg_t; } draft_msg_id = get_draft_msg_id(context, chat_id); @@ -1213,33 +1187,33 @@ pub unsafe fn dc_get_chat_msgs( ) -> *mut dc_array_t { //clock_t start = clock(); let mut success: libc::c_int = 0i32; - let mut ret: *mut dc_array_t = dc_array_new(context, 512i32 as size_t); + let mut ret: *mut dc_array_t = dc_array_new(512i32 as size_t); let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut curr_id: uint32_t = 0; let mut curr_local_timestamp: time_t = 0; let mut curr_day: libc::c_int = 0; let mut last_day: libc::c_int = 0i32; let mut cnv_to_local: libc::c_long = dc_gm2local_offset(); - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint || ret.is_null()) { + if !ret.is_null() { if chat_id == 1i32 as libc::c_uint { let mut show_emails: libc::c_int = dc_sqlite3_get_config_int( - (*context).sql, + &mut context.sql.lock().unwrap(), b"show_emails\x00" as *const u8 as *const libc::c_char, 0i32, ); stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), b"SELECT m.id, m.timestamp FROM msgs m LEFT JOIN chats ON m.chat_id=chats.id LEFT JOIN contacts ON m.from_id=contacts.id WHERE m.from_id!=1 AND m.from_id!=2 AND m.hidden=0 AND chats.blocked=2 AND contacts.blocked=0 AND m.msgrmsg>=? ORDER BY m.timestamp,m.id;\x00" as *const u8 as *const libc::c_char); sqlite3_bind_int(stmt, 1i32, if show_emails == 2i32 { 0i32 } else { 1i32 }); } else if chat_id == 5i32 as libc::c_uint { stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), b"SELECT m.id, m.timestamp FROM msgs m LEFT JOIN contacts ct ON m.from_id=ct.id WHERE m.starred=1 AND m.hidden=0 AND ct.blocked=0 ORDER BY m.timestamp,m.id;\x00" as *const u8 as *const libc::c_char) } else { stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), b"SELECT m.id, m.timestamp FROM msgs m WHERE m.chat_id=? AND m.hidden=0 ORDER BY m.timestamp,m.id;\x00" as *const u8 as *const libc::c_char); sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); @@ -1274,16 +1248,15 @@ pub unsafe fn dc_get_chat_msgs( pub unsafe fn dc_get_msg_cnt(mut context: &dc_context_t, mut chat_id: uint32_t) -> libc::c_int { let mut ret: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - stmt = dc_sqlite3_prepare( - (*context).sql, - b"SELECT COUNT(*) FROM msgs WHERE chat_id=?;\x00" as *const u8 as *const libc::c_char, - ); - sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); - if !(sqlite3_step(stmt) != 100i32) { - ret = sqlite3_column_int(stmt, 0i32) - } + stmt = dc_sqlite3_prepare( + &mut context.sql.lock().unwrap(), + b"SELECT COUNT(*) FROM msgs WHERE chat_id=?;\x00" as *const u8 as *const libc::c_char, + ); + sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); + if !(sqlite3_step(stmt) != 100i32) { + ret = sqlite3_column_int(stmt, 0i32); } + sqlite3_finalize(stmt); return ret; } @@ -1293,16 +1266,14 @@ pub unsafe fn dc_get_fresh_msg_cnt( ) -> libc::c_int { let mut ret: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - stmt = dc_sqlite3_prepare( - (*context).sql, - b"SELECT COUNT(*) FROM msgs WHERE state=10 AND hidden=0 AND chat_id=?;\x00" - as *const u8 as *const libc::c_char, - ); - sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); - if !(sqlite3_step(stmt) != 100i32) { - ret = sqlite3_column_int(stmt, 0i32) - } + stmt = dc_sqlite3_prepare( + &mut context.sql.lock().unwrap(), + b"SELECT COUNT(*) FROM msgs WHERE state=10 AND hidden=0 AND chat_id=?;\x00" + as *const u8 as *const libc::c_char, + ); + sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); + if !(sqlite3_step(stmt) != 100i32) { + ret = sqlite3_column_int(stmt, 0i32); } sqlite3_finalize(stmt); return ret; @@ -1310,55 +1281,54 @@ pub unsafe fn dc_get_fresh_msg_cnt( pub unsafe fn dc_marknoticed_chat(mut context: &dc_context_t, mut chat_id: uint32_t) { let mut check: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut update: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - check = dc_sqlite3_prepare( - (*context).sql, - b"SELECT id FROM msgs WHERE chat_id=? AND state=10;\x00" as *const u8 + + check = dc_sqlite3_prepare( + &mut context.sql.lock().unwrap(), + b"SELECT id FROM msgs WHERE chat_id=? AND state=10;\x00" as *const u8 + as *const libc::c_char, + ); + sqlite3_bind_int(check, 1i32, chat_id as libc::c_int); + if !(sqlite3_step(check) != 100i32) { + update = dc_sqlite3_prepare( + &mut context.sql.lock().unwrap(), + b"UPDATE msgs SET state=13 WHERE chat_id=? AND state=10;\x00" as *const u8 as *const libc::c_char, ); - sqlite3_bind_int(check, 1i32, chat_id as libc::c_int); - if !(sqlite3_step(check) != 100i32) { - update = dc_sqlite3_prepare( - (*context).sql, - b"UPDATE msgs SET state=13 WHERE chat_id=? AND state=10;\x00" as *const u8 - as *const libc::c_char, - ); - sqlite3_bind_int(update, 1i32, chat_id as libc::c_int); - sqlite3_step(update); - ((*context).cb)( - context, - Event::MSGS_CHANGED, - 0i32 as uintptr_t, - 0i32 as uintptr_t, - ); - } + sqlite3_bind_int(update, 1i32, chat_id as libc::c_int); + sqlite3_step(update); + (context.cb)( + context, + Event::MSGS_CHANGED, + 0i32 as uintptr_t, + 0i32 as uintptr_t, + ); } + sqlite3_finalize(check); sqlite3_finalize(update); } pub unsafe fn dc_marknoticed_all_chats(mut context: &dc_context_t) { let mut check: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut update: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - check = dc_sqlite3_prepare( - (*context).sql, - b"SELECT id FROM msgs WHERE state=10;\x00" as *const u8 as *const libc::c_char, + + check = dc_sqlite3_prepare( + &mut context.sql.lock().unwrap(), + b"SELECT id FROM msgs WHERE state=10;\x00" as *const u8 as *const libc::c_char, + ); + if !(sqlite3_step(check) != 100i32) { + update = dc_sqlite3_prepare( + &mut context.sql.lock().unwrap(), + b"UPDATE msgs SET state=13 WHERE state=10;\x00" as *const u8 as *const libc::c_char, + ); + sqlite3_step(update); + (context.cb)( + context, + Event::MSGS_CHANGED, + 0i32 as uintptr_t, + 0i32 as uintptr_t, ); - if !(sqlite3_step(check) != 100i32) { - update = dc_sqlite3_prepare( - (*context).sql, - b"UPDATE msgs SET state=13 WHERE state=10;\x00" as *const u8 - as *const libc::c_char, - ); - sqlite3_step(update); - ((*context).cb)( - context, - Event::MSGS_CHANGED, - 0i32 as uintptr_t, - 0i32 as uintptr_t, - ); - } } + sqlite3_finalize(check); sqlite3_finalize(update); } @@ -1369,12 +1339,9 @@ pub unsafe fn dc_get_chat_media( mut msg_type2: libc::c_int, mut msg_type3: libc::c_int, ) -> *mut dc_array_t { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return 0 as *mut dc_array_t; - } - let mut ret: *mut dc_array_t = dc_array_new(context, 100i32 as size_t); + let mut ret: *mut dc_array_t = dc_array_new(100i32 as size_t); let mut stmt: *mut sqlite3_stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), b"SELECT id FROM msgs WHERE chat_id=? AND (type=? OR type=? OR type=?) ORDER BY timestamp, id;\x00" as *const u8 as *const libc::c_char); sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); @@ -1416,41 +1383,41 @@ pub unsafe fn dc_get_next_media( let mut list: *mut dc_array_t = 0 as *mut dc_array_t; let mut i: libc::c_int = 0i32; let mut cnt: libc::c_int = 0i32; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - if !(0 == dc_msg_load_from_db(msg, context, curr_msg_id)) { - list = dc_get_chat_media( - context, - (*msg).chat_id, - if msg_type > 0i32 { - msg_type - } else { - (*msg).type_0 - }, - msg_type2, - msg_type3, - ); - if !list.is_null() { - cnt = dc_array_get_cnt(list) as libc::c_int; - i = 0i32; - while i < cnt { - if curr_msg_id == dc_array_get_id(list, i as size_t) { - if dir > 0i32 { - if i + 1i32 < cnt { - ret_msg_id = dc_array_get_id(list, (i + 1i32) as size_t) - } - } else if dir < 0i32 { - if i - 1i32 >= 0i32 { - ret_msg_id = dc_array_get_id(list, (i - 1i32) as size_t) - } + + if !(0 == dc_msg_load_from_db(msg, context, curr_msg_id)) { + list = dc_get_chat_media( + context, + (*msg).chat_id, + if msg_type > 0i32 { + msg_type + } else { + (*msg).type_0 + }, + msg_type2, + msg_type3, + ); + if !list.is_null() { + cnt = dc_array_get_cnt(list) as libc::c_int; + i = 0i32; + while i < cnt { + if curr_msg_id == dc_array_get_id(list, i as size_t) { + if dir > 0i32 { + if i + 1i32 < cnt { + ret_msg_id = dc_array_get_id(list, (i + 1i32) as size_t) + } + } else if dir < 0i32 { + if i - 1i32 >= 0i32 { + ret_msg_id = dc_array_get_id(list, (i - 1i32) as size_t) } - break; - } else { - i += 1 } + break; + } else { + i += 1 } } } } + dc_array_unref(list); dc_msg_unref(msg); return ret_msg_id; @@ -1460,16 +1427,12 @@ pub unsafe fn dc_archive_chat( mut chat_id: uint32_t, mut archive: libc::c_int, ) { - if context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || chat_id <= 9i32 as libc::c_uint - || archive != 0i32 && archive != 1i32 - { + if chat_id <= 9i32 as libc::c_uint || archive != 0i32 && archive != 1i32 { return; } if 0 != archive { let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"UPDATE msgs SET state=13 WHERE chat_id=? AND state=10;\x00" as *const u8 as *const libc::c_char, ); @@ -1478,14 +1441,14 @@ pub unsafe fn dc_archive_chat( sqlite3_finalize(stmt); } let mut stmt_0: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"UPDATE chats SET archived=? WHERE id=?;\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_int(stmt_0, 1i32, archive); sqlite3_bind_int(stmt_0, 2i32, chat_id as libc::c_int); sqlite3_step(stmt_0); sqlite3_finalize(stmt_0); - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -1497,26 +1460,22 @@ pub unsafe fn dc_delete_chat(mut context: &dc_context_t, mut chat_id: uint32_t) let mut pending_transaction: libc::c_int = 0i32; let mut obj: *mut dc_chat_t = dc_chat_new(context); let mut q3: *mut libc::c_char = 0 as *mut libc::c_char; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || chat_id <= 9i32 as libc::c_uint) - { + if !(chat_id <= 9i32 as libc::c_uint) { if !(0 == dc_chat_load_from_db(obj, chat_id)) { - dc_sqlite3_begin_transaction((*context).sql); pending_transaction = 1i32; q3 = sqlite3_mprintf( b"DELETE FROM msgs_mdns WHERE msg_id IN (SELECT id FROM msgs WHERE chat_id=%i);\x00" as *const u8 as *const libc::c_char, chat_id, ); - if !(0 == dc_sqlite3_execute((*context).sql, q3)) { + if !(0 == dc_sqlite3_execute(&mut context.sql.lock().unwrap(), q3)) { sqlite3_free(q3 as *mut libc::c_void); q3 = 0 as *mut libc::c_char; q3 = sqlite3_mprintf( b"DELETE FROM msgs WHERE chat_id=%i;\x00" as *const u8 as *const libc::c_char, chat_id, ); - if !(0 == dc_sqlite3_execute((*context).sql, q3)) { + if !(0 == dc_sqlite3_execute(&mut context.sql.lock().unwrap(), q3)) { sqlite3_free(q3 as *mut libc::c_void); q3 = 0 as *mut libc::c_char; q3 = sqlite3_mprintf( @@ -1524,7 +1483,7 @@ pub unsafe fn dc_delete_chat(mut context: &dc_context_t, mut chat_id: uint32_t) as *const libc::c_char, chat_id, ); - if !(0 == dc_sqlite3_execute((*context).sql, q3)) { + if !(0 == dc_sqlite3_execute(&mut context.sql.lock().unwrap(), q3)) { sqlite3_free(q3 as *mut libc::c_void); q3 = 0 as *mut libc::c_char; q3 = sqlite3_mprintf( @@ -1532,12 +1491,11 @@ pub unsafe fn dc_delete_chat(mut context: &dc_context_t, mut chat_id: uint32_t) as *const libc::c_char, chat_id, ); - if !(0 == dc_sqlite3_execute((*context).sql, q3)) { + if !(0 == dc_sqlite3_execute(&mut context.sql.lock().unwrap(), q3)) { sqlite3_free(q3 as *mut libc::c_void); q3 = 0 as *mut libc::c_char; - dc_sqlite3_commit((*context).sql); pending_transaction = 0i32; - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -1551,9 +1509,6 @@ pub unsafe fn dc_delete_chat(mut context: &dc_context_t, mut chat_id: uint32_t) } } } - if 0 != pending_transaction { - dc_sqlite3_rollback((*context).sql); - } dc_chat_unref(obj); sqlite3_free(q3 as *mut libc::c_void); } @@ -1563,32 +1518,32 @@ pub unsafe fn dc_get_chat_contacts( ) -> *mut dc_array_t { /* Normal chats do not include SELF. Group chats do (as it may happen that one is deleted from a groupchat but the chats stays visible, moreover, this makes displaying lists easier) */ - let mut ret: *mut dc_array_t = dc_array_new(context, 100i32 as size_t); + let mut ret: *mut dc_array_t = dc_array_new(100i32 as size_t); let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - if !(chat_id == 1i32 as libc::c_uint) { - /* we could also create a list for all contacts in the deaddrop by searching contacts belonging to chats with chats.blocked=2, however, currently this is not needed */ - stmt = - dc_sqlite3_prepare((*context).sql, - b"SELECT cc.contact_id FROM chats_contacts cc LEFT JOIN contacts c ON c.id=cc.contact_id WHERE cc.chat_id=? ORDER BY c.id=1, LOWER(c.name||c.addr), c.id;\x00" - as *const u8 as *const libc::c_char); - sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); - while sqlite3_step(stmt) == 100i32 { - dc_array_add_id(ret, sqlite3_column_int(stmt, 0i32) as uint32_t); - } + + if !(chat_id == 1i32 as libc::c_uint) { + /* we could also create a list for all contacts in the deaddrop by searching contacts belonging to chats with chats.blocked=2, however, currently this is not needed */ + stmt = + dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), + b"SELECT cc.contact_id FROM chats_contacts cc LEFT JOIN contacts c ON c.id=cc.contact_id WHERE cc.chat_id=? ORDER BY c.id=1, LOWER(c.name||c.addr), c.id;\x00" + as *const u8 as *const libc::c_char); + sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); + while sqlite3_step(stmt) == 100i32 { + dc_array_add_id(ret, sqlite3_column_int(stmt, 0i32) as uint32_t); } } + sqlite3_finalize(stmt); return ret; } pub unsafe fn dc_get_chat(mut context: &dc_context_t, mut chat_id: uint32_t) -> *mut dc_chat_t { let mut success: libc::c_int = 0i32; let mut obj: *mut dc_chat_t = dc_chat_new(context); - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - if !(0 == dc_chat_load_from_db(obj, chat_id)) { - success = 1i32 - } + + if !(0 == dc_chat_load_from_db(obj, chat_id)) { + success = 1i32 } + if 0 != success { return obj; } else { @@ -1607,17 +1562,13 @@ pub unsafe fn dc_create_group_chat( let mut draft_msg: *mut dc_msg_t = 0 as *mut dc_msg_t; let mut grpid: *mut libc::c_char = 0 as *mut libc::c_char; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || chat_name.is_null() - || *chat_name.offset(0isize) as libc::c_int == 0i32 - { + if chat_name.is_null() || *chat_name.offset(0isize) as libc::c_int == 0i32 { return 0i32 as uint32_t; } draft_txt = dc_stock_str_repl_string(context, 14i32, chat_name); grpid = dc_create_id(); stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"INSERT INTO chats (type, name, grpid, param) VALUES(?, ?, ?, \'U=1\');\x00" as *const u8 as *const libc::c_char, ); @@ -1626,7 +1577,7 @@ pub unsafe fn dc_create_group_chat( sqlite3_bind_text(stmt, 3i32, grpid, -1i32, None); if !(sqlite3_step(stmt) != 101i32) { chat_id = dc_sqlite3_get_rowid( - (*context).sql, + &mut context.sql.lock().unwrap(), b"chats\x00" as *const u8 as *const libc::c_char, b"grpid\x00" as *const u8 as *const libc::c_char, grpid, @@ -1644,7 +1595,7 @@ pub unsafe fn dc_create_group_chat( dc_msg_unref(draft_msg); free(grpid as *mut libc::c_void); if 0 != chat_id { - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -1663,7 +1614,7 @@ pub unsafe fn dc_add_to_chat_contacts_table( /* add a contact to a chat; the function does not check the type or if any of the record exist or are already added to the chat! */ let mut ret: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"INSERT INTO chats_contacts (chat_id, contact_id) VALUES(?, ?)\x00" as *const u8 as *const libc::c_char, ); @@ -1696,11 +1647,7 @@ pub unsafe fn dc_add_contact_to_chat_ex( let mut chat: *mut dc_chat_t = dc_chat_new(context); let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); let mut self_addr: *mut libc::c_char = 0 as *mut libc::c_char; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || contact.is_null() - || chat_id <= 9i32 as libc::c_uint) - { + if !(contact.is_null() || chat_id <= 9i32 as libc::c_uint) { dc_reset_gossiped_timestamp(context, chat_id); /*this also makes sure, not contacts are added to special or normal chats*/ if !(0i32 == real_group_exists(context, chat_id) @@ -1724,7 +1671,7 @@ pub unsafe fn dc_add_contact_to_chat_ex( dc_chat_update_param(chat); } self_addr = dc_sqlite3_get_config( - (*context).sql, + &mut context.sql.lock().unwrap(), b"configured_addr\x00" as *const u8 as *const libc::c_char, b"\x00" as *const u8 as *const libc::c_char, ); @@ -1781,14 +1728,14 @@ pub unsafe fn dc_add_contact_to_chat_ex( dc_param_set((*msg).param, 'E' as i32, (*contact).addr); dc_param_set_int((*msg).param, 'F' as i32, flags); (*msg).id = dc_send_msg(context, chat_id, msg); - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, (*msg).id as uintptr_t, ); } - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, @@ -1811,15 +1758,11 @@ unsafe fn real_group_exists(mut context: &dc_context_t, mut chat_id: uint32_t) - // check if a group or a verified group exists under the given ID let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut ret: libc::c_int = 0i32; - if context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || (*(*context).sql).cobj.is_null() - || chat_id <= 9i32 as libc::c_uint - { + if (*context.sql.lock().unwrap()).cobj.is_null() || chat_id <= 9i32 as libc::c_uint { return 0i32; } stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"SELECT id FROM chats WHERE id=? AND (type=120 OR type=130);\x00" as *const u8 as *const libc::c_char, ); @@ -1848,7 +1791,7 @@ pub unsafe fn dc_set_gossiped_timestamp( timestamp as libc::c_int, ); stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"UPDATE chats SET gossiped_timestamp=? WHERE id=?;\x00" as *const u8 as *const libc::c_char, ); @@ -1862,7 +1805,7 @@ pub unsafe fn dc_set_gossiped_timestamp( timestamp as libc::c_int, ); stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"UPDATE chats SET gossiped_timestamp=?;\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_int64(stmt, 1i32, timestamp as sqlite3_int64); @@ -1880,9 +1823,7 @@ pub unsafe fn dc_remove_contact_from_chat( let mut chat: *mut dc_chat_t = dc_chat_new(context); let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); let mut q3: *mut libc::c_char = 0 as *mut libc::c_char; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || chat_id <= 9i32 as libc::c_uint + if !(chat_id <= 9i32 as libc::c_uint || contact_id <= 9i32 as libc::c_uint && contact_id != 1i32 as libc::c_uint) { /* we do not check if "contact_id" exists but just delete all records with the id from chats_contacts */ @@ -1924,7 +1865,7 @@ pub unsafe fn dc_remove_contact_from_chat( dc_param_set_int((*msg).param, 'S' as i32, 5i32); dc_param_set((*msg).param, 'E' as i32, (*contact).addr); (*msg).id = dc_send_msg(context, chat_id, msg); - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, @@ -1938,8 +1879,8 @@ pub unsafe fn dc_remove_contact_from_chat( chat_id, contact_id, ); - if !(0 == dc_sqlite3_execute((*context).sql, q3)) { - ((*context).cb)( + if !(0 == dc_sqlite3_execute(&mut context.sql.lock().unwrap(), q3)) { + (context.cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, @@ -1962,7 +1903,7 @@ pub unsafe fn dc_set_group_explicitly_left( ) { if 0 == dc_is_group_explicitly_left(context, grpid) { let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"INSERT INTO leftgrps (grpid) VALUES(?);\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_text(stmt, 1i32, grpid, -1i32, None); @@ -1975,7 +1916,7 @@ pub unsafe fn dc_is_group_explicitly_left( mut grpid: *const libc::c_char, ) -> libc::c_int { let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"SELECT id FROM leftgrps WHERE grpid=?;\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_text(stmt, 1i32, grpid, -1i32, None); @@ -1993,9 +1934,7 @@ pub unsafe fn dc_set_chat_name( let mut chat: *mut dc_chat_t = dc_chat_new(context); let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); let mut q3: *mut libc::c_char = 0 as *mut libc::c_char; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || new_name.is_null() + if !(new_name.is_null() || *new_name.offset(0isize) as libc::c_int == 0i32 || chat_id <= 9i32 as libc::c_uint) { @@ -2020,7 +1959,7 @@ pub unsafe fn dc_set_chat_name( new_name, chat_id, ); - if !(0 == dc_sqlite3_execute((*context).sql, q3)) { + if !(0 == dc_sqlite3_execute(&mut context.sql.lock().unwrap(), q3)) { if dc_param_get_int((*chat).param, 'U' as i32, 0i32) == 0i32 { (*msg).type_0 = 10i32; (*msg).text = dc_stock_system_msg( @@ -2033,14 +1972,14 @@ pub unsafe fn dc_set_chat_name( dc_param_set_int((*msg).param, 'S' as i32, 2i32); dc_param_set((*msg).param, 'E' as i32, (*chat).name); (*msg).id = dc_send_msg(context, chat_id, msg); - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, (*msg).id as uintptr_t, ); } - ((*context).cb)( + (context.cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, @@ -2066,10 +2005,7 @@ pub unsafe fn dc_set_chat_profile_image( let mut chat: *mut dc_chat_t = dc_chat_new(context); let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); let mut new_image_rel: *mut libc::c_char = 0 as *mut libc::c_char; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || chat_id <= 9i32 as libc::c_uint) - { + if !(chat_id <= 9i32 as libc::c_uint) { if !(0i32 == real_group_exists(context, chat_id) || 0i32 == dc_chat_load_from_db(chat, chat_id)) { @@ -2114,14 +2050,14 @@ pub unsafe fn dc_set_chat_profile_image( 1i32 as uint32_t, ); (*msg).id = dc_send_msg(context, chat_id, msg); - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, (*msg).id as uintptr_t, ); } - ((*context).cb)( + (context.cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, @@ -2156,16 +2092,10 @@ pub unsafe fn dc_forward_msgs( let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut curr_timestamp: time_t = 0i32 as time_t; let mut original_param: *mut dc_param_t = dc_param_new(); - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || msg_ids.is_null() - || msg_cnt <= 0i32 - || chat_id <= 9i32 as libc::c_uint) - { - dc_sqlite3_begin_transaction((*context).sql); + if !(msg_ids.is_null() || msg_cnt <= 0i32 || chat_id <= 9i32 as libc::c_uint) { transaction_pending = 1i32; dc_unarchive_chat(context, chat_id); - (*(*context).smtp).log_connect_errors = 1i32; + context.smtp.lock().unwrap().log_connect_errors = 1i32; if !(0 == dc_chat_load_from_db(chat, chat_id)) { curr_timestamp = dc_create_smeared_timestamps(context, msg_cnt); idsstr = dc_arr_to_string(msg_ids, msg_cnt); @@ -2174,7 +2104,7 @@ pub unsafe fn dc_forward_msgs( as *const libc::c_char, idsstr, ); - stmt = dc_sqlite3_prepare((*context).sql, q3); + stmt = dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), q3); loop { if !(sqlite3_step(stmt) == 100i32) { current_block = 10758786907990354186; @@ -2235,21 +2165,15 @@ pub unsafe fn dc_forward_msgs( } match current_block { 2015322633586469911 => {} - _ => { - dc_sqlite3_commit((*context).sql); - transaction_pending = 0i32 - } + _ => transaction_pending = 0i32, } } } - if 0 != transaction_pending { - dc_sqlite3_rollback((*context).sql); - } if !created_db_entries.is_null() { let mut i = 0u32; let mut icnt = carray_count(created_db_entries); while i < icnt { - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, carray_get(created_db_entries, i) as uintptr_t, @@ -2296,7 +2220,7 @@ pub unsafe extern "C" fn dc_chat_get_subtitle(mut chat: *const dc_chat_t) -> *mu } else if (*chat).type_0 == 100i32 { let mut r: libc::c_int = 0; let mut stmt: *mut sqlite3_stmt = - dc_sqlite3_prepare((*(*chat).context).sql, + dc_sqlite3_prepare(&mut (*chat).context.sql.lock().unwrap(), b"SELECT c.addr FROM chats_contacts cc LEFT JOIN contacts c ON c.id=cc.contact_id WHERE cc.chat_id=?;\x00" as *const u8 as *const libc::c_char); sqlite3_bind_int(stmt, 1i32, (*chat).id as libc::c_int); @@ -2326,7 +2250,7 @@ pub unsafe fn dc_get_chat_contact_cnt( ) -> libc::c_int { let mut ret: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"SELECT COUNT(*) FROM chats_contacts WHERE chat_id=?;\x00" as *const u8 as *const libc::c_char, ); @@ -2411,13 +2335,10 @@ pub unsafe fn dc_chat_is_sending_locations(mut chat: *const dc_chat_t) -> libc:: pub unsafe fn dc_get_chat_cnt(mut context: &dc_context_t) -> size_t { let mut ret: size_t = 0i32 as size_t; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || (*(*context).sql).cobj.is_null()) - { + if !(*context.sql.lock().unwrap()).cobj.is_null() { /* no database, no chats - this is no error (needed eg. for information) */ stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"SELECT COUNT(*) FROM chats WHERE id>9 AND blocked=0;\x00" as *const u8 as *const libc::c_char, ); @@ -2442,23 +2363,23 @@ pub unsafe fn dc_get_chat_id_by_grpid( if !ret_verified.is_null() { *ret_verified = 0i32 } - if !(context.is_null() || grpid.is_null()) { - stmt = dc_sqlite3_prepare( - (*context).sql, - b"SELECT id, blocked, type FROM chats WHERE grpid=?;\x00" as *const u8 - as *const libc::c_char, - ); - sqlite3_bind_text(stmt, 1i32, grpid, -1i32, None); - if sqlite3_step(stmt) == 100i32 { - chat_id = sqlite3_column_int(stmt, 0i32) as uint32_t; - if !ret_blocked.is_null() { - *ret_blocked = sqlite3_column_int(stmt, 1i32) - } - if !ret_verified.is_null() { - *ret_verified = (sqlite3_column_int(stmt, 2i32) == 130i32) as libc::c_int - } + + stmt = dc_sqlite3_prepare( + &mut context.sql.lock().unwrap(), + b"SELECT id, blocked, type FROM chats WHERE grpid=?;\x00" as *const u8 + as *const libc::c_char, + ); + sqlite3_bind_text(stmt, 1i32, grpid, -1i32, None); + if sqlite3_step(stmt) == 100i32 { + chat_id = sqlite3_column_int(stmt, 0i32) as uint32_t; + if !ret_blocked.is_null() { + *ret_blocked = sqlite3_column_int(stmt, 1i32) + } + if !ret_verified.is_null() { + *ret_verified = (sqlite3_column_int(stmt, 2i32) == 130i32) as libc::c_int } } + sqlite3_finalize(stmt); return chat_id; } @@ -2473,9 +2394,9 @@ pub unsafe fn dc_add_device_msg( 0 as *const libc::c_char, b"@device\x00" as *const u8 as *const libc::c_char, ); - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint || text.is_null()) { + if !text.is_null() { stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), b"INSERT INTO msgs (chat_id,from_id,to_id, timestamp,type,state, txt,rfc724_mid) VALUES (?,?,?, ?,?,?, ?,?);\x00" as *const u8 as *const libc::c_char); sqlite3_bind_int(stmt, 1i32, chat_id as libc::c_int); @@ -2492,12 +2413,12 @@ pub unsafe fn dc_add_device_msg( sqlite3_bind_text(stmt, 8i32, rfc724_mid, -1i32, None); if !(sqlite3_step(stmt) != 101i32) { msg_id = dc_sqlite3_get_rowid( - (*context).sql, + &mut context.sql.lock().unwrap(), b"msgs\x00" as *const u8 as *const libc::c_char, b"rfc724_mid\x00" as *const u8 as *const libc::c_char, rfc724_mid, ); - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, diff --git a/src/dc_chatlist.rs b/src/dc_chatlist.rs index bf203a95b..56267da8b 100644 --- a/src/dc_chatlist.rs +++ b/src/dc_chatlist.rs @@ -15,27 +15,27 @@ use crate::x::*; /* * the structure behind dc_chatlist_t */ #[derive(Copy, Clone)] #[repr(C)] -pub struct dc_chatlist_t { +pub struct dc_chatlist_t<'a> { pub magic: uint32_t, - pub context: &dc_context_t, + pub context: &'a dc_context_t, pub cnt: size_t, pub chatNlastmsg_ids: *mut dc_array_t, } // handle chatlists -pub unsafe fn dc_get_chatlist( - mut context: &dc_context_t, +pub unsafe fn dc_get_chatlist<'a>( + mut context: &'a dc_context_t, mut listflags: libc::c_int, mut query_str: *const libc::c_char, mut query_id: uint32_t, -) -> *mut dc_chatlist_t { +) -> *mut dc_chatlist_t<'a> { let mut success: libc::c_int = 0i32; let mut obj: *mut dc_chatlist_t = dc_chatlist_new(context); - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - if !(0 == dc_chatlist_load_from_db(obj, listflags, query_str, query_id)) { - success = 1i32 - } + + if !(0 == dc_chatlist_load_from_db(obj, listflags, query_str, query_id)) { + success = 1i32 } + if 0 != success { return obj; } else { @@ -89,7 +89,7 @@ pub unsafe fn dc_chatlist_new(mut context: &dc_context_t) -> *mut dc_chatlist_t } (*chatlist).magic = 0xc4a71157u32; (*chatlist).context = context; - (*chatlist).chatNlastmsg_ids = dc_array_new(context, 128i32 as size_t); + (*chatlist).chatNlastmsg_ids = dc_array_new(128i32 as size_t); if (*chatlist).chatNlastmsg_ids.is_null() { exit(32i32); } @@ -129,8 +129,7 @@ unsafe fn dc_chatlist_load_from_db( let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut strLikeCmd: *mut libc::c_char = 0 as *mut libc::c_char; let mut query: *mut libc::c_char = 0 as *mut libc::c_char; - if !(chatlist.is_null() || (*chatlist).magic != 0xc4a71157u32 || (*chatlist).context.is_null()) - { + if !(chatlist.is_null() || (*chatlist).magic != 0xc4a71157u32) { dc_chatlist_empty(chatlist); // select with left join and minimum: // - the inner select must use `hidden` and _not_ `m.hidden` @@ -144,14 +143,14 @@ unsafe fn dc_chatlist_load_from_db( // shown at all permanent in the chatlist. if 0 != query_contact_id { stmt = - dc_sqlite3_prepare((*(*chatlist).context).sql, + dc_sqlite3_prepare(&mut (*chatlist).context.sql.lock().unwrap(), b"SELECT c.id, m.id FROM chats c LEFT JOIN msgs m ON c.id=m.chat_id AND m.timestamp=( SELECT MAX(timestamp) FROM msgs WHERE chat_id=c.id AND (hidden=0 OR (hidden=1 AND state=19))) WHERE c.id>9 AND c.blocked=0 AND c.id IN(SELECT chat_id FROM chats_contacts WHERE contact_id=?) GROUP BY c.id ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;\x00" as *const u8 as *const libc::c_char); sqlite3_bind_int(stmt, 1i32, query_contact_id as libc::c_int); current_block = 3437258052017859086; } else if 0 != listflags & 0x1i32 { stmt = - dc_sqlite3_prepare((*(*chatlist).context).sql, + dc_sqlite3_prepare(&mut (*chatlist).context.sql.lock().unwrap(), b"SELECT c.id, m.id FROM chats c LEFT JOIN msgs m ON c.id=m.chat_id AND m.timestamp=( SELECT MAX(timestamp) FROM msgs WHERE chat_id=c.id AND (hidden=0 OR (hidden=1 AND state=19))) WHERE c.id>9 AND c.blocked=0 AND c.archived=1 GROUP BY c.id ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;\x00" as *const u8 as *const libc::c_char); current_block = 3437258052017859086; @@ -166,7 +165,7 @@ unsafe fn dc_chatlist_load_from_db( add_archived_link_item = 1i32 } stmt = - dc_sqlite3_prepare((*(*chatlist).context).sql, + dc_sqlite3_prepare(&mut (*chatlist).context.sql.lock().unwrap(), b"SELECT c.id, m.id FROM chats c LEFT JOIN msgs m ON c.id=m.chat_id AND m.timestamp=( SELECT MAX(timestamp) FROM msgs WHERE chat_id=c.id AND (hidden=0 OR (hidden=1 AND state=19))) WHERE c.id>9 AND c.blocked=0 AND c.archived=0 GROUP BY c.id ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;\x00" as *const u8 as *const libc::c_char); current_block = 3437258052017859086; @@ -179,7 +178,7 @@ unsafe fn dc_chatlist_load_from_db( } else { strLikeCmd = dc_mprintf(b"%%%s%%\x00" as *const u8 as *const libc::c_char, query); stmt = - dc_sqlite3_prepare((*(*chatlist).context).sql, + dc_sqlite3_prepare(&mut (*chatlist).context.sql.lock().unwrap(), b"SELECT c.id, m.id FROM chats c LEFT JOIN msgs m ON c.id=m.chat_id AND m.timestamp=( SELECT MAX(timestamp) FROM msgs WHERE chat_id=c.id AND (hidden=0 OR (hidden=1 AND state=19))) WHERE c.id>9 AND c.blocked=0 AND c.name LIKE ? GROUP BY c.id ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;\x00" as *const u8 as *const libc::c_char); @@ -224,7 +223,7 @@ unsafe fn dc_chatlist_load_from_db( pub unsafe fn dc_get_archived_cnt(mut context: &dc_context_t) -> libc::c_int { let mut ret: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"SELECT COUNT(*) FROM chats WHERE blocked=0 AND archived=1;\x00" as *const u8 as *const libc::c_char, ); @@ -238,7 +237,7 @@ unsafe fn get_last_deaddrop_fresh_msg(mut context: &dc_context_t) -> uint32_t { let mut ret: uint32_t = 0i32 as uint32_t; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.lock().unwrap(), b"SELECT m.id FROM msgs m LEFT JOIN chats c ON c.id=m.chat_id WHERE m.state=10 AND m.hidden=0 AND c.blocked=2 ORDER BY m.timestamp DESC, m.id DESC;\x00" as *const u8 as *const libc::c_char); /* we have an index over the state-column, this should be sufficient as there are typically only few fresh messages */ @@ -283,10 +282,10 @@ pub unsafe fn dc_chatlist_get_msg_id( index.wrapping_mul(2).wrapping_add(1), ); } -pub unsafe fn dc_chatlist_get_summary( - mut chatlist: *const dc_chatlist_t, +pub unsafe fn dc_chatlist_get_summary<'a>( + mut chatlist: *const dc_chatlist_t<'a>, mut index: size_t, - mut chat: *mut dc_chat_t, + mut chat: *mut dc_chat_t<'a>, ) -> *mut dc_lot_t { let mut current_block: u64; /* The summary is created by the chat, not by the last message. @@ -335,7 +334,7 @@ pub unsafe fn dc_chatlist_get_summary( lastcontact = dc_contact_new((*chatlist).context); dc_contact_load_from_db( lastcontact, - (*(*chatlist).context).sql, + &mut (*chatlist).context.sql.lock().unwrap(), (*lastmsg).from_id, ); } @@ -355,9 +354,8 @@ pub unsafe fn dc_chatlist_get_summary( dc_chat_unref(chat_to_delete); return ret; } -pub unsafe fn dc_chatlist_get_context(mut chatlist: *mut dc_chatlist_t) -> &dc_context_t { - if chatlist.is_null() || (*chatlist).magic != 0xc4a71157u32 { - return 0 as *mut dc_context_t; - } - return (*chatlist).context; + +pub unsafe fn dc_chatlist_get_context<'a>(chatlist: *mut dc_chatlist_t<'a>) -> &'a dc_context_t { + assert!(!chatlist.is_null()); + (*chatlist).context } diff --git a/src/dc_configure.rs b/src/dc_configure.rs index c63ade0c8..0a7a05da9 100644 --- a/src/dc_configure.rs +++ b/src/dc_configure.rs @@ -72,9 +72,6 @@ pub unsafe fn dc_configure(mut context: &dc_context_t) { dc_job_add(context, 900i32, 0i32, 0 as *const libc::c_char, 0i32); } pub unsafe fn dc_has_ongoing(mut context: &dc_context_t) -> libc::c_int { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return 0i32; - } return if 0 != (*context).ongoing_running || (*context).shall_stop_ongoing == 0i32 { 1i32 } else { @@ -82,12 +79,9 @@ pub unsafe fn dc_has_ongoing(mut context: &dc_context_t) -> libc::c_int { }; } pub unsafe fn dc_is_configured(mut context: &dc_context_t) -> libc::c_int { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return 0i32; - } return if 0 != dc_sqlite3_get_config_int( - (*context).sql, + &mut context.sql.lock().unwrap(), b"configured\x00" as *const u8 as *const libc::c_char, 0i32, ) { @@ -97,9 +91,6 @@ pub unsafe fn dc_is_configured(mut context: &dc_context_t) -> libc::c_int { }; } pub unsafe fn dc_stop_ongoing_process(mut context: &dc_context_t) { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return; - } if 0 != (*context).ongoing_running && (*context).shall_stop_ongoing == 0i32 { dc_log_info( context, @@ -129,1159 +120,1154 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut let mut param_domain: *mut libc::c_char = 0 as *mut libc::c_char; let mut param_addr_urlencoded: *mut libc::c_char = 0 as *mut libc::c_char; let mut param_autoconfig: *mut dc_loginparam_t = 0 as *mut dc_loginparam_t; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - if !(0 == dc_alloc_ongoing(context)) { - ongoing_allocated_here = 1i32; - if 0 == dc_sqlite3_is_open((*context).sql) { - dc_log_error( + + if !(0 == dc_alloc_ongoing(context)) { + ongoing_allocated_here = 1i32; + if 0 == dc_sqlite3_is_open(&mut context.sql.lock().unwrap()) { + dc_log_error( + context, + 0i32, + b"Cannot configure, database not opened.\x00" as *const u8 as *const libc::c_char, + ); + } else { + dc_imap_disconnect(context, &mut context.inbox.clone().lock().unwrap()); + dc_imap_disconnect( + context, + &mut context.sentbox_thread.imap.clone().lock().unwrap(), + ); + dc_imap_disconnect( + context, + &mut context.mvbox_thread.imap.clone().lock().unwrap(), + ); + dc_smtp_disconnect(&mut context.smtp.clone().lock().unwrap()); + context.smtp.clone().lock().unwrap().log_connect_errors = 1i32; + context.inbox.clone().lock().unwrap().log_connect_errors = 1i32; + context + .sentbox_thread + .imap + .clone() + .lock() + .unwrap() + .log_connect_errors = 1i32; + context + .mvbox_thread + .imap + .clone() + .lock() + .unwrap() + .log_connect_errors = 1i32; + dc_log_info( + context, + 0i32, + b"Configure ...\x00" as *const u8 as *const libc::c_char, + ); + if !(0 != context.shall_stop_ongoing) { + (context.cb)( context, - 0i32, - b"Cannot configure, database not opened.\x00" as *const u8 - as *const libc::c_char, - ); - } else { - dc_imap_disconnect((*context).inbox); - dc_imap_disconnect((*context).sentbox_thread.imap); - dc_imap_disconnect((*context).mvbox_thread.imap); - dc_smtp_disconnect((*context).smtp); - (*(*context).smtp).log_connect_errors = 1i32; - (*(*context).inbox).log_connect_errors = 1i32; - (*(*context).sentbox_thread.imap).log_connect_errors = 1i32; - (*(*context).mvbox_thread.imap).log_connect_errors = 1i32; - dc_log_info( - context, - 0i32, - b"Configure ...\x00" as *const u8 as *const libc::c_char, - ); - if !(0 != (*context).shall_stop_ongoing) { - ((*context).cb)( - context, - Event::CONFIGURE_PROGRESS, - (if 0i32 < 1i32 { - 1i32 - } else if 0i32 > 999i32 { - 999i32 - } else { - 0i32 - }) as uintptr_t, - 0i32 as uintptr_t, - ); - param = dc_loginparam_new(); - dc_loginparam_read( - param, - (*context).sql, - b"\x00" as *const u8 as *const libc::c_char, - ); - if (*param).addr.is_null() { - dc_log_error( - context, - 0i32, - b"Please enter the email address.\x00" as *const u8 - as *const libc::c_char, - ); + Event::CONFIGURE_PROGRESS, + (if 0i32 < 1i32 { + 1i32 + } else if 0i32 > 999i32 { + 999i32 } else { - dc_trim((*param).addr); - if 0 != (*param).server_flags & 0x2i32 { - // the used oauth2 addr may differ, check this. - // if dc_get_oauth2_addr() is not available in the oauth2 implementation, - // just use the given one. - if 0 != (*context).shall_stop_ongoing { + 0i32 + }) as uintptr_t, + 0i32 as uintptr_t, + ); + param = dc_loginparam_new(); + dc_loginparam_read( + param, + &mut context.sql.lock().unwrap(), + b"\x00" as *const u8 as *const libc::c_char, + ); + if (*param).addr.is_null() { + dc_log_error( + context, + 0i32, + b"Please enter the email address.\x00" as *const u8 as *const libc::c_char, + ); + } else { + dc_trim((*param).addr); + if 0 != (*param).server_flags & 0x2i32 { + // the used oauth2 addr may differ, check this. + // if dc_get_oauth2_addr() is not available in the oauth2 implementation, + // just use the given one. + if 0 != context.shall_stop_ongoing { + current_block = 2927484062889439186; + } else { + (context.cb)( + context, + Event::CONFIGURE_PROGRESS, + (if 10i32 < 1i32 { + 1i32 + } else if 10i32 > 999i32 { + 999i32 + } else { + 10i32 + }) as uintptr_t, + 0i32 as uintptr_t, + ); + let mut oauth2_addr: *mut libc::c_char = + dc_get_oauth2_addr(context, (*param).addr, (*param).mail_pw); + if !oauth2_addr.is_null() { + free((*param).addr as *mut libc::c_void); + (*param).addr = oauth2_addr; + dc_sqlite3_set_config( + context, + &mut context.sql.lock().unwrap(), + b"addr\x00" as *const u8 as *const libc::c_char, + (*param).addr, + ); + } + if 0 != context.shall_stop_ongoing { current_block = 2927484062889439186; } else { - ((*context).cb)( + (context.cb)( context, Event::CONFIGURE_PROGRESS, - (if 10i32 < 1i32 { + (if 20i32 < 1i32 { 1i32 - } else if 10i32 > 999i32 { + } else if 20i32 > 999i32 { 999i32 } else { - 10i32 + 20i32 }) as uintptr_t, 0i32 as uintptr_t, ); - let mut oauth2_addr: *mut libc::c_char = - dc_get_oauth2_addr(context, (*param).addr, (*param).mail_pw); - if !oauth2_addr.is_null() { - free((*param).addr as *mut libc::c_void); - (*param).addr = oauth2_addr; - dc_sqlite3_set_config( - (*context).sql, - b"addr\x00" as *const u8 as *const libc::c_char, - (*param).addr, - ); + current_block = 7746103178988627676; + } + } + } else { + current_block = 7746103178988627676; + } + match current_block { + 2927484062889439186 => {} + _ => { + param_domain = strchr((*param).addr, '@' as i32); + if param_domain.is_null() + || *param_domain.offset(0isize) as libc::c_int == 0i32 + { + dc_log_error( + context, + 0i32, + b"Bad email-address.\x00" as *const u8 as *const libc::c_char, + ); + } else { + param_domain = param_domain.offset(1isize); + param_addr_urlencoded = dc_urlencode((*param).addr); + if (*param).mail_pw.is_null() { + (*param).mail_pw = dc_strdup(0 as *const libc::c_char) } - if 0 != (*context).shall_stop_ongoing { - current_block = 2927484062889439186; - } else { - ((*context).cb)( + if !(0 != context.shall_stop_ongoing) { + (context.cb)( context, Event::CONFIGURE_PROGRESS, - (if 20i32 < 1i32 { + (if 200i32 < 1i32 { 1i32 - } else if 20i32 > 999i32 { + } else if 200i32 > 999i32 { 999i32 } else { - 20i32 + 200i32 }) as uintptr_t, 0i32 as uintptr_t, ); - current_block = 7746103178988627676; - } - } - } else { - current_block = 7746103178988627676; - } - match current_block { - 2927484062889439186 => {} - _ => { - param_domain = strchr((*param).addr, '@' as i32); - if param_domain.is_null() - || *param_domain.offset(0isize) as libc::c_int == 0i32 - { - dc_log_error( - context, - 0i32, - b"Bad email-address.\x00" as *const u8 - as *const libc::c_char, - ); - } else { - param_domain = param_domain.offset(1isize); - param_addr_urlencoded = dc_urlencode((*param).addr); - if (*param).mail_pw.is_null() { - (*param).mail_pw = dc_strdup(0 as *const libc::c_char) - } - if !(0 != (*context).shall_stop_ongoing) { - ((*context).cb)( - context, - Event::CONFIGURE_PROGRESS, - (if 200i32 < 1i32 { - 1i32 - } else if 200i32 > 999i32 { - 999i32 - } else { - 200i32 - }) - as uintptr_t, - 0i32 as uintptr_t, - ); - /* 2. Autoconfig - **************************************************************************/ - if (*param).mail_server.is_null() - && (*param).mail_port == 0i32 - && (*param).send_server.is_null() - && (*param).send_port == 0i32 - && (*param).send_user.is_null() - && (*param).server_flags & !0x2i32 == 0i32 - { - /*&¶m->mail_user ==NULL -- the user can enter a loginname which is used by autoconfig then */ - /*&¶m->send_pw ==NULL -- the password cannot be auto-configured and is no criterion for autoconfig or not */ - /* flags but OAuth2 avoid autoconfig */ - let mut keep_flags: libc::c_int = - (*param).server_flags & 0x2i32; - /* A. Search configurations from the domain used in the email-address, prefer encrypted */ - if param_autoconfig.is_null() { - let mut url: - *mut libc::c_char = - dc_mprintf(b"https://autoconfig.%s/mail/config-v1.1.xml?emailaddress=%s\x00" - as - *const u8 - as - *const libc::c_char, - param_domain, - param_addr_urlencoded); - param_autoconfig = - moz_autoconfigure(context, url, param); - free(url as *mut libc::c_void); - if 0 != (*context).shall_stop_ongoing { - current_block = 2927484062889439186; - } else { - ((*context).cb)( - context, - Event::CONFIGURE_PROGRESS, - (if 300i32 < 1i32 { - 1i32 - } else if 300i32 > 999i32 { - 999i32 - } else { - 300i32 - }) - as uintptr_t, - 0i32 as uintptr_t, - ); - current_block = 13325891313334703151; - } + /* 2. Autoconfig + **************************************************************************/ + if (*param).mail_server.is_null() + && (*param).mail_port == 0i32 + && (*param).send_server.is_null() + && (*param).send_port == 0i32 + && (*param).send_user.is_null() + && (*param).server_flags & !0x2i32 == 0i32 + { + /*&¶m->mail_user ==NULL -- the user can enter a loginname which is used by autoconfig then */ + /*&¶m->send_pw ==NULL -- the password cannot be auto-configured and is no criterion for autoconfig or not */ + /* flags but OAuth2 avoid autoconfig */ + let mut keep_flags: libc::c_int = + (*param).server_flags & 0x2i32; + /* A. Search configurations from the domain used in the email-address, prefer encrypted */ + if param_autoconfig.is_null() { + let mut url: + *mut libc::c_char = + dc_mprintf(b"https://autoconfig.%s/mail/config-v1.1.xml?emailaddress=%s\x00" + as + *const u8 + as + *const libc::c_char, + param_domain, + param_addr_urlencoded); + param_autoconfig = + moz_autoconfigure(context, url, param); + free(url as *mut libc::c_void); + if 0 != context.shall_stop_ongoing { + current_block = 2927484062889439186; } else { + (context.cb)( + context, + Event::CONFIGURE_PROGRESS, + (if 300i32 < 1i32 { + 1i32 + } else if 300i32 > 999i32 { + 999i32 + } else { + 300i32 + }) + as uintptr_t, + 0i32 as uintptr_t, + ); current_block = 13325891313334703151; } - match current_block { - 2927484062889439186 => {} - _ => { - if param_autoconfig.is_null() { - // the doc does not mention `emailaddress=`, however, Thunderbird adds it, see https://releases.mozilla.org/pub/thunderbird/ , which makes some sense - let mut url_0: - *mut libc::c_char = - dc_mprintf(b"https://%s/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=%s\x00" - as - *const u8 - as - *const libc::c_char, - param_domain, - param_addr_urlencoded); - param_autoconfig = moz_autoconfigure( - context, url_0, param, - ); - free(url_0 as *mut libc::c_void); - if 0 != (*context).shall_stop_ongoing { - current_block = 2927484062889439186; - } else { - ((*context).cb)( - context, - Event::CONFIGURE_PROGRESS, - (if 310i32 < 1i32 { - 1i32 - } else if 310i32 > 999i32 { - 999i32 - } else { - 310i32 - }) - as uintptr_t, - 0i32 as uintptr_t, - ); - current_block = 5597585068398118923; - } - } else { - current_block = 5597585068398118923; - } - match current_block { - 2927484062889439186 => {} - _ => { - let mut i: libc::c_int = 0i32; - loop { - if !(i <= 1i32) { - current_block = - 12961834331865314435; - break; - } - if param_autoconfig.is_null() { - /* Outlook uses always SSL but different domains */ - let mut url_1: - *mut libc::c_char = - dc_mprintf(b"https://%s%s/autodiscover/autodiscover.xml\x00" - as - *const u8 - as - *const libc::c_char, - if i - == - 0i32 - { - b"\x00" - as - *const u8 - as - *const libc::c_char - } else { - b"autodiscover.\x00" - as - *const u8 - as - *const libc::c_char - }, - param_domain); - param_autoconfig = - outlk_autodiscover( - context, url_1, param, - ); - free( - url_1 as *mut libc::c_void, - ); - if 0 != (*context) - .shall_stop_ongoing - { - current_block = - 2927484062889439186; - break; - } - ((*context).cb)( - context, - Event::CONFIGURE_PROGRESS, - (if 320i32 + i * 10i32 - < 1i32 - { - 1i32 - } else if 320i32 + i * 10i32 - > 999i32 - { - 999i32 - } else { - 320i32 + i * 10i32 - }) - as uintptr_t, - 0i32 as uintptr_t, - ); - } - i += 1 - } - match current_block { - 2927484062889439186 => {} - _ => { - if param_autoconfig.is_null() { - let mut url_2: - *mut libc::c_char = - dc_mprintf(b"http://autoconfig.%s/mail/config-v1.1.xml?emailaddress=%s\x00" - as - *const u8 - as - *const libc::c_char, - param_domain, - param_addr_urlencoded); - param_autoconfig = - moz_autoconfigure( - context, url_2, - param, - ); - free(url_2 - as - *mut libc::c_void); - if 0 != (*context) - .shall_stop_ongoing - { - current_block = - 2927484062889439186; - } else { - ((*context).cb)( - context, - Event::CONFIGURE_PROGRESS, - (if 340i32 - < - 1i32 - { - 1i32 - } else if 340i32 - > - 999i32 - { - 999i32 - } else { - 340i32 - }) - as - uintptr_t, - 0i32 - as - uintptr_t - ); - current_block - = - 10778260831612459202; - } - } else { - current_block = - 10778260831612459202; - } - match current_block { - 2927484062889439186 => {} - _ => { - if param_autoconfig - .is_null() - { - // do not transfer the email-address unencrypted - let mut url_3: - *mut libc::c_char = - dc_mprintf(b"http://%s/.well-known/autoconfig/mail/config-v1.1.xml\x00" - as - *const u8 - as - *const libc::c_char, - param_domain); - param_autoconfig - = - moz_autoconfigure(context, - url_3, - param); - free(url_3 - as - *mut libc::c_void); - if 0 - != - (*context).shall_stop_ongoing - { - current_block - = - 2927484062889439186; - } else { - ((*context).cb)(context, - Event::CONFIGURE_PROGRESS, - (if 350i32 - < - 1i32 - { - 1i32 - } else if 350i32 - > - 999i32 - { - 999i32 - } else { - 350i32 - }) - as - uintptr_t, - 0i32 - as - uintptr_t); - current_block - = - 5207889489643863322; - } - } else { - current_block - = - 5207889489643863322; - } - match current_block - { - 2927484062889439186 - => - { - } - _ - => - { - /* B. If we have no configuration yet, search configuration in Thunderbird's centeral database */ - if param_autoconfig.is_null() - { - /* always SSL for Thunderbird's database */ - let mut url_4: - *mut libc::c_char = - dc_mprintf(b"https://autoconfig.thunderbird.net/v1.1/%s\x00" - as - *const u8 - as - *const libc::c_char, - param_domain); - param_autoconfig - = - moz_autoconfigure(context, - url_4, - param); - free(url_4 - as - *mut libc::c_void); - if 0 - != - (*context).shall_stop_ongoing - { - current_block - = - 2927484062889439186; - } else { - ((*context).cb)(context, - Event::CONFIGURE_PROGRESS, - (if 500i32 - < - 1i32 - { - 1i32 - } else if 500i32 - > - 999i32 - { - 999i32 - } else { - 500i32 - }) - as - uintptr_t, - 0i32 - as - uintptr_t); - current_block - = - 2798392256336243897; - } - } else { - current_block - = - 2798392256336243897; - } - match current_block - { - 2927484062889439186 - => - { - } - _ - => - { - if !param_autoconfig.is_null() - { - let mut r: - *mut libc::c_char = - dc_loginparam_get_readable(param_autoconfig); - dc_log_info(context, - 0i32, - b"Got autoconfig: %s\x00" - as - *const u8 - as - *const libc::c_char, - r); - free(r - as - *mut libc::c_void); - if !(*param_autoconfig).mail_user.is_null() - { - free((*param).mail_user - as - *mut libc::c_void); - (*param).mail_user - = - dc_strdup_keep_null((*param_autoconfig).mail_user) - } - (*param).mail_server - = - dc_strdup_keep_null((*param_autoconfig).mail_server); - (*param).mail_port - = - (*param_autoconfig).mail_port; - (*param).send_server - = - dc_strdup_keep_null((*param_autoconfig).send_server); - (*param).send_port - = - (*param_autoconfig).send_port; - (*param).send_user - = - dc_strdup_keep_null((*param_autoconfig).send_user); - (*param).server_flags - = - (*param_autoconfig).server_flags - } - (*param).server_flags - |= - keep_flags; - current_block - = - 3024367268842933116; - } - } - } - } - } - } - } - } - } - } - } - } } else { - current_block = 3024367268842933116; + current_block = 13325891313334703151; } match current_block { 2927484062889439186 => {} _ => { - if (*param).mail_server.is_null() { - (*param).mail_server = dc_mprintf( - b"imap.%s\x00" as *const u8 - as *const libc::c_char, - param_domain, - ) - } - if (*param).mail_port == 0i32 { - (*param).mail_port = if 0 - != (*param).server_flags - & (0x100i32 | 0x400i32) - { - 143i32 + if param_autoconfig.is_null() { + // the doc does not mention `emailaddress=`, however, Thunderbird adds it, see https://releases.mozilla.org/pub/thunderbird/ , which makes some sense + let mut url_0: + *mut libc::c_char = + dc_mprintf(b"https://%s/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=%s\x00" + as + *const u8 + as + *const libc::c_char, + param_domain, + param_addr_urlencoded); + param_autoconfig = + moz_autoconfigure(context, url_0, param); + free(url_0 as *mut libc::c_void); + if 0 != context.shall_stop_ongoing { + current_block = 2927484062889439186; } else { - 993i32 - } - } - if (*param).mail_user.is_null() { - (*param).mail_user = dc_strdup((*param).addr) - } - if (*param).send_server.is_null() - && !(*param).mail_server.is_null() - { - (*param).send_server = - dc_strdup((*param).mail_server); - if strncmp( - (*param).send_server, - b"imap.\x00" as *const u8 - as *const libc::c_char, - 5, - ) == 0i32 - { - memcpy( - (*param).send_server - as *mut libc::c_void, - b"smtp\x00" as *const u8 - as *const libc::c_char - as *const libc::c_void, - 4, - ); - } - } - if (*param).send_port == 0i32 { - (*param).send_port = - if 0 != (*param).server_flags & 0x10000i32 { - 587i32 - } else if 0 - != (*param).server_flags & 0x40000i32 - { - 25i32 - } else { - 465i32 - } - } - if (*param).send_user.is_null() - && !(*param).mail_user.is_null() - { - (*param).send_user = - dc_strdup((*param).mail_user) - } - if (*param).send_pw.is_null() - && !(*param).mail_pw.is_null() - { - (*param).send_pw = dc_strdup((*param).mail_pw) - } - if 0 == dc_exactly_one_bit_set( - (*param).server_flags & (0x2i32 | 0x4i32), - ) { - (*param).server_flags &= !(0x2i32 | 0x4i32); - (*param).server_flags |= 0x4i32 - } - if 0 == dc_exactly_one_bit_set( - (*param).server_flags - & (0x100i32 | 0x200i32 | 0x400i32), - ) { - (*param).server_flags &= - !(0x100i32 | 0x200i32 | 0x400i32); - (*param).server_flags |= - if (*param).send_port == 143i32 { - 0x100i32 - } else { - 0x200i32 - } - } - if 0 == dc_exactly_one_bit_set( - (*param).server_flags - & (0x10000i32 | 0x20000i32 | 0x40000i32), - ) { - (*param).server_flags &= - !(0x10000i32 | 0x20000i32 | 0x40000i32); - (*param).server_flags |= - if (*param).send_port == 587i32 { - 0x10000i32 - } else if (*param).send_port == 25i32 { - 0x40000i32 - } else { - 0x20000i32 - } - } - /* do we have a complete configuration? */ - if (*param).addr.is_null() - || (*param).mail_server.is_null() - || (*param).mail_port == 0i32 - || (*param).mail_user.is_null() - || (*param).mail_pw.is_null() - || (*param).send_server.is_null() - || (*param).send_port == 0i32 - || (*param).send_user.is_null() - || (*param).send_pw.is_null() - || (*param).server_flags == 0i32 - { - dc_log_error( - context, - 0i32, - b"Account settings incomplete.\x00" - as *const u8 - as *const libc::c_char, - ); - } else if !(0 != (*context).shall_stop_ongoing) { - ((*context).cb)( - context, - Event::CONFIGURE_PROGRESS, - (if 600i32 < 1i32 { - 1i32 - } else if 600i32 > 999i32 { - 999i32 - } else { - 600i32 - }) - as uintptr_t, - 0i32 as uintptr_t, - ); - /* try to connect to IMAP - if we did not got an autoconfig, - do some further tries with different settings and username variations */ - let mut username_variation: libc::c_int = 0i32; - loop { - if !(username_variation <= 1i32) { - current_block = 14187386403465544025; - break; - } - let mut r_0: *mut libc::c_char = - dc_loginparam_get_readable(param); - dc_log_info( - context, - 0i32, - b"Trying: %s\x00" as *const u8 - as *const libc::c_char, - r_0, - ); - free(r_0 as *mut libc::c_void); - if 0 != dc_imap_connect( - (*context).inbox, - param, - ) { - current_block = 14187386403465544025; - break; - } - if !param_autoconfig.is_null() { - current_block = 2927484062889439186; - break; - } - // probe STARTTLS/993 - if 0 != (*context).shall_stop_ongoing { - current_block = 2927484062889439186; - break; - } - ((*context).cb)( + (context.cb)( context, Event::CONFIGURE_PROGRESS, - (if 650i32 + username_variation * 30i32 - < 1i32 - { + (if 310i32 < 1i32 { 1i32 - } else if 650i32 - + username_variation * 30i32 - > 999i32 - { + } else if 310i32 > 999i32 { 999i32 } else { - 650i32 + username_variation * 30i32 + 310i32 }) as uintptr_t, 0i32 as uintptr_t, ); - (*param).server_flags &= - !(0x100i32 | 0x200i32 | 0x400i32); - (*param).server_flags |= 0x100i32; - let mut r_1: *mut libc::c_char = - dc_loginparam_get_readable(param); - dc_log_info( - context, - 0i32, - b"Trying: %s\x00" as *const u8 - as *const libc::c_char, - r_1, - ); - free(r_1 as *mut libc::c_void); - if 0 != dc_imap_connect( - (*context).inbox, - param, - ) { - current_block = 14187386403465544025; - break; - } - // probe STARTTLS/143 - if 0 != (*context).shall_stop_ongoing { - current_block = 2927484062889439186; - break; - } - ((*context).cb)( - context, - Event::CONFIGURE_PROGRESS, - (if 660i32 + username_variation * 30i32 - < 1i32 - { - 1i32 - } else if 660i32 - + username_variation * 30i32 - > 999i32 - { - 999i32 - } else { - 660i32 + username_variation * 30i32 - }) - as uintptr_t, - 0i32 as uintptr_t, - ); - (*param).mail_port = 143i32; - let mut r_2: *mut libc::c_char = - dc_loginparam_get_readable(param); - dc_log_info( - context, - 0i32, - b"Trying: %s\x00" as *const u8 - as *const libc::c_char, - r_2, - ); - free(r_2 as *mut libc::c_void); - if 0 != dc_imap_connect( - (*context).inbox, - param, - ) { - current_block = 14187386403465544025; - break; - } - if 0 != username_variation { - current_block = 2927484062889439186; - break; - } - // next probe round with only the localpart of the email-address as the loginname - if 0 != (*context).shall_stop_ongoing { - current_block = 2927484062889439186; - break; - } - ((*context).cb)( - context, - Event::CONFIGURE_PROGRESS, - (if 670i32 + username_variation * 30i32 - < 1i32 - { - 1i32 - } else if 670i32 - + username_variation * 30i32 - > 999i32 - { - 999i32 - } else { - 670i32 + username_variation * 30i32 - }) - as uintptr_t, - 0i32 as uintptr_t, - ); - (*param).server_flags &= - !(0x100i32 | 0x200i32 | 0x400i32); - (*param).server_flags |= 0x200i32; - (*param).mail_port = 993i32; - let mut at: *mut libc::c_char = - strchr((*param).mail_user, '@' as i32); - if !at.is_null() { - *at = 0i32 as libc::c_char - } - at = strchr((*param).send_user, '@' as i32); - if !at.is_null() { - *at = 0i32 as libc::c_char - } - username_variation += 1 + current_block = 5597585068398118923; } - match current_block { - 2927484062889439186 => {} - _ => { - imap_connected_here = 1i32; - if !(0 != (*context).shall_stop_ongoing) - { - ((*context).cb)( + } else { + current_block = 5597585068398118923; + } + match current_block { + 2927484062889439186 => {} + _ => { + let mut i: libc::c_int = 0i32; + loop { + if !(i <= 1i32) { + current_block = + 12961834331865314435; + break; + } + if param_autoconfig.is_null() { + /* Outlook uses always SSL but different domains */ + let mut url_1: + *mut libc::c_char = + dc_mprintf(b"https://%s%s/autodiscover/autodiscover.xml\x00" + as + *const u8 + as + *const libc::c_char, + if i + == + 0i32 + { + b"\x00" + as + *const u8 + as + *const libc::c_char + } else { + b"autodiscover.\x00" + as + *const u8 + as + *const libc::c_char + }, + param_domain); + param_autoconfig = + outlk_autodiscover( + context, url_1, param, + ); + free(url_1 as *mut libc::c_void); + if 0 != context.shall_stop_ongoing { + current_block = + 2927484062889439186; + break; + } + (context.cb)( context, Event::CONFIGURE_PROGRESS, - (if 800i32 < 1i32 { + (if 320i32 + i * 10i32 < 1i32 { 1i32 - } else if 800i32 > 999i32 { + } else if 320i32 + i * 10i32 + > 999i32 + { 999i32 } else { - 800i32 + 320i32 + i * 10i32 }) as uintptr_t, 0i32 as uintptr_t, ); - /* try to connect to SMTP - if we did not got an autoconfig, the first try was SSL-465 and we do a second try with STARTTLS-587 */ - if 0 == dc_smtp_connect( - (*context).smtp, - param, - ) { - if !param_autoconfig.is_null() { - current_block = - 2927484062889439186; - } else if 0 - != (*context) - .shall_stop_ongoing + } + i += 1 + } + match current_block { + 2927484062889439186 => {} + _ => { + if param_autoconfig.is_null() { + let mut url_2: + *mut libc::c_char = + dc_mprintf(b"http://autoconfig.%s/mail/config-v1.1.xml?emailaddress=%s\x00" + as + *const u8 + as + *const libc::c_char, + param_domain, + param_addr_urlencoded); + param_autoconfig = + moz_autoconfigure( + context, url_2, param, + ); + free( + url_2 as *mut libc::c_void, + ); + if 0 != context + .shall_stop_ongoing { current_block = 2927484062889439186; } else { - ((*context).cb)(context, - Event::CONFIGURE_PROGRESS, - (if 850i32 - < - 1i32 - { - 1i32 - } else if 850i32 - > - 999i32 - { - 999i32 - } else { - 850i32 - }) - as - uintptr_t, - 0i32 - as - uintptr_t); - (*param).server_flags &= - !(0x10000i32 - | 0x20000i32 - | 0x40000i32); - (*param).server_flags |= - 0x10000i32; - (*param).send_port = 587i32; - let mut r_3: - *mut libc::c_char = - dc_loginparam_get_readable(param); - dc_log_info(context, - 0i32, - b"Trying: %s\x00" - as - *const u8 - as - *const libc::c_char, - r_3); - free(r_3 - as - *mut libc::c_void); - if 0 == dc_smtp_connect( - (*context).smtp, - param, - ) { - if 0 != (*context) + (context.cb)( + context, + Event::CONFIGURE_PROGRESS, + (if 340i32 + < + 1i32 + { + 1i32 + } else if 340i32 + > + 999i32 + { + 999i32 + } else { + 340i32 + }) + as + uintptr_t, + 0i32 + as + uintptr_t + ); + current_block = + 10778260831612459202; + } + } else { + current_block = + 10778260831612459202; + } + match current_block { + 2927484062889439186 => {} + _ => { + if param_autoconfig + .is_null() + { + // do not transfer the email-address unencrypted + let mut url_3: + *mut libc::c_char = + dc_mprintf(b"http://%s/.well-known/autoconfig/mail/config-v1.1.xml\x00" + as + *const u8 + as + *const libc::c_char, + param_domain); + param_autoconfig = + moz_autoconfigure( + context, url_3, + param, + ); + free(url_3 + as + *mut libc::c_void); + if 0 != context .shall_stop_ongoing { current_block = 2927484062889439186; } else { - ((*context).cb)(context, - Event::CONFIGURE_PROGRESS, - (if 860i32 - < - 1i32 - { - 1i32 - } else if 860i32 - > - 999i32 - { - 999i32 - } else { - 860i32 - }) - as - uintptr_t, - 0i32 - as - uintptr_t); - (*param).server_flags - &= - !(0x10000i32 - | - 0x20000i32 - | - 0x40000i32); - (*param).server_flags - |= - 0x10000i32; - (*param) - .send_port = - 25i32; - let mut r_4: - *mut libc::c_char = - dc_loginparam_get_readable(param); - dc_log_info(context, - 0i32, - b"Trying: %s\x00" - as - *const u8 - as - *const libc::c_char, - r_4); - free(r_4 - as - *mut libc::c_void); - if 0 - == - dc_smtp_connect((*context).smtp, - param) - { - current_block - = - 2927484062889439186; - } else { - current_block - = - 5083741289379115417; - } + (context.cb)(context, + Event::CONFIGURE_PROGRESS, + (if 350i32 + < + 1i32 + { + 1i32 + } else if 350i32 + > + 999i32 + { + 999i32 + } else { + 350i32 + }) + as + uintptr_t, + 0i32 + as + uintptr_t); + current_block + = + 5207889489643863322; } } else { current_block = - 5083741289379115417; + 5207889489643863322; + } + match current_block { + 2927484062889439186 => { + } + _ => { + /* B. If we have no configuration yet, search configuration in Thunderbird's centeral database */ + if param_autoconfig + .is_null() + { + /* always SSL for Thunderbird's database */ + let mut url_4: + *mut libc::c_char = + dc_mprintf(b"https://autoconfig.thunderbird.net/v1.1/%s\x00" + as + *const u8 + as + *const libc::c_char, + param_domain); + param_autoconfig + = + moz_autoconfigure(context, + url_4, + param); + free(url_4 + as + *mut libc::c_void); + if 0 + != + context.shall_stop_ongoing + { + current_block + = + 2927484062889439186; + } else { + (context.cb)(context, + Event::CONFIGURE_PROGRESS, + (if 500i32 + < + 1i32 + { + 1i32 + } else if 500i32 + > + 999i32 + { + 999i32 + } else { + 500i32 + }) + as + uintptr_t, + 0i32 + as + uintptr_t); + current_block + = + 2798392256336243897; + } + } else { + current_block + = + 2798392256336243897; + } + match current_block + { + 2927484062889439186 + => + { + } + _ + => + { + if !param_autoconfig.is_null() + { + let mut r: + *mut libc::c_char = + dc_loginparam_get_readable(param_autoconfig); + dc_log_info(context, + 0i32, + b"Got autoconfig: %s\x00" + as + *const u8 + as + *const libc::c_char, + r); + free(r + as + *mut libc::c_void); + if !(*param_autoconfig).mail_user.is_null() + { + free((*param).mail_user + as + *mut libc::c_void); + (*param).mail_user + = + dc_strdup_keep_null((*param_autoconfig).mail_user) + } + (*param).mail_server + = + dc_strdup_keep_null((*param_autoconfig).mail_server); + (*param).mail_port + = + (*param_autoconfig).mail_port; + (*param).send_server + = + dc_strdup_keep_null((*param_autoconfig).send_server); + (*param).send_port + = + (*param_autoconfig).send_port; + (*param).send_user + = + dc_strdup_keep_null((*param_autoconfig).send_user); + (*param).server_flags + = + (*param_autoconfig).server_flags + } + (*param).server_flags + |= + keep_flags; + current_block + = + 3024367268842933116; + } + } + } } } - } else { - current_block = - 5083741289379115417; } - match current_block { - 2927484062889439186 => {} - _ => { - smtp_connected_here = 1i32; + } + } + } + } + } + } + } else { + current_block = 3024367268842933116; + } + match current_block { + 2927484062889439186 => {} + _ => { + if (*param).mail_server.is_null() { + (*param).mail_server = dc_mprintf( + b"imap.%s\x00" as *const u8 + as *const libc::c_char, + param_domain, + ) + } + if (*param).mail_port == 0i32 { + (*param).mail_port = if 0 + != (*param).server_flags & (0x100i32 | 0x400i32) + { + 143i32 + } else { + 993i32 + } + } + if (*param).mail_user.is_null() { + (*param).mail_user = dc_strdup((*param).addr) + } + if (*param).send_server.is_null() + && !(*param).mail_server.is_null() + { + (*param).send_server = + dc_strdup((*param).mail_server); + if strncmp( + (*param).send_server, + b"imap.\x00" as *const u8 + as *const libc::c_char, + 5, + ) == 0i32 + { + memcpy( + (*param).send_server as *mut libc::c_void, + b"smtp\x00" as *const u8 + as *const libc::c_char + as *const libc::c_void, + 4, + ); + } + } + if (*param).send_port == 0i32 { + (*param).send_port = if 0 + != (*param).server_flags & 0x10000i32 + { + 587i32 + } else if 0 != (*param).server_flags & 0x40000i32 { + 25i32 + } else { + 465i32 + } + } + if (*param).send_user.is_null() + && !(*param).mail_user.is_null() + { + (*param).send_user = dc_strdup((*param).mail_user) + } + if (*param).send_pw.is_null() + && !(*param).mail_pw.is_null() + { + (*param).send_pw = dc_strdup((*param).mail_pw) + } + if 0 == dc_exactly_one_bit_set( + (*param).server_flags & (0x2i32 | 0x4i32), + ) { + (*param).server_flags &= !(0x2i32 | 0x4i32); + (*param).server_flags |= 0x4i32 + } + if 0 == dc_exactly_one_bit_set( + (*param).server_flags + & (0x100i32 | 0x200i32 | 0x400i32), + ) { + (*param).server_flags &= + !(0x100i32 | 0x200i32 | 0x400i32); + (*param).server_flags |= + if (*param).send_port == 143i32 { + 0x100i32 + } else { + 0x200i32 + } + } + if 0 == dc_exactly_one_bit_set( + (*param).server_flags + & (0x10000i32 | 0x20000i32 | 0x40000i32), + ) { + (*param).server_flags &= + !(0x10000i32 | 0x20000i32 | 0x40000i32); + (*param).server_flags |= + if (*param).send_port == 587i32 { + 0x10000i32 + } else if (*param).send_port == 25i32 { + 0x40000i32 + } else { + 0x20000i32 + } + } + /* do we have a complete configuration? */ + if (*param).addr.is_null() + || (*param).mail_server.is_null() + || (*param).mail_port == 0i32 + || (*param).mail_user.is_null() + || (*param).mail_pw.is_null() + || (*param).send_server.is_null() + || (*param).send_port == 0i32 + || (*param).send_user.is_null() + || (*param).send_pw.is_null() + || (*param).server_flags == 0i32 + { + dc_log_error( + context, + 0i32, + b"Account settings incomplete.\x00" as *const u8 + as *const libc::c_char, + ); + } else if !(0 != context.shall_stop_ongoing) { + (context.cb)( + context, + Event::CONFIGURE_PROGRESS, + (if 600i32 < 1i32 { + 1i32 + } else if 600i32 > 999i32 { + 999i32 + } else { + 600i32 + }) + as uintptr_t, + 0i32 as uintptr_t, + ); + /* try to connect to IMAP - if we did not got an autoconfig, + do some further tries with different settings and username variations */ + let mut username_variation: libc::c_int = 0i32; + loop { + if !(username_variation <= 1i32) { + current_block = 14187386403465544025; + break; + } + let mut r_0: *mut libc::c_char = + dc_loginparam_get_readable(param); + dc_log_info( + context, + 0i32, + b"Trying: %s\x00" as *const u8 + as *const libc::c_char, + r_0, + ); + free(r_0 as *mut libc::c_void); + if 0 != dc_imap_connect( + context, + &mut context.inbox.clone().lock().unwrap(), + param, + ) { + current_block = 14187386403465544025; + break; + } + if !param_autoconfig.is_null() { + current_block = 2927484062889439186; + break; + } + // probe STARTTLS/993 + if 0 != context.shall_stop_ongoing { + current_block = 2927484062889439186; + break; + } + (context.cb)( + context, + Event::CONFIGURE_PROGRESS, + (if 650i32 + username_variation * 30i32 + < 1i32 + { + 1i32 + } else if 650i32 + + username_variation * 30i32 + > 999i32 + { + 999i32 + } else { + 650i32 + username_variation * 30i32 + }) + as uintptr_t, + 0i32 as uintptr_t, + ); + (*param).server_flags &= + !(0x100i32 | 0x200i32 | 0x400i32); + (*param).server_flags |= 0x100i32; + let mut r_1: *mut libc::c_char = + dc_loginparam_get_readable(param); + dc_log_info( + context, + 0i32, + b"Trying: %s\x00" as *const u8 + as *const libc::c_char, + r_1, + ); + free(r_1 as *mut libc::c_void); + if 0 != dc_imap_connect( + context, + &mut context.inbox.clone().lock().unwrap(), + param, + ) { + current_block = 14187386403465544025; + break; + } + // probe STARTTLS/143 + if 0 != context.shall_stop_ongoing { + current_block = 2927484062889439186; + break; + } + (context.cb)( + context, + Event::CONFIGURE_PROGRESS, + (if 660i32 + username_variation * 30i32 + < 1i32 + { + 1i32 + } else if 660i32 + + username_variation * 30i32 + > 999i32 + { + 999i32 + } else { + 660i32 + username_variation * 30i32 + }) + as uintptr_t, + 0i32 as uintptr_t, + ); + (*param).mail_port = 143i32; + let mut r_2: *mut libc::c_char = + dc_loginparam_get_readable(param); + dc_log_info( + context, + 0i32, + b"Trying: %s\x00" as *const u8 + as *const libc::c_char, + r_2, + ); + free(r_2 as *mut libc::c_void); + if 0 != dc_imap_connect( + context, + &mut context.inbox.clone().lock().unwrap(), + param, + ) { + current_block = 14187386403465544025; + break; + } + if 0 != username_variation { + current_block = 2927484062889439186; + break; + } + // next probe round with only the localpart of the email-address as the loginname + if 0 != context.shall_stop_ongoing { + current_block = 2927484062889439186; + break; + } + (context.cb)( + context, + Event::CONFIGURE_PROGRESS, + (if 670i32 + username_variation * 30i32 + < 1i32 + { + 1i32 + } else if 670i32 + + username_variation * 30i32 + > 999i32 + { + 999i32 + } else { + 670i32 + username_variation * 30i32 + }) + as uintptr_t, + 0i32 as uintptr_t, + ); + (*param).server_flags &= + !(0x100i32 | 0x200i32 | 0x400i32); + (*param).server_flags |= 0x200i32; + (*param).mail_port = 993i32; + let mut at: *mut libc::c_char = + strchr((*param).mail_user, '@' as i32); + if !at.is_null() { + *at = 0i32 as libc::c_char + } + at = strchr((*param).send_user, '@' as i32); + if !at.is_null() { + *at = 0i32 as libc::c_char + } + username_variation += 1 + } + match current_block { + 2927484062889439186 => {} + _ => { + imap_connected_here = 1i32; + if !(0 != context.shall_stop_ongoing) { + (context.cb)( + context, + Event::CONFIGURE_PROGRESS, + (if 800i32 < 1i32 { + 1i32 + } else if 800i32 > 999i32 { + 999i32 + } else { + 800i32 + }) + as uintptr_t, + 0i32 as uintptr_t, + ); + /* try to connect to SMTP - if we did not got an autoconfig, the first try was SSL-465 and we do a second try with STARTTLS-587 */ + if 0 == dc_smtp_connect( + &mut context + .smtp + .clone() + .lock() + .unwrap(), + param, + ) { + if !param_autoconfig.is_null() { + current_block = + 2927484062889439186; + } else if 0 + != context.shall_stop_ongoing + { + current_block = + 2927484062889439186; + } else { + (context.cb)( + context, + Event::CONFIGURE_PROGRESS, + (if 850i32 < 1i32 { + 1i32 + } else if 850i32 > 999i32 { + 999i32 + } else { + 850i32 + }) + as uintptr_t, + 0i32 as uintptr_t, + ); + (*param).server_flags &= + !(0x10000i32 + | 0x20000i32 + | 0x40000i32); + (*param).server_flags |= + 0x10000i32; + (*param).send_port = 587i32; + let mut r_3: *mut libc::c_char = + dc_loginparam_get_readable( + param, + ); + dc_log_info( + context, + 0i32, + b"Trying: %s\x00" + as *const u8 + as *const libc::c_char, + r_3, + ); + free(r_3 as *mut libc::c_void); + if 0 == dc_smtp_connect( + &mut context + .smtp + .clone() + .lock() + .unwrap(), + param, + ) { + if 0 != context + .shall_stop_ongoing + { + current_block = + 2927484062889439186; + } else { + (context.cb)(context, + Event::CONFIGURE_PROGRESS, + (if 860i32 + < + 1i32 + { + 1i32 + } else if 860i32 + > + 999i32 + { + 999i32 + } else { + 860i32 + }) + as + uintptr_t, + 0i32 + as + uintptr_t); + (*param) + .server_flags &= + !(0x10000i32 + | 0x20000i32 + | 0x40000i32); + (*param) + .server_flags |= + 0x10000i32; + (*param).send_port = + 25i32; + let mut r_4: + *mut libc::c_char = + dc_loginparam_get_readable(param); + dc_log_info(context, + 0i32, + b"Trying: %s\x00" + as + *const u8 + as + *const libc::c_char, + r_4); + free(r_4 + as + *mut libc::c_void); + if 0 == dc_smtp_connect( + &mut context + .smtp + .clone() + .lock() + .unwrap(), + param, + ) { + current_block + = + 2927484062889439186; + } else { + current_block + = + 5083741289379115417; + } + } + } else { + current_block = + 5083741289379115417; + } + } + } else { + current_block = 5083741289379115417; + } + match current_block { + 2927484062889439186 => {} + _ => { + smtp_connected_here = 1i32; + if !(0 + != context + .shall_stop_ongoing) + { + (context.cb)(context, + Event::CONFIGURE_PROGRESS, + (if 900i32 + < + 1i32 + { + 1i32 + } else if 900i32 + > + 999i32 + { + 999i32 + } else { + 900i32 + }) + as + uintptr_t, + 0i32 + as + uintptr_t); + flags + = + if 0 + != + dc_sqlite3_get_config_int(&mut context.sql.lock().unwrap(), + b"mvbox_watch\x00" + as + *const u8 + as + *const libc::c_char, + 1i32) + || + 0 + != + dc_sqlite3_get_config_int(&mut context.sql.lock().unwrap(), + b"mvbox_move\x00" + as + *const u8 + as + *const libc::c_char, + 1i32) + { + 0x1i32 + } else { + 0i32 + }; + dc_configure_folders( + context, + &mut context + .inbox + .clone() + .lock() + .unwrap(), + flags, + ); if !(0 - != (*context) + != context .shall_stop_ongoing) { - ((*context).cb)(context, - Event::CONFIGURE_PROGRESS, - (if 900i32 - < - 1i32 - { - 1i32 - } else if 900i32 - > - 999i32 - { - 999i32 - } else { - 900i32 - }) - as - uintptr_t, - 0i32 - as - uintptr_t); - flags - = - if 0 - != - dc_sqlite3_get_config_int((*context).sql, - b"mvbox_watch\x00" - as - *const u8 - as - *const libc::c_char, - 1i32) - || - 0 - != - dc_sqlite3_get_config_int((*context).sql, - b"mvbox_move\x00" - as - *const u8 - as - *const libc::c_char, - 1i32) - { - 0x1i32 - } else { - 0i32 - }; - dc_configure_folders( - context, - (*context).inbox, - flags, - ); - if !(0 != (*context) - .shall_stop_ongoing) - { - ((*context).cb)(context, - Event::CONFIGURE_PROGRESS, - (if 910i32 - < - 1i32 - { - 1i32 - } else if 910i32 - > - 999i32 - { - 999i32 - } else { - 910i32 - }) - as - uintptr_t, - 0i32 - as - uintptr_t); - dc_loginparam_write(param, - (*context).sql, - b"configured_\x00" - as - *const u8 - as - *const libc::c_char); - dc_sqlite3_set_config_int((*context).sql, - b"configured\x00" - as - *const u8 - as - *const libc::c_char, - 1i32); - if !(0 - != - (*context).shall_stop_ongoing) - { - ((*context).cb)(context, - Event::CONFIGURE_PROGRESS, - (if 920i32 - < - 1i32 - { - 1i32 - } else if 920i32 - > - 999i32 - { - 999i32 - } else { - 920i32 - }) - as - uintptr_t, - 0i32 - as - uintptr_t); - dc_ensure_secret_key_exists(context); - success - = - 1i32; - dc_log_info(context, - 0i32, - b"Configure completed.\x00" + (context.cb)(context, + Event::CONFIGURE_PROGRESS, + (if 910i32 + < + 1i32 + { + 1i32 + } else if 910i32 + > + 999i32 + { + 999i32 + } else { + 910i32 + }) + as + uintptr_t, + 0i32 + as + uintptr_t); + dc_loginparam_write(param, + &mut context.sql.lock().unwrap(), + b"configured_\x00" as *const u8 as *const libc::c_char); - if !(0 - != - (*context).shall_stop_ongoing) - { - ((*context).cb)(context, - Event::CONFIGURE_PROGRESS, - (if 940i32 - < - 1i32 - { - 1i32 - } else if 940i32 - > - 999i32 - { - 999i32 - } else { - 940i32 - }) - as - uintptr_t, - 0i32 - as - uintptr_t); - } + dc_sqlite3_set_config_int(&mut context.sql.lock().unwrap(), + b"configured\x00" + as + *const u8 + as + *const libc::c_char, + 1i32); + if !(0 != context + .shall_stop_ongoing) + { + (context.cb)(context, + Event::CONFIGURE_PROGRESS, + (if 920i32 + < + 1i32 + { + 1i32 + } else if 920i32 + > + 999i32 + { + 999i32 + } else { + 920i32 + }) + as + uintptr_t, + 0i32 + as + uintptr_t); + dc_ensure_secret_key_exists(context); + success = 1i32; + dc_log_info(context, + 0i32, + b"Configure completed.\x00" + as + *const u8 + as + *const libc::c_char); + if !(0 + != + context.shall_stop_ongoing) + { + (context.cb)(context, + Event::CONFIGURE_PROGRESS, + (if 940i32 + < + 1i32 + { + 1i32 + } else if 940i32 + > + 999i32 + { + 999i32 + } else { + 940i32 + }) + as + uintptr_t, + 0i32 + as + uintptr_t); } } } @@ -1302,11 +1288,12 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut } } } + if 0 != imap_connected_here { - dc_imap_disconnect((*context).inbox); + dc_imap_disconnect(context, &mut context.inbox.clone().lock().unwrap()); } if 0 != smtp_connected_here { - dc_smtp_disconnect((*context).smtp); + dc_smtp_disconnect(&mut context.smtp.clone().lock().unwrap()); } dc_loginparam_unref(param); dc_loginparam_unref(param_autoconfig); @@ -1315,7 +1302,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut dc_free_ongoing(context); } free(mvbox_folder as *mut libc::c_void); - ((*context).cb)( + (context.cb)( context, Event::CONFIGURE_PROGRESS, (if 0 != success { 1000i32 } else { 0i32 }) as uintptr_t, @@ -1324,29 +1311,26 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut } pub unsafe fn dc_free_ongoing(mut context: &dc_context_t) { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return; - } - (*context).ongoing_running = 0i32; - (*context).shall_stop_ongoing = 1i32; + context.ongoing_running = 0i32; + context.shall_stop_ongoing = 1i32; } pub unsafe fn dc_configure_folders( - mut context: &dc_context_t, - mut imap: *mut dc_imap_t, - mut flags: libc::c_int, + context: &dc_context_t, + imap: &mut dc_imap_t, + flags: libc::c_int, ) { let mut folder_list: *mut clist = 0 as *mut clist; let mut iter: *mut clistiter = 0 as *mut clistiter; let mut mvbox_folder: *mut libc::c_char = 0 as *mut libc::c_char; let mut sentbox_folder: *mut libc::c_char = 0 as *mut libc::c_char; let mut fallback_folder: *mut libc::c_char = 0 as *mut libc::c_char; - if !(imap.is_null() || (*imap).etpan.is_null()) { + if !(*imap).etpan.is_null() { dc_log_info( context, 0i32, b"Configuring IMAP-folders.\x00" as *const u8 as *const libc::c_char, ); - folder_list = list_folders(imap); + folder_list = list_folders(context, imap); fallback_folder = dc_mprintf( b"INBOX%c%s\x00" as *const u8 as *const libc::c_char, (*imap).imap_delimiter as libc::c_int, @@ -1391,7 +1375,7 @@ pub unsafe fn dc_configure_folders( (*imap).etpan, b"DeltaChat\x00" as *const u8 as *const libc::c_char, ); - if 0 != dc_imap_is_error(imap, r) { + if 0 != dc_imap_is_error(context, imap, r) { dc_log_warning( context, 0i32, @@ -1399,7 +1383,7 @@ pub unsafe fn dc_configure_folders( as *const libc::c_char, ); r = mailimap_create((*imap).etpan, fallback_folder); - if 0 != dc_imap_is_error(imap, r) { + if 0 != dc_imap_is_error(context, imap, r) { dc_log_warning( context, 0i32, @@ -1425,17 +1409,19 @@ pub unsafe fn dc_configure_folders( mailimap_subscribe((*imap).etpan, mvbox_folder); } dc_sqlite3_set_config_int( - (*context).sql, + &mut context.sql.lock().unwrap(), b"folders_configured\x00" as *const u8 as *const libc::c_char, 3i32, ); dc_sqlite3_set_config( - (*context).sql, + context, + &mut context.sql.lock().unwrap(), b"configured_mvbox_folder\x00" as *const u8 as *const libc::c_char, mvbox_folder, ); dc_sqlite3_set_config( - (*context).sql, + context, + &mut context.sql.lock().unwrap(), b"configured_sentbox_folder\x00" as *const u8 as *const libc::c_char, sentbox_folder, ); @@ -1466,13 +1452,13 @@ unsafe fn free_folders(mut folders: *mut clist) { clist_free(folders); }; } -unsafe fn list_folders(mut imap: *mut dc_imap_t) -> *mut clist { +unsafe fn list_folders(context: &dc_context_t, imap: &mut dc_imap_t) -> *mut clist { let mut imap_list: *mut clist = 0 as *mut clist; let mut iter1: *mut clistiter = 0 as *mut clistiter; let mut ret_list: *mut clist = clist_new(); let mut r: libc::c_int = 0i32; let mut xlist_works: libc::c_int = 0i32; - if !(imap.is_null() || (*imap).etpan.is_null()) { + if !(*imap).etpan.is_null() { if 0 != (*imap).has_xlist { r = mailimap_xlist( (*imap).etpan, @@ -1488,16 +1474,16 @@ unsafe fn list_folders(mut imap: *mut dc_imap_t) -> *mut clist { &mut imap_list, ) } - if 0 != dc_imap_is_error(imap, r) || imap_list.is_null() { + if 0 != dc_imap_is_error(context, imap, r) || imap_list.is_null() { imap_list = 0 as *mut clist; dc_log_warning( - (*imap).context, + context, 0i32, b"Cannot get folder list.\x00" as *const u8 as *const libc::c_char, ); } else if (*imap_list).count <= 0i32 { dc_log_warning( - (*imap).context, + context, 0i32, b"Folder list is empty.\x00" as *const u8 as *const libc::c_char, ); @@ -1869,7 +1855,7 @@ unsafe fn read_autoconf_file( b"Testing %s ...\x00" as *const u8 as *const libc::c_char, url, ); - filecontent = ((*context).cb)( + filecontent = (context.cb)( context, Event::HTTP_GET, url as uintptr_t, @@ -1887,9 +1873,9 @@ unsafe fn read_autoconf_file( return filecontent; } unsafe fn outlk_autodiscover( - mut context: &dc_context_t, - mut url__: *const libc::c_char, - mut param_in: *const dc_loginparam_t, + context: &dc_context_t, + url__: *const libc::c_char, + param_in: *const dc_loginparam_t, ) -> *mut dc_loginparam_t { let mut current_block: u64; let mut xml_raw: *mut libc::c_char = 0 as *mut libc::c_char; @@ -2071,10 +2057,7 @@ unsafe fn outlk_autodiscover_starttag_cb( (*outlk_ad).tag_config = 5i32 }; } -pub unsafe fn dc_alloc_ongoing(mut context: &dc_context_t) -> libc::c_int { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return 0i32; - } +pub unsafe fn dc_alloc_ongoing(context: &dc_context_t) -> libc::c_int { if 0 != dc_has_ongoing(context) { dc_log_warning( context, @@ -2084,43 +2067,37 @@ pub unsafe fn dc_alloc_ongoing(mut context: &dc_context_t) -> libc::c_int { ); return 0i32; } - (*context).ongoing_running = 1i32; - (*context).shall_stop_ongoing = 0i32; + context.ongoing_running = 1i32; + context.shall_stop_ongoing = 0i32; return 1i32; } pub unsafe fn dc_connect_to_configured_imap( - mut context: &dc_context_t, - mut imap: *mut dc_imap_t, + context: &dc_context_t, + imap: &mut dc_imap_t, ) -> libc::c_int { let mut ret_connected: libc::c_int = 0i32; let mut param: *mut dc_loginparam_t = dc_loginparam_new(); - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint || imap.is_null() { - dc_log_warning( - (*imap).context, - 0i32, - b"Cannot connect to IMAP: Bad parameters.\x00" as *const u8 as *const libc::c_char, - ); - } else if 0 != dc_imap_is_connected(imap) { + if 0 != dc_imap_is_connected(imap) { ret_connected = 1i32 } else if dc_sqlite3_get_config_int( - (*(*imap).context).sql, + &mut context.sql.clone().lock().unwrap(), b"configured\x00" as *const u8 as *const libc::c_char, 0i32, ) == 0i32 { dc_log_warning( - (*imap).context, + context, 0i32, b"Not configured, cannot connect.\x00" as *const u8 as *const libc::c_char, ); } else { dc_loginparam_read( param, - (*(*imap).context).sql, + &mut context.sql.clone().lock().unwrap(), b"configured_\x00" as *const u8 as *const libc::c_char, ); /*the trailing underscore is correct*/ - if !(0 == dc_imap_connect(imap, param)) { + if !(0 == dc_imap_connect(context, imap, param)) { ret_connected = 2i32 } } diff --git a/src/dc_contact.rs b/src/dc_contact.rs index 81c463f07..9635dd78e 100644 --- a/src/dc_contact.rs +++ b/src/dc_contact.rs @@ -9,7 +9,6 @@ use crate::dc_e2ee::*; use crate::dc_key::*; use crate::dc_log::*; use crate::dc_loginparam::*; -use crate::dc_pgp::*; use crate::dc_sqlite3::*; use crate::dc_stock::*; use crate::dc_strbuilder::*; @@ -19,9 +18,9 @@ use crate::x::*; #[derive(Copy, Clone)] #[repr(C)] -pub struct dc_contact_t { +pub struct dc_contact_t<'a> { pub magic: uint32_t, - pub context: *mut dc_context_t, + pub context: &'a dc_context_t, pub id: uint32_t, pub name: *mut libc::c_char, pub authname: *mut libc::c_char, @@ -30,12 +29,9 @@ pub struct dc_contact_t { pub origin: libc::c_int, } -pub unsafe fn dc_marknoticed_contact(mut context: &dc_context_t, mut contact_id: uint32_t) { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return; - } +pub unsafe fn dc_marknoticed_contact(context: &dc_context_t, contact_id: uint32_t) { let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"UPDATE msgs SET state=13 WHERE from_id=? AND state=10;\x00" as *const u8 as *const libc::c_char, ); @@ -76,14 +72,10 @@ pub unsafe fn dc_lookup_contact_id_by_addr( let mut addr_normalized: *mut libc::c_char = 0 as *mut libc::c_char; let mut addr_self: *mut libc::c_char = 0 as *mut libc::c_char; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || addr.is_null() - || *addr.offset(0isize) as libc::c_int == 0i32) - { + if !(addr.is_null() || *addr.offset(0isize) as libc::c_int == 0i32) { addr_normalized = dc_addr_normalize(addr); addr_self = dc_sqlite3_get_config( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"configured_addr\x00" as *const u8 as *const libc::c_char, b"\x00" as *const u8 as *const libc::c_char, ); @@ -91,7 +83,7 @@ pub unsafe fn dc_lookup_contact_id_by_addr( contact_id = 1i32 } else { stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.clone().lock().unwrap(), b"SELECT id FROM contacts WHERE addr=?1 COLLATE NOCASE AND id>?2 AND origin>=?3 AND blocked=0;\x00" as *const u8 as *const libc::c_char); sqlite3_bind_text( @@ -137,11 +129,7 @@ pub unsafe fn dc_create_contact( let mut contact_id: uint32_t = 0i32 as uint32_t; let mut sth_modified: libc::c_int = 0i32; let mut blocked: libc::c_int = 0i32; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || addr.is_null() - || *addr.offset(0isize) as libc::c_int == 0i32) - { + if !(addr.is_null() || *addr.offset(0isize) as libc::c_int == 0i32) { contact_id = dc_add_or_lookup_contact(context, name, addr, 0x4000000i32, &mut sth_modified); blocked = dc_is_contact_blocked(context, contact_id); ((*context).cb)( @@ -169,15 +157,15 @@ pub unsafe fn dc_block_contact( let mut send_event: libc::c_int = 0i32; let mut contact: *mut dc_contact_t = dc_contact_new(context); let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || contact_id <= 9i32 as libc::c_uint) - { - if 0 != dc_contact_load_from_db(contact, (*context).sql, contact_id) - && (*contact).blocked != new_blocking + if !(contact_id <= 9i32 as libc::c_uint) { + if 0 != dc_contact_load_from_db( + contact, + &mut context.sql.clone().lock().unwrap(), + contact_id, + ) && (*contact).blocked != new_blocking { stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"UPDATE contacts SET blocked=? WHERE id=?;\x00" as *const u8 as *const libc::c_char, ); @@ -189,7 +177,7 @@ pub unsafe fn dc_block_contact( sqlite3_finalize(stmt); stmt = 0 as *mut sqlite3_stmt; stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.clone().lock().unwrap(), b"UPDATE chats SET blocked=? WHERE type=? AND id IN (SELECT chat_id FROM chats_contacts WHERE contact_id=?);\x00" as *const u8 as *const libc::c_char); @@ -243,7 +231,7 @@ pub unsafe fn dc_block_contact( * dc_create_contact() or dc_add_address_book()) * only affect the given-name. */ -pub unsafe fn dc_contact_new(mut context: &dc_context_t) -> *mut dc_contact_t { +pub unsafe fn dc_contact_new<'a>(context: &'a dc_context_t) -> *mut dc_contact_t<'a> { let mut contact: *mut dc_contact_t = 0 as *mut dc_contact_t; contact = calloc(1, ::std::mem::size_of::()) as *mut dc_contact_t; if contact.is_null() { @@ -253,7 +241,7 @@ pub unsafe fn dc_contact_new(mut context: &dc_context_t) -> *mut dc_contact_t { (*contact).context = context; return contact; } -pub unsafe fn dc_contact_unref(mut contact: *mut dc_contact_t) { +pub unsafe fn dc_contact_unref(contact: *mut dc_contact_t) { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return; } @@ -296,13 +284,13 @@ pub unsafe fn dc_contact_empty(mut contact: *mut dc_contact_t) { /* contacts with at least this origin value start a new "normal" chat, defaults to off */ pub unsafe fn dc_contact_load_from_db( mut contact: *mut dc_contact_t, - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, mut contact_id: uint32_t, ) -> libc::c_int { let mut current_block: u64; let mut success: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint || sql.is_null()) { + if !(contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint) { dc_contact_empty(contact); if contact_id == 1i32 as libc::c_uint { (*contact).id = contact_id; @@ -346,7 +334,11 @@ pub unsafe fn dc_is_contact_blocked( ) -> libc::c_int { let mut is_blocked: libc::c_int = 0i32; let mut contact: *mut dc_contact_t = dc_contact_new(context); - if 0 != dc_contact_load_from_db(contact, (*context).sql, contact_id) { + if 0 != dc_contact_load_from_db( + contact, + &mut context.sql.clone().lock().unwrap(), + contact_id, + ) { if 0 != (*contact).blocked { is_blocked = 1i32 } @@ -374,14 +366,10 @@ pub unsafe fn dc_add_or_lookup_contact( sth_modified = &mut dummy } *sth_modified = 0i32; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || addr__.is_null() - || origin <= 0i32) - { + if !(addr__.is_null() || origin <= 0i32) { addr = dc_addr_normalize(addr__); addr_self = dc_sqlite3_get_config( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"configured_addr\x00" as *const u8 as *const libc::c_char, b"\x00" as *const u8 as *const libc::c_char, ); @@ -401,7 +389,7 @@ pub unsafe fn dc_add_or_lookup_contact( ); } else { stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.clone().lock().unwrap(), b"SELECT id, name, addr, origin, authname FROM contacts WHERE addr=? COLLATE NOCASE;\x00" as *const u8 as *const libc::c_char); sqlite3_bind_text(stmt, 1i32, addr as *const libc::c_char, -1i32, None); @@ -438,7 +426,7 @@ pub unsafe fn dc_add_or_lookup_contact( || origin > row_origin { stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"UPDATE contacts SET name=?, addr=?, origin=?, authname=? WHERE id=?;\x00" as *const u8 as *const libc::c_char, ); @@ -482,7 +470,7 @@ pub unsafe fn dc_add_or_lookup_contact( stmt = 0 as *mut sqlite3_stmt; if 0 != update_name { stmt = - dc_sqlite3_prepare((*context).sql, + dc_sqlite3_prepare(&mut context.sql.clone().lock().unwrap(), b"UPDATE chats SET name=? WHERE type=? AND id IN(SELECT chat_id FROM chats_contacts WHERE contact_id=?);\x00" as *const u8 as *const libc::c_char); @@ -497,7 +485,7 @@ pub unsafe fn dc_add_or_lookup_contact( sqlite3_finalize(stmt); stmt = 0 as *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"INSERT INTO contacts (name, addr, origin) VALUES(?, ?, ?);\x00" as *const u8 as *const libc::c_char, ); @@ -516,7 +504,7 @@ pub unsafe fn dc_add_or_lookup_contact( sqlite3_bind_int(stmt, 3i32, origin); if sqlite3_step(stmt) == 101i32 { row_id = dc_sqlite3_get_rowid( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"contacts\x00" as *const u8 as *const libc::c_char, b"addr\x00" as *const u8 as *const libc::c_char, addr, @@ -549,13 +537,9 @@ pub unsafe fn dc_add_address_book( let mut iCnt: size_t = 0i32 as size_t; let mut sth_modified: libc::c_int = 0i32; let mut modify_cnt: libc::c_int = 0i32; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || adr_book.is_null()) - { + if !(adr_book.is_null()) { lines = dc_split_into_lines(adr_book); if !lines.is_null() { - dc_sqlite3_begin_transaction((*context).sql); iCnt = carray_count(lines) as size_t; i = 0i32 as size_t; while i.wrapping_add(1) < iCnt { @@ -570,7 +554,6 @@ pub unsafe fn dc_add_address_book( } i = (i as libc::c_ulong).wrapping_add(2i32 as libc::c_ulong) as size_t as size_t } - dc_sqlite3_commit((*context).sql); if 0 != modify_cnt { ((*context).cb)( context, @@ -628,84 +611,84 @@ pub unsafe fn dc_get_contacts( let mut self_name: *mut libc::c_char = 0 as *mut libc::c_char; let mut self_name2: *mut libc::c_char = 0 as *mut libc::c_char; let mut add_self: libc::c_int = 0i32; - let mut ret: *mut dc_array_t = dc_array_new(context, 100i32 as size_t); + let mut ret: *mut dc_array_t = dc_array_new(100i32 as size_t); let mut s3strLikeCmd: *mut libc::c_char = 0 as *mut libc::c_char; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - self_addr = dc_sqlite3_get_config( - (*context).sql, - b"configured_addr\x00" as *const u8 as *const libc::c_char, - b"\x00" as *const u8 as *const libc::c_char, - ); - if 0 != listflags & 0x1i32 as libc::c_uint || !query.is_null() { - s3strLikeCmd = sqlite3_mprintf( - b"%%%s%%\x00" as *const u8 as *const libc::c_char, - if !query.is_null() { - query - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - ); - if s3strLikeCmd.is_null() { - current_block = 7597307149762829253; + + self_addr = dc_sqlite3_get_config( + &mut context.sql.clone().lock().unwrap(), + b"configured_addr\x00" as *const u8 as *const libc::c_char, + b"\x00" as *const u8 as *const libc::c_char, + ); + if 0 != listflags & 0x1i32 as libc::c_uint || !query.is_null() { + s3strLikeCmd = sqlite3_mprintf( + b"%%%s%%\x00" as *const u8 as *const libc::c_char, + if !query.is_null() { + query } else { - stmt = - dc_sqlite3_prepare((*context).sql, + b"\x00" as *const u8 as *const libc::c_char + }, + ); + if s3strLikeCmd.is_null() { + current_block = 7597307149762829253; + } else { + stmt = + dc_sqlite3_prepare(&mut context.sql.clone().lock().unwrap(), b"SELECT c.id FROM contacts c LEFT JOIN acpeerstates ps ON c.addr=ps.addr WHERE c.addr!=?1 AND c.id>?2 AND c.origin>=?3 AND c.blocked=0 AND (c.name LIKE ?4 OR c.addr LIKE ?5) AND (1=?6 OR LENGTH(ps.verified_key_fingerprint)!=0) ORDER BY LOWER(c.name||c.addr),c.id;\x00" as *const u8 as *const libc::c_char); - sqlite3_bind_text(stmt, 1i32, self_addr, -1i32, None); - sqlite3_bind_int(stmt, 2i32, 9i32); - sqlite3_bind_int(stmt, 3i32, 0x100i32); - sqlite3_bind_text(stmt, 4i32, s3strLikeCmd, -1i32, None); - sqlite3_bind_text(stmt, 5i32, s3strLikeCmd, -1i32, None); - sqlite3_bind_int( - stmt, - 6i32, - if 0 != listflags & 0x1i32 as libc::c_uint { - 0i32 - } else { - 1i32 - }, - ); - self_name = dc_sqlite3_get_config( - (*context).sql, - b"displayname\x00" as *const u8 as *const libc::c_char, - b"\x00" as *const u8 as *const libc::c_char, - ); - self_name2 = dc_stock_str(context, 2i32); - if query.is_null() - || 0 != dc_str_contains(self_addr, query) - || 0 != dc_str_contains(self_name, query) - || 0 != dc_str_contains(self_name2, query) - { - add_self = 1i32 - } - current_block = 15768484401365413375; - } - } else { - stmt = - dc_sqlite3_prepare((*context).sql, - b"SELECT id FROM contacts WHERE addr!=?1 AND id>?2 AND origin>=?3 AND blocked=0 ORDER BY LOWER(name||addr),id;\x00" - as *const u8 as *const libc::c_char); sqlite3_bind_text(stmt, 1i32, self_addr, -1i32, None); sqlite3_bind_int(stmt, 2i32, 9i32); sqlite3_bind_int(stmt, 3i32, 0x100i32); - add_self = 1i32; + sqlite3_bind_text(stmt, 4i32, s3strLikeCmd, -1i32, None); + sqlite3_bind_text(stmt, 5i32, s3strLikeCmd, -1i32, None); + sqlite3_bind_int( + stmt, + 6i32, + if 0 != listflags & 0x1i32 as libc::c_uint { + 0i32 + } else { + 1i32 + }, + ); + self_name = dc_sqlite3_get_config( + &mut context.sql.clone().lock().unwrap(), + b"displayname\x00" as *const u8 as *const libc::c_char, + b"\x00" as *const u8 as *const libc::c_char, + ); + self_name2 = dc_stock_str(context, 2i32); + if query.is_null() + || 0 != dc_str_contains(self_addr, query) + || 0 != dc_str_contains(self_name, query) + || 0 != dc_str_contains(self_name2, query) + { + add_self = 1i32 + } current_block = 15768484401365413375; } - match current_block { - 7597307149762829253 => {} - _ => { - while sqlite3_step(stmt) == 100i32 { - dc_array_add_id(ret, sqlite3_column_int(stmt, 0i32) as uint32_t); - } - if 0 != listflags & 0x2i32 as libc::c_uint && 0 != add_self { - dc_array_add_id(ret, 1i32 as uint32_t); - } + } else { + stmt = + dc_sqlite3_prepare(&mut context.sql.clone().lock().unwrap(), + b"SELECT id FROM contacts WHERE addr!=?1 AND id>?2 AND origin>=?3 AND blocked=0 ORDER BY LOWER(name||addr),id;\x00" + as *const u8 as *const libc::c_char); + sqlite3_bind_text(stmt, 1i32, self_addr, -1i32, None); + sqlite3_bind_int(stmt, 2i32, 9i32); + sqlite3_bind_int(stmt, 3i32, 0x100i32); + add_self = 1i32; + current_block = 15768484401365413375; + } + match current_block { + 7597307149762829253 => {} + _ => { + while sqlite3_step(stmt) == 100i32 { + dc_array_add_id(ret, sqlite3_column_int(stmt, 0i32) as uint32_t); + } + if 0 != listflags & 0x2i32 as libc::c_uint && 0 != add_self { + dc_array_add_id(ret, 1i32 as uint32_t); } } } + sqlite3_finalize(stmt); sqlite3_free(s3strLikeCmd as *mut libc::c_void); free(self_addr as *mut libc::c_void); @@ -713,37 +696,39 @@ pub unsafe fn dc_get_contacts( free(self_name2 as *mut libc::c_void); return ret; } -pub unsafe fn dc_get_blocked_cnt(mut context: &dc_context_t) -> libc::c_int { + +pub unsafe fn dc_get_blocked_cnt(context: &dc_context_t) -> libc::c_int { let mut ret: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - stmt = dc_sqlite3_prepare( - (*context).sql, - b"SELECT COUNT(*) FROM contacts WHERE id>? AND blocked!=0\x00" as *const u8 - as *const libc::c_char, - ); - sqlite3_bind_int(stmt, 1i32, 9i32); - if !(sqlite3_step(stmt) != 100i32) { - ret = sqlite3_column_int(stmt, 0i32) - } + + stmt = dc_sqlite3_prepare( + &mut context.sql.clone().lock().unwrap(), + b"SELECT COUNT(*) FROM contacts WHERE id>? AND blocked!=0\x00" as *const u8 + as *const libc::c_char, + ); + sqlite3_bind_int(stmt, 1i32, 9i32); + if !(sqlite3_step(stmt) != 100i32) { + ret = sqlite3_column_int(stmt, 0i32) } + sqlite3_finalize(stmt); return ret; } + pub unsafe fn dc_get_blocked_contacts(mut context: &dc_context_t) -> *mut dc_array_t { - let mut ret: *mut dc_array_t = dc_array_new(context, 100i32 as size_t); + let mut ret: *mut dc_array_t = dc_array_new(100i32 as size_t); let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - stmt = dc_sqlite3_prepare( - (*context).sql, - b"SELECT id FROM contacts WHERE id>? AND blocked!=0 ORDER BY LOWER(name||addr),id;\x00" - as *const u8 as *const libc::c_char, - ); - sqlite3_bind_int(stmt, 1i32, 9i32); - while sqlite3_step(stmt) == 100i32 { - dc_array_add_id(ret, sqlite3_column_int(stmt, 0i32) as uint32_t); - } + + stmt = dc_sqlite3_prepare( + &mut context.sql.clone().lock().unwrap(), + b"SELECT id FROM contacts WHERE id>? AND blocked!=0 ORDER BY LOWER(name||addr),id;\x00" + as *const u8 as *const libc::c_char, + ); + sqlite3_bind_int(stmt, 1i32, 9i32); + while sqlite3_step(stmt) == 100i32 { + dc_array_add_id(ret, sqlite3_column_int(stmt, 0i32) as uint32_t); } + sqlite3_finalize(stmt); return ret; } @@ -765,87 +750,105 @@ pub unsafe fn dc_get_contact_encrinfo( let mut fingerprint_other_verified: *mut libc::c_char = 0 as *mut libc::c_char; let mut fingerprint_other_unverified: *mut libc::c_char = 0 as *mut libc::c_char; let mut p: *mut libc::c_char = 0 as *mut libc::c_char; - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - ret = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut ret, 0i32); - if !(0 == dc_contact_load_from_db(contact, (*context).sql, contact_id)) { - dc_apeerstate_load_by_addr(peerstate, (*context).sql, (*contact).addr); - dc_loginparam_read( - loginparam, - (*context).sql, - b"configured_\x00" as *const u8 as *const libc::c_char, - ); - dc_key_load_self_public(self_key, (*loginparam).addr, (*context).sql); - if !dc_apeerstate_peek_key(peerstate, 0i32).is_null() { - p = dc_stock_str( - context, - if (*peerstate).prefer_encrypt == 1i32 { - 34i32 - } else { - 25i32 - }, - ); - dc_strbuilder_cat(&mut ret, p); - free(p as *mut libc::c_void); - if (*self_key).binary.is_null() { - dc_ensure_secret_key_exists(context); - dc_key_load_self_public(self_key, (*loginparam).addr, (*context).sql); - } - dc_strbuilder_cat(&mut ret, b" \x00" as *const u8 as *const libc::c_char); - p = dc_stock_str(context, 30i32); - dc_strbuilder_cat(&mut ret, p); - free(p as *mut libc::c_void); - dc_strbuilder_cat(&mut ret, b":\x00" as *const u8 as *const libc::c_char); - fingerprint_self = dc_key_get_formatted_fingerprint(self_key); - fingerprint_other_verified = - dc_key_get_formatted_fingerprint(dc_apeerstate_peek_key(peerstate, 2i32)); - fingerprint_other_unverified = - dc_key_get_formatted_fingerprint(dc_apeerstate_peek_key(peerstate, 0i32)); - if strcmp((*loginparam).addr, (*peerstate).addr) < 0i32 { - cat_fingerprint( - &mut ret, - (*loginparam).addr, - fingerprint_self, - 0 as *const libc::c_char, - ); - cat_fingerprint( - &mut ret, - (*peerstate).addr, - fingerprint_other_verified, - fingerprint_other_unverified, - ); + + ret = dc_strbuilder_t { + buf: 0 as *mut libc::c_char, + allocated: 0, + free: 0, + eos: 0 as *mut libc::c_char, + }; + dc_strbuilder_init(&mut ret, 0i32); + if !(0 + == dc_contact_load_from_db( + contact, + &mut context.sql.clone().lock().unwrap(), + contact_id, + )) + { + dc_apeerstate_load_by_addr( + peerstate, + &mut context.sql.clone().lock().unwrap(), + (*contact).addr, + ); + dc_loginparam_read( + loginparam, + &mut context.sql.clone().lock().unwrap(), + b"configured_\x00" as *const u8 as *const libc::c_char, + ); + dc_key_load_self_public( + self_key, + (*loginparam).addr, + &mut context.sql.clone().lock().unwrap(), + ); + if !dc_apeerstate_peek_key(peerstate, 0i32).is_null() { + p = dc_stock_str( + context, + if (*peerstate).prefer_encrypt == 1i32 { + 34i32 } else { - cat_fingerprint( - &mut ret, - (*peerstate).addr, - fingerprint_other_verified, - fingerprint_other_unverified, - ); - cat_fingerprint( - &mut ret, - (*loginparam).addr, - fingerprint_self, - 0 as *const libc::c_char, - ); - } - } else if 0 == (*loginparam).server_flags & 0x400i32 - && 0 == (*loginparam).server_flags & 0x40000i32 - { - p = dc_stock_str(context, 27i32); - dc_strbuilder_cat(&mut ret, p); - free(p as *mut libc::c_void); - } else { - p = dc_stock_str(context, 28i32); - dc_strbuilder_cat(&mut ret, p); - free(p as *mut libc::c_void); + 25i32 + }, + ); + dc_strbuilder_cat(&mut ret, p); + free(p as *mut libc::c_void); + if (*self_key).binary.is_null() { + dc_ensure_secret_key_exists(context); + dc_key_load_self_public( + self_key, + (*loginparam).addr, + &mut context.sql.clone().lock().unwrap(), + ); } + dc_strbuilder_cat(&mut ret, b" \x00" as *const u8 as *const libc::c_char); + p = dc_stock_str(context, 30i32); + dc_strbuilder_cat(&mut ret, p); + free(p as *mut libc::c_void); + dc_strbuilder_cat(&mut ret, b":\x00" as *const u8 as *const libc::c_char); + fingerprint_self = dc_key_get_formatted_fingerprint(self_key); + fingerprint_other_verified = + dc_key_get_formatted_fingerprint(dc_apeerstate_peek_key(peerstate, 2i32)); + fingerprint_other_unverified = + dc_key_get_formatted_fingerprint(dc_apeerstate_peek_key(peerstate, 0i32)); + if strcmp((*loginparam).addr, (*peerstate).addr) < 0i32 { + cat_fingerprint( + &mut ret, + (*loginparam).addr, + fingerprint_self, + 0 as *const libc::c_char, + ); + cat_fingerprint( + &mut ret, + (*peerstate).addr, + fingerprint_other_verified, + fingerprint_other_unverified, + ); + } else { + cat_fingerprint( + &mut ret, + (*peerstate).addr, + fingerprint_other_verified, + fingerprint_other_unverified, + ); + cat_fingerprint( + &mut ret, + (*loginparam).addr, + fingerprint_self, + 0 as *const libc::c_char, + ); + } + } else if 0 == (*loginparam).server_flags & 0x400i32 + && 0 == (*loginparam).server_flags & 0x40000i32 + { + p = dc_stock_str(context, 27i32); + dc_strbuilder_cat(&mut ret, p); + free(p as *mut libc::c_void); + } else { + p = dc_stock_str(context, 28i32); + dc_strbuilder_cat(&mut ret, p); + free(p as *mut libc::c_void); } } + dc_apeerstate_unref(peerstate); dc_contact_unref(contact); dc_loginparam_unref(loginparam); @@ -895,12 +898,9 @@ pub unsafe fn dc_delete_contact( ) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || contact_id <= 9i32 as libc::c_uint) - { + if !contact_id <= 9i32 as libc::c_uint { stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"SELECT COUNT(*) FROM chats_contacts WHERE contact_id=?;\x00" as *const u8 as *const libc::c_char, ); @@ -909,7 +909,7 @@ pub unsafe fn dc_delete_contact( sqlite3_finalize(stmt); stmt = 0 as *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"SELECT COUNT(*) FROM msgs WHERE from_id=? OR to_id=?;\x00" as *const u8 as *const libc::c_char, ); @@ -919,7 +919,7 @@ pub unsafe fn dc_delete_contact( sqlite3_finalize(stmt); stmt = 0 as *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"DELETE FROM contacts WHERE id=?;\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_int(stmt, 1i32, contact_id as libc::c_int); @@ -943,7 +943,7 @@ pub unsafe fn dc_get_contact( mut contact_id: uint32_t, ) -> *mut dc_contact_t { let mut ret: *mut dc_contact_t = dc_contact_new(context); - if 0 == dc_contact_load_from_db(ret, (*context).sql, contact_id) { + if 0 == dc_contact_load_from_db(ret, &mut context.sql.clone().lock().unwrap(), contact_id) { dc_contact_unref(ret); ret = 0 as *mut dc_contact_t } @@ -1044,9 +1044,9 @@ pub unsafe fn dc_contact_is_blocked(mut contact: *const dc_contact_t) -> libc::c pub unsafe fn dc_contact_is_verified(mut contact: *mut dc_contact_t) -> libc::c_int { return dc_contact_is_verified_ex(contact, 0 as *const dc_apeerstate_t); } -pub unsafe fn dc_contact_is_verified_ex( - mut contact: *mut dc_contact_t, - mut peerstate: *const dc_apeerstate_t, +pub unsafe fn dc_contact_is_verified_ex<'a>( + contact: *mut dc_contact_t<'a>, + peerstate: *const dc_apeerstate_t<'a>, ) -> libc::c_int { let mut current_block: u64; let mut contact_verified: libc::c_int = 0i32; @@ -1060,7 +1060,7 @@ pub unsafe fn dc_contact_is_verified_ex( peerstate_to_delete = dc_apeerstate_new((*contact).context); if 0 == dc_apeerstate_load_by_addr( peerstate_to_delete, - (*(*contact).context).sql, + &mut (*contact).context.sql.clone().lock().unwrap(), (*contact).addr, ) { current_block = 8667923638376902112; @@ -1105,10 +1105,10 @@ pub unsafe fn dc_addr_equals_self( let mut ret: libc::c_int = 0i32; let mut normalized_addr: *mut libc::c_char = 0 as *mut libc::c_char; let mut self_addr: *mut libc::c_char = 0 as *mut libc::c_char; - if !(context.is_null() || addr.is_null()) { + if !addr.is_null() { normalized_addr = dc_addr_normalize(addr); self_addr = dc_sqlite3_get_config( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"configured_addr\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); @@ -1132,7 +1132,11 @@ pub unsafe fn dc_addr_equals_contact( let mut addr_are_equal: libc::c_int = 0i32; if !addr.is_null() { let mut contact: *mut dc_contact_t = dc_contact_new(context); - if 0 != dc_contact_load_from_db(contact, (*context).sql, contact_id) { + if 0 != dc_contact_load_from_db( + contact, + &mut context.sql.clone().lock().unwrap(), + contact_id, + ) { if !(*contact).addr.is_null() { let mut normalized_addr: *mut libc::c_char = dc_addr_normalize(addr); if strcasecmp((*contact).addr, normalized_addr) == 0i32 { @@ -1149,12 +1153,9 @@ pub unsafe fn dc_addr_equals_contact( pub unsafe fn dc_get_real_contact_cnt(mut context: &dc_context_t) -> size_t { let mut ret: size_t = 0i32 as size_t; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || (*(*context).sql).cobj.is_null()) - { + if !context.sql.clone().lock().unwrap().cobj.is_null() { stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"SELECT COUNT(*) FROM contacts WHERE id>?;\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_int(stmt, 1i32, 9i32); @@ -1177,7 +1178,13 @@ pub unsafe fn dc_get_contact_origin( } let mut contact: *mut dc_contact_t = dc_contact_new(context); *ret_blocked = 0i32; - if !(0 == dc_contact_load_from_db(contact, (*context).sql, contact_id)) { + if !(0 + == dc_contact_load_from_db( + contact, + &mut context.sql.clone().lock().unwrap(), + contact_id, + )) + { /* we could optimize this by loading only the needed fields */ if 0 != (*contact).blocked { *ret_blocked = 1i32 @@ -1194,13 +1201,9 @@ pub unsafe fn dc_real_contact_exists( ) -> libc::c_int { let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut ret: libc::c_int = 0i32; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || (*(*context).sql).cobj.is_null() - || contact_id <= 9i32 as libc::c_uint) - { + if !(context.sql.clone().lock().unwrap().cobj.is_null() || contact_id <= 9i32 as libc::c_uint) { stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"SELECT id FROM contacts WHERE id=?;\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_int(stmt, 1i32, contact_id as libc::c_int); @@ -1216,11 +1219,8 @@ pub unsafe fn dc_scaleup_contact_origin( mut contact_id: uint32_t, mut origin: libc::c_int, ) { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return; - } let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"UPDATE contacts SET origin=? WHERE id=? AND origin>, + pub inbox: Arc>, pub perform_inbox_jobs_needed: Arc>, pub probe_imap_network: Arc>, pub sentbox_thread: dc_jobthread_t, pub mvbox_thread: dc_jobthread_t, - pub smtp: dc_smtp_t, + pub smtp: Arc>, pub smtpidle: Arc<(Mutex, Condvar)>, pub smtp_suspended: Arc>, pub smtp_doing_jobs: Arc>, @@ -79,21 +78,21 @@ pub fn dc_context_new( dc_context_t { blobdir: std::ptr::null_mut(), dbfile: std::ptr::null_mut(), - inbox: unsafe { + inbox: Arc::new(Mutex::new(unsafe { dc_imap_new( Some(cb_get_config), Some(cb_set_config), Some(cb_precheck_imf), Some(cb_receive_imf), ) - }, + })), userdata, cb, os_name: dc_strdup_keep_null(os_name), ongoing_running: 0, shall_stop_ongoing: 1, - sql: dc_sqlite3_new(), - smtp: dc_smtp_new(), + sql: Arc::new(Mutex::new(dc_sqlite3_new())), + smtp: Arc::new(Mutex::new(dc_smtp_new())), smtpidle: Arc::new((Mutex::new(false), Condvar::new())), smtp_suspended: Arc::new(RwLock::new(0)), smtp_doing_jobs: Arc::new(RwLock::new(0)), @@ -135,7 +134,7 @@ pub fn dc_context_new( } unsafe fn cb_receive_imf( - context: &dc_context, + context: &dc_context_t, imf_raw_not_terminated: *const libc::c_char, imf_raw_bytes: size_t, server_folder: *const libc::c_char, @@ -212,7 +211,12 @@ unsafe fn cb_set_config( key: *const libc::c_char, value: *const libc::c_char, ) { - dc_sqlite3_set_config(&context.sql, key, value); + dc_sqlite3_set_config( + context, + &mut context.sql.clone().lock().unwrap(), + key, + value, + ); } /* * * The following three callback are given to dc_imap_new() to read/write configuration @@ -222,22 +226,28 @@ unsafe fn cb_set_config( * @private @memberof dc_context_t */ unsafe fn cb_get_config( - context: &dc_context, + context: &dc_context_t, key: *const libc::c_char, def: *const libc::c_char, ) -> *mut libc::c_char { - dc_sqlite3_get_config(context.sql, key, def) + dc_sqlite3_get_config(&mut context.sql.clone().lock().unwrap(), key, def) } pub unsafe fn dc_context_unref(context: &mut dc_context_t) { if 0 != dc_is_open(context) { dc_close(context); } - dc_imap_unref(&mut context.inbox); - dc_imap_unref(&mut context.sentbox_thread.imap); - dc_imap_unref(&mut context.mvbox_thread.imap); - dc_smtp_unref(&mut context.smtp); - dc_sqlite3_unref(&mut context.sql); + dc_imap_unref(context, &mut context.inbox.clone().lock().unwrap()); + dc_imap_unref( + context, + &mut context.sentbox_thread.imap.clone().lock().unwrap(), + ); + dc_imap_unref( + context, + &mut context.mvbox_thread.imap.clone().lock().unwrap(), + ); + dc_smtp_unref(&mut context.smtp.clone().lock().unwrap()); + dc_sqlite3_unref(context, &mut context.sql.clone().lock().unwrap()); dc_jobthread_exit(&mut context.sentbox_thread); dc_jobthread_exit(&mut context.mvbox_thread); @@ -246,13 +256,19 @@ pub unsafe fn dc_context_unref(context: &mut dc_context_t) { } pub unsafe fn dc_close(context: &mut dc_context_t) { - dc_imap_disconnect(&mut context.inbox); - dc_imap_disconnect(&mut context.sentbox_thread.imap); - dc_imap_disconnect(&mut context.mvbox_thread.imap); - dc_smtp_disconnect(&mut context.smtp); + dc_imap_disconnect(context, &mut context.inbox.clone().lock().unwrap()); + dc_imap_disconnect( + context, + &mut context.sentbox_thread.imap.clone().lock().unwrap(), + ); + dc_imap_disconnect( + context, + &mut context.mvbox_thread.imap.clone().lock().unwrap(), + ); + dc_smtp_disconnect(&mut context.smtp.clone().lock().unwrap()); - if 0 != dc_sqlite3_is_open(context.sql) { - dc_sqlite3_close(context.sql); + if 0 != dc_sqlite3_is_open(&mut context.sql.clone().lock().unwrap()) { + dc_sqlite3_close(context, &mut context.sql.clone().lock().unwrap()); } free(context.dbfile as *mut libc::c_void); context.dbfile = 0 as *mut libc::c_char; @@ -261,11 +277,11 @@ pub unsafe fn dc_close(context: &mut dc_context_t) { } pub unsafe fn dc_is_open(context: &dc_context_t) -> libc::c_int { - dc_sqlite3_is_open(context.sql) + dc_sqlite3_is_open(&mut context.sql.clone().lock().unwrap()) } pub unsafe fn dc_get_userdata(context: &mut dc_context_t) -> *mut libc::c_void { - &mut context.userdata as *mut _ + context.userdata as *mut _ } pub unsafe fn dc_open( @@ -288,7 +304,14 @@ pub unsafe fn dc_open( dc_create_folder(context, context.blobdir); } // Create/open sqlite database, this may already use the blobdir - if !(0 == dc_sqlite3_open(&mut context.sql, dbfile, 0i32)) { + if !(0 + == dc_sqlite3_open( + context, + &mut context.sql.clone().lock().unwrap(), + dbfile, + 0i32, + )) + { success = 1i32 } } @@ -316,25 +339,46 @@ pub unsafe fn dc_set_config( if strcmp(key, b"selfavatar\x00" as *const u8 as *const libc::c_char) == 0 && !value.is_null() { rel_path = dc_strdup(value); if !(0 == dc_make_rel_and_copy(context, &mut rel_path)) { - ret = dc_sqlite3_set_config(context.sql, key, rel_path) + ret = dc_sqlite3_set_config( + context, + &mut context.sql.clone().lock().unwrap(), + key, + rel_path, + ) } } else if strcmp(key, b"inbox_watch\x00" as *const u8 as *const libc::c_char) == 0 { - ret = dc_sqlite3_set_config(&context.sql, key, value); + ret = dc_sqlite3_set_config( + context, + &mut context.sql.clone().lock().unwrap(), + key, + value, + ); dc_interrupt_imap_idle(context); } else if strcmp( key, b"sentbox_watch\x00" as *const u8 as *const libc::c_char, ) == 0 { - ret = dc_sqlite3_set_config(&context.sql, key, value); + ret = dc_sqlite3_set_config( + context, + &mut context.sql.clone().lock().unwrap(), + key, + value, + ); dc_interrupt_sentbox_idle(context); } else if strcmp(key, b"mvbox_watch\x00" as *const u8 as *const libc::c_char) == 0 { - ret = dc_sqlite3_set_config(&context.sql, key, value); + ret = dc_sqlite3_set_config( + context, + &mut context.sql.clone().lock().unwrap(), + key, + value, + ); dc_interrupt_mvbox_idle(context); } else if strcmp(key, b"selfstatus\x00" as *const u8 as *const libc::c_char) == 0 { let mut def = dc_stock_str(context, 13); ret = dc_sqlite3_set_config( - context.sql, + context, + &mut context.sql.clone().lock().unwrap(), key, if value.is_null() || strcmp(value, def) == 0 { 0 as *const libc::c_char @@ -344,7 +388,12 @@ pub unsafe fn dc_set_config( ); free(def as *mut libc::c_void); } else { - ret = dc_sqlite3_set_config(&context.sql, key, value); + ret = dc_sqlite3_set_config( + context, + &mut context.sql.clone().lock().unwrap(), + key, + value, + ); } free(rel_path as *mut libc::c_void); ret @@ -420,14 +469,21 @@ pub unsafe fn dc_get_config(context: &dc_context_t, key: *const libc::c_char) -> } if strcmp(key, b"selfavatar\x00" as *const u8 as *const libc::c_char) == 0 { - let mut rel_path: *mut libc::c_char = - dc_sqlite3_get_config(context.sql, key, 0 as *const libc::c_char); + let mut rel_path: *mut libc::c_char = dc_sqlite3_get_config( + &mut context.sql.clone().lock().unwrap(), + key, + 0 as *const libc::c_char, + ); if !rel_path.is_null() { value = dc_get_abs_path(context, rel_path); free(rel_path as *mut libc::c_void); } } else { - value = dc_sqlite3_get_config(context.sql, key, 0 as *const libc::c_char) + value = dc_sqlite3_get_config( + &mut context.sql.clone().lock().unwrap(), + key, + 0 as *const libc::c_char, + ) } if value.is_null() { @@ -578,20 +634,20 @@ pub unsafe fn dc_get_info(context: &dc_context_t) -> *mut libc::c_char { eos: 0 as *mut libc::c_char, }; dc_strbuilder_init(&mut ret, 0); - if context.is_null() || context.magic != 0x11a11807 as libc::c_uint { - return dc_strdup(b"ErrBadPtr\x00" as *const u8 as *const libc::c_char); - } - l = dc_loginparam_new(); l2 = dc_loginparam_new(); - dc_loginparam_read(l, context.sql, b"\x00" as *const u8 as *const libc::c_char); + dc_loginparam_read( + l, + &mut context.sql.clone().lock().unwrap(), + b"\x00" as *const u8 as *const libc::c_char, + ); dc_loginparam_read( l2, - context.sql, + &mut context.sql.clone().lock().unwrap(), b"configured_\x00" as *const u8 as *const libc::c_char, ); displayname = dc_sqlite3_get_config( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"displayname\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); @@ -600,40 +656,44 @@ pub unsafe fn dc_get_info(context: &dc_context_t) -> *mut libc::c_char { deaddrop_msgs = dc_get_deaddrop_msg_cnt(context) as libc::c_int; contacts = dc_get_real_contact_cnt(context) as libc::c_int; is_configured = dc_sqlite3_get_config_int( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"configured\x00" as *const u8 as *const libc::c_char, 0, ); dbversion = dc_sqlite3_get_config_int( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"dbversion\x00" as *const u8 as *const libc::c_char, 0, ); e2ee_enabled = dc_sqlite3_get_config_int( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"e2ee_enabled\x00" as *const u8 as *const libc::c_char, 1, ); mdns_enabled = dc_sqlite3_get_config_int( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"mdns_enabled\x00" as *const u8 as *const libc::c_char, 1, ); let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"SELECT COUNT(*) FROM keypairs;\x00" as *const u8 as *const libc::c_char, ); sqlite3_step(stmt); prv_key_cnt = sqlite3_column_int(stmt, 0); sqlite3_finalize(stmt); stmt = dc_sqlite3_prepare( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"SELECT COUNT(*) FROM acpeerstates;\x00" as *const u8 as *const libc::c_char, ); sqlite3_step(stmt); pub_key_cnt = sqlite3_column_int(stmt, 0); sqlite3_finalize(stmt); - if 0 != dc_key_load_self_public(self_public, (*l2).addr, context.sql) { + if 0 != dc_key_load_self_public( + self_public, + (*l2).addr, + &mut context.sql.clone().lock().unwrap(), + ) { fingerprint_str = dc_key_get_fingerprint(self_public) } else { fingerprint_str = dc_strdup(b"\x00" as *const u8 as *const libc::c_char) @@ -641,37 +701,37 @@ pub unsafe fn dc_get_info(context: &dc_context_t) -> *mut libc::c_char { l_readable_str = dc_loginparam_get_readable(l); l2_readable_str = dc_loginparam_get_readable(l2); inbox_watch = dc_sqlite3_get_config_int( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"inbox_watch\x00" as *const u8 as *const libc::c_char, 1, ); sentbox_watch = dc_sqlite3_get_config_int( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"sentbox_watch\x00" as *const u8 as *const libc::c_char, 1, ); mvbox_watch = dc_sqlite3_get_config_int( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"mvbox_watch\x00" as *const u8 as *const libc::c_char, 1, ); mvbox_move = dc_sqlite3_get_config_int( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"mvbox_move\x00" as *const u8 as *const libc::c_char, 1, ); folders_configured = dc_sqlite3_get_config_int( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"folders_configured\x00" as *const u8 as *const libc::c_char, 0, ); configured_sentbox_folder = dc_sqlite3_get_config( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"configured_sentbox_folder\x00" as *const u8 as *const libc::c_char, b"\x00" as *const u8 as *const libc::c_char, ); configured_mvbox_folder = dc_sqlite3_get_config( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"configured_mvbox_folder\x00" as *const u8 as *const libc::c_char, b"\x00" as *const u8 as *const libc::c_char, ); @@ -779,11 +839,11 @@ pub unsafe fn dc_get_version_str() -> *mut libc::c_char { pub unsafe fn dc_get_fresh_msgs(context: &dc_context_t) -> *mut dc_array_t { let mut show_deaddrop = 0; - let mut ret = dc_array_new(context, 128 as size_t); + let mut ret = dc_array_new(128 as size_t); let mut stmt = 0 as *mut sqlite3_stmt; if !ret.is_null() { stmt = dc_sqlite3_prepare( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"SELECT m.id FROM msgs m LEFT JOIN contacts ct \ ON m.from_id=ct.id LEFT JOIN chats c ON m.chat_id=c.id WHERE m.state=? \ AND m.hidden=0 \ @@ -809,7 +869,7 @@ pub unsafe fn dc_search_msgs( query: *const libc::c_char, ) -> *mut dc_array_t { let mut success = 0; - let mut ret = dc_array_new(context, 100 as size_t); + let mut ret = dc_array_new(100 as size_t); let mut strLikeInText = 0 as *mut libc::c_char; let mut strLikeBeg = 0 as *mut libc::c_char; let mut real_query = 0 as *mut libc::c_char; @@ -828,7 +888,7 @@ pub unsafe fn dc_search_msgs( strLikeBeg = dc_mprintf(b"%s%%\x00" as *const u8 as *const libc::c_char, real_query); if 0 != chat_id { stmt = dc_sqlite3_prepare( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"SELECT m.id, m.timestamp FROM msgs m LEFT JOIN contacts ct ON m.from_id=ct.id WHERE m.chat_id=? \ AND m.hidden=0 \ AND ct.blocked=0 AND (txt LIKE ? OR ct.name LIKE ?) ORDER BY m.timestamp,m.id;\x00" @@ -840,7 +900,7 @@ pub unsafe fn dc_search_msgs( } else { let mut show_deaddrop = 0; stmt = dc_sqlite3_prepare( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"SELECT m.id, m.timestamp FROM msgs m LEFT JOIN contacts ct ON m.from_id=ct.id \ LEFT JOIN chats c ON m.chat_id=c.id WHERE m.chat_id>9 AND m.hidden=0 \ AND (c.blocked=0 OR c.blocked=?) \ @@ -897,7 +957,7 @@ pub unsafe fn dc_is_sentbox( folder_name: *const libc::c_char, ) -> libc::c_int { let mut sentbox_name = dc_sqlite3_get_config( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"configured_sentbox_folder\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); @@ -915,7 +975,7 @@ pub unsafe fn dc_is_sentbox( pub unsafe fn dc_is_mvbox(context: &dc_context_t, folder_name: *const libc::c_char) -> libc::c_int { let mut mvbox_name = dc_sqlite3_get_config( - context.sql, + &mut context.sql.clone().lock().unwrap(), b"configured_mvbox_folder\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); diff --git a/src/dc_e2ee.rs b/src/dc_e2ee.rs index dc01a16e3..ff2e2f271 100644 --- a/src/dc_e2ee.rs +++ b/src/dc_e2ee.rs @@ -54,7 +54,7 @@ pub unsafe fn dc_e2ee_encrypt( let mut plain: *mut MMAPString = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char); let mut ctext: *mut libc::c_char = 0 as *mut libc::c_char; let mut ctext_bytes: size_t = 0i32 as size_t; - let mut peerstates: &dc_array_t = dc_array_new(0 as *mut dc_context_t, 10i32 as size_t); + let mut peerstates = dc_array_new(10i32 as size_t); if !helper.is_null() { memset( helper as *mut libc::c_void, @@ -62,9 +62,7 @@ pub unsafe fn dc_e2ee_encrypt( ::std::mem::size_of::(), ); } - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || recipients_addr.is_null() + if !(recipients_addr.is_null() || in_out_message.is_null() || !(*in_out_message).mm_parent.is_null() || autocryptheader.is_null() @@ -76,14 +74,14 @@ pub unsafe fn dc_e2ee_encrypt( /* libEtPan's pgp_encrypt_mime() takes the parent as the new root. We just expect the root as being given to this function. */ (*autocryptheader).prefer_encrypt = 0i32; if 0 != dc_sqlite3_get_config_int( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"e2ee_enabled\x00" as *const u8 as *const libc::c_char, 1i32, ) { (*autocryptheader).prefer_encrypt = 1i32 } (*autocryptheader).addr = dc_sqlite3_get_config( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"configured_addr\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); @@ -113,7 +111,7 @@ pub unsafe fn dc_e2ee_encrypt( if !(strcasecmp(recipient_addr, (*autocryptheader).addr) == 0i32) { if 0 != dc_apeerstate_load_by_addr( peerstate, - (*context).sql, + &mut context.sql.clone().lock().unwrap(), recipient_addr, ) && { key_to_use = dc_apeerstate_peek_key(peerstate, min_verified); @@ -141,7 +139,7 @@ pub unsafe fn dc_e2ee_encrypt( if 0 == dc_key_load_self_private( sign_key, (*autocryptheader).addr, - (*context).sql, + &mut context.sql.clone().lock().unwrap(), ) { do_encrypt = 0i32 } @@ -516,11 +514,12 @@ unsafe fn load_or_generate_self_public_key( let mut key_created: libc::c_int = 0i32; let mut success: libc::c_int = 0i32; let mut key_creation_here: libc::c_int = 0i32; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || public_key.is_null()) - { - if 0 == dc_key_load_self_public(public_key, self_addr, (*context).sql) { + if !public_key.is_null() { + if 0 == dc_key_load_self_public( + public_key, + self_addr, + &mut context.sql.clone().lock().unwrap(), + ) { /* create the keypair - this may take a moment, however, as this is in a thread, this is no big deal */ if 0 != s_in_key_creation { current_block = 10496152961502316708; @@ -579,7 +578,7 @@ unsafe fn load_or_generate_self_public_key( private_key, self_addr, 1i32, - (*context).sql, + &mut context.sql.clone().lock().unwrap(), ) { /*set default*/ @@ -643,12 +642,7 @@ pub unsafe fn dc_e2ee_decrypt( ::std::mem::size_of::(), ); } - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || in_out_message.is_null() - || helper.is_null() - || imffields.is_null()) - { + if !(in_out_message.is_null() || helper.is_null() || imffields.is_null()) { if !imffields.is_null() { let mut field: *mut mailimf_field = mailimf_find_field(imffields, MAILIMF_FIELD_FROM as libc::c_int); @@ -676,24 +670,36 @@ pub unsafe fn dc_e2ee_decrypt( } } if message_time > 0i32 as libc::c_long && !from.is_null() { - if 0 != dc_apeerstate_load_by_addr(peerstate, (*context).sql, from) { + if 0 != dc_apeerstate_load_by_addr( + peerstate, + &mut context.sql.clone().lock().unwrap(), + from, + ) { if !autocryptheader.is_null() { dc_apeerstate_apply_header(peerstate, autocryptheader, message_time); - dc_apeerstate_save_to_db(peerstate, (*context).sql, 0i32); + dc_apeerstate_save_to_db( + peerstate, + &mut context.sql.clone().lock().unwrap(), + 0i32, + ); } else if message_time > (*peerstate).last_seen_autocrypt && 0 == contains_report(in_out_message) { dc_apeerstate_degrade_encryption(peerstate, message_time); - dc_apeerstate_save_to_db(peerstate, (*context).sql, 0i32); + dc_apeerstate_save_to_db( + peerstate, + &mut context.sql.clone().lock().unwrap(), + 0i32, + ); } } else if !autocryptheader.is_null() { dc_apeerstate_init_from_header(peerstate, autocryptheader, message_time); - dc_apeerstate_save_to_db(peerstate, (*context).sql, 1i32); + dc_apeerstate_save_to_db(peerstate, &mut context.sql.clone().lock().unwrap(), 1i32); } } /* load private key for decryption */ self_addr = dc_sqlite3_get_config( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"configured_addr\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); @@ -702,11 +708,15 @@ pub unsafe fn dc_e2ee_decrypt( == dc_keyring_load_self_private_for_decrypting( private_keyring, self_addr, - (*context).sql, + &mut context.sql.clone().lock().unwrap(), )) { if (*peerstate).last_seen == 0i32 as libc::c_long { - dc_apeerstate_load_by_addr(peerstate, (*context).sql, from); + dc_apeerstate_load_by_addr( + peerstate, + &mut context.sql.clone().lock().unwrap(), + from, + ); } if 0 != (*peerstate).degrade_event { dc_handle_degrade_event(context, peerstate); @@ -795,14 +805,22 @@ unsafe fn update_gossip_peerstates( let mut peerstate: *mut dc_apeerstate_t = dc_apeerstate_new(context); if 0 == dc_apeerstate_load_by_addr( peerstate, - (*context).sql, + &mut context.sql.clone().lock().unwrap(), (*gossip_header).addr, ) { dc_apeerstate_init_from_gossip(peerstate, gossip_header, message_time); - dc_apeerstate_save_to_db(peerstate, (*context).sql, 1i32); + dc_apeerstate_save_to_db( + peerstate, + &mut context.sql.clone().lock().unwrap(), + 1i32, + ); } else { dc_apeerstate_apply_gossip(peerstate, gossip_header, message_time); - dc_apeerstate_save_to_db(peerstate, (*context).sql, 0i32); + dc_apeerstate_save_to_db( + peerstate, + &mut context.sql.clone().lock().unwrap(), + 0i32, + ); } if 0 != (*peerstate).degrade_event { dc_handle_degrade_event(context, peerstate); @@ -855,7 +873,7 @@ unsafe fn decrypt_recursive( ) -> libc::c_int { let mut ct: *mut mailmime_content = 0 as *mut mailmime_content; let mut cur: *mut clistiter = 0 as *mut clistiter; - if context.is_null() || mime.is_null() { + if mime.is_null() { return 0i32; } if (*mime).mm_type == MAILMIME_MULTIPLE as libc::c_int { @@ -1198,12 +1216,9 @@ pub unsafe fn dc_ensure_secret_key_exists(mut context: &dc_context_t) -> libc::c let mut success: libc::c_int = 0i32; let mut public_key: *mut dc_key_t = dc_key_new(); let mut self_addr: *mut libc::c_char = 0 as *mut libc::c_char; - if !(context.is_null() - || (*context).magic != 0x11a11807i32 as libc::c_uint - || public_key.is_null()) - { + if !public_key.is_null() { self_addr = dc_sqlite3_get_config( - (*context).sql, + &mut context.sql.clone().lock().unwrap(), b"configured_addr\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); diff --git a/src/dc_imap.rs b/src/dc_imap.rs index ce4f59ab5..83cbb3802 100644 --- a/src/dc_imap.rs +++ b/src/dc_imap.rs @@ -38,8 +38,6 @@ pub struct dc_imap_t { pub set_config: dc_set_config_t, pub precheck_imf: dc_precheck_imf_t, pub receive_imf: dc_receive_imf_t, - // TODO: remove - pub context: *mut dc_context_t, pub log_connect_errors: i32, pub skip_log_capabilities: i32, } @@ -75,8 +73,6 @@ pub unsafe fn dc_imap_new( set_config, precheck_imf, receive_imf, - // TODO: remove - context: std::ptr::null_mut(), log_connect_errors: 1, skip_log_capabilities: 0, }; @@ -107,8 +103,8 @@ pub unsafe fn dc_imap_new( imap } -pub unsafe fn dc_imap_unref(imap: &mut dc_imap_t) { - dc_imap_disconnect(imap); +pub unsafe fn dc_imap_unref(context: &dc_context_t, imap: &mut dc_imap_t) { + dc_imap_disconnect(context, imap); free(imap.watch_folder as *mut libc::c_void); free(imap.selected_folder as *mut libc::c_void); if !imap.fetch_type_prefetch.is_null() { @@ -122,9 +118,9 @@ pub unsafe fn dc_imap_unref(imap: &mut dc_imap_t) { } } -pub unsafe fn dc_imap_disconnect(imap: &mut dc_imap_t) { +pub unsafe fn dc_imap_disconnect(context: &dc_context_t, imap: &mut dc_imap_t) { if 0 != imap.connected { - unsetup_handle(imap); + unsetup_handle(context, imap); free_connect_param(imap); imap.connected = 0 }; @@ -150,7 +146,7 @@ unsafe fn free_connect_param(imap: &mut dc_imap_t) { imap.has_xlist = 0; } -unsafe fn unsetup_handle(imap: &mut dc_imap_t) { +unsafe fn unsetup_handle(context: &dc_context_t, imap: &mut dc_imap_t) { if !imap.etpan.is_null() { if 0 != imap.idle_set_up { mailstream_unsetup_idle((*imap.etpan).imap_stream); @@ -163,7 +159,7 @@ unsafe fn unsetup_handle(imap: &mut dc_imap_t) { mailimap_free(imap.etpan); imap.etpan = 0 as *mut mailimap; dc_log_info( - imap.context, + context, 0, b"IMAP disconnected.\x00" as *const u8 as *const libc::c_char, ); @@ -171,7 +167,11 @@ unsafe fn unsetup_handle(imap: &mut dc_imap_t) { *imap.selected_folder.offset(0isize) = 0 as libc::c_char; } -pub unsafe fn dc_imap_connect(imap: &mut dc_imap_t, lp: *const dc_loginparam_t) -> libc::c_int { +pub unsafe fn dc_imap_connect( + context: &dc_context_t, + imap: &mut dc_imap_t, + lp: *const dc_loginparam_t, +) -> libc::c_int { let mut success: libc::c_int = 0; if lp.is_null() || (*lp).mail_server.is_null() @@ -189,7 +189,7 @@ pub unsafe fn dc_imap_connect(imap: &mut dc_imap_t, lp: *const dc_loginparam_t) imap.imap_user = dc_strdup((*lp).mail_user); imap.imap_pw = dc_strdup((*lp).mail_pw); imap.server_flags = (*lp).server_flags; - if !(0 == setup_handle_if_needed(imap)) { + if !(0 == setup_handle_if_needed(context, imap)) { imap.can_idle = mailimap_has_idle(imap.etpan); imap.has_xlist = mailimap_has_xlist(imap.etpan); imap.can_idle = 0; @@ -236,7 +236,7 @@ pub unsafe fn dc_imap_connect(imap: &mut dc_imap_t, lp: *const dc_loginparam_t) } } dc_log_info( - imap.context, + context, 0, b"IMAP-capabilities:%s\x00" as *const u8 as *const libc::c_char, capinfostr.buf, @@ -248,19 +248,19 @@ pub unsafe fn dc_imap_connect(imap: &mut dc_imap_t, lp: *const dc_loginparam_t) } } if success == 0 { - unsetup_handle(imap); + unsetup_handle(context, imap); free_connect_param(imap); } success } -unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { +unsafe fn setup_handle_if_needed(context: &dc_context_t, imap: &mut dc_imap_t) -> libc::c_int { let mut current_block: u64; let mut r: libc::c_int = 0; let mut success: libc::c_int = 0; if !(imap.imap_server.is_null()) { if 0 != imap.should_reconnect { - unsetup_handle(imap); + unsetup_handle(context, imap); } if !imap.etpan.is_null() { success = 1 @@ -273,9 +273,9 @@ unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { imap.imap_server, imap.imap_port as uint16_t, ); - if 0 != dc_imap_is_error(imap, r) { + if 0 != dc_imap_is_error(context, imap, r) { dc_log_event_seq( - imap.context, + context, Event::ERROR_NETWORK, &mut imap.log_connect_errors as *mut libc::c_int, b"Could not connect to IMAP-server %s:%i. (Error #%i)\x00" as *const u8 @@ -287,8 +287,8 @@ unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { current_block = 15811161807000851472; } else if 0 != imap.server_flags & 0x100 { r = mailimap_socket_starttls(imap.etpan); - if 0 != dc_imap_is_error(imap, r) { - dc_log_event_seq(imap.context, Event::ERROR_NETWORK, + if 0 != dc_imap_is_error(context, imap, r) { + dc_log_event_seq(context, Event::ERROR_NETWORK, &mut imap.log_connect_errors as *mut libc::c_int, b"Could not connect to IMAP-server %s:%i using STARTTLS. (Error #%i)\x00" @@ -300,7 +300,7 @@ unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { current_block = 15811161807000851472; } else { dc_log_info( - imap.context, + context, 0, b"IMAP-server %s:%i STARTTLS-connected.\x00" as *const u8 as *const libc::c_char, @@ -311,7 +311,7 @@ unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { } } else { dc_log_info( - imap.context, + context, 0, b"IMAP-server %s:%i connected.\x00" as *const u8 as *const libc::c_char, imap.imap_server, @@ -321,9 +321,9 @@ unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { } } else { r = mailimap_ssl_connect(imap.etpan, imap.imap_server, imap.imap_port as uint16_t); - if 0 != dc_imap_is_error(imap, r) { + if 0 != dc_imap_is_error(context, imap, r) { dc_log_event_seq( - imap.context, + context, Event::ERROR_NETWORK, &mut imap.log_connect_errors as *mut libc::c_int, b"Could not connect to IMAP-server %s:%i using SSL. (Error #%i)\x00" @@ -335,7 +335,7 @@ unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { current_block = 15811161807000851472; } else { dc_log_info( - imap.context, + context, 0, b"IMAP-server %s:%i SSL-connected.\x00" as *const u8 as *const libc::c_char, imap.imap_server, @@ -349,21 +349,17 @@ unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { _ => { if 0 != imap.server_flags & 0x2 { dc_log_info( - imap.context, + context, 0, b"IMAP-OAuth2 connect...\x00" as *const u8 as *const libc::c_char, ); let mut access_token: *mut libc::c_char = - dc_get_oauth2_access_token(imap.context, imap.addr, imap.imap_pw, 0); + dc_get_oauth2_access_token(context, imap.addr, imap.imap_pw, 0); r = mailimap_oauth2_authenticate(imap.etpan, imap.imap_user, access_token); - if 0 != dc_imap_is_error(imap, r) { + if 0 != dc_imap_is_error(context, imap, r) { free(access_token as *mut libc::c_void); - access_token = dc_get_oauth2_access_token( - imap.context, - imap.addr, - imap.imap_pw, - 0x1, - ); + access_token = + dc_get_oauth2_access_token(context, imap.addr, imap.imap_pw, 0x1); r = mailimap_oauth2_authenticate( imap.etpan, imap.imap_user, @@ -374,14 +370,15 @@ unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { } else { r = mailimap_login(imap.etpan, imap.imap_user, imap.imap_pw) } - if 0 != dc_imap_is_error(imap, r) { + if 0 != dc_imap_is_error(context, imap, r) { let mut msg: *mut libc::c_char = get_error_msg( + context, imap, b"Cannot login\x00" as *const u8 as *const libc::c_char, r, ); dc_log_event_seq( - imap.context, + context, Event::ERROR_NETWORK, &mut imap.log_connect_errors as *mut libc::c_int, b"%s\x00" as *const u8 as *const libc::c_char, @@ -390,7 +387,7 @@ unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { free(msg as *mut libc::c_void); } else { dc_log_event( - imap.context, + context, Event::IMAP_CONNECTED, 0, b"IMAP-login as %s ok.\x00" as *const u8 as *const libc::c_char, @@ -403,13 +400,14 @@ unsafe fn setup_handle_if_needed(imap: &mut dc_imap_t) -> libc::c_int { } } if success == 0 { - unsetup_handle(imap); + unsetup_handle(context, imap); } imap.should_reconnect = 0; success } unsafe fn get_error_msg( + context: &dc_context_t, imap: &mut dc_imap_t, what_failed: *const libc::c_char, code: libc::c_int, @@ -424,7 +422,7 @@ unsafe fn get_error_msg( dc_strbuilder_init(&mut msg, 1000); match code { 28 => { - stock = dc_stock_str_repl_string(imap.context, 60, imap.imap_user); + stock = dc_stock_str_repl_string(context, 60, imap.imap_user); dc_strbuilder_cat(&mut msg, stock); } _ => { @@ -440,12 +438,8 @@ unsafe fn get_error_msg( stock = 0 as *mut libc::c_char; if !(*imap.etpan).imap_response.is_null() { dc_strbuilder_cat(&mut msg, b"\n\n\x00" as *const u8 as *const libc::c_char); - stock = dc_stock_str_repl_string2( - imap.context, - 61, - imap.imap_server, - (*imap.etpan).imap_response, - ); + stock = + dc_stock_str_repl_string2(context, 61, imap.imap_server, (*imap.etpan).imap_response); dc_strbuilder_cat(&mut msg, stock); } free(stock as *mut libc::c_void); @@ -453,7 +447,11 @@ unsafe fn get_error_msg( msg.buf } -pub unsafe fn dc_imap_is_error(imap: &mut dc_imap_t, code: libc::c_int) -> libc::c_int { +pub unsafe fn dc_imap_is_error( + context: &dc_context_t, + imap: &mut dc_imap_t, + code: libc::c_int, +) -> libc::c_int { if code == MAILIMAP_NO_ERROR as libc::c_int || code == MAILIMAP_NO_ERROR_AUTHENTICATED as libc::c_int || code == MAILIMAP_NO_ERROR_NON_AUTHENTICATED as libc::c_int @@ -462,7 +460,7 @@ pub unsafe fn dc_imap_is_error(imap: &mut dc_imap_t, code: libc::c_int) -> libc: } if code == MAILIMAP_ERROR_STREAM as libc::c_int || code == MAILIMAP_ERROR_PARSE as libc::c_int { dc_log_info( - imap.context, + context, 0, b"IMAP stream lost; we\'ll reconnect soon.\x00" as *const u8 as *const libc::c_char, ); @@ -482,21 +480,22 @@ pub unsafe extern "C" fn dc_imap_set_watch_folder( imap.watch_folder = dc_strdup(watch_folder); } -pub unsafe fn dc_imap_is_connected(imap: *const dc_imap_t) -> libc::c_int { +pub unsafe fn dc_imap_is_connected(imap: &dc_imap_t) -> libc::c_int { (0 != imap.connected) as libc::c_int } -pub unsafe fn dc_imap_fetch(imap: &mut dc_imap_t) -> libc::c_int { +pub unsafe fn dc_imap_fetch(context: &dc_context_t, imap: &mut dc_imap_t) -> libc::c_int { let mut success: libc::c_int = 0; - if !(imap.is_null() || 0 == imap.connected) { - setup_handle_if_needed(imap); - while fetch_from_single_folder(imap, imap.watch_folder) > 0 {} + if !0 == imap.connected { + setup_handle_if_needed(context, imap); + while fetch_from_single_folder(context, imap, imap.watch_folder) > 0 {} success = 1; } success } unsafe fn fetch_from_single_folder( + context: &dc_context_t, imap: &mut dc_imap_t, folder: *const libc::c_char, ) -> libc::c_int { @@ -512,25 +511,25 @@ unsafe fn fetch_from_single_folder( let mut set: *mut mailimap_set = 0 as *mut mailimap_set; if imap.etpan.is_null() { dc_log_info( - imap.context, + context, 0, b"Cannot fetch from \"%s\" - not connected.\x00" as *const u8 as *const libc::c_char, folder, ); - } else if select_folder(imap, folder) == 0 { + } else if select_folder(context, imap, folder) == 0 { dc_log_warning( - imap.context, + context, 0, b"Cannot select folder %s for fetching.\x00" as *const u8 as *const libc::c_char, folder, ); } else { - get_config_lastseenuid(imap, folder, &mut uidvalidity, &mut lastseenuid); + get_config_lastseenuid(context, imap, folder, &mut uidvalidity, &mut lastseenuid); if uidvalidity != (*(*imap.etpan).imap_selection_info).sel_uidvalidity { /* first time this folder is selected or UIDVALIDITY has changed, init lastseenuid and save it to config */ if (*(*imap.etpan).imap_selection_info).sel_uidvalidity <= 0 as libc::c_uint { dc_log_error( - imap.context, + context, 0, b"Cannot get UIDVALIDITY for folder \"%s\".\x00" as *const u8 as *const libc::c_char, @@ -541,13 +540,14 @@ unsafe fn fetch_from_single_folder( if 0 != (*(*imap.etpan).imap_selection_info).sel_has_exists() { if (*(*imap.etpan).imap_selection_info).sel_exists <= 0 as libc::c_uint { dc_log_info( - imap.context, + context, 0, b"Folder \"%s\" is empty.\x00" as *const u8 as *const libc::c_char, folder, ); if (*(*imap.etpan).imap_selection_info).sel_exists == 0 as libc::c_uint { set_config_lastseenuid( + context, imap, folder, (*(*imap.etpan).imap_selection_info).sel_uidvalidity, @@ -563,7 +563,7 @@ unsafe fn fetch_from_single_folder( } } else { dc_log_info( - imap.context, + context, 0, b"EXISTS is missing for folder \"%s\", using fallback.\x00" as *const u8 as *const libc::c_char, @@ -585,10 +585,10 @@ unsafe fn fetch_from_single_folder( mailimap_set_free(set); set = 0 as *mut mailimap_set } - if 0 != dc_imap_is_error(imap, r) || fetch_result.is_null() { + if 0 != dc_imap_is_error(context, imap, r) || fetch_result.is_null() { fetch_result = 0 as *mut clist; dc_log_info( - imap.context, + context, 0, b"No result returned for folder \"%s\".\x00" as *const u8 as *const libc::c_char, @@ -600,7 +600,7 @@ unsafe fn fetch_from_single_folder( cur = (*fetch_result).first; if cur.is_null() { dc_log_info( - imap.context, + context, 0, b"Empty result returned for folder \"%s\".\x00" as *const u8 as *const libc::c_char, @@ -622,7 +622,7 @@ unsafe fn fetch_from_single_folder( } if lastseenuid <= 0 as libc::c_uint { dc_log_error( - imap.context, + context, 0, b"Cannot get largest UID for folder \"%s\"\x00" as *const u8 as *const libc::c_char, @@ -640,9 +640,15 @@ unsafe fn fetch_from_single_folder( } uidvalidity = (*(*imap.etpan).imap_selection_info).sel_uidvalidity; - set_config_lastseenuid(imap, folder, uidvalidity, lastseenuid); + set_config_lastseenuid( + context, + imap, + folder, + uidvalidity, + lastseenuid, + ); dc_log_info( - imap.context, + context, 0, b"lastseenuid initialized to %i for %s@%i\x00" as *const u8 as *const libc::c_char, @@ -677,11 +683,11 @@ unsafe fn fetch_from_single_folder( mailimap_set_free(set); set = 0 as *mut mailimap_set } - if 0 != dc_imap_is_error(imap, r) || fetch_result.is_null() { + if 0 != dc_imap_is_error(context, imap, r) || fetch_result.is_null() { fetch_result = 0 as *mut clist; if r == MAILIMAP_ERROR_PROTOCOL as libc::c_int { dc_log_info( - imap.context, + context, 0, b"Folder \"%s\" is empty\x00" as *const u8 as *const libc::c_char, folder, @@ -689,7 +695,7 @@ unsafe fn fetch_from_single_folder( } else { /* the folder is simply empty, this is no error */ dc_log_warning( - imap.context, + context, 0, b"Cannot fetch message list from folder \"%s\".\x00" as *const u8 as *const libc::c_char, @@ -711,10 +717,10 @@ unsafe fn fetch_from_single_folder( unquote_rfc724_mid(peek_rfc724_mid(msg_att_0)); read_cnt = read_cnt.wrapping_add(1); if 0 == imap.precheck_imf.expect("non-null function pointer")( - imap, rfc724_mid, folder, cur_uid, + context, rfc724_mid, folder, cur_uid, ) { - if fetch_single_msg(imap, folder, cur_uid) == 0 { - dc_log_info(imap.context, 0, + if fetch_single_msg(context, imap, folder, cur_uid) == 0 { + dc_log_info(context, 0, b"Read error for message %s from \"%s\", trying over later.\x00" as *const u8 as *const libc::c_char, @@ -723,7 +729,7 @@ unsafe fn fetch_from_single_folder( } } else { dc_log_info( - imap.context, + context, 0, b"Skipping message %s from \"%s\" by precheck.\x00" as *const u8 as *const libc::c_char, @@ -743,7 +749,7 @@ unsafe fn fetch_from_single_folder( } } if 0 == read_errors && new_lastseenuid > 0 as libc::c_uint { - set_config_lastseenuid(imap, folder, uidvalidity, new_lastseenuid); + set_config_lastseenuid(context, imap, folder, uidvalidity, new_lastseenuid); } } } @@ -753,7 +759,7 @@ unsafe fn fetch_from_single_folder( /* done */ if 0 != read_errors { dc_log_warning( - imap.context, + context, 0, b"%i mails read from \"%s\" with %i errors.\x00" as *const u8 as *const libc::c_char, read_cnt as libc::c_int, @@ -762,7 +768,7 @@ unsafe fn fetch_from_single_folder( ); } else { dc_log_info( - imap.context, + context, 0i32, b"%i mails read from \"%s\".\x00" as *const u8 as *const libc::c_char, read_cnt as libc::c_int, @@ -777,6 +783,7 @@ unsafe fn fetch_from_single_folder( } unsafe fn set_config_lastseenuid( + context: &dc_context_t, imap: &mut dc_imap_t, folder: *const libc::c_char, uidvalidity: uint32_t, @@ -791,7 +798,7 @@ unsafe fn set_config_lastseenuid( uidvalidity, lastseenuid, ); - imap.set_config.expect("non-null function pointer")(imap, key, val); + imap.set_config.expect("non-null function pointer")(context, key, val); free(val as *mut libc::c_void); free(key as *mut libc::c_void); } @@ -878,6 +885,7 @@ unsafe fn peek_uid(msg_att: *mut mailimap_msg_att) -> uint32_t { } unsafe fn fetch_single_msg( + context: &dc_context_t, imap: &mut dc_imap_t, folder: *const libc::c_char, server_uid: uint32_t, @@ -901,10 +909,10 @@ unsafe fn fetch_single_msg( mailimap_set_free(set); set = 0 as *mut mailimap_set } - if 0 != dc_imap_is_error(imap, r) || fetch_result.is_null() { + if 0 != dc_imap_is_error(context, imap, r) || fetch_result.is_null() { fetch_result = 0 as *mut clist; dc_log_warning( - imap.context, + context, 0, b"Error #%i on fetching message #%i from folder \"%s\"; retry=%i.\x00" as *const u8 as *const libc::c_char, @@ -921,7 +929,7 @@ unsafe fn fetch_single_msg( cur = (*fetch_result).first; if cur.is_null() { dc_log_warning( - imap.context, + context, 0, b"Message #%i does not exist in folder \"%s\".\x00" as *const u8 as *const libc::c_char, @@ -945,7 +953,7 @@ unsafe fn fetch_single_msg( if !(msg_content.is_null() || msg_bytes <= 0 || 0 != deleted) { /* dc_log_warning(imap->context, 0, "Message #%i in folder \"%s\" is empty or deleted.", (int)server_uid, folder); -- this is a quite usual situation, do not print a warning */ imap.receive_imf.expect("non-null function pointer")( - imap, + context, msg_content, msg_bytes, folder, @@ -1038,6 +1046,7 @@ unsafe fn peek_body( } unsafe fn get_config_lastseenuid( + context: &dc_context_t, imap: &mut dc_imap_t, folder: *const libc::c_char, uidvalidity: *mut uint32_t, @@ -1050,7 +1059,7 @@ unsafe fn get_config_lastseenuid( folder, ); let mut val1: *mut libc::c_char = - imap.get_config.expect("non-null function pointer")(imap, key, 0 as *const libc::c_char); + imap.get_config.expect("non-null function pointer")(context, key, 0 as *const libc::c_char); let mut val2: *mut libc::c_char = 0 as *mut libc::c_char; let mut val3: *mut libc::c_char = 0 as *mut libc::c_char; if !val1.is_null() { @@ -1074,7 +1083,11 @@ unsafe fn get_config_lastseenuid( * Handle folders ******************************************************************************/ -unsafe fn select_folder(imap: &mut dc_imap_t, folder: *const libc::c_char) -> libc::c_int { +unsafe fn select_folder( + context: &dc_context_t, + imap: &mut dc_imap_t, + folder: *const libc::c_char, +) -> libc::c_int { if imap.etpan.is_null() { *imap.selected_folder.offset(0isize) = 0 as libc::c_char; imap.selected_folder_needs_expunge = 0; @@ -1089,7 +1102,7 @@ unsafe fn select_folder(imap: &mut dc_imap_t, folder: *const libc::c_char) -> li if 0 != imap.selected_folder_needs_expunge { if 0 != *imap.selected_folder.offset(0isize) { dc_log_info( - imap.context, + context, 0, b"Expunge messages in \"%s\".\x00" as *const u8 as *const libc::c_char, imap.selected_folder, @@ -1100,9 +1113,9 @@ unsafe fn select_folder(imap: &mut dc_imap_t, folder: *const libc::c_char) -> li } if !folder.is_null() { let mut r: libc::c_int = mailimap_select(imap.etpan, folder); - if 0 != dc_imap_is_error(imap, r) || (*imap.etpan).imap_selection_info.is_null() { + if 0 != dc_imap_is_error(context, imap, r) || (*imap.etpan).imap_selection_info.is_null() { dc_log_info( - imap.context, + context, 0, b"Cannot select folder; code=%i, imap_response=%s\x00" as *const u8 as *const libc::c_char, @@ -1122,22 +1135,21 @@ unsafe fn select_folder(imap: &mut dc_imap_t, folder: *const libc::c_char) -> li 1 } -pub unsafe fn dc_imap_idle(imap: &mut dc_imap_t) { +pub unsafe fn dc_imap_idle(context: &dc_context_t, imap: &mut dc_imap_t) { let mut current_block: u64; let mut r: libc::c_int = 0; let mut r2: libc::c_int = 0; if 0 != imap.can_idle { - setup_handle_if_needed(imap); - if imap.idle_set_up == 0 && !imap.etpan.is_null() && !(*imap.etpan).imap_stream.is_null() - { + setup_handle_if_needed(context, imap); + if imap.idle_set_up == 0 && !imap.etpan.is_null() && !(*imap.etpan).imap_stream.is_null() { r = mailstream_setup_idle((*imap.etpan).imap_stream); - if 0 != dc_imap_is_error(imap, r) { + if 0 != dc_imap_is_error(context, imap, r) { dc_log_warning( - imap.context, + context, 0, b"IMAP-IDLE: Cannot setup.\x00" as *const u8 as *const libc::c_char, ); - fake_idle(imap); + fake_idle(context, imap); current_block = 14832935472441733737; } else { imap.idle_set_up = 1; @@ -1149,22 +1161,22 @@ pub unsafe fn dc_imap_idle(imap: &mut dc_imap_t) { match current_block { 14832935472441733737 => {} _ => { - if 0 == imap.idle_set_up || 0 == select_folder(imap, imap.watch_folder) { + if 0 == imap.idle_set_up || 0 == select_folder(context, imap, imap.watch_folder) { dc_log_warning( - imap.context, + context, 0, b"IMAP-IDLE not setup.\x00" as *const u8 as *const libc::c_char, ); - fake_idle(imap); + fake_idle(context, imap); } else { r = mailimap_idle(imap.etpan); - if 0 != dc_imap_is_error(imap, r) { + if 0 != dc_imap_is_error(context, imap, r) { dc_log_warning( - imap.context, + context, 0, b"IMAP-IDLE: Cannot start.\x00" as *const u8 as *const libc::c_char, ); - fake_idle(imap); + fake_idle(context, imap); } else { r = mailstream_wait_idle((*imap.etpan).imap_stream, 23 * 60); r2 = mailimap_idle_done(imap.etpan); @@ -1172,7 +1184,7 @@ pub unsafe fn dc_imap_idle(imap: &mut dc_imap_t) { || r == MAILSTREAM_IDLE_CANCELLED as libc::c_int { dc_log_info( - imap.context, + context, 0, b"IMAP-IDLE wait cancelled, r=%i, r2=%i; we\'ll reconnect soon.\x00" as *const u8 @@ -1183,25 +1195,25 @@ pub unsafe fn dc_imap_idle(imap: &mut dc_imap_t) { imap.should_reconnect = 1 } else if r == MAILSTREAM_IDLE_INTERRUPTED as libc::c_int { dc_log_info( - imap.context, + context, 0, b"IMAP-IDLE interrupted.\x00" as *const u8 as *const libc::c_char, ); } else if r == MAILSTREAM_IDLE_HASDATA as libc::c_int { dc_log_info( - imap.context, + context, 0, b"IMAP-IDLE has data.\x00" as *const u8 as *const libc::c_char, ); } else if r == MAILSTREAM_IDLE_TIMEOUT as libc::c_int { dc_log_info( - imap.context, + context, 0, b"IMAP-IDLE timeout.\x00" as *const u8 as *const libc::c_char, ); } else { dc_log_warning( - imap.context, + context, 0, b"IMAP-IDLE returns unknown value r=%i, r2=%i.\x00" as *const u8 as *const libc::c_char, @@ -1214,17 +1226,17 @@ pub unsafe fn dc_imap_idle(imap: &mut dc_imap_t) { } } } else { - fake_idle(imap); + fake_idle(context, imap); } } -unsafe fn fake_idle(imap: &mut dc_imap_t) { +unsafe fn fake_idle(context: &dc_context_t, imap: &mut dc_imap_t) { /* Idle using timeouts. This is also needed if we're not yet configured - in this case, we're waiting for a configure job */ let mut fake_idle_start_time: time_t = time(0 as *mut time_t); let mut seconds_to_wait: time_t = 0 as time_t; dc_log_info( - imap.context, + context, 0, b"IMAP-fake-IDLEing...\x00" as *const u8 as *const libc::c_char, ); @@ -1236,7 +1248,8 @@ unsafe fn fake_idle(imap: &mut dc_imap_t) { } else { 60 }) as time_t; - pthread_mutex_lock(&mut imap.watch_condmutex); + // FIXME + // pthread_mutex_lock(&mut imap.watch_condmutex); let mut r: libc::c_int = 0; let mut wakeup_at: timespec = timespec { tv_sec: 0, @@ -1248,23 +1261,24 @@ unsafe fn fake_idle(imap: &mut dc_imap_t) { ::std::mem::size_of::(), ); wakeup_at.tv_sec = time(0 as *mut time_t) + seconds_to_wait; - while imap.watch_condflag == 0 && r == 0 { - r = pthread_cond_timedwait( - &mut imap.watch_cond, - &mut imap.watch_condmutex, - &mut wakeup_at, - ); - if 0 != imap.watch_condflag { - do_fake_idle = 0 - } - } - imap.watch_condflag = 0; - pthread_mutex_unlock(&mut imap.watch_condmutex); + // FIXME + // while imap.watch_condflag == 0 && r == 0 { + // r = pthread_cond_timedwait( + // &mut imap.watch_cond, + // &mut imap.watch_condmutex, + // &mut wakeup_at, + // ); + // if 0 != imap.watch_condflag { + // do_fake_idle = 0 + // } + // } + // imap.watch_condflag = 0; + // pthread_mutex_unlock(&mut imap.watch_condmutex); if do_fake_idle == 0 { return; } - if 0 != setup_handle_if_needed(imap) { - if 0 != fetch_from_single_folder(imap, imap.watch_folder) { + if 0 != setup_handle_if_needed(context, imap) { + if 0 != fetch_from_single_folder(context, imap, imap.watch_folder) { do_fake_idle = 0 } } else { @@ -1279,17 +1293,19 @@ pub unsafe fn dc_imap_interrupt_idle(imap: &mut dc_imap_t) { mailstream_interrupt_idle((*imap.etpan).imap_stream); } } - pthread_mutex_lock(&mut imap.watch_condmutex); - imap.watch_condflag = 1; - pthread_cond_signal(&mut imap.watch_cond); - pthread_mutex_unlock(&mut imap.watch_condmutex); + // FIXME + // pthread_mutex_lock(&mut imap.watch_condmutex); + // imap.watch_condflag = 1; + // pthread_cond_signal(&mut imap.watch_cond); + // pthread_mutex_unlock(&mut imap.watch_condmutex); } pub unsafe fn dc_imap_move( + context: &dc_context_t, imap: &mut dc_imap_t, folder: *const libc::c_char, uid: uint32_t, - _folder: *const libc::c_char, + dest_folder: *const libc::c_char, dest_uid: *mut uint32_t, ) -> dc_imap_res { let mut current_block: u64; @@ -1308,7 +1324,7 @@ pub unsafe fn dc_imap_move( res = DC_FAILED } else if strcasecmp(folder, dest_folder) == 0 { dc_log_info( - imap.context, + context, 0, b"Skip moving message; message %s/%i is already in %s...\x00" as *const u8 as *const libc::c_char, @@ -1319,16 +1335,16 @@ pub unsafe fn dc_imap_move( res = DC_ALREADY_DONE } else { dc_log_info( - imap.context, + context, 0, b"Moving message %s/%i to %s...\x00" as *const u8 as *const libc::c_char, folder, uid as libc::c_int, dest_folder, ); - if select_folder(imap, folder) == 0 { + if select_folder(context, imap, folder) == 0 { dc_log_warning( - imap.context, + context, 0, b"Cannot select folder %s for moving message.\x00" as *const u8 as *const libc::c_char, @@ -1343,7 +1359,7 @@ pub unsafe fn dc_imap_move( &mut res_setsrc, &mut res_setdest, ); - if 0 != dc_imap_is_error(imap, r) { + if 0 != dc_imap_is_error(context, imap, r) { if !res_setsrc.is_null() { mailimap_set_free(res_setsrc); res_setsrc = 0 as *mut mailimap_set @@ -1353,7 +1369,7 @@ pub unsafe fn dc_imap_move( res_setdest = 0 as *mut mailimap_set } dc_log_info( - imap.context, + context, 0, b"Cannot move message, fallback to COPY/DELETE %s/%i to %s...\x00" as *const u8 as *const libc::c_char, @@ -1369,9 +1385,9 @@ pub unsafe fn dc_imap_move( &mut res_setsrc, &mut res_setdest, ); - if 0 != dc_imap_is_error(imap, r) { + if 0 != dc_imap_is_error(context, imap, r) { dc_log_info( - imap.context, + context, 0, b"Cannot copy message.\x00" as *const u8 as *const libc::c_char, ); @@ -1379,7 +1395,7 @@ pub unsafe fn dc_imap_move( } else { if add_flag(imap, uid, mailimap_flag_new_deleted()) == 0 { dc_log_warning( - imap.context, + context, 0, b"Cannot mark message as \"Deleted\".\x00" as *const u8 as *const libc::c_char, @@ -1463,6 +1479,7 @@ unsafe fn add_flag( } pub unsafe fn dc_imap_set_seen( + context: &dc_context_t, imap: &mut dc_imap_t, folder: *const libc::c_char, uid: uint32_t, @@ -1472,15 +1489,15 @@ pub unsafe fn dc_imap_set_seen( res = DC_FAILED } else if !imap.etpan.is_null() { dc_log_info( - imap.context, + context, 0, b"Marking message %s/%i as seen...\x00" as *const u8 as *const libc::c_char, folder, uid as libc::c_int, ); - if select_folder(imap, folder) == 0 { + if select_folder(context, imap, folder) == 0 { dc_log_warning( - imap.context, + context, 0, b"Cannot select folder %s for setting SEEN flag.\x00" as *const u8 as *const libc::c_char, @@ -1488,7 +1505,7 @@ pub unsafe fn dc_imap_set_seen( ); } else if add_flag(imap, uid, mailimap_flag_new_seen()) == 0 { dc_log_warning( - imap.context, + context, 0, b"Cannot mark message as seen.\x00" as *const u8 as *const libc::c_char, ); @@ -1508,6 +1525,7 @@ pub unsafe fn dc_imap_set_seen( } pub unsafe fn dc_imap_set_mdnsent( + context: &dc_context_t, imap: &mut dc_imap_t, folder: *const libc::c_char, uid: uint32_t, @@ -1522,15 +1540,15 @@ pub unsafe fn dc_imap_set_mdnsent( res = DC_FAILED } else if !imap.etpan.is_null() { dc_log_info( - imap.context, + context, 0, b"Marking message %s/%i as $MDNSent...\x00" as *const u8 as *const libc::c_char, folder, uid as libc::c_int, ); - if select_folder(imap, folder) == 0 { + if select_folder(context, imap, folder) == 0 { dc_log_warning( - imap.context, + context, 0, b"Cannot select folder %s for setting $MDNSent flag.\x00" as *const u8 as *const libc::c_char, @@ -1584,7 +1602,7 @@ pub unsafe fn dc_imap_set_mdnsent( if 0 != can_create_flag { let mut r: libc::c_int = mailimap_uid_fetch(imap.etpan, set, imap.fetch_type_flags, &mut fetch_result); - if 0 != dc_imap_is_error(imap, r) || fetch_result.is_null() { + if 0 != dc_imap_is_error(context, imap, r) || fetch_result.is_null() { fetch_result = 0 as *mut clist } else { let mut cur: *mut clistiter = (*fetch_result).first; @@ -1616,7 +1634,7 @@ pub unsafe fn dc_imap_set_mdnsent( 17044610252497760460 => {} _ => { dc_log_info( - imap.context, + context, 0, if res as libc::c_uint == DC_SUCCESS as libc::c_int as libc::c_uint @@ -1636,7 +1654,7 @@ pub unsafe fn dc_imap_set_mdnsent( } else { res = DC_SUCCESS; dc_log_info( - imap.context, + context, 0, b"Cannot store $MDNSent flags, risk sending duplicate MDN.\x00" as *const u8 as *const libc::c_char, @@ -1724,6 +1742,7 @@ unsafe fn peek_flag_keyword( /* only returns 0 on connection problems; we should try later again in this case */ pub unsafe fn dc_imap_delete_msg( + context: &dc_context_t, imap: &mut dc_imap_t, rfc724_mid: *const libc::c_char, folder: *const libc::c_char, @@ -1742,7 +1761,7 @@ pub unsafe fn dc_imap_delete_msg( success = 1 } else { dc_log_info( - imap.context, + context, 0, b"Marking message \"%s\", %s/%i for deletion...\x00" as *const u8 as *const libc::c_char, @@ -1750,9 +1769,9 @@ pub unsafe fn dc_imap_delete_msg( folder, server_uid as libc::c_int, ); - if select_folder(imap, folder) == 0 { + if select_folder(context, imap, folder) == 0 { dc_log_warning( - imap.context, + context, 0, b"Cannot select folder %s for deleting message.\x00" as *const u8 as *const libc::c_char, @@ -1767,10 +1786,10 @@ pub unsafe fn dc_imap_delete_msg( mailimap_set_free(set); set = 0 as *mut mailimap_set } - if 0 != dc_imap_is_error(imap, r) || fetch_result.is_null() { + if 0 != dc_imap_is_error(context, imap, r) || fetch_result.is_null() { fetch_result = 0 as *mut clist; dc_log_warning( - imap.context, + context, 0, b"Cannot delete on IMAP, %s/%i not found.\x00" as *const u8 as *const libc::c_char, @@ -1798,7 +1817,7 @@ pub unsafe fn dc_imap_delete_msg( || strcmp(is_rfc724_mid, rfc724_mid) != 0 { dc_log_warning( - imap.context, + context, 0, b"Cannot delete on IMAP, %s/%i does not match %s.\x00" as *const u8 as *const libc::c_char, @@ -1811,7 +1830,7 @@ pub unsafe fn dc_imap_delete_msg( /* mark the message for deletion */ if add_flag(imap, server_uid, mailimap_flag_new_deleted()) == 0 { dc_log_warning( - imap.context, + context, 0, b"Cannot mark message as \"Deleted\".\x00" as *const u8 as *const libc::c_char, ); @@ -1828,7 +1847,7 @@ pub unsafe fn dc_imap_delete_msg( free(is_rfc724_mid as *mut libc::c_void); free(new_folder as *mut libc::c_void); - 0 != success { + if 0 != success { 1 } else { dc_imap_is_connected(imap) diff --git a/src/dc_imex.rs b/src/dc_imex.rs index 539e1eadf..9b66c178a 100644 --- a/src/dc_imex.rs +++ b/src/dc_imex.rs @@ -1347,7 +1347,7 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char * Classic key import ******************************************************************************/ unsafe fn import_self_keys( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut dir_name: *const libc::c_char, ) -> libc::c_int { /* hint: even if we switch to import Autocrypt Setup Files, we should leave the possibility to import @@ -1481,7 +1481,7 @@ unsafe fn import_self_keys( return imported_cnt; } unsafe fn export_self_keys( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut dir: *const libc::c_char, ) -> libc::c_int { let mut success: libc::c_int = 0i32; @@ -1522,7 +1522,7 @@ unsafe fn export_self_keys( * Classic key export ******************************************************************************/ unsafe fn export_key_to_asc_file( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut dir: *const libc::c_char, mut id: libc::c_int, mut key: *const dc_key_t, diff --git a/src/dc_job.rs b/src/dc_job.rs index 0662900a2..1d949c0f7 100644 --- a/src/dc_job.rs +++ b/src/dc_job.rs @@ -1001,7 +1001,7 @@ unsafe fn dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(mut context: &dc_context_t, mut jo dc_msg_unref(msg); } /* delete all pending jobs with the given action */ -pub unsafe fn dc_job_kill_action(mut context: *mut dc_context_t, mut action: libc::c_int) { +pub unsafe fn dc_job_kill_action(mut context: &dc_context_t, mut action: libc::c_int) { if context.is_null() { return; } @@ -1013,7 +1013,7 @@ pub unsafe fn dc_job_kill_action(mut context: *mut dc_context_t, mut action: lib sqlite3_step(stmt); sqlite3_finalize(stmt); } -pub unsafe fn dc_perform_imap_fetch(mut context: *mut dc_context_t) { +pub unsafe fn dc_perform_imap_fetch(mut context: &dc_context_t) { let mut start: libc::clock_t = clock(); if 0 == connect_to_inbox(context) { return; @@ -1228,7 +1228,7 @@ pub unsafe fn dc_perform_smtp_idle(mut context: &dc_context_t) { b"SMTP-idle ended.\x00" as *const u8 as *const libc::c_char, ); } -unsafe fn get_next_wakeup_time(mut context: *mut dc_context_t, mut thread: libc::c_int) -> time_t { +unsafe fn get_next_wakeup_time(mut context: &dc_context_t, mut thread: libc::c_int) -> time_t { let mut wakeup_time: time_t = 0i32 as time_t; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( @@ -1246,7 +1246,7 @@ unsafe fn get_next_wakeup_time(mut context: *mut dc_context_t, mut thread: libc: sqlite3_finalize(stmt); return wakeup_time; } -pub unsafe fn dc_maybe_network(mut context: *mut dc_context_t) { +pub unsafe fn dc_maybe_network(mut context: &dc_context_t) { pthread_mutex_lock(&mut (*context).smtpidle_condmutex); (*context).probe_smtp_network = 1i32; pthread_mutex_unlock(&mut (*context).smtpidle_condmutex); @@ -1274,7 +1274,7 @@ pub unsafe fn dc_job_action_exists( return job_exists; } /* special case for DC_JOB_SEND_MSG_TO_SMTP */ -pub unsafe fn dc_job_send_msg(mut context: *mut dc_context_t, mut msg_id: uint32_t) -> libc::c_int { +pub unsafe fn dc_job_send_msg(mut context: &dc_context_t, mut msg_id: uint32_t) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut mimefactory: dc_mimefactory_t = dc_mimefactory_t { from_addr: 0 as &libc::c_char, @@ -1296,7 +1296,7 @@ pub unsafe fn dc_job_send_msg(mut context: *mut dc_context_t, mut msg_id: uint32 out_gossiped: 0, out_last_added_location_id: 0, error: 0 as *mut libc::c_char, - context: 0 as *mut dc_context_t, + context: std::ptr::null_mut(), }; dc_mimefactory_init(&mut mimefactory, context); /* load message data */ @@ -1381,7 +1381,6 @@ pub unsafe fn dc_job_send_msg(mut context: *mut dc_context_t, mut msg_id: uint32 dc_strdup(mimefactory.from_addr) as *mut libc::c_void, ); } - dc_sqlite3_begin_transaction((*context).sql); if 0 != mimefactory.out_gossiped { dc_set_gossiped_timestamp( context, @@ -1416,7 +1415,6 @@ pub unsafe fn dc_job_send_msg(mut context: *mut dc_context_t, mut msg_id: uint32 0 as *const libc::c_char, 0 as *const libc::c_char, ); - dc_sqlite3_commit((*context).sql); success = dc_add_smtp_job(context, 5901i32, &mut mimefactory) } } diff --git a/src/dc_jobthread.rs b/src/dc_jobthread.rs index e9faa7a69..f3ae31622 100644 --- a/src/dc_jobthread.rs +++ b/src/dc_jobthread.rs @@ -1,4 +1,4 @@ -use std::sync::{Condvar, Mutex}; +use std::sync::{Arc, Condvar, Mutex}; use libc; @@ -18,7 +18,7 @@ pub struct dc_jobthread_t { pub context: *mut dc_context_t, pub name: *mut libc::c_char, pub folder_config_name: *mut libc::c_char, - pub imap: *mut dc_imap_t, + pub imap: Arc>, pub idle: (Mutex, Condvar), pub jobs_needed: libc::c_int, pub suspended: libc::c_int, @@ -28,13 +28,13 @@ pub struct dc_jobthread_t { pub unsafe fn dc_jobthread_init( name: *const libc::c_char, folder_config_name: *const libc::c_char, - imap: *mut dc_imap_t, + imap: dc_imap_t, ) -> dc_jobthread_t { dc_jobthread_t { context: std::ptr::null_mut(), name: dc_strdup(name), folder_config_name: dc_strdup(folder_config_name), - imap, + imap: Arc::new(Mutex::New(imap)), idle: (Mutex::new(false), Condvar::new()), jobs_needed: 0i32, suspended: 0i32, diff --git a/src/dc_key.rs b/src/dc_key.rs index d301ef585..c2567de81 100644 --- a/src/dc_key.rs +++ b/src/dc_key.rs @@ -256,7 +256,7 @@ pub unsafe fn dc_key_save_self_keypair( mut private_key: *const dc_key_t, mut addr: *const libc::c_char, mut is_default: libc::c_int, - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, ) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; @@ -292,7 +292,7 @@ pub unsafe fn dc_key_save_self_keypair( pub unsafe fn dc_key_load_self_public( mut key: *mut dc_key_t, mut self_addr: *const libc::c_char, - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, ) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; @@ -315,7 +315,7 @@ pub unsafe fn dc_key_load_self_public( pub unsafe fn dc_key_load_self_private( mut key: *mut dc_key_t, mut self_addr: *const libc::c_char, - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, ) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; diff --git a/src/dc_keyring.rs b/src/dc_keyring.rs index 3abec44b9..50c0c8e94 100644 --- a/src/dc_keyring.rs +++ b/src/dc_keyring.rs @@ -56,7 +56,7 @@ pub unsafe fn dc_keyring_add(mut keyring: *mut dc_keyring_t, mut to_add: *mut dc pub unsafe fn dc_keyring_load_self_private_for_decrypting( mut keyring: *mut dc_keyring_t, mut self_addr: *const libc::c_char, - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, ) -> libc::c_int { if keyring.is_null() || self_addr.is_null() || sql.is_null() { return 0i32; diff --git a/src/dc_loginparam.rs b/src/dc_loginparam.rs index 34e2faa1e..344731ba4 100644 --- a/src/dc_loginparam.rs +++ b/src/dc_loginparam.rs @@ -61,7 +61,7 @@ pub unsafe fn dc_loginparam_empty(mut loginparam: *mut dc_loginparam_t) { } pub unsafe fn dc_loginparam_read( mut loginparam: *mut dc_loginparam_t, - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, mut prefix: *const libc::c_char, ) { let mut key: *mut libc::c_char = 0 as *mut libc::c_char; @@ -140,7 +140,7 @@ pub unsafe fn dc_loginparam_read( } pub unsafe fn dc_loginparam_write( mut loginparam: *const dc_loginparam_t, - mut sql: *mut dc_sqlite3_t, + mut sql: &mut dc_sqlite3_t, mut prefix: *const libc::c_char, ) { let mut key: *mut libc::c_char = 0 as *mut libc::c_char; diff --git a/src/dc_mimefactory.rs b/src/dc_mimefactory.rs index 6df7ffbf5..0201afd7e 100644 --- a/src/dc_mimefactory.rs +++ b/src/dc_mimefactory.rs @@ -19,7 +19,7 @@ use crate::x::*; #[derive(Copy, Clone)] #[repr(C)] -pub struct dc_mimefactory_t { +pub struct dc_mimefactory_t<'a> { pub from_addr: *mut libc::c_char, pub from_displayname: *mut libc::c_char, pub selfstatus: *mut libc::c_char, @@ -28,8 +28,8 @@ pub struct dc_mimefactory_t { pub timestamp: time_t, pub rfc724_mid: *mut libc::c_char, pub loaded: dc_mimefactory_loaded_t, - pub msg: *mut dc_msg_t, - pub chat: *mut dc_chat_t, + pub msg: *mut dc_msg_t<'a>, + pub chat: *mut dc_chat_t<'a>, pub increation: libc::c_int, pub in_reply_to: *mut libc::c_char, pub references: *mut libc::c_char, @@ -39,7 +39,7 @@ pub struct dc_mimefactory_t { pub out_gossiped: libc::c_int, pub out_last_added_location_id: uint32_t, pub error: *mut libc::c_char, - pub context: *mut dc_context_t, + pub context: &'a dc_context_t, } pub type dc_mimefactory_loaded_t = libc::c_uint; @@ -47,7 +47,10 @@ pub const DC_MF_MDN_LOADED: dc_mimefactory_loaded_t = 2; pub const DC_MF_MSG_LOADED: dc_mimefactory_loaded_t = 1; pub const DC_MF_NOTHING_LOADED: dc_mimefactory_loaded_t = 0; -pub unsafe fn dc_mimefactory_init(mut factory: *mut dc_mimefactory_t, mut context: &dc_context_t) { +pub unsafe fn dc_mimefactory_init<'a>( + factory: *mut dc_mimefactory_t<'a>, + context: &'a dc_context_t, +) { if factory.is_null() || context.is_null() { return; } diff --git a/src/dc_msg.rs b/src/dc_msg.rs index ae73cf388..72c9c8e25 100644 --- a/src/dc_msg.rs +++ b/src/dc_msg.rs @@ -20,7 +20,7 @@ use crate::x::*; /* * the structure behind dc_msg_t */ #[derive(Copy, Clone)] #[repr(C)] -pub struct dc_msg_t { +pub struct dc_msg_t<'a> { pub magic: uint32_t, pub id: uint32_t, pub from_id: uint32_t, @@ -34,7 +34,7 @@ pub struct dc_msg_t { pub timestamp_sent: time_t, pub timestamp_rcvd: time_t, pub text: *mut libc::c_char, - pub context: *mut dc_context_t, + pub context: &'a dc_context_t, pub rfc724_mid: *mut libc::c_char, pub in_reply_to: *mut libc::c_char, pub server_folder: *mut libc::c_char, @@ -292,9 +292,10 @@ pub unsafe fn dc_get_msg_info( free(rawtxt as *mut libc::c_void); return ret.buf; } -pub unsafe fn dc_msg_new_untyped(mut context: &dc_context_t) -> *mut dc_msg_t { - return dc_msg_new(context, 0i32); +pub unsafe fn dc_msg_new_untyped<'a>(context: &'a dc_context_t) -> *mut dc_msg_t<'a> { + dc_msg_new(context, 0i32) } + /* * * @class dc_msg_t * @@ -305,7 +306,10 @@ pub unsafe fn dc_msg_new_untyped(mut context: &dc_context_t) -> *mut dc_msg_t { // to check if a mail was sent, use dc_msg_is_sent() // approx. max. lenght returned by dc_msg_get_text() // approx. max. lenght returned by dc_get_msg_info() -pub unsafe fn dc_msg_new(mut context: &dc_context_t, mut viewtype: libc::c_int) -> *mut dc_msg_t { +pub unsafe fn dc_msg_new<'a>( + mut context: &'a dc_context_t, + mut viewtype: libc::c_int, +) -> *mut dc_msg_t<'a> { let mut msg: *mut dc_msg_t = 0 as *mut dc_msg_t; msg = calloc(1, ::std::mem::size_of::()) as *mut dc_msg_t; if msg.is_null() { @@ -501,7 +505,7 @@ pub unsafe fn dc_msg_get_timestamp(mut msg: *const dc_msg_t) -> time_t { } pub unsafe fn dc_msg_load_from_db( mut msg: *mut dc_msg_t, - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut id: uint32_t, ) -> libc::c_int { let mut success: libc::c_int = 0i32; @@ -605,7 +609,7 @@ unsafe fn dc_msg_set_from_stmt( return 1i32; } pub unsafe fn dc_get_mime_headers( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut msg_id: uint32_t, ) -> *mut libc::c_char { let mut eml: &libc::c_char = 0 as *mut libc::c_char; @@ -624,7 +628,7 @@ pub unsafe fn dc_get_mime_headers( return eml; } pub unsafe fn dc_delete_msgs( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut msg_ids: *const uint32_t, mut msg_cnt: libc::c_int, ) { @@ -635,7 +639,6 @@ pub unsafe fn dc_delete_msgs( { return; } - dc_sqlite3_begin_transaction((*context).sql); let mut i: libc::c_int = 0i32; while i < msg_cnt { dc_update_msg_chat_id(context, *msg_ids.offset(i as isize), 3i32 as uint32_t); @@ -648,7 +651,7 @@ pub unsafe fn dc_delete_msgs( ); i += 1 } - dc_sqlite3_commit((*context).sql); + if 0 != msg_cnt { ((*context).cb)( context, @@ -675,7 +678,7 @@ pub unsafe fn dc_update_msg_chat_id( sqlite3_finalize(stmt); } pub unsafe fn dc_markseen_msgs( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut msg_ids: *const uint32_t, mut msg_cnt: libc::c_int, ) { @@ -690,7 +693,6 @@ pub unsafe fn dc_markseen_msgs( || msg_ids.is_null() || msg_cnt <= 0i32) { - dc_sqlite3_begin_transaction((*context).sql); transaction_pending = 1i32; stmt = dc_sqlite3_prepare((*context).sql, @@ -728,7 +730,6 @@ pub unsafe fn dc_markseen_msgs( } i += 1 } - dc_sqlite3_commit((*context).sql); transaction_pending = 0i32; if 0 != send_event { ((*context).cb)( @@ -739,18 +740,15 @@ pub unsafe fn dc_markseen_msgs( ); } } - if 0 != transaction_pending { - dc_sqlite3_rollback((*context).sql); - } sqlite3_finalize(stmt); } pub unsafe fn dc_update_msg_state( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut msg_id: uint32_t, mut state: libc::c_int, ) { let mut stmt: &sqlite3_stmt = dc_sqlite3_prepare( - (*context).sql, + &mut context.sql.lock().unwrap(), b"UPDATE msgs SET state=? WHERE id=?;\x00" as *const u8 as *const libc::c_char, ); sqlite3_bind_int(stmt, 1i32, state); @@ -759,7 +757,7 @@ pub unsafe fn dc_update_msg_state( sqlite3_finalize(stmt); } pub unsafe fn dc_star_msgs( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut msg_ids: *const uint32_t, mut msg_cnt: libc::c_int, mut star: libc::c_int, @@ -772,7 +770,6 @@ pub unsafe fn dc_star_msgs( { return; } - dc_sqlite3_begin_transaction((*context).sql); let mut stmt: &sqlite3_stmt = dc_sqlite3_prepare( (*context).sql, b"UPDATE msgs SET starred=? WHERE id=?;\x00" as *const u8 as *const libc::c_char, @@ -786,23 +783,21 @@ pub unsafe fn dc_star_msgs( i += 1 } sqlite3_finalize(stmt); - dc_sqlite3_commit((*context).sql); } -pub unsafe fn dc_get_msg(mut context: *mut dc_context_t, mut msg_id: uint32_t) -> *mut dc_msg_t { +pub unsafe fn dc_get_msg<'a>(context: &'a dc_context_t, msg_id: uint32_t) -> *mut dc_msg_t<'a> { let mut success: libc::c_int = 0i32; let mut obj: *mut dc_msg_t = dc_msg_new_untyped(context); - if !(context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint) { - if !(0 == dc_msg_load_from_db(obj, context, msg_id)) { - success = 1i32 - } + if !(0 == dc_msg_load_from_db(obj, context, msg_id)) { + success = 1i32 } if 0 != success { - return obj; + obj } else { dc_msg_unref(obj); - return 0 as &dc_msg_t; - }; + 0 as &dc_msg_t + } } + pub unsafe fn dc_msg_get_id(mut msg: *const dc_msg_t) -> uint32_t { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32 as uint32_t; @@ -970,7 +965,7 @@ pub unsafe fn dc_msg_get_summarytext_by_raw( mut text: *const libc::c_char, mut param: *mut dc_param_t, mut approx_characters: libc::c_int, - mut context: *mut dc_context_t, + mut context: &dc_context_t, ) -> *mut libc::c_char { /* get a summary text, result must be free()'d, never returns NULL. */ let mut ret: &libc::c_char = 0 as *mut libc::c_char; @@ -1219,15 +1214,16 @@ pub unsafe fn dc_msg_save_param_to_disk(mut msg: *mut dc_msg_t) { sqlite3_step(stmt); sqlite3_finalize(stmt); } -pub unsafe fn dc_msg_new_load( - mut context: *mut dc_context_t, - mut msg_id: uint32_t, -) -> *mut dc_msg_t { +pub unsafe fn dc_msg_new_load<'a>( + context: &'a dc_context_t, + msg_id: uint32_t, +) -> *mut dc_msg_t<'a> { let mut msg: &dc_msg_t = dc_msg_new_untyped(context); dc_msg_load_from_db(msg, context, msg_id); - return msg; + msg } -pub unsafe fn dc_delete_msg_from_db(mut context: *mut dc_context_t, mut msg_id: uint32_t) { + +pub unsafe fn dc_delete_msg_from_db(context: &dc_context_t, mut msg_id: uint32_t) { let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; if !(0 == dc_msg_load_from_db(msg, context, msg_id)) { @@ -1256,7 +1252,7 @@ Do not use too long subjects - we add a tag after the subject which gets truncat It should also be very clear, the subject is _not_ the whole message. The value is also used for CC:-summaries */ // Context functions to work with messages -pub unsafe fn dc_msg_exists(mut context: *mut dc_context_t, mut msg_id: uint32_t) -> libc::c_int { +pub unsafe fn dc_msg_exists(mut context: &dc_context_t, mut msg_id: uint32_t) -> libc::c_int { let mut msg_exists: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; if !(context.is_null() @@ -1295,7 +1291,7 @@ pub unsafe fn dc_update_msg_move_state( sqlite3_finalize(stmt); } pub unsafe fn dc_set_msg_failed( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut msg_id: uint32_t, mut error: *const libc::c_char, ) { @@ -1334,7 +1330,7 @@ pub unsafe fn dc_set_msg_failed( } /* returns 1 if an event should be send */ pub unsafe fn dc_mdn_from_ext( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut from_id: uint32_t, mut rfc724_mid: *const libc::c_char, mut timestamp_sent: time_t, @@ -1443,7 +1439,7 @@ pub unsafe fn dc_mdn_from_ext( return read_by_all; } /* the number of messages assigned to real chat (!=deaddrop, !=trash) */ -pub unsafe fn dc_get_real_msg_cnt(mut context: *mut dc_context_t) -> size_t { +pub unsafe fn dc_get_real_msg_cnt(mut context: &dc_context_t) -> size_t { let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut ret: size_t = 0i32 as size_t; if !(*(*context).sql).cobj.is_null() { @@ -1506,7 +1502,7 @@ pub unsafe fn dc_rfc724_mid_cnt( return ret; } pub unsafe fn dc_rfc724_mid_exists( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut rfc724_mid: *const libc::c_char, mut ret_server_folder: *mut *mut libc::c_char, mut ret_server_uid: &uint32_t, @@ -1544,7 +1540,7 @@ pub unsafe fn dc_rfc724_mid_exists( return ret; } pub unsafe fn dc_update_server_uid( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut rfc724_mid: *const libc::c_char, mut server_folder: *const libc::c_char, mut server_uid: uint32_t, diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 46bfb91b9..4f7f56061 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -107,7 +107,6 @@ pub unsafe fn dc_receive_imf( sent_timestamp = dc_timestamp_from_date((*orig_date).dt_date_time) } } - dc_sqlite3_begin_transaction((*context).sql); transaction_pending = 1i32; field = dc_mimeparser_lookup_field( mime_parser, @@ -226,7 +225,6 @@ pub unsafe fn dc_receive_imf( if strcmp(old_server_folder, server_folder) != 0i32 || old_server_uid != server_uid { - dc_sqlite3_rollback((*context).sql); transaction_pending = 0i32; dc_update_server_uid( context, @@ -282,7 +280,6 @@ pub unsafe fn dc_receive_imf( msgrmsg = 1i32; chat_id = 0i32 as uint32_t; allow_creation = 1i32; - dc_sqlite3_commit((*context).sql); let mut handshake: libc::c_int = dc_handle_securejoin_handshake( context, mime_parser, @@ -293,7 +290,6 @@ pub unsafe fn dc_receive_imf( add_delete_job = handshake & 0x4i32; state = 16i32 } - dc_sqlite3_begin_transaction((*context).sql); } let mut test_normal_chat_id: uint32_t = 0i32 as uint32_t; let mut test_normal_chat_id_blocked: libc::c_int = 0i32; @@ -988,15 +984,11 @@ pub unsafe fn dc_receive_imf( 0i32, ); } - dc_sqlite3_commit((*context).sql); transaction_pending = 0i32 } } } } - if 0 != transaction_pending { - dc_sqlite3_rollback((*context).sql); - } dc_mimeparser_unref(mime_parser); free(rfc724_mid as *mut libc::c_void); free(mime_in_reply_to as *mut libc::c_void); diff --git a/src/dc_securejoin.rs b/src/dc_securejoin.rs index 88e356c7f..3a397ea8f 100644 --- a/src/dc_securejoin.rs +++ b/src/dc_securejoin.rs @@ -1034,7 +1034,7 @@ unsafe fn encrypted_and_signed( return 1i32; } pub unsafe fn dc_handle_degrade_event( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut peerstate: *mut dc_apeerstate_t, ) { let mut stmt: &sqlite3_stmt = 0 as *mut sqlite3_stmt; diff --git a/src/dc_sqlite3.rs b/src/dc_sqlite3.rs index b49905999..8721919d2 100644 --- a/src/dc_sqlite3.rs +++ b/src/dc_sqlite3.rs @@ -1,6 +1,5 @@ use libc; -use crate::constants::*; use crate::dc_apeerstate::*; use crate::dc_context::dc_context_t; use crate::dc_hash::*; @@ -10,6 +9,9 @@ use crate::dc_tools::*; use crate::types::*; use crate::x::*; +const DC_OPEN_READONLY: usize = 0x01; +const DC_HOUSEKEEPING_DELAY_SEC: usize = 10; + /// A simple wrapper around the underlying Sqlite3 object. #[repr(C)] pub struct dc_sqlite3_t { @@ -42,7 +44,7 @@ pub unsafe fn dc_sqlite3_close(context: &dc_context_t, sql: &mut dc_sqlite3_t) { } pub unsafe fn dc_sqlite3_open( - context: &context, + context: &dc_context_t, sql: &mut dc_sqlite3_t, dbfile: *const libc::c_char, flags: libc::c_int, @@ -71,7 +73,7 @@ pub unsafe fn dc_sqlite3_open( dbfile, &mut sql.cobj, SQLITE_OPEN_FULLMUTEX - | (if (flags & DC_OPEN_READONLY) { + | (if flags & DC_OPEN_READONLY { SQLITE_OPEN_READONLY } else { SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE @@ -928,7 +930,7 @@ pub unsafe fn dc_sqlite3_open( // handle configurations, private pub unsafe fn dc_sqlite3_set_config( - context: &context, + context: &dc_context_t, sql: &mut dc_sqlite3_t, key: *const libc::c_char, value: *const libc::c_char, @@ -1039,7 +1041,7 @@ pub unsafe fn dc_sqlite3_prepare( } pub unsafe extern "C" fn dc_sqlite3_log_error( - context: &context, + context: &dc_context_t, sql: &dc_sqlite3_t, msg_format: *const libc::c_char, va: ... @@ -1159,6 +1161,7 @@ pub unsafe fn dc_sqlite3_get_config_int( } pub unsafe fn dc_sqlite3_table_exists( + context: &dc_context_t, sql: &mut dc_sqlite3_t, name: *const libc::c_char, ) -> libc::c_int { @@ -1197,7 +1200,7 @@ pub unsafe fn dc_sqlite3_table_exists( } pub unsafe fn dc_sqlite3_set_config_int64( - sql: &mutdc_sqlite3_t, + sql: &mut dc_sqlite3_t, key: *const libc::c_char, value: int64_t, ) -> libc::c_int { diff --git a/src/dc_stock.rs b/src/dc_stock.rs index 7a44ac528..b7604efc7 100644 --- a/src/dc_stock.rs +++ b/src/dc_stock.rs @@ -12,11 +12,12 @@ The result must be free()'d! */ pub unsafe fn dc_stock_str(mut context: &dc_context_t, mut id: libc::c_int) -> *mut libc::c_char { return get_string(context, id, 0i32); } + unsafe fn get_string( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut id: libc::c_int, mut qty: libc::c_int, -) -> &libc::c_char { +) -> *mut libc::c_char { let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; if !context.is_null() { ret = ((*context).cb)( @@ -212,10 +213,10 @@ unsafe fn default_string(mut id: libc::c_int) -> *mut libc::c_char { /* Replaces the first `%1$s` in the given String-ID by the given value. The result must be free()'d! */ pub unsafe fn dc_stock_str_repl_string( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut id: libc::c_int, mut to_insert: *const libc::c_char, -) -> &libc::c_char { +) -> *mut libc::c_char { let mut ret: *mut libc::c_char = get_string(context, id, 0i32); dc_str_replace( &mut ret, @@ -230,10 +231,10 @@ pub unsafe fn dc_stock_str_repl_string( return ret; } pub unsafe fn dc_stock_str_repl_int( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut id: libc::c_int, mut to_insert_int: libc::c_int, -) -> &libc::c_char { +) -> *mut libc::c_char { let mut ret: *mut libc::c_char = get_string(context, id, to_insert_int); let mut to_insert_str: *mut libc::c_char = dc_mprintf( b"%i\x00" as *const u8 as *const libc::c_char, @@ -255,11 +256,11 @@ pub unsafe fn dc_stock_str_repl_int( /* Replaces the first `%1$s` and `%2$s` in the given String-ID by the two given strings. The result must be free()'d! */ pub unsafe fn dc_stock_str_repl_string2( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut id: libc::c_int, mut to_insert: *const libc::c_char, mut to_insert2: *const libc::c_char, -) -> &libc::c_char { +) -> *mut libc::c_char { let mut ret: *mut libc::c_char = get_string(context, id, 0i32); dc_str_replace( &mut ret, @@ -285,12 +286,12 @@ pub unsafe fn dc_stock_str_repl_string2( } /* Misc. */ pub unsafe fn dc_stock_system_msg( - mut context: *mut dc_context_t, + mut context: &dc_context_t, mut str_id: libc::c_int, mut param1: *const libc::c_char, mut param2: *const libc::c_char, mut from_id: uint32_t, -) -> &libc::c_char { +) -> *mut libc::c_char { let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; let mut mod_contact: *mut dc_contact_t = 0 as *mut dc_contact_t; let mut mod_displayname: *mut libc::c_char = 0 as *mut libc::c_char; diff --git a/src/types.rs b/src/types.rs index bdb55956b..5bf577c84 100644 --- a/src/types.rs +++ b/src/types.rs @@ -3,7 +3,6 @@ use libc; use crate::constants::Event; use crate::dc_context::dc_context_t; -use crate::dc_imap::dc_imap_t; use crate::x::*; pub use libc::{ @@ -1173,7 +1172,7 @@ pub type nlink_t = __uint16_t; pub type dc_receive_imf_t = Option< unsafe fn( - _: *mut dc_imap_t, + _: &dc_context_t, _: *const libc::c_char, _: size_t, _: *const libc::c_char, @@ -1181,38 +1180,28 @@ pub type dc_receive_imf_t = Option< _: uint32_t, ) -> (), >; + /* Purpose: Reading from IMAP servers with no dependencies to the database. dc_context_t is only used for logging and to get information about the online state. */ pub type dc_precheck_imf_t = Option< unsafe fn( - _: *mut dc_imap_t, + _: &dc_context_t, _: *const libc::c_char, _: *const libc::c_char, _: uint32_t, ) -> libc::c_int, >; pub type dc_set_config_t = - Option ()>; + Option ()>; pub type dc_get_config_t = Option< unsafe fn( - _: *mut dc_imap_t, + _: &dc_context_t, _: *const libc::c_char, _: *const libc::c_char, ) -> *mut libc::c_char, >; -/* ** library-private **********************************************************/ - -/* * - * Library-internal. - */ -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _dc_sqlite3 { - pub cobj: *mut sqlite3, - pub context: &dc_context_t, -} #[inline] pub unsafe fn isascii(mut _c: libc::c_int) -> libc::c_int {