diff --git a/Cargo.toml b/Cargo.toml index 0db069ab1..18b502809 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,10 @@ num-derive = "0.2.5" num-traits = "0.2.6" native-tls = "0.2.3" lettre = "0.9.0" +imap = "1.0.1" +# rental = "0.5.3" +mmime = { git = "https://github.com/dignifiedquire/mmime" } +base64 = "0.10.1" [dev-dependencies] tempfile = "3.0.7" @@ -42,4 +46,4 @@ path = "examples/repl/main.rs" [features] default = [] -vendored = ["native-tls/vendored"] \ No newline at end of file +vendored = ["native-tls/vendored"] diff --git a/build.rs b/build.rs index 91b08f231..8c5ffb7fb 100644 --- a/build.rs +++ b/build.rs @@ -1,9 +1,5 @@ extern crate cc; -fn link_dylib(lib: &str) { - println!("cargo:rustc-link-lib=dylib={}", lib); -} - fn link_static(lib: &str) { println!("cargo:rustc-link-lib=static={}", lib); } @@ -29,57 +25,12 @@ fn main() { build_tools(); add_search_path("/usr/local/lib"); - add_search_path("./include/include"); - add_search_path("./include/libs"); let target = std::env::var("TARGET").unwrap(); if target.contains("-apple") || target.contains("-darwin") { - link_static("etpan"); - link_dylib("iconv"); - link_framework("CoreFoundation"); link_framework("CoreServices"); link_framework("Security"); - - link_dylib("sasl2"); - link_dylib("z"); - link_dylib("pthread"); - } else if target.contains("-android") { - add_search_path("./include/cyrus-sasl-android-4/include"); - add_search_path("./include/openssl-android-3/include"); - add_search_path("./include/iconv-android-1/include"); - - let arch = if target.contains("x86") { - "x86" - } else if target.contains("64") { - "arm64-v8a" - } else if target.contains("v7") { - "armeabi-v7a" - } else { - "armeabi" - }; - - add_search_path(&format!("./include/libs/{}", arch)); - add_search_path(&format!("./include/iconv-android-1/libs/{}", arch)); - add_search_path(&format!("./include/openssl-android-3/libs/{}", arch)); - add_search_path(&format!("./include/cyrus-sasl-android-4/libs/{}", arch)); - - // dependencies for libetpan - link_static("crypto"); - link_static("sasl2"); - link_static("iconv"); - link_static("ssl"); - link_dylib("z"); - - // libetpan iteself - link_static("etpan"); - } else if target.contains("-linux") { - link_dylib("etpan"); - link_dylib("sasl2"); - link_dylib("z"); - link_dylib("pthread"); - } else { - panic!("unsupported target"); } // local tools diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index 677dee0f7..abbc148cd 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -46,7 +46,6 @@ use deltachat::dc_token::*; use deltachat::dc_tools::*; use deltachat::types::*; use deltachat::x::*; -use libc; use num_traits::FromPrimitive; /* @@ -196,9 +195,9 @@ unsafe extern "C" fn poke_spec( let mut real_spec: *mut libc::c_char = 0 as *mut libc::c_char; let mut suffix: *mut libc::c_char = 0 as *mut libc::c_char; let mut dir: *mut DIR = 0 as *mut DIR; - let mut dir_entry: *mut dirent = 0 as *mut dirent; + let mut dir_entry: *mut dirent; let mut read_cnt: libc::c_int = 0i32; - let mut name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut name: *mut libc::c_char; if 0 == dc_sqlite3_is_open(&context.sql.clone().read().unwrap()) { dc_log_error( context, @@ -390,7 +389,6 @@ unsafe extern "C" fn log_msglist(mut context: &dc_context_t, mut msglist: *mut d let mut i: libc::c_int = 0; let mut cnt: libc::c_int = dc_array_get_cnt(msglist) as libc::c_int; let mut lines_out: libc::c_int = 0i32; - i = 0i32; while i < cnt { let mut msg_id: uint32_t = dc_array_get_id(msglist, i as size_t); if msg_id == 9i32 as libc::c_uint { @@ -421,7 +419,7 @@ unsafe extern "C" fn log_msglist(mut context: &dc_context_t, mut msglist: *mut d }; } unsafe extern "C" fn log_contactlist(mut context: &dc_context_t, mut contacts: *mut dc_array_t) { - let mut contact: *mut dc_contact_t = 0 as *mut dc_contact_t; + let mut contact: *mut dc_contact_t; let mut peerstate: *mut dc_apeerstate_t = dc_apeerstate_new(context); if 0 == dc_array_search_id(contacts, 1i32 as uint32_t, 0 as *mut size_t) { dc_array_add_id(contacts, 1i32 as uint32_t); @@ -429,7 +427,7 @@ unsafe extern "C" fn log_contactlist(mut context: &dc_context_t, mut contacts: * let mut i = 0; while i < dc_array_get_cnt(contacts) { let mut contact_id: uint32_t = dc_array_get_id(contacts, i as size_t); - let mut line: *mut libc::c_char = 0 as *mut libc::c_char; + let mut line: *mut libc::c_char; let mut line2: *mut libc::c_char = 0 as *mut libc::c_char; contact = dc_get_contact(context, contact_id); if !contact.is_null() { @@ -462,7 +460,7 @@ unsafe extern "C" fn log_contactlist(mut context: &dc_context_t, mut contacts: * let mut peerstate_ok: libc::c_int = dc_apeerstate_load_by_addr(peerstate, &context.sql.clone().read().unwrap(), addr); if 0 != peerstate_ok && contact_id != 1i32 as libc::c_uint { - let mut pe: *mut libc::c_char = 0 as *mut libc::c_char; + let mut pe: *mut libc::c_char; match (*peerstate).prefer_encrypt { 1 => pe = dc_strdup(b"mutual\x00" as *const u8 as *const libc::c_char), 0 => pe = dc_strdup(b"no-preference\x00" as *const u8 as *const libc::c_char), @@ -521,8 +519,8 @@ pub unsafe extern "C" fn dc_cmdline( context: &mut dc_context_t, cmdline: &str, ) -> *mut libc::c_char { - let mut cmd = 0 as *mut libc::c_char; - let mut arg1: *mut libc::c_char = 0 as *mut libc::c_char; + let mut cmd: *mut libc::c_char; + let mut arg1: *mut libc::c_char; let mut ret: *mut libc::c_char = 1i32 as *mut libc::c_char; let mut sel_chat: *mut dc_chat_t = 0 as *mut dc_chat_t; @@ -648,7 +646,7 @@ pub unsafe extern "C" fn dc_cmdline( ) } } else if strcmp(cmd, b"has-backup\x00" as *const u8 as *const libc::c_char) == 0i32 { - ret = dc_imex_has_backup(&context, context.blobdir); + ret = dc_imex_has_backup(&context, context.get_blobdir()); if ret.is_null() { ret = dc_strdup(b"No backup found.\x00" as *const u8 as *const libc::c_char) } @@ -657,7 +655,12 @@ pub unsafe extern "C" fn dc_cmdline( b"export-backup\x00" as *const u8 as *const libc::c_char, ) == 0i32 { - dc_imex(&context, 11i32, context.blobdir, 0 as *const libc::c_char); + dc_imex( + &context, + 11i32, + context.get_blobdir(), + 0 as *const libc::c_char, + ); ret = 2i32 as *mut libc::c_char } else if strcmp( cmd, @@ -673,18 +676,28 @@ pub unsafe extern "C" fn dc_cmdline( ) } } else if strcmp(cmd, b"export-keys\x00" as *const u8 as *const libc::c_char) == 0i32 { - dc_imex(&context, 1i32, context.blobdir, 0 as *const libc::c_char); + dc_imex( + &context, + 1i32, + context.get_blobdir(), + 0 as *const libc::c_char, + ); ret = 2i32 as *mut libc::c_char } else if strcmp(cmd, b"import-keys\x00" as *const u8 as *const libc::c_char) == 0i32 { - dc_imex(&context, 2i32, context.blobdir, 0 as *const libc::c_char); + dc_imex( + &context, + 2i32, + context.get_blobdir(), + 0 as *const libc::c_char, + ); ret = 2i32 as *mut libc::c_char } else if strcmp(cmd, b"export-setup\x00" as *const u8 as *const libc::c_char) == 0i32 { let mut setup_code_0: *mut libc::c_char = dc_create_setup_code(&context); let mut file_name: *mut libc::c_char = dc_mprintf( b"%s/autocrypt-setup-message.html\x00" as *const u8 as *const libc::c_char, - context.blobdir, + context.get_blobdir(), ); - let mut file_content: *mut libc::c_char = 0 as *mut libc::c_char; + let mut file_content: *mut libc::c_char; file_content = dc_render_setup_file(&context, setup_code_0); if !file_content.is_null() && 0 != dc_write_file( @@ -784,7 +797,7 @@ pub unsafe extern "C" fn dc_cmdline( let mut chatlist: *mut dc_chatlist_t = dc_get_chatlist(&context, listflags, arg1, 0i32 as uint32_t); if !chatlist.is_null() { - let mut i: libc::c_int = 0; + let mut i: libc::c_int; let mut cnt: libc::c_int = dc_chatlist_get_cnt(chatlist) as libc::c_int; if cnt > 0i32 { dc_log_info(&context, 0i32, @@ -1332,7 +1345,7 @@ pub unsafe extern "C" fn dc_cmdline( if !sel_chat.is_null() { let mut images: *mut dc_array_t = dc_get_chat_media(&context, dc_chat_get_id(sel_chat), 20i32, 21i32, 50i32); - let mut i_0: libc::c_int = 0; + let mut i_0: libc::c_int; let mut icnt: libc::c_int = dc_array_get_cnt(images) as libc::c_int; ret = dc_mprintf( b"%i images or videos: \x00" as *const u8 as *const libc::c_char, @@ -1692,7 +1705,6 @@ pub unsafe extern "C" fn dc_cmdline( } if !sel_chat.is_null() { dc_chat_unref(sel_chat); - sel_chat = 0 as *mut dc_chat_t } free(cmd as *mut libc::c_void); return ret; diff --git a/examples/repl/main.rs b/examples/repl/main.rs index 43feb7ced..5b1eb6702 100644 --- a/examples/repl/main.rs +++ b/examples/repl/main.rs @@ -11,7 +11,6 @@ non_camel_case_types, non_snake_case, non_upper_case_globals, - unused_assignments, unused_mut, unused_attributes, non_upper_case_globals, @@ -68,8 +67,6 @@ use deltachat::dc_token::*; use deltachat::dc_tools::*; use deltachat::types::*; use deltachat::x::*; -use libc; - mod cmdline; use self::cmdline::*; @@ -150,7 +147,7 @@ unsafe extern "C" fn receive_event( let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; let mut tempFile: *mut libc::c_char = dc_get_fine_pathNfilename( context, - (*context).blobdir, + context.get_blobdir(), b"curl.result\x00" as *const u8 as *const libc::c_char, ); let mut cmd: *mut libc::c_char = if event == Event::HTTP_GET { @@ -279,51 +276,55 @@ unsafe fn start_threads( run_threads = 1; let context = c.clone(); let h1 = std::thread::spawn(move || { - let ctx = context.clone(); - let context = ctx.read().unwrap(); while 0 != run_threads { + let ctx = context.clone(); + let context = ctx.read().unwrap(); + + println!("-- imap jobs"); dc_perform_imap_jobs(&context); + println!("-- imap fetch"); dc_perform_imap_fetch(&context); if 0 != run_threads { + println!("-- imap idle"); dc_perform_imap_idle(&context); } } }); - let context = c.clone(); + let _context = c.clone(); let h2 = std::thread::spawn(move || { - let ctx = context.clone(); - let context = ctx.read().unwrap(); - while 0 != run_threads { - dc_perform_mvbox_fetch(&context); - if 0 != run_threads { - dc_perform_mvbox_idle(&context); - } - } + // let ctx = context.clone(); + // let context = ctx.read().unwrap(); + // while 0 != run_threads { + // dc_perform_mvbox_fetch(&context); + // if 0 != run_threads { + // dc_perform_mvbox_idle(&context); + // } + // } }); - let context = c.clone(); + let _context = c.clone(); let h3 = std::thread::spawn(move || { - let ctx = context.clone(); - let context = ctx.read().unwrap(); - while 0 != run_threads { - dc_perform_sentbox_fetch(&context); - if 0 != run_threads { - dc_perform_sentbox_idle(&context); - } - } + // let ctx = context.clone(); + // let context = ctx.read().unwrap(); + // while 0 != run_threads { + // dc_perform_sentbox_fetch(&context); + // if 0 != run_threads { + // dc_perform_sentbox_idle(&context); + // } + // } }); - let context = c.clone(); + let _context = c.clone(); let h4 = std::thread::spawn(move || { - let ctx = context.clone(); - let context = ctx.read().unwrap(); - while 0 != run_threads { - dc_perform_smtp_jobs(&context); - if 0 != run_threads { - dc_perform_smtp_idle(&context); - } - } + // let ctx = context.clone(); + // let context = ctx.read().unwrap(); + // while 0 != run_threads { + // dc_perform_smtp_jobs(&context); + // if 0 != run_threads { + // dc_perform_smtp_idle(&context); + // } + // } }); (h1, h2, h3, h4) @@ -391,8 +392,6 @@ unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> lib let ctx = Arc::new(RwLock::new(context)); loop { - let ctx = ctx.clone(); - /* read command */ let cmdline = read_cmd(); free(cmd as *mut libc::c_void); @@ -426,10 +425,7 @@ unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> lib dc_perform_imap_jobs(&ctx.read().unwrap()); } } else if strcmp(cmd, b"configure\x00" as *const u8 as *const libc::c_char) == 0i32 { - handles = { - let ctx = ctx.clone(); - Some(start_threads(ctx)) - }; + handles = { Some(start_threads(ctx.clone())) }; dc_configure(&ctx.read().unwrap()); } else if strcmp(cmd, b"oauth2\x00" as *const u8 as *const libc::c_char) == 0i32 { let mut addr: *mut libc::c_char = dc_get_config( @@ -463,10 +459,7 @@ unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> lib } else if strcmp(cmd, b"getqr\x00" as *const u8 as *const libc::c_char) == 0i32 || strcmp(cmd, b"getbadqr\x00" as *const u8 as *const libc::c_char) == 0i32 { - handles = { - let ctx = ctx.clone(); - Some(start_threads(ctx)) - }; + handles = Some(start_threads(ctx.clone())); let mut qrstr: *mut libc::c_char = dc_get_securejoin_qr( &ctx.read().unwrap(), (if !arg1.is_null() { atoi(arg1) } else { 0i32 }) as uint32_t, @@ -491,10 +484,7 @@ unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> lib } free(qrstr as *mut libc::c_void); } else if strcmp(cmd, b"joinqr\x00" as *const u8 as *const libc::c_char) == 0i32 { - handles = { - let ctx = ctx.clone(); - Some(start_threads(ctx)) - }; + handles = Some(start_threads(ctx.clone())); if !arg1.is_null() { dc_join_securejoin(&ctx.read().unwrap(), arg1); } @@ -522,7 +512,6 @@ unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> lib let mut ctx = ctx.write().unwrap(); free(cmd as *mut libc::c_void); stop_threads(&ctx, handles); - handles = None; dc_close(&mut ctx); dc_context_unref(&mut ctx); } diff --git a/examples/simple.rs b/examples/simple.rs index 646797ddb..5d892dd73 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,7 +1,8 @@ extern crate deltachat; use std::ffi::{CStr, CString}; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; +use std::{thread, time}; use tempfile::tempdir; use deltachat::constants::Event; @@ -20,6 +21,10 @@ extern "C" fn cb(_ctx: &dc_context_t, event: Event, data1: usize, data2: usize) println!("[{:?}]", event); match event { + Event::CONFIGURE_PROGRESS => { + println!(" progress: {}", data1); + 0 + } Event::HTTP_GET => { let url = unsafe { CStr::from_ptr(data1 as *const _).to_str().unwrap() }; @@ -54,21 +59,21 @@ fn main() { let ctx = dc_context_new(cb, std::ptr::null_mut(), std::ptr::null_mut()); let info = dc_get_info(&ctx); let info_s = CStr::from_ptr(info); + let duration = time::Duration::from_millis(4000); println!("info: {}", info_s.to_str().unwrap()); - let ctx = Arc::new(RwLock::new(ctx)); - + let ctx = Arc::new(ctx); let ctx1 = ctx.clone(); - let t1 = std::thread::spawn(move || loop { - dc_perform_imap_jobs(&ctx1.clone().read().unwrap()); - dc_perform_imap_fetch(&ctx1.clone().read().unwrap()); - dc_perform_imap_idle(&ctx1.clone().read().unwrap()); + let t1 = thread::spawn(move || loop { + dc_perform_imap_jobs(&ctx1); + dc_perform_imap_fetch(&ctx1); + dc_perform_imap_idle(&ctx1); }); let ctx1 = ctx.clone(); - let t2 = std::thread::spawn(move || loop { - dc_perform_smtp_jobs(&ctx1.clone().read().unwrap()); - dc_perform_smtp_idle(&ctx1.clone().read().unwrap()); + let t2 = thread::spawn(move || loop { + dc_perform_smtp_jobs(&ctx1); + dc_perform_smtp_idle(&ctx1); }); let dir = tempdir().unwrap(); @@ -76,42 +81,32 @@ fn main() { println!("opening database {:?}", dbfile); - dc_open( - &mut ctx.clone().write().unwrap(), - dbfile.as_ptr(), - std::ptr::null(), - ); + dc_open(&ctx, dbfile.as_ptr(), std::ptr::null()); println!("configuring"); + let pw = std::env::args().collect::>()[1].clone(); dc_set_config( - &ctx.clone().read().unwrap(), + &ctx, CString::new("addr").unwrap().as_ptr(), CString::new("d@testrun.org").unwrap().as_ptr(), ); dc_set_config( - &ctx.clone().read().unwrap(), + &ctx, CString::new("mail_pw").unwrap().as_ptr(), - CString::new("***").unwrap().as_ptr(), + CString::new(pw).unwrap().as_ptr(), ); - dc_configure(&ctx.clone().read().unwrap()); + dc_configure(&ctx); - std::thread::sleep_ms(4000); + thread::sleep(duration); let email = CString::new("dignifiedquire@gmail.com").unwrap(); println!("sending a message"); - let contact_id = dc_create_contact( - &ctx.clone().read().unwrap(), - std::ptr::null(), - email.as_ptr(), - ); - let chat_id = dc_create_chat_by_contact_id(&ctx.clone().read().unwrap(), contact_id); + let contact_id = dc_create_contact(&ctx, std::ptr::null(), email.as_ptr()); + let chat_id = dc_create_chat_by_contact_id(&ctx, contact_id); let msg_text = CString::new("Hi, here is my first message!").unwrap(); - dc_send_text_msg(&ctx.clone().read().unwrap(), chat_id, msg_text.as_ptr()); + dc_send_text_msg(&ctx, chat_id, msg_text.as_ptr()); println!("fetching chats.."); - let ctx1 = ctx.clone(); - let ctx = ctx1.read().unwrap(); - let chats = dc_get_chatlist(&ctx, 0, std::ptr::null(), 0); for i in 0..dc_chatlist_get_cnt(chats) { @@ -134,7 +129,7 @@ fn main() { } dc_chatlist_unref(chats); - // let msglist = dc_get_chat_msgs(&ctx.clone().read().unwrap(), chat_id, 0, 0); + // let msglist = dc_get_chat_msgs(&ctx, chat_id, 0, 0); // for i in 0..dc_array_get_cnt(msglist) { // let msg_id = dc_array_get_id(msglist, i); // let msg = dc_get_msg(context, msg_id); diff --git a/src/constants.rs b/src/constants.rs index b1790e904..e2c2e5cc5 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -2,6 +2,11 @@ pub const VERSION: &'static [u8; 7] = b"0.43.0\x00"; +pub const DC_MOVE_STATE_MOVING: u32 = 3; +pub const DC_MOVE_STATE_STAY: u32 = 2; +pub const DC_MOVE_STATE_PENDING: u32 = 1; +pub const DC_MOVE_STATE_UNDEFINED: u32 = 0; + pub const DC_GCL_ARCHIVED_ONLY: usize = 0x01; pub const DC_GCL_NO_SPECIALS: usize = 0x02; pub const DC_GCL_ADD_ALLDONE_HINT: usize = 0x04; @@ -88,6 +93,8 @@ pub const DC_TEXT1_DRAFT: usize = 1; pub const DC_TEXT1_USERNAME: usize = 2; pub const DC_TEXT1_SELF: usize = 3; +pub const DC_CREATE_MVBOX: usize = 1; + /// Text message. /// The text of the message is set using dc_msg_set_text() /// and retrieved with dc_msg_get_text(). diff --git a/src/dc_aheader.rs b/src/dc_aheader.rs index 79a36b78a..b23d75532 100644 --- a/src/dc_aheader.rs +++ b/src/dc_aheader.rs @@ -1,4 +1,4 @@ -use libc; +use mmime::mailimf_types::*; use crate::dc_contact::*; use crate::dc_key::*; @@ -34,7 +34,7 @@ pub unsafe fn dc_aheader_new_from_imffields( mut wanted_from: *const libc::c_char, mut header: *const mailimf_fields, ) -> *mut dc_aheader_t { - let mut cur = 0 as *mut clistiter; + let mut cur; let mut fine_header = 0 as *mut dc_aheader_t; if wanted_from.is_null() || header.is_null() { @@ -78,7 +78,7 @@ pub unsafe fn dc_aheader_new_from_imffields( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } @@ -105,10 +105,10 @@ pub unsafe fn dc_aheader_set_from_string( (b) for the key, non-base64-characters are ignored and (c) for parsing, we ignore `\r\n` as well as tabs for spaces */ let mut header_str = 0 as *mut libc::c_char; - let mut p = 0 as *mut libc::c_char; - let mut beg_attr_name = 0 as *mut libc::c_char; - let mut after_attr_name = 0 as *mut libc::c_char; - let mut beg_attr_value = 0 as *mut libc::c_char; + let mut p; + let mut beg_attr_name; + let mut after_attr_name; + let mut beg_attr_value; let mut success: libc::c_int = 0; dc_aheader_empty(aheader); @@ -263,12 +263,8 @@ pub unsafe fn dc_aheader_render(mut aheader: *const dc_aheader_t) -> *mut libc:: // TODO replace 78 with enum /rtn /* adds a whitespace every 78 characters, this allows libEtPan to wrap the lines according to RFC 5322 (which may insert a linebreak before every whitespace) */ - keybase64_wrapped = dc_key_render_base64( - (*aheader).public_key, - 78, - b" \x00" as *const u8 as *const libc::c_char, - 0, - ); + keybase64_wrapped = dc_key_render_base64((*aheader).public_key, 78); + if !keybase64_wrapped.is_null() { /*no checksum*/ dc_strbuilder_cat(&mut ret, keybase64_wrapped); diff --git a/src/dc_apeerstate.rs b/src/dc_apeerstate.rs index 9404dc9cf..ef538d5ee 100644 --- a/src/dc_apeerstate.rs +++ b/src/dc_apeerstate.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_aheader::*; use crate::dc_chat::*; use crate::dc_context::dc_context_t; @@ -11,7 +9,7 @@ use crate::types::*; use crate::x::*; /* prefer-encrypt states */ -/* * +/** * @class dc_apeerstate_t * Library-internal. */ @@ -36,20 +34,22 @@ pub struct dc_apeerstate_t<'a> { /* the returned pointer is ref'd and must be unref'd after usage */ 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; + let mut peerstate: *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; + peerstate } + pub unsafe fn dc_apeerstate_unref(mut peerstate: *mut dc_apeerstate_t) { dc_apeerstate_empty(peerstate); free(peerstate as *mut libc::c_void); } -/* ****************************************************************************** + +/******************************************************************************* * dc_apeerstate_t represents the state of an Autocrypt peer - Load/save ******************************************************************************/ unsafe fn dc_apeerstate_empty(mut peerstate: *mut dc_apeerstate_t) { @@ -77,6 +77,8 @@ unsafe fn dc_apeerstate_empty(mut peerstate: *mut dc_apeerstate_t) { (*peerstate).verified_key = 0 as *mut dc_key_t; (*peerstate).degrade_event = 0i32; } + +// TODO should return bool /rtn pub unsafe fn dc_apeerstate_init_from_header( mut peerstate: *mut dc_apeerstate_t, mut header: *const dc_aheader_t, @@ -94,8 +96,11 @@ pub unsafe fn dc_apeerstate_init_from_header( (*peerstate).public_key = dc_key_new(); dc_key_set_from_key((*peerstate).public_key, (*header).public_key); dc_apeerstate_recalc_fingerprint(peerstate); - return 1i32; + + 1 } + +// TODO should return bool /rtn pub unsafe fn dc_apeerstate_recalc_fingerprint(mut peerstate: *mut dc_apeerstate_t) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut old_public_fingerprint: *mut libc::c_char = 0 as *mut libc::c_char; @@ -139,10 +144,14 @@ pub unsafe fn dc_apeerstate_recalc_fingerprint(mut peerstate: *mut dc_apeerstate } success = 1i32 } + free(old_public_fingerprint as *mut libc::c_void); free(old_gossip_fingerprint as *mut libc::c_void); - return success; + + success } + +// TODO should return bool /rtn pub unsafe extern "C" fn dc_apeerstate_init_from_gossip( mut peerstate: *mut dc_apeerstate_t, mut gossip_header: *const dc_aheader_t, @@ -158,8 +167,11 @@ pub unsafe extern "C" fn dc_apeerstate_init_from_gossip( (*peerstate).gossip_key = dc_key_new(); dc_key_set_from_key((*peerstate).gossip_key, (*gossip_header).public_key); dc_apeerstate_recalc_fingerprint(peerstate); - return 1i32; + + 1 } + +// TODO should return bool /rtn pub unsafe fn dc_apeerstate_degrade_encryption( mut peerstate: *mut dc_apeerstate_t, mut message_time: time_t, @@ -173,8 +185,10 @@ pub unsafe fn dc_apeerstate_degrade_encryption( (*peerstate).prefer_encrypt = 20i32; (*peerstate).last_seen = message_time; (*peerstate).to_save |= 0x2i32; - return 1i32; + + 1 } + pub unsafe fn dc_apeerstate_apply_header( mut peerstate: *mut dc_apeerstate_t, mut header: *const dc_aheader_t, @@ -212,6 +226,7 @@ pub unsafe fn dc_apeerstate_apply_header( } }; } + pub unsafe fn dc_apeerstate_apply_gossip( mut peerstate: *mut dc_apeerstate_t, mut gossip_header: *const dc_aheader_t, @@ -239,6 +254,7 @@ pub unsafe fn dc_apeerstate_apply_gossip( } }; } + pub unsafe fn dc_apeerstate_render_gossip_header( mut peerstate: *const dc_apeerstate_t, mut min_verified: libc::c_int, @@ -252,8 +268,9 @@ pub unsafe fn dc_apeerstate_render_gossip_header( ret = dc_aheader_render(autocryptheader) } dc_aheader_unref(autocryptheader); - return ret; + ret } + pub unsafe fn dc_apeerstate_peek_key( mut peerstate: *const dc_apeerstate_t, mut min_verified: libc::c_int, @@ -277,8 +294,10 @@ pub unsafe fn dc_apeerstate_peek_key( if !(*peerstate).public_key.is_null() { return (*peerstate).public_key; } - return (*peerstate).gossip_key; + (*peerstate).gossip_key } + +// TODO should return bool /rtn pub unsafe fn dc_apeerstate_set_verified( mut peerstate: *mut dc_apeerstate_t, mut which_key: libc::c_int, @@ -310,8 +329,11 @@ pub unsafe fn dc_apeerstate_set_verified( success = 1i32 } } - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_apeerstate_load_by_addr( mut peerstate: *mut dc_apeerstate_t, mut sql: &dc_sqlite3_t, @@ -334,8 +356,9 @@ pub unsafe fn dc_apeerstate_load_by_addr( } } sqlite3_finalize(stmt); - return success; + success } + unsafe fn dc_apeerstate_set_from_stmt( mut peerstate: *mut dc_apeerstate_t, mut stmt: *mut sqlite3_stmt, @@ -364,6 +387,8 @@ unsafe fn dc_apeerstate_set_from_stmt( dc_key_set_from_stmt((*peerstate).verified_key, stmt, 9i32, 0i32); }; } + +// TODO should return bool /rtn pub unsafe fn dc_apeerstate_load_by_fingerprint( mut peerstate: *mut dc_apeerstate_t, mut sql: &dc_sqlite3_t, @@ -388,8 +413,10 @@ pub unsafe fn dc_apeerstate_load_by_fingerprint( } } sqlite3_finalize(stmt); - return success; + success } + +// TODO should return bool /rtn pub unsafe fn dc_apeerstate_save_to_db( mut peerstate: *const dc_apeerstate_t, mut sql: &dc_sqlite3_t, @@ -522,8 +549,11 @@ pub unsafe fn dc_apeerstate_save_to_db( _ => {} } sqlite3_finalize(stmt); - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_apeerstate_has_verified_key( mut peerstate: *const dc_apeerstate_t, mut fingerprints: *const dc_hash_t, @@ -542,5 +572,6 @@ pub unsafe fn dc_apeerstate_has_verified_key( { return 1i32; } - return 0i32; + + 0 } diff --git a/src/dc_array.rs b/src/dc_array.rs index 4ce144bba..cf03b0b60 100644 --- a/src/dc_array.rs +++ b/src/dc_array.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_context::*; use crate::dc_tools::*; use crate::types::*; @@ -16,7 +14,7 @@ pub struct dc_array_t { pub array: *mut uintptr_t, } -/* * +/** * @class dc_array_t * * An object containing a simple array. @@ -35,6 +33,7 @@ pub unsafe fn dc_array_unref(mut array: *mut dc_array_t) { (*array).magic = 0i32 as uint32_t; free(array as *mut libc::c_void); } + pub unsafe fn dc_array_free_ptr(mut array: *mut dc_array_t) { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint { return; @@ -52,6 +51,7 @@ pub unsafe fn dc_array_free_ptr(mut array: *mut dc_array_t) { i = i.wrapping_add(1) } } + pub unsafe fn dc_array_add_uint(mut array: *mut dc_array_t, mut item: uintptr_t) { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint { return; @@ -70,24 +70,29 @@ pub unsafe fn dc_array_add_uint(mut array: *mut dc_array_t, mut item: uintptr_t) *(*array).array.offset((*array).count as isize) = item; (*array).count = (*array).count.wrapping_add(1); } + pub unsafe fn dc_array_add_id(mut array: *mut dc_array_t, mut item: uint32_t) { dc_array_add_uint(array, item as uintptr_t); } + pub unsafe fn dc_array_add_ptr(mut array: *mut dc_array_t, mut item: *mut libc::c_void) { dc_array_add_uint(array, item as uintptr_t); } + pub unsafe fn dc_array_get_cnt(mut array: *const dc_array_t) -> size_t { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint { return 0i32 as size_t; } - return (*array).count; + (*array).count } + pub unsafe fn dc_array_get_uint(mut array: *const dc_array_t, mut index: size_t) -> uintptr_t { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || index >= (*array).count { return 0i32 as uintptr_t; } - return *(*array).array.offset(index as isize); + *(*array).array.offset(index as isize) } + pub unsafe fn dc_array_get_id(mut array: *const dc_array_t, mut index: size_t) -> uint32_t { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || index >= (*array).count { return 0i32 as uint32_t; @@ -95,8 +100,9 @@ pub unsafe fn dc_array_get_id(mut array: *const dc_array_t, mut index: size_t) - if (*array).type_0 == 1i32 { return (*(*(*array).array.offset(index as isize) as *mut _dc_location)).location_id; } - return *(*array).array.offset(index as isize) as uint32_t; + *(*array).array.offset(index as isize) as uint32_t } + pub unsafe fn dc_array_get_ptr( mut array: *const dc_array_t, mut index: size_t, @@ -104,8 +110,9 @@ pub unsafe fn dc_array_get_ptr( if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || index >= (*array).count { return 0 as *mut libc::c_void; } - return *(*array).array.offset(index as isize) as *mut libc::c_void; + *(*array).array.offset(index as isize) as *mut libc::c_void } + pub unsafe fn dc_array_get_latitude( mut array: *const dc_array_t, mut index: size_t, @@ -118,8 +125,9 @@ pub unsafe fn dc_array_get_latitude( { return 0i32 as libc::c_double; } - return (*(*(*array).array.offset(index as isize) as *mut _dc_location)).latitude; + (*(*(*array).array.offset(index as isize) as *mut _dc_location)).latitude } + pub unsafe fn dc_array_get_longitude( mut array: *const dc_array_t, mut index: size_t, @@ -132,8 +140,9 @@ pub unsafe fn dc_array_get_longitude( { return 0i32 as libc::c_double; } - return (*(*(*array).array.offset(index as isize) as *mut _dc_location)).longitude; + (*(*(*array).array.offset(index as isize) as *mut _dc_location)).longitude } + pub unsafe fn dc_array_get_accuracy( mut array: *const dc_array_t, mut index: size_t, @@ -146,8 +155,9 @@ pub unsafe fn dc_array_get_accuracy( { return 0i32 as libc::c_double; } - return (*(*(*array).array.offset(index as isize) as *mut _dc_location)).accuracy; + (*(*(*array).array.offset(index as isize) as *mut _dc_location)).accuracy } + pub unsafe fn dc_array_get_timestamp(mut array: *const dc_array_t, mut index: size_t) -> time_t { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint @@ -157,8 +167,9 @@ pub unsafe fn dc_array_get_timestamp(mut array: *const dc_array_t, mut index: si { return 0i32 as time_t; } - return (*(*(*array).array.offset(index as isize) as *mut _dc_location)).timestamp; + (*(*(*array).array.offset(index as isize) as *mut _dc_location)).timestamp } + pub unsafe fn dc_array_get_chat_id(mut array: *const dc_array_t, mut index: size_t) -> uint32_t { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint @@ -168,8 +179,9 @@ pub unsafe fn dc_array_get_chat_id(mut array: *const dc_array_t, mut index: size { return 0i32 as uint32_t; } - return (*(*(*array).array.offset(index as isize) as *mut _dc_location)).chat_id; + (*(*(*array).array.offset(index as isize) as *mut _dc_location)).chat_id } + pub unsafe fn dc_array_get_contact_id(mut array: *const dc_array_t, mut index: size_t) -> uint32_t { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint @@ -179,8 +191,9 @@ pub unsafe fn dc_array_get_contact_id(mut array: *const dc_array_t, mut index: s { return 0i32 as uint32_t; } - return (*(*(*array).array.offset(index as isize) as *mut _dc_location)).contact_id; + (*(*(*array).array.offset(index as isize) as *mut _dc_location)).contact_id } + pub unsafe fn dc_array_get_msg_id(mut array: *const dc_array_t, mut index: size_t) -> uint32_t { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint @@ -190,8 +203,9 @@ pub unsafe fn dc_array_get_msg_id(mut array: *const dc_array_t, mut index: size_ { return 0i32 as uint32_t; } - return (*(*(*array).array.offset(index as isize) as *mut _dc_location)).msg_id; + (*(*(*array).array.offset(index as isize) as *mut _dc_location)).msg_id } + pub unsafe fn dc_array_get_marker( mut array: *const dc_array_t, mut index: size_t, @@ -204,9 +218,7 @@ pub unsafe fn dc_array_get_marker( { return 0 as *mut libc::c_char; } - return dc_strdup_keep_null( - (*(*(*array).array.offset(index as isize) as *mut _dc_location)).marker, - ); + dc_strdup_keep_null((*(*(*array).array.offset(index as isize) as *mut _dc_location)).marker) } /** @@ -232,6 +244,7 @@ pub unsafe fn dc_array_is_independent(array: *const dc_array_t, index: size_t) - (*(*(*array).array.offset(index as isize) as *mut _dc_location)).independent as libc::c_int } +// TODO should return bool /rtn pub unsafe fn dc_array_search_id( mut array: *const dc_array_t, mut needle: uint32_t, @@ -243,7 +256,6 @@ pub unsafe fn dc_array_search_id( let mut data: *mut uintptr_t = (*array).array; let mut i: size_t = 0; let mut cnt: size_t = (*array).count; - i = 0i32 as size_t; while i < cnt { if *data.offset(i as isize) == needle as size_t { if !ret_index.is_null() { @@ -253,14 +265,16 @@ pub unsafe fn dc_array_search_id( } i = i.wrapping_add(1) } - return 0i32; + 0 } + pub unsafe fn dc_array_get_raw(mut array: *const dc_array_t) -> *const uintptr_t { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint { return 0 as *const uintptr_t; } - return (*array).array; + (*array).array } + pub unsafe fn dc_array_new(initsize: size_t) -> *mut dc_array_t { dc_array_new_typed(0, initsize) } @@ -269,7 +283,7 @@ pub unsafe extern "C" fn dc_array_new_typed( mut type_0: libc::c_int, mut initsize: size_t, ) -> *mut dc_array_t { - let mut array: *mut dc_array_t = 0 as *mut dc_array_t; + let mut array: *mut dc_array_t; array = calloc(1, ::std::mem::size_of::()) as *mut dc_array_t; if array.is_null() { exit(47i32); @@ -295,8 +309,9 @@ pub unsafe fn dc_array_empty(mut array: *mut dc_array_t) { } (*array).count = 0i32 as size_t; } + pub unsafe fn dc_array_duplicate(mut array: *const dc_array_t) -> *mut dc_array_t { - let mut ret: *mut dc_array_t = 0 as *mut dc_array_t; + let mut ret: *mut dc_array_t; if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint { return 0 as *mut dc_array_t; } @@ -309,8 +324,9 @@ pub unsafe fn dc_array_duplicate(mut array: *const dc_array_t) -> *mut dc_array_ .count .wrapping_mul(::std::mem::size_of::()), ); - return ret; + ret } + pub unsafe fn dc_array_sort_ids(mut array: *mut dc_array_t) { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || (*array).count <= 1 { return; @@ -322,6 +338,7 @@ pub unsafe fn dc_array_sort_ids(mut array: *mut dc_array_t) { Some(cmp_intptr_t), ); } + unsafe extern "C" fn cmp_intptr_t( mut p1: *const libc::c_void, mut p2: *const libc::c_void, @@ -336,6 +353,7 @@ unsafe extern "C" fn cmp_intptr_t( 0i32 }; } + pub unsafe fn dc_array_sort_strings(mut array: *mut dc_array_t) { if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || (*array).count <= 1 { return; @@ -347,19 +365,22 @@ pub unsafe fn dc_array_sort_strings(mut array: *mut dc_array_t) { Some(cmp_strings_t), ); } + unsafe extern "C" fn cmp_strings_t( mut p1: *const libc::c_void, mut p2: *const libc::c_void, ) -> libc::c_int { let mut v1: *const libc::c_char = *(p1 as *mut *const libc::c_char); let mut v2: *const libc::c_char = *(p2 as *mut *const libc::c_char); - return strcmp(v1, v2); + + strcmp(v1, v2) } + pub unsafe fn dc_array_get_string( mut array: *const dc_array_t, mut sep: *const libc::c_char, ) -> *mut libc::c_char { - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; + let mut ret: *mut libc::c_char; if array.is_null() || (*array).magic != 0xa11aai32 as libc::c_uint || sep.is_null() { return dc_strdup(b"\x00" as *const u8 as *const libc::c_char); } @@ -374,7 +395,6 @@ pub unsafe fn dc_array_get_string( exit(35i32); } *ret.offset(0isize) = 0i32 as libc::c_char; - i = 0; while i < (*array).count { if 0 != i { strcat(ret, sep); @@ -386,19 +406,21 @@ pub unsafe fn dc_array_get_string( ); i += 1 } - return ret; + + ret } + pub unsafe fn dc_arr_to_string( mut arr: *const uint32_t, mut cnt: libc::c_int, ) -> *mut libc::c_char { /* return comma-separated value-string from integer array */ - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; + let mut ret: *mut libc::c_char; let mut sep: *const libc::c_char = b",\x00" as *const u8 as *const libc::c_char; if arr.is_null() || cnt <= 0i32 { return dc_strdup(b"\x00" as *const u8 as *const libc::c_char); } - let mut i: libc::c_int = 0; + let mut i: libc::c_int; ret = malloc( (cnt as usize) .wrapping_mul((11usize).wrapping_add(strlen(sep))) @@ -420,5 +442,6 @@ pub unsafe fn dc_arr_to_string( ); i += 1 } - return ret; + + ret } diff --git a/src/dc_chat.rs b/src/dc_chat.rs index ad8606611..d30dc4d63 100644 --- a/src/dc_chat.rs +++ b/src/dc_chat.rs @@ -1,5 +1,3 @@ -use libc; - use crate::constants::*; use crate::dc_array::*; use crate::dc_chatlist::*; @@ -64,9 +62,10 @@ pub unsafe fn dc_create_chat_by_msg_id( 0i32 as uintptr_t, ); } - return chat_id; + chat_id } -/* * + +/** * @class dc_chat_t * * An object representing a single chat in memory. @@ -82,14 +81,15 @@ pub unsafe fn dc_create_chat_by_msg_id( // only an indicator in a chatlist // larger chat IDs are "real" chats, their messages are "real" messages. 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; + let mut chat: *mut dc_chat_t; chat = calloc(1, ::std::mem::size_of::()) as *mut dc_chat_t; (*chat).magic = 0xc4a7c4a7u32; (*chat).context = context; (*chat).type_0 = 0i32; (*chat).param = dc_param_new(); - return chat; + chat } + pub unsafe fn dc_chat_unref(mut chat: *mut dc_chat_t) { if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 { return; @@ -99,6 +99,7 @@ pub unsafe fn dc_chat_unref(mut chat: *mut dc_chat_t) { (*chat).magic = 0i32 as uint32_t; free(chat as *mut libc::c_void); } + pub unsafe fn dc_chat_empty(mut chat: *mut dc_chat_t) { if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 { return; @@ -113,15 +114,17 @@ pub unsafe fn dc_chat_empty(mut chat: *mut dc_chat_t) { (*chat).gossiped_timestamp = 0i32 as time_t; dc_param_set_packed((*chat).param, 0 as *const libc::c_char); } + pub unsafe fn dc_unblock_chat(mut context: &dc_context_t, mut chat_id: uint32_t) { dc_block_chat(context, chat_id, 0i32); } + pub unsafe fn dc_block_chat( mut context: &dc_context_t, mut chat_id: uint32_t, mut new_blocking: libc::c_int, ) { - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -132,6 +135,8 @@ pub unsafe fn dc_block_chat( sqlite3_step(stmt); sqlite3_finalize(stmt); } + +// TODO should return bool /rtn pub unsafe fn dc_chat_load_from_db(mut chat: *mut dc_chat_t, mut chat_id: uint32_t) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; @@ -152,8 +157,9 @@ pub unsafe fn dc_chat_load_from_db(mut chat: *mut dc_chat_t, mut chat_id: uint32 } } sqlite3_finalize(stmt); - return success; + success } + unsafe fn set_from_stmt(mut chat: *mut dc_chat_t, mut row: *mut sqlite3_stmt) -> libc::c_int { let mut row_offset: libc::c_int = 0i32; if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 || row.is_null() { @@ -211,8 +217,9 @@ unsafe fn set_from_stmt(mut chat: *mut dc_chat_t, mut row: *mut sqlite3_stmt) -> free((*chat).name as *mut libc::c_void); (*chat).name = dc_stock_str((*chat).context, 2i32) } - return row_offset; + row_offset } + pub unsafe fn dc_create_chat_by_contact_id( mut context: &dc_context_t, mut contact_id: uint32_t, @@ -257,8 +264,9 @@ pub unsafe fn dc_create_chat_by_contact_id( 0i32 as uintptr_t, ); } - return chat_id; + chat_id } + pub unsafe fn dc_create_or_lookup_nchat_by_contact_id( mut context: &dc_context_t, mut contact_id: uint32_t, @@ -268,8 +276,8 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id( ) { let mut chat_id: uint32_t = 0i32 as uint32_t; let mut chat_blocked: libc::c_int = 0i32; - let mut contact: *mut dc_contact_t = 0 as *mut dc_contact_t; - let mut chat_name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut contact: *mut dc_contact_t; + let mut chat_name: *mut libc::c_char; let mut q: *mut libc::c_char = 0 as *mut libc::c_char; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; if !ret_chat_id.is_null() { @@ -338,9 +346,7 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id( (*contact).addr, ); sqlite3_free(q as *mut libc::c_void); - q = 0 as *mut libc::c_char; sqlite3_finalize(stmt); - stmt = 0 as *mut sqlite3_stmt; q = sqlite3_mprintf( b"INSERT INTO chats_contacts (chat_id, contact_id) VALUES(%i, %i)\x00" as *const u8 as *const libc::c_char, @@ -367,6 +373,7 @@ pub unsafe fn dc_create_or_lookup_nchat_by_contact_id( *ret_chat_blocked = create_blocked }; } + pub unsafe fn dc_lookup_real_nchat_by_contact_id( mut context: &dc_context_t, mut contact_id: uint32_t, @@ -374,7 +381,7 @@ pub unsafe fn dc_lookup_real_nchat_by_contact_id( mut ret_chat_blocked: *mut libc::c_int, ) { /* checks for "real" chats or self-chat */ - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; if !ret_chat_id.is_null() { *ret_chat_id = 0i32 as uint32_t } @@ -401,6 +408,7 @@ pub unsafe fn dc_lookup_real_nchat_by_contact_id( } sqlite3_finalize(stmt); } + pub unsafe fn dc_get_chat_id_by_contact_id( mut context: &dc_context_t, mut contact_id: uint32_t, @@ -414,6 +422,7 @@ pub unsafe fn dc_get_chat_id_by_contact_id( chat_id }; } + pub unsafe fn dc_prepare_msg<'a>( mut context: &'a dc_context_t, mut chat_id: uint32_t, @@ -432,6 +441,7 @@ pub unsafe fn dc_prepare_msg<'a>( ); return msg_id; } + unsafe fn prepare_msg_common<'a>( mut context: &'a dc_context_t, mut chat_id: uint32_t, @@ -528,16 +538,18 @@ unsafe fn prepare_msg_common<'a>( /* potential error already logged */ dc_chat_unref(chat); free(pathNfilename as *mut libc::c_void); - return (*msg).id; + + (*msg).id } + unsafe fn prepare_msg_raw( mut context: &dc_context_t, mut chat: *mut dc_chat_t, mut msg: *const dc_msg_t, mut timestamp: time_t, ) -> uint32_t { - let mut do_guarantee_e2ee: libc::c_int = 0; - let mut e2ee_enabled: libc::c_int = 0; + let mut do_guarantee_e2ee: libc::c_int; + let mut e2ee_enabled: libc::c_int; let mut current_block: u64; let mut parent_rfc724_mid: *mut libc::c_char = 0 as *mut libc::c_char; let mut parent_references: *mut libc::c_char = 0 as *mut libc::c_char; @@ -677,7 +689,6 @@ unsafe fn prepare_msg_raw( } } sqlite3_finalize(stmt); - stmt = 0 as *mut sqlite3_stmt; if 0 != can_encrypt { if 0 != all_mutual { do_guarantee_e2ee = 1i32 @@ -710,7 +721,7 @@ unsafe fn prepare_msg_raw( new_in_reply_to = dc_strdup(parent_rfc724_mid) } if !parent_references.is_null() { - let mut space: *mut libc::c_char = 0 as *mut libc::c_char; + let mut space: *mut libc::c_char; space = strchr(parent_references, ' ' as i32); if !space.is_null() { *space = 0i32 as libc::c_char @@ -781,7 +792,6 @@ unsafe fn prepare_msg_raw( ); sqlite3_step(stmt); sqlite3_finalize(stmt); - stmt = 0 as *mut sqlite3_stmt; location_id = dc_sqlite3_get_rowid2( context, @@ -853,8 +863,11 @@ unsafe fn prepare_msg_raw( free(new_in_reply_to as *mut libc::c_void); free(new_references as *mut libc::c_void); sqlite3_finalize(stmt); - return msg_id; + + msg_id } + +// TODO should return bool /rtn unsafe fn get_parent_mime_headers( mut chat: *const dc_chat_t, mut parent_rfc724_mid: *mut *mut libc::c_char, @@ -903,17 +916,20 @@ unsafe fn get_parent_mime_headers( } } sqlite3_finalize(stmt); - return success; + success } + pub unsafe fn dc_chat_is_self_talk(mut chat: *const dc_chat_t) -> libc::c_int { if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 { return 0i32; } - return dc_param_exists((*chat).param, 'K' as i32); + dc_param_exists((*chat).param, 'K' as i32) } -/* ****************************************************************************** + +/******************************************************************************* * Sending messages ******************************************************************************/ +// TODO should return bool /rtn unsafe fn last_msg_in_chat_encrypted( context: &dc_context_t, sql: &dc_sqlite3_t, @@ -938,10 +954,12 @@ unsafe fn last_msg_in_chat_encrypted( dc_param_unref(msg_param); } sqlite3_finalize(stmt); - return last_is_encrypted; + last_is_encrypted } + +// TODO should return bool /rtn 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 success: libc::c_int; let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( (*chat).context, &mut (*chat).context.sql.clone().read().unwrap(), @@ -955,8 +973,9 @@ pub unsafe fn dc_chat_update_param(mut chat: *mut dc_chat_t) -> libc::c_int { 0i32 }; sqlite3_finalize(stmt); - return success; + success } + pub unsafe fn dc_is_contact_in_chat( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -964,8 +983,8 @@ pub unsafe fn dc_is_contact_in_chat( ) -> libc::c_int { /* this function works for group and for normal chats, however, it is more useful for group chats. 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; + let mut ret: libc::c_int; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, @@ -995,6 +1014,7 @@ pub unsafe fn dc_unarchive_chat(mut context: &dc_context_t, mut chat_id: uint32_ sqlite3_step(stmt); sqlite3_finalize(stmt); } + pub unsafe fn dc_send_msg<'a>( mut context: &'a dc_context_t, mut chat_id: uint32_t, @@ -1050,8 +1070,10 @@ pub unsafe fn dc_send_msg<'a>( } free(forwards as *mut libc::c_void); } - return (*msg).id; + + (*msg).id } + pub unsafe fn dc_send_text_msg( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -1064,8 +1086,9 @@ pub unsafe fn dc_send_text_msg( ret = dc_send_msg(context, chat_id, msg) } dc_msg_unref(msg); - return ret; + ret } + pub unsafe fn dc_set_draft( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -1083,6 +1106,8 @@ pub unsafe fn dc_set_draft( ); }; } + +// TODO should return bool /rtn unsafe fn set_draft_raw( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -1092,7 +1117,7 @@ unsafe fn set_draft_raw( // similar to as dc_set_draft() but does not emit an event let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char; - let mut prev_draft_msg_id: uint32_t = 0i32 as uint32_t; + let mut prev_draft_msg_id: uint32_t; let mut sth_changed: libc::c_int = 0i32; prev_draft_msg_id = get_draft_msg_id(context, chat_id); if 0 != prev_draft_msg_id { @@ -1165,8 +1190,9 @@ unsafe fn set_draft_raw( } sqlite3_finalize(stmt); free(pathNfilename as *mut libc::c_void); - return sth_changed; + sth_changed } + 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( @@ -1180,11 +1206,12 @@ unsafe fn get_draft_msg_id(mut context: &dc_context_t, mut chat_id: uint32_t) -> draft_msg_id = sqlite3_column_int(stmt, 0i32) as uint32_t } sqlite3_finalize(stmt); - return draft_msg_id; + draft_msg_id } + 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; + let mut draft_msg_id: uint32_t; + let mut draft_msg: *mut dc_msg_t; if chat_id <= 9i32 as libc::c_uint { return 0 as *mut dc_msg_t; } @@ -1197,8 +1224,10 @@ pub unsafe fn dc_get_draft(mut context: &dc_context_t, mut chat_id: uint32_t) -> dc_msg_unref(draft_msg); return 0 as *mut dc_msg_t; } - return draft_msg; + + draft_msg } + pub unsafe fn dc_get_chat_msgs( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -1209,9 +1238,9 @@ pub unsafe fn dc_get_chat_msgs( let mut success: libc::c_int = 0i32; 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 curr_id: uint32_t; + let mut curr_local_timestamp: time_t; + let mut curr_day: libc::c_int; let mut last_day: libc::c_int = 0i32; let mut cnv_to_local: libc::c_long = dc_gm2local_offset(); if !ret.is_null() { @@ -1269,9 +1298,10 @@ pub unsafe fn dc_get_chat_msgs( return 0 as *mut dc_array_t; }; } + 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; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -1283,14 +1313,15 @@ pub unsafe fn dc_get_msg_cnt(mut context: &dc_context_t, mut chat_id: uint32_t) } sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_get_fresh_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; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -1302,10 +1333,11 @@ pub unsafe fn dc_get_fresh_msg_cnt( ret = sqlite3_column_int(stmt, 0i32); } sqlite3_finalize(stmt); - return ret; + ret } + 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 check: *mut sqlite3_stmt; let mut update: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; check = dc_sqlite3_prepare( @@ -1335,8 +1367,9 @@ pub unsafe fn dc_marknoticed_chat(mut context: &dc_context_t, mut chat_id: uint3 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 check: *mut sqlite3_stmt; let mut update: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; check = dc_sqlite3_prepare( @@ -1362,6 +1395,7 @@ pub unsafe fn dc_marknoticed_all_chats(mut context: &dc_context_t) { sqlite3_finalize(check); sqlite3_finalize(update); } + pub unsafe fn dc_get_chat_media( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -1399,8 +1433,9 @@ pub unsafe fn dc_get_chat_media( dc_array_add_id(ret, sqlite3_column_int(stmt, 0i32) as uint32_t); } sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_get_next_media( mut context: &dc_context_t, mut curr_msg_id: uint32_t, @@ -1412,8 +1447,8 @@ pub unsafe fn dc_get_next_media( let mut ret_msg_id: uint32_t = 0i32 as uint32_t; let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); 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; + let mut i: libc::c_int; + let mut cnt: libc::c_int; if !(0 == dc_msg_load_from_db(msg, context, curr_msg_id)) { list = dc_get_chat_media( @@ -1451,8 +1486,9 @@ pub unsafe fn dc_get_next_media( dc_array_unref(list); dc_msg_unref(msg); - return ret_msg_id; + ret_msg_id } + pub unsafe fn dc_archive_chat( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -1488,6 +1524,7 @@ pub unsafe fn dc_archive_chat( 0i32 as uintptr_t, ); } + pub unsafe fn dc_delete_chat(mut context: &dc_context_t, mut chat_id: uint32_t) { /* Up to 2017-11-02 deleting a group also implied leaving it, see above why we have changed this. */ let mut obj: *mut dc_chat_t = dc_chat_new(context); @@ -1501,14 +1538,12 @@ pub unsafe fn dc_delete_chat(mut context: &dc_context_t, mut chat_id: uint32_t) ); if !(0 == dc_sqlite3_execute(context, &context.sql.clone().read().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, &context.sql.clone().read().unwrap(), q3)) { sqlite3_free(q3 as *mut libc::c_void); - q3 = 0 as *mut libc::c_char; q3 = sqlite3_mprintf( b"DELETE FROM chats_contacts WHERE chat_id=%i;\x00" as *const u8 as *const libc::c_char, @@ -1517,7 +1552,6 @@ pub unsafe fn dc_delete_chat(mut context: &dc_context_t, mut chat_id: uint32_t) if !(0 == dc_sqlite3_execute(context, &context.sql.clone().read().unwrap(), q3)) { sqlite3_free(q3 as *mut libc::c_void); - q3 = 0 as *mut libc::c_char; q3 = sqlite3_mprintf( b"DELETE FROM chats WHERE id=%i;\x00" as *const u8 as *const libc::c_char, @@ -1549,6 +1583,7 @@ pub unsafe fn dc_delete_chat(mut context: &dc_context_t, mut chat_id: uint32_t) dc_chat_unref(obj); sqlite3_free(q3 as *mut libc::c_void); } + pub unsafe fn dc_get_chat_contacts( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -1572,8 +1607,9 @@ pub unsafe fn dc_get_chat_contacts( } sqlite3_finalize(stmt); - return ret; + 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); @@ -1589,6 +1625,7 @@ pub unsafe fn dc_get_chat(mut context: &dc_context_t, mut chat_id: uint32_t) -> return 0 as *mut dc_chat_t; }; } + // handle group chats pub unsafe fn dc_create_group_chat( mut context: &dc_context_t, @@ -1596,10 +1633,10 @@ pub unsafe fn dc_create_group_chat( mut chat_name: *const libc::c_char, ) -> uint32_t { let mut chat_id: uint32_t = 0i32 as uint32_t; - let mut draft_txt: *mut libc::c_char = 0 as *mut libc::c_char; + let mut draft_txt: *mut libc::c_char; 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; + let mut grpid: *mut libc::c_char; + let mut stmt: *mut sqlite3_stmt; if chat_name.is_null() || *chat_name.offset(0isize) as libc::c_int == 0i32 { return 0i32 as uint32_t; } @@ -1642,17 +1679,20 @@ pub unsafe fn dc_create_group_chat( 0i32 as uintptr_t, ); } - return chat_id; + + chat_id } + /* you MUST NOT modify this or the following strings */ // Context functions to work with chats +// TODO should return bool /rtn pub unsafe fn dc_add_to_chat_contacts_table( mut context: &dc_context_t, mut chat_id: uint32_t, mut contact_id: uint32_t, ) -> libc::c_int { /* 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 ret: libc::c_int; let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -1667,15 +1707,18 @@ pub unsafe fn dc_add_to_chat_contacts_table( 0i32 }; sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_add_contact_to_chat( mut context: &dc_context_t, mut chat_id: uint32_t, mut contact_id: uint32_t, ) -> libc::c_int { - return dc_add_contact_to_chat_ex(context, chat_id, contact_id, 0i32); + dc_add_contact_to_chat_ex(context, chat_id, contact_id, 0i32) } + +// TODO should return bool /rtn pub unsafe fn dc_add_contact_to_chat_ex( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -1794,11 +1837,14 @@ pub unsafe fn dc_add_contact_to_chat_ex( dc_contact_unref(contact); dc_msg_unref(msg); free(self_addr as *mut libc::c_void); - return success; + + success } + +// TODO should return bool /rtn unsafe fn real_group_exists(mut context: &dc_context_t, mut chat_id: uint32_t) -> libc::c_int { // 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 stmt: *mut sqlite3_stmt; let mut ret: libc::c_int = 0i32; if (*context.sql.clone().read().unwrap()).cobj.is_null() || chat_id <= 9i32 as libc::c_uint { return 0i32; @@ -1814,17 +1860,19 @@ unsafe fn real_group_exists(mut context: &dc_context_t, mut chat_id: uint32_t) - ret = 1i32 } sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_reset_gossiped_timestamp(mut context: &dc_context_t, mut chat_id: uint32_t) { dc_set_gossiped_timestamp(context, chat_id, 0i32 as time_t); } + pub unsafe fn dc_set_gossiped_timestamp( mut context: &dc_context_t, mut chat_id: uint32_t, mut timestamp: time_t, ) { - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; if 0 != chat_id { dc_log_info( context, @@ -1858,6 +1906,8 @@ pub unsafe fn dc_set_gossiped_timestamp( sqlite3_step(stmt); sqlite3_finalize(stmt); } + +// TODO should return bool /rtn pub unsafe fn dc_remove_contact_from_chat( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -1940,8 +1990,10 @@ pub unsafe fn dc_remove_contact_from_chat( dc_chat_unref(chat); dc_contact_unref(contact); dc_msg_unref(msg); - return success; + + success } + pub unsafe fn dc_set_group_explicitly_left( mut context: &dc_context_t, mut grpid: *const libc::c_char, @@ -1957,6 +2009,8 @@ pub unsafe fn dc_set_group_explicitly_left( sqlite3_finalize(stmt); }; } + +// TODO should return bool /rtn pub unsafe fn dc_is_group_explicitly_left( mut context: &dc_context_t, mut grpid: *const libc::c_char, @@ -1969,8 +2023,10 @@ pub unsafe fn dc_is_group_explicitly_left( sqlite3_bind_text(stmt, 1i32, grpid, -1i32, None); let mut ret: libc::c_int = (sqlite3_step(stmt) == 100i32) as libc::c_int; sqlite3_finalize(stmt); - return ret; + ret } + +// TODO should return bool /rtn pub unsafe fn dc_set_chat_name( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -2037,11 +2093,15 @@ pub unsafe fn dc_set_chat_name( } } } + sqlite3_free(q3 as *mut libc::c_void); dc_chat_unref(chat); dc_msg_unref(msg); - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_set_chat_profile_image( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -2117,11 +2177,14 @@ pub unsafe fn dc_set_chat_profile_image( } } } + dc_chat_unref(chat); dc_msg_unref(msg); free(new_image_rel as *mut libc::c_void); - return success; + + success } + pub unsafe fn dc_forward_msgs( mut context: &dc_context_t, mut msg_ids: *const uint32_t, @@ -2135,7 +2198,7 @@ pub unsafe fn dc_forward_msgs( let mut idsstr: *mut libc::c_char = 0 as *mut libc::c_char; let mut q3: *mut libc::c_char = 0 as *mut libc::c_char; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - let mut curr_timestamp: time_t = 0i32 as time_t; + let mut curr_timestamp: time_t; let mut original_param: *mut dc_param_t = dc_param_new(); if !(msg_ids.is_null() || msg_cnt <= 0i32 || chat_id <= 9i32 as libc::c_uint) { dc_unarchive_chat(context, chat_id); @@ -2163,7 +2226,7 @@ pub unsafe fn dc_forward_msgs( dc_param_set((*msg).param, 'c' as i32, 0 as *const libc::c_char); dc_param_set((*msg).param, 'u' as i32, 0 as *const libc::c_char); dc_param_set((*msg).param, 'S' as i32, 0 as *const libc::c_char); - let mut new_msg_id: uint32_t = 0; + let mut new_msg_id: uint32_t; if (*msg).state == 18i32 { let fresh9 = curr_timestamp; curr_timestamp = curr_timestamp + 1; @@ -2228,24 +2291,28 @@ pub unsafe fn dc_forward_msgs( sqlite3_free(q3 as *mut libc::c_void); dc_param_unref(original_param); } + pub unsafe fn dc_chat_get_id(mut chat: *const dc_chat_t) -> uint32_t { if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 { return 0i32 as uint32_t; } - return (*chat).id; + (*chat).id } + pub unsafe fn dc_chat_get_type(mut chat: *const dc_chat_t) -> libc::c_int { if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 { return 0i32; } - return (*chat).type_0; + (*chat).type_0 } + pub unsafe fn dc_chat_get_name(mut chat: *const dc_chat_t) -> *mut libc::c_char { if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 { return dc_strdup(b"Err\x00" as *const u8 as *const libc::c_char); } - return dc_strdup((*chat).name); + dc_strdup((*chat).name) } + pub unsafe extern "C" fn dc_chat_get_subtitle(mut chat: *const dc_chat_t) -> *mut libc::c_char { /* returns either the address or the number of chat members */ let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; @@ -2255,7 +2322,7 @@ pub unsafe extern "C" fn dc_chat_get_subtitle(mut chat: *const dc_chat_t) -> *mu if (*chat).type_0 == 100i32 && 0 != dc_param_exists((*chat).param, 'K' as i32) { ret = dc_stock_str((*chat).context, 50i32) } else if (*chat).type_0 == 100i32 { - let mut r: libc::c_int = 0; + let mut r: libc::c_int; let mut stmt: *mut sqlite3_stmt = dc_sqlite3_prepare( (*chat).context,&mut (*chat).context.sql.clone().read().unwrap(), @@ -2268,7 +2335,7 @@ pub unsafe extern "C" fn dc_chat_get_subtitle(mut chat: *const dc_chat_t) -> *mu } sqlite3_finalize(stmt); } else if (*chat).type_0 == 120i32 || (*chat).type_0 == 130i32 { - let mut cnt: libc::c_int = 0i32; + let mut cnt: libc::c_int; if (*chat).id == 1i32 as libc::c_uint { ret = dc_stock_str((*chat).context, 8i32) } else { @@ -2282,6 +2349,7 @@ pub unsafe extern "C" fn dc_chat_get_subtitle(mut chat: *const dc_chat_t) -> *mu dc_strdup(b"Err\x00" as *const u8 as *const libc::c_char) }; } + pub unsafe fn dc_get_chat_contact_cnt( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -2298,8 +2366,9 @@ pub unsafe fn dc_get_chat_contact_cnt( ret = sqlite3_column_int(stmt, 0i32) } sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_chat_get_profile_image(mut chat: *const dc_chat_t) -> *mut libc::c_char { let mut image_rel: *mut libc::c_char = 0 as *mut libc::c_char; let mut image_abs: *mut libc::c_char = 0 as *mut libc::c_char; @@ -2320,11 +2389,14 @@ pub unsafe fn dc_chat_get_profile_image(mut chat: *const dc_chat_t) -> *mut libc } } } + free(image_rel as *mut libc::c_void); dc_array_unref(contacts); dc_contact_unref(contact); - return image_abs; + + image_abs } + pub unsafe fn dc_chat_get_color(mut chat: *const dc_chat_t) -> uint32_t { let mut color: uint32_t = 0i32 as uint32_t; let mut contacts: *mut dc_array_t = 0 as *mut dc_array_t; @@ -2343,34 +2415,45 @@ pub unsafe fn dc_chat_get_color(mut chat: *const dc_chat_t) -> uint32_t { color = dc_str_to_color((*chat).name) as uint32_t } } + dc_array_unref(contacts); dc_contact_unref(contact); - return color; + + color } + +// TODO should return bool /rtn pub unsafe fn dc_chat_get_archived(mut chat: *const dc_chat_t) -> libc::c_int { if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 { return 0i32; } - return (*chat).archived; + (*chat).archived } + +// TODO should return bool /rtn pub unsafe fn dc_chat_is_unpromoted(mut chat: *const dc_chat_t) -> libc::c_int { if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 { return 0i32; } - return dc_param_get_int((*chat).param, 'U' as i32, 0i32); + dc_param_get_int((*chat).param, 'U' as i32, 0i32) } + +// TODO should return bool /rtn pub unsafe fn dc_chat_is_verified(mut chat: *const dc_chat_t) -> libc::c_int { if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 { return 0i32; } - return ((*chat).type_0 == 130i32) as libc::c_int; + ((*chat).type_0 == 130i32) as libc::c_int } + +// TODO should return bool /rtn pub unsafe fn dc_chat_is_sending_locations(mut chat: *const dc_chat_t) -> libc::c_int { if chat.is_null() || (*chat).magic != 0xc4a7c4a7u32 { return 0i32; } - return (*chat).is_sending_locations; + (*chat).is_sending_locations } + 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; @@ -2387,8 +2470,9 @@ pub unsafe fn dc_get_chat_cnt(mut context: &dc_context_t) -> size_t { } } sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_get_chat_id_by_grpid( mut context: &dc_context_t, mut grpid: *const libc::c_char, @@ -2396,7 +2480,7 @@ pub unsafe fn dc_get_chat_id_by_grpid( mut ret_verified: *mut libc::c_int, ) -> uint32_t { let mut chat_id: uint32_t = 0i32 as uint32_t; - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; if !ret_blocked.is_null() { *ret_blocked = 0i32 } @@ -2422,14 +2506,15 @@ pub unsafe fn dc_get_chat_id_by_grpid( } sqlite3_finalize(stmt); - return chat_id; + chat_id } + pub unsafe fn dc_add_device_msg( mut context: &dc_context_t, mut chat_id: uint32_t, mut text: *const libc::c_char, ) { - let mut msg_id: uint32_t = 0i32 as uint32_t; + let mut msg_id: uint32_t; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut rfc724_mid: *mut libc::c_char = dc_create_outgoing_rfc724_mid( 0 as *const libc::c_char, diff --git a/src/dc_chatlist.rs b/src/dc_chatlist.rs index 03312fdca..bf3d7b10a 100644 --- a/src/dc_chatlist.rs +++ b/src/dc_chatlist.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_array::*; use crate::dc_chat::*; use crate::dc_contact::*; @@ -43,7 +41,8 @@ pub unsafe fn dc_get_chatlist<'a>( return 0 as *mut dc_chatlist_t; }; } -/* * + +/** * @class dc_chatlist_t * * An object representing a single chatlist in memory. @@ -82,7 +81,7 @@ pub unsafe fn dc_get_chatlist<'a>( * would not add extra work in the UI then. */ pub unsafe fn dc_chatlist_new(mut context: &dc_context_t) -> *mut dc_chatlist_t { - let mut chatlist: *mut dc_chatlist_t = 0 as *mut dc_chatlist_t; + let mut chatlist: *mut dc_chatlist_t; chatlist = calloc(1, ::std::mem::size_of::()) as *mut dc_chatlist_t; if chatlist.is_null() { exit(20i32); @@ -93,8 +92,9 @@ pub unsafe fn dc_chatlist_new(mut context: &dc_context_t) -> *mut dc_chatlist_t if (*chatlist).chatNlastmsg_ids.is_null() { exit(32i32); } - return chatlist; + chatlist } + pub unsafe fn dc_chatlist_unref(mut chatlist: *mut dc_chatlist_t) { if chatlist.is_null() || (*chatlist).magic != 0xc4a71157u32 { return; @@ -104,6 +104,7 @@ pub unsafe fn dc_chatlist_unref(mut chatlist: *mut dc_chatlist_t) { (*chatlist).magic = 0i32 as uint32_t; free(chatlist as *mut libc::c_void); } + pub unsafe fn dc_chatlist_empty(mut chatlist: *mut dc_chatlist_t) { if chatlist.is_null() || (*chatlist).magic != 0xc4a71157u32 { return; @@ -111,11 +112,13 @@ pub unsafe fn dc_chatlist_empty(mut chatlist: *mut dc_chatlist_t) { (*chatlist).cnt = 0i32 as size_t; dc_array_empty((*chatlist).chatNlastmsg_ids); } -/* * + +/** * Load a chatlist from the database to the chatlist object. * * @private @memberof dc_chatlist_t */ +// TODO should return bool /rtn unsafe fn dc_chatlist_load_from_db( mut chatlist: *mut dc_chatlist_t, mut listflags: libc::c_int, @@ -226,8 +229,9 @@ unsafe fn dc_chatlist_load_from_db( sqlite3_finalize(stmt); free(query as *mut libc::c_void); free(strLikeCmd as *mut libc::c_void); - return success; + success } + // Context functions to work with chatlist pub unsafe fn dc_get_archived_cnt(mut context: &dc_context_t) -> libc::c_int { let mut ret: libc::c_int = 0i32; @@ -241,11 +245,12 @@ pub unsafe fn dc_get_archived_cnt(mut context: &dc_context_t) -> libc::c_int { ret = sqlite3_column_int(stmt, 0i32) } sqlite3_finalize(stmt); - return ret; + ret } + 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; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, @@ -257,14 +262,16 @@ unsafe fn get_last_deaddrop_fresh_msg(mut context: &dc_context_t) -> uint32_t { ret = sqlite3_column_int(stmt, 0i32) as uint32_t } sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_chatlist_get_cnt(mut chatlist: *const dc_chatlist_t) -> size_t { if chatlist.is_null() || (*chatlist).magic != 0xc4a71157u32 { return 0i32 as size_t; } - return (*chatlist).cnt; + (*chatlist).cnt } + pub unsafe fn dc_chatlist_get_chat_id( mut chatlist: *const dc_chatlist_t, mut index: size_t, @@ -276,8 +283,9 @@ pub unsafe fn dc_chatlist_get_chat_id( { return 0i32 as uint32_t; } - return dc_array_get_id((*chatlist).chatNlastmsg_ids, index.wrapping_mul(2)); + dc_array_get_id((*chatlist).chatNlastmsg_ids, index.wrapping_mul(2)) } + pub unsafe fn dc_chatlist_get_msg_id( mut chatlist: *const dc_chatlist_t, mut index: size_t, @@ -289,11 +297,12 @@ pub unsafe fn dc_chatlist_get_msg_id( { return 0i32 as uint32_t; } - return dc_array_get_id( + dc_array_get_id( (*chatlist).chatNlastmsg_ids, index.wrapping_mul(2).wrapping_add(1), - ); + ) } + pub unsafe fn dc_chatlist_get_summary<'a>( mut chatlist: *const dc_chatlist_t<'a>, mut index: size_t, @@ -307,7 +316,7 @@ pub unsafe fn dc_chatlist_get_summary<'a>( message. */ /* the function never returns NULL */ let mut ret: *mut dc_lot_t = dc_lot_new(); - let mut lastmsg_id: uint32_t = 0i32 as uint32_t; + let mut lastmsg_id: uint32_t; let mut lastmsg: *mut dc_msg_t = 0 as *mut dc_msg_t; let mut lastcontact: *mut dc_contact_t = 0 as *mut dc_contact_t; let mut chat_to_delete: *mut dc_chat_t = 0 as *mut dc_chat_t; @@ -364,5 +373,5 @@ pub unsafe fn dc_chatlist_get_summary<'a>( dc_msg_unref(lastmsg); dc_contact_unref(lastcontact); dc_chat_unref(chat_to_delete); - return ret; + ret } diff --git a/src/dc_configure.rs b/src/dc_configure.rs index 8dc380343..d0811637d 100644 --- a/src/dc_configure.rs +++ b/src/dc_configure.rs @@ -1,5 +1,3 @@ -use libc; - use crate::constants::Event; use crate::dc_context::dc_context_t; use crate::dc_e2ee::*; @@ -114,7 +112,7 @@ pub unsafe fn dc_stop_ongoing_process(context: &dc_context_t) { } // the other dc_job_do_DC_JOB_*() functions are declared static in the c-file pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut dc_job_t) { - let mut flags: libc::c_int = 0; + let mut flags: libc::c_int; let mut current_block: u64; let mut success: libc::c_int = 0i32; let mut imap_connected_here: libc::c_int = 0i32; @@ -123,7 +121,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut let mut mvbox_folder: *mut libc::c_char = 0 as *mut libc::c_char; let mut param: *mut dc_loginparam_t = 0 as *mut dc_loginparam_t; /* just a pointer inside param, must not be freed! */ - let mut param_domain: *mut libc::c_char = 0 as *mut libc::c_char; + let mut param_domain: *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; @@ -136,62 +134,37 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut 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 - .clone() - .lock() - .unwrap() - .imap - .clone() - .lock() - .unwrap(), - ); - dc_imap_disconnect( - context, - &mut context - .mvbox_thread - .clone() - .lock() - .unwrap() - .imap - .clone() - .lock() - .unwrap(), - ); - context.smtp.clone().lock().unwrap().disconnect(); - context.inbox.clone().lock().unwrap().log_connect_errors = 1i32; + context.inbox.read().unwrap().disconnect(context); context .sentbox_thread - .clone() .lock() .unwrap() .imap - .clone() .lock() .unwrap() - .log_connect_errors = 1i32; + .disconnect(context); context .mvbox_thread - .clone() .lock() .unwrap() .imap - .clone() .lock() .unwrap() - .log_connect_errors = 1i32; + .disconnect(context); + context.smtp.clone().lock().unwrap().disconnect(); dc_log_info( context, 0i32, b"Configure ...\x00" as *const u8 as *const libc::c_char, ); + + println!("configure lock"); let s_a = context.running_state.clone(); let s = s_a.read().unwrap(); - if s.shall_stop_ongoing { + println!("ongoing: {:?}", s); + if !s.shall_stop_ongoing { + println!("configure progress"); (context.cb)( context, Event::CONFIGURE_PROGRESS, @@ -205,12 +178,15 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut 0i32 as uintptr_t, ); param = dc_loginparam_new(); + println!("reading params"); dc_loginparam_read( context, param, &context.sql.clone().read().unwrap(), b"\x00" as *const u8 as *const libc::c_char, ); + println!("got params"); + if (*param).addr.is_null() { dc_log_error( context, @@ -846,11 +822,12 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut r_0, ); free(r_0 as *mut libc::c_void); - if 0 != dc_imap_connect( - context, - &mut context.inbox.clone().lock().unwrap(), - param, - ) { + if 0 != context + .inbox + .read() + .unwrap() + .connect(context, param) + { current_block = 14187386403465544025; break; } @@ -894,11 +871,12 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut r_1, ); free(r_1 as *mut libc::c_void); - if 0 != dc_imap_connect( - context, - &mut context.inbox.clone().lock().unwrap(), - param, - ) { + if 0 != context + .inbox + .read() + .unwrap() + .connect(context, param) + { current_block = 14187386403465544025; break; } @@ -936,11 +914,12 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut r_2, ); free(r_2 as *mut libc::c_void); - if 0 != dc_imap_connect( - context, - &mut context.inbox.clone().lock().unwrap(), - param, - ) { + if 0 != context + .inbox + .read() + .unwrap() + .connect(context, param) + { current_block = 14187386403465544025; break; } @@ -1184,15 +1163,14 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut } else { 0i32 }; - dc_configure_folders( - context, - &mut context - .inbox - .clone() - .lock() - .unwrap(), - flags, - ); + + context + .inbox + .read() + .unwrap() + .configure_folders( + context, flags, + ); if !s.shall_stop_ongoing { (context.cb)(context, Event::CONFIGURE_PROGRESS, @@ -1303,7 +1281,7 @@ 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, &mut context.inbox.clone().lock().unwrap()); + context.inbox.read().unwrap().disconnect(context); } if 0 != smtp_connected_here { context.smtp.clone().lock().unwrap().disconnect(); @@ -1331,325 +1309,14 @@ pub unsafe fn dc_free_ongoing(context: &dc_context_t) { s.shall_stop_ongoing = true; } -pub unsafe fn dc_configure_folders( - 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).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(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, - b"DeltaChat\x00" as *const u8 as *const libc::c_char, - ); - iter = (*folder_list).first; - while !iter.is_null() { - let mut folder: *mut dc_imapfolder_t = (if !iter.is_null() { - (*iter).data - } else { - 0 as *mut libc::c_void - }) as *mut dc_imapfolder_t; - if strcmp( - (*folder).name_utf8, - b"DeltaChat\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - || strcmp((*folder).name_utf8, fallback_folder) == 0i32 - { - if mvbox_folder.is_null() { - mvbox_folder = dc_strdup((*folder).name_to_select) - } - } - if (*folder).meaning == 1i32 { - if sentbox_folder.is_null() { - sentbox_folder = dc_strdup((*folder).name_to_select) - } - } - iter = if !iter.is_null() { - (*iter).next - } else { - 0 as *mut clistcell_s - } - } - if mvbox_folder.is_null() && 0 != flags & 0x1i32 { - dc_log_info( - context, - 0i32, - b"Creating MVBOX-folder \"%s\"...\x00" as *const u8 as *const libc::c_char, - b"DeltaChat\x00" as *const u8 as *const libc::c_char, - ); - let mut r: libc::c_int = mailimap_create( - (*imap).etpan, - b"DeltaChat\x00" as *const u8 as *const libc::c_char, - ); - if 0 != dc_imap_is_error(context, imap, r) { - dc_log_warning( - context, - 0i32, - b"Cannot create MVBOX-folder, using trying INBOX subfolder.\x00" as *const u8 - as *const libc::c_char, - ); - r = mailimap_create((*imap).etpan, fallback_folder); - if 0 != dc_imap_is_error(context, imap, r) { - dc_log_warning( - context, - 0i32, - b"Cannot create MVBOX-folder.\x00" as *const u8 as *const libc::c_char, - ); - } else { - mvbox_folder = dc_strdup(fallback_folder); - dc_log_info( - context, - 0i32, - b"MVBOX-folder created as INBOX subfolder.\x00" as *const u8 - as *const libc::c_char, - ); - } - } else { - mvbox_folder = dc_strdup(b"DeltaChat\x00" as *const u8 as *const libc::c_char); - dc_log_info( - context, - 0i32, - b"MVBOX-folder created.\x00" as *const u8 as *const libc::c_char, - ); - } - mailimap_subscribe((*imap).etpan, mvbox_folder); - } - dc_sqlite3_set_config_int( - context, - &context.sql.clone().read().unwrap(), - b"folders_configured\x00" as *const u8 as *const libc::c_char, - 3i32, - ); - dc_sqlite3_set_config( - context, - &context.sql.clone().read().unwrap(), - b"configured_mvbox_folder\x00" as *const u8 as *const libc::c_char, - mvbox_folder, - ); - dc_sqlite3_set_config( - context, - &context.sql.clone().read().unwrap(), - b"configured_sentbox_folder\x00" as *const u8 as *const libc::c_char, - sentbox_folder, - ); - } - free_folders(folder_list); - free(mvbox_folder as *mut libc::c_void); - free(fallback_folder as *mut libc::c_void); -} -unsafe fn free_folders(mut folders: *mut clist) { - if !folders.is_null() { - let mut iter1: *mut clistiter = 0 as *mut clistiter; - iter1 = (*folders).first; - while !iter1.is_null() { - let mut ret_folder: *mut dc_imapfolder_t = (if !iter1.is_null() { - (*iter1).data - } else { - 0 as *mut libc::c_void - }) as *mut dc_imapfolder_t; - free((*ret_folder).name_to_select as *mut libc::c_void); - free((*ret_folder).name_utf8 as *mut libc::c_void); - free(ret_folder as *mut libc::c_void); - iter1 = if !iter1.is_null() { - (*iter1).next - } else { - 0 as *mut clistcell_s - } - } - clist_free(folders); - }; -} -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).etpan.is_null() { - if 0 != (*imap).has_xlist { - r = mailimap_xlist( - (*imap).etpan, - b"\x00" as *const u8 as *const libc::c_char, - b"*\x00" as *const u8 as *const libc::c_char, - &mut imap_list, - ) - } else { - r = mailimap_list( - (*imap).etpan, - b"\x00" as *const u8 as *const libc::c_char, - b"*\x00" as *const u8 as *const libc::c_char, - &mut imap_list, - ) - } - if 0 != dc_imap_is_error(context, imap, r) || imap_list.is_null() { - imap_list = 0 as *mut clist; - dc_log_warning( - 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( - context, - 0i32, - b"Folder list is empty.\x00" as *const u8 as *const libc::c_char, - ); - } else { - (*imap).imap_delimiter = '.' as i32 as libc::c_char; - iter1 = (*imap_list).first; - while !iter1.is_null() { - let mut imap_folder: *mut mailimap_mailbox_list = (if !iter1.is_null() { - (*iter1).data - } else { - 0 as *mut libc::c_void - }) - as *mut mailimap_mailbox_list; - if 0 != (*imap_folder).mb_delimiter { - (*imap).imap_delimiter = (*imap_folder).mb_delimiter - } - let mut ret_folder: *mut dc_imapfolder_t = - calloc(1, ::std::mem::size_of::()) as *mut dc_imapfolder_t; - if strcasecmp( - (*imap_folder).mb_name, - b"INBOX\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { - (*ret_folder).name_to_select = - dc_strdup(b"INBOX\x00" as *const u8 as *const libc::c_char) - } else { - (*ret_folder).name_to_select = dc_strdup((*imap_folder).mb_name) - } - (*ret_folder).name_utf8 = dc_decode_modified_utf7((*imap_folder).mb_name, 0i32); - (*ret_folder).meaning = get_folder_meaning((*imap_folder).mb_flag); - if (*ret_folder).meaning == 2i32 || (*ret_folder).meaning == 1i32 { - xlist_works = 1i32 - } - clist_insert_after(ret_list, (*ret_list).last, ret_folder as *mut libc::c_void); - iter1 = if !iter1.is_null() { - (*iter1).next - } else { - 0 as *mut clistcell_s - } - } - if 0 == xlist_works { - iter1 = (*ret_list).first; - while !iter1.is_null() { - let mut ret_folder_0: *mut dc_imapfolder_t = (if !iter1.is_null() { - (*iter1).data - } else { - 0 as *mut libc::c_void - }) - as *mut dc_imapfolder_t; - (*ret_folder_0).meaning = get_folder_meaning_by_name((*ret_folder_0).name_utf8); - iter1 = if !iter1.is_null() { - (*iter1).next - } else { - 0 as *mut clistcell_s - } - } - } - } - } - if !imap_list.is_null() { - mailimap_list_result_free(imap_list); - } - return ret_list; -} -unsafe fn get_folder_meaning_by_name(mut folder_name: *const libc::c_char) -> libc::c_int { - // try to get the folder meaning by the name of the folder. - // only used if the server does not support XLIST. - let mut ret_meaning: libc::c_int = 0i32; - // TODO: lots languages missing - maybe there is a list somewhere on other MUAs? - // however, if we fail to find out the sent-folder, - // only watching this folder is not working. at least, this is no show stopper. - // CAVE: if possible, take care not to add a name here that is "sent" in one language - // but sth. different in others - a hard job. - static mut sent_names: *const libc::c_char = - b",sent,sent objects,gesendet,\x00" as *const u8 as *const libc::c_char; - let mut lower: *mut libc::c_char = - dc_mprintf(b",%s,\x00" as *const u8 as *const libc::c_char, folder_name); - dc_strlower_in_place(lower); - if !strstr(sent_names, lower).is_null() { - ret_meaning = 1i32 - } - free(lower as *mut libc::c_void); - return ret_meaning; -} -unsafe fn get_folder_meaning(mut flags: *mut mailimap_mbx_list_flags) -> libc::c_int { - let mut ret_meaning: libc::c_int = 0i32; - if !flags.is_null() { - let mut iter2: *mut clistiter = 0 as *mut clistiter; - iter2 = (*(*flags).mbf_oflags).first; - while !iter2.is_null() { - let mut oflag: *mut mailimap_mbx_list_oflag = (if !iter2.is_null() { - (*iter2).data - } else { - 0 as *mut libc::c_void - }) - as *mut mailimap_mbx_list_oflag; - match (*oflag).of_type { - 2 => { - if strcasecmp( - (*oflag).of_flag_ext, - b"spam\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - || strcasecmp( - (*oflag).of_flag_ext, - b"trash\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - || strcasecmp( - (*oflag).of_flag_ext, - b"drafts\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - || strcasecmp( - (*oflag).of_flag_ext, - b"junk\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { - ret_meaning = 2i32 - } else if strcasecmp( - (*oflag).of_flag_ext, - b"sent\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { - ret_meaning = 1i32 - } - } - _ => {} - } - iter2 = if !iter2.is_null() { - (*iter2).next - } else { - 0 as *mut clistcell_s - } - } - } - return ret_meaning; -} unsafe fn moz_autoconfigure( mut context: &dc_context_t, mut url: *const libc::c_char, mut param_in: *const dc_loginparam_t, ) -> *mut dc_loginparam_t { - let mut p: *mut libc::c_char = 0 as *mut libc::c_char; - let mut saxparser: dc_saxparser_t = dc_saxparser_t { - starttag_cb: None, - endtag_cb: None, - text_cb: None, - userdata: 0 as *mut libc::c_void, - }; - let mut xml_raw: *mut libc::c_char = 0 as *mut libc::c_char; + let mut p: *mut libc::c_char; + let mut saxparser: dc_saxparser_t; + let mut xml_raw: *mut libc::c_char; let mut moz_ac: moz_autoconfigure_t = moz_autoconfigure_t { in_0: 0 as *const dc_loginparam_t, in_emaildomain: 0 as *mut libc::c_char, @@ -1714,6 +1381,7 @@ unsafe fn moz_autoconfigure( free(moz_ac.in_emaillocalpart as *mut libc::c_void); return moz_ac.out; } + unsafe fn moz_autoconfigure_text_cb( userdata: *mut libc::c_void, text: *const libc::c_char, @@ -1866,7 +1534,7 @@ unsafe fn read_autoconf_file( mut context: &dc_context_t, mut url: *const libc::c_char, ) -> *mut libc::c_char { - let mut filecontent: *mut libc::c_char = 0 as *mut libc::c_char; + let mut filecontent: *mut libc::c_char; dc_log_info( context, 0i32, @@ -1908,7 +1576,6 @@ unsafe fn outlk_autodiscover( redirect: 0 as *mut libc::c_char, }; let mut i: libc::c_int = 0; - i = 0i32; loop { if !(i < 10i32) { current_block = 11584701595673473500; @@ -1985,7 +1652,6 @@ unsafe fn outlk_autodiscover( } unsafe fn outlk_clean_config(mut outlk_ad: *mut outlk_autodiscover_t) { let mut i: libc::c_int = 0; - i = 0i32; while i < 6i32 { free((*outlk_ad).config[i as usize] as *mut libc::c_void); (*outlk_ad).config[i as usize] = 0 as *mut libc::c_char; @@ -2096,11 +1762,12 @@ pub unsafe fn dc_alloc_ongoing(context: &dc_context_t) -> libc::c_int { pub unsafe fn dc_connect_to_configured_imap( context: &dc_context_t, - imap: &mut dc_imap_t, + imap: &dc_imap_t, ) -> libc::c_int { + println!("connect to imap"); let mut ret_connected: libc::c_int = 0i32; let mut param: *mut dc_loginparam_t = dc_loginparam_new(); - if 0 != dc_imap_is_connected(imap) { + if imap.is_connected() { ret_connected = 1i32 } else if dc_sqlite3_get_config_int( context, @@ -2122,10 +1789,11 @@ pub unsafe fn dc_connect_to_configured_imap( b"configured_\x00" as *const u8 as *const libc::c_char, ); /*the trailing underscore is correct*/ - if !(0 == dc_imap_connect(context, imap, param)) { + if !(0 == imap.connect(context, param)) { ret_connected = 2i32 } } dc_loginparam_unref(param); - return ret_connected; + println!("done, {}", ret_connected); + ret_connected } diff --git a/src/dc_contact.rs b/src/dc_contact.rs index fd74a7e18..6e35c22e3 100644 --- a/src/dc_contact.rs +++ b/src/dc_contact.rs @@ -1,5 +1,3 @@ -use libc; - use crate::constants::Event; use crate::dc_apeerstate::*; use crate::dc_array::*; @@ -46,7 +44,9 @@ pub unsafe fn dc_marknoticed_contact(context: &dc_context_t, contact_id: uint32_ 0i32 as uintptr_t, ); } + // handle contacts +// TODO should return bool /rtn pub unsafe extern "C" fn dc_may_be_valid_addr(mut addr: *const libc::c_char) -> libc::c_int { if addr.is_null() { return 0i32; @@ -63,8 +63,10 @@ pub unsafe extern "C" fn dc_may_be_valid_addr(mut addr: *const libc::c_char) -> { return 0i32; } - return 1i32; + + 1 } + pub unsafe fn dc_lookup_contact_id_by_addr( mut context: &dc_context_t, mut addr: *const libc::c_char, @@ -106,8 +108,10 @@ pub unsafe fn dc_lookup_contact_id_by_addr( sqlite3_finalize(stmt); free(addr_normalized as *mut libc::c_void); free(addr_self as *mut libc::c_void); - return contact_id as uint32_t; + + contact_id as uint32_t } + pub unsafe fn dc_addr_normalize(mut addr: *const libc::c_char) -> *mut libc::c_char { let mut addr_normalized: *mut libc::c_char = dc_strdup(addr); dc_trim(addr_normalized); @@ -122,8 +126,10 @@ pub unsafe fn dc_addr_normalize(mut addr: *const libc::c_char) -> *mut libc::c_c free(old as *mut libc::c_void); dc_trim(addr_normalized); } - return addr_normalized; + + addr_normalized } + pub unsafe fn dc_create_contact( mut context: &dc_context_t, mut name: *const libc::c_char, @@ -131,7 +137,7 @@ pub unsafe fn dc_create_contact( ) -> uint32_t { 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; + let mut blocked: libc::c_int; 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); @@ -149,8 +155,10 @@ pub unsafe fn dc_create_contact( dc_block_contact(context, contact_id, 0i32); } } - return contact_id; + + contact_id } + pub unsafe fn dc_block_contact( mut context: &dc_context_t, mut contact_id: uint32_t, @@ -176,7 +184,6 @@ pub unsafe fn dc_block_contact( current_block = 5249903830285462583; } else { sqlite3_finalize(stmt); - stmt = 0 as *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context,&context.sql.clone().read().unwrap(), @@ -214,7 +221,8 @@ pub unsafe fn dc_block_contact( sqlite3_finalize(stmt); dc_contact_unref(contact); } -/* * + +/** * @class dc_contact_t * * An object representing a single contact in memory. @@ -234,15 +242,17 @@ pub unsafe fn dc_block_contact( * only affect the given-name. */ 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; + let mut contact: *mut dc_contact_t; contact = calloc(1, ::std::mem::size_of::()) as *mut dc_contact_t; if contact.is_null() { exit(19i32); } (*contact).magic = 0xc047ac7i32 as uint32_t; (*contact).context = context; - return contact; + + contact } + pub unsafe fn dc_contact_unref(contact: *mut dc_contact_t) { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return; @@ -251,6 +261,7 @@ pub unsafe fn dc_contact_unref(contact: *mut dc_contact_t) { (*contact).magic = 0i32 as uint32_t; free(contact as *mut libc::c_void); } + pub unsafe fn dc_contact_empty(mut contact: *mut dc_contact_t) { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return; @@ -265,6 +276,7 @@ pub unsafe fn dc_contact_empty(mut contact: *mut dc_contact_t) { (*contact).origin = 0i32; (*contact).blocked = 0i32; } + /* From: of incoming messages of unknown sender */ /* Cc: of incoming messages of unknown sender */ /* To: of incoming messages of unknown sender */ @@ -284,6 +296,7 @@ pub unsafe fn dc_contact_empty(mut contact: *mut dc_contact_t) { /* contacts with at least this origin value are shown in the contact list */ /* contacts with at least this origin value are verified and known not to be spam */ /* contacts with at least this origin value start a new "normal" chat, defaults to off */ +// TODO should return bool /rtn pub unsafe fn dc_contact_load_from_db( contact: *mut dc_contact_t, sql: &dc_sqlite3_t, @@ -330,8 +343,11 @@ pub unsafe fn dc_contact_load_from_db( } } sqlite3_finalize(stmt); - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_is_contact_blocked( mut context: &dc_context_t, mut contact_id: uint32_t, @@ -344,8 +360,10 @@ pub unsafe fn dc_is_contact_blocked( } } dc_contact_unref(contact); - return is_blocked; + + is_blocked } + /*can be NULL*/ pub unsafe fn dc_add_or_lookup_contact( mut context: &dc_context_t, @@ -396,7 +414,7 @@ pub unsafe fn dc_add_or_lookup_contact( as *const u8 as *const libc::c_char); sqlite3_bind_text(stmt, 1i32, addr as *const libc::c_char, -1i32, None); if sqlite3_step(stmt) == 100i32 { - let mut row_origin: libc::c_int = 0; + let mut row_origin: libc::c_int; let mut update_addr: libc::c_int = 0i32; let mut update_name: libc::c_int = 0i32; let mut update_authname: libc::c_int = 0i32; @@ -487,7 +505,6 @@ pub unsafe fn dc_add_or_lookup_contact( } } else { sqlite3_finalize(stmt); - stmt = 0 as *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -532,15 +549,17 @@ pub unsafe fn dc_add_or_lookup_contact( free(row_name as *mut libc::c_void); free(row_authname as *mut libc::c_void); sqlite3_finalize(stmt); - return row_id; + + row_id } + pub unsafe fn dc_add_address_book( mut context: &dc_context_t, mut adr_book: *const libc::c_char, ) -> libc::c_int { let mut lines: *mut carray = 0 as *mut carray; - let mut i: size_t = 0i32 as size_t; - let mut iCnt: size_t = 0i32 as size_t; + let mut i: size_t; + let mut iCnt: size_t; let mut sth_modified: libc::c_int = 0i32; let mut modify_cnt: libc::c_int = 0i32; if !(adr_book.is_null()) { @@ -571,8 +590,10 @@ pub unsafe fn dc_add_address_book( } } dc_free_splitted_lines(lines); - return modify_cnt; + + modify_cnt } + // Working with names pub unsafe fn dc_normalize_name(mut full_name: *mut libc::c_char) { if full_name.is_null() { @@ -607,13 +628,14 @@ pub unsafe fn dc_normalize_name(mut full_name: *mut libc::c_char) { dc_trim(full_name); }; } + pub unsafe fn dc_get_contacts( mut context: &dc_context_t, mut listflags: uint32_t, mut query: *const libc::c_char, ) -> *mut dc_array_t { let mut current_block: u64; - let mut self_addr: *mut libc::c_char = 0 as *mut libc::c_char; + let mut self_addr: *mut libc::c_char; 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; @@ -704,12 +726,13 @@ pub unsafe fn dc_get_contacts( free(self_addr as *mut libc::c_void); free(self_name as *mut libc::c_void); free(self_name2 as *mut libc::c_void); - return ret; + + ret } 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; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, @@ -723,12 +746,12 @@ pub unsafe fn dc_get_blocked_cnt(context: &dc_context_t) -> libc::c_int { } sqlite3_finalize(stmt); - return ret; + 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(100i32 as size_t); - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, @@ -742,18 +765,14 @@ pub unsafe fn dc_get_blocked_contacts(mut context: &dc_context_t) -> *mut dc_arr } sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_get_contact_encrinfo( mut context: &dc_context_t, mut contact_id: uint32_t, ) -> *mut libc::c_char { - let mut ret: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; + let mut ret: dc_strbuilder_t; let mut loginparam: *mut dc_loginparam_t = dc_loginparam_new(); let mut contact: *mut dc_contact_t = dc_contact_new(context); let mut peerstate: *mut dc_apeerstate_t = dc_apeerstate_new(context); @@ -761,7 +780,7 @@ pub unsafe fn dc_get_contact_encrinfo( let mut fingerprint_self: *mut libc::c_char = 0 as *mut libc::c_char; 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; + let mut p: *mut libc::c_char; ret = dc_strbuilder_t { buf: 0 as *mut libc::c_char, @@ -865,8 +884,10 @@ pub unsafe fn dc_get_contact_encrinfo( free(fingerprint_self as *mut libc::c_void); free(fingerprint_other_verified as *mut libc::c_void); free(fingerprint_other_unverified as *mut libc::c_void); - return ret.buf; + + ret.buf } + unsafe fn cat_fingerprint( mut ret: *mut dc_strbuilder_t, mut addr: *const libc::c_char, @@ -901,6 +922,8 @@ unsafe fn cat_fingerprint( dc_strbuilder_cat(ret, fingerprint_unverified); }; } + +// TODO should return bool /rtn pub unsafe fn dc_delete_contact( mut context: &dc_context_t, mut contact_id: uint32_t, @@ -917,7 +940,6 @@ pub unsafe fn dc_delete_contact( sqlite3_bind_int(stmt, 1i32, contact_id as libc::c_int); if !(sqlite3_step(stmt) != 100i32 || sqlite3_column_int(stmt, 0i32) >= 1i32) { sqlite3_finalize(stmt); - stmt = 0 as *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -928,7 +950,6 @@ pub unsafe fn dc_delete_contact( sqlite3_bind_int(stmt, 2i32, contact_id as libc::c_int); if !(sqlite3_step(stmt) != 100i32 || sqlite3_column_int(stmt, 0i32) >= 1i32) { sqlite3_finalize(stmt); - stmt = 0 as *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -948,8 +969,10 @@ pub unsafe fn dc_delete_contact( } } sqlite3_finalize(stmt); - return success; + + success } + pub unsafe fn dc_get_contact( mut context: &dc_context_t, mut contact_id: uint32_t, @@ -959,26 +982,30 @@ pub unsafe fn dc_get_contact( dc_contact_unref(ret); ret = 0 as *mut dc_contact_t } - return ret; + ret } + pub unsafe fn dc_contact_get_id(mut contact: *const dc_contact_t) -> uint32_t { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return 0i32 as uint32_t; } - return (*contact).id; + (*contact).id } + pub unsafe fn dc_contact_get_addr(mut contact: *const dc_contact_t) -> *mut libc::c_char { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return dc_strdup(0 as *const libc::c_char); } - return dc_strdup((*contact).addr); + dc_strdup((*contact).addr) } + pub unsafe fn dc_contact_get_name(mut contact: *const dc_contact_t) -> *mut libc::c_char { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return dc_strdup(0 as *const libc::c_char); } - return dc_strdup((*contact).name); + dc_strdup((*contact).name) } + pub unsafe fn dc_contact_get_display_name(mut contact: *const dc_contact_t) -> *mut libc::c_char { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return dc_strdup(0 as *const libc::c_char); @@ -986,8 +1013,9 @@ pub unsafe fn dc_contact_get_display_name(mut contact: *const dc_contact_t) -> * if !(*contact).name.is_null() && 0 != *(*contact).name.offset(0isize) as libc::c_int { return dc_strdup((*contact).name); } - return dc_strdup((*contact).addr); + dc_strdup((*contact).addr) } + pub unsafe fn dc_contact_get_name_n_addr(mut contact: *const dc_contact_t) -> *mut libc::c_char { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return dc_strdup(0 as *const libc::c_char); @@ -999,8 +1027,9 @@ pub unsafe fn dc_contact_get_name_n_addr(mut contact: *const dc_contact_t) -> *m (*contact).addr, ); } - return dc_strdup((*contact).addr); + dc_strdup((*contact).addr) } + pub unsafe fn dc_contact_get_first_name(mut contact: *const dc_contact_t) -> *mut libc::c_char { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return dc_strdup(0 as *const libc::c_char); @@ -1008,8 +1037,9 @@ pub unsafe fn dc_contact_get_first_name(mut contact: *const dc_contact_t) -> *mu if !(*contact).name.is_null() && 0 != *(*contact).name.offset(0isize) as libc::c_int { return dc_get_first_name((*contact).name); } - return dc_strdup((*contact).addr); + dc_strdup((*contact).addr) } + pub unsafe fn dc_get_first_name(mut full_name: *const libc::c_char) -> *mut libc::c_char { let mut first_name: *mut libc::c_char = dc_strdup(full_name); let mut p1: *mut libc::c_char = strchr(first_name, ' ' as i32); @@ -1021,8 +1051,9 @@ pub unsafe fn dc_get_first_name(mut full_name: *const libc::c_char) -> *mut libc first_name = dc_strdup(full_name) } } - return first_name; + first_name } + pub unsafe fn dc_contact_get_profile_image(mut contact: *const dc_contact_t) -> *mut libc::c_char { let mut selfavatar: *mut libc::c_char = 0 as *mut libc::c_char; let mut image_abs: *mut libc::c_char = 0 as *mut libc::c_char; @@ -1039,23 +1070,27 @@ pub unsafe fn dc_contact_get_profile_image(mut contact: *const dc_contact_t) -> } // TODO: else get image_abs from contact param free(selfavatar as *mut libc::c_void); - return image_abs; + image_abs } + pub unsafe fn dc_contact_get_color(mut contact: *const dc_contact_t) -> uint32_t { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return 0i32 as uint32_t; } - return dc_str_to_color((*contact).addr) as uint32_t; + dc_str_to_color((*contact).addr) as uint32_t } + pub unsafe fn dc_contact_is_blocked(mut contact: *const dc_contact_t) -> libc::c_int { if contact.is_null() || (*contact).magic != 0xc047ac7i32 as libc::c_uint { return 0i32; } - return (*contact).blocked; + (*contact).blocked } + 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 *mut dc_apeerstate_t); + dc_contact_is_verified_ex(contact, 0 as *mut dc_apeerstate_t) } + pub unsafe fn dc_contact_is_verified_ex<'a>( contact: *mut dc_contact_t<'a>, mut peerstate: *mut dc_apeerstate_t<'a>, @@ -1096,8 +1131,9 @@ pub unsafe fn dc_contact_is_verified_ex<'a>( } } dc_apeerstate_unref(peerstate_to_delete); - return contact_verified; + contact_verified } + // Working with e-mail-addresses pub unsafe fn dc_addr_cmp( mut addr1: *const libc::c_char, @@ -1108,8 +1144,9 @@ pub unsafe fn dc_addr_cmp( let mut ret: libc::c_int = strcasecmp(addr1, addr2); free(norm1 as *mut libc::c_void); free(norm2 as *mut libc::c_void); - return ret; + ret } + pub unsafe fn dc_addr_equals_self( mut context: &dc_context_t, mut addr: *const libc::c_char, @@ -1135,8 +1172,10 @@ pub unsafe fn dc_addr_equals_self( } free(self_addr as *mut libc::c_void); free(normalized_addr as *mut libc::c_void); - return ret; + ret } + +// TODO should return bool /rtn pub unsafe fn dc_addr_equals_contact( mut context: &dc_context_t, mut addr: *const libc::c_char, @@ -1156,8 +1195,9 @@ pub unsafe fn dc_addr_equals_contact( } dc_contact_unref(contact); } - return addr_are_equal; + addr_are_equal } + // Context functions to work with contacts pub unsafe fn dc_get_real_contact_cnt(mut context: &dc_context_t) -> size_t { let mut ret: size_t = 0i32 as size_t; @@ -1174,8 +1214,9 @@ pub unsafe fn dc_get_real_contact_cnt(mut context: &dc_context_t) -> size_t { } } sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_get_contact_origin( mut context: &dc_context_t, mut contact_id: uint32_t, @@ -1197,8 +1238,10 @@ pub unsafe fn dc_get_contact_origin( } } dc_contact_unref(contact); - return ret; + ret } + +// TODO should return bool /rtn pub unsafe fn dc_real_contact_exists( mut context: &dc_context_t, mut contact_id: uint32_t, @@ -1217,8 +1260,9 @@ pub unsafe fn dc_real_contact_exists( } } sqlite3_finalize(stmt); - return ret; + ret } + pub unsafe fn dc_scaleup_contact_origin( mut context: &dc_context_t, mut contact_id: uint32_t, diff --git a/src/dc_context.rs b/src/dc_context.rs index 5a6441ed6..2e5504f53 100644 --- a/src/dc_context.rs +++ b/src/dc_context.rs @@ -1,7 +1,6 @@ -use libc; use std::sync::{Arc, Condvar, Mutex, RwLock}; -use crate::constants::VERSION; +use crate::constants::*; use crate::dc_array::*; use crate::dc_chat::*; use crate::dc_contact::*; @@ -26,10 +25,10 @@ use crate::x::*; #[repr(C)] pub struct dc_context_t { pub userdata: *mut libc::c_void, - pub dbfile: *mut libc::c_char, - pub blobdir: *mut libc::c_char, + pub dbfile: Arc>, + pub blobdir: Arc>, pub sql: Arc>, - pub inbox: Arc>, + pub inbox: Arc>, pub perform_inbox_jobs_needed: Arc>, pub probe_imap_network: Arc>, pub sentbox_thread: Arc>, @@ -54,6 +53,24 @@ pub struct RunningState { pub shall_stop_ongoing: bool, } +impl dc_context_t { + pub fn has_dbfile(&self) -> bool { + !self.get_dbfile().is_null() + } + + pub fn has_blobdir(&self) -> bool { + !self.get_blobdir().is_null() + } + + pub fn get_dbfile(&self) -> *const libc::c_char { + *self.dbfile.clone().read().unwrap() + } + + pub fn get_blobdir(&self) -> *const libc::c_char { + *self.blobdir.clone().read().unwrap() + } +} + impl Default for RunningState { fn default() -> Self { RunningState { @@ -112,14 +129,14 @@ pub fn dc_context_new( os_name: *const libc::c_char, ) -> dc_context_t { dc_context_t { - blobdir: std::ptr::null_mut(), - dbfile: std::ptr::null_mut(), - inbox: Arc::new(Mutex::new(unsafe { + blobdir: Arc::new(RwLock::new(std::ptr::null_mut())), + dbfile: Arc::new(RwLock::new(std::ptr::null_mut())), + inbox: Arc::new(RwLock::new({ dc_imap_new( - Some(cb_get_config), - Some(cb_set_config), - Some(cb_precheck_imf), - Some(cb_receive_imf), + cb_get_config, + cb_set_config, + cb_precheck_imf, + cb_receive_imf, ) })), userdata, @@ -138,10 +155,10 @@ pub fn dc_context_new( b"SENTBOX\x00" as *const u8 as *const libc::c_char, b"configured_sentbox_folder\x00" as *const u8 as *const libc::c_char, dc_imap_new( - Some(cb_get_config), - Some(cb_set_config), - Some(cb_precheck_imf), - Some(cb_receive_imf), + cb_get_config, + cb_set_config, + cb_precheck_imf, + cb_receive_imf, ), ) })), @@ -150,10 +167,10 @@ pub fn dc_context_new( b"MVBOX\x00" as *const u8 as *const libc::c_char, b"configured_mvbox_folder\x00" as *const u8 as *const libc::c_char, dc_imap_new( - Some(cb_get_config), - Some(cb_set_config), - Some(cb_precheck_imf), - Some(cb_receive_imf), + cb_get_config, + cb_set_config, + cb_precheck_imf, + cb_receive_imf, ), ) })), @@ -187,7 +204,7 @@ unsafe fn cb_precheck_imf( server_uid: uint32_t, ) -> libc::c_int { let mut rfc724_mid_exists: libc::c_int = 0i32; - let mut msg_id: uint32_t = 0i32 as uint32_t; + let mut msg_id: uint32_t; let mut old_server_folder: *mut libc::c_char = 0 as *mut libc::c_char; let mut old_server_uid: uint32_t = 0i32 as uint32_t; let mut mark_seen: libc::c_int = 0i32; @@ -235,6 +252,7 @@ unsafe fn cb_precheck_imf( free(old_server_folder as *mut libc::c_void); return rfc724_mid_exists; } + unsafe fn cb_set_config( context: &dc_context_t, key: *const libc::c_char, @@ -242,6 +260,7 @@ unsafe fn cb_set_config( ) { dc_sqlite3_set_config(context, &context.sql.clone().read().unwrap(), key, value); } + /* * * The following three callback are given to dc_imap_new() to read/write configuration * and to handle received messages. As the imap-functions are typically used in @@ -261,32 +280,6 @@ pub unsafe fn dc_context_unref(context: &mut dc_context_t) { if 0 != dc_is_open(context) { dc_close(context); } - dc_imap_unref(context, &mut context.inbox.clone().lock().unwrap()); - dc_imap_unref( - context, - &mut context - .sentbox_thread - .clone() - .lock() - .unwrap() - .imap - .clone() - .lock() - .unwrap(), - ); - dc_imap_unref( - context, - &mut context - .mvbox_thread - .clone() - .lock() - .unwrap() - .imap - .clone() - .lock() - .unwrap(), - ); - dc_sqlite3_unref(context, &mut context.sql.clone().write().unwrap()); dc_jobthread_exit(&mut context.sentbox_thread.clone().lock().unwrap()); @@ -295,41 +288,36 @@ pub unsafe fn dc_context_unref(context: &mut dc_context_t) { free(context.os_name as *mut libc::c_void); } -pub unsafe fn dc_close(context: &mut dc_context_t) { - dc_imap_disconnect(context, &mut context.inbox.clone().lock().unwrap()); - dc_imap_disconnect( - context, - &mut context - .sentbox_thread - .clone() - .lock() - .unwrap() - .imap - .clone() - .lock() - .unwrap(), - ); - dc_imap_disconnect( - context, - &mut context - .mvbox_thread - .clone() - .lock() - .unwrap() - .imap - .clone() - .lock() - .unwrap(), - ); +pub unsafe fn dc_close(context: &dc_context_t) { + context.inbox.read().unwrap().disconnect(context); + context + .sentbox_thread + .lock() + .unwrap() + .imap + .lock() + .unwrap() + .disconnect(context); + context + .mvbox_thread + .lock() + .unwrap() + .imap + .lock() + .unwrap() + .disconnect(context); + context.smtp.clone().lock().unwrap().disconnect(); if 0 != dc_sqlite3_is_open(&context.sql.clone().read().unwrap()) { dc_sqlite3_close(context, &mut context.sql.clone().write().unwrap()); } - free(context.dbfile as *mut libc::c_void); - context.dbfile = 0 as *mut libc::c_char; - free(context.blobdir as *mut libc::c_void); - context.blobdir = 0 as *mut libc::c_char; + let mut dbfile = context.dbfile.write().unwrap(); + free(*dbfile as *mut libc::c_void); + *dbfile = 0 as *mut libc::c_char; + let mut blobdir = context.blobdir.write().unwrap(); + free(*blobdir as *mut libc::c_void); + *blobdir = 0 as *mut libc::c_char; } pub unsafe fn dc_is_open(context: &dc_context_t) -> libc::c_int { @@ -341,7 +329,7 @@ pub unsafe fn dc_get_userdata(context: &mut dc_context_t) -> *mut libc::c_void { } pub unsafe fn dc_open( - context: &mut dc_context_t, + context: &dc_context_t, dbfile: *const libc::c_char, blobdir: *const libc::c_char, ) -> libc::c_int { @@ -350,24 +338,18 @@ pub unsafe fn dc_open( return 0; } if !dbfile.is_null() { - context.dbfile = dc_strdup(dbfile); + *context.dbfile.write().unwrap() = dc_strdup(dbfile); if !blobdir.is_null() && 0 != *blobdir.offset(0isize) as libc::c_int { - context.blobdir = dc_strdup(blobdir); - dc_ensure_no_slash(context.blobdir); + let dir = dc_strdup(blobdir); + dc_ensure_no_slash(dir); + *context.blobdir.write().unwrap() = dir; } else { - context.blobdir = - dc_mprintf(b"%s-blobs\x00" as *const u8 as *const libc::c_char, dbfile); - dc_create_folder(context, context.blobdir); + let dir = dc_mprintf(b"%s-blobs\x00" as *const u8 as *const libc::c_char, dbfile); + dc_create_folder(context, dir); + *context.blobdir.write().unwrap() = dir; } // Create/open sqlite database, this may already use the blobdir - if !(0 - == dc_sqlite3_open( - context, - &mut context.sql.clone().write().unwrap(), - dbfile, - 0i32, - )) - { + if !(0 == dc_sqlite3_open(context, &mut context.sql.write().unwrap(), dbfile, 0i32)) { success = 1i32 } } @@ -378,7 +360,7 @@ pub unsafe fn dc_open( } pub unsafe fn dc_get_blobdir(context: &dc_context_t) -> *mut libc::c_char { - dc_strdup((*context).blobdir) + dc_strdup(*context.blobdir.clone().read().unwrap()) } pub unsafe fn dc_set_config( @@ -634,30 +616,30 @@ unsafe fn get_config_keys_str() -> *mut libc::c_char { pub unsafe fn dc_get_info(context: &dc_context_t) -> *mut libc::c_char { let mut unset = b"0\x00" as *const u8 as *const libc::c_char; - let mut displayname = 0 as *mut libc::c_char; - let mut temp = 0 as *mut libc::c_char; - let mut l_readable_str = 0 as *mut libc::c_char; - let mut l2_readable_str = 0 as *mut libc::c_char; - let mut fingerprint_str = 0 as *mut libc::c_char; - let mut l = 0 as *mut dc_loginparam_t; - let mut l2 = 0 as *mut dc_loginparam_t; - let mut inbox_watch = 0; - let mut sentbox_watch = 0; - let mut mvbox_watch = 0; - let mut mvbox_move = 0; - let mut folders_configured = 0; - let mut configured_sentbox_folder = 0 as *mut libc::c_char; - let mut configured_mvbox_folder = 0 as *mut libc::c_char; - let mut contacts = 0; - let mut chats = 0; - let mut real_msgs = 0; - let mut deaddrop_msgs = 0; - let mut is_configured = 0; - let mut dbversion = 0; - let mut mdns_enabled = 0; - let mut e2ee_enabled = 0; - let mut prv_key_cnt = 0; - let mut pub_key_cnt = 0; + let mut displayname; + let mut temp; + let mut l_readable_str; + let mut l2_readable_str; + let mut fingerprint_str; + let mut l; + let mut l2; + let mut inbox_watch; + let mut sentbox_watch; + let mut mvbox_watch; + let mut mvbox_move; + let mut folders_configured; + let mut configured_sentbox_folder; + let mut configured_mvbox_folder; + let mut contacts; + let mut chats; + let mut real_msgs; + let mut deaddrop_msgs; + let mut is_configured; + let mut dbversion; + let mut mdns_enabled; + let mut e2ee_enabled; + let mut prv_key_cnt; + let mut pub_key_cnt; let mut self_public = dc_key_new(); let mut rpgp_enabled = 1; @@ -822,8 +804,9 @@ pub unsafe fn dc_get_info(context: &dc_context_t) -> *mut libc::c_char { VERSION as *const u8 as *const libc::c_char, libsqlite3_sys::SQLITE_VERSION as *const u8 as *const libc::c_char, sqlite3_threadsafe(), - libetpan_get_version_major(), - libetpan_get_version_minor(), + // no libetpan + 0, + 0, // openssl (none used, so setting to 0) 0 as libc::c_int, 0 as libc::c_int, @@ -836,14 +819,14 @@ pub unsafe fn dc_get_info(context: &dc_context_t) -> *mut libc::c_char { real_msgs, deaddrop_msgs, contacts, - if !context.dbfile.is_null() { - context.dbfile + if context.has_dbfile() { + context.get_dbfile() } else { unset }, dbversion, - if !context.blobdir.is_null() { - context.blobdir + if context.has_blobdir() { + context.get_blobdir() } else { unset }, diff --git a/src/dc_dehtml.rs b/src/dc_dehtml.rs index 082063a0d..dd270dacc 100644 --- a/src/dc_dehtml.rs +++ b/src/dc_dehtml.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_saxparser::*; use crate::dc_strbuilder::*; use crate::dc_tools::*; diff --git a/src/dc_e2ee.rs b/src/dc_e2ee.rs index a917c260c..d6e5ac0ea 100644 --- a/src/dc_e2ee.rs +++ b/src/dc_e2ee.rs @@ -1,4 +1,15 @@ -use libc; +use mmime::clist::*; +use mmime::mailimf::*; +use mmime::mailimf_types::*; +use mmime::mailimf_types_helper::*; +use mmime::mailmime::*; +use mmime::mailmime_content::*; +use mmime::mailmime_types::*; +use mmime::mailmime_types_helper::*; +use mmime::mailmime_write_mem::*; +use mmime::mailprivacy_prepare_mime; +use mmime::mmapstring::*; +use mmime::{mailmime_substitute, MAILIMF_NO_ERROR, MAIL_NO_ERROR}; use crate::dc_aheader::*; use crate::dc_apeerstate::*; @@ -42,13 +53,13 @@ pub unsafe fn dc_e2ee_encrypt( mut in_out_message: *mut mailmime, mut helper: *mut dc_e2ee_helper_t, ) { - let mut p_0: *mut libc::c_char = 0 as *mut libc::c_char; + let mut p_0: *mut libc::c_char; let mut current_block: u64; let mut col: libc::c_int = 0i32; let mut do_encrypt: libc::c_int = 0i32; let mut autocryptheader: *mut dc_aheader_t = dc_aheader_new(); /*just a pointer into mailmime structure, must not be freed*/ - let mut imffields_unprotected: *mut mailimf_fields = 0 as *mut mailimf_fields; + let mut imffields_unprotected: *mut mailimf_fields; let mut keyring: *mut dc_keyring_t = dc_keyring_new(); let mut sign_key: *mut dc_key_t = dc_key_new(); let mut plain: *mut MMAPString = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char); @@ -99,7 +110,7 @@ pub unsafe fn dc_e2ee_encrypt( /*only for random-seed*/ if (*autocryptheader).prefer_encrypt == 1i32 || 0 != e2ee_guaranteed { do_encrypt = 1i32; - let mut iter1: *mut clistiter = 0 as *mut clistiter; + let mut iter1: *mut clistiter; iter1 = (*recipients_addr).first; while !iter1.is_null() { let mut recipient_addr: *const libc::c_char = (if !iter1.is_null() { @@ -132,7 +143,7 @@ pub unsafe fn dc_e2ee_encrypt( iter1 = if !iter1.is_null() { (*iter1).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -248,7 +259,7 @@ pub unsafe fn dc_e2ee_encrypt( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -394,7 +405,8 @@ pub unsafe fn dc_e2ee_encrypt( } dc_array_unref(peerstates); } -/* ****************************************************************************** + +/******************************************************************************* * Tools ******************************************************************************/ unsafe fn new_data_part( @@ -405,15 +417,15 @@ unsafe fn new_data_part( ) -> *mut mailmime { let mut current_block: u64; //char basename_buf[PATH_MAX]; - let mut encoding: *mut mailmime_mechanism = 0 as *mut mailmime_mechanism; - let mut content: *mut mailmime_content = 0 as *mut mailmime_content; - let mut mime: *mut mailmime = 0 as *mut mailmime; + let mut encoding: *mut mailmime_mechanism; + let mut content: *mut mailmime_content; + let mut mime: *mut mailmime; //int r; //char * dup_filename; - let mut mime_fields: *mut mailmime_fields = 0 as *mut mailmime_fields; - let mut encoding_type: libc::c_int = 0; - let mut content_type_str: *mut libc::c_char = 0 as *mut libc::c_char; - let mut do_encoding: libc::c_int = 0; + let mut mime_fields: *mut mailmime_fields; + let mut encoding_type: libc::c_int; + let mut content_type_str: *mut libc::c_char; + let mut do_encoding: libc::c_int; encoding = 0 as *mut mailmime_mechanism; if default_content_type.is_null() { content_type_str = @@ -427,7 +439,7 @@ unsafe fn new_data_part( } else { do_encoding = 1i32; if (*(*content).ct_type).tp_type == MAILMIME_TYPE_COMPOSITE_TYPE as libc::c_int { - let mut composite: *mut mailmime_composite_type = 0 as *mut mailmime_composite_type; + let mut composite: *mut mailmime_composite_type; composite = (*(*content).ct_type).tp_data.tp_composite_type; match (*composite).ct_type { 1 => { @@ -502,9 +514,11 @@ unsafe fn new_data_part( } return 0 as *mut mailmime; } -/* ****************************************************************************** + +/******************************************************************************* * Generate Keypairs ******************************************************************************/ +// TODO should return bool /rtn unsafe fn load_or_generate_self_public_key( mut context: &dc_context_t, mut public_key: *mut dc_key_t, @@ -514,7 +528,7 @@ unsafe fn load_or_generate_self_public_key( let mut current_block: u64; /* avoid double creation (we unlock the database during creation) */ static mut s_in_key_creation: libc::c_int = 0i32; - let mut key_created: libc::c_int = 0i32; + let mut key_created: libc::c_int; let mut success: libc::c_int = 0i32; let mut key_creation_here: libc::c_int = 0i32; if !public_key.is_null() { @@ -531,7 +545,7 @@ unsafe fn load_or_generate_self_public_key( key_creation_here = 1i32; s_in_key_creation = 1i32; if !random_data_mime.is_null() { - let mut random_data_mmap: *mut MMAPString = 0 as *mut MMAPString; + let mut random_data_mmap: *mut MMAPString; let mut col: libc::c_int = 0i32; random_data_mmap = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char); if random_data_mmap.is_null() { @@ -619,15 +633,17 @@ unsafe fn load_or_generate_self_public_key( if 0 != key_creation_here { s_in_key_creation = 0i32 } - return success; + + success } + /* returns 1 if sth. was decrypted, 0 in other cases */ pub unsafe fn dc_e2ee_decrypt( mut context: &dc_context_t, mut in_out_message: *mut mailmime, mut helper: *mut dc_e2ee_helper_t, ) { - let mut iterations: libc::c_int = 0; + let mut iterations: libc::c_int; /* return values: 0=nothing to decrypt/cannot decrypt, 1=sth. decrypted (to detect parts that could not be decrypted, simply look for left "multipart/encrypted" MIME types */ /*just a pointer into mailmime structure, must not be freed*/ @@ -761,13 +777,14 @@ pub unsafe fn dc_e2ee_decrypt( free(from as *mut libc::c_void); free(self_addr as *mut libc::c_void); } + unsafe fn update_gossip_peerstates( mut context: &dc_context_t, mut message_time: time_t, mut imffields: *mut mailimf_fields, mut gossip_headers: *const mailimf_fields, ) -> *mut dc_hash_t { - let mut cur1: *mut clistiter = 0 as *mut clistiter; + let mut cur1: *mut clistiter; let mut recipients: *mut dc_hash_t = 0 as *mut dc_hash_t; let mut gossipped_addr: *mut dc_hash_t = 0 as *mut dc_hash_t; cur1 = (*(*gossip_headers).fld_list).first; @@ -852,15 +869,18 @@ unsafe fn update_gossip_peerstates( cur1 = if !cur1.is_null() { (*cur1).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } if !recipients.is_null() { dc_hash_clear(recipients); free(recipients as *mut libc::c_void); } - return gossipped_addr; + + gossipped_addr } + +// TODO should return bool /rtn unsafe fn decrypt_recursive( mut context: &dc_context_t, mut mime: *mut mailmime, @@ -870,8 +890,8 @@ unsafe fn decrypt_recursive( mut ret_gossip_headers: *mut *mut mailimf_fields, mut ret_has_unencrypted_parts: *mut libc::c_int, ) -> libc::c_int { - let mut ct: *mut mailmime_content = 0 as *mut mailmime_content; - let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut ct: *mut mailmime_content; + let mut cur: *mut clistiter; if mime.is_null() { return 0i32; } @@ -920,7 +940,7 @@ unsafe fn decrypt_recursive( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } *ret_has_unencrypted_parts = 1i32 @@ -945,7 +965,7 @@ unsafe fn decrypt_recursive( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -964,8 +984,10 @@ unsafe fn decrypt_recursive( } else { *ret_has_unencrypted_parts = 1i32 } - return 0i32; + + 0 } + unsafe fn decrypt_part( mut context: &dc_context_t, mut mime: *mut mailmime, @@ -974,9 +996,9 @@ unsafe fn decrypt_part( mut ret_valid_signatures: *mut dc_hash_t, mut ret_decrypted_mime: *mut *mut mailmime, ) -> libc::c_int { - let mut add_signatures: *mut dc_hash_t = 0 as *mut dc_hash_t; + let mut add_signatures: *mut dc_hash_t; let mut current_block: u64; - let mut mime_data: *mut mailmime_data = 0 as *mut mailmime_data; + let mut mime_data: *mut mailmime_data; let mut mime_transfer_encoding: libc::c_int = MAILMIME_MECHANISM_BINARY as libc::c_int; /* mmap_string_unref()'d if set */ let mut transfer_decoding_buffer: *mut libc::c_char = 0 as *mut libc::c_char; @@ -994,7 +1016,7 @@ unsafe fn decrypt_part( || (*mime_data).dt_data.dt_text.dt_length <= 0) { if !(*mime).mm_mime_fields.is_null() { - let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut cur: *mut clistiter; cur = (*(*(*mime).mm_mime_fields).fld_list).first; while !cur.is_null() { let mut field: *mut mailmime_field = (if !cur.is_null() { @@ -1012,7 +1034,7 @@ unsafe fn decrypt_part( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -1030,7 +1052,7 @@ unsafe fn decrypt_part( current_block = 4488286894823169796; } } else { - let mut r: libc::c_int = 0; + let mut r: libc::c_int; let mut current_index: size_t = 0i32 as size_t; r = mailmime_part_parse( (*mime_data).dt_data.dt_text.dt_data, @@ -1105,11 +1127,14 @@ unsafe fn decrypt_part( if !transfer_decoding_buffer.is_null() { mmap_string_unref(transfer_decoding_buffer); } - return sth_decrypted; + + sth_decrypted } -/* ****************************************************************************** + +/******************************************************************************* * Decrypt ******************************************************************************/ +// TODO should return bool /rtn unsafe fn has_decrypted_pgp_armor( mut str__: *const libc::c_char, mut str_bytes: libc::c_int, @@ -1133,9 +1158,11 @@ unsafe fn has_decrypted_pgp_armor( { return 1; } - return 0; + + 0 } -/* * + +/** * Check if a MIME structure contains a multipart/report part. * * As reports are often unencrypted, we do not reset the Autocrypt header in @@ -1149,6 +1176,7 @@ unsafe fn has_decrypted_pgp_armor( * @param mime The mime struture to check * @return 1=multipart/report found in MIME, 0=no multipart/report found */ +// TODO should return bool /rtn unsafe fn contains_report(mut mime: *mut mailmime) -> libc::c_int { if (*mime).mm_type == MAILMIME_MULTIPLE as libc::c_int { if (*(*(*mime).mm_content_type).ct_type).tp_type @@ -1165,7 +1193,7 @@ unsafe fn contains_report(mut mime: *mut mailmime) -> libc::c_int { { return 1i32; } - let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut cur: *mut clistiter; cur = (*(*mime).mm_data.mm_multipart.mm_mp_list).first; while !cur.is_null() { if 0 != contains_report( @@ -1180,7 +1208,7 @@ unsafe fn contains_report(mut mime: *mut mailmime) -> libc::c_int { cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } else if (*mime).mm_type == MAILMIME_MESSAGE as libc::c_int { @@ -1188,8 +1216,10 @@ unsafe fn contains_report(mut mime: *mut mailmime) -> libc::c_int { return 1i32; } } - return 0i32; + + 0 } + /* frees data referenced by "mailmime" but not freed by mailmime_free(). After calling this function, in_out_message cannot be used any longer! */ pub unsafe fn dc_e2ee_thanks(mut helper: *mut dc_e2ee_helper_t) { if helper.is_null() { @@ -1208,7 +1238,9 @@ pub unsafe fn dc_e2ee_thanks(mut helper: *mut dc_e2ee_helper_t) { (*helper).signatures = 0 as *mut dc_hash_t }; } + /* makes sure, the private key exists, needed only for exporting keys and the case no message was sent before */ +// TODO should return bool /rtn pub unsafe fn dc_ensure_secret_key_exists(mut context: &dc_context_t) -> libc::c_int { /* normally, the key is generated as soon as the first mail is send (this is to gain some extra-random-seed by the message content and the timespan between program start and message sending) */ @@ -1238,5 +1270,6 @@ pub unsafe fn dc_ensure_secret_key_exists(mut context: &dc_context_t) -> libc::c } dc_key_unref(public_key); free(self_addr as *mut libc::c_void); - return success; + + success } diff --git a/src/dc_hash.rs b/src/dc_hash.rs index 533a52d8e..a0e76d535 100644 --- a/src/dc_hash.rs +++ b/src/dc_hash.rs @@ -1,5 +1,3 @@ -use libc; - use crate::types::*; use crate::x::*; @@ -21,13 +19,16 @@ pub struct dc_hash_t { pub htsize: libc::c_int, pub ht: *mut _ht, } + #[derive(Copy, Clone)] #[repr(C)] pub struct _ht { pub count: libc::c_int, pub chain: *mut dc_hashelem_t, } + pub type dc_hashelem_t = _dc_hashelem; + /* Each element in the hash table is an instance of the following * structure. All elements are stored on a single doubly-linked list. * @@ -43,6 +44,7 @@ pub struct _dc_hashelem { pub pKey: *mut libc::c_void, pub nKey: libc::c_int, } + /* * There are 4 different modes of operation for a hash table: * @@ -102,6 +104,7 @@ pub unsafe fn dc_hash_init( (*pNew).htsize = 0i32; (*pNew).ht = 0 as *mut _ht; } + pub unsafe fn dc_hash_insert( mut pH: *mut dc_hash_t, mut pKey: *const libc::c_void, @@ -109,15 +112,15 @@ pub unsafe fn dc_hash_insert( mut data: *mut libc::c_void, ) -> *mut libc::c_void { /* Raw hash value of the key */ - let mut hraw: libc::c_int = 0; + let mut hraw: libc::c_int; /* the hash of the key modulo hash table size */ - let mut h: libc::c_int = 0; + let mut h: libc::c_int; /* Used to loop thru the element list */ - let mut elem: *mut dc_hashelem_t = 0 as *mut dc_hashelem_t; + let mut elem: *mut dc_hashelem_t; /* New element added to the pH */ - let mut new_elem: *mut dc_hashelem_t = 0 as *mut dc_hashelem_t; + let mut new_elem: *mut dc_hashelem_t; /* The hash function */ - let mut xHash: Option libc::c_int> = None; + let mut xHash: Option libc::c_int>; if 0 != pH.is_null() as libc::c_int as libc::c_long { __assert_rtn( (*::std::mem::transmute::<&[u8; 15], &[libc::c_char; 15]>(b"dc_hash_insert\x00")) @@ -215,8 +218,10 @@ pub unsafe fn dc_hash_insert( h = hraw & (*pH).htsize - 1i32; insertElement(pH, &mut *(*pH).ht.offset(h as isize), new_elem); (*new_elem).data = data; - return 0 as *mut libc::c_void; + + 0 as *mut libc::c_void } + /* Link an element into the hash table */ unsafe extern "C" fn insertElement( @@ -225,7 +230,7 @@ unsafe extern "C" fn insertElement( mut pNew: *mut dc_hashelem_t, ) { /* First element already in pEntry */ - let mut pHead: *mut dc_hashelem_t = 0 as *mut dc_hashelem_t; + let mut pHead: *mut dc_hashelem_t; pHead = (*pEntry).chain; if !pHead.is_null() { (*pNew).next = pHead; @@ -247,18 +252,19 @@ unsafe extern "C" fn insertElement( (*pEntry).count += 1; (*pEntry).chain = pNew; } + /* Resize the hash table so that it cantains "new_size" buckets. * "new_size" must be a power of 2. The hash table might fail * to resize if sjhashMalloc() fails. */ unsafe fn rehash(mut pH: *mut dc_hash_t, mut new_size: libc::c_int) { /* The new hash table */ - let mut new_ht: *mut _ht = 0 as *mut _ht; + let mut new_ht: *mut _ht; /* For looping over existing elements */ - let mut elem: *mut dc_hashelem_t = 0 as *mut dc_hashelem_t; - let mut next_elem: *mut dc_hashelem_t = 0 as *mut dc_hashelem_t; + let mut elem: *mut dc_hashelem_t; + let mut next_elem: *mut dc_hashelem_t; /* The hash function */ - let mut xHash: Option libc::c_int> = None; + let mut xHash: Option libc::c_int>; if 0 != !(new_size & new_size - 1i32 == 0i32) as libc::c_int as libc::c_long { __assert_rtn( (*::std::mem::transmute::<&[u8; 7], &[libc::c_char; 7]>(b"rehash\x00")).as_ptr(), @@ -290,6 +296,7 @@ unsafe fn rehash(mut pH: *mut dc_hash_t, mut new_size: libc::c_int) { elem = next_elem } } + /* Return a pointer to the appropriate hash function given the key class. * * About the syntax: @@ -309,8 +316,10 @@ unsafe fn hashFunction( 4 => return Some(binHash), _ => {} } - return None; + + None } + /* Hash and comparison functions when the mode is SJHASH_BINARY */ unsafe fn binHash(mut pKey: *const libc::c_void, mut nKey: libc::c_int) -> libc::c_int { @@ -326,13 +335,16 @@ unsafe fn binHash(mut pKey: *const libc::c_void, mut nKey: libc::c_int) -> libc: z = z.offset(1); h = h << 3i32 ^ h ^ *fresh1 as libc::c_int } - return h & 0x7fffffffi32; + + h & 0x7fffffffi32 } + /* Hash and comparison functions when the mode is SJHASH_STRING */ unsafe fn strHash(mut pKey: *const libc::c_void, mut nKey: libc::c_int) -> libc::c_int { - return sjhashNoCase(pKey as *const libc::c_char, nKey); + sjhashNoCase(pKey as *const libc::c_char, nKey) } + /* This function computes a hash on the name of a keyword. * Case is not significant. */ @@ -347,8 +359,10 @@ unsafe fn sjhashNoCase(mut z: *const libc::c_char, mut n: libc::c_int) -> libc:: h = h << 3i32 ^ h ^ sjhashUpperToLower[*fresh2 as libc::c_uchar as usize] as libc::c_int; n -= 1 } - return h & 0x7fffffffi32; + + h & 0x7fffffffi32 } + /* An array to map all upper-case characters into their corresponding * lower-case character. */ @@ -610,17 +624,20 @@ static mut sjhashUpperToLower: [libc::c_uchar; 256] = [ 254i32 as libc::c_uchar, 255i32 as libc::c_uchar, ]; + /* Hash and comparison functions when the mode is SJHASH_POINTER */ unsafe fn ptrHash(pKey: *const libc::c_void, _nKey: libc::c_int) -> libc::c_int { let mut x: uintptr_t = pKey as uintptr_t; - return (x ^ x << 8i32 ^ x >> 8i32) as libc::c_int; + (x ^ x << 8i32 ^ x >> 8i32) as libc::c_int } + /* Hash and comparison functions when the mode is SJHASH_INT */ unsafe fn intHash(_pKey: *const libc::c_void, mut nKey: libc::c_int) -> libc::c_int { - return nKey ^ nKey << 8i32 ^ nKey >> 8i32; + nKey ^ nKey << 8i32 ^ nKey >> 8i32 } + /* ** Based upon hash.c from sqlite which author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -636,6 +653,7 @@ unsafe fn sjhashMalloc(mut bytes: libc::c_int) -> *mut libc::c_void { } p } + /* Remove a single entry from the hash table given a pointer to that * element and a hash on the element's key. */ @@ -644,7 +662,7 @@ unsafe fn removeElementGivenHash( mut elem: *mut dc_hashelem_t, mut h: libc::c_int, ) { - let mut pEntry: *mut _ht = 0 as *mut _ht; + let mut pEntry: *mut _ht; if !(*elem).prev.is_null() { (*(*elem).prev).next = (*elem).next } else { @@ -667,6 +685,7 @@ unsafe fn removeElementGivenHash( free(elem as *mut libc::c_void); (*pH).count -= 1; } + /* This function (for internal use only) locates an element in an * hash table that matches the given key. The hash for this key has * already been computed and is passed as the 4th parameter. @@ -678,9 +697,9 @@ unsafe fn findElementGivenHash( mut h: libc::c_int, ) -> *mut dc_hashelem_t { /* Used to loop thru the element list */ - let mut elem: *mut dc_hashelem_t = 0 as *mut dc_hashelem_t; + let mut elem: *mut dc_hashelem_t; /* Number of elements left to test */ - let mut count: libc::c_int = 0; + let mut count: libc::c_int; /* comparison function */ let mut xCompare: Option< unsafe fn( @@ -689,7 +708,7 @@ unsafe fn findElementGivenHash( _: *const libc::c_void, _: libc::c_int, ) -> libc::c_int, - > = None; + >; if !(*pH).ht.is_null() { let mut pEntry: *mut _ht = &mut *(*pH).ht.offset(h as isize) as *mut _ht; elem = (*pEntry).chain; @@ -709,8 +728,10 @@ unsafe fn findElementGivenHash( elem = (*elem).next } } - return 0 as *mut dc_hashelem_t; + + 0 as *mut dc_hashelem_t } + /* Return a pointer to the appropriate hash function given the key class. */ unsafe fn compareFunction( @@ -730,8 +751,9 @@ unsafe fn compareFunction( 4 => return Some(binCompare), _ => {} } - return None; + None } + unsafe fn binCompare( mut pKey1: *const libc::c_void, mut n1: libc::c_int, @@ -741,8 +763,9 @@ unsafe fn binCompare( if n1 != n2 { return 1i32; } - return memcmp(pKey1, pKey2, n1 as libc::size_t); + memcmp(pKey1, pKey2, n1 as libc::size_t) } + unsafe fn strCompare( mut pKey1: *const libc::c_void, mut n1: libc::c_int, @@ -752,12 +775,13 @@ unsafe fn strCompare( if n1 != n2 { return 1i32; } - return sjhashStrNICmp( + sjhashStrNICmp( pKey1 as *const libc::c_char, pKey2 as *const libc::c_char, n1, - ); + ) } + /* Some systems have stricmp(). Others have strcasecmp(). Because * there is no consistency, we will define our own. */ @@ -766,8 +790,8 @@ unsafe fn sjhashStrNICmp( mut zRight: *const libc::c_char, mut N: libc::c_int, ) -> libc::c_int { - let mut a: *mut libc::c_uchar = 0 as *mut libc::c_uchar; - let mut b: *mut libc::c_uchar = 0 as *mut libc::c_uchar; + let mut a: *mut libc::c_uchar; + let mut b: *mut libc::c_uchar; a = zLeft as *mut libc::c_uchar; b = zRight as *mut libc::c_uchar; loop { @@ -790,6 +814,7 @@ unsafe fn sjhashStrNICmp( - sjhashUpperToLower[*b as usize] as libc::c_int }; } + unsafe fn ptrCompare( pKey1: *const libc::c_void, _n1: libc::c_int, @@ -820,11 +845,11 @@ pub unsafe fn dc_hash_find( mut nKey: libc::c_int, ) -> *mut libc::c_void { /* A hash on key */ - let mut h: libc::c_int = 0; + let mut h: libc::c_int; /* The element that matches key */ - let mut elem: *mut dc_hashelem_t = 0 as *mut dc_hashelem_t; + let mut elem: *mut dc_hashelem_t; /* The hash function */ - let mut xHash: Option libc::c_int> = None; + let mut xHash: Option libc::c_int>; if pH.is_null() || (*pH).ht.is_null() { return 0 as *mut libc::c_void; } @@ -857,9 +882,10 @@ pub unsafe fn dc_hash_find( 0 as *mut libc::c_void }; } + pub unsafe fn dc_hash_clear(mut pH: *mut dc_hash_t) { /* For looping over all elements of the table */ - let mut elem: *mut dc_hashelem_t = 0 as *mut dc_hashelem_t; + let mut elem: *mut dc_hashelem_t; if pH.is_null() { return; } diff --git a/src/dc_imap.rs b/src/dc_imap.rs index c225e22f7..9d45b0f6f 100644 --- a/src/dc_imap.rs +++ b/src/dc_imap.rs @@ -1,1848 +1,1773 @@ -use std::sync::{Arc, Condvar, Mutex}; +use std::ffi::{CStr, CString}; +use std::sync::{Arc, Condvar, Mutex, RwLock}; use std::time::{Duration, SystemTime}; -use libc; - -use crate::constants::Event; +use crate::constants::*; use crate::dc_context::dc_context_t; use crate::dc_log::*; use crate::dc_loginparam::*; -use crate::dc_oauth2::*; -use crate::dc_stock::*; -use crate::dc_strbuilder::*; -use crate::dc_tools::*; +use crate::dc_sqlite3::*; use crate::types::*; -use crate::x::*; + +pub const DC_IMAP_SEEN: usize = 0x0001; + +pub const DC_SUCCESS: usize = 3; +pub const DC_ALREADY_DONE: usize = 2; +pub const DC_RETRY_LATER: usize = 1; +pub const DC_FAILED: usize = 0; + +const PREFETCH_FLAGS: &'static str = "(UID ENVELOPE)"; +const BODY_FLAGS: &'static str = "(FLAGS BODY.PEEK[])"; +const FETCH_FLAGS: &'static str = "(FLAGS)"; #[repr(C)] pub struct dc_imap_t { - pub addr: *mut libc::c_char, - pub imap_server: *mut libc::c_char, - pub imap_port: i32, - pub imap_user: *mut libc::c_char, - pub imap_pw: *mut libc::c_char, - pub server_flags: i32, - pub connected: i32, - pub etpan: *mut mailimap, - pub idle_set_up: i32, - pub selected_folder: *mut libc::c_char, - pub selected_folder_needs_expunge: i32, - pub should_reconnect: i32, - pub can_idle: i32, - pub has_xlist: i32, - pub imap_delimiter: libc::c_char, - pub watch_folder: *mut libc::c_char, - pub watch: Arc<(Mutex, Condvar)>, - pub fetch_type_prefetch: *mut mailimap_fetch_type, - pub fetch_type_body: *mut mailimap_fetch_type, - pub fetch_type_flags: *mut mailimap_fetch_type, - pub get_config: dc_get_config_t, - pub set_config: dc_set_config_t, - pub precheck_imf: dc_precheck_imf_t, - pub receive_imf: dc_receive_imf_t, - pub log_connect_errors: i32, - pub skip_log_capabilities: i32, + config: Arc>, + watch: Arc<(Mutex, Condvar)>, + + get_config: dc_get_config_t, + set_config: dc_set_config_t, + precheck_imf: dc_precheck_imf_t, + receive_imf: dc_receive_imf_t, + + session: Arc>>, + // idle: Arc>>, } -pub unsafe fn dc_imap_new( +// rental! { +// pub mod rent { +// use crate::dc_imap::{Session, IdleHandle}; + +// #[rental_mut] +// pub struct RentSession { +// session: Box, +// idle: IdleHandle<'session>, +// } +// } +// } + +// use rent::*; + +#[derive(Debug)] +pub enum FolderMeaning { + Unknown, + SentObjects, + Other, +} + +pub enum Client { + Secure(imap::Client>), + Insecure(imap::Client), +} + +pub enum Session { + Secure(imap::Session>), + Insecure(imap::Session), +} + +pub enum IdleHandle<'a> { + Secure(imap::extensions::idle::Handle<'a, native_tls::TlsStream>), + Insecure(imap::extensions::idle::Handle<'a, std::net::TcpStream>), +} + +impl From>> for Client { + fn from(client: imap::Client>) -> Self { + Client::Secure(client) + } +} + +impl From> for Client { + fn from(client: imap::Client) -> Self { + Client::Insecure(client) + } +} + +impl From>> for Session { + fn from(session: imap::Session>) -> Self { + Session::Secure(session) + } +} + +impl From> for Session { + fn from(session: imap::Session) -> Self { + Session::Insecure(session) + } +} + +impl<'a> From>> + for IdleHandle<'a> +{ + fn from( + handle: imap::extensions::idle::Handle<'a, native_tls::TlsStream>, + ) -> Self { + IdleHandle::Secure(handle) + } +} + +impl<'a> From> for IdleHandle<'a> { + fn from(handle: imap::extensions::idle::Handle<'a, std::net::TcpStream>) -> Self { + IdleHandle::Insecure(handle) + } +} + +impl<'a> IdleHandle<'a> { + pub fn set_keepalive(&mut self, interval: Duration) { + match self { + IdleHandle::Secure(i) => i.set_keepalive(interval), + IdleHandle::Insecure(i) => i.set_keepalive(interval), + } + } + + pub fn wait_keepalive(self) -> imap::error::Result<()> { + match self { + IdleHandle::Secure(i) => i.wait_keepalive(), + IdleHandle::Insecure(i) => i.wait_keepalive(), + } + } +} + +impl Client { + pub fn login, P: AsRef>( + self, + username: U, + password: P, + ) -> Result { + match self { + Client::Secure(i) => i + .login(username, password) + .map(Into::into) + .map_err(|(e, c)| (e, c.into())), + Client::Insecure(i) => i + .login(username, password) + .map(Into::into) + .map_err(|(e, c)| (e, c.into())), + } + } +} + +impl Session { + pub fn capabilities( + &mut self, + ) -> imap::error::Result> { + match self { + Session::Secure(i) => i.capabilities(), + Session::Insecure(i) => i.capabilities(), + } + } + + pub fn list( + &mut self, + reference_name: Option<&str>, + mailbox_pattern: Option<&str>, + ) -> imap::error::Result>> { + match self { + Session::Secure(i) => i.list(reference_name, mailbox_pattern), + Session::Insecure(i) => i.list(reference_name, mailbox_pattern), + } + } + + pub fn create>(&mut self, mailbox_name: S) -> imap::error::Result<()> { + match self { + Session::Secure(i) => i.subscribe(mailbox_name), + Session::Insecure(i) => i.subscribe(mailbox_name), + } + } + + pub fn subscribe>(&mut self, mailbox: S) -> imap::error::Result<()> { + match self { + Session::Secure(i) => i.subscribe(mailbox), + Session::Insecure(i) => i.subscribe(mailbox), + } + } + + pub fn close(&mut self) -> imap::error::Result<()> { + match self { + Session::Secure(i) => i.close(), + Session::Insecure(i) => i.close(), + } + } + + pub fn select>( + &mut self, + mailbox_name: S, + ) -> imap::error::Result { + match self { + Session::Secure(i) => i.select(mailbox_name), + Session::Insecure(i) => i.select(mailbox_name), + } + } + + pub fn fetch( + &mut self, + sequence_set: S1, + query: S2, + ) -> imap::error::Result>> + where + S1: AsRef, + S2: AsRef, + { + match self { + Session::Secure(i) => i.fetch(sequence_set, query), + Session::Insecure(i) => i.fetch(sequence_set, query), + } + } + + pub fn uid_fetch( + &mut self, + uid_set: S1, + query: S2, + ) -> imap::error::Result>> + where + S1: AsRef, + S2: AsRef, + { + match self { + Session::Secure(i) => i.uid_fetch(uid_set, query), + Session::Insecure(i) => i.uid_fetch(uid_set, query), + } + } + + pub fn idle(&mut self) -> imap::error::Result { + match self { + Session::Secure(i) => i.idle().map(Into::into), + Session::Insecure(i) => i.idle().map(Into::into), + } + } + + pub fn uid_store( + &mut self, + uid_set: S1, + query: S2, + ) -> imap::error::Result>> + where + S1: AsRef, + S2: AsRef, + { + match self { + Session::Secure(i) => i.uid_store(uid_set, query), + Session::Insecure(i) => i.uid_store(uid_set, query), + } + } + + pub fn uid_mv, S2: AsRef>( + &mut self, + uid_set: S1, + mailbox_name: S2, + ) -> imap::error::Result<()> { + match self { + Session::Secure(i) => i.uid_mv(uid_set, mailbox_name), + Session::Insecure(i) => i.uid_mv(uid_set, mailbox_name), + } + } + + pub fn uid_copy, S2: AsRef>( + &mut self, + uid_set: S1, + mailbox_name: S2, + ) -> imap::error::Result<()> { + match self { + Session::Secure(i) => i.uid_copy(uid_set, mailbox_name), + Session::Insecure(i) => i.uid_copy(uid_set, mailbox_name), + } + } +} + +pub struct ImapConfig { + pub addr: Option, + pub imap_server: Option, + pub imap_port: Option, + pub imap_user: Option, + pub imap_pw: Option, + pub server_flags: Option, + pub selected_folder: Option, + pub selected_mailbox: Option, + pub selected_folder_needs_expunge: bool, + pub should_reconnect: bool, + pub can_idle: bool, + pub has_xlist: bool, + pub imap_delimiter: char, + pub watch_folder: Option, +} + +impl Default for ImapConfig { + fn default() -> Self { + let mut cfg = ImapConfig { + addr: None, + imap_server: None, + imap_port: None, + imap_user: None, + imap_pw: None, + server_flags: None, + selected_folder: None, + selected_mailbox: None, + selected_folder_needs_expunge: false, + should_reconnect: false, + can_idle: false, + has_xlist: false, + imap_delimiter: '.', + watch_folder: None, + }; + + cfg + } +} + +pub fn dc_imap_new( get_config: dc_get_config_t, set_config: dc_set_config_t, precheck_imf: dc_precheck_imf_t, receive_imf: dc_receive_imf_t, ) -> dc_imap_t { - let mut imap = dc_imap_t { - addr: std::ptr::null_mut(), - imap_server: std::ptr::null_mut(), - imap_port: 0, - imap_user: std::ptr::null_mut(), - imap_pw: std::ptr::null_mut(), - server_flags: 0, - connected: 0, - etpan: std::ptr::null_mut(), - idle_set_up: 0, - selected_folder: calloc(1, 1) as *mut libc::c_char, - selected_folder_needs_expunge: 0, - should_reconnect: 0, - can_idle: 0, - has_xlist: 0, - imap_delimiter: 0 as libc::c_char, - watch_folder: calloc(1, 1) as *mut libc::c_char, - watch: Arc::new((Mutex::new(false), Condvar::new())), - fetch_type_prefetch: mailimap_fetch_type_new_fetch_att_list_empty(), - fetch_type_body: mailimap_fetch_type_new_fetch_att_list_empty(), - fetch_type_flags: mailimap_fetch_type_new_fetch_att_list_empty(), - get_config, - set_config, - precheck_imf, - receive_imf, - log_connect_errors: 1, - skip_log_capabilities: 0, - }; - mailimap_fetch_type_new_fetch_att_list_add( - imap.fetch_type_prefetch, - mailimap_fetch_att_new_uid(), - ); - mailimap_fetch_type_new_fetch_att_list_add( - imap.fetch_type_prefetch, - mailimap_fetch_att_new_envelope(), - ); - - mailimap_fetch_type_new_fetch_att_list_add( - imap.fetch_type_body, - mailimap_fetch_att_new_flags(), - ); - mailimap_fetch_type_new_fetch_att_list_add( - imap.fetch_type_body, - mailimap_fetch_att_new_body_peek_section(mailimap_section_new( - 0 as *mut mailimap_section_spec, - )), - ); - mailimap_fetch_type_new_fetch_att_list_add( - imap.fetch_type_flags, - mailimap_fetch_att_new_flags(), - ); - - imap + dc_imap_t::new(get_config, set_config, precheck_imf, receive_imf) } -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() { - mailimap_fetch_type_free(imap.fetch_type_prefetch); - } - if !imap.fetch_type_body.is_null() { - mailimap_fetch_type_free(imap.fetch_type_body); - } - if !imap.fetch_type_flags.is_null() { - mailimap_fetch_type_free(imap.fetch_type_flags); - } -} - -pub unsafe fn dc_imap_disconnect(context: &dc_context_t, imap: &mut dc_imap_t) { - if 0 != imap.connected { - unsetup_handle(context, imap); - free_connect_param(imap); - imap.connected = 0 - }; -} - -/* ****************************************************************************** - * Connect/Disconnect - ******************************************************************************/ - -unsafe fn free_connect_param(imap: &mut dc_imap_t) { - free(imap.addr as *mut libc::c_void); - imap.addr = 0 as *mut libc::c_char; - free(imap.imap_server as *mut libc::c_void); - imap.imap_server = 0 as *mut libc::c_char; - free(imap.imap_user as *mut libc::c_void); - imap.imap_user = 0 as *mut libc::c_char; - free(imap.imap_pw as *mut libc::c_void); - imap.imap_pw = 0 as *mut libc::c_char; - *imap.watch_folder.offset(0isize) = 0 as libc::c_char; - *imap.selected_folder.offset(0isize) = 0 as libc::c_char; - imap.imap_port = 0; - imap.can_idle = 0; - imap.has_xlist = 0; -} - -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); - imap.idle_set_up = 0 +impl dc_imap_t { + pub fn new( + get_config: dc_get_config_t, + set_config: dc_set_config_t, + precheck_imf: dc_precheck_imf_t, + receive_imf: dc_receive_imf_t, + ) -> Self { + dc_imap_t { + session: Arc::new(Mutex::new(None)), + // idle: Arc::new(Mutex::new(None)), + config: Arc::new(RwLock::new(ImapConfig::default())), + watch: Arc::new((Mutex::new(false), Condvar::new())), + get_config, + set_config, + precheck_imf, + receive_imf, } - if !(*imap.etpan).imap_stream.is_null() { - mailstream_close((*imap.etpan).imap_stream); - (*imap.etpan).imap_stream = 0 as *mut mailstream - } - mailimap_free(imap.etpan); - imap.etpan = 0 as *mut mailimap; - dc_log_info( - context, - 0, - b"IMAP disconnected.\x00" as *const u8 as *const libc::c_char, - ); } - *imap.selected_folder.offset(0isize) = 0 as libc::c_char; -} -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() - || (*lp).mail_user.is_null() - || (*lp).mail_pw.is_null() - { - return 0; + pub fn is_connected(&self) -> bool { + self.session.lock().unwrap().is_some() } - if 0 != imap.connected { - success = 1 - } else { - imap.addr = dc_strdup((*lp).addr); - imap.imap_server = dc_strdup((*lp).mail_server); - imap.imap_port = (*lp).mail_port as libc::c_int; - 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(context, imap)) { - imap.can_idle = mailimap_has_idle(imap.etpan); - imap.has_xlist = mailimap_has_xlist(imap.etpan); - imap.can_idle = 0; - if 0 == imap.skip_log_capabilities - && !(*imap.etpan).imap_connection_info.is_null() - && !(*(*imap.etpan).imap_connection_info) - .imap_capability - .is_null() - { - imap.skip_log_capabilities = 1; - let mut capinfostr: dc_strbuilder_t = 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 capinfostr, 0); - let mut list: *mut clist = - (*(*(*imap.etpan).imap_connection_info).imap_capability).cap_list; - if !list.is_null() { - let mut cur: *mut clistiter = 0 as *mut clistiter; - cur = (*list).first; - while !cur.is_null() { - let mut cap: *mut mailimap_capability = (if !cur.is_null() { - (*cur).data - } else { - 0 as *mut libc::c_void - }) - as *mut mailimap_capability; - if !cap.is_null() - && (*cap).cap_type == MAILIMAP_CAPABILITY_NAME as libc::c_int - { - dc_strbuilder_cat( - &mut capinfostr, - b" \x00" as *const u8 as *const libc::c_char, - ); - dc_strbuilder_cat(&mut capinfostr, (*cap).cap_data.cap_name); - } - cur = if !cur.is_null() { - (*cur).next - } else { - 0 as *mut clistcell_s - } + + pub fn should_reconnect(&self) -> bool { + self.config.read().unwrap().should_reconnect + } + + pub fn connect(&self, context: &dc_context_t, lp: *const dc_loginparam_t) -> libc::c_int { + if lp.is_null() { + return 0; + } + let lp = unsafe { *lp }; + if lp.mail_server.is_null() || lp.mail_user.is_null() || lp.mail_pw.is_null() { + return 0; + } + + if self.is_connected() { + return 1; + } + + let addr = to_str(lp.addr); + let imap_server = to_str(lp.mail_server); + let imap_port = lp.mail_port as u16; + let imap_user = to_str(lp.mail_user); + let imap_pw = to_str(lp.mail_pw); + let server_flags = lp.server_flags as usize; + + let connection_res: imap::error::Result = + if (server_flags & (DC_LP_IMAP_SOCKET_STARTTLS | DC_LP_IMAP_SOCKET_PLAIN)) != 0 { + imap::connect_insecure((imap_server, imap_port)).and_then(|client| { + if (server_flags & DC_LP_IMAP_SOCKET_STARTTLS) != 0 { + let tls = native_tls::TlsConnector::builder().build().unwrap(); + client.secure(imap_server, &tls).map(Into::into) + } else { + Ok(client.into()) + } + }) + } else { + let tls = native_tls::TlsConnector::builder() + // FIXME: unfortunately this is needed to make things work on macos + testrun.org + .danger_accept_invalid_hostnames(true) + .build() + .unwrap(); + imap::connect((imap_server, imap_port), imap_server, &tls).map(Into::into) + }; + + match connection_res { + Ok(client) => { + println!("imap: loggingin with user {}", imap_user); + // TODO: handle oauth2 + match client.login(imap_user, imap_pw) { + Ok(mut session) => { + println!("imap: logged in"); + // TODO: error handling + let caps = session.capabilities().unwrap(); + let can_idle = caps.has("IDLE"); + let has_xlist = caps.has("XLIST"); + + let caps_list = caps.iter().fold(String::new(), |mut s, c| { + s += " "; + s += c; + s + }); + let caps_list_c = std::ffi::CString::new(caps_list).unwrap(); + + unsafe { + dc_log_info( + context, + 0, + b"IMAP-capabilities:%s\x00" as *const u8 as *const libc::c_char, + caps_list_c.as_ptr(), + ) + }; + + let mut config = self.config.write().unwrap(); + config.can_idle = can_idle; + config.has_xlist = has_xlist; + config.addr = Some(addr.into()); + config.imap_server = Some(imap_server.into()); + config.imap_port = Some(imap_port.into()); + config.imap_user = Some(imap_user.into()); + config.imap_pw = Some(imap_pw.into()); + config.server_flags = Some(server_flags); + + *self.session.lock().unwrap() = Some(session); + + 1 + } + Err((err, _)) => { + eprintln!("failed to login: {:?}", err); + + unsafe { + dc_log_event_seq( + context, + Event::ERROR_NETWORK, + &mut 0 as *mut i32, + b"Cannot login\x00" as *const u8 as *const libc::c_char, + ) + }; + + 0 } } + } + Err(err) => { + eprintln!("failed to connect: {:?}", err); + unsafe { + dc_log_event_seq( + context, + Event::ERROR_NETWORK, + &mut 0 as *mut i32, + b"Could not connect to IMAP-server %s:%i.\x00" as *const u8 + as *const libc::c_char, + imap_server, + imap_port as usize as libc::c_int, + ) + }; + + 0 + } + } + } + + pub fn disconnect(&self, context: &dc_context_t) { + println!("disconnecting"); + + let mut session = self.session.lock().unwrap().take(); + if session.is_some() { + match session.unwrap().close() { + Ok(_) => {} + Err(err) => { + eprintln!("failed to close connection: {:?}", err); + } + } + } + + let mut cfg = self.config.write().unwrap(); + + cfg.addr = None; + cfg.imap_server = None; + cfg.imap_user = None; + cfg.imap_pw = None; + cfg.imap_port = None; + + cfg.can_idle = false; + cfg.has_xlist = false; + + cfg.watch_folder = None; + cfg.selected_folder = None; + cfg.selected_mailbox = None; + + unsafe { + dc_log_info( + context, + 0, + b"IMAP disconnected.\x00" as *const u8 as *const libc::c_char, + ) + }; + } + + pub fn set_watch_folder(&self, watch_folder: *const libc::c_char) { + self.config.write().unwrap().watch_folder = Some(to_string(watch_folder)); + } + + pub fn fetch(&self, context: &dc_context_t) -> libc::c_int { + println!("dc_imap_fetch"); + let mut success = 0; + + let watch_folder = self.config.read().unwrap().watch_folder.to_owned(); + if self.is_connected() && watch_folder.is_some() { + let watch_folder = watch_folder.unwrap(); + loop { + let cnt = self.fetch_from_single_folder(context, &watch_folder); + if cnt == 0 { + break; + } + } + success = 1; + } + + println!("dc_imap_fetch done {}", success); + success + } + + fn select_folder>(&self, context: &dc_context_t, folder: Option) -> usize { + if !self.is_connected() { + let mut cfg = self.config.write().unwrap(); + cfg.selected_folder = None; + cfg.selected_folder_needs_expunge = false; + return 0; + } + + // if there is a new folder and the new folder is equal to the selected one, there's nothing to do. + // if there is _no_ new folder, we continue as we might want to expunge below. + if let Some(ref folder) = folder { + if let Some(ref selected_folder) = self.config.read().unwrap().selected_folder { + if folder.as_ref() == selected_folder { + return 1; + } + } + } + + // deselect existing folder, if needed (it's also done implicitly by SELECT, however, without EXPUNGE then) + if self.config.read().unwrap().selected_folder_needs_expunge { + if let Some(ref folder) = self.config.read().unwrap().selected_folder { + unsafe { + dc_log_info( + context, + 0, + b"Expunge messages in \"%s\".\x00" as *const u8 as *const libc::c_char, + CString::new(folder.to_owned()).unwrap().as_ptr(), + ) + }; + + // a CLOSE-SELECT is considerably faster than an EXPUNGE-SELECT, see https://tools.ietf.org/html/rfc3501#section-6.4.2 + if let Some(ref mut session) = *self.session.lock().unwrap() { + session.close().expect("failed to expunge"); + } else { + return 0; + } + } + } + + // select new folder + if let Some(folder) = folder { + if let Some(ref mut session) = *self.session.lock().unwrap() { + match session.select(folder) { + Ok(mailbox) => { + self.config.write().unwrap().selected_mailbox = Some(mailbox); + } + Err(err) => { + eprintln!("select error: {:?}", err); + unsafe { + dc_log_info( + context, + 0, + b"Cannot select folder.\x00" as *const u8 as *const libc::c_char, + ) + }; + self.config.write().unwrap().selected_folder = None; + } + } + } else { + return 0; + } + } + + 1 + } + + fn get_config_last_seen_uid>( + &self, + context: &dc_context_t, + folder: S, + ) -> (u32, u32) { + let key = format!("imap.mailbox.{}", folder.as_ref()); + let val1 = unsafe { + (self.get_config)( + context, + CString::new(key).unwrap().as_ptr(), + 0 as *const libc::c_char, + ) + }; + if val1.is_null() { + return (0, 0); + } + let entry = to_str(val1); + + // the entry has the format `imap.mailbox.=:` + let mut parts = entry.split(':'); + ( + parts.next().unwrap().parse().unwrap_or_else(|_| 0), + parts.next().unwrap().parse().unwrap_or_else(|_| 0), + ) + } + + fn fetch_from_single_folder>(&self, context: &dc_context_t, folder: S) -> usize { + println!("fetching from single folder"); + if !self.is_connected() { + unsafe { dc_log_info( context, 0, - b"IMAP-capabilities:%s\x00" as *const u8 as *const libc::c_char, - capinfostr.buf, - ); - free(capinfostr.buf as *mut libc::c_void); - } - imap.connected = 1; - success = 1 + b"Cannot fetch from \"%s\" - not connected.\x00" as *const u8 + as *const libc::c_char, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + ) + }; + + return 0; } - } - if success == 0 { - unsetup_handle(context, imap); - free_connect_param(imap); - } - success -} -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(context, imap); + if self.select_folder(context, Some(&folder)) == 0 { + unsafe { + dc_log_info( + context, + 0, + b"Cannot select folder \"%s\" for fetching.\x00" as *const u8 + as *const libc::c_char, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + ) + }; + return 0; } - if !imap.etpan.is_null() { - success = 1 - } else { - imap.etpan = mailimap_new(0 as size_t, None); - mailimap_set_timeout(imap.etpan, 10 as time_t); - if 0 != imap.server_flags & (0x100 | 0x400) { - r = mailimap_socket_connect( - imap.etpan, - imap.imap_server, - imap.imap_port as uint16_t, - ); - 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. (Error #%i)\x00" as *const u8 - as *const libc::c_char, - imap.imap_server, - imap.imap_port as libc::c_int, - r as 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(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" - as *const u8 as - *const libc::c_char, - imap.imap_server, - imap.imap_port as libc::c_int, - r as libc::c_int); - current_block = 15811161807000851472; - } else { - dc_log_info( - context, - 0, - b"IMAP-server %s:%i STARTTLS-connected.\x00" as *const u8 - as *const libc::c_char, - imap.imap_server, - imap.imap_port as libc::c_int, - ); - current_block = 14763689060501151050; - } - } else { - dc_log_info( - context, - 0, - b"IMAP-server %s:%i connected.\x00" as *const u8 as *const libc::c_char, - imap.imap_server, - imap.imap_port as libc::c_int, - ); - current_block = 14763689060501151050; - } - } else { - r = mailimap_ssl_connect(imap.etpan, imap.imap_server, imap.imap_port as uint16_t); - 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 SSL. (Error #%i)\x00" - as *const u8 as *const libc::c_char, - imap.imap_server, - imap.imap_port as libc::c_int, - r as libc::c_int, - ); - current_block = 15811161807000851472; - } else { - dc_log_info( - context, - 0, - b"IMAP-server %s:%i SSL-connected.\x00" as *const u8 as *const libc::c_char, - imap.imap_server, - imap.imap_port as libc::c_int, - ); - current_block = 14763689060501151050; - } - } - match current_block { - 15811161807000851472 => {} - _ => { - if 0 != imap.server_flags & 0x2 { - dc_log_info( - 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(context, imap.addr, imap.imap_pw, 0); - r = mailimap_oauth2_authenticate(imap.etpan, imap.imap_user, access_token); - if 0 != dc_imap_is_error(context, imap, r) { - free(access_token as *mut libc::c_void); - access_token = - dc_get_oauth2_access_token(context, imap.addr, imap.imap_pw, 0x1); - r = mailimap_oauth2_authenticate( - imap.etpan, - imap.imap_user, - access_token, - ) - } - free(access_token as *mut libc::c_void); - } else { - r = mailimap_login(imap.etpan, imap.imap_user, imap.imap_pw) - } - 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( - 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, - msg, - ); - free(msg as *mut libc::c_void); - } else { - dc_log_event( - context, - Event::IMAP_CONNECTED, - 0, - b"IMAP-login as %s ok.\x00" as *const u8 as *const libc::c_char, - imap.imap_user, - ); - success = 1 - } - } - } - } - } - if success == 0 { - 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, -) -> *mut libc::c_char { - let mut stock: *mut libc::c_char = 0 as *mut libc::c_char; - let mut msg: dc_strbuilder_t = 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 msg, 1000); - match code { - 28 => { - stock = dc_stock_str_repl_string(context, 60, imap.imap_user); - dc_strbuilder_cat(&mut msg, stock); - } - _ => { - dc_strbuilder_catf( - &mut msg as *mut dc_strbuilder_t, - b"%s, IMAP-error #%i\x00" as *const u8 as *const libc::c_char, - what_failed, - code, - ); - } - } - free(stock as *mut libc::c_void); - 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(context, 61, imap.imap_server, (*imap.etpan).imap_response); - dc_strbuilder_cat(&mut msg, stock); - } - free(stock as *mut libc::c_void); - stock = 0 as *mut libc::c_char; - msg.buf -} + println!("selected folder {}", folder.as_ref()); -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 - { - return 0; - } - if code == MAILIMAP_ERROR_STREAM as libc::c_int || code == MAILIMAP_ERROR_PARSE as libc::c_int { - dc_log_info( - context, - 0, - b"IMAP stream lost; we\'ll reconnect soon.\x00" as *const u8 as *const libc::c_char, - ); - imap.should_reconnect = 1 - } - 1 -} + let (mut uid_validity, mut last_seen_uid) = self.get_config_last_seen_uid(context, &folder); -pub unsafe extern "C" fn dc_imap_set_watch_folder( - imap: &mut dc_imap_t, - watch_folder: *const libc::c_char, -) { - if watch_folder.is_null() { - return; - } - free(imap.watch_folder as *mut libc::c_void); - imap.watch_folder = dc_strdup(watch_folder); -} + println!("got validity: {} - {}", uid_validity, last_seen_uid); -pub unsafe fn dc_imap_is_connected(imap: &dc_imap_t) -> libc::c_int { - (0 != imap.connected) as libc::c_int -} + let config = self.config.read().unwrap(); + let mailbox = config.selected_mailbox.as_ref().expect("just selected"); -pub unsafe fn dc_imap_fetch(context: &dc_context_t, imap: &mut dc_imap_t) -> libc::c_int { - let mut success = 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 { - let mut current_block: u64; - let mut r: libc::c_int = 0; - let mut uidvalidity: uint32_t = 0 as uint32_t; - let mut lastseenuid: uint32_t = 0 as uint32_t; - let mut new_lastseenuid: uint32_t = 0 as uint32_t; - let mut fetch_result: *mut clist = 0 as *mut clist; - let mut read_cnt: size_t = 0 as size_t; - let mut read_errors: size_t = 0 as size_t; - let mut cur: *mut clistiter = 0 as *mut clistiter; - let mut set: *mut mailimap_set = 0 as *mut mailimap_set; - if imap.etpan.is_null() { - dc_log_info( - context, - 0, - b"Cannot fetch from \"%s\" - not connected.\x00" as *const u8 as *const libc::c_char, - folder, - ); - } else if select_folder(context, imap, folder) == 0 { - dc_log_warning( - context, - 0, - b"Cannot select folder %s for fetching.\x00" as *const u8 as *const libc::c_char, - folder, - ); - } else { - 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 { + if mailbox.uid_validity.is_none() { + unsafe { dc_log_error( context, 0, b"Cannot get UIDVALIDITY for folder \"%s\".\x00" as *const u8 as *const libc::c_char, - folder, - ); - current_block = 17288151659885296046; - } else { - 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( - 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, - 0 as uint32_t, - ); - } - current_block = 17288151659885296046; - } else { - set = mailimap_set_new_single( - (*(*imap.etpan).imap_selection_info).sel_exists, - ); - current_block = 11057878835866523405; - } - } else { + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + ) + }; + return 0; + } + + if mailbox.uid_validity.unwrap() != uid_validity { + // first time this folder is selected or UIDVALIDITY has changed, init lastseenuid and save it to config + + if mailbox.exists == 0 { + unsafe { dc_log_info( context, 0, - b"EXISTS is missing for folder \"%s\", using fallback.\x00" as *const u8 - as *const libc::c_char, - folder, - ); - set = mailimap_set_new_single(0 as uint32_t); - current_block = 11057878835866523405; - } - match current_block { - 17288151659885296046 => {} - _ => { - r = mailimap_fetch( - imap.etpan, - set, - imap.fetch_type_prefetch, - &mut fetch_result, - ); - if !set.is_null() { - mailimap_set_free(set); - set = 0 as *mut mailimap_set - } - if 0 != dc_imap_is_error(context, imap, r) || fetch_result.is_null() { - fetch_result = 0 as *mut clist; + b"Folder \"%s\" is empty.\x00" as *const u8 as *const libc::c_char, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + ) + }; + + // set lastseenuid=0 for empty folders. + // id we do not do this here, we'll miss the first message + // as we will get in here again and fetch from lastseenuid+1 then + + self.set_config_last_seen_uid(context, &folder, mailbox.uid_validity.unwrap(), 0); + return 0; + } + + let list = if let Some(ref mut session) = *self.session.lock().unwrap() { + // `FETCH (UID)` + let set = format!("{}", mailbox.exists); + match session.fetch(set, PREFETCH_FLAGS) { + Ok(list) => list, + Err(err) => { + eprintln!("fetch error: {:?}", err); + unsafe { dc_log_info( context, 0, b"No result returned for folder \"%s\".\x00" as *const u8 as *const libc::c_char, - folder, - ); - /* this might happen if the mailbox is empty an EXISTS does not work */ - current_block = 17288151659885296046; - } else { - cur = (*fetch_result).first; - if cur.is_null() { - dc_log_info( - context, - 0, - b"Empty result returned for folder \"%s\".\x00" as *const u8 - as *const libc::c_char, - folder, - ); - /* this might happen if the mailbox is empty an EXISTS does not work */ - current_block = 17288151659885296046; - } else { - let mut msg_att: *mut mailimap_msg_att = (if !cur.is_null() { - (*cur).data - } else { - 0 as *mut libc::c_void - }) - as *mut mailimap_msg_att; - lastseenuid = peek_uid(msg_att); - if !fetch_result.is_null() { - mailimap_fetch_list_free(fetch_result); - fetch_result = 0 as *mut clist - } - if lastseenuid <= 0 as libc::c_uint { - dc_log_error( - context, - 0, - b"Cannot get largest UID for folder \"%s\"\x00" as *const u8 - as *const libc::c_char, - folder, - ); - current_block = 17288151659885296046; - } else { - if uidvalidity > 0 as libc::c_uint - && lastseenuid > 1 as libc::c_uint - { - lastseenuid = (lastseenuid as libc::c_uint) - .wrapping_sub(1 as libc::c_uint) - as uint32_t - as uint32_t - } - uidvalidity = - (*(*imap.etpan).imap_selection_info).sel_uidvalidity; - set_config_lastseenuid( - context, - imap, - folder, - uidvalidity, - lastseenuid, - ); - dc_log_info( - context, - 0, - b"lastseenuid initialized to %i for %s@%i\x00" as *const u8 - as *const libc::c_char, - lastseenuid as libc::c_int, - folder, - uidvalidity as libc::c_int, - ); - current_block = 2516253395664191498; - } - } - } + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + ) + }; + return 0; } } + } else { + return 0; + }; + println!("fetched {} messages", list.len()); + + last_seen_uid = list[0].uid.unwrap_or_else(|| 0); + + // if the UIDVALIDITY has _changed_, decrease lastseenuid by one to avoid gaps (well add 1 below + if uid_validity > 0 && last_seen_uid > 1 { + last_seen_uid -= 1; + } + + uid_validity = mailbox.uid_validity.unwrap(); + self.set_config_last_seen_uid(context, &folder, uid_validity, last_seen_uid); + unsafe { + dc_log_info( + context, + 0, + b"lastseenuid initialized to %i for %s@%i\x00" as *const u8 + as *const libc::c_char, + last_seen_uid as libc::c_int, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + uid_validity as libc::c_int, + ); + } + } + + let mut read_cnt = 0; + let mut read_errors = 0; + let mut new_last_seen_uid = 0; + + let list = if let Some(ref mut session) = *self.session.lock().unwrap() { + // fetch messages with larger UID than the last one seen + // (`UID FETCH lastseenuid+1:*)`, see RFC 4549 + let set = format!("{}:*", last_seen_uid + 1); + match session.uid_fetch(set, PREFETCH_FLAGS) { + Ok(list) => list, + Err(err) => { + eprintln!("fetch err: {:?}", err); + return 0; + } } } else { - current_block = 2516253395664191498; - } - match current_block { - 17288151659885296046 => {} - _ => { - set = mailimap_set_new_interval( - lastseenuid.wrapping_add(1 as libc::c_uint), - 0 as uint32_t, - ); - r = mailimap_uid_fetch( - imap.etpan, - set, - imap.fetch_type_prefetch, - &mut fetch_result, - ); - if !set.is_null() { - mailimap_set_free(set); - set = 0 as *mut mailimap_set - } - 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 { + return 0; + }; + + println!("fetched {} messages", list.len()); + // go through all mails in folder (this is typically _fast_ as we already have the whole list) + + for msg in &list { + let cur_uid = msg.uid.unwrap_or_else(|| 0); + if cur_uid > last_seen_uid { + read_cnt += 1; + + let message_id = msg + .envelope() + .expect("missing envelope") + .message_id + .expect("missing message id"); + + let message_id_c = CString::new(message_id).unwrap(); + let folder_c = CString::new(folder.as_ref().to_owned()).unwrap(); + if 0 == unsafe { + (self.precheck_imf)(context, message_id_c.as_ptr(), folder_c.as_ptr(), cur_uid) + } { + // check passed, go fetch the rest + if self.fetch_single_msg(context, &folder, cur_uid) == 0 { + unsafe { + 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, + message_id_c.as_ptr(), + folder_c.as_ptr(), + ) + }; + read_errors += 1; + } + } else { + // check failed + unsafe { dc_log_info( context, 0, - b"Folder \"%s\" is empty\x00" as *const u8 as *const libc::c_char, - folder, - ); - } else { - /* the folder is simply empty, this is no error */ + b"Skipping message %s from \"%s\" by precheck.\x00" as *const u8 + as *const libc::c_char, + message_id_c.as_ptr(), + folder_c.as_ptr(), + ) + }; + } + if cur_uid > new_last_seen_uid { + new_last_seen_uid = cur_uid + } + } + } + + if 0 == read_errors && new_last_seen_uid > 0 { + // TODO: it might be better to increase the lastseenuid also on partial errors. + // however, this requires to sort the list before going through it above. + self.set_config_last_seen_uid(context, &folder, uid_validity, new_last_seen_uid); + } + + if read_errors > 0 { + unsafe { + dc_log_warning( + context, + 0i32, + b"%i mails read from \"%s\" with %i errors.\x00" as *const u8 + as *const libc::c_char, + read_cnt as libc::c_int, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + read_errors as libc::c_int, + ) + }; + } else { + unsafe { + dc_log_info( + context, + 0i32, + b"%i mails read from \"%s\".\x00" as *const u8 as *const libc::c_char, + read_cnt as libc::c_int, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + ) + }; + } + + read_cnt + } + + fn set_config_last_seen_uid>( + &self, + context: &dc_context_t, + folder: S, + uidvalidity: u32, + lastseenuid: u32, + ) { + let key = format!("imap.mailbox.{}", folder.as_ref()); + let val = format!("{}:{}", uidvalidity, lastseenuid); + + unsafe { + (self.set_config)( + context, + CString::new(key).unwrap().as_ptr(), + CString::new(val).unwrap().as_ptr(), + ) + }; + } + + fn fetch_single_msg>( + &self, + context: &dc_context_t, + folder: S, + server_uid: u32, + ) -> usize { + // the function returns: + // 0 the caller should try over again later + // or 1 if the messages should be treated as received, the caller should not try to read the message again (even if no database entries are returned) + if !self.is_connected() { + return 0; + } + + let mut retry_later = false; + + let msgs = if let Some(ref mut session) = *self.session.lock().unwrap() { + let set = format!("{}", server_uid); + match session.uid_fetch(set, BODY_FLAGS) { + Ok(msgs) => msgs, + Err(err) => { + eprintln!("error fetch single: {:?}", err); + unsafe { dc_log_warning( context, 0, - b"Cannot fetch message list from folder \"%s\".\x00" as *const u8 - as *const libc::c_char, - folder, - ); - } - } else { - cur = (*fetch_result).first; - while !cur.is_null() { - let mut msg_att_0: *mut mailimap_msg_att = (if !cur.is_null() { - (*cur).data - } else { - 0 as *mut libc::c_void - }) - as *mut mailimap_msg_att; - let mut cur_uid: uint32_t = peek_uid(msg_att_0); - if cur_uid > lastseenuid { - let mut rfc724_mid: *mut libc::c_char = - 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")( - context, rfc724_mid, folder, cur_uid, - ) { - 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, - rfc724_mid, folder); - read_errors = read_errors.wrapping_add(1) - } - } else { - dc_log_info( - context, - 0, - b"Skipping message %s from \"%s\" by precheck.\x00" as *const u8 - as *const libc::c_char, - rfc724_mid, - folder, - ); - } - if cur_uid > new_lastseenuid { - new_lastseenuid = cur_uid - } - free(rfc724_mid as *mut libc::c_void); - } - cur = if !cur.is_null() { - (*cur).next - } else { - 0 as *mut clistcell_s - } - } - if 0 == read_errors && new_lastseenuid > 0 as libc::c_uint { - set_config_lastseenuid(context, imap, folder, uidvalidity, new_lastseenuid); + b"Error on fetching message #%i from folder \"%s\"; retry=%i.\x00" + as *const u8 as *const libc::c_char, + server_uid as libc::c_int, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + self.should_reconnect() as libc::c_int, + ) + }; + + if self.should_reconnect() { + retry_later = true; } + + return if retry_later { 0 } else { 1 }; } } - } - } - - /* done */ - if 0 != read_errors { - dc_log_warning( - 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, - folder, - read_errors as libc::c_int, - ); - } else { - dc_log_info( - context, - 0i32, - b"%i mails read from \"%s\".\x00" as *const u8 as *const libc::c_char, - read_cnt as libc::c_int, - folder, - ); - } - if !fetch_result.is_null() { - mailimap_fetch_list_free(fetch_result); - fetch_result = 0 as *mut clist - } - - read_cnt as libc::c_int -} - -unsafe fn set_config_lastseenuid( - context: &dc_context_t, - imap: &mut dc_imap_t, - folder: *const libc::c_char, - uidvalidity: uint32_t, - lastseenuid: uint32_t, -) { - let mut key: *mut libc::c_char = dc_mprintf( - b"imap.mailbox.%s\x00" as *const u8 as *const libc::c_char, - folder, - ); - let mut val: *mut libc::c_char = dc_mprintf( - b"%lu:%lu\x00" as *const u8 as *const libc::c_char, - uidvalidity, - lastseenuid, - ); - 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); -} - -unsafe fn peek_rfc724_mid(msg_att: *mut mailimap_msg_att) -> *const libc::c_char { - if msg_att.is_null() { - return 0 as *const libc::c_char; - } - /* search the UID in a list of attributes returned by a FETCH command */ - let mut iter1: *mut clistiter = 0 as *mut clistiter; - iter1 = (*(*msg_att).att_list).first; - while !iter1.is_null() { - let mut item: *mut mailimap_msg_att_item = (if !iter1.is_null() { - (*iter1).data } else { - 0 as *mut libc::c_void - }) as *mut mailimap_msg_att_item; - if !item.is_null() { - if (*item).att_type == MAILIMAP_MSG_ATT_ITEM_STATIC as libc::c_int { - if (*(*item).att_data.att_static).att_type - == MAILIMAP_MSG_ATT_ENVELOPE as libc::c_int - { - let mut env: *mut mailimap_envelope = - (*(*item).att_data.att_static).att_data.att_env; - if !env.is_null() && !(*env).env_message_id.is_null() { - return (*env).env_message_id; - } - } - } - } - iter1 = if !iter1.is_null() { - (*iter1).next - } else { - 0 as *mut clistcell_s - } - } - return 0 as *const libc::c_char; -} + return if retry_later { 0 } else { 1 }; + }; -unsafe fn unquote_rfc724_mid(in_0: *const libc::c_char) -> *mut libc::c_char { - /* remove < and > from the given message id */ - let mut out: *mut libc::c_char = dc_strdup(in_0); - let mut out_len: libc::c_int = strlen(out) as libc::c_int; - if out_len > 2 { - if *out.offset(0isize) as libc::c_int == '<' as i32 { - *out.offset(0isize) = ' ' as i32 as libc::c_char - } - if *out.offset((out_len - 1) as isize) as libc::c_int == '>' as i32 { - *out.offset((out_len - 1) as isize) = ' ' as i32 as libc::c_char - } - dc_trim(out); - } - out -} - -/* ****************************************************************************** - * Fetch Messages - ******************************************************************************/ - -unsafe fn peek_uid(msg_att: *mut mailimap_msg_att) -> uint32_t { - /* search the UID in a list of attributes returned by a FETCH command */ - let mut iter1: *mut clistiter = 0 as *mut clistiter; - iter1 = (*(*msg_att).att_list).first; - while !iter1.is_null() { - let mut item: *mut mailimap_msg_att_item = (if !iter1.is_null() { - (*iter1).data - } else { - 0 as *mut libc::c_void - }) as *mut mailimap_msg_att_item; - if !item.is_null() { - if (*item).att_type == MAILIMAP_MSG_ATT_ITEM_STATIC as libc::c_int { - if (*(*item).att_data.att_static).att_type == MAILIMAP_MSG_ATT_UID as libc::c_int { - return (*(*item).att_data.att_static).att_data.att_uid; - } - } - } - iter1 = if !iter1.is_null() { - (*iter1).next - } else { - 0 as *mut clistcell_s - } - } - 0 as 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, -) -> libc::c_int { - let mut msg_att: *mut mailimap_msg_att = 0 as *mut mailimap_msg_att; - /* the function returns: - 0 the caller should try over again later - or 1 if the messages should be treated as received, the caller should not try to read the message again (even if no database entries are returned) */ - let mut msg_content: *mut libc::c_char = 0 as *mut libc::c_char; - let mut msg_bytes: size_t = 0 as size_t; - let mut r: libc::c_int = 0; - let mut retry_later: libc::c_int = 0; - let mut deleted: libc::c_int = 0; - let mut flags: uint32_t = 0 as uint32_t; - let mut fetch_result: *mut clist = 0 as *mut clist; - let mut cur: *mut clistiter = 0 as *mut clistiter; - if !imap.etpan.is_null() { - let mut set: *mut mailimap_set = mailimap_set_new_single(server_uid); - r = mailimap_uid_fetch(imap.etpan, set, imap.fetch_type_body, &mut fetch_result); - if !set.is_null() { - mailimap_set_free(set); - set = 0 as *mut mailimap_set - } - if 0 != dc_imap_is_error(context, imap, r) || fetch_result.is_null() { - fetch_result = 0 as *mut clist; - dc_log_warning( - context, - 0, - b"Error #%i on fetching message #%i from folder \"%s\"; retry=%i.\x00" as *const u8 - as *const libc::c_char, - r as libc::c_int, - server_uid as libc::c_int, - folder, - imap.should_reconnect as libc::c_int, - ); - if 0 != imap.should_reconnect { - retry_later = 1 - } - } else { - /* this is an error that should be recovered; the caller should try over later to fetch the message again (if there is no such message, we simply get an empty result) */ - cur = (*fetch_result).first; - if cur.is_null() { + if msgs.is_empty() { + unsafe { dc_log_warning( context, 0, b"Message #%i does not exist in folder \"%s\".\x00" as *const u8 as *const libc::c_char, server_uid as libc::c_int, - folder, - ); - } else { - /* server response is fine, however, there is no such message, do not try to fetch the message again */ - msg_att = (if !cur.is_null() { - (*cur).data - } else { - 0 as *mut libc::c_void - }) as *mut mailimap_msg_att; - peek_body( - msg_att, - &mut msg_content, - &mut msg_bytes, - &mut flags, - &mut deleted, - ); - 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")( + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + ) + }; + } else { + let msg = &msgs[0]; + + let is_deleted = msg + .flags() + .iter() + .find(|flag| match flag { + imap::types::Flag::Deleted => true, + _ => false, + }) + .is_some(); + let is_seen = msg + .flags() + .iter() + .find(|flag| match flag { + imap::types::Flag::Seen => true, + _ => false, + }) + .is_some(); + + let flags = if is_seen { DC_IMAP_SEEN } else { 0 }; + + if !is_deleted && msg.body().is_some() { + unsafe { + let folder_c = CString::new(folder.as_ref().to_owned()).unwrap(); + (self.receive_imf)( context, - msg_content, - msg_bytes, - folder, + msg.body().unwrap().as_ptr() as *const libc::c_char, + msg.body().unwrap().len(), + folder_c.as_ptr(), server_uid, - flags, + flags as u32, ); } } } - } - if !fetch_result.is_null() { - mailimap_fetch_list_free(fetch_result); - fetch_result = 0 as *mut clist - } - if 0 != retry_later { - 0 - } else { - 1 - } -} - -unsafe fn peek_body( - msg_att: *mut mailimap_msg_att, - p_msg: *mut *mut libc::c_char, - p_msg_bytes: *mut size_t, - flags: *mut uint32_t, - deleted: *mut libc::c_int, -) { - if msg_att.is_null() { - return; - } - /* search body & Co. in a list of attributes returned by a FETCH command */ - let mut iter1: *mut clistiter = 0 as *mut clistiter; - let mut iter2: *mut clistiter = 0 as *mut clistiter; - iter1 = (*(*msg_att).att_list).first; - while !iter1.is_null() { - let mut item: *mut mailimap_msg_att_item = (if !iter1.is_null() { - (*iter1).data + if retry_later { + 0 } else { - 0 as *mut libc::c_void - }) as *mut mailimap_msg_att_item; - if !item.is_null() { - if (*item).att_type == MAILIMAP_MSG_ATT_ITEM_DYNAMIC as libc::c_int { - if !(*(*item).att_data.att_dyn).att_list.is_null() { - iter2 = (*(*(*item).att_data.att_dyn).att_list).first; - while !iter2.is_null() { - let mut flag_fetch: *mut mailimap_flag_fetch = (if !iter2.is_null() { - (*iter2).data - } else { - 0 as *mut libc::c_void - }) - as *mut mailimap_flag_fetch; - if !flag_fetch.is_null() - && (*flag_fetch).fl_type == MAILIMAP_FLAG_FETCH_OTHER as libc::c_int - { - let mut flag: *mut mailimap_flag = (*flag_fetch).fl_flag; - if !flag.is_null() { - if (*flag).fl_type == MAILIMAP_FLAG_SEEN as libc::c_int { - *flags = (*flags as libc::c_long | 0x1) as uint32_t - } else if (*flag).fl_type == MAILIMAP_FLAG_DELETED as libc::c_int { - *deleted = 1 - } - } - } - iter2 = if !iter2.is_null() { - (*iter2).next - } else { - 0 as *mut clistcell_s - } - } - } - } else if (*item).att_type == MAILIMAP_MSG_ATT_ITEM_STATIC as libc::c_int { - if (*(*item).att_data.att_static).att_type - == MAILIMAP_MSG_ATT_BODY_SECTION as libc::c_int - { - *p_msg = - (*(*(*item).att_data.att_static).att_data.att_body_section).sec_body_part; - *p_msg_bytes = - (*(*(*item).att_data.att_static).att_data.att_body_section).sec_length; - } - } - } - iter1 = if !iter1.is_null() { - (*iter1).next - } else { - 0 as *mut clistcell_s - }; - } -} - -unsafe fn get_config_lastseenuid( - context: &dc_context_t, - imap: &mut dc_imap_t, - folder: *const libc::c_char, - uidvalidity: *mut uint32_t, - lastseenuid: *mut uint32_t, -) { - *uidvalidity = 0 as uint32_t; - *lastseenuid = 0 as uint32_t; - let mut key: *mut libc::c_char = dc_mprintf( - b"imap.mailbox.%s\x00" as *const u8 as *const libc::c_char, - folder, - ); - let mut val1: *mut 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() { - val2 = strchr(val1, ':' as i32); - if !val2.is_null() { - *val2 = 0 as libc::c_char; - val2 = val2.offset(1isize); - val3 = strchr(val2, ':' as i32); - if !val3.is_null() { - *val3 = 0 as libc::c_char - } - *uidvalidity = atol(val1) as uint32_t; - *lastseenuid = atol(val2) as uint32_t + 1 } } - free(val1 as *mut libc::c_void); - free(key as *mut libc::c_void); -} -/* ****************************************************************************** - * Handle folders - ******************************************************************************/ - -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; - return 0; - } - if !folder.is_null() - && 0 != *folder.offset(0isize) as libc::c_int - && strcmp(imap.selected_folder, folder) == 0 - { - return 1; - } - if 0 != imap.selected_folder_needs_expunge { - if 0 != *imap.selected_folder.offset(0isize) { - dc_log_info( - context, - 0, - b"Expunge messages in \"%s\".\x00" as *const u8 as *const libc::c_char, - imap.selected_folder, - ); - mailimap_close(imap.etpan); + pub fn idle(&self, context: &dc_context_t) { + println!( + "trying idle: can_idle: {}", + self.config.read().unwrap().can_idle + ); + if !self.config.read().unwrap().can_idle { + return self.fake_idle(context); } - imap.selected_folder_needs_expunge = 0 - } - if !folder.is_null() { - let mut r: libc::c_int = mailimap_select(imap.etpan, folder); - if 0 != dc_imap_is_error(context, imap, r) || (*imap.etpan).imap_selection_info.is_null() { - dc_log_info( - context, - 0, - b"Cannot select folder; code=%i, imap_response=%s\x00" as *const u8 - as *const libc::c_char, - r, - if !(*imap.etpan).imap_response.is_null() { - (*imap.etpan).imap_response - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - ); - *imap.selected_folder.offset(0isize) = 0 as libc::c_char; - return 0; - } - } - free(imap.selected_folder as *mut libc::c_void); - imap.selected_folder = dc_strdup(folder); - 1 -} -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(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(context, imap, r) { + // TODO: reconnect in all methods that need it + if !self.is_connected() { + println!("can't idle, disconnected"); + return; + } + + let watch_folder = self.config.read().unwrap().watch_folder.clone(); + if self.select_folder(context, watch_folder.as_ref()) == 0 { + unsafe { dc_log_warning( context, 0, - b"IMAP-IDLE: Cannot setup.\x00" as *const u8 as *const libc::c_char, - ); - fake_idle(context, imap); - current_block = 14832935472441733737; - } else { - imap.idle_set_up = 1; - current_block = 17965632435239708295; - } - } else { - current_block = 17965632435239708295; + b"IMAP-IDLE not setup.\x00" as *const u8 as *const libc::c_char, + ) + }; + + return self.fake_idle(context); } - match current_block { - 14832935472441733737 => {} - _ => { - if 0 == imap.idle_set_up || 0 == select_folder(context, imap, imap.watch_folder) { + + // let mut session = self.session.lock().unwrap().take().unwrap(); + + // match RentSession::try_new(Box::new(session), |session| session.idle()) { + // Ok(idle) => { + // *self.idle.lock().unwrap() = Some(idle); + // } + // Err(err) => { + // eprintln!("imap idle error: {:?}", err.0); + // unsafe { + // dc_log_warning( + // context, + // 0, + // b"IMAP-IDLE: Cannot start.\x00" as *const u8 as *const libc::c_char, + // ); + // } + + // // put session back + // *self.session.lock().unwrap() = Some(*err.1); + + // return self.fake_idle(context); + // } + // } + + println!("setting up idle"); + let mut session = self.session.lock().unwrap().take().unwrap(); + let mut idle = match session.idle() { + Ok(idle) => idle, + Err(err) => { + eprintln!("imap idle error: {:?}", err); + unsafe { dc_log_warning( context, 0, - b"IMAP-IDLE not setup.\x00" as *const u8 as *const libc::c_char, + b"IMAP-IDLE: Cannot start.\x00" as *const u8 as *const libc::c_char, ); - fake_idle(context, imap); + } + + return self.fake_idle(context); + } + }; + + // most servers do not allow more than ~28 minutes; stay clearly below that. + // a good value that is also used by other MUAs is 23 minutes. + // if needed, the ui can call dc_imap_interrupt_idle() to trigger a reconnect. + idle.set_keepalive(Duration::from_secs(23 * 60)); + + println!("imap idle waiting"); + // TODO: proper logging of different states + // TODO: reconnect if we timed out + match idle.wait_keepalive() { + Ok(_) => { + println!("imap done"); + } + Err(err) => { + eprintln!("idle error: {:?}", err); + } + } + + // put session back + *self.session.lock().unwrap() = Some(session); + } + + fn fake_idle(&self, context: &dc_context_t) { + println!("fake idle"); + + // 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 = SystemTime::now(); + + unsafe { + dc_log_info( + context, + 0, + b"IMAP-fake-IDLEing...\x00" as *const u8 as *const libc::c_char, + ) + }; + let mut do_fake_idle = true; + while do_fake_idle { + let seconds_to_wait = + if fake_idle_start_time.elapsed().unwrap() < Duration::new(3 * 60, 0) { + Duration::new(5, 0) } else { - r = mailimap_idle(imap.etpan); - if 0 != dc_imap_is_error(context, imap, r) { - dc_log_warning( - context, - 0, - b"IMAP-IDLE: Cannot start.\x00" as *const u8 as *const libc::c_char, - ); - fake_idle(context, imap); - } else { - r = mailstream_wait_idle((*imap.etpan).imap_stream, 23 * 60); - r2 = mailimap_idle_done(imap.etpan); - if r == MAILSTREAM_IDLE_ERROR as libc::c_int - || r == MAILSTREAM_IDLE_CANCELLED as libc::c_int - { - dc_log_info( - context, - 0, - b"IMAP-IDLE wait cancelled, r=%i, r2=%i; we\'ll reconnect soon.\x00" - as *const u8 - as *const libc::c_char, - r, - r2, - ); - imap.should_reconnect = 1 - } else if r == MAILSTREAM_IDLE_INTERRUPTED as libc::c_int { - dc_log_info( - 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( - 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( - context, - 0, - b"IMAP-IDLE timeout.\x00" as *const u8 as *const libc::c_char, - ); - } else { - dc_log_warning( - context, - 0, - b"IMAP-IDLE returns unknown value r=%i, r2=%i.\x00" as *const u8 - as *const libc::c_char, - r, - r2, - ); + Duration::new(60, 0) + }; + + let &(ref lock, ref cvar) = &*self.watch.clone(); + + let mut watch = lock.lock().unwrap(); + + loop { + let res = cvar.wait_timeout(watch, seconds_to_wait).unwrap(); + watch = res.0; + if *watch { + do_fake_idle = false; + } + if *watch || res.1.timed_out() { + break; + } + } + + *watch = false; + + if !do_fake_idle { + return; + } + + // TODO: connect if needed + if let Some(ref watch_folder) = self.config.read().unwrap().watch_folder { + if 0 != self.fetch_from_single_folder(context, watch_folder) { + do_fake_idle = false; + } + } + } + } + + pub fn interrupt_idle(&self) { + println!("interrupt idle"); + + // TODO: interrupt real idle + // ref: https://github.com/jonhoo/rust-imap/issues/121 + + println!("waiting for lock"); + let &(ref lock, ref cvar) = &*self.watch.clone(); + let mut watch = lock.lock().unwrap(); + + *watch = true; + println!("notify"); + cvar.notify_one(); + } + + pub fn mv, S2: AsRef>( + &self, + context: &dc_context_t, + folder: S1, + uid: u32, + dest_folder: S2, + dest_uid: &mut u32, + ) -> usize { + let mut res = DC_RETRY_LATER; + let set = format!("{}", uid); + + if uid == 0 { + res = DC_FAILED; + } else if folder.as_ref() == dest_folder.as_ref() { + unsafe { + dc_log_info( + context, + 0, + b"Skip moving message; message %s/%i is already in %s...\x00" as *const u8 + as *const libc::c_char, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + uid as libc::c_int, + CString::new(dest_folder.as_ref().to_owned()) + .unwrap() + .as_ptr(), + ) + }; + res = DC_ALREADY_DONE; + } else { + unsafe { + dc_log_info( + context, + 0, + b"Moving message %s/%i to %s...\x00" as *const u8 as *const libc::c_char, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + uid as libc::c_int, + CString::new(dest_folder.as_ref().to_owned()) + .unwrap() + .as_ptr(), + ) + }; + if self.select_folder(context, Some(folder.as_ref())) == 0 { + unsafe { + dc_log_warning( + context, + 0, + b"Cannot select folder %s for moving message.\x00" as *const u8 + as *const libc::c_char, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + ) + }; + } else { + let moved = if let Some(ref mut session) = *self.session.lock().unwrap() { + match session.uid_mv(&set, &dest_folder) { + Ok(_) => { + res = DC_SUCCESS; + true } + Err(err) => { + eprintln!("move error: {:?}", err); + unsafe { + dc_log_info( + context, + 0, + b"Cannot move message, fallback to COPY/DELETE %s/%i to %s...\x00" + as *const u8 as *const libc::c_char, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + uid as libc::c_int, + CString::new(dest_folder.as_ref().to_owned()).unwrap().as_ptr(), + ) + }; + false + } + } + } else { + unreachable!(); + }; + + if !moved { + let copied = if let Some(ref mut session) = *self.session.lock().unwrap() { + match session.uid_copy(&set, &dest_folder) { + Ok(_) => true, + Err(err) => { + eprintln!("error copy: {:?}", err); + unsafe { + dc_log_info( + context, + 0, + b"Cannot copy message.\x00" as *const u8 + as *const libc::c_char, + ) + }; + false + } + } + } else { + unreachable!(); + }; + + if copied { + if self.add_flag(uid, "\\Deleted") == 0 { + unsafe { + dc_log_warning( + context, + 0, + b"Cannot mark message as \"Deleted\".\x00" as *const u8 + as *const libc::c_char, + ) + }; + } + self.config.write().unwrap().selected_folder_needs_expunge = true; + res = DC_SUCCESS; } } } } - } else { - fake_idle(context, imap); - } -} -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 = SystemTime::now(); + if res == DC_SUCCESS { + // TODO: is this correct? + *dest_uid = uid; + } - dc_log_info( - context, - 0, - b"IMAP-fake-IDLEing...\x00" as *const u8 as *const libc::c_char, - ); - let mut do_fake_idle: libc::c_int = 1; - while 0 != do_fake_idle { - let seconds_to_wait = if fake_idle_start_time.elapsed().unwrap() < Duration::new(3 * 60, 0) - { - Duration::new(5, 0) + if res == DC_RETRY_LATER { + if self.should_reconnect() { + DC_RETRY_LATER + } else { + DC_FAILED + } } else { - Duration::new(60, 0) - }; - - let &(ref lock, ref cvar) = &*imap.watch.clone(); - - let mut watch = lock.lock().unwrap(); - - loop { - let res = cvar.wait_timeout(watch, seconds_to_wait).unwrap(); - watch = res.0; - if *watch { - do_fake_idle = 0; - } - if *watch || res.1.timed_out() { - break; - } - } - - *watch = false; - if do_fake_idle == 0 { - return; - } - if 0 != setup_handle_if_needed(context, imap) { - if 0 != fetch_from_single_folder(context, imap, imap.watch_folder) { - do_fake_idle = 0; - } - } - } -} - -pub unsafe fn dc_imap_interrupt_idle(imap: &mut dc_imap_t) { - if 0 != imap.can_idle { - if !imap.etpan.is_null() && !(*imap.etpan).imap_stream.is_null() { - mailstream_interrupt_idle((*imap.etpan).imap_stream); + res } } - let &(ref lock, ref cvar) = &*imap.watch.clone(); - let mut watch = lock.lock().unwrap(); + fn add_flag>(&self, server_uid: u32, flag: S) -> usize { + if let Some(ref mut session) = *self.session.lock().unwrap() { + let set = format!("{}", server_uid); + let query = format!("+ FLAGS ({})", flag.as_ref()); + match session.uid_store(set, query) { + Ok(_) => {} + Err(err) => { + eprintln!("imap store error {:?}", err); + } + } + } - *watch = true; - cvar.notify_one(); -} - -pub unsafe fn dc_imap_move( - context: &dc_context_t, - imap: &mut dc_imap_t, - folder: *const libc::c_char, - uid: uint32_t, - dest_folder: *const libc::c_char, - dest_uid: *mut uint32_t, -) -> dc_imap_res { - let mut current_block: u64; - let mut res: dc_imap_res = DC_RETRY_LATER; - let mut r: libc::c_int = 0; - let mut set: *mut mailimap_set = mailimap_set_new_single(uid); - let mut res_uid: uint32_t = 0 as uint32_t; - let mut res_setsrc: *mut mailimap_set = 0 as *mut mailimap_set; - let mut res_setdest: *mut mailimap_set = 0 as *mut mailimap_set; - if folder.is_null() - || uid == 0 as libc::c_uint - || dest_folder.is_null() - || dest_uid.is_null() - || set.is_null() - { - res = DC_FAILED - } else if strcasecmp(folder, dest_folder) == 0 { - dc_log_info( - context, - 0, - b"Skip moving message; message %s/%i is already in %s...\x00" as *const u8 - as *const libc::c_char, - folder, - uid as libc::c_int, - dest_folder, - ); - res = DC_ALREADY_DONE - } else { - dc_log_info( - 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(context, imap, folder) == 0 { - dc_log_warning( - context, - 0, - b"Cannot select folder %s for moving message.\x00" as *const u8 - as *const libc::c_char, - folder, - ); + if self.should_reconnect() { + 0 } else { - r = mailimap_uidplus_uid_move( - imap.etpan, - set, - dest_folder, - &mut res_uid, - &mut res_setsrc, - &mut res_setdest, - ); - 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 - } - if !res_setdest.is_null() { - mailimap_set_free(res_setdest); - res_setdest = 0 as *mut mailimap_set - } + 1 + } + } + + pub fn set_seen>(&self, context: &dc_context_t, folder: S, uid: u32) -> usize { + let mut res = DC_RETRY_LATER; + + if uid == 0 { + res = DC_FAILED + } else if self.is_connected() { + let folder_c = CString::new(folder.as_ref().to_owned()).unwrap(); + + unsafe { dc_log_info( context, 0, - b"Cannot move message, fallback to COPY/DELETE %s/%i to %s...\x00" as *const u8 - as *const libc::c_char, - folder, + b"Marking message %s/%i as seen...\x00" as *const u8 as *const libc::c_char, + folder_c.as_ptr(), uid as libc::c_int, - dest_folder, - ); - r = mailimap_uidplus_uid_copy( - imap.etpan, - set, - dest_folder, - &mut res_uid, - &mut res_setsrc, - &mut res_setdest, - ); - if 0 != dc_imap_is_error(context, imap, r) { - dc_log_info( + ) + }; + if self.select_folder(context, Some(folder)) == 0 { + unsafe { + dc_log_warning( context, 0, - b"Cannot copy message.\x00" as *const u8 as *const libc::c_char, - ); - current_block = 14415637129417834392; + b"Cannot select folder %s for setting SEEN flag.\x00" as *const u8 + as *const libc::c_char, + folder_c.as_ptr(), + ) + }; + } else if self.add_flag(uid, "\\Seen") == 0 { + unsafe { + dc_log_warning( + context, + 0, + b"Cannot mark message as seen.\x00" as *const u8 as *const libc::c_char, + ) + }; + } else { + res = DC_SUCCESS + } + } + + if res == DC_RETRY_LATER { + if self.should_reconnect() { + DC_RETRY_LATER + } else { + DC_FAILED + } + } else { + res + } + } + + pub fn set_mdnsent>(&self, context: &dc_context_t, folder: S, uid: u32) -> usize { + // returns 0=job should be retried later, 1=job done, 2=job done and flag just set + let mut res = DC_RETRY_LATER; + let mut set = format!("{}", uid); + + if uid == 0 { + res = DC_FAILED; + } else if self.is_connected() { + let folder_c = CString::new(folder.as_ref().to_owned()).unwrap(); + unsafe { + dc_log_info( + context, + 0, + b"Marking message %s/%i as $MDNSent...\x00" as *const u8 as *const libc::c_char, + folder_c.as_ptr(), + uid as libc::c_int, + ) + }; + + if self.select_folder(context, Some(folder)) == 0 { + unsafe { + dc_log_warning( + context, + 0, + b"Cannot select folder %s for setting $MDNSent flag.\x00" as *const u8 + as *const libc::c_char, + folder_c.as_ptr(), + ) + }; + } else { + // Check if the folder can handle the `$MDNSent` flag (see RFC 3503). If so, and not + // set: set the flags and return this information. + // If the folder cannot handle the `$MDNSent` flag, we risk duplicated MDNs; it's up + // to the receiving MUA to handle this then (eg. Delta Chat has no problem with this). + + let can_create_flag = self + .config + .read() + .unwrap() + .selected_mailbox + .as_ref() + .map(|mbox| { + // empty means, everything can be stored + mbox.permanent_flags.is_empty() + || mbox + .permanent_flags + .iter() + .find(|flag| match flag { + imap::types::Flag::Custom(s) => s == "$MDNSent", + _ => false, + }) + .is_some() + }) + .expect("just selected folder"); + + if can_create_flag { + let fetched_msgs = if let Some(ref mut session) = *self.session.lock().unwrap() + { + match session.uid_fetch(set, FETCH_FLAGS) { + Ok(res) => Some(res), + Err(err) => { + eprintln!("fetch error: {:?}", err); + None + } + } + } else { + unreachable!(); + }; + + if let Some(msgs) = fetched_msgs { + let flag_set = msgs + .first() + .map(|msg| { + msg.flags() + .iter() + .find(|flag| match flag { + imap::types::Flag::Custom(s) => s == "$MDNSent", + _ => false, + }) + .is_some() + }) + .unwrap_or_else(|| false); + + res = if flag_set { + DC_ALREADY_DONE + } else if self.add_flag(uid, "$MDNSent") != 0 { + DC_SUCCESS + } else { + res + }; + + unsafe { + dc_log_info( + context, + 0, + if res == DC_SUCCESS { + b"$MDNSent just set and MDN will be sent.\x00" as *const u8 + as *const libc::c_char + } else { + b"$MDNSent already set and MDN already sent.\x00" as *const u8 + as *const libc::c_char + }, + ) + }; + } } else { - if add_flag(imap, uid, mailimap_flag_new_deleted()) == 0 { + res = DC_SUCCESS; + unsafe { + dc_log_info( + context, + 0, + b"Cannot store $MDNSent flags, risk sending duplicate MDN.\x00" + as *const u8 as *const libc::c_char, + ) + }; + } + } + } + + if res == DC_RETRY_LATER { + if self.should_reconnect() { + DC_RETRY_LATER + } else { + DC_FAILED + } + } else { + res + } + } + + // only returns 0 on connection problems; we should try later again in this case * + pub fn delete_msg, S2: AsRef>( + &self, + context: &dc_context_t, + message_id: S1, + folder: S2, + server_uid: &mut u32, + ) -> usize { + let mut success = false; + if *server_uid == 0 { + success = true + } else { + unsafe { + dc_log_info( + context, + 0, + b"Marking message \"%s\", %s/%i for deletion...\x00" as *const u8 + as *const libc::c_char, + &message_id, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + *server_uid as libc::c_int, + ) + }; + + if self.select_folder(context, Some(&folder)) == 0 { + unsafe { + dc_log_warning( + context, + 0, + b"Cannot select folder %s for deleting message.\x00" as *const u8 + as *const libc::c_char, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + ) + }; + } else { + let set = format!("{}", server_uid); + if let Some(ref mut session) = *self.session.lock().unwrap() { + match session.uid_fetch(set, PREFETCH_FLAGS) { + Ok(msgs) => { + if msgs.is_empty() + || msgs + .first() + .unwrap() + .envelope() + .expect("missing envelope") + .message_id + .expect("missing message id") + != message_id.as_ref() + { + unsafe { + dc_log_warning( + context, + 0, + b"Cannot delete on IMAP, %s/%i does not match %s.\x00" + as *const u8 + as *const libc::c_char, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + *server_uid as libc::c_int, + message_id, + ) + }; + *server_uid = 0; + } + } + Err(err) => { + eprintln!("fetch error: {:?}", err); + + unsafe { + dc_log_warning( + context, + 0, + b"Cannot delete on IMAP, %s/%i not found.\x00" as *const u8 + as *const libc::c_char, + CString::new(folder.as_ref().to_owned()).unwrap().as_ptr(), + *server_uid as libc::c_int, + ) + }; + *server_uid = 0; + } + } + } + + // mark the message for deletion + if self.add_flag(*server_uid, "\\Deleted") == 0 { + unsafe { dc_log_warning( context, 0, b"Cannot mark message as \"Deleted\".\x00" as *const u8 as *const libc::c_char, - ); - } - imap.selected_folder_needs_expunge = 1; - current_block = 1538046216550696469; - } - } else { - current_block = 1538046216550696469; - } - match current_block { - 14415637129417834392 => {} - _ => { - if !res_setdest.is_null() { - let mut cur: *mut clistiter = (*(*res_setdest).set_list).first; - if !cur.is_null() { - let mut item: *mut mailimap_set_item = 0 as *mut mailimap_set_item; - item = (if !cur.is_null() { - (*cur).data - } else { - 0 as *mut libc::c_void - }) as *mut mailimap_set_item; - *dest_uid = (*item).set_first - } - } - res = DC_SUCCESS - } - } - } - } - if !set.is_null() { - mailimap_set_free(set); - set = 0 as *mut mailimap_set - } - if !res_setsrc.is_null() { - mailimap_set_free(res_setsrc); - res_setsrc = 0 as *mut mailimap_set - } - if !res_setdest.is_null() { - mailimap_set_free(res_setdest); - res_setdest = 0 as *mut mailimap_set - } - return (if res as libc::c_uint == DC_RETRY_LATER as libc::c_int as libc::c_uint { - (if 0 != imap.should_reconnect { - DC_RETRY_LATER as libc::c_int - } else { - DC_FAILED as libc::c_int - }) as libc::c_uint - } else { - res as libc::c_uint - }) as dc_imap_res; -} - -unsafe fn add_flag( - imap: &mut dc_imap_t, - server_uid: uint32_t, - flag: *mut mailimap_flag, -) -> libc::c_int { - let mut flag_list: *mut mailimap_flag_list = 0 as *mut mailimap_flag_list; - let mut store_att_flags: *mut mailimap_store_att_flags = 0 as *mut mailimap_store_att_flags; - let mut set: *mut mailimap_set = mailimap_set_new_single(server_uid); - if !(imap.etpan.is_null()) { - flag_list = mailimap_flag_list_new_empty(); - mailimap_flag_list_add(flag_list, flag); - store_att_flags = mailimap_store_att_flags_new_add_flags(flag_list); - mailimap_uid_store(imap.etpan, set, store_att_flags); - } - if !store_att_flags.is_null() { - mailimap_store_att_flags_free(store_att_flags); - } - if !set.is_null() { - mailimap_set_free(set); - set = 0 as *mut mailimap_set - } - if 0 != imap.should_reconnect { - 0 - } else { - 1 - } -} - -pub unsafe fn dc_imap_set_seen( - context: &dc_context_t, - imap: &mut dc_imap_t, - folder: *const libc::c_char, - uid: uint32_t, -) -> dc_imap_res { - let mut res: dc_imap_res = DC_RETRY_LATER; - if folder.is_null() || uid == 0 as libc::c_uint { - res = DC_FAILED - } else if !imap.etpan.is_null() { - dc_log_info( - 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(context, imap, folder) == 0 { - dc_log_warning( - context, - 0, - b"Cannot select folder %s for setting SEEN flag.\x00" as *const u8 - as *const libc::c_char, - folder, - ); - } else if add_flag(imap, uid, mailimap_flag_new_seen()) == 0 { - dc_log_warning( - context, - 0, - b"Cannot mark message as seen.\x00" as *const u8 as *const libc::c_char, - ); - } else { - res = DC_SUCCESS - } - } - return (if res as libc::c_uint == DC_RETRY_LATER as libc::c_int as libc::c_uint { - (if 0 != imap.should_reconnect { - DC_RETRY_LATER as libc::c_int - } else { - DC_FAILED as libc::c_int - }) as libc::c_uint - } else { - res as libc::c_uint - }) as dc_imap_res; -} - -pub unsafe fn dc_imap_set_mdnsent( - context: &dc_context_t, - imap: &mut dc_imap_t, - folder: *const libc::c_char, - uid: uint32_t, -) -> dc_imap_res { - let mut can_create_flag: libc::c_int = 0; - let mut current_block: u64; - // returns 0=job should be retried later, 1=job done, 2=job done and flag just set - let mut res: dc_imap_res = DC_RETRY_LATER; - let mut set: *mut mailimap_set = mailimap_set_new_single(uid); - let mut fetch_result: *mut clist = 0 as *mut clist; - if folder.is_null() || uid == 0 as libc::c_uint || set.is_null() { - res = DC_FAILED - } else if !imap.etpan.is_null() { - dc_log_info( - 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(context, imap, folder) == 0 { - dc_log_warning( - context, - 0, - b"Cannot select folder %s for setting $MDNSent flag.\x00" as *const u8 - as *const libc::c_char, - folder, - ); - } else { - /* Check if the folder can handle the `$MDNSent` flag (see RFC 3503). If so, and not set: set the flags and return this information. - If the folder cannot handle the `$MDNSent` flag, we risk duplicated MDNs; it's up to the receiving MUA to handle this then (eg. Delta Chat has no problem with this). */ - can_create_flag = 0; - if !(*imap.etpan).imap_selection_info.is_null() - && !(*(*imap.etpan).imap_selection_info) - .sel_perm_flags - .is_null() - { - let mut iter: *mut clistiter = 0 as *mut clistiter; - iter = (*(*(*imap.etpan).imap_selection_info).sel_perm_flags).first; - while !iter.is_null() { - let mut fp: *mut mailimap_flag_perm = (if !iter.is_null() { - (*iter).data - } else { - 0 as *mut libc::c_void - }) - as *mut mailimap_flag_perm; - if !fp.is_null() { - if (*fp).fl_type == MAILIMAP_FLAG_PERM_ALL as libc::c_int { - can_create_flag = 1; - break; - } else if (*fp).fl_type == MAILIMAP_FLAG_PERM_FLAG as libc::c_int - && !(*fp).fl_flag.is_null() - { - let mut fl: *mut mailimap_flag = (*fp).fl_flag as *mut mailimap_flag; - if (*fl).fl_type == MAILIMAP_FLAG_KEYWORD as libc::c_int - && !(*fl).fl_data.fl_keyword.is_null() - && strcmp( - (*fl).fl_data.fl_keyword, - b"$MDNSent\x00" as *const u8 as *const libc::c_char, - ) == 0 - { - can_create_flag = 1; - break; - } - } - } - iter = if !iter.is_null() { - (*iter).next - } else { - 0 as *mut clistcell_s - } - } - } - 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(context, imap, r) || fetch_result.is_null() { - fetch_result = 0 as *mut clist + ) + }; } else { - let mut cur: *mut clistiter = (*fetch_result).first; - if !cur.is_null() { - if 0 != peek_flag_keyword( - (if !cur.is_null() { - (*cur).data - } else { - 0 as *mut libc::c_void - }) as *mut mailimap_msg_att, - b"$MDNSent\x00" as *const u8 as *const libc::c_char, - ) { - res = DC_ALREADY_DONE; - current_block = 14832935472441733737; - } else if add_flag( - imap, - uid, - mailimap_flag_new_flag_keyword(dc_strdup( - b"$MDNSent\x00" as *const u8 as *const libc::c_char, - )), - ) == 0 - { - current_block = 17044610252497760460; - } else { - res = DC_SUCCESS; - current_block = 14832935472441733737; - } - match current_block { - 17044610252497760460 => {} - _ => { - dc_log_info( - context, - 0, - if res as libc::c_uint - == DC_SUCCESS as libc::c_int as libc::c_uint - { - b"$MDNSent just set and MDN will be sent.\x00" as *const u8 - as *const libc::c_char - } else { - b"$MDNSent already set and MDN already sent.\x00" - as *const u8 - as *const libc::c_char - }, - ); - } - } - } + self.config.write().unwrap().selected_folder_needs_expunge = true; + success = true } - } else { - res = DC_SUCCESS; + } + } + + if success { + 1 + } else { + self.is_connected() as usize + } + } + + pub fn configure_folders(&self, context: &dc_context_t, flags: libc::c_int) { + if !self.is_connected() { + return; + } + + unsafe { + dc_log_info( + context, + 0, + b"Configuring IMAP-folders.\x00" as *const u8 as *const libc::c_char, + ) + }; + + let folders = self.list_folders(context).unwrap(); + let delimiter = self.config.read().unwrap().imap_delimiter; + let fallback_folder = format!("INBOX{}DeltaChat", delimiter); + + let mut mvbox_folder = folders + .iter() + .find(|folder| folder.name() == "DeltaChat" || folder.name() == fallback_folder) + .map(|n| n.name().to_string()); + + let mut sentbox_folder = folders + .iter() + .find(|folder| match get_folder_meaning(folder) { + FolderMeaning::SentObjects => true, + _ => false, + }); + + println!("folders: {:?} - {:?}", mvbox_folder, sentbox_folder); + + if mvbox_folder.is_none() && 0 != (flags as usize & DC_CREATE_MVBOX) { + unsafe { dc_log_info( context, - 0, - b"Cannot store $MDNSent flags, risk sending duplicate MDN.\x00" as *const u8 - as *const libc::c_char, - ); - } - } - } - if !set.is_null() { - mailimap_set_free(set); - set = 0 as *mut mailimap_set - } - if !fetch_result.is_null() { - mailimap_fetch_list_free(fetch_result); - fetch_result = 0 as *mut clist - } + 0i32, + b"Creating MVBOX-folder \"%s\"...\x00" as *const u8 as *const libc::c_char, + b"DeltaChat\x00" as *const u8 as *const libc::c_char, + ) + }; - (if res as libc::c_uint == DC_RETRY_LATER as libc::c_int as libc::c_uint { - (if 0 != imap.should_reconnect { - DC_RETRY_LATER as libc::c_int - } else { - DC_FAILED as libc::c_int - }) as libc::c_uint - } else { - res as libc::c_uint - }) as dc_imap_res -} + if let Some(ref mut session) = *self.session.lock().unwrap() { + match session.create("DeltaChat") { + Ok(_) => { + mvbox_folder = Some("DeltaChat".into()); -unsafe fn peek_flag_keyword( - msg_att: *mut mailimap_msg_att, - flag_keyword: *const libc::c_char, -) -> libc::c_int { - if msg_att.is_null() || (*msg_att).att_list.is_null() || flag_keyword.is_null() { - return 0; - } - let mut iter1: *mut clistiter = 0 as *mut clistiter; - let mut iter2: *mut clistiter = 0 as *mut clistiter; - iter1 = (*(*msg_att).att_list).first; - while !iter1.is_null() { - let mut item: *mut mailimap_msg_att_item = (if !iter1.is_null() { - (*iter1).data - } else { - 0 as *mut libc::c_void - }) as *mut mailimap_msg_att_item; - if !item.is_null() { - if (*item).att_type == MAILIMAP_MSG_ATT_ITEM_DYNAMIC as libc::c_int { - if !(*(*item).att_data.att_dyn).att_list.is_null() { - iter2 = (*(*(*item).att_data.att_dyn).att_list).first; - while !iter2.is_null() { - let mut flag_fetch: *mut mailimap_flag_fetch = (if !iter2.is_null() { - (*iter2).data - } else { - 0 as *mut libc::c_void - }) - as *mut mailimap_flag_fetch; - if !flag_fetch.is_null() - && (*flag_fetch).fl_type == MAILIMAP_FLAG_FETCH_OTHER as libc::c_int - { - let mut flag: *mut mailimap_flag = (*flag_fetch).fl_flag; - if !flag.is_null() { - if (*flag).fl_type == MAILIMAP_FLAG_KEYWORD as libc::c_int - && !(*flag).fl_data.fl_keyword.is_null() - && strcmp((*flag).fl_data.fl_keyword, flag_keyword) == 0 - { - return 1; - } + unsafe { + dc_log_info( + context, + 0i32, + b"MVBOX-folder created.\x00" as *const u8 as *const libc::c_char, + ) + }; + } + Err(err) => { + eprintln!("create error: {:?}", err); + unsafe { + dc_log_warning( + context, + 0, + b"Cannot create MVBOX-folder, using trying INBOX subfolder.\x00" + as *const u8 + as *const libc::c_char, + ) + }; + + match session.create(&fallback_folder) { + Ok(_) => { + mvbox_folder = Some(fallback_folder); + unsafe { + dc_log_info( + context, + 0, + b"MVBOX-folder created as INBOX subfolder.\x00" as *const u8 + as *const libc::c_char, + ) + }; + } + Err(err) => { + eprintln!("create error: {:?}", err); + unsafe { + dc_log_warning( + context, + 0i32, + b"Cannot create MVBOX-folder.\x00" as *const u8 + as *const libc::c_char, + ) + }; } - } - iter2 = if !iter2.is_null() { - (*iter2).next - } else { - 0 as *mut clistcell_s } } } + // SUBSCRIBE is needed to make the folder visible to the LSUB command + // that may be used by other MUAs to list folders. + // for the LIST command, the folder is always visible. + if let Some(ref mvbox) = mvbox_folder { + // TODO: better error handling + session.subscribe(mvbox).expect("failed to subscribe"); + } } } - iter1 = if !iter1.is_null() { - (*iter1).next - } else { - 0 as *mut clistcell_s - } - } - 0 -} -/* 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, - mut server_uid: uint32_t, -) -> libc::c_int { - let mut success: libc::c_int = 0; - let mut r: libc::c_int = 0; - let mut fetch_result: *mut clist = 0 as *mut clist; - let mut is_rfc724_mid: *mut libc::c_char = 0 as *mut libc::c_char; - let mut new_folder: *mut libc::c_char = 0 as *mut libc::c_char; - if rfc724_mid.is_null() - || folder.is_null() - || *folder.offset(0isize) as libc::c_int == 0 - || server_uid == 0 as libc::c_uint - { - success = 1 - } else { - dc_log_info( - context, - 0, - b"Marking message \"%s\", %s/%i for deletion...\x00" as *const u8 - as *const libc::c_char, - rfc724_mid, - folder, - server_uid as libc::c_int, - ); - if select_folder(context, imap, folder) == 0 { - dc_log_warning( + unsafe { + dc_sqlite3_set_config_int( context, - 0, - b"Cannot select folder %s for deleting message.\x00" as *const u8 - as *const libc::c_char, - folder, + &context.sql.read().unwrap(), + b"folders_configured\x00" as *const u8 as *const libc::c_char, + 3, ); - } else { - let mut cur: *mut clistiter = 0 as *mut clistiter; - let mut is_quoted_rfc724_mid: *const libc::c_char = 0 as *const libc::c_char; - let mut set: *mut mailimap_set = mailimap_set_new_single(server_uid); - r = mailimap_uid_fetch(imap.etpan, set, imap.fetch_type_prefetch, &mut fetch_result); - if !set.is_null() { - mailimap_set_free(set); - set = 0 as *mut mailimap_set - } - if 0 != dc_imap_is_error(context, imap, r) || fetch_result.is_null() { - fetch_result = 0 as *mut clist; - dc_log_warning( + if let Some(ref mvbox_folder) = mvbox_folder { + dc_sqlite3_set_config( context, - 0, - b"Cannot delete on IMAP, %s/%i not found.\x00" as *const u8 - as *const libc::c_char, - folder, - server_uid as libc::c_int, + &context.sql.read().unwrap(), + b"configured_mvbox_folder\x00" as *const u8 as *const libc::c_char, + CString::new(mvbox_folder.clone()).unwrap().as_ptr(), ); - server_uid = 0 as uint32_t } - cur = (*fetch_result).first; - if cur.is_null() - || { - is_quoted_rfc724_mid = peek_rfc724_mid( - (if !cur.is_null() { - (*cur).data - } else { - 0 as *mut libc::c_void - }) as *mut mailimap_msg_att, - ); - is_quoted_rfc724_mid.is_null() - } - || { - is_rfc724_mid = unquote_rfc724_mid(is_quoted_rfc724_mid); - is_rfc724_mid.is_null() - } - || strcmp(is_rfc724_mid, rfc724_mid) != 0 - { - dc_log_warning( + if let Some(ref sentbox_folder) = sentbox_folder { + dc_sqlite3_set_config( context, - 0, - b"Cannot delete on IMAP, %s/%i does not match %s.\x00" as *const u8 - as *const libc::c_char, - folder, - server_uid as libc::c_int, - rfc724_mid, + &context.sql.read().unwrap(), + b"configured_sentbox_folder\x00" as *const u8 as *const libc::c_char, + CString::new(sentbox_folder.name()).unwrap().as_ptr(), ); - server_uid = 0 as uint32_t - } - /* mark the message for deletion */ - if add_flag(imap, server_uid, mailimap_flag_new_deleted()) == 0 { - dc_log_warning( - context, - 0, - b"Cannot mark message as \"Deleted\".\x00" as *const u8 as *const libc::c_char, - ); - } else { - imap.selected_folder_needs_expunge = 1; - success = 1 } } } - if !fetch_result.is_null() { - mailimap_fetch_list_free(fetch_result); - fetch_result = 0 as *mut clist - } - free(is_rfc724_mid as *mut libc::c_void); - free(new_folder as *mut libc::c_void); - if 0 != success { - 1 - } else { - dc_imap_is_connected(imap) + fn list_folders( + &self, + context: &dc_context_t, + ) -> Option>> { + if let Some(ref mut session) = *self.session.lock().unwrap() { + // TODO: use xlist when available + match session.list(Some(""), Some("*")) { + Ok(list) => { + if list.is_empty() { + unsafe { + dc_log_warning( + context, + 0i32, + b"Folder list is empty.\x00" as *const u8 as *const libc::c_char, + ) + }; + } + Some(list) + } + Err(err) => { + eprintln!("list error: {:?}", err); + unsafe { + dc_log_warning( + context, + 0i32, + b"Cannot get folder list.\x00" as *const u8 as *const libc::c_char, + ) + }; + None + } + } + } else { + None + } + } +} + +fn to_string(str: *const libc::c_char) -> String { + unsafe { CStr::from_ptr(str).to_str().unwrap().to_string() } +} + +fn to_str<'a>(str: *const libc::c_char) -> &'a str { + unsafe { CStr::from_ptr(str).to_str().unwrap() } +} + +/// Try to get the folder meaning by the name of the folder only used if the server does not support XLIST. +// TODO: lots languages missing - maybe there is a list somewhere on other MUAs? +// however, if we fail to find out the sent-folder, +// only watching this folder is not working. at least, this is no show stopper. +// CAVE: if possible, take care not to add a name here that is "sent" in one language +// but sth. different in others - a hard job. +fn get_folder_meaning_by_name(folder_name: &imap::types::Name) -> FolderMeaning { + let sent_names = vec!["sent", "sent objects", "gesendet"]; + let lower = folder_name.name().to_lowercase(); + + if sent_names.into_iter().find(|s| *s == lower).is_some() { + FolderMeaning::SentObjects + } else { + FolderMeaning::Unknown + } +} + +fn get_folder_meaning(folder_name: &imap::types::Name) -> FolderMeaning { + if folder_name.attributes().is_empty() { + return FolderMeaning::Unknown; + } + + let mut res = FolderMeaning::Unknown; + let special_names = vec!["\\Spam", "\\Trash", "\\Drafts", "\\Junk"]; + + for attr in folder_name.attributes() { + match attr { + imap::types::NameAttribute::Custom(ref label) => { + if special_names.iter().find(|s| *s == label).is_some() { + res = FolderMeaning::Other; + } else if label == "\\Sent" { + res = FolderMeaning::SentObjects + } + } + _ => {} + } + } + + match res { + FolderMeaning::Unknown => get_folder_meaning_by_name(folder_name), + _ => res, } } diff --git a/src/dc_imex.rs b/src/dc_imex.rs index 8bd30b127..3f2a0a001 100644 --- a/src/dc_imex.rs +++ b/src/dc_imex.rs @@ -1,4 +1,6 @@ -use libc; +use mmime::mailmime_content::*; +use mmime::mmapstring::*; +use mmime::other::*; use rand::{thread_rng, Rng}; use crate::constants::Event; @@ -39,14 +41,15 @@ pub unsafe fn dc_imex( dc_job_add(context, 910i32, 0i32, (*param).packed, 0i32); dc_param_unref(param); } + pub unsafe fn dc_imex_has_backup( mut context: &dc_context_t, mut dir_name: *const libc::c_char, ) -> *mut libc::c_char { let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; let mut ret_backup_time: time_t = 0i32 as time_t; - let mut dir_handle: *mut DIR = 0 as *mut DIR; - let mut dir_entry: *mut dirent = 0 as *mut dirent; + let mut dir_handle: *mut DIR; + let mut dir_entry: *mut dirent; let mut prefix_len = strlen(b"delta-chat\x00" as *const u8 as *const libc::c_char); let mut suffix_len = strlen(b"bak\x00" as *const u8 as *const libc::c_char); let mut curr_pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char; @@ -148,17 +151,19 @@ pub unsafe fn dc_check_password( } dc_loginparam_unref(loginparam); - return success; + + success } + pub unsafe fn dc_initiate_key_transfer(mut context: &dc_context_t) -> *mut libc::c_char { let mut current_block: u64; let mut success: libc::c_int = 0i32; - let mut setup_code: *mut libc::c_char = 0 as *mut libc::c_char; + let mut setup_code: *mut libc::c_char; let mut setup_file_content: *mut libc::c_char = 0 as *mut libc::c_char; let mut setup_file_name: *mut libc::c_char = 0 as *mut libc::c_char; - let mut chat_id: uint32_t = 0i32 as uint32_t; + let mut chat_id: uint32_t; let mut msg: *mut dc_msg_t = 0 as *mut dc_msg_t; - let mut msg_id: uint32_t = 0i32 as uint32_t; + let mut msg_id: uint32_t; if 0 == dc_alloc_ongoing(context) { return 0 as *mut libc::c_char; } @@ -273,8 +278,10 @@ pub unsafe fn dc_initiate_key_transfer(mut context: &dc_context_t) -> *mut libc: free(setup_file_content as *mut libc::c_void); dc_msg_unref(msg); dc_free_ongoing(context); - return setup_code; + + setup_code } + pub unsafe extern "C" fn dc_render_setup_file( mut context: &dc_context_t, mut passphrase: *const libc::c_char, @@ -311,9 +318,9 @@ pub unsafe extern "C" fn dc_render_setup_file( let mut payload_key_asc: *mut libc::c_char = dc_key_render_asc( curr_private_key, if 0 != e2ee_enabled { - b"Autocrypt-Prefer-Encrypt: mutual\r\n\x00" as *const u8 as *const libc::c_char + Some(("Autocrypt-Prefer-Encrypt", "mutual")) } else { - 0 as *const libc::c_char + None }, ); if !payload_key_asc.is_null() { @@ -364,11 +371,13 @@ pub unsafe extern "C" fn dc_render_setup_file( dc_key_unref(curr_private_key); free(encr_string as *mut libc::c_void); free(self_addr as *mut libc::c_void); - return ret_setupfilecontent; + + ret_setupfilecontent } + pub unsafe fn dc_create_setup_code(_context: &dc_context_t) -> *mut libc::c_char { - let mut random_val: uint16_t = 0i32 as uint16_t; - let mut i: libc::c_int = 0i32; + let mut random_val: uint16_t; + let mut i: libc::c_int; let mut ret: dc_strbuilder_t = dc_strbuilder_t { buf: 0 as *mut libc::c_char, allocated: 0, @@ -398,8 +407,11 @@ pub unsafe fn dc_create_setup_code(_context: &dc_context_t) -> *mut libc::c_char ); i += 1 } - return ret.buf; + + ret.buf } + +// TODO should return bool /rtn pub unsafe fn dc_continue_key_transfer( mut context: &dc_context_t, mut msg_id: uint32_t, @@ -473,15 +485,18 @@ pub unsafe fn dc_continue_key_transfer( free(filename as *mut libc::c_void); dc_msg_unref(msg); free(norm_sc as *mut libc::c_void); - return success; + + success } + +// TODO should return bool /rtn unsafe fn set_self_key( mut context: &dc_context_t, mut armored: *const libc::c_char, mut set_default: libc::c_int, ) -> libc::c_int { let mut success: libc::c_int = 0i32; - let mut buf: *mut libc::c_char = 0 as *mut libc::c_char; + let mut buf: *mut libc::c_char; // pointer inside buf, MUST NOT be free()'d let mut buf_headerline: *const libc::c_char = 0 as *const libc::c_char; // - " - @@ -597,14 +612,16 @@ unsafe fn set_self_key( free(self_addr as *mut libc::c_void); dc_key_unref(private_key); dc_key_unref(public_key); - return success; + + success } + pub unsafe fn dc_decrypt_setup_file( mut context: &dc_context_t, mut passphrase: *const libc::c_char, mut filecontent: *const libc::c_char, ) -> *mut libc::c_char { - let mut fc_buf: *mut libc::c_char = 0 as *mut libc::c_char; + let mut fc_buf: *mut libc::c_char; let mut fc_headerline: *const libc::c_char = 0 as *const libc::c_char; let mut fc_base64: *const libc::c_char = 0 as *const libc::c_char; let mut binary: *mut libc::c_char = 0 as *mut libc::c_char; @@ -661,8 +678,10 @@ pub unsafe fn dc_decrypt_setup_file( if !binary.is_null() { mmap_string_unref(binary); } - return payload; + + payload } + pub unsafe fn dc_normalize_setup_code( _context: &dc_context_t, in_0: *const libc::c_char, @@ -677,7 +696,7 @@ pub unsafe fn dc_normalize_setup_code( eos: 0 as *mut libc::c_char, }; dc_strbuilder_init(&mut out, 0i32); - let mut outlen: libc::c_int = 0i32; + let mut outlen: libc::c_int; let mut p1: *const libc::c_char = in_0; while 0 != *p1 { if *p1 as libc::c_int >= '0' as i32 && *p1 as libc::c_int <= '9' as i32 { @@ -701,13 +720,15 @@ pub unsafe fn dc_normalize_setup_code( } p1 = p1.offset(1isize) } - return out.buf; + + out.buf } + pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(mut context: &dc_context_t, mut job: *mut dc_job_t) { let mut current_block: u64; let mut success: libc::c_int = 0i32; let mut ongoing_allocated_here: libc::c_int = 0i32; - let mut what: libc::c_int = 0i32; + let mut what: libc::c_int; let mut param1: *mut libc::c_char = 0 as *mut libc::c_char; let mut param2: *mut libc::c_char = 0 as *mut libc::c_char; if !(0 == dc_alloc_ongoing(context)) { @@ -957,9 +978,12 @@ pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(mut context: &dc_context_t, mut job: *m 0i32 as uintptr_t, ); } -/* ****************************************************************************** + +/******************************************************************************* * Import backup ******************************************************************************/ + +// TODO should return bool /rtn unsafe fn import_backup( mut context: &dc_context_t, mut backup_to_import: *const libc::c_char, @@ -967,7 +991,7 @@ unsafe fn import_backup( let mut current_block: u64; let mut success: libc::c_int = 0i32; let mut processed_files_cnt: libc::c_int = 0i32; - let mut total_files_cnt: libc::c_int = 0i32; + let mut total_files_cnt: libc::c_int; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char; let mut repl_from: *mut libc::c_char = 0 as *mut libc::c_char; @@ -977,7 +1001,7 @@ unsafe fn import_backup( 0i32, b"Import \"%s\" to \"%s\".\x00" as *const u8 as *const libc::c_char, backup_to_import, - context.dbfile, + context.get_dbfile(), ); if 0 != dc_is_configured(context) { dc_log_error( @@ -989,22 +1013,22 @@ unsafe fn import_backup( if 0 != dc_sqlite3_is_open(&context.sql.clone().read().unwrap()) { dc_sqlite3_close(context, &mut context.sql.clone().write().unwrap()); } - dc_delete_file(context, context.dbfile); - if 0 != dc_file_exist(context, context.dbfile) { + dc_delete_file(context, context.get_dbfile()); + if 0 != dc_file_exist(context, context.get_dbfile()) { dc_log_error( context, 0i32, b"Cannot import backups: Cannot delete the old file.\x00" as *const u8 as *const libc::c_char, ); - } else if !(0 == dc_copy_file(context, backup_to_import, context.dbfile)) { + } else if !(0 == dc_copy_file(context, backup_to_import, context.get_dbfile())) { /* error already logged */ /* re-open copied database file */ if !(0 == dc_sqlite3_open( context, &mut context.sql.clone().write().unwrap(), - context.dbfile, + context.get_dbfile(), 0i32, )) { @@ -1016,7 +1040,6 @@ unsafe fn import_backup( sqlite3_step(stmt); total_files_cnt = sqlite3_column_int(stmt, 0i32); sqlite3_finalize(stmt); - stmt = 0 as *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -1062,7 +1085,7 @@ unsafe fn import_backup( free(pathNfilename as *mut libc::c_void); pathNfilename = dc_mprintf( b"%s/%s\x00" as *const u8 as *const libc::c_char, - context.blobdir, + context.get_blobdir(), file_name, ); if !(0 @@ -1112,34 +1135,37 @@ unsafe fn import_backup( free(repl_from as *mut libc::c_void); free(repl_to as *mut libc::c_void); sqlite3_finalize(stmt); - return success; + + success } -/* ****************************************************************************** + +/******************************************************************************* * Export backup ******************************************************************************/ /* the FILE_PROGRESS macro calls the callback with the permille of files processed. The macro avoids weird values of 0% or 100% while still working. */ +// TODO should return bool /rtn unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char) -> libc::c_int { let mut current_block: u64; let mut success: libc::c_int = 0i32; let mut closed: libc::c_int = 0i32; - let mut dest_pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char; + let mut dest_pathNfilename: *mut libc::c_char; let mut now = time(0 as *mut time_t); let mut dir_handle: *mut DIR = 0 as *mut DIR; - let mut dir_entry: *mut dirent = 0 as *mut dirent; + let mut dir_entry: *mut dirent; let mut prefix_len = strlen(b"delta-chat\x00" as *const u8 as *const libc::c_char); let mut suffix_len = strlen(b"bak\x00" as *const u8 as *const libc::c_char); let mut curr_pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char; let mut buf: *mut libc::c_void = 0 as *mut libc::c_void; let mut buf_bytes: size_t = 0i32 as size_t; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - let mut total_files_cnt: libc::c_int = 0i32; + let mut total_files_cnt: libc::c_int; let mut processed_files_cnt: libc::c_int = 0i32; let mut delete_dest_file: libc::c_int = 0i32; let mut dest_sql: Option = None; /* get a fine backup file name (the name includes the date so that multiple backup instances are possible) FIXME: we should write to a temporary file first and rename it on success. this would guarantee the backup is complete. however, currently it is not clear it the import exists in the long run (may be replaced by a restore-from-imap)*/ - let mut timeinfo: *mut tm = 0 as *mut tm; + let mut timeinfo: *mut tm; let mut buffer: [libc::c_char; 256] = [0; 256]; timeinfo = localtime(&mut now); strftime( @@ -1168,15 +1194,15 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char context, 0i32, b"Backup \"%s\" to \"%s\".\x00" as *const u8 as *const libc::c_char, - context.dbfile, + context.get_dbfile(), dest_pathNfilename, ); - if !(0 == dc_copy_file(context, context.dbfile, dest_pathNfilename)) { + if !(0 == dc_copy_file(context, context.get_dbfile(), dest_pathNfilename)) { /* error already logged */ dc_sqlite3_open( context, &mut context.sql.clone().write().unwrap(), - context.dbfile, + context.get_dbfile(), 0i32, ); closed = 0i32; @@ -1205,7 +1231,7 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char 11487273724841241105 => {} _ => { total_files_cnt = 0i32; - dir_handle = opendir(context.blobdir); + dir_handle = opendir(context.get_blobdir()); if dir_handle.is_null() { dc_log_error( context, @@ -1213,7 +1239,7 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char b"Backup: Cannot get info for blob-directory \"%s\".\x00" as *const u8 as *const libc::c_char, - context.blobdir, + context.get_blobdir(), ); } else { loop { @@ -1227,7 +1253,7 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char dir_handle = 0 as *mut DIR; if total_files_cnt > 0i32 { /* scan directory, pass 2: copy files */ - dir_handle = opendir(context.blobdir); + dir_handle = opendir(context.get_blobdir()); if dir_handle.is_null() { dc_log_error( context, @@ -1235,7 +1261,7 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char b"Backup: Cannot copy from blob-directory \"%s\".\x00" as *const u8 as *const libc::c_char, - context.blobdir, + context.get_blobdir(), ); current_block = 11487273724841241105; } else { @@ -1312,7 +1338,7 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char curr_pathNfilename = dc_mprintf( b"%s/%s\x00" as *const u8 as *const libc::c_char, - context.blobdir, + context.get_blobdir(), name, ); free(buf); @@ -1359,7 +1385,7 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char 0i32, b"Backup: No files to copy.\x00" as *const u8 as *const libc::c_char, - context.blobdir, + context.get_blobdir(), ); current_block = 2631791190359682872; } @@ -1395,7 +1421,7 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char dc_sqlite3_open( context, &mut context.sql.clone().write().unwrap(), - context.dbfile, + context.get_dbfile(), 0i32, ); } @@ -1410,9 +1436,11 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char free(dest_pathNfilename as *mut libc::c_void); free(curr_pathNfilename as *mut libc::c_void); free(buf); - return success; + + success } -/* ****************************************************************************** + +/******************************************************************************* * Classic key import ******************************************************************************/ unsafe fn import_self_keys( @@ -1427,14 +1455,14 @@ unsafe fn import_self_keys( (currently, the last imported key is the standard key unless it contains the string "legacy" in its name) */ let mut imported_cnt: libc::c_int = 0i32; let mut dir_handle = 0 as *mut DIR; - let mut dir_entry: *mut dirent = 0 as *mut dirent; + let mut dir_entry: *mut dirent; let mut suffix: *mut libc::c_char = 0 as *mut libc::c_char; let mut path_plus_name: *mut libc::c_char = 0 as *mut libc::c_char; - let mut set_default: libc::c_int = 0i32; + let mut set_default: libc::c_int; let mut buf: *mut libc::c_char = 0 as *mut libc::c_char; let mut buf_bytes: size_t = 0i32 as size_t; // a pointer inside buf, MUST NOT be free()'d - let mut private_key: *const libc::c_char = 0 as *const libc::c_char; + let mut private_key: *const libc::c_char; let mut buf2: *mut libc::c_char = 0 as *mut libc::c_char; // a pointer inside buf2, MUST NOT be free()'d let mut buf2_headerline: *const libc::c_char = 0 as *const libc::c_char; @@ -1544,16 +1572,19 @@ unsafe fn import_self_keys( free(path_plus_name as *mut libc::c_void); free(buf as *mut libc::c_void); free(buf2 as *mut libc::c_void); - return imported_cnt; + + imported_cnt } + +// TODO should return bool /rtn unsafe fn export_self_keys( mut context: &dc_context_t, mut dir: *const libc::c_char, ) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut export_errors: libc::c_int = 0i32; - let mut id: libc::c_int = 0i32; - let mut is_default: libc::c_int = 0i32; + let mut id: libc::c_int; + let mut is_default: libc::c_int; let mut public_key: *mut dc_key_t = dc_key_new(); let mut private_key: *mut dc_key_t = dc_key_new(); let stmt = dc_sqlite3_prepare( @@ -1582,11 +1613,14 @@ unsafe fn export_self_keys( sqlite3_finalize(stmt); dc_key_unref(public_key); dc_key_unref(private_key); - return success; + + success } -/* ****************************************************************************** + +/******************************************************************************* * Classic key export ******************************************************************************/ +// TODO should return bool /rtn unsafe fn export_key_to_asc_file( mut context: &dc_context_t, mut dir: *const libc::c_char, @@ -1595,7 +1629,7 @@ unsafe fn export_key_to_asc_file( mut is_default: libc::c_int, ) -> libc::c_int { let mut success: libc::c_int = 0i32; - let mut file_name = 0 as *mut libc::c_char; + let mut file_name; if 0 != is_default { file_name = dc_mprintf( b"%s/%s-key-default.asc\x00" as *const u8 as *const libc::c_char, @@ -1642,5 +1676,6 @@ unsafe fn export_key_to_asc_file( success = 1i32 } free(file_name as *mut libc::c_void); - return success; + + success } diff --git a/src/dc_job.rs b/src/dc_job.rs index 5eb397e3c..cecc14f22 100644 --- a/src/dc_job.rs +++ b/src/dc_job.rs @@ -1,7 +1,10 @@ -use libc; -use rand::{thread_rng, Rng}; +use mmime::mmapstring::*; + +use std::ffi::CStr; use std::time::{Duration, SystemTime}; +use rand::{thread_rng, Rng}; + use crate::constants::Event; use crate::dc_chat::*; use crate::dc_configure::*; @@ -68,7 +71,8 @@ unsafe fn dc_job_perform( mut thread: libc::c_int, mut probe_network: libc::c_int, ) { - let mut select_stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + println!("perform job"); + let mut select_stmt: *mut sqlite3_stmt; let mut job: dc_job_t = dc_job_t { job_id: 0, action: 0, @@ -470,9 +474,12 @@ unsafe fn dc_job_do_DC_JOB_MOVE_MSG(mut context: &dc_context_t, mut job: *mut dc let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); let mut dest_folder: *mut libc::c_char = 0 as *mut libc::c_char; let mut dest_uid: uint32_t = 0i32 as uint32_t; - if 0 == dc_imap_is_connected(&mut context.inbox.clone().lock().unwrap()) { - connect_to_inbox(context); - if 0 == dc_imap_is_connected(&mut context.inbox.clone().lock().unwrap()) { + + let inbox = context.inbox.read().unwrap(); + + if !inbox.is_connected() { + connect_to_inbox(context, &inbox); + if !inbox.is_connected() { dc_job_try_again_later(job, 3i32, 0 as *const libc::c_char); current_block = 2238328302157162973; } else { @@ -491,11 +498,7 @@ unsafe fn dc_job_do_DC_JOB_MOVE_MSG(mut context: &dc_context_t, mut job: *mut dc 0i32, ) < 3i32 { - dc_configure_folders( - context, - &mut context.inbox.clone().lock().unwrap(), - 0x1i32, - ); + inbox.configure_folders(context, 0x1i32); } dest_folder = dc_sqlite3_get_config( context, @@ -503,12 +506,13 @@ unsafe fn dc_job_do_DC_JOB_MOVE_MSG(mut context: &dc_context_t, mut job: *mut dc b"configured_mvbox_folder\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); - match dc_imap_move( + + let server_folder = CStr::from_ptr((*msg).server_folder).to_str().unwrap(); + match inbox.mv( context, - &mut context.inbox.clone().lock().unwrap(), - (*msg).server_folder, + server_folder, (*msg).server_uid, - dest_folder, + CStr::from_ptr(dest_folder).to_str().unwrap(), &mut dest_uid, ) as libc::c_uint { @@ -556,31 +560,28 @@ unsafe fn dc_job_do_DC_JOB_MOVE_MSG(mut context: &dc_context_t, mut job: *mut dc /* ****************************************************************************** * IMAP-jobs ******************************************************************************/ -unsafe fn connect_to_inbox(mut context: &dc_context_t) -> libc::c_int { - let mut ret_connected: libc::c_int = 0i32; - ret_connected = - dc_connect_to_configured_imap(context, &mut context.inbox.clone().lock().unwrap()); +unsafe fn connect_to_inbox(context: &dc_context_t, inbox: &dc_imap_t) -> libc::c_int { + let mut ret_connected: libc::c_int; + + ret_connected = dc_connect_to_configured_imap(context, inbox); if !(0 == ret_connected) { - dc_imap_set_watch_folder( - &mut context.inbox.clone().lock().unwrap(), - b"INBOX\x00" as *const u8 as *const libc::c_char, - ); + inbox.set_watch_folder(b"INBOX\x00" as *const u8 as *const libc::c_char); } - return ret_connected; + ret_connected } -unsafe fn dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP( - mut context: &dc_context_t, - mut job: *mut dc_job_t, -) { + +unsafe fn dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP(context: &dc_context_t, job: *mut dc_job_t) { let mut current_block: u64; let mut folder: *mut libc::c_char = dc_param_get((*job).param, 'Z' as i32, 0 as *const libc::c_char); let mut uid: uint32_t = dc_param_get_int((*job).param, 'z' as i32, 0i32) as uint32_t; let mut dest_folder: *mut libc::c_char = 0 as *mut libc::c_char; let mut dest_uid: uint32_t = 0i32 as uint32_t; - if 0 == dc_imap_is_connected(&mut context.inbox.clone().lock().unwrap()) { - connect_to_inbox(context); - if 0 == dc_imap_is_connected(&mut context.inbox.clone().lock().unwrap()) { + let mut inbox = context.inbox.read().unwrap(); + + if !inbox.is_connected() { + connect_to_inbox(context, &inbox); + if !inbox.is_connected() { dc_job_try_again_later(job, 3i32, 0 as *const libc::c_char); current_block = 2670689566614003383; } else { @@ -591,14 +592,8 @@ unsafe fn dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP( } match current_block { 11006700562992250127 => { - if dc_imap_set_seen( - context, - &mut context.inbox.clone().lock().unwrap(), - folder, - uid, - ) as libc::c_uint - == 0i32 as libc::c_uint - { + let folder = CStr::from_ptr(folder).to_str().unwrap(); + if inbox.set_seen(context, folder, uid) as libc::c_uint == 0i32 as libc::c_uint { dc_job_try_again_later(job, 3i32, 0 as *const libc::c_char); } if 0 != dc_param_get_int((*job).param, 'M' as i32, 0i32) { @@ -609,11 +604,7 @@ unsafe fn dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP( 0i32, ) < 3i32 { - dc_configure_folders( - context, - &mut context.inbox.clone().lock().unwrap(), - 0x1i32, - ); + inbox.configure_folders(context, 0x1i32); } dest_folder = dc_sqlite3_get_config( context, @@ -621,15 +612,8 @@ unsafe fn dc_job_do_DC_JOB_MARKSEEN_MDN_ON_IMAP( b"configured_mvbox_folder\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); - match dc_imap_move( - context, - &mut context.inbox.clone().lock().unwrap(), - folder, - uid, - dest_folder, - &mut dest_uid, - ) as libc::c_uint - { + let dest_folder = CStr::from_ptr(dest_folder).to_str().unwrap(); + match inbox.mv(context, folder, uid, dest_folder, &mut dest_uid) as libc::c_uint { 1 => { dc_job_try_again_later(job, 3i32, 0 as *const libc::c_char); } @@ -648,9 +632,11 @@ unsafe fn dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP( ) { let mut current_block: u64; let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); - if 0 == dc_imap_is_connected(&mut context.inbox.clone().lock().unwrap()) { - connect_to_inbox(context); - if 0 == dc_imap_is_connected(&mut context.inbox.clone().lock().unwrap()) { + let inbox = context.inbox.read().unwrap(); + + if !inbox.is_connected() { + connect_to_inbox(context, &inbox); + if !inbox.is_connected() { dc_job_try_again_later(job, 3i32, 0 as *const libc::c_char); current_block = 17792648348530113339; } else { @@ -662,13 +648,8 @@ unsafe fn dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP( match current_block { 15240798224410183470 => { if !(0 == dc_msg_load_from_db(msg, context, (*job).foreign_id)) { - match dc_imap_set_seen( - context, - &mut context.inbox.clone().lock().unwrap(), - (*msg).server_folder, - (*msg).server_uid, - ) as libc::c_uint - { + let server_folder = CStr::from_ptr((*msg).server_folder).to_str().unwrap(); + match inbox.set_seen(context, server_folder, (*msg).server_uid) as libc::c_uint { 0 => {} 1 => { current_block = 12392248546350854223; @@ -685,12 +666,10 @@ unsafe fn dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP( 1i32, ) { - match dc_imap_set_mdnsent( - context, - &mut context.inbox.clone().lock().unwrap(), - (*msg).server_folder, - (*msg).server_uid, - ) as libc::c_uint + let folder = + CStr::from_ptr((*msg).server_folder).to_str().unwrap(); + match inbox.set_mdnsent(context, folder, (*msg).server_uid) + as libc::c_uint { 1 => { current_block = 4016212065805849280; @@ -743,12 +722,11 @@ unsafe fn dc_job_do_DC_JOB_MARKSEEN_MSG_ON_IMAP( 1i32, ) { - match dc_imap_set_mdnsent( - context, - &mut context.inbox.clone().lock().unwrap(), - (*msg).server_folder, - (*msg).server_uid, - ) as libc::c_uint + let folder = + CStr::from_ptr((*msg).server_folder).to_str().unwrap(); + + match inbox.set_mdnsent(context, folder, (*msg).server_uid) + as libc::c_uint { 1 => { current_block = 4016212065805849280; @@ -840,7 +818,7 @@ unsafe fn dc_add_smtp_job( mut action: libc::c_int, mut mimefactory: *mut dc_mimefactory_t, ) -> libc::c_int { - let mut pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char; + let mut pathNfilename: *mut libc::c_char; let mut success: libc::c_int = 0i32; let mut recipients: *mut libc::c_char = 0 as *mut libc::c_char; let mut param: *mut dc_param_t = dc_param_new(); @@ -907,8 +885,8 @@ pub unsafe fn dc_job_add( mut delay_seconds: libc::c_int, ) { let mut timestamp: time_t = time(0 as *mut time_t); - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - let mut thread: libc::c_int = 0i32; + let mut stmt: *mut sqlite3_stmt; + let mut thread: libc::c_int; if action >= 100i32 && action < 100i32 + 1000i32 { thread = 100i32 } else if action >= 5000i32 && action < 5000i32 + 1000i32 { @@ -965,21 +943,25 @@ pub unsafe fn dc_interrupt_smtp_idle(mut context: &dc_context_t) { cvar.notify_one(); } -pub unsafe fn dc_interrupt_imap_idle(mut context: &dc_context_t) { +pub unsafe fn dc_interrupt_imap_idle(context: &dc_context_t) { dc_log_info( context, 0i32, b"Interrupting IMAP-IDLE...\x00" as *const u8 as *const libc::c_char, ); - *context.perform_inbox_jobs_needed.clone().write().unwrap() = 1; - dc_imap_interrupt_idle(&mut context.inbox.clone().lock().unwrap()); + println!("inbox lock"); + *context.perform_inbox_jobs_needed.write().unwrap() = 1; + println!("imap lock"); + context.inbox.read().unwrap().interrupt_idle(); } unsafe fn dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(mut context: &dc_context_t, mut job: *mut dc_job_t) { let mut current_block: u64; let mut delete_from_server: libc::c_int = 1i32; let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); + let inbox = context.inbox.read().unwrap(); + if !(0 == dc_msg_load_from_db(msg, context, (*job).foreign_id) || (*msg).rfc724_mid.is_null() || *(*msg).rfc724_mid.offset(0isize) as libc::c_int == 0i32) @@ -996,9 +978,9 @@ unsafe fn dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(mut context: &dc_context_t, mut jo } /* if this is the last existing part of the message, we delete the message from the server */ if 0 != delete_from_server { - if 0 == dc_imap_is_connected(&mut context.inbox.clone().lock().unwrap()) { - connect_to_inbox(context); - if 0 == dc_imap_is_connected(&mut context.inbox.clone().lock().unwrap()) { + if !inbox.is_connected() { + connect_to_inbox(context, &inbox); + if !inbox.is_connected() { dc_job_try_again_later(job, 3i32, 0 as *const libc::c_char); current_block = 8913536887710889399; } else { @@ -1010,13 +992,9 @@ unsafe fn dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(mut context: &dc_context_t, mut jo match current_block { 8913536887710889399 => {} _ => { - if 0 == dc_imap_delete_msg( - context, - &mut context.inbox.clone().lock().unwrap(), - (*msg).rfc724_mid, - (*msg).server_folder, - (*msg).server_uid, - ) { + let mid = CStr::from_ptr((*msg).rfc724_mid).to_str().unwrap(); + let server_folder = CStr::from_ptr((*msg).server_folder).to_str().unwrap(); + if 0 == inbox.delete_msg(context, mid, server_folder, &mut (*msg).server_uid) { dc_job_try_again_later(job, -1i32, 0 as *const libc::c_char); current_block = 8913536887710889399; } else { @@ -1049,9 +1027,11 @@ pub unsafe fn dc_job_kill_action(mut context: &dc_context_t, mut action: libc::c sqlite3_finalize(stmt); } -pub unsafe fn dc_perform_imap_fetch(mut context: &dc_context_t) { +pub unsafe fn dc_perform_imap_fetch(context: &dc_context_t) { + let inbox = context.inbox.read().unwrap(); + let mut start: libc::clock_t = clock(); - if 0 == connect_to_inbox(context) { + if 0 == connect_to_inbox(context, &inbox) { return; } if dc_sqlite3_get_config_int( @@ -1073,14 +1053,14 @@ pub unsafe fn dc_perform_imap_fetch(mut context: &dc_context_t) { 0i32, b"INBOX-fetch started...\x00" as *const u8 as *const libc::c_char, ); - dc_imap_fetch(context, &mut context.inbox.clone().lock().unwrap()); - if 0 != context.inbox.clone().lock().unwrap().should_reconnect { + inbox.fetch(context); + if inbox.should_reconnect() { dc_log_info( context, 0i32, b"INBOX-fetch aborted, starting over...\x00" as *const u8 as *const libc::c_char, ); - dc_imap_fetch(context, &mut context.inbox.clone().lock().unwrap()); + inbox.fetch(context); } dc_log_info( context, @@ -1089,8 +1069,11 @@ pub unsafe fn dc_perform_imap_fetch(mut context: &dc_context_t) { clock().wrapping_sub(start) as libc::c_double * 1000.0f64 / 1000000i32 as libc::c_double, ); } + pub unsafe fn dc_perform_imap_idle(context: &dc_context_t) { - connect_to_inbox(context); + let inbox = context.inbox.read().unwrap(); + + connect_to_inbox(context, &inbox); if 0 != *context.perform_inbox_jobs_needed.clone().read().unwrap() { dc_log_info( @@ -1106,7 +1089,7 @@ pub unsafe fn dc_perform_imap_idle(context: &dc_context_t) { 0i32, b"INBOX-IDLE started...\x00" as *const u8 as *const libc::c_char, ); - dc_imap_idle(context, &mut context.inbox.clone().lock().unwrap()); + inbox.idle(context); dc_log_info( context, 0i32, @@ -1304,8 +1287,8 @@ pub unsafe fn dc_job_action_exists( mut context: &dc_context_t, mut action: libc::c_int, ) -> libc::c_int { - let mut job_exists: libc::c_int = 0i32; - let mut stmt = 0 as *mut sqlite3_stmt; + let mut job_exists: libc::c_int; + let mut stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), diff --git a/src/dc_jobthread.rs b/src/dc_jobthread.rs index c911c43f5..d223c7a5e 100644 --- a/src/dc_jobthread.rs +++ b/src/dc_jobthread.rs @@ -1,11 +1,8 @@ use std::sync::{Arc, Condvar, Mutex}; -use libc; - use crate::dc_configure::*; use crate::dc_context::dc_context_t; use crate::dc_imap::dc_imap_t; -use crate::dc_imap::*; use crate::dc_log::*; use crate::dc_sqlite3::*; use crate::dc_tools::*; @@ -104,7 +101,8 @@ pub unsafe extern "C" fn dc_jobthread_interrupt_idle( jobthread.name, ); - dc_imap_interrupt_idle(&mut jobthread.imap.clone().lock().unwrap()); + println!("jobthread interrupt, waiting for lock"); + jobthread.imap.lock().unwrap().interrupt_idle(); let &(ref lock, ref cvar) = &*jobthread.state.clone(); let mut state = lock.lock().unwrap(); @@ -118,7 +116,7 @@ pub unsafe fn dc_jobthread_fetch( jobthread: &mut dc_jobthread_t, use_network: libc::c_int, ) { - let mut start = 0; + let mut start; { let &(ref lock, _) = &*jobthread.state.clone(); @@ -140,16 +138,16 @@ pub unsafe fn dc_jobthread_fetch( b"%s-fetch started...\x00" as *const u8 as *const libc::c_char, jobthread.name, ); - dc_imap_fetch(context, &mut jobthread.imap.clone().lock().unwrap()); + jobthread.imap.lock().unwrap().fetch(context); - if 0 != jobthread.imap.clone().lock().unwrap().should_reconnect { + if jobthread.imap.lock().unwrap().should_reconnect() { dc_log_info( context, 0i32, b"%s-fetch aborted, starting over...\x00" as *const u8 as *const libc::c_char, jobthread.name, ); - dc_imap_fetch(context, &mut jobthread.imap.clone().lock().unwrap()); + jobthread.imap.lock().unwrap().fetch(context); } dc_log_info( context, @@ -170,10 +168,10 @@ pub unsafe fn dc_jobthread_fetch( ******************************************************************************/ unsafe fn connect_to_imap(context: &dc_context_t, jobthread: &mut dc_jobthread_t) -> libc::c_int { - let mut ret_connected: libc::c_int = 0i32; + let mut ret_connected: libc::c_int; let mut mvbox_name: *mut libc::c_char = 0 as *mut libc::c_char; - if 0 != dc_imap_is_connected(&mut jobthread.imap.clone().lock().unwrap()) { + if jobthread.imap.lock().unwrap().is_connected() { ret_connected = 1 } else { ret_connected = @@ -186,7 +184,11 @@ unsafe fn connect_to_imap(context: &dc_context_t, jobthread: &mut dc_jobthread_t 0, ) < 3 { - dc_configure_folders(context, &mut jobthread.imap.clone().lock().unwrap(), 0x1); + jobthread + .imap + .lock() + .unwrap() + .configure_folders(context, 0x1); } mvbox_name = dc_sqlite3_get_config( context, @@ -195,10 +197,10 @@ unsafe fn connect_to_imap(context: &dc_context_t, jobthread: &mut dc_jobthread_t 0 as *const libc::c_char, ); if mvbox_name.is_null() { - dc_imap_disconnect(context, &mut jobthread.imap.clone().lock().unwrap()); + jobthread.imap.lock().unwrap().disconnect(context); ret_connected = 0; } else { - dc_imap_set_watch_folder(&mut jobthread.imap.clone().lock().unwrap(), mvbox_name); + jobthread.imap.lock().unwrap().set_watch_folder(mvbox_name); } } } @@ -256,7 +258,7 @@ pub unsafe fn dc_jobthread_idle( b"%s-IDLE started...\x00" as *const u8 as *const libc::c_char, jobthread.name, ); - dc_imap_idle(context, &mut jobthread.imap.clone().lock().unwrap()); + jobthread.imap.lock().unwrap().idle(context); dc_log_info( context, 0i32, diff --git a/src/dc_jsmn.rs b/src/dc_jsmn.rs index cd5d8267b..4db16f99c 100644 --- a/src/dc_jsmn.rs +++ b/src/dc_jsmn.rs @@ -1,5 +1,3 @@ -use libc; - use crate::types::*; /* @@ -68,6 +66,7 @@ pub struct jsmn_parser { pub toknext: libc::c_uint, pub toksuper: libc::c_int, } + /* * * Create JSON parser over an array of tokens */ @@ -76,6 +75,7 @@ pub unsafe fn jsmn_init(mut parser: *mut jsmn_parser) { (*parser).toknext = 0i32 as libc::c_uint; (*parser).toksuper = -1i32; } + /* * * Run JSON parser. It parses a JSON data string into and array of tokens, each describing * a single JSON object. @@ -87,15 +87,15 @@ pub unsafe fn jsmn_parse( mut tokens: *mut jsmntok_t, mut num_tokens: libc::c_uint, ) -> libc::c_int { - let mut r: libc::c_int = 0; - let mut i: libc::c_int = 0; - let mut token: *mut jsmntok_t = 0 as *mut jsmntok_t; + let mut r: libc::c_int; + let mut i: libc::c_int; + let mut token: *mut jsmntok_t; let mut count: libc::c_int = (*parser).toknext as libc::c_int; while (*parser).pos < len as libc::c_uint && *js.offset((*parser).pos as isize) as libc::c_int != '\u{0}' as i32 { - let mut c: libc::c_char = 0; - let mut type_0: jsmntype_t = JSMN_UNDEFINED; + let mut c: libc::c_char; + let mut type_0: jsmntype_t; c = *js.offset((*parser).pos as isize); match c as libc::c_int { 123 | 91 => { @@ -222,8 +222,10 @@ pub unsafe fn jsmn_parse( i -= 1 } } - return count; + + count } + /* * * Fills next available token with JSON primitive. */ @@ -234,8 +236,8 @@ unsafe fn jsmn_parse_primitive( mut tokens: *mut jsmntok_t, mut num_tokens: size_t, ) -> libc::c_int { - let mut token: *mut jsmntok_t = 0 as *mut jsmntok_t; - let mut start: libc::c_int = 0; + let mut token: *mut jsmntok_t; + let mut start: libc::c_int; start = (*parser).pos as libc::c_int; while (*parser).pos < len as libc::c_uint && *js.offset((*parser).pos as isize) as libc::c_int != '\u{0}' as i32 @@ -265,8 +267,10 @@ unsafe fn jsmn_parse_primitive( } jsmn_fill_token(token, JSMN_PRIMITIVE, start, (*parser).pos as libc::c_int); (*parser).pos = (*parser).pos.wrapping_sub(1); - return 0i32; + + 0 } + /* * * Fills token type and boundaries. */ @@ -281,6 +285,7 @@ unsafe fn jsmn_fill_token( (*token).end = end; (*token).size = 0i32; } + /* Copyright (c) 2010 Serge A. Zaitsev @@ -302,6 +307,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + /* * * Allocates a fresh unused token from the token pool. */ @@ -310,7 +316,7 @@ unsafe fn jsmn_alloc_token( mut tokens: *mut jsmntok_t, mut num_tokens: size_t, ) -> *mut jsmntok_t { - let mut tok: *mut jsmntok_t = 0 as *mut jsmntok_t; + let mut tok: *mut jsmntok_t; if (*parser).toknext as size_t >= num_tokens { return 0 as *mut jsmntok_t; } @@ -320,8 +326,10 @@ unsafe fn jsmn_alloc_token( (*tok).end = -1i32; (*tok).start = (*tok).end; (*tok).size = 0i32; - return tok; + + tok } + /* * * Fills next token with JSON string. */ @@ -332,7 +340,7 @@ unsafe fn jsmn_parse_string( mut tokens: *mut jsmntok_t, mut num_tokens: size_t, ) -> libc::c_int { - let mut token: *mut jsmntok_t = 0 as *mut jsmntok_t; + let mut token: *mut jsmntok_t; let mut start: libc::c_int = (*parser).pos as libc::c_int; (*parser).pos = (*parser).pos.wrapping_add(1); while ((*parser).pos as size_t) < len @@ -357,7 +365,7 @@ unsafe fn jsmn_parse_string( return 0i32; } if c as libc::c_int == '\\' as i32 && ((*parser).pos.wrapping_add(1) as size_t) < len { - let mut i: libc::c_int = 0; + let mut i: libc::c_int; (*parser).pos = (*parser).pos.wrapping_add(1); match *js.offset((*parser).pos as isize) as libc::c_int { 34 | 47 | 92 | 98 | 102 | 114 | 110 | 116 => {} @@ -392,5 +400,6 @@ unsafe fn jsmn_parse_string( (*parser).pos = (*parser).pos.wrapping_add(1) } (*parser).pos = start as libc::c_uint; - return JSMN_ERROR_PART as libc::c_int; + + JSMN_ERROR_PART as libc::c_int } diff --git a/src/dc_key.rs b/src/dc_key.rs index 37e7412a2..f7b0a6884 100644 --- a/src/dc_key.rs +++ b/src/dc_key.rs @@ -1,4 +1,15 @@ +use mmime::mailmime_content::*; +use mmime::mmapstring::*; +use mmime::other::*; + +use std::collections::BTreeMap; +use std::ffi::CString; +use std::io::Cursor; +use std::slice; + use libc; +use pgp::composed::{Deserializable, SignedPublicKey, SignedSecretKey}; +use pgp::ser::Serialize; use crate::dc_context::dc_context_t; use crate::dc_log::*; @@ -8,9 +19,7 @@ use crate::dc_strbuilder::*; use crate::dc_tools::*; use crate::types::*; use crate::x::*; -/* * - * Library-internal. - */ + #[derive(Copy, Clone)] #[repr(C)] pub struct dc_key_t { @@ -26,21 +35,25 @@ pub unsafe fn toupper(mut _c: libc::c_int) -> libc::c_int { } pub unsafe fn dc_key_new() -> *mut dc_key_t { - let mut key: *mut dc_key_t = 0 as *mut dc_key_t; + let mut key: *mut dc_key_t; key = calloc(1, ::std::mem::size_of::()) as *mut dc_key_t; if key.is_null() { exit(44i32); } (*key)._m_heap_refcnt = 1i32; - return key; + + key } + pub unsafe fn dc_key_ref(mut key: *mut dc_key_t) -> *mut dc_key_t { if key.is_null() { return 0 as *mut dc_key_t; } (*key)._m_heap_refcnt += 1; - return key; + + key } + pub unsafe fn dc_key_unref(mut key: *mut dc_key_t) { if key.is_null() { return; @@ -52,6 +65,7 @@ pub unsafe fn dc_key_unref(mut key: *mut dc_key_t) { dc_key_empty(key); free(key as *mut libc::c_void); } + unsafe fn dc_key_empty(mut key: *mut dc_key_t) { if key.is_null() { return; @@ -64,12 +78,15 @@ unsafe fn dc_key_empty(mut key: *mut dc_key_t) { (*key).bytes = 0i32; (*key).type_0 = 0i32; } + pub unsafe fn dc_wipe_secret_mem(mut buf: *mut libc::c_void, mut buf_bytes: size_t) { if buf.is_null() || buf_bytes <= 0 { return; } memset(buf, 0i32, buf_bytes); } + +// TODO should return bool /rtn pub unsafe fn dc_key_set_from_binary( mut key: *mut dc_key_t, mut data: *const libc::c_void, @@ -87,15 +104,20 @@ pub unsafe fn dc_key_set_from_binary( memcpy((*key).binary, data, bytes as size_t); (*key).bytes = bytes; (*key).type_0 = type_0; - return 1i32; + + 1 } + pub unsafe fn dc_key_set_from_key(mut key: *mut dc_key_t, mut o: *const dc_key_t) -> libc::c_int { dc_key_empty(key); if key.is_null() || o.is_null() { return 0i32; } - return dc_key_set_from_binary(key, (*o).binary, (*o).bytes, (*o).type_0); + + dc_key_set_from_binary(key, (*o).binary, (*o).bytes, (*o).type_0) } + +// TODO should return bool /rtn pub unsafe extern "C" fn dc_key_set_from_stmt( mut key: *mut dc_key_t, mut stmt: *mut sqlite3_stmt, @@ -106,13 +128,16 @@ pub unsafe extern "C" fn dc_key_set_from_stmt( if key.is_null() || stmt.is_null() { return 0i32; } - return dc_key_set_from_binary( + + dc_key_set_from_binary( key, sqlite3_column_blob(stmt, index) as *mut libc::c_uchar as *const libc::c_void, sqlite3_column_bytes(stmt, index), type_0, - ); + ) } + +// TODO should return bool /rtn pub unsafe fn dc_key_set_from_base64( mut key: *mut dc_key_t, mut base64: *const libc::c_char, @@ -144,8 +169,11 @@ pub unsafe fn dc_key_set_from_base64( type_0, ); mmap_string_unref(result); - return 1i32; + + 1 } + +// TODO should return bool /rtn pub unsafe fn dc_key_equals(mut key: *const dc_key_t, mut o: *const dc_key_t) -> libc::c_int { if key.is_null() || o.is_null() @@ -169,6 +197,8 @@ pub unsafe fn dc_key_equals(mut key: *const dc_key_t, mut o: *const dc_key_t) -> 0 } } + +// TODO should return bool /rtn pub unsafe fn dc_key_save_self_keypair( context: &dc_context_t, public_key: *const dc_key_t, @@ -207,8 +237,11 @@ pub unsafe fn dc_key_save_self_keypair( } } sqlite3_finalize(stmt); - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_key_load_self_public( context: &dc_context_t, key: *mut dc_key_t, @@ -232,8 +265,11 @@ pub unsafe fn dc_key_load_self_public( } } sqlite3_finalize(stmt); - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_key_load_self_private( context: &dc_context_t, key: *mut dc_key_t, @@ -253,139 +289,87 @@ pub unsafe fn dc_key_load_self_private( sqlite3_bind_text(stmt, 1i32, self_addr, -1i32, None); if !(sqlite3_step(stmt) != 100i32) { dc_key_set_from_stmt(key, stmt, 0i32, 1i32); - success = 1i32 + success = 1i32; } } sqlite3_finalize(stmt); - return success; + + success } + /* the result must be freed */ -pub unsafe fn dc_render_base64( - mut buf: *const libc::c_void, - mut buf_bytes: size_t, - mut break_every: libc::c_int, - mut break_chars: *const libc::c_char, - mut add_checksum: libc::c_int, -) -> *mut libc::c_char { - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; - if !(buf == 0 as *mut libc::c_void || buf_bytes <= 0) { - ret = encode_base64(buf as *const libc::c_char, buf_bytes as libc::c_int); - if !ret.is_null() { - if break_every > 0i32 { - let mut temp: *mut libc::c_char = ret; - ret = dc_insert_breaks(temp, break_every, break_chars); - free(temp as *mut libc::c_void); - } - if add_checksum == 2i32 { - let mut checksum: libc::c_long = crc_octets(buf as *const libc::c_uchar, buf_bytes); - let mut c: [uint8_t; 3] = [0; 3]; - c[0usize] = (checksum >> 16i32 & 0xffi32 as libc::c_long) as uint8_t; - c[1usize] = (checksum >> 8i32 & 0xffi32 as libc::c_long) as uint8_t; - c[2usize] = (checksum & 0xffi32 as libc::c_long) as uint8_t; - let mut c64: *mut libc::c_char = - encode_base64(c.as_mut_ptr() as *const libc::c_char, 3i32); - let mut temp_0: *mut libc::c_char = ret; - ret = dc_mprintf( - b"%s%s=%s\x00" as *const u8 as *const libc::c_char, - temp_0, - break_chars, - c64, - ); - free(temp_0 as *mut libc::c_void); - free(c64 as *mut libc::c_void); - } - } - } - return ret; -} -/* ****************************************************************************** - * Render keys - ******************************************************************************/ -unsafe fn crc_octets(mut octets: *const libc::c_uchar, mut len: size_t) -> libc::c_long { - let mut crc: libc::c_long = 0xb704ce; - loop { - let fresh0 = len; - len = len.wrapping_sub(1); - if !(0 != fresh0) { - break; - } - let fresh1 = octets; - octets = octets.offset(1); - crc ^= ((*fresh1 as libc::c_int) << 16i32) as libc::c_long; - let mut i: libc::c_int = 0i32; - while i < 8i32 { - crc <<= 1i32; - if 0 != crc & 0x1000000 as libc::c_long { - crc ^= 0x1864cfb - } - i += 1 - } - } - return crc & 0xffffff; -} -/* the result must be freed */ -pub unsafe fn dc_key_render_base64( - mut key: *const dc_key_t, - mut break_every: libc::c_int, - mut break_chars: *const libc::c_char, - mut add_checksum: libc::c_int, -) -> *mut libc::c_char { +pub fn dc_key_render_base64(key: *const dc_key_t, break_every: usize) -> *mut libc::c_char { if key.is_null() { - return 0 as *mut libc::c_char; + return std::ptr::null_mut(); } - return dc_render_base64( - (*key).binary, - (*key).bytes as size_t, - break_every, - break_chars, - add_checksum, - ); + + let key = unsafe { *key }; + let bytes = unsafe { slice::from_raw_parts(key.binary as *const u8, key.bytes as usize) }; + assert_eq!(bytes.len(), key.bytes as usize); + + let buf = if key.type_0 == 0 { + // public key + let skey = SignedPublicKey::from_bytes(Cursor::new(bytes)).expect("invalid pub key"); + skey.to_bytes().expect("failed to serialize key") + } else { + // secret key + let skey = SignedSecretKey::from_bytes(Cursor::new(bytes)).expect("invalid sec key"); + skey.to_bytes().expect("failed to serialize key") + }; + + let encoded = base64::encode(&buf); + let res = encoded + .as_bytes() + .chunks(break_every) + .fold(String::new(), |mut res, buf| { + // safe because we are using a base64 encoded string + res += unsafe { std::str::from_utf8_unchecked(buf) }; + res += " "; + res + }); + + let res_c = CString::new(res.trim()).unwrap(); + + // need to use strdup to allocate the result with malloc + // so it can be `free`d later. + unsafe { libc::strdup(res_c.as_ptr()) } } -/* each header line must be terminated by \r\n, the result must be freed */ -pub unsafe fn dc_key_render_asc( - mut key: *const dc_key_t, - mut add_header_lines: *const libc::c_char, -) -> *mut libc::c_char { - /* see RFC 4880, 6.2. Forming ASCII Armor, https://tools.ietf.org/html/rfc4880#section-6.2 */ - let mut base64: *mut libc::c_char = 0 as *mut libc::c_char; - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; - if !key.is_null() { - base64 = dc_key_render_base64( - key, - 76i32, - b"\r\n\x00" as *const u8 as *const libc::c_char, - 2i32, - ); - if !base64.is_null() { - /*checksum in new line*/ - /* RFC: The encoded output stream must be represented in lines of no more than 76 characters each. */ - ret = - dc_mprintf(b"-----BEGIN PGP %s KEY BLOCK-----\r\n%s\r\n%s\r\n-----END PGP %s KEY BLOCK-----\r\n\x00" - as *const u8 as *const libc::c_char, - if (*key).type_0 == 0i32 { - b"PUBLIC\x00" as *const u8 as - *const libc::c_char - } else { - b"PRIVATE\x00" as *const u8 as - *const libc::c_char - }, - if !add_header_lines.is_null() { - add_header_lines - } else { - b"\x00" as *const u8 as *const libc::c_char - }, base64, - if (*key).type_0 == 0i32 { - b"PUBLIC\x00" as *const u8 as - *const libc::c_char - } else { - b"PRIVATE\x00" as *const u8 as - *const libc::c_char - }) - } + +/// each header line must be terminated by `\r\n`, the result must be freed. +pub fn dc_key_render_asc(key: *const dc_key_t, header: Option<(&str, &str)>) -> *mut libc::c_char { + if key.is_null() { + return std::ptr::null_mut(); } - free(base64 as *mut libc::c_void); - return ret; + + let key = unsafe { *key }; + + let headers = header.map(|(key, value)| { + let mut m = BTreeMap::new(); + m.insert(key.to_string(), value.to_string()); + m + }); + + let bytes = unsafe { slice::from_raw_parts(key.binary as *const u8, key.bytes as usize) }; + + let buf = if key.type_0 == 0 { + // public key + let skey = SignedPublicKey::from_bytes(Cursor::new(bytes)).expect("invalid key"); + skey.to_armored_string(headers.as_ref()) + .expect("failed to serialize key") + } else { + // secret key + let skey = SignedSecretKey::from_bytes(Cursor::new(bytes)).expect("invalid key"); + skey.to_armored_string(headers.as_ref()) + .expect("failed to serialize key") + }; + + let buf_c = CString::new(buf).unwrap(); + + // need to use strdup to allocate the result with malloc + // so it can be `free`d later. + unsafe { libc::strdup(buf_c.as_ptr()) } } + pub unsafe fn dc_key_render_asc_to_file( mut key: *const dc_key_t, mut file: *const libc::c_char, @@ -393,8 +377,10 @@ pub unsafe fn dc_key_render_asc_to_file( ) -> libc::c_int { let mut success: libc::c_int = 0i32; let mut file_content: *mut libc::c_char = 0 as *mut libc::c_char; + if !(key.is_null() || file.is_null()) { - file_content = dc_key_render_asc(key, 0 as *const libc::c_char); + file_content = dc_key_render_asc(key, None); + if !file_content.is_null() { if 0 == dc_write_file( context, @@ -414,8 +400,10 @@ pub unsafe fn dc_key_render_asc_to_file( } } free(file_content as *mut libc::c_void); - return success; + + success } + pub unsafe fn dc_format_fingerprint(mut fingerprint: *const libc::c_char) -> *mut libc::c_char { let mut i: libc::c_int = 0i32; let mut fingerprint_len: libc::c_int = strlen(fingerprint) as libc::c_int; @@ -441,8 +429,10 @@ pub unsafe fn dc_format_fingerprint(mut fingerprint: *const libc::c_char) -> *mu } } } - return ret.buf; + + ret.buf } + pub unsafe fn dc_normalize_fingerprint(mut in_0: *const libc::c_char) -> *mut libc::c_char { if in_0.is_null() { return 0 as *mut libc::c_char; @@ -468,8 +458,10 @@ pub unsafe fn dc_normalize_fingerprint(mut in_0: *const libc::c_char) -> *mut li } p1 = p1.offset(1isize) } - return out.buf; + + out.buf } + pub unsafe fn dc_key_get_fingerprint( context: &dc_context_t, key: *const dc_key_t, @@ -491,6 +483,7 @@ pub unsafe fn dc_key_get_fingerprint( dc_strdup(0 as *const libc::c_char) }; } + pub unsafe fn dc_key_get_formatted_fingerprint( context: &dc_context_t, key: *const dc_key_t, @@ -498,5 +491,6 @@ pub unsafe fn dc_key_get_formatted_fingerprint( let mut rawhex: *mut libc::c_char = dc_key_get_fingerprint(context, key); let mut formatted: *mut libc::c_char = dc_format_fingerprint(rawhex); free(rawhex as *mut libc::c_void); - return formatted; + + formatted } diff --git a/src/dc_keyhistory.rs b/src/dc_keyhistory.rs index a83fb888e..beb077042 100644 --- a/src/dc_keyhistory.rs +++ b/src/dc_keyhistory.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_context::dc_context_t; use crate::types::*; diff --git a/src/dc_keyring.rs b/src/dc_keyring.rs index 38cc40cd9..81bc280ab 100644 --- a/src/dc_keyring.rs +++ b/src/dc_keyring.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_context::dc_context_t; use crate::dc_key::*; use crate::dc_sqlite3::*; @@ -15,13 +13,15 @@ pub struct dc_keyring_t { } pub unsafe fn dc_keyring_new() -> *mut dc_keyring_t { - let mut keyring: *mut dc_keyring_t = 0 as *mut dc_keyring_t; + let mut keyring: *mut dc_keyring_t; keyring = calloc(1, ::std::mem::size_of::()) as *mut dc_keyring_t; if keyring.is_null() { exit(42i32); } - return keyring; + + keyring } + pub unsafe fn dc_keyring_unref(mut keyring: *mut dc_keyring_t) { if keyring.is_null() { return; @@ -34,6 +34,7 @@ pub unsafe fn dc_keyring_unref(mut keyring: *mut dc_keyring_t) { free((*keyring).keys as *mut libc::c_void); free(keyring as *mut libc::c_void); } + /* the reference counter of the key is increased by one */ pub unsafe fn dc_keyring_add(mut keyring: *mut dc_keyring_t, mut to_add: *mut dc_key_t) { if keyring.is_null() || to_add.is_null() { @@ -54,12 +55,15 @@ pub unsafe fn dc_keyring_add(mut keyring: *mut dc_keyring_t, mut to_add: *mut dc *fresh0 = dc_key_ref(to_add); (*keyring).count += 1; } + +// TODO should return bool? /rtn pub unsafe fn dc_keyring_load_self_private_for_decrypting( context: &dc_context_t, keyring: *mut dc_keyring_t, self_addr: *const libc::c_char, sql: &dc_sqlite3_t, ) -> libc::c_int { + // Can we prevent keyring and self_addr to be null? if keyring.is_null() || self_addr.is_null() { return 0i32; } @@ -78,5 +82,6 @@ pub unsafe fn dc_keyring_load_self_private_for_decrypting( dc_key_unref(key); } sqlite3_finalize(stmt); - return 1i32; + + 1 } diff --git a/src/dc_location.rs b/src/dc_location.rs index 42dd51c79..8e5d820a4 100644 --- a/src/dc_location.rs +++ b/src/dc_location.rs @@ -1,5 +1,3 @@ -use libc; - use crate::constants::Event; use crate::dc_array::*; use crate::dc_chat::*; @@ -50,7 +48,7 @@ pub unsafe fn dc_send_locations_to_chat( let mut now: time_t = time(0 as *mut time_t); let mut msg: *mut dc_msg_t = 0 as *mut dc_msg_t; let mut stock_str: *mut libc::c_char = 0 as *mut libc::c_char; - let mut is_sending_locations_before: libc::c_int = 0i32; + let mut is_sending_locations_before: libc::c_int; if !(seconds < 0i32 || chat_id <= 9i32 as libc::c_uint) { is_sending_locations_before = dc_is_sending_locations_to_chat(context, chat_id); stmt = @@ -121,7 +119,8 @@ pub unsafe fn dc_send_locations_to_chat( dc_msg_unref(msg); sqlite3_finalize(stmt); } -/* ****************************************************************************** + +/******************************************************************************* * job to send locations out to all chats that want them ******************************************************************************/ unsafe fn schedule_MAYBE_SEND_LOCATIONS(mut context: &dc_context_t, mut flags: libc::c_int) { @@ -129,12 +128,13 @@ unsafe fn schedule_MAYBE_SEND_LOCATIONS(mut context: &dc_context_t, mut flags: l dc_job_add(context, 5005i32, 0i32, 0 as *const libc::c_char, 60i32); }; } + pub unsafe extern "C" fn dc_is_sending_locations_to_chat( mut context: &dc_context_t, mut chat_id: uint32_t, ) -> libc::c_int { let mut is_sending_locations: libc::c_int = 0i32; - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, @@ -158,8 +158,10 @@ pub unsafe extern "C" fn dc_is_sending_locations_to_chat( } sqlite3_finalize(stmt); - return is_sending_locations; + + is_sending_locations } + pub unsafe fn dc_set_location( mut context: &dc_context_t, mut latitude: libc::c_double, @@ -208,8 +210,10 @@ pub unsafe fn dc_set_location( } sqlite3_finalize(stmt_chats); sqlite3_finalize(stmt_insert); - return continue_streaming; + + continue_streaming } + pub unsafe fn dc_get_locations( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -218,7 +222,7 @@ pub unsafe fn dc_get_locations( mut timestamp_to: time_t, ) -> *mut dc_array_t { let mut ret: *mut dc_array_t = dc_array_new_typed(1i32, 500i32 as size_t); - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; if timestamp_to == 0i32 as libc::c_long { timestamp_to = time(0 as *mut time_t) + 10i32 as libc::c_long @@ -283,8 +287,11 @@ pub unsafe fn dc_get_locations( } sqlite3_finalize(stmt); - return ret; + + ret } + +// TODO should be bool /rtn unsafe fn is_marker(mut txt: *const libc::c_char) -> libc::c_int { if !txt.is_null() { let mut len: libc::c_int = dc_utf8_strlen(txt) as libc::c_int; @@ -292,10 +299,12 @@ unsafe fn is_marker(mut txt: *const libc::c_char) -> libc::c_int { return 1i32; } } - return 0i32; + + 0 } + pub unsafe fn dc_delete_all_locations(mut context: &dc_context_t) { - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, @@ -312,18 +321,19 @@ pub unsafe fn dc_delete_all_locations(mut context: &dc_context_t) { sqlite3_finalize(stmt); } + pub unsafe fn dc_get_location_kml( mut context: &dc_context_t, mut chat_id: uint32_t, mut last_added_location_id: *mut uint32_t, ) -> *mut libc::c_char { let mut success: libc::c_int = 0i32; - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - let mut self_addr: *mut libc::c_char = 0 as *mut libc::c_char; + let mut stmt: *mut sqlite3_stmt; + let mut self_addr: *mut libc::c_char; let mut now: time_t = time(0 as *mut time_t); - let mut locations_send_begin: time_t = 0i32 as time_t; - let mut locations_send_until: time_t = 0i32 as time_t; - let mut locations_last_sent: time_t = 0i32 as time_t; + let mut locations_send_begin: time_t; + let mut locations_send_until: time_t; + let mut locations_last_sent: time_t; let mut location_count: libc::c_int = 0i32; let mut ret: dc_strbuilder_t = dc_strbuilder_t { buf: 0 as *mut libc::c_char, @@ -416,7 +426,8 @@ pub unsafe fn dc_get_location_kml( 0 as *mut libc::c_char }; } -/* ****************************************************************************** + +/******************************************************************************* * create kml-files ******************************************************************************/ unsafe fn get_kml_timestamp(mut utc: time_t) -> *mut libc::c_char { @@ -439,7 +450,8 @@ unsafe fn get_kml_timestamp(mut utc: time_t) -> *mut libc::c_char { gmtime(&mut utc) as *const libc::c_void, ::std::mem::size_of::(), ); - return dc_mprintf( + + dc_mprintf( b"%04i-%02i-%02iT%02i:%02i:%02iZ\x00" as *const u8 as *const libc::c_char, wanted_struct.tm_year as libc::c_int + 1900i32, wanted_struct.tm_mon as libc::c_int + 1i32, @@ -447,7 +459,7 @@ unsafe fn get_kml_timestamp(mut utc: time_t) -> *mut libc::c_char { wanted_struct.tm_hour as libc::c_int, wanted_struct.tm_min as libc::c_int, wanted_struct.tm_sec as libc::c_int, - ); + ) } pub unsafe fn dc_get_message_kml( @@ -486,7 +498,7 @@ pub unsafe fn dc_set_kml_sent_timestamp( chat_id: uint32_t, timestamp: time_t, ) { - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -498,12 +510,13 @@ pub unsafe fn dc_set_kml_sent_timestamp( sqlite3_step(stmt); sqlite3_finalize(stmt); } + pub unsafe fn dc_set_msg_location_id( mut context: &dc_context_t, mut msg_id: uint32_t, mut location_id: uint32_t, ) { - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -514,6 +527,7 @@ pub unsafe fn dc_set_msg_location_id( sqlite3_step(stmt); sqlite3_finalize(stmt); } + pub unsafe fn dc_save_locations( mut context: &dc_context_t, mut chat_id: uint32_t, @@ -574,8 +588,10 @@ pub unsafe fn dc_save_locations( } sqlite3_finalize(stmt_test); sqlite3_finalize(stmt_insert); - return newest_location_id; + + newest_location_id } + pub unsafe fn dc_kml_parse( mut context: &dc_context_t, mut content: *const libc::c_char, @@ -614,7 +630,8 @@ pub unsafe fn dc_kml_parse( } free(content_nullterminated as *mut libc::c_void); - return kml; + + kml } unsafe fn kml_text_cb(userdata: *mut libc::c_void, text: *const libc::c_char, _len: libc::c_int) { @@ -693,6 +710,7 @@ unsafe fn kml_text_cb(userdata: *mut libc::c_void, text: *const libc::c_char, _l free(val as *mut libc::c_void); }; } + unsafe fn kml_endtag_cb(mut userdata: *mut libc::c_void, mut tag: *const libc::c_char) { let mut kml: *mut dc_kml_t = userdata as *mut dc_kml_t; if strcmp(tag, b"placemark\x00" as *const u8 as *const libc::c_char) == 0i32 { @@ -709,7 +727,8 @@ unsafe fn kml_endtag_cb(mut userdata: *mut libc::c_void, mut tag: *const libc::c (*kml).tag = 0i32 }; } -/* ****************************************************************************** + +/******************************************************************************* * parse kml-files ******************************************************************************/ unsafe fn kml_starttag_cb( @@ -753,6 +772,7 @@ unsafe fn kml_starttag_cb( } }; } + pub unsafe fn dc_kml_unref(mut kml: *mut dc_kml_t) { if kml.is_null() { return; @@ -761,8 +781,9 @@ pub unsafe fn dc_kml_unref(mut kml: *mut dc_kml_t) { free((*kml).addr as *mut libc::c_void); free(kml as *mut libc::c_void); } + pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &dc_context_t, _job: *mut dc_job_t) { - let mut stmt_chats: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt_chats: *mut sqlite3_stmt; let mut stmt_locations: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut now: time_t = time(0 as *mut time_t); let mut continue_streaming: libc::c_int = 1i32; @@ -833,6 +854,7 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &dc_context_t, _job sqlite3_finalize(stmt_chats); sqlite3_finalize(stmt_locations); } + pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED( mut context: &dc_context_t, mut job: &mut dc_job_t, @@ -841,9 +863,9 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED( // the function checks, if location-streaming is really ended; // if so, a device-message is added if not yet done. let mut chat_id: uint32_t = (*job).foreign_id; - let mut locations_send_begin: time_t = 0i32 as time_t; - let mut locations_send_until: time_t = 0i32 as time_t; - let mut stmt = 0 as *mut sqlite3_stmt; + let mut locations_send_begin: time_t; + let mut locations_send_until: time_t; + let mut stmt; let mut stock_str: *mut libc::c_char = 0 as *mut libc::c_char; stmt = dc_sqlite3_prepare( context, diff --git a/src/dc_log.rs b/src/dc_log.rs index d4022f604..82ef65866 100644 --- a/src/dc_log.rs +++ b/src/dc_log.rs @@ -1,5 +1,3 @@ -use libc; - use crate::constants::Event; use crate::dc_context::dc_context_t; use crate::dc_tools::*; @@ -15,6 +13,7 @@ pub unsafe extern "C" fn dc_log_event( ) { log_vprintf(context, event_code, data1, msg, va); } + /* Asynchronous "Thread-errors" are reported by the dc_log_error() function. These errors must be shown to the user by a bubble or so. @@ -29,7 +28,7 @@ unsafe fn log_vprintf( mut msg_format: *const libc::c_char, mut va_0: ::std::ffi::VaList, ) { - let mut msg: *mut libc::c_char = 0 as *mut libc::c_char; + let mut msg: *mut libc::c_char; if !msg_format.is_null() { let mut tempbuf: [libc::c_char; 1025] = [0; 1025]; vsnprintf( @@ -48,6 +47,7 @@ unsafe fn log_vprintf( ((*context).cb)(context, event, data1 as uintptr_t, msg as uintptr_t); free(msg as *mut libc::c_void); } + pub unsafe extern "C" fn dc_log_event_seq( mut context: &dc_context_t, mut event_code: Event, @@ -61,6 +61,7 @@ pub unsafe extern "C" fn dc_log_event_seq( log_vprintf(context, event_code, *sequence_start, msg, va_0); *sequence_start = 0i32; } + pub unsafe extern "C" fn dc_log_error( mut context: &dc_context_t, mut data1: libc::c_int, @@ -69,6 +70,7 @@ pub unsafe extern "C" fn dc_log_error( ) { log_vprintf(context, Event::ERROR, data1, msg, va_1); } + pub unsafe extern "C" fn dc_log_warning( mut context: &dc_context_t, mut data1: libc::c_int, @@ -77,6 +79,7 @@ pub unsafe extern "C" fn dc_log_warning( ) { log_vprintf(context, Event::WARNING, data1, msg, va_2); } + pub unsafe extern "C" fn dc_log_info( mut context: &dc_context_t, mut data1: libc::c_int, diff --git a/src/dc_loginparam.rs b/src/dc_loginparam.rs index a8860369e..b9cc29a87 100644 --- a/src/dc_loginparam.rs +++ b/src/dc_loginparam.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_context::dc_context_t; use crate::dc_sqlite3::*; use crate::dc_strbuilder::*; @@ -23,13 +21,15 @@ pub struct dc_loginparam_t { } pub unsafe fn dc_loginparam_new() -> *mut dc_loginparam_t { - let mut loginparam: *mut dc_loginparam_t = 0 as *mut dc_loginparam_t; + let mut loginparam: *mut dc_loginparam_t; loginparam = calloc(1, ::std::mem::size_of::()) as *mut dc_loginparam_t; if loginparam.is_null() { exit(22i32); } - return loginparam; + + loginparam } + pub unsafe fn dc_loginparam_unref(mut loginparam: *mut dc_loginparam_t) { if loginparam.is_null() { return; @@ -37,6 +37,7 @@ pub unsafe fn dc_loginparam_unref(mut loginparam: *mut dc_loginparam_t) { dc_loginparam_empty(loginparam); free(loginparam as *mut libc::c_void); } + /* clears all data and frees its memory. All pointers are NULL after this function is called. */ pub unsafe fn dc_loginparam_empty(mut loginparam: *mut dc_loginparam_t) { if loginparam.is_null() { @@ -60,6 +61,7 @@ pub unsafe fn dc_loginparam_empty(mut loginparam: *mut dc_loginparam_t) { (*loginparam).send_pw = 0 as *mut libc::c_char; (*loginparam).server_flags = 0i32; } + pub unsafe fn dc_loginparam_read( context: &dc_context_t, loginparam: *mut dc_loginparam_t, @@ -140,6 +142,7 @@ pub unsafe fn dc_loginparam_read( (*loginparam).server_flags = dc_sqlite3_get_config_int(context, sql, key, 0i32); sqlite3_free(key as *mut libc::c_void); } + pub unsafe fn dc_loginparam_write( context: &dc_context_t, loginparam: *const dc_loginparam_t, @@ -219,6 +222,7 @@ pub unsafe fn dc_loginparam_write( dc_sqlite3_set_config_int(context, sql, key, (*loginparam).server_flags); sqlite3_free(key as *mut libc::c_void); } + pub unsafe fn dc_loginparam_get_readable( mut loginparam: *const dc_loginparam_t, ) -> *mut libc::c_char { @@ -270,8 +274,10 @@ pub unsafe fn dc_loginparam_get_readable( flags_readable, ); free(flags_readable as *mut libc::c_void); - return ret; + + ret } + unsafe fn get_readable_flags(mut flags: libc::c_int) -> *mut libc::c_char { let mut strbuilder: dc_strbuilder_t = dc_strbuilder_t { buf: 0 as *mut libc::c_char, @@ -358,5 +364,6 @@ unsafe fn get_readable_flags(mut flags: libc::c_int) -> *mut libc::c_char { ); } dc_trim(strbuilder.buf); - return strbuilder.buf; + + strbuilder.buf } diff --git a/src/dc_lot.rs b/src/dc_lot.rs index 9fd319606..e548d47b4 100644 --- a/src/dc_lot.rs +++ b/src/dc_lot.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_chat::*; use crate::dc_contact::*; use crate::dc_context::dc_context_t; @@ -36,15 +34,17 @@ pub struct dc_lot_t { * NB: _Lot_ is used in the meaning _heap_ here. */ pub unsafe fn dc_lot_new() -> *mut dc_lot_t { - let mut lot: *mut dc_lot_t = 0 as *mut dc_lot_t; + let mut lot: *mut dc_lot_t; lot = calloc(1, ::std::mem::size_of::()) as *mut dc_lot_t; if lot.is_null() { exit(27i32); } (*lot).magic = 0x107107i32 as uint32_t; (*lot).text1_meaning = 0i32; - return lot; + + lot } + pub unsafe fn dc_lot_empty(mut lot: *mut dc_lot_t) { if lot.is_null() || (*lot).magic != 0x107107i32 as libc::c_uint { return; @@ -64,6 +64,7 @@ pub unsafe fn dc_lot_empty(mut lot: *mut dc_lot_t) { (*lot).state = 0i32; (*lot).id = 0i32 as uint32_t; } + pub unsafe fn dc_lot_unref(mut set: *mut dc_lot_t) { if set.is_null() || (*set).magic != 0x107107i32 as libc::c_uint { return; @@ -72,42 +73,55 @@ pub unsafe fn dc_lot_unref(mut set: *mut dc_lot_t) { (*set).magic = 0i32 as uint32_t; free(set as *mut libc::c_void); } + pub unsafe fn dc_lot_get_text1(mut lot: *const dc_lot_t) -> *mut libc::c_char { if lot.is_null() || (*lot).magic != 0x107107i32 as libc::c_uint { return 0 as *mut libc::c_char; } - return dc_strdup_keep_null((*lot).text1); + + dc_strdup_keep_null((*lot).text1) } + pub unsafe fn dc_lot_get_text2(mut lot: *const dc_lot_t) -> *mut libc::c_char { if lot.is_null() || (*lot).magic != 0x107107i32 as libc::c_uint { return 0 as *mut libc::c_char; } - return dc_strdup_keep_null((*lot).text2); + + dc_strdup_keep_null((*lot).text2) } + pub unsafe fn dc_lot_get_text1_meaning(mut lot: *const dc_lot_t) -> libc::c_int { if lot.is_null() || (*lot).magic != 0x107107i32 as libc::c_uint { return 0i32; } - return (*lot).text1_meaning; + + (*lot).text1_meaning } + pub unsafe fn dc_lot_get_state(mut lot: *const dc_lot_t) -> libc::c_int { if lot.is_null() || (*lot).magic != 0x107107i32 as libc::c_uint { return 0i32; } - return (*lot).state; + + (*lot).state } + pub unsafe fn dc_lot_get_id(mut lot: *const dc_lot_t) -> uint32_t { if lot.is_null() || (*lot).magic != 0x107107i32 as libc::c_uint { return 0i32 as uint32_t; } - return (*lot).id; + + (*lot).id } + pub unsafe fn dc_lot_get_timestamp(mut lot: *const dc_lot_t) -> time_t { if lot.is_null() || (*lot).magic != 0x107107i32 as libc::c_uint { return 0i32 as time_t; } - return (*lot).timestamp; + + (*lot).timestamp } + /* library-internal */ /* in practice, the user additionally cuts the string himself pixel-accurate */ pub unsafe fn dc_lot_fill( diff --git a/src/dc_mimefactory.rs b/src/dc_mimefactory.rs index 1e8fbc666..4b2355713 100644 --- a/src/dc_mimefactory.rs +++ b/src/dc_mimefactory.rs @@ -1,4 +1,11 @@ -use libc; +use mmime::mailimf_types::*; +use mmime::mailimf_types_helper::*; +use mmime::mailmime_disposition::*; +use mmime::mailmime_types::*; +use mmime::mailmime_types_helper::*; +use mmime::mailmime_write_mem::*; +use mmime::mmapstring::*; +use mmime::other::*; use crate::constants::VERSION; use crate::dc_chat::*; @@ -61,6 +68,7 @@ pub unsafe fn dc_mimefactory_init<'a>( ); (*factory).context = context; } + pub unsafe fn dc_mimefactory_empty(mut factory: *mut dc_mimefactory_t) { if factory.is_null() { return; @@ -101,6 +109,7 @@ pub unsafe fn dc_mimefactory_empty(mut factory: *mut dc_mimefactory_t) { (*factory).error = 0 as *mut libc::c_char; (*factory).timestamp = 0i32 as time_t; } + pub unsafe fn dc_mimefactory_load_msg( mut factory: *mut dc_mimefactory_t, mut msg_id: uint32_t, @@ -163,7 +172,6 @@ pub unsafe fn dc_mimefactory_load_msg( } } sqlite3_finalize(stmt); - stmt = 0 as *mut sqlite3_stmt; let mut command: libc::c_int = dc_param_get_int((*(*factory).msg).param, 'S' as i32, 0i32); if command == 5i32 { @@ -236,6 +244,7 @@ pub unsafe fn dc_mimefactory_load_msg( sqlite3_finalize(stmt); return success; } + unsafe fn load_from(mut factory: *mut dc_mimefactory_t) { (*factory).from_addr = dc_sqlite3_get_config( (*factory).context, @@ -259,6 +268,7 @@ unsafe fn load_from(mut factory: *mut dc_mimefactory_t) { (*factory).selfstatus = dc_stock_str((*factory).context, 13i32) }; } + pub unsafe fn dc_mimefactory_load_mdn( mut factory: *mut dc_mimefactory_t, mut msg_id: uint32_t, @@ -319,12 +329,15 @@ pub unsafe fn dc_mimefactory_load_mdn( } } dc_contact_unref(contact); - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc::c_int { - let mut subject: *mut mailimf_subject = 0 as *mut mailimf_subject; + let mut subject: *mut mailimf_subject; let mut current_block: u64; - let mut imf_fields: *mut mailimf_fields = 0 as *mut mailimf_fields; + let mut imf_fields: *mut mailimf_fields; let mut message: *mut mailmime = 0 as *mut mailmime; let mut message_text: *mut libc::c_char = 0 as *mut libc::c_char; let mut message_text2: *mut libc::c_char = 0 as *mut libc::c_char; @@ -378,8 +391,8 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc: && !(*factory).recipients_addr.is_null() && (*(*factory).recipients_addr).count > 0i32 { - let mut iter1: *mut clistiter = 0 as *mut clistiter; - let mut iter2: *mut clistiter = 0 as *mut clistiter; + let mut iter1: *mut clistiter; + let mut iter2: *mut clistiter; to = mailimf_address_list_new_empty(); iter1 = (*(*factory).recipients_names).first; iter2 = (*(*factory).recipients_addr).first; @@ -412,12 +425,12 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc: iter1 = if !iter1.is_null() { (*iter1).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell }; iter2 = if !iter2.is_null() { (*iter2).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -848,7 +861,6 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc: } else { if !meta_part.is_null() { mailmime_smart_add_part(message, meta_part); - parts += 1 } if 0 != dc_param_exists((*msg).param, DC_PARAM_SET_LATITUDE as libc::c_int) { @@ -880,7 +892,6 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc: mailmime_set_body_text(kml_mime_part, kml_file, strlen(kml_file)); mailmime_smart_add_part(message, kml_mime_part); - parts += 1; } } @@ -909,7 +920,6 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc: mailmime_new_empty(content_type, mime_fields); mailmime_set_body_text(kml_mime_part, kml_file, strlen(kml_file)); mailmime_smart_add_part(message, kml_mime_part); - parts += 1; if 0 == dc_param_exists( (*msg).param, DC_PARAM_SET_LATITUDE as libc::c_int, @@ -939,8 +949,8 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc: ) as *mut libc::c_void, ); mailmime_add_part(message, multipart); - let mut p1: *mut libc::c_char = 0 as *mut libc::c_char; - let mut p2: *mut libc::c_char = 0 as *mut libc::c_char; + let mut p1: *mut libc::c_char; + let mut p2: *mut libc::c_char; if 0 != dc_param_get_int((*(*factory).msg).param, 'c' as i32, 0i32) { p1 = dc_stock_str((*factory).context, 24i32) } else { @@ -1050,15 +1060,17 @@ pub unsafe fn dc_mimefactory_render(mut factory: *mut dc_mimefactory_t) -> libc: free(message_text2 as *mut libc::c_void); free(subject_str as *mut libc::c_void); free(grpimage as *mut libc::c_void); - return success; + + success } + unsafe fn get_subject( mut chat: *const dc_chat_t, mut msg: *const dc_msg_t, mut afwd_email: libc::c_int, ) -> *mut libc::c_char { let context = (*chat).context; - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; + let mut ret: *mut libc::c_char; let mut raw_subject: *mut libc::c_char = dc_msg_get_summarytext_by_raw((*msg).type_0, (*msg).text, (*msg).param, 32i32, context); let mut fwd: *const libc::c_char = if 0 != afwd_email { @@ -1083,8 +1095,10 @@ unsafe fn get_subject( ) } free(raw_subject as *mut libc::c_void); - return ret; + + ret } + unsafe fn set_error(mut factory: *mut dc_mimefactory_t, mut text: *const libc::c_char) { if factory.is_null() { return; @@ -1092,10 +1106,11 @@ unsafe fn set_error(mut factory: *mut dc_mimefactory_t, mut text: *const libc::c free((*factory).error as *mut libc::c_void); (*factory).error = dc_strdup_keep_null(text); } + unsafe fn build_body_text(mut text: *mut libc::c_char) -> *mut mailmime { - let mut mime_fields: *mut mailmime_fields = 0 as *mut mailmime_fields; - let mut message_part: *mut mailmime = 0 as *mut mailmime; - let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + let mut mime_fields: *mut mailmime_fields; + let mut message_part: *mut mailmime; + let mut content: *mut mailmime_content; content = mailmime_content_new_with_str(b"text/plain\x00" as *const u8 as *const libc::c_char); clist_insert_after( (*content).ct_parameters, @@ -1108,17 +1123,19 @@ unsafe fn build_body_text(mut text: *mut libc::c_char) -> *mut mailmime { mime_fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT as libc::c_int); message_part = mailmime_new_empty(content, mime_fields); mailmime_set_body_text(message_part, text, strlen(text)); - return message_part; + + message_part } + unsafe fn build_body_file( mut msg: *const dc_msg_t, mut base_name: *const libc::c_char, mut ret_file_name_as_sent: *mut *mut libc::c_char, ) -> *mut mailmime { - let mut needs_ext: libc::c_int = 0; - let mut mime_fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + let mut needs_ext: libc::c_int; + let mut mime_fields: *mut mailmime_fields; let mut mime_sub: *mut mailmime = 0 as *mut mailmime; - let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + let mut content: *mut mailmime_content; let mut pathNfilename: *mut libc::c_char = dc_param_get((*msg).param, 'f' as i32, 0 as *const libc::c_char); let mut mimetype: *mut libc::c_char = @@ -1264,7 +1281,7 @@ unsafe fn build_body_file( cur1 = if !cur1.is_null() { (*cur1).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -1291,11 +1308,14 @@ unsafe fn build_body_file( free(filename_to_send as *mut libc::c_void); free(filename_encoded as *mut libc::c_void); free(suffix as *mut libc::c_void); - return mime_sub; + + mime_sub } -/* ****************************************************************************** + +/******************************************************************************* * Render ******************************************************************************/ + unsafe fn is_file_size_okay(mut msg: *const dc_msg_t) -> libc::c_int { let mut file_size_okay: libc::c_int = 1i32; let mut pathNfilename: *mut libc::c_char = @@ -1305,5 +1325,6 @@ unsafe fn is_file_size_okay(mut msg: *const dc_msg_t) -> libc::c_int { file_size_okay = 0i32 } free(pathNfilename as *mut libc::c_void); - return file_size_okay; + + file_size_okay } diff --git a/src/dc_mimeparser.rs b/src/dc_mimeparser.rs index 36c8cfe69..f4617fed8 100644 --- a/src/dc_mimeparser.rs +++ b/src/dc_mimeparser.rs @@ -1,4 +1,12 @@ -use libc; +use mmime::charconv::*; +use mmime::mailimf::*; +use mmime::mailimf_types::*; +use mmime::mailmime::*; +use mmime::mailmime_content::*; +use mmime::mailmime_disposition::*; +use mmime::mailmime_types::*; +use mmime::mmapstring::*; +use mmime::other::*; use crate::dc_contact::*; use crate::dc_context::dc_context_t; @@ -58,13 +66,15 @@ pub struct dc_mimeparser_t<'a> { pub unsafe fn dc_no_compound_msgs() { s_generate_compound_msgs = 0i32; } + // deprecated: flag to switch generation of compound messages on and off. static mut s_generate_compound_msgs: libc::c_int = 1i32; + pub unsafe fn dc_mimeparser_new( blobdir: *const libc::c_char, context: &dc_context_t, ) -> *mut dc_mimeparser_t { - let mut mimeparser: *mut dc_mimeparser_t = 0 as *mut dc_mimeparser_t; + let mut mimeparser: *mut dc_mimeparser_t; mimeparser = calloc(1, ::std::mem::size_of::()) as *mut dc_mimeparser_t; if mimeparser.is_null() { exit(30i32); @@ -76,8 +86,10 @@ pub unsafe fn dc_mimeparser_new( (*mimeparser).e2ee_helper = calloc(1, ::std::mem::size_of::()) as *mut dc_e2ee_helper_t; dc_hash_init(&mut (*mimeparser).header, 3i32, 0i32); - return mimeparser; + + mimeparser } + pub unsafe fn dc_mimeparser_unref(mut mimeparser: *mut dc_mimeparser_t) { if mimeparser.is_null() { return; @@ -98,7 +110,7 @@ pub unsafe fn dc_mimeparser_empty(mut mimeparser: *mut dc_mimeparser_t) { return; } if !(*mimeparser).parts.is_null() { - let mut i: libc::c_int = 0; + let mut i: libc::c_int; let mut cnt: libc::c_int = carray_count((*mimeparser).parts) as libc::c_int; i = 0i32; while i < cnt { @@ -150,14 +162,15 @@ unsafe fn dc_mimepart_unref(mut mimepart: *mut dc_mimepart_t) { dc_param_unref((*mimepart).param); free(mimepart as *mut libc::c_void); } + pub unsafe fn dc_mimeparser_parse( mut mimeparser: *mut dc_mimeparser_t, mut body_not_terminated: *const libc::c_char, mut body_bytes: size_t, ) { - let mut r: libc::c_int = 0i32; + let mut r: libc::c_int; let mut index: size_t = 0i32 as size_t; - let mut optional_field: *mut mailimf_optional_field = 0 as *mut mailimf_optional_field; + let mut optional_field: *mut mailimf_optional_field; dc_mimeparser_empty(mimeparser); r = mailmime_parse( body_not_terminated, @@ -194,7 +207,7 @@ pub unsafe fn dc_mimeparser_parse( ) .is_null() { - let mut i: libc::c_int = 0; + let mut i: libc::c_int; let mut has_setup_file: libc::c_int = 0i32; i = 0i32; while (i as libc::c_uint) < carray_count((*mimeparser).parts) { @@ -303,7 +316,7 @@ pub unsafe fn dc_mimeparser_parse( } dc_trim(subj); if 0 != *subj.offset(0isize) { - let mut i_0: libc::c_int = 0; + let mut i_0: libc::c_int; let mut icnt: libc::c_int = carray_count((*mimeparser).parts) as libc::c_int; i_0 = 0i32; while i_0 < icnt { @@ -328,7 +341,7 @@ pub unsafe fn dc_mimeparser_parse( } } if 0 != (*mimeparser).is_forwarded { - let mut i_1: libc::c_int = 0; + let mut i_1: libc::c_int; let mut icnt_0: libc::c_int = carray_count((*mimeparser).parts) as libc::c_int; i_1 = 0i32; while i_1 < icnt_0 { @@ -430,24 +443,26 @@ pub unsafe fn dc_mimeparser_parse( ); }; } -/* ****************************************************************************** + +/******************************************************************************* * a MIME part ******************************************************************************/ unsafe fn dc_mimepart_new() -> *mut dc_mimepart_t { - let mut mimepart: *mut dc_mimepart_t = 0 as *mut dc_mimepart_t; + let mut mimepart: *mut dc_mimepart_t; mimepart = calloc(1, ::std::mem::size_of::()) as *mut dc_mimepart_t; if mimepart.is_null() { exit(33i32); } (*mimepart).type_0 = 0i32; (*mimepart).param = dc_param_new(); - return mimepart; + mimepart } + pub unsafe fn dc_mimeparser_get_last_nonmeta( mut mimeparser: *mut dc_mimeparser_t, ) -> *mut dc_mimepart_t { if !mimeparser.is_null() && !(*mimeparser).parts.is_null() { - let mut i: libc::c_int = 0; + let mut i: libc::c_int; let mut icnt: libc::c_int = carray_count((*mimeparser).parts) as libc::c_int; i = icnt - 1i32; while i >= 0i32 { @@ -459,8 +474,10 @@ pub unsafe fn dc_mimeparser_get_last_nonmeta( i -= 1 } } - return 0 as *mut dc_mimepart_t; + + 0 as *mut dc_mimepart_t } + /*the result must be freed*/ pub unsafe fn mailimf_find_first_addr( mut mb_list: *const mailimf_mailbox_list, @@ -481,22 +498,25 @@ pub unsafe fn mailimf_find_first_addr( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } - return 0 as *mut libc::c_char; + + 0 as *mut libc::c_char } + /* the following functions can be used only after a call to dc_mimeparser_parse() */ pub unsafe fn dc_mimeparser_lookup_field( mut mimeparser: *mut dc_mimeparser_t, mut field_name: *const libc::c_char, ) -> *mut mailimf_field { - return dc_hash_find( + dc_hash_find( &mut (*mimeparser).header, field_name as *const libc::c_void, strlen(field_name) as libc::c_int, - ) as *mut mailimf_field; + ) as *mut mailimf_field } + pub unsafe fn dc_mimeparser_lookup_optional_field( mut mimeparser: *mut dc_mimeparser_t, mut field_name: *const libc::c_char, @@ -509,14 +529,16 @@ pub unsafe fn dc_mimeparser_lookup_optional_field( if !field.is_null() && (*field).fld_type == MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int { return (*field).fld_data.fld_optional_field; } - return 0 as *mut mailimf_optional_field; + + 0 as *mut mailimf_optional_field } + unsafe fn dc_mimeparser_parse_mime_recursive( mut mimeparser: *mut dc_mimeparser_t, mut mime: *mut mailmime, ) -> libc::c_int { let mut any_part_added: libc::c_int = 0i32; - let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut cur: *mut clistiter; if mimeparser.is_null() || mime.is_null() { return 0i32; } @@ -577,6 +599,7 @@ unsafe fn dc_mimeparser_parse_mime_recursive( } } match (*mime).mm_type { + // TODO match on enums /rtn 1 => any_part_added = dc_mimeparser_add_single_part_if_known(mimeparser, mime), 2 => { match mailmime_get_mime_type(mime, 0 as *mut libc::c_int, 0 as *mut *mut libc::c_char) { @@ -602,7 +625,7 @@ unsafe fn dc_mimeparser_parse_mime_recursive( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -628,7 +651,7 @@ unsafe fn dc_mimeparser_parse_mime_recursive( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -651,7 +674,7 @@ unsafe fn dc_mimeparser_parse_mime_recursive( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -762,7 +785,7 @@ unsafe fn dc_mimeparser_parse_mime_recursive( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } if plain_cnt == 1i32 && html_cnt == 1i32 { @@ -788,7 +811,7 @@ unsafe fn dc_mimeparser_parse_mime_recursive( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -812,8 +835,10 @@ unsafe fn dc_mimeparser_parse_mime_recursive( } _ => {} } - return any_part_added; + + any_part_added } + unsafe fn hash_header(out: *mut dc_hash_t, in_0: *const mailimf_fields, _context: &dc_context_t) { if in_0.is_null() { return; @@ -826,6 +851,7 @@ unsafe fn hash_header(out: *mut dc_hash_t, in_0: *const mailimf_fields, _context 0 as *mut libc::c_void }) as *mut mailimf_field; let mut key: *const libc::c_char = 0 as *const libc::c_char; + // TODO match on enums /rtn match (*field).fld_type { 1 => key = b"Return-Path\x00" as *const u8 as *const libc::c_char, 9 => key = b"Date\x00" as *const u8 as *const libc::c_char, @@ -875,10 +901,11 @@ unsafe fn hash_header(out: *mut dc_hash_t, in_0: *const mailimf_fields, _context cur1 = if !cur1.is_null() { (*cur1).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } + unsafe fn mailmime_get_mime_type( mut mime: *mut mailmime, mut msg_type: *mut libc::c_int, @@ -893,6 +920,7 @@ unsafe fn mailmime_get_mime_type( if c.is_null() || (*c).ct_type.is_null() { return 0i32; } + // TODO match on enums /rtn match (*(*c).ct_type).tp_type { 1 => match (*(*(*c).ct_type).tp_data.tp_discrete_type).dt_type { 1 => { @@ -1047,8 +1075,10 @@ unsafe fn mailmime_get_mime_type( } _ => {} } - return 0i32; + + 0 } + unsafe fn reconcat_mime( mut raw_mime: *mut *mut libc::c_char, mut type_0: *const libc::c_char, @@ -1070,6 +1100,7 @@ unsafe fn reconcat_mime( ) }; } + unsafe fn mailmime_is_attachment_disposition(mut mime: *mut mailmime) -> libc::c_int { if !(*mime).mm_mime_fields.is_null() { let mut cur: *mut clistiter = (*(*(*mime).mm_mime_fields).fld_list).first; @@ -1093,12 +1124,14 @@ unsafe fn mailmime_is_attachment_disposition(mut mime: *mut mailmime) -> libc::c cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } - return 0i32; + + 0 } + /* low-level-tools for working with mailmime structures directly */ pub unsafe fn mailmime_find_ct_parameter( mut mime: *mut mailmime, @@ -1111,7 +1144,7 @@ pub unsafe fn mailmime_find_ct_parameter( { return 0 as *mut mailmime_parameter; } - let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut cur: *mut clistiter; cur = (*(*(*mime).mm_content_type).ct_parameters).first; while !cur.is_null() { let mut param: *mut mailmime_parameter = (if !cur.is_null() { @@ -1127,11 +1160,13 @@ pub unsafe fn mailmime_find_ct_parameter( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } - return 0 as *mut mailmime_parameter; + + 0 as *mut mailmime_parameter } + unsafe fn dc_mimeparser_add_single_part_if_known( mut mimeparser: *mut dc_mimeparser_t, mut mime: *mut mailmime, @@ -1139,8 +1174,8 @@ unsafe fn dc_mimeparser_add_single_part_if_known( let mut current_block: u64; let mut part: *mut dc_mimepart_t = 0 as *mut dc_mimepart_t; let mut old_part_count: libc::c_int = carray_count((*mimeparser).parts) as libc::c_int; - let mut mime_type: libc::c_int = 0; - let mut mime_data: *mut mailmime_data = 0 as *mut mailmime_data; + let mut mime_type: libc::c_int; + let mut mime_data: *mut mailmime_data; let mut file_suffix: *mut libc::c_char = 0 as *mut libc::c_char; let mut desired_filename: *mut libc::c_char = 0 as *mut libc::c_char; let mut msg_type: libc::c_int = 0i32; @@ -1183,6 +1218,7 @@ unsafe fn dc_mimeparser_add_single_part_if_known( } else { current_block = 13797916685926291137; } + // TODO match on enums /rtn match current_block { 8795901732489102124 => {} _ => { @@ -1342,7 +1378,7 @@ unsafe fn dc_mimeparser_add_single_part_if_known( cur2 = if !cur2.is_null() { (*cur2).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -1351,7 +1387,7 @@ unsafe fn dc_mimeparser_add_single_part_if_known( cur1 = if !cur1.is_null() { (*cur1).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -1474,6 +1510,7 @@ unsafe fn dc_mimeparser_add_single_part_if_known( 0i32 }; } + unsafe fn do_add_single_file_part( mut parser: *mut dc_mimeparser_t, mut msg_type: libc::c_int, @@ -1484,7 +1521,7 @@ unsafe fn do_add_single_file_part( mut desired_filename: *const libc::c_char, ) { let mut part: *mut dc_mimepart_t = 0 as *mut dc_mimepart_t; - let mut pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char; + let mut pathNfilename: *mut libc::c_char; /* create a free file name to use */ pathNfilename = dc_get_fine_pathNfilename( (*parser).context, @@ -1526,6 +1563,7 @@ unsafe fn do_add_single_file_part( free(pathNfilename as *mut libc::c_void); dc_mimepart_unref(part); } + unsafe fn do_add_single_part(mut parser: *mut dc_mimeparser_t, mut part: *mut dc_mimepart_t) { if 0 != (*(*parser).e2ee_helper).encrypted && (*(*(*parser).e2ee_helper).signatures).count > 0i32 @@ -1540,6 +1578,8 @@ unsafe fn do_add_single_part(mut parser: *mut dc_mimeparser_t, mut part: *mut dc 0 as *mut libc::c_uint, ); } + +// TODO should return bool /rtn pub unsafe fn mailmime_transfer_decode( mut mime: *mut mailmime, mut ret_decoded_data: *mut *const libc::c_char, @@ -1547,9 +1587,9 @@ pub unsafe fn mailmime_transfer_decode( mut ret_to_mmap_string_unref: *mut *mut libc::c_char, ) -> libc::c_int { let mut mime_transfer_encoding: libc::c_int = MAILMIME_MECHANISM_BINARY as libc::c_int; - let mut mime_data: *mut mailmime_data = 0 as *mut mailmime_data; + let mut mime_data: *mut mailmime_data; /* must not be free()'d */ - let mut decoded_data: *const libc::c_char = 0 as *const libc::c_char; + let mut decoded_data: *const libc::c_char; let mut decoded_data_bytes: size_t = 0i32 as size_t; /* mmap_string_unref()'d if set */ let mut transfer_decoding_buffer: *mut libc::c_char = 0 as *mut libc::c_char; @@ -1565,7 +1605,7 @@ pub unsafe fn mailmime_transfer_decode( } mime_data = (*mime).mm_data.mm_single; if !(*mime).mm_mime_fields.is_null() { - let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut cur: *mut clistiter; cur = (*(*(*mime).mm_mime_fields).fld_list).first; while !cur.is_null() { let mut field: *mut mailmime_field = (if !cur.is_null() { @@ -1583,7 +1623,7 @@ pub unsafe fn mailmime_transfer_decode( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -1598,7 +1638,7 @@ pub unsafe fn mailmime_transfer_decode( return 0i32; } } else { - let mut r: libc::c_int = 0; + let mut r: libc::c_int; let mut current_index: size_t = 0i32 as size_t; r = mailmime_part_parse( (*mime_data).dt_data.dt_text.dt_data, @@ -1619,8 +1659,11 @@ pub unsafe fn mailmime_transfer_decode( *ret_decoded_data = decoded_data; *ret_decoded_data_bytes = decoded_data_bytes; *ret_to_mmap_string_unref = transfer_decoding_buffer; - return 1i32; + + 1 } + +// TODO should return bool /rtn pub unsafe fn dc_mimeparser_is_mailinglist_message( mut mimeparser: *mut dc_mimeparser_t, ) -> libc::c_int { @@ -1652,15 +1695,17 @@ pub unsafe fn dc_mimeparser_is_mailinglist_message( return 1i32; } } - return 0i32; + + 0 } + pub unsafe fn dc_mimeparser_sender_equals_recipient( mut mimeparser: *mut dc_mimeparser_t, ) -> libc::c_int { let mut sender_equals_recipient: libc::c_int = 0i32; - let mut fld: *const mailimf_field = 0 as *const mailimf_field; + let mut fld: *const mailimf_field; let mut fld_from: *const mailimf_from = 0 as *const mailimf_from; - let mut mb: *mut mailimf_mailbox = 0 as *mut mailimf_mailbox; + let mut mb: *mut mailimf_mailbox; let mut from_addr_norm: *mut libc::c_char = 0 as *mut libc::c_char; let mut recipients: *mut dc_hash_t = 0 as *mut dc_hash_t; if !(mimeparser.is_null() || (*mimeparser).header_root.is_null()) { @@ -1700,14 +1745,16 @@ pub unsafe fn dc_mimeparser_sender_equals_recipient( dc_hash_clear(recipients); free(recipients as *mut libc::c_void); free(from_addr_norm as *mut libc::c_void); - return sender_equals_recipient; + + sender_equals_recipient } + pub unsafe fn mailimf_get_recipients(mut imffields: *mut mailimf_fields) -> *mut dc_hash_t { /* the returned value must be dc_hash_clear()'d and free()'d. returned addresses are normalized. */ let mut recipients: *mut dc_hash_t = malloc(::std::mem::size_of::()) as *mut dc_hash_t; dc_hash_init(recipients, 3i32, 1i32); - let mut cur1: *mut clistiter = 0 as *mut clistiter; + let mut cur1: *mut clistiter; cur1 = (*(*imffields).fld_list).first; while !cur1.is_null() { let mut fld: *mut mailimf_field = (if !cur1.is_null() { @@ -1715,9 +1762,10 @@ pub unsafe fn mailimf_get_recipients(mut imffields: *mut mailimf_fields) -> *mut } else { 0 as *mut libc::c_void }) as *mut mailimf_field; - let mut fld_to: *mut mailimf_to = 0 as *mut mailimf_to; - let mut fld_cc: *mut mailimf_cc = 0 as *mut mailimf_cc; + let mut fld_to: *mut mailimf_to; + let mut fld_cc: *mut mailimf_cc; let mut addr_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + // TODO match on enums /rtn match (*fld).fld_type { 13 => { fld_to = (*fld).fld_data.fld_to; @@ -1734,7 +1782,7 @@ pub unsafe fn mailimf_get_recipients(mut imffields: *mut mailimf_fields) -> *mut _ => {} } if !addr_list.is_null() { - let mut cur2: *mut clistiter = 0 as *mut clistiter; + let mut cur2: *mut clistiter; cur2 = (*(*addr_list).ad_list).first; while !cur2.is_null() { let mut adr: *mut mailimf_address = (if !cur2.is_null() { @@ -1748,7 +1796,7 @@ pub unsafe fn mailimf_get_recipients(mut imffields: *mut mailimf_fields) -> *mut } else if (*adr).ad_type == MAILIMF_ADDRESS_GROUP as libc::c_int { let mut group: *mut mailimf_group = (*adr).ad_data.ad_group; if !group.is_null() && !(*group).grp_mb_list.is_null() { - let mut cur3: *mut clistiter = 0 as *mut clistiter; + let mut cur3: *mut clistiter; cur3 = (*(*(*group).grp_mb_list).mb_list).first; while !cur3.is_null() { mailimf_get_recipients__add_addr( @@ -1762,7 +1810,7 @@ pub unsafe fn mailimf_get_recipients(mut imffields: *mut mailimf_fields) -> *mut cur3 = if !cur3.is_null() { (*cur3).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -1771,18 +1819,20 @@ pub unsafe fn mailimf_get_recipients(mut imffields: *mut mailimf_fields) -> *mut cur2 = if !cur2.is_null() { (*cur2).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } cur1 = if !cur1.is_null() { (*cur1).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } - return recipients; + + recipients } + /* ****************************************************************************** * debug output ******************************************************************************/ @@ -1805,6 +1855,7 @@ unsafe fn mailimf_get_recipients__add_addr( free(addr_norm as *mut libc::c_void); }; } + /*the result is a pointer to mime, must not be freed*/ pub unsafe fn mailimf_find_field( mut header: *mut mailimf_fields, @@ -1828,17 +1879,19 @@ pub unsafe fn mailimf_find_field( cur1 = if !cur1.is_null() { (*cur1).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } - return 0 as *mut mailimf_field; + + 0 as *mut mailimf_field } + pub unsafe fn dc_mimeparser_repl_msg_by_error( mut mimeparser: *mut dc_mimeparser_t, mut error_msg: *const libc::c_char, ) { - let mut part: *mut dc_mimepart_t = 0 as *mut dc_mimepart_t; - let mut i: libc::c_int = 0i32; + let mut part: *mut dc_mimepart_t; + let mut i: libc::c_int; if mimeparser.is_null() || (*mimeparser).parts.is_null() || carray_count((*mimeparser).parts) <= 0i32 as libc::c_uint @@ -1859,11 +1912,13 @@ pub unsafe fn dc_mimeparser_repl_msg_by_error( } carray_set_size((*mimeparser).parts, 1i32 as libc::c_uint); } + /*the result is a pointer to mime, must not be freed*/ pub unsafe fn mailmime_find_mailimf_fields(mut mime: *mut mailmime) -> *mut mailimf_fields { if mime.is_null() { return 0 as *mut mailimf_fields; } + // TODO match on enums match (*mime).mm_type { 2 => { let mut cur: *mut clistiter = (*(*mime).mm_data.mm_multipart.mm_mp_list).first; @@ -1881,15 +1936,17 @@ pub unsafe fn mailmime_find_mailimf_fields(mut mime: *mut mailmime) -> *mut mail cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } 3 => return (*mime).mm_data.mm_message.mm_fields, _ => {} } - return 0 as *mut mailimf_fields; + + 0 as *mut mailimf_fields } + pub unsafe fn mailimf_find_optional_field( mut header: *mut mailimf_fields, mut wanted_fld_name: *const libc::c_char, @@ -1918,8 +1975,9 @@ pub unsafe fn mailimf_find_optional_field( cur1 = if !cur1.is_null() { (*cur1).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } - return 0 as *mut mailimf_optional_field; + + 0 as *mut mailimf_optional_field } diff --git a/src/dc_move.rs b/src/dc_move.rs index 880ea7ab0..384af8116 100644 --- a/src/dc_move.rs +++ b/src/dc_move.rs @@ -1,5 +1,4 @@ -use libc; - +use crate::constants::*; use crate::dc_context::*; use crate::dc_job::*; use crate::dc_msg::*; diff --git a/src/dc_msg.rs b/src/dc_msg.rs index fcc60ccbf..f277802b5 100644 --- a/src/dc_msg.rs +++ b/src/dc_msg.rs @@ -1,5 +1,3 @@ -use libc; - use crate::constants::Event; use crate::dc_chat::*; use crate::dc_contact::*; @@ -51,15 +49,15 @@ pub unsafe fn dc_get_msg_info( mut context: &dc_context_t, mut msg_id: uint32_t, ) -> *mut libc::c_char { - let mut e2ee_errors: libc::c_int = 0; - let mut w: libc::c_int = 0; - let mut h: libc::c_int = 0; - let mut duration: libc::c_int = 0; - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut e2ee_errors: libc::c_int; + let mut w: libc::c_int; + let mut h: libc::c_int; + let mut duration: libc::c_int; + let mut stmt: *mut sqlite3_stmt; let mut msg: *mut dc_msg_t = dc_msg_new_untyped(context); let mut contact_from: *mut dc_contact_t = dc_contact_new(context); let mut rawtxt: *mut libc::c_char = 0 as *mut libc::c_char; - let mut p: *mut libc::c_char = 0 as *mut libc::c_char; + let mut p: *mut libc::c_char; let mut ret: dc_strbuilder_t = dc_strbuilder_t { buf: 0 as *mut libc::c_char, allocated: 0, @@ -148,7 +146,6 @@ pub unsafe fn dc_get_msg_info( } sqlite3_finalize(stmt); stmt = 0 as *mut sqlite3_stmt; - p = 0 as *mut libc::c_char; match (*msg).state { 10 => p = dc_strdup(b"Fresh\x00" as *const u8 as *const libc::c_char), 13 => p = dc_strdup(b"Noticed\x00" as *const u8 as *const libc::c_char), @@ -173,7 +170,6 @@ pub unsafe fn dc_get_msg_info( ); } p = 0 as *mut libc::c_char; - e2ee_errors = 0; e2ee_errors = dc_param_get_int((*msg).param, 'e' as i32, 0i32); if 0 != e2ee_errors { if 0 != e2ee_errors & 0x2i32 { @@ -213,7 +209,6 @@ pub unsafe fn dc_get_msg_info( } free(p as *mut libc::c_void); if (*msg).type_0 != 10i32 { - p = 0 as *mut libc::c_char; match (*msg).type_0 { 40 => p = dc_strdup(b"Audio\x00" as *const u8 as *const libc::c_char), 60 => p = dc_strdup(b"File\x00" as *const u8 as *const libc::c_char), @@ -289,8 +284,10 @@ pub unsafe fn dc_get_msg_info( dc_msg_unref(msg); dc_contact_unref(contact_from); free(rawtxt as *mut libc::c_void); - return ret.buf; + + ret.buf } + pub unsafe fn dc_msg_new_untyped<'a>(context: &'a dc_context_t) -> *mut dc_msg_t<'a> { dc_msg_new(context, 0i32) } @@ -309,7 +306,7 @@ 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; + let mut msg: *mut dc_msg_t; msg = calloc(1, ::std::mem::size_of::()) as *mut dc_msg_t; if msg.is_null() { exit(15i32); @@ -319,8 +316,10 @@ pub unsafe fn dc_msg_new<'a>( (*msg).type_0 = viewtype; (*msg).state = 0i32; (*msg).param = dc_param_new(); - return msg; + + msg } + pub unsafe fn dc_msg_unref(mut msg: *mut dc_msg_t) { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return; @@ -330,6 +329,7 @@ pub unsafe fn dc_msg_unref(mut msg: *mut dc_msg_t) { (*msg).magic = 0i32 as uint32_t; free(msg as *mut libc::c_void); } + pub unsafe fn dc_msg_empty(mut msg: *mut dc_msg_t) { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return; @@ -345,6 +345,7 @@ pub unsafe fn dc_msg_empty(mut msg: *mut dc_msg_t) { dc_param_set_packed((*msg).param, 0 as *const libc::c_char); (*msg).hidden = 0i32; } + pub unsafe fn dc_msg_get_filemime(mut msg: *const dc_msg_t) -> *mut libc::c_char { let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; let mut file: *mut libc::c_char = 0 as *mut libc::c_char; @@ -369,6 +370,7 @@ pub unsafe fn dc_msg_get_filemime(mut msg: *const dc_msg_t) -> *mut libc::c_char dc_strdup(0 as *const libc::c_char) }; } + pub unsafe fn dc_msg_guess_msgtype_from_suffix( mut pathNfilename: *const libc::c_char, mut ret_msgtype: *mut libc::c_int, @@ -422,6 +424,7 @@ pub unsafe fn dc_msg_guess_msgtype_from_suffix( free(suffix as *mut libc::c_void); free(dummy_buf as *mut libc::c_void); } + pub unsafe fn dc_msg_get_file(mut msg: *const dc_msg_t) -> *mut libc::c_char { let mut file_rel: *mut libc::c_char = 0 as *mut libc::c_char; let mut file_abs: *mut libc::c_char = 0 as *mut libc::c_char; @@ -448,11 +451,13 @@ pub unsafe fn dc_msg_get_file(mut msg: *const dc_msg_t) -> *mut libc::c_char { * @param msg The message object. * @return 1=Message has location bound to it, 0=No location bound to message. */ +// TODO should return bool /rtn pub unsafe fn dc_msg_has_location(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; } - return ((*msg).location_id != 0i32 as libc::c_uint) as libc::c_int; + + ((*msg).location_id != 0i32 as libc::c_uint) as libc::c_int } /** @@ -502,6 +507,8 @@ pub unsafe fn dc_msg_get_timestamp(mut msg: *const dc_msg_t) -> time_t { (*msg).timestamp_sort }; } + +// TODO should return bool /rtn pub unsafe fn dc_msg_load_from_db<'a>( msg: *mut dc_msg_t<'a>, context: &'a dc_context_t, @@ -526,8 +533,11 @@ pub unsafe fn dc_msg_load_from_db<'a>( } } sqlite3_finalize(stmt); - return success; + + success } + +// TODO always returns 1, should be void /rtn unsafe fn dc_msg_set_from_stmt( mut msg: *mut dc_msg_t, mut row: *mut sqlite3_stmt, @@ -598,19 +608,20 @@ unsafe fn dc_msg_set_from_stmt( row_offset = row_offset + 1; (*msg).location_id = sqlite3_column_int(row, fresh19) as uint32_t; let fresh20 = row_offset; - row_offset = row_offset + 1; (*msg).chat_blocked = sqlite3_column_int(row, fresh20); if (*msg).chat_blocked == 2i32 { dc_truncate_n_unwrap_str((*msg).text, 256i32, 0i32); } - return 1i32; + + 1 } + pub unsafe fn dc_get_mime_headers( mut context: &dc_context_t, mut msg_id: uint32_t, ) -> *mut libc::c_char { let mut eml = 0 as *mut libc::c_char; - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, @@ -623,8 +634,10 @@ pub unsafe fn dc_get_mime_headers( } sqlite3_finalize(stmt); - return eml; + + eml } + pub unsafe fn dc_delete_msgs( mut context: &dc_context_t, mut msg_ids: *const uint32_t, @@ -657,6 +670,7 @@ pub unsafe fn dc_delete_msgs( dc_job_add(context, 105i32, 0i32, 0 as *const libc::c_char, 10i32); }; } + pub unsafe fn dc_update_msg_chat_id( mut context: &dc_context_t, mut msg_id: uint32_t, @@ -672,15 +686,16 @@ pub unsafe fn dc_update_msg_chat_id( sqlite3_step(stmt); sqlite3_finalize(stmt); } + pub unsafe fn dc_markseen_msgs( mut context: &dc_context_t, mut msg_ids: *const uint32_t, mut msg_cnt: libc::c_int, ) { - let mut i: libc::c_int = 0i32; + let mut i: libc::c_int; let mut send_event: libc::c_int = 0i32; - let mut curr_state: libc::c_int = 0i32; - let mut curr_blocked: libc::c_int = 0i32; + let mut curr_state: libc::c_int; + let mut curr_blocked: libc::c_int; let mut stmt = 0 as *mut sqlite3_stmt; if !(msg_ids.is_null() || msg_cnt <= 0i32) { stmt = @@ -731,6 +746,7 @@ pub unsafe fn dc_markseen_msgs( } sqlite3_finalize(stmt); } + pub unsafe fn dc_update_msg_state( mut context: &dc_context_t, mut msg_id: uint32_t, @@ -746,6 +762,7 @@ pub unsafe fn dc_update_msg_state( sqlite3_step(stmt); sqlite3_finalize(stmt); } + pub unsafe fn dc_star_msgs( mut context: &dc_context_t, mut msg_ids: *const uint32_t, @@ -770,6 +787,7 @@ pub unsafe fn dc_star_msgs( } sqlite3_finalize(stmt); } + 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); @@ -788,14 +806,18 @@ 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; } - return (*msg).id; + + (*msg).id } + pub unsafe fn dc_msg_get_from_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; } - return (*msg).from_id; + + (*msg).from_id } + pub unsafe fn dc_msg_get_chat_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; @@ -806,39 +828,50 @@ pub unsafe fn dc_msg_get_chat_id(mut msg: *const dc_msg_t) -> uint32_t { (*msg).chat_id }; } + pub unsafe fn dc_msg_get_viewtype(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; } - return (*msg).type_0; + + (*msg).type_0 } + pub unsafe fn dc_msg_get_state(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; } - return (*msg).state; + + (*msg).state } + pub unsafe fn dc_msg_get_received_timestamp(mut msg: *const dc_msg_t) -> time_t { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32 as time_t; } - return (*msg).timestamp_rcvd; + + (*msg).timestamp_rcvd } + pub unsafe fn dc_msg_get_sort_timestamp(mut msg: *const dc_msg_t) -> time_t { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32 as time_t; } - return (*msg).timestamp_sort; + + (*msg).timestamp_sort } + pub unsafe fn dc_msg_get_text(mut msg: *const dc_msg_t) -> *mut libc::c_char { - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; + let mut ret: *mut libc::c_char; if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return dc_strdup(0 as *const libc::c_char); } ret = dc_strdup((*msg).text); dc_truncate_str(ret, 30000i32); - return ret; + + ret } + pub unsafe fn dc_msg_get_filename(mut msg: *const dc_msg_t) -> *mut libc::c_char { let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; let mut pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char; @@ -855,6 +888,7 @@ pub unsafe fn dc_msg_get_filename(mut msg: *const dc_msg_t) -> *mut libc::c_char dc_strdup(0 as *const libc::c_char) }; } + pub unsafe fn dc_msg_get_filebytes(mut msg: *const dc_msg_t) -> uint64_t { let mut ret: uint64_t = 0i32 as uint64_t; let mut file: *mut libc::c_char = 0 as *mut libc::c_char; @@ -865,26 +899,36 @@ pub unsafe fn dc_msg_get_filebytes(mut msg: *const dc_msg_t) -> uint64_t { } } free(file as *mut libc::c_void); - return ret; + + ret } + pub unsafe fn dc_msg_get_width(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; } - return dc_param_get_int((*msg).param, 'w' as i32, 0i32); + + dc_param_get_int((*msg).param, 'w' as i32, 0i32) } + pub unsafe fn dc_msg_get_height(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; } - return dc_param_get_int((*msg).param, 'h' as i32, 0i32); + + dc_param_get_int((*msg).param, 'h' as i32, 0i32) } + +// TODO should return bool /rtn pub unsafe fn dc_msg_get_duration(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; } - return dc_param_get_int((*msg).param, 'd' as i32, 0i32); + + dc_param_get_int((*msg).param, 'd' as i32, 0i32) } + +// TODO should return bool /rtn pub unsafe fn dc_msg_get_showpadlock(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; @@ -892,8 +936,10 @@ pub unsafe fn dc_msg_get_showpadlock(mut msg: *const dc_msg_t) -> libc::c_int { if dc_param_get_int((*msg).param, 'c' as i32, 0i32) != 0i32 { return 1i32; } - return 0i32; + + 0 } + pub unsafe fn dc_msg_get_summary<'a>( msg: *const dc_msg_t<'a>, mut chat: *const dc_chat_t<'a>, @@ -928,8 +974,10 @@ pub unsafe fn dc_msg_get_summary<'a>( } dc_contact_unref(contact); dc_chat_unref(chat_to_delete); - return ret; + + ret } + pub unsafe fn dc_msg_get_summarytext( mut msg: *const dc_msg_t, mut approx_characters: libc::c_int, @@ -937,14 +985,16 @@ pub unsafe fn dc_msg_get_summarytext( if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return dc_strdup(0 as *const libc::c_char); } - return dc_msg_get_summarytext_by_raw( + + dc_msg_get_summarytext_by_raw( (*msg).type_0, (*msg).text, (*msg).param, approx_characters, (*msg).context, - ); + ) } + /* the returned value must be free()'d */ pub unsafe fn dc_msg_get_summarytext_by_raw( mut type_0: libc::c_int, @@ -954,7 +1004,7 @@ pub unsafe fn dc_msg_get_summarytext_by_raw( mut context: &dc_context_t, ) -> *mut libc::c_char { /* get a summary text, result must be free()'d, never returns NULL. */ - let mut ret = 0 as *mut libc::c_char; + let mut ret; let mut prefix: *mut libc::c_char = 0 as *mut libc::c_char; let mut pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char; let mut label: *mut libc::c_char = 0 as *mut libc::c_char; @@ -1016,27 +1066,36 @@ pub unsafe fn dc_msg_get_summarytext_by_raw( if ret.is_null() { ret = dc_strdup(0 as *const libc::c_char) } - return ret; + + ret } + pub unsafe fn dc_msg_has_deviating_timestamp(mut msg: *const dc_msg_t) -> libc::c_int { let mut cnv_to_local: libc::c_long = dc_gm2local_offset(); let mut sort_timestamp: time_t = dc_msg_get_sort_timestamp(msg) + cnv_to_local; let mut send_timestamp: time_t = dc_msg_get_timestamp(msg) + cnv_to_local; - return (sort_timestamp / 86400i32 as libc::c_long != send_timestamp / 86400i32 as libc::c_long) - as libc::c_int; + + (sort_timestamp / 86400i32 as libc::c_long != send_timestamp / 86400i32 as libc::c_long) + as libc::c_int } + +// TODO should return bool /rtn pub unsafe fn dc_msg_is_sent(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; } return if (*msg).state >= 26i32 { 1i32 } else { 0i32 }; } + +// TODO should return bool /rtn pub unsafe fn dc_msg_is_starred(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; } return if 0 != (*msg).starred { 1i32 } else { 0i32 }; } + +// TODO should return bool /rtn pub unsafe fn dc_msg_is_forwarded(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; @@ -1047,6 +1106,8 @@ pub unsafe fn dc_msg_is_forwarded(mut msg: *const dc_msg_t) -> libc::c_int { 0i32 }; } + +// TODO should return bool /rtn pub unsafe fn dc_msg_is_info(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; @@ -1058,20 +1119,26 @@ pub unsafe fn dc_msg_is_info(mut msg: *const dc_msg_t) -> libc::c_int { { return 1i32; } - return 0i32; + + 0 } + +// TODO should return bool /rtn pub unsafe fn dc_msg_is_increation(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return 0i32; } - return (((*msg).type_0 == 20i32 + + (((*msg).type_0 == 20i32 || (*msg).type_0 == 21i32 || (*msg).type_0 == 40i32 || (*msg).type_0 == 41i32 || (*msg).type_0 == 50i32 || (*msg).type_0 == 60i32) - && (*msg).state == 18i32) as libc::c_int; + && (*msg).state == 18i32) as libc::c_int } + +// TODO should return bool /rtn pub unsafe fn dc_msg_is_setupmessage(mut msg: *const dc_msg_t) -> libc::c_int { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint || (*msg).type_0 != 60i32 { return 0i32; @@ -1082,6 +1149,7 @@ pub unsafe fn dc_msg_is_setupmessage(mut msg: *const dc_msg_t) -> libc::c_int { 0i32 }; } + pub unsafe fn dc_msg_get_setupcodebegin(mut msg: *const dc_msg_t) -> *mut libc::c_char { let mut filename: *mut libc::c_char = 0 as *mut libc::c_char; let mut buf: *mut libc::c_char = 0 as *mut libc::c_char; @@ -1131,6 +1199,7 @@ pub unsafe fn dc_msg_get_setupcodebegin(mut msg: *const dc_msg_t) -> *mut libc:: dc_strdup(0 as *const libc::c_char) }; } + pub unsafe fn dc_msg_set_text(mut msg: *mut dc_msg_t, mut text: *const libc::c_char) { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return; @@ -1138,6 +1207,8 @@ pub unsafe fn dc_msg_set_text(mut msg: *mut dc_msg_t, mut text: *const libc::c_c free((*msg).text as *mut libc::c_void); (*msg).text = dc_strdup(text); } + +// TODO should return bool /rtn pub unsafe fn dc_msg_set_file( mut msg: *mut dc_msg_t, mut file: *const libc::c_char, @@ -1149,6 +1220,7 @@ pub unsafe fn dc_msg_set_file( dc_param_set((*msg).param, 'f' as i32, file); dc_param_set((*msg).param, 'm' as i32, filemime); } + pub unsafe fn dc_msg_set_dimension( mut msg: *mut dc_msg_t, mut width: libc::c_int, @@ -1160,12 +1232,14 @@ pub unsafe fn dc_msg_set_dimension( dc_param_set_int((*msg).param, 'w' as i32, width); dc_param_set_int((*msg).param, 'h' as i32, height); } + pub unsafe fn dc_msg_set_duration(mut msg: *mut dc_msg_t, mut duration: libc::c_int) { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return; } dc_param_set_int((*msg).param, 'd' as i32, duration); } + pub unsafe fn dc_msg_latefiling_mediasize( mut msg: *mut dc_msg_t, mut width: libc::c_int, @@ -1183,6 +1257,7 @@ pub unsafe fn dc_msg_latefiling_mediasize( dc_msg_save_param_to_disk(msg); }; } + pub unsafe fn dc_msg_save_param_to_disk(mut msg: *mut dc_msg_t) { if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint { return; @@ -1197,6 +1272,7 @@ 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<'a>( context: &'a dc_context_t, msg_id: uint32_t, @@ -1218,7 +1294,6 @@ pub unsafe fn dc_delete_msg_from_db(context: &dc_context_t, mut msg_id: uint32_t sqlite3_bind_int(stmt, 1i32, (*msg).id as libc::c_int); sqlite3_step(stmt); sqlite3_finalize(stmt); - stmt = 0 as *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -1232,6 +1307,7 @@ pub unsafe fn dc_delete_msg_from_db(context: &dc_context_t, mut msg_id: uint32_t sqlite3_finalize(stmt); dc_msg_unref(msg); } + /* as we do not cut inside words, this results in about 32-42 characters. Do not use too long subjects - we add a tag after the subject which gets truncated by the clients otherwise. It should also be very clear, the subject is _not_ the whole message. @@ -1255,8 +1331,10 @@ pub unsafe fn dc_msg_exists(mut context: &dc_context_t, mut msg_id: uint32_t) -> } } sqlite3_finalize(stmt); - return msg_exists; + + msg_exists } + pub unsafe fn dc_update_msg_move_state( mut context: &dc_context_t, mut rfc724_mid: *const libc::c_char, @@ -1274,6 +1352,7 @@ pub unsafe fn dc_update_msg_move_state( sqlite3_step(stmt); sqlite3_finalize(stmt); } + pub unsafe fn dc_set_msg_failed( mut context: &dc_context_t, mut msg_id: uint32_t, @@ -1313,6 +1392,7 @@ pub unsafe fn dc_set_msg_failed( sqlite3_finalize(stmt); dc_msg_unref(msg); } + /* returns 1 if an event should be send */ pub unsafe fn dc_mdn_from_ext( mut context: &dc_context_t, @@ -1322,11 +1402,11 @@ pub unsafe fn dc_mdn_from_ext( mut ret_chat_id: *mut uint32_t, mut ret_msg_id: *mut uint32_t, ) -> libc::c_int { - let mut chat_type: libc::c_int = 0; - let mut msg_state: libc::c_int = 0; - let mut mdn_already_in_table: libc::c_int = 0; - let mut ist_cnt: libc::c_int = 0; - let mut soll_cnt: libc::c_int = 0; + let mut chat_type: libc::c_int; + let mut msg_state: libc::c_int; + let mut mdn_already_in_table: libc::c_int; + let mut ist_cnt: libc::c_int; + let mut soll_cnt: libc::c_int; let mut read_by_all: libc::c_int = 0i32; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; if !(from_id <= 9i32 as libc::c_uint @@ -1426,8 +1506,10 @@ pub unsafe fn dc_mdn_from_ext( } } sqlite3_finalize(stmt); - return read_by_all; + + read_by_all } + /* the number of messages assigned to real chat (!=deaddrop, !=trash) */ 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; @@ -1450,8 +1532,10 @@ pub unsafe fn dc_get_real_msg_cnt(mut context: &dc_context_t) -> size_t { } } sqlite3_finalize(stmt); - return ret; + + ret } + pub unsafe fn dc_get_deaddrop_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; @@ -1465,8 +1549,10 @@ pub unsafe fn dc_get_deaddrop_msg_cnt(mut context: &dc_context_t) -> size_t { } } sqlite3_finalize(stmt); - return ret; + + ret } + pub unsafe fn dc_rfc724_mid_cnt( mut context: &dc_context_t, mut rfc724_mid: *const libc::c_char, @@ -1487,8 +1573,10 @@ pub unsafe fn dc_rfc724_mid_cnt( } } sqlite3_finalize(stmt); - return ret; + + ret } + pub unsafe fn dc_rfc724_mid_exists( mut context: &dc_context_t, mut rfc724_mid: *const libc::c_char, @@ -1523,8 +1611,10 @@ pub unsafe fn dc_rfc724_mid_exists( } } sqlite3_finalize(stmt); - return ret; + + ret } + pub unsafe fn dc_update_server_uid( mut context: &dc_context_t, mut rfc724_mid: *const libc::c_char, diff --git a/src/dc_oauth2.rs b/src/dc_oauth2.rs index 3efbafbea..9d47e3e45 100644 --- a/src/dc_oauth2.rs +++ b/src/dc_oauth2.rs @@ -1,5 +1,3 @@ -use libc; - use crate::constants::Event; use crate::dc_contact::*; use crate::dc_context::dc_context_t; @@ -51,8 +49,10 @@ pub unsafe fn dc_get_oauth2_url( } } free(oauth2 as *mut libc::c_void); - return oauth2_url; + + oauth2_url } + unsafe fn replace_in_uri( mut uri: *mut *mut libc::c_char, mut key: *const libc::c_char, @@ -64,10 +64,11 @@ unsafe fn replace_in_uri( free(value_urlencoded as *mut libc::c_void); }; } + unsafe fn get_info(mut addr: *const libc::c_char) -> *mut oauth2_t { let mut oauth2: *mut oauth2_t = 0 as *mut oauth2_t; - let mut addr_normalized: *mut libc::c_char = 0 as *mut libc::c_char; - let mut domain: *const libc::c_char = 0 as *const libc::c_char; + let mut addr_normalized: *mut libc::c_char; + let mut domain: *const libc::c_char; addr_normalized = dc_addr_normalize(addr); domain = strchr(addr_normalized, '@' as i32); if !(domain.is_null() || *domain.offset(0isize) as libc::c_int == 0i32) { @@ -116,8 +117,10 @@ unsafe fn get_info(mut addr: *const libc::c_char) -> *mut oauth2_t { } } free(addr_normalized as *mut libc::c_void); - return oauth2; + + oauth2 } + // the following function may block due http-requests; // must not be called from the main thread or by the ui! pub unsafe fn dc_get_oauth2_access_token( @@ -150,7 +153,7 @@ pub unsafe fn dc_get_oauth2_access_token( end: 0, size: 0, }; 128]; - let mut tok_cnt: libc::c_int = 0i32; + let mut tok_cnt: libc::c_int; if code.is_null() || *code.offset(0isize) as libc::c_int == 0i32 { dc_log_warning( context, @@ -452,6 +455,7 @@ pub unsafe fn dc_get_oauth2_access_token( dc_strdup(0 as *const libc::c_char) }; } + unsafe fn jsondup(mut json: *const libc::c_char, mut tok: *mut jsmntok_t) -> *mut libc::c_char { if (*tok).type_0 as libc::c_uint == JSMN_STRING as libc::c_int as libc::c_uint || (*tok).type_0 as libc::c_uint == JSMN_PRIMITIVE as libc::c_int as libc::c_uint @@ -461,8 +465,10 @@ unsafe fn jsondup(mut json: *const libc::c_char, mut tok: *mut jsmntok_t) -> *mu ((*tok).end - (*tok).start) as libc::c_ulong, ); } - return strdup(b"\x00" as *const u8 as *const libc::c_char); + + strdup(b"\x00" as *const u8 as *const libc::c_char) } + unsafe extern "C" fn jsoneq( mut json: *const libc::c_char, mut tok: *mut jsmntok_t, @@ -478,8 +484,11 @@ unsafe extern "C" fn jsoneq( { return 0i32; } - return -1i32; + + -1 } + +// TODO should return bool /rtn unsafe fn is_expired(mut context: &dc_context_t) -> libc::c_int { let mut expire_timestamp: time_t = dc_sqlite3_get_config_int64( context, @@ -493,8 +502,10 @@ unsafe fn is_expired(mut context: &dc_context_t) -> libc::c_int { if expire_timestamp > time(0 as *mut time_t) { return 0i32; } - return 1i32; + + 1 } + pub unsafe fn dc_get_oauth2_addr( mut context: &dc_context_t, mut addr: *const libc::c_char, @@ -502,7 +513,7 @@ pub unsafe fn dc_get_oauth2_addr( ) -> *mut libc::c_char { let mut access_token: *mut libc::c_char = 0 as *mut libc::c_char; let mut addr_out: *mut libc::c_char = 0 as *mut libc::c_char; - let mut oauth2: *mut oauth2_t = 0 as *mut oauth2_t; + let mut oauth2: *mut oauth2_t; if !({ oauth2 = get_info(addr); oauth2.is_null() @@ -518,8 +529,10 @@ pub unsafe fn dc_get_oauth2_addr( } free(access_token as *mut libc::c_void); free(oauth2 as *mut libc::c_void); - return addr_out; + + addr_out } + unsafe fn get_oauth2_addr( mut context: &dc_context_t, mut oauth2: *const oauth2_t, @@ -540,7 +553,7 @@ unsafe fn get_oauth2_addr( end: 0, size: 0, }; 128]; - let mut tok_cnt: libc::c_int = 0i32; + let mut tok_cnt: libc::c_int; if !(access_token.is_null() || *access_token.offset(0isize) as libc::c_int == 0i32 || oauth2.is_null()) @@ -608,5 +621,6 @@ unsafe fn get_oauth2_addr( } free(userinfo_url as *mut libc::c_void); free(json as *mut libc::c_void); - return addr_out; + + addr_out } diff --git a/src/dc_param.rs b/src/dc_param.rs index dc167a09d..6ccf9f55c 100644 --- a/src/dc_param.rs +++ b/src/dc_param.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_tools::*; use crate::types::*; use crate::x::*; @@ -94,8 +92,8 @@ unsafe extern "C" fn find_param( mut key: libc::c_int, mut ret_p2: *mut *mut libc::c_char, ) -> *mut libc::c_char { - let mut p1: *mut libc::c_char = 0 as *mut libc::c_char; - let mut p2: *mut libc::c_char = 0 as *mut libc::c_char; + let mut p1: *mut libc::c_char; + let mut p2: *mut libc::c_char; p1 = haystack; loop { if p1.is_null() || *p1 as libc::c_int == 0i32 { @@ -115,18 +113,20 @@ unsafe extern "C" fn find_param( p2 = &mut *p1.offset(strlen(p1) as isize) as *mut libc::c_char } *ret_p2 = p2; - return p1; + + p1 } + /* the value may be an empty string, "def" is returned only if the value unset. The result must be free()'d in any case. */ pub unsafe fn dc_param_get( param: *const dc_param_t, key: libc::c_int, def: *const libc::c_char, ) -> *mut libc::c_char { - let mut p1: *mut libc::c_char = 0 as *mut libc::c_char; + let mut p1: *mut libc::c_char; let mut p2: *mut libc::c_char = 0 as *mut libc::c_char; - let mut bak: libc::c_char = 0i32 as libc::c_char; - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; + let mut bak: libc::c_char; + let mut ret: *mut libc::c_char; if param.is_null() || key == 0i32 { return if !def.is_null() { dc_strdup(def) @@ -148,8 +148,10 @@ pub unsafe fn dc_param_get( ret = dc_strdup(p1); dc_rtrim(ret); *p2 = bak; - return ret; + + ret } + pub unsafe fn dc_param_get_int( mut param: *const dc_param_t, mut key: libc::c_int, @@ -164,7 +166,8 @@ pub unsafe fn dc_param_get_int( } let mut ret: int32_t = atol(str) as int32_t; free(str as *mut libc::c_void); - return ret; + + ret } /** @@ -201,16 +204,16 @@ pub unsafe fn dc_param_set( mut key: libc::c_int, mut value: *const libc::c_char, ) { - let mut old1: *mut libc::c_char = 0 as *mut libc::c_char; - let mut old2: *mut libc::c_char = 0 as *mut libc::c_char; - let mut new1: *mut libc::c_char = 0 as *mut libc::c_char; + let mut old1: *mut libc::c_char; + let mut old2: *mut libc::c_char; + let mut new1: *mut libc::c_char; if param.is_null() || key == 0i32 { return; } old1 = (*param).packed; old2 = 0 as *mut libc::c_char; if !old1.is_null() { - let mut p1: *mut libc::c_char = 0 as *mut libc::c_char; + let mut p1: *mut libc::c_char; let mut p2: *mut libc::c_char = 0 as *mut libc::c_char; p1 = find_param(old1, key, &mut p2); if !p1.is_null() { @@ -277,6 +280,7 @@ pub unsafe fn dc_param_set( free((*param).packed as *mut libc::c_void); (*param).packed = new1; } + pub unsafe fn dc_param_set_int( mut param: *mut dc_param_t, mut key: libc::c_int, @@ -295,22 +299,26 @@ pub unsafe fn dc_param_set_int( dc_param_set(param, key, value_str); free(value_str as *mut libc::c_void); } + /* library-private */ pub unsafe fn dc_param_new() -> *mut dc_param_t { - let mut param: *mut dc_param_t = 0 as *mut dc_param_t; + let mut param: *mut dc_param_t; param = calloc(1, ::std::mem::size_of::()) as *mut dc_param_t; if param.is_null() { exit(28i32); } (*param).packed = calloc(1, 1) as *mut libc::c_char; - return param; + + param } + pub unsafe fn dc_param_empty(mut param: *mut dc_param_t) { if param.is_null() { return; } *(*param).packed.offset(0isize) = 0i32 as libc::c_char; } + pub unsafe fn dc_param_unref(mut param: *mut dc_param_t) { if param.is_null() { return; @@ -319,6 +327,7 @@ pub unsafe fn dc_param_unref(mut param: *mut dc_param_t) { free((*param).packed as *mut libc::c_void); free(param as *mut libc::c_void); } + pub unsafe fn dc_param_set_packed(mut param: *mut dc_param_t, mut packed: *const libc::c_char) { if param.is_null() { return; @@ -329,6 +338,7 @@ pub unsafe fn dc_param_set_packed(mut param: *mut dc_param_t, mut packed: *const (*param).packed = dc_strdup(packed) }; } + pub unsafe fn dc_param_set_urlencoded( mut param: *mut dc_param_t, mut urlencoded: *const libc::c_char, diff --git a/src/dc_pgp.rs b/src/dc_pgp.rs index 84e358a2a..6796276ce 100644 --- a/src/dc_pgp.rs +++ b/src/dc_pgp.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_context::dc_context_t; use crate::dc_hash::*; use crate::dc_key::*; @@ -12,6 +10,7 @@ use crate::x::*; pub unsafe fn dc_pgp_exit() {} +// TODO should return bool /rtn pub unsafe fn dc_split_armored_data( mut buf: *mut libc::c_char, mut ret_headerline: *mut *const libc::c_char, @@ -23,7 +22,7 @@ pub unsafe fn dc_split_armored_data( let mut line_chars: size_t = 0i32 as size_t; let mut line: *mut libc::c_char = buf; let mut p1: *mut libc::c_char = buf; - let mut p2: *mut libc::c_char = 0 as *mut libc::c_char; + let mut p2: *mut libc::c_char; let mut headerline: *mut libc::c_char = 0 as *mut libc::c_char; let mut base64: *mut libc::c_char = 0 as *mut libc::c_char; if !ret_headerline.is_null() { @@ -126,9 +125,12 @@ pub unsafe fn dc_split_armored_data( } } } - return success; + + success } + /* public key encryption */ +// TODO should return bool /rtn pub unsafe fn dc_pgp_create_keypair( mut context: &dc_context_t, mut addr: *const libc::c_char, @@ -136,11 +138,11 @@ pub unsafe fn dc_pgp_create_keypair( mut ret_private_key: *mut dc_key_t, ) -> libc::c_int { let mut success: libc::c_int = 0i32; - let mut skey: *mut rpgp::signed_secret_key = 0 as *mut rpgp::signed_secret_key; + let mut skey: *mut rpgp::signed_secret_key; let mut pkey: *mut rpgp::signed_public_key = 0 as *mut rpgp::signed_public_key; let mut skey_bytes: *mut rpgp::cvec = 0 as *mut rpgp::cvec; let mut pkey_bytes: *mut rpgp::cvec = 0 as *mut rpgp::cvec; - let mut user_id: *mut libc::c_char = 0 as *mut libc::c_char; + let mut user_id: *mut libc::c_char; user_id = dc_mprintf(b"<%s>\x00" as *const u8 as *const libc::c_char, addr); skey = rpgp::rpgp_create_rsa_skey(2048i32 as uint32_t, user_id); if !(0 != dc_pgp_handle_rpgp_error(context)) { @@ -187,12 +189,15 @@ pub unsafe fn dc_pgp_create_keypair( if !user_id.is_null() { free(user_id as *mut libc::c_void); } - return success; + + success } + /* returns 0 if there is no error, otherwise logs the error if a context is provided and returns 1*/ +// TODO should return bool /rtn pub unsafe fn dc_pgp_handle_rpgp_error(mut context: &dc_context_t) -> libc::c_int { let mut success: libc::c_int = 0i32; - let mut len: libc::c_int = 0i32; + let mut len: libc::c_int; let mut msg: *mut libc::c_char = 0 as *mut libc::c_char; len = rpgp::rpgp_last_error_length(); if !(len == 0i32) { @@ -208,8 +213,11 @@ pub unsafe fn dc_pgp_handle_rpgp_error(mut context: &dc_context_t) -> libc::c_in if !msg.is_null() { rpgp::rpgp_string_drop(msg); } - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_pgp_is_valid_key( mut context: &dc_context_t, mut raw_key: *const dc_key_t, @@ -233,8 +241,11 @@ pub unsafe fn dc_pgp_is_valid_key( if !key.is_null() { rpgp::rpgp_key_drop(key); } - return key_is_valid; + + key_is_valid } + +// TODO should return bool /rtn pub unsafe fn dc_pgp_calc_fingerprint( context: &dc_context_t, raw_key: *const dc_key_t, @@ -276,8 +287,11 @@ pub unsafe fn dc_pgp_calc_fingerprint( if !fingerprint.is_null() { rpgp::rpgp_cvec_drop(fingerprint); } - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_pgp_split_key( mut context: &dc_context_t, mut private_in: *const dc_key_t, @@ -325,8 +339,11 @@ pub unsafe fn dc_pgp_split_key( if !buf.is_null() { rpgp::rpgp_cvec_drop(buf); } - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_pgp_pk_encrypt( mut context: &dc_context_t, mut plain_text: *const libc::c_void, @@ -338,7 +355,7 @@ pub unsafe fn dc_pgp_pk_encrypt( mut ret_ctext_bytes: *mut size_t, ) -> libc::c_int { let mut current_block: u64; - let mut i: libc::c_int = 0i32; + let mut i: libc::c_int; let mut success: libc::c_int = 0i32; let mut public_keys_len: libc::c_int = 0i32; let mut public_keys: *mut *mut rpgp::signed_public_key = 0 as *mut *mut rpgp::signed_public_key; @@ -406,7 +423,7 @@ pub unsafe fn dc_pgp_pk_encrypt( 2132137392766895896 => {} _ => { /* sign & encrypt */ - let mut op_clocks: libc::clock_t = 0i32 as libc::clock_t; + let mut op_clocks: libc::clock_t; let mut start: libc::clock_t = clock(); if private_key.is_null() { encrypted = rpgp::rpgp_encrypt_bytes_to_keys( @@ -493,8 +510,11 @@ pub unsafe fn dc_pgp_pk_encrypt( if !encrypted.is_null() { rpgp::rpgp_msg_drop(encrypted); } - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_pgp_pk_decrypt( mut context: &dc_context_t, mut ctext: *const libc::c_void, @@ -507,7 +527,7 @@ pub unsafe fn dc_pgp_pk_decrypt( mut ret_signature_fingerprints: *mut dc_hash_t, ) -> libc::c_int { let mut current_block: u64; - let mut i: libc::c_int = 0i32; + let mut i: libc::c_int; let mut success: libc::c_int = 0i32; let mut encrypted: *mut rpgp::Message = 0 as *mut rpgp::Message; let mut decrypted: *mut rpgp::message_decrypt_result = 0 as *mut rpgp::message_decrypt_result; @@ -654,9 +674,12 @@ pub unsafe fn dc_pgp_pk_decrypt( if !decrypted.is_null() { rpgp::rpgp_message_decrypt_result_drop(decrypted); } - return success; + + success } + /* symm. encryption */ +// TODO should return bool /rtn pub unsafe fn dc_pgp_symm_encrypt( mut context: &dc_context_t, mut passphrase: *const libc::c_char, @@ -686,8 +709,11 @@ pub unsafe fn dc_pgp_symm_encrypt( if !decrypted.is_null() { rpgp::rpgp_msg_drop(decrypted); } - return success; + + success } + +// TODO should return bool /rtn pub unsafe fn dc_pgp_symm_decrypt( mut context: &dc_context_t, mut passphrase: *const libc::c_char, @@ -696,9 +722,9 @@ pub unsafe fn dc_pgp_symm_decrypt( mut ret_plain_text: *mut *mut libc::c_void, mut ret_plain_bytes: *mut size_t, ) -> libc::c_int { - let mut decrypted_bytes: *mut rpgp::cvec = 0 as *mut rpgp::cvec; + let mut decrypted_bytes: *mut rpgp::cvec; let mut success: libc::c_int = 0i32; - let mut encrypted: *mut rpgp::Message = 0 as *mut rpgp::Message; + let mut encrypted: *mut rpgp::Message; let mut decrypted: *mut rpgp::Message = 0 as *mut rpgp::Message; encrypted = rpgp::rpgp_msg_from_bytes(ctext as *const uint8_t, ctext_bytes as usize); if !(0 != dc_pgp_handle_rpgp_error(context)) { @@ -719,5 +745,6 @@ pub unsafe fn dc_pgp_symm_decrypt( if !decrypted.is_null() { rpgp::rpgp_msg_drop(decrypted); } - return success; + + success } diff --git a/src/dc_qr.rs b/src/dc_qr.rs index 597615e9d..9bd5ec2ca 100644 --- a/src/dc_qr.rs +++ b/src/dc_qr.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_apeerstate::*; use crate::dc_chat::*; use crate::dc_contact::*; diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index a2668e2d7..3f5884e56 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -1,4 +1,10 @@ -use libc; +use mmime::mailimf::*; +use mmime::mailimf_types::*; +use mmime::mailmime::*; +use mmime::mailmime_content::*; +use mmime::mailmime_types::*; +use mmime::mmapstring::*; +use mmime::other::*; use crate::constants::*; use crate::dc_apeerstate::*; @@ -36,28 +42,28 @@ pub unsafe fn dc_receive_imf( /* the function returns the number of created messages in the database */ let mut incoming: libc::c_int = 1i32; let mut incoming_origin: libc::c_int = 0i32; - let mut to_ids: *mut dc_array_t = 0 as *mut dc_array_t; + let mut to_ids: *mut dc_array_t; let mut to_self: libc::c_int = 0i32; let mut from_id: uint32_t = 0i32 as uint32_t; let mut from_id_blocked: libc::c_int = 0i32; let mut to_id: uint32_t = 0i32 as uint32_t; let mut chat_id: uint32_t = 0i32 as uint32_t; let mut chat_id_blocked: libc::c_int = 0i32; - let mut state: libc::c_int = 0i32; + let mut state: libc::c_int; let mut hidden: libc::c_int = 0i32; - let mut msgrmsg: libc::c_int = 0i32; + let mut msgrmsg: libc::c_int; let mut add_delete_job: libc::c_int = 0i32; let mut insert_msg_id: uint32_t = 0i32 as uint32_t; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - let mut i: size_t = 0i32 as size_t; - let mut icnt: size_t = 0i32 as size_t; + let mut i: size_t; + let mut icnt: size_t; /* Message-ID from the header */ let mut rfc724_mid: *mut libc::c_char = 0 as *mut libc::c_char; let mut sort_timestamp: time_t = -1i32 as time_t; let mut sent_timestamp: time_t = -1i32 as time_t; let mut rcvd_timestamp: time_t = -1i32 as time_t; - let mut mime_parser: *mut dc_mimeparser_t = dc_mimeparser_new((*context).blobdir, context); - let mut field: *const mailimf_field = 0 as *const mailimf_field; + let mut mime_parser: *mut dc_mimeparser_t = dc_mimeparser_new(context.get_blobdir(), context); + let mut field: *const mailimf_field; let mut mime_in_reply_to: *mut libc::c_char = 0 as *mut libc::c_char; let mut mime_references: *mut libc::c_char = 0 as *mut libc::c_char; let mut created_db_entries: *mut carray = carray_new(16i32 as libc::c_uint); @@ -470,7 +476,7 @@ pub unsafe fn dc_receive_imf( ); let mut header_bytes: libc::c_int = imf_raw_bytes as libc::c_int; if 0 != save_mime_headers { - let mut p: *mut libc::c_char = 0 as *mut libc::c_char; + let mut p: *mut libc::c_char; p = strstr( imf_raw_not_terminated, b"\r\n\r\n\x00" as *const u8 as *const libc::c_char, @@ -750,7 +756,7 @@ pub unsafe fn dc_receive_imf( .first) .next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } .is_null() { @@ -768,7 +774,7 @@ pub unsafe fn dc_receive_imf( .first) .next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell }) .data } else { @@ -1001,7 +1007,6 @@ pub unsafe fn dc_receive_imf( if let Some(create_event_to_send) = create_event_to_send { let mut i_0: size_t = 0; let mut icnt_0: size_t = carray_count(created_db_entries) as size_t; - i_0 = 0i32 as size_t; while i_0 < icnt_0 { ((*context).cb)( context, @@ -1016,7 +1021,7 @@ pub unsafe fn dc_receive_imf( carray_free(created_db_entries); } if !rr_event_to_send.is_null() { - let mut i_1: size_t = 0; + let mut i_1: size_t; let mut icnt_1: size_t = carray_count(rr_event_to_send) as size_t; i_1 = 0i32 as size_t; while i_1 < icnt_1 { @@ -1098,15 +1103,15 @@ unsafe fn create_or_lookup_group( mut ret_chat_id: *mut uint32_t, mut ret_chat_id_blocked: *mut libc::c_int, ) { - let mut group_explicitly_left: libc::c_int = 0; + let mut group_explicitly_left: libc::c_int; let mut current_block: u64; let mut chat_id: uint32_t = 0i32 as uint32_t; let mut chat_id_blocked: libc::c_int = 0i32; let mut chat_id_verified: libc::c_int = 0i32; let mut grpid: *mut libc::c_char = 0 as *mut libc::c_char; let mut grpname: *mut libc::c_char = 0 as *mut libc::c_char; - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - let mut i: libc::c_int = 0i32; + let mut stmt: *mut sqlite3_stmt; + let mut i: libc::c_int; let mut to_ids_cnt: libc::c_int = dc_array_get_cnt(to_ids) as libc::c_int; let mut self_addr: *mut libc::c_char = 0 as *mut libc::c_char; let mut recreate_member_list: libc::c_int = 0i32; @@ -1130,8 +1135,8 @@ unsafe fn create_or_lookup_group( } set_better_msg(mime_parser, &mut better_msg); /* search the grpid in the header */ - let mut field: *mut mailimf_field = 0 as *mut mailimf_field; - let mut optional_field: *mut mailimf_optional_field = 0 as *mut mailimf_optional_field; + let mut field: *mut mailimf_field; + let mut optional_field: *mut mailimf_optional_field; optional_field = dc_mimeparser_lookup_optional_field( mime_parser, b"Chat-Group-ID\x00" as *const u8 as *const libc::c_char, @@ -1361,7 +1366,6 @@ unsafe fn create_or_lookup_group( create_verified, ); chat_id_blocked = create_blocked; - chat_id_verified = create_verified; recreate_member_list = 1i32; current_block = 200744462051969938; } @@ -1573,7 +1577,7 @@ unsafe fn create_or_lookup_adhoc_group( let mut member_ids: *mut dc_array_t = 0 as *mut dc_array_t; let mut chat_id: uint32_t = 0i32 as uint32_t; let mut chat_id_blocked = 0; - let mut i = 0; + let mut i; let mut chat_ids: *mut dc_array_t = 0 as *mut dc_array_t; let mut chat_ids_str: *mut libc::c_char = 0 as *mut libc::c_char; let mut q3: *mut libc::c_char = 0 as *mut libc::c_char; @@ -1679,7 +1683,7 @@ unsafe fn create_group_record( mut create_verified: libc::c_int, ) -> uint32_t { let mut chat_id: uint32_t = 0i32 as uint32_t; - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -1719,11 +1723,11 @@ unsafe fn create_adhoc_grp_id( let mut member_addrs: *mut dc_array_t = dc_array_new(23i32 as size_t); let mut member_ids_str: *mut libc::c_char = dc_array_get_string(member_ids, b",\x00" as *const u8 as *const libc::c_char); - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - let mut q3: *mut libc::c_char = 0 as *mut libc::c_char; - let mut addr: *mut libc::c_char = 0 as *mut libc::c_char; - let mut i: libc::c_int = 0i32; - let mut iCnt: libc::c_int = 0i32; + let mut stmt: *mut sqlite3_stmt; + let mut q3: *mut libc::c_char; + let mut addr: *mut libc::c_char; + let mut i: libc::c_int; + let mut iCnt: libc::c_int; let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; let mut member_cs: dc_strbuilder_t = dc_strbuilder_t { buf: 0 as *mut libc::c_char, @@ -1804,7 +1808,7 @@ unsafe fn search_chat_ids_by_contact_ids( let mut chat_ids: *mut dc_array_t = dc_array_new(23i32 as size_t); /* copy array, remove duplicates and SELF, sort by ID */ - let mut i: libc::c_int = 0; + let mut i: libc::c_int; let mut iCnt: libc::c_int = dc_array_get_cnt(unsorted_contact_ids) as libc::c_int; if !(iCnt <= 0i32) { i = 0i32; @@ -2036,7 +2040,7 @@ unsafe fn dc_is_reply_to_known_message( ) -> libc::c_int { /* check if the message is a reply to a known message; the replies are identified by the Message-ID from `In-Reply-To`/`References:` (to support non-Delta-Clients) or from `Chat-Predecessor:` (Delta clients, see comment in dc_chat.c) */ - let mut optional_field: *mut mailimf_optional_field = 0 as *mut mailimf_optional_field; + let mut optional_field: *mut mailimf_optional_field; optional_field = dc_mimeparser_lookup_optional_field( mime_parser, b"Chat-Predecessor\x00" as *const u8 as *const libc::c_char, @@ -2046,7 +2050,7 @@ unsafe fn dc_is_reply_to_known_message( return 1i32; } } - let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + let mut field: *mut mailimf_field; field = dc_mimeparser_lookup_field( mime_parser, b"In-Reply-To\x00" as *const u8 as *const libc::c_char, @@ -2084,7 +2088,7 @@ unsafe fn is_known_rfc724_mid_in_list( mut mid_list: *const clist, ) -> libc::c_int { if !mid_list.is_null() { - let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut cur: *mut clistiter; cur = (*mid_list).first; while !cur.is_null() { if 0 != is_known_rfc724_mid( @@ -2100,7 +2104,7 @@ unsafe fn is_known_rfc724_mid_in_list( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -2136,7 +2140,7 @@ unsafe fn dc_is_reply_to_messenger_message( - checks also if any of the referenced IDs are send by a messenger - it is okay, if the referenced messages are moved to trash here - no check for the Chat-* headers (function is only called if it is no messenger message itself) */ - let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + let mut field: *mut mailimf_field; field = dc_mimeparser_lookup_field( mime_parser, b"In-Reply-To\x00" as *const u8 as *const libc::c_char, @@ -2189,7 +2193,7 @@ unsafe fn is_msgrmsg_rfc724_mid_in_list( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -2264,7 +2268,7 @@ unsafe fn dc_add_or_lookup_contacts_by_address_list( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } @@ -2298,7 +2302,7 @@ unsafe fn dc_add_or_lookup_contacts_by_mailbox_list( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } diff --git a/src/dc_saxparser.rs b/src/dc_saxparser.rs index f9a1840cb..a78994fdc 100644 --- a/src/dc_saxparser.rs +++ b/src/dc_saxparser.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_tools::*; use crate::types::*; use crate::x::*; @@ -31,6 +29,7 @@ pub unsafe extern "C" fn dc_saxparser_init( (*saxparser).endtag_cb = Some(def_endtag_cb); (*saxparser).text_cb = Some(def_text_cb); } + unsafe fn def_text_cb(_userdata: *mut libc::c_void, _text: *const libc::c_char, _len: libc::c_int) { } @@ -65,6 +64,7 @@ pub unsafe fn dc_saxparser_set_tag_handler( Some(def_endtag_cb) }; } + pub unsafe fn dc_saxparser_set_text_handler( mut saxparser: *mut dc_saxparser_t, mut text_cb: dc_saxparser_text_cb_t, @@ -78,15 +78,16 @@ pub unsafe fn dc_saxparser_set_text_handler( Some(def_text_cb) }; } + pub unsafe fn dc_saxparser_parse( mut saxparser: *mut dc_saxparser_t, mut buf_start__: *const libc::c_char, ) { let mut current_block: u64; - let mut bak: libc::c_char = 0i32 as libc::c_char; - let mut buf_start: *mut libc::c_char = 0 as *mut libc::c_char; - let mut last_text_start: *mut libc::c_char = 0 as *mut libc::c_char; - let mut p: *mut libc::c_char = 0 as *mut libc::c_char; + let mut bak: libc::c_char; + let mut buf_start: *mut libc::c_char; + let mut last_text_start: *mut libc::c_char; + let mut p: *mut libc::c_char; /* attributes per tag - a fixed border here is a security feature, not a limit */ /* attributes as key/value pairs, +1 for terminating the list */ let mut attr: [*mut libc::c_char; 202] = [0 as *mut libc::c_char; 202]; @@ -214,7 +215,7 @@ pub unsafe fn dc_saxparser_parse( { let mut beg_attr_name: *mut libc::c_char = p; let mut beg_attr_value: *mut libc::c_char = 0 as *mut libc::c_char; - let mut beg_attr_value_new: *mut libc::c_char = 0 as *mut libc::c_char; + let mut beg_attr_value_new: *mut libc::c_char; if '=' as i32 == *beg_attr_name as libc::c_int { p = p.offset(1isize) } else { @@ -356,6 +357,7 @@ pub unsafe fn dc_saxparser_parse( do_free_attr(attr.as_mut_ptr(), free_attr.as_mut_ptr()); free(buf_start as *mut libc::c_void); } + unsafe fn do_free_attr(mut attr: *mut *mut libc::c_char, mut free_attr: *mut libc::c_int) { /* "attr" are key/value pairs; the function frees the data if the corresponding bit in "free_attr" is set. (we need this as we try to use the strings from the "main" document instead of allocating small strings) */ @@ -376,6 +378,7 @@ unsafe fn do_free_attr(mut attr: *mut *mut libc::c_char, mut free_attr: *mut lib let ref mut fresh0 = *attr.offset(0isize); *fresh0 = 0 as *mut libc::c_char; } + unsafe fn call_text_cb( mut saxparser: *mut dc_saxparser_t, mut text: *mut libc::c_char, @@ -384,7 +387,7 @@ unsafe fn call_text_cb( ) { if !text.is_null() && 0 != len { let mut bak: libc::c_char = *text.offset(len as isize); - let mut text_new: *mut libc::c_char = 0 as *mut libc::c_char; + let mut text_new: *mut libc::c_char; *text.offset(len as isize) = '\u{0}' as i32 as libc::c_char; text_new = xml_decode(text, type_0); (*saxparser).text_cb.expect("non-null function pointer")( @@ -398,6 +401,7 @@ unsafe fn call_text_cb( *text.offset(len as isize) = bak }; } + /* Convert entities as ä to UTF-8 characters. - The first strings MUST NOT start with `&` and MUST end with `;`. @@ -421,10 +425,10 @@ unsafe fn xml_decode(mut s: *mut libc::c_char, mut type_0: libc::c_char) -> *mut let mut e: *mut libc::c_char = 0 as *mut libc::c_char; let mut r: *mut libc::c_char = s; let mut original_buf: *const libc::c_char = s; - let mut b = 0; - let mut c: isize = 0; - let mut d: isize = 0; - let mut l: isize = 0; + let mut b; + let mut c: isize; + let mut d: isize; + let mut l: isize; while 0 != *s { while *s as libc::c_int == '\r' as i32 { let fresh1 = s; @@ -547,8 +551,10 @@ unsafe fn xml_decode(mut s: *mut libc::c_char, mut type_0: libc::c_char) -> *mut s = s.offset(1isize) } } - return r; + + r } + /* dc_saxparser_t parses XML and HTML files that may not be wellformed and spits out all text and tags found. @@ -1077,6 +1083,7 @@ static mut s_ent: [*const libc::c_char; 508] = [ 0 as *const libc::c_char, 0 as *const libc::c_char, ]; + pub unsafe fn dc_attr_find( mut attr: *mut *mut libc::c_char, mut key: *const libc::c_char, @@ -1090,5 +1097,6 @@ pub unsafe fn dc_attr_find( return *attr.offset((i + 1i32) as isize); } } - return 0 as *const libc::c_char; + + 0 as *const libc::c_char } diff --git a/src/dc_securejoin.rs b/src/dc_securejoin.rs index 61ca43a93..b9e243cca 100644 --- a/src/dc_securejoin.rs +++ b/src/dc_securejoin.rs @@ -1,4 +1,4 @@ -use libc; +use mmime::mailimf_types::*; use crate::constants::Event; use crate::dc_apeerstate::*; @@ -34,13 +34,13 @@ pub unsafe fn dc_get_securejoin_qr( ==== Step 1 in "Setup verified contact" protocol ==== ========================================================= */ let mut qr: *mut libc::c_char = 0 as *mut libc::c_char; - let mut self_addr: *mut libc::c_char = 0 as *mut libc::c_char; + let mut self_addr: *mut libc::c_char; let mut self_addr_urlencoded: *mut libc::c_char = 0 as *mut libc::c_char; let mut self_name: *mut libc::c_char = 0 as *mut libc::c_char; let mut self_name_urlencoded: *mut libc::c_char = 0 as *mut libc::c_char; let mut fingerprint: *mut libc::c_char = 0 as *mut libc::c_char; - let mut invitenumber: *mut libc::c_char = 0 as *mut libc::c_char; - let mut auth: *mut libc::c_char = 0 as *mut libc::c_char; + let mut invitenumber: *mut libc::c_char; + let mut auth: *mut libc::c_char; let mut chat: *mut dc_chat_t = 0 as *mut dc_chat_t; let mut group_name: *mut libc::c_char = 0 as *mut libc::c_char; let mut group_name_urlencoded: *mut libc::c_char = 0 as *mut libc::c_char; @@ -148,7 +148,7 @@ pub unsafe fn dc_get_securejoin_qr( } unsafe fn get_self_fingerprint(mut context: &dc_context_t) -> *mut libc::c_char { - let mut self_addr: *mut libc::c_char = 0 as *mut libc::c_char; + let mut self_addr: *mut libc::c_char; let mut self_key: *mut dc_key_t = dc_key_new(); let mut fingerprint: *mut libc::c_char = 0 as *mut libc::c_char; self_addr = dc_sqlite3_get_config( @@ -170,8 +170,10 @@ unsafe fn get_self_fingerprint(mut context: &dc_context_t) -> *mut libc::c_char } free(self_addr as *mut libc::c_void); dc_key_unref(self_key); - return fingerprint; + + fingerprint } + pub unsafe fn dc_join_securejoin( mut context: &dc_context_t, mut qr: *const libc::c_char, @@ -181,7 +183,7 @@ pub unsafe fn dc_join_securejoin( ==== Step 2 in "Setup verified contact" protocol ===== ========================================================== */ let mut ret_chat_id: libc::c_int = 0i32; - let mut ongoing_allocated: libc::c_int = 0i32; + let mut ongoing_allocated: libc::c_int; let mut contact_chat_id: uint32_t = 0i32 as uint32_t; let mut join_vg: libc::c_int = 0i32; let mut qr_scan: *mut dc_lot_t = 0 as *mut dc_lot_t; @@ -341,6 +343,7 @@ unsafe fn send_handshake_msg( dc_send_msg(context, contact_chat_id, msg); dc_msg_unref(msg); } + unsafe fn chat_id_2_contact_id( mut context: &dc_context_t, mut contact_chat_id: uint32_t, @@ -351,8 +354,10 @@ unsafe fn chat_id_2_contact_id( contact_id = dc_array_get_id(contacts, 0i32 as size_t) } dc_array_unref(contacts); - return contact_id; + + contact_id } + unsafe fn fingerprint_equals_sender( mut context: &dc_context_t, mut fingerprint: *const libc::c_char, @@ -385,8 +390,10 @@ unsafe fn fingerprint_equals_sender( free(fingerprint_normalized as *mut libc::c_void); dc_contact_unref(contact); dc_array_unref(contacts); - return fingerprint_equal; + + fingerprint_equal } + /* library private: secure-join */ pub unsafe fn dc_handle_securejoin_handshake( context: &dc_context_t, @@ -394,8 +401,8 @@ pub unsafe fn dc_handle_securejoin_handshake( contact_id: uint32_t, ) -> libc::c_int { let mut current_block: u64; - let mut step: *const libc::c_char = 0 as *const libc::c_char; - let mut join_vg: libc::c_int = 0i32; + let mut step: *const libc::c_char; + let mut join_vg: libc::c_int; let mut scanned_fingerprint_of_alice: *mut libc::c_char = 0 as *mut libc::c_char; let mut auth: *mut libc::c_char = 0 as *mut libc::c_char; let mut own_fingerprint: *mut libc::c_char = 0 as *mut libc::c_char; @@ -441,7 +448,7 @@ pub unsafe fn dc_handle_securejoin_handshake( // it just ensures, we have Bobs key now. If we do _not_ have the key because eg. MitM has removed it, // send_message() will fail with the error "End-to-end-encryption unavailable unexpectedly.", so, there is no additional check needed here. // verify that the `Secure-Join-Invitenumber:`-header matches invitenumber written to the QR code - let mut invitenumber: *const libc::c_char = 0 as *const libc::c_char; + let mut invitenumber: *const libc::c_char; invitenumber = lookup_field( mimeparser, b"Secure-Join-Invitenumber\x00" as *const u8 as *const libc::c_char, @@ -588,7 +595,7 @@ pub unsafe fn dc_handle_securejoin_handshake( ==== Step 6 in "Out-of-band verified groups" protocol ==== ============================================================ */ // verify that Secure-Join-Fingerprint:-header matches the fingerprint of Bob - let mut fingerprint: *const libc::c_char = 0 as *const libc::c_char; + let mut fingerprint: *const libc::c_char; fingerprint = lookup_field( mimeparser, b"Secure-Join-Fingerprint\x00" as *const u8 as *const libc::c_char, @@ -622,7 +629,7 @@ pub unsafe fn dc_handle_securejoin_handshake( b"Fingerprint verified.\x00" as *const u8 as *const libc::c_char, ); // verify that the `Secure-Join-Auth:`-header matches the secret written to the QR code - let mut auth_0: *const libc::c_char = 0 as *const libc::c_char; + let mut auth_0: *const libc::c_char; auth_0 = lookup_field( mimeparser, b"Secure-Join-Auth\x00" as *const u8 as *const libc::c_char, @@ -896,12 +903,15 @@ pub unsafe fn dc_handle_securejoin_handshake( free(auth as *mut libc::c_void); free(own_fingerprint as *mut libc::c_void); free(grpid as *mut libc::c_void); - return ret; + + ret } + unsafe fn end_bobs_joining(context: &dc_context_t, status: libc::c_int) { context.bob.clone().write().unwrap().status = status; dc_stop_ongoing_process(context); } + unsafe fn secure_connection_established(mut context: &dc_context_t, mut contact_chat_id: uint32_t) { let mut contact_id: uint32_t = chat_id_2_contact_id(context, contact_chat_id); let mut contact: *mut dc_contact_t = dc_get_contact(context, contact_id); @@ -924,6 +934,7 @@ unsafe fn secure_connection_established(mut context: &dc_context_t, mut contact_ free(msg as *mut libc::c_void); dc_contact_unref(contact); } + unsafe fn lookup_field( mut mimeparser: *mut dc_mimeparser_t, mut key: *const libc::c_char, @@ -940,8 +951,10 @@ unsafe fn lookup_field( { return 0 as *const libc::c_char; } - return value; + + value } + unsafe fn could_not_establish_secure_connection( context: &dc_context_t, contact_chat_id: uint32_t, @@ -969,6 +982,7 @@ unsafe fn could_not_establish_secure_connection( free(msg as *mut libc::c_void); dc_contact_unref(contact); } + unsafe fn mark_peer_as_verified( context: &dc_context_t, fingerprint: *const libc::c_char, @@ -990,11 +1004,15 @@ unsafe fn mark_peer_as_verified( } } dc_apeerstate_unref(peerstate); - return success; + + success } + /* ****************************************************************************** * Tools: Misc. ******************************************************************************/ + +// TODO should return bool unsafe fn encrypted_and_signed( mut mimeparser: *mut dc_mimeparser_t, mut expected_fingerprint: *const libc::c_char, @@ -1039,14 +1057,16 @@ unsafe fn encrypted_and_signed( ); return 0i32; } - return 1i32; + + 1 } + pub unsafe fn dc_handle_degrade_event( mut context: &dc_context_t, mut peerstate: *mut dc_apeerstate_t, ) { - let mut stmt = 0 as *mut sqlite3_stmt; - let mut contact_id: uint32_t = 0i32 as uint32_t; + let mut stmt; + let mut contact_id: uint32_t; let mut contact_chat_id: uint32_t = 0i32 as uint32_t; if !peerstate.is_null() { // - we do not issue an warning for DC_DE_ENCRYPTION_PAUSED as this is quite normal diff --git a/src/dc_simplify.rs b/src/dc_simplify.rs index 69bd03e75..340b9ccd1 100644 --- a/src/dc_simplify.rs +++ b/src/dc_simplify.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_dehtml::*; use crate::dc_strbuilder::*; use crate::dc_tools::*; @@ -15,19 +13,22 @@ pub struct dc_simplify_t { } pub unsafe fn dc_simplify_new() -> *mut dc_simplify_t { - let mut simplify: *mut dc_simplify_t = 0 as *mut dc_simplify_t; + let mut simplify: *mut dc_simplify_t; simplify = calloc(1, ::std::mem::size_of::()) as *mut dc_simplify_t; if simplify.is_null() { exit(31i32); } - return simplify; + + simplify } + pub unsafe fn dc_simplify_unref(mut simplify: *mut dc_simplify_t) { if simplify.is_null() { return; } free(simplify as *mut libc::c_void); } + /* Simplify and normalise text: Remove quotes, signatures, unnecessary lineends etc. The data returned from Simplify() must be free()'d when no longer used, private */ @@ -39,8 +40,8 @@ pub unsafe fn dc_simplify_simplify( mut is_msgrmsg: libc::c_int, ) -> *mut libc::c_char { /* create a copy of the given buffer */ - let mut out: *mut libc::c_char = 0 as *mut libc::c_char; - let mut temp: *mut libc::c_char = 0 as *mut libc::c_char; + let mut out: *mut libc::c_char; + let mut temp: *mut libc::c_char; if simplify.is_null() || in_unterminated.is_null() || in_bytes <= 0i32 { return dc_strdup(b"\x00" as *const u8 as *const libc::c_char); } @@ -68,8 +69,10 @@ pub unsafe fn dc_simplify_simplify( out = temp } dc_remove_cr_chars(out); - return out; + + out } + /* ****************************************************************************** * Simplify Plain Text ******************************************************************************/ @@ -85,12 +88,12 @@ unsafe fn dc_simplify_simplify_plain_text( ... remove a non-empty line before the removed quote (contains sth. like "On 2.9.2016, Bjoern wrote:" in different formats and lanugages) */ /* split the given buffer into lines */ let mut lines: *mut carray = dc_split_into_lines(buf_terminated); - let mut l: libc::c_int = 0i32; + let mut l: libc::c_int; let mut l_first: libc::c_int = 0i32; /* if l_last is -1, there are no lines */ let mut l_last: libc::c_int = carray_count(lines).wrapping_sub(1i32 as libc::c_uint) as libc::c_int; - let mut line: *mut libc::c_char = 0 as *mut libc::c_char; + let mut line: *mut libc::c_char; let mut footer_mark: libc::c_int = 0i32; l = l_first; while l <= l_last { @@ -246,11 +249,14 @@ unsafe fn dc_simplify_simplify_plain_text( dc_strbuilder_cat(&mut ret, b" [...]\x00" as *const u8 as *const libc::c_char); } dc_free_splitted_lines(lines); - return ret.buf; + + ret.buf } + /* ****************************************************************************** * Tools ******************************************************************************/ +// TODO should return bool /rtn unsafe fn is_empty_line(mut buf: *const libc::c_char) -> libc::c_int { /* force unsigned - otherwise the `> ' '` comparison will fail */ let mut p1: *const libc::c_uchar = buf as *const libc::c_uchar; @@ -260,8 +266,11 @@ unsafe fn is_empty_line(mut buf: *const libc::c_char) -> libc::c_int { } p1 = p1.offset(1isize) } - return 1i32; + + 1 } + +// TODO should return bool /rtn unsafe fn is_quoted_headline(mut buf: *const libc::c_char) -> libc::c_int { /* This function may be called for the line _directly_ before a quote. The function checks if the line contains sth. like "On 01.02.2016, xy@z wrote:" in various languages. @@ -274,11 +283,15 @@ unsafe fn is_quoted_headline(mut buf: *const libc::c_char) -> libc::c_int { if buf_len > 0i32 && *buf.offset((buf_len - 1i32) as isize) as libc::c_int == ':' as i32 { return 1i32; } - return 0i32; + + 0 } + +// TODO should return bool /rtn unsafe fn is_plain_quote(mut buf: *const libc::c_char) -> libc::c_int { if *buf.offset(0isize) as libc::c_int == '>' as i32 { return 1i32; } - return 0i32; + + 0 } diff --git a/src/dc_smtp.rs b/src/dc_smtp.rs index f4538d5c8..b2d0f1783 100644 --- a/src/dc_smtp.rs +++ b/src/dc_smtp.rs @@ -2,7 +2,6 @@ use std::ffi::{CStr, CString}; use lettre::smtp::client::net::*; use lettre::*; -use libc; use native_tls::TlsConnector; use crate::constants::Event; diff --git a/src/dc_sqlite3.rs b/src/dc_sqlite3.rs index b7cdce76c..c13ba0a97 100644 --- a/src/dc_sqlite3.rs +++ b/src/dc_sqlite3.rs @@ -1,5 +1,4 @@ -use libc; - +use crate::constants::*; use crate::dc_apeerstate::*; use crate::dc_context::dc_context_t; use crate::dc_hash::*; @@ -10,7 +9,6 @@ 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)] @@ -900,7 +898,6 @@ pub unsafe fn dc_sqlite3_open( b"ALTER TABLE locations ADD COLUMN independent INTEGER DEFAULT 0;\x00" as *const u8 as *const libc::c_char ); - dbversion = 55; dc_sqlite3_set_config_int( context, sql, @@ -936,7 +933,7 @@ pub unsafe fn dc_sqlite3_open( context, sql, b"backup_for\x00" as *const u8 as *const libc::c_char, - context.blobdir, + context.get_blobdir(), ); dc_ensure_no_slash(repl_from); if 0 != !('f' as i32 == 'f' as i32) as libc::c_int as libc::c_long { @@ -1018,8 +1015,8 @@ pub unsafe fn dc_sqlite3_set_config( key: *const libc::c_char, value: *const libc::c_char, ) -> libc::c_int { - let mut state = 0; - let mut stmt = 0 as *mut sqlite3_stmt; + let mut state; + let mut stmt; if key.is_null() { dc_log_error( context, @@ -1135,7 +1132,7 @@ pub unsafe extern "C" fn dc_sqlite3_log_error( msg_format: *const libc::c_char, va: ... ) { - let mut msg = 0 as *mut libc::c_char; + let mut msg; if msg_format.is_null() { return; } @@ -1174,7 +1171,7 @@ pub unsafe fn dc_sqlite3_get_config( key: *const libc::c_char, def: *const libc::c_char, ) -> *mut libc::c_char { - let mut stmt = 0 as *mut sqlite3_stmt; + let mut stmt; if 0 == dc_sqlite3_is_open(sql) || key.is_null() { return dc_strdup_keep_null(def); } @@ -1202,7 +1199,7 @@ pub unsafe fn dc_sqlite3_execute( querystr: *const libc::c_char, ) -> libc::c_int { let mut success = 0; - let mut sqlState = 0; + let mut sqlState; let stmt = dc_sqlite3_prepare(context, sql, querystr); if !stmt.is_null() { sqlState = sqlite3_step(stmt); @@ -1262,7 +1259,7 @@ pub unsafe fn dc_sqlite3_table_exists( ) -> libc::c_int { let mut ret = 0; let mut stmt = 0 as *mut sqlite3_stmt; - let mut sqlState = 0; + let mut sqlState; let querystr = sqlite3_mprintf( b"PRAGMA table_info(%s)\x00" as *const u8 as *const libc::c_char, @@ -1339,7 +1336,7 @@ pub unsafe fn dc_sqlite3_try_execute( ) -> libc::c_int { // same as dc_sqlite3_execute() but does not pass error to ui let mut success = 0; - let mut sql_state = 0; + let mut sql_state; let stmt = dc_sqlite3_prepare(context, sql, querystr); if !stmt.is_null() { sql_state = sqlite3_step(stmt); @@ -1416,9 +1413,9 @@ pub unsafe fn dc_sqlite3_get_rowid2( } pub unsafe fn dc_housekeeping(context: &dc_context_t) { - let mut stmt = 0 as *mut sqlite3_stmt; - let mut dir_handle = 0 as *mut DIR; - let mut dir_entry = 0 as *mut dirent; + let mut stmt; + let mut dir_handle; + let mut dir_entry; let mut files_in_use = dc_hash_t { keyClass: 0, copyKey: 0, @@ -1478,13 +1475,13 @@ pub unsafe fn dc_housekeeping(context: &dc_context_t) { files_in_use.count as libc::c_int, ); /* go through directory and delete unused files */ - dir_handle = opendir(context.blobdir); + dir_handle = opendir(context.get_blobdir()); if dir_handle.is_null() { dc_log_warning( context, 0, b"Housekeeping: Cannot open %s.\x00" as *const u8 as *const libc::c_char, - context.blobdir, + context.get_blobdir(), ); } else { /* avoid deletion of files that are just created to build a message object */ @@ -1529,7 +1526,7 @@ pub unsafe fn dc_housekeeping(context: &dc_context_t) { free(path as *mut libc::c_void); path = dc_mprintf( b"%s/%s\x00" as *const u8 as *const libc::c_char, - context.blobdir, + context.get_blobdir(), name, ); diff --git a/src/dc_stock.rs b/src/dc_stock.rs index d167f45b7..195dced53 100644 --- a/src/dc_stock.rs +++ b/src/dc_stock.rs @@ -1,5 +1,3 @@ -use libc; - use crate::constants::Event; use crate::dc_contact::*; use crate::dc_context::dc_context_t; @@ -18,7 +16,7 @@ unsafe fn get_string( mut id: libc::c_int, mut qty: libc::c_int, ) -> *mut libc::c_char { - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; + let mut ret: *mut libc::c_char; ret = ((*context).cb)( context, @@ -30,12 +28,15 @@ unsafe fn get_string( if ret.is_null() { ret = default_string(id) } - return ret; + + ret } + /* Add translated strings that are used by the messager backend. As the logging functions may use these strings, do not log any errors from here. */ unsafe fn default_string(mut id: libc::c_int) -> *mut libc::c_char { + // TODO match on enum values /rtn match id { 1 => { return dc_strdup(b"No messages.\x00" as *const u8 as @@ -208,8 +209,10 @@ unsafe fn default_string(mut id: libc::c_int) -> *mut libc::c_char { } _ => { } } - return dc_strdup(b"ErrStr\x00" as *const u8 as *const libc::c_char); + + dc_strdup(b"ErrStr\x00" as *const u8 as *const 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( @@ -228,8 +231,10 @@ pub unsafe fn dc_stock_str_repl_string( b"%1$d\x00" as *const u8 as *const libc::c_char, to_insert, ); - return ret; + + ret } + pub unsafe fn dc_stock_str_repl_int( mut context: &dc_context_t, mut id: libc::c_int, @@ -251,8 +256,10 @@ pub unsafe fn dc_stock_str_repl_int( to_insert_str, ); free(to_insert_str as *mut libc::c_void); - return ret; + + ret } + /* 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( @@ -282,8 +289,10 @@ pub unsafe fn dc_stock_str_repl_string2( b"%2$d\x00" as *const u8 as *const libc::c_char, to_insert2, ); - return ret; + + ret } + /* Misc. */ pub unsafe fn dc_stock_system_msg( mut context: &dc_context_t, @@ -292,7 +301,7 @@ pub unsafe fn dc_stock_system_msg( mut param2: *const libc::c_char, mut from_id: uint32_t, ) -> *mut libc::c_char { - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; + let mut ret: *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; let mut from_contact: *mut dc_contact_t = 0 as *mut dc_contact_t; @@ -332,5 +341,6 @@ pub unsafe fn dc_stock_system_msg( free(mod_displayname as *mut libc::c_void); dc_contact_unref(from_contact); dc_contact_unref(mod_contact); - return ret; + + ret } diff --git a/src/dc_strbuilder.rs b/src/dc_strbuilder.rs index 2b729935f..5f6974a98 100644 --- a/src/dc_strbuilder.rs +++ b/src/dc_strbuilder.rs @@ -1,5 +1,3 @@ -use libc; - use crate::x::*; #[derive(Copy, Clone)] diff --git a/src/dc_strencode.rs b/src/dc_strencode.rs index d1edf2e9f..18280e53e 100644 --- a/src/dc_strencode.rs +++ b/src/dc_strencode.rs @@ -1,17 +1,10 @@ -use libc; +use mmime::charconv::*; +use mmime::mailmime_decode::*; +use mmime::mmapstring::*; +use mmime::other::*; use crate::dc_tools::*; use crate::types::*; -use crate::x::*; - -#[inline] -unsafe fn __isctype(mut _c: __darwin_ct_rune_t, mut _f: libc::c_ulong) -> __darwin_ct_rune_t { - return if _c < 0i32 || _c >= 1i32 << 8i32 { - 0i32 - } else { - (0 != _DefaultRuneLocale.__runetype[_c as usize] as libc::c_ulong & _f) as libc::c_int - }; -} #[inline] pub fn isalnum(mut _c: libc::c_int) -> libc::c_int { @@ -70,8 +63,10 @@ pub unsafe extern "C" fn dc_urlencode(mut to_encode: *const libc::c_char) -> *mu pstr = pstr.offset(1isize) } *pbuf = '\u{0}' as i32 as libc::c_char; - return buf; + + buf } + /* ****************************************************************************** * URL encoding and decoding, RFC 3986 ******************************************************************************/ @@ -79,8 +74,10 @@ unsafe fn int_2_uppercase_hex(mut code: libc::c_char) -> libc::c_char { static mut hex: [libc::c_char; 17] = [ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 0, ]; - return hex[(code as libc::c_int & 15i32) as usize]; + + hex[(code as libc::c_int & 15i32) as usize] } + pub unsafe fn dc_urldecode(mut to_decode: *const libc::c_char) -> *mut libc::c_char { let mut pstr: *const libc::c_char = to_decode; if to_decode.is_null() { @@ -114,15 +111,18 @@ pub unsafe fn dc_urldecode(mut to_decode: *const libc::c_char) -> *mut libc::c_c pstr = pstr.offset(1isize) } *pbuf = '\u{0}' as i32 as libc::c_char; - return buf; + + buf } + unsafe fn hex_2_int(mut ch: libc::c_char) -> libc::c_char { return (if 0 != isdigit(ch as libc::c_int) { ch as libc::c_int - '0' as i32 } else { - tolower(ch as libc::c_int) - 'a' as i32 + 10i32 + libc::tolower(ch as libc::c_int) - 'a' as i32 + 10i32 }) as libc::c_char; } + pub unsafe fn dc_encode_header_words(mut to_encode: *const libc::c_char) -> *mut libc::c_char { let mut current_block: u64; let mut ret_str: *mut libc::c_char = 0 as *mut libc::c_char; @@ -143,10 +143,10 @@ pub unsafe fn dc_encode_header_words(mut to_encode: *const libc::c_char) -> *mut } _ => { if *cur as libc::c_int != '\u{0}' as i32 { - let mut begin: *const libc::c_char = 0 as *const libc::c_char; - let mut end: *const libc::c_char = 0 as *const libc::c_char; - let mut do_quote: libc::c_int = 0; - let mut quote_words: libc::c_int = 0; + let mut begin: *const libc::c_char; + let mut end: *const libc::c_char; + let mut do_quote: libc::c_int; + let mut quote_words: libc::c_int; begin = cur; end = begin; quote_words = 0i32; @@ -218,15 +218,18 @@ pub unsafe fn dc_encode_header_words(mut to_encode: *const libc::c_char) -> *mut } } } - return ret_str; + + ret_str } + +// TODO return bool /rtn unsafe fn quote_word( mut display_charset: *const libc::c_char, mut mmapstr: *mut MMAPString, mut word: *const libc::c_char, mut size: size_t, ) -> libc::c_int { - let mut cur: *const libc::c_char = 0 as *const libc::c_char; + let mut cur: *const libc::c_char; let mut i: size_t = 0i32 as size_t; let mut hex: [libc::c_char; 4] = [0; 4]; // let mut col: libc::c_int = 0i32; @@ -241,10 +244,8 @@ unsafe fn quote_word( } // col = (*mmapstr).len as libc::c_int; cur = word; - i = 0i32 as size_t; while i < size { let mut do_quote_char: libc::c_int = 0; - do_quote_char = 0i32; match *cur as libc::c_int { 44 | 58 | 33 | 34 | 35 | 36 | 64 | 91 | 92 | 93 | 94 | 96 | 123 | 124 | 125 | 126 | 61 | 63 | 95 => do_quote_char = 1i32, @@ -281,12 +282,14 @@ unsafe fn quote_word( if mmap_string_append(mmapstr, b"?=\x00" as *const u8 as *const libc::c_char).is_null() { return 0i32; } - return 1i32; + + 1 } + unsafe fn get_word( mut begin: *const libc::c_char, mut pend: *mut *const libc::c_char, - mut pto_be_quoted: *mut libc::c_int, + mut pto_be_quoted: *mut libc::c_int, // TODO should be bool /rtn ) { let mut cur: *const libc::c_char = begin; while *cur as libc::c_int != ' ' as i32 @@ -301,14 +304,16 @@ unsafe fn get_word( ); *pend = cur; } + /* ****************************************************************************** * Encode/decode header words, RFC 2047 ******************************************************************************/ + /* see comment below */ +// TODO should be bool /rtn unsafe fn to_be_quoted(mut word: *const libc::c_char, mut size: size_t) -> libc::c_int { let mut cur: *const libc::c_char = word; let mut i: size_t = 0i32 as size_t; - i = 0i32 as size_t; while i < size { match *cur as libc::c_int { 44 | 58 | 33 | 34 | 35 | 36 | 64 | 91 | 92 | 93 | 94 | 96 | 123 | 124 | 125 | 126 @@ -322,8 +327,10 @@ unsafe fn to_be_quoted(mut word: *const libc::c_char, mut size: size_t) -> libc: cur = cur.offset(1isize); i = i.wrapping_add(1) } - return 0i32; + + 0 } + pub unsafe fn dc_decode_header_words(mut in_0: *const libc::c_char) -> *mut libc::c_char { if in_0.is_null() { return 0 as *mut libc::c_char; @@ -341,22 +348,24 @@ pub unsafe fn dc_decode_header_words(mut in_0: *const libc::c_char) -> *mut libc if r != MAILIMF_NO_ERROR as libc::c_int || out.is_null() { out = dc_strdup(in_0) } - return out; + + out } + pub unsafe fn dc_encode_modified_utf7( mut to_encode: *const libc::c_char, mut change_spaces: libc::c_int, ) -> *mut libc::c_char { - let mut utf8pos: libc::c_uint = 0i32 as libc::c_uint; - let mut utf8total: libc::c_uint = 0i32 as libc::c_uint; - let mut c: libc::c_uint = 0i32 as libc::c_uint; - let mut utf7mode: libc::c_uint = 0i32 as libc::c_uint; - let mut bitstogo: libc::c_uint = 0i32 as libc::c_uint; - let mut utf16flag: libc::c_uint = 0i32 as libc::c_uint; + let mut utf8pos: libc::c_uint; + let mut utf8total: libc::c_uint; + let mut c: libc::c_uint; + let mut utf7mode: libc::c_uint; + let mut bitstogo: libc::c_uint; + let mut utf16flag: libc::c_uint; let mut ucs4: libc::c_ulong = 0; let mut bitbuf: libc::c_ulong = 0; - let mut dst: *mut libc::c_char = 0 as *mut libc::c_char; - let mut res: *mut libc::c_char = 0 as *mut libc::c_char; + let mut dst: *mut libc::c_char; + let mut res: *mut libc::c_char; if to_encode.is_null() { return dc_strdup(b"\x00" as *const u8 as *const libc::c_char); } @@ -479,31 +488,35 @@ pub unsafe fn dc_encode_modified_utf7( *fresh16 = '-' as i32 as libc::c_char } *dst = '\u{0}' as i32 as libc::c_char; - return res; + + res } + /* ****************************************************************************** * Encode/decode modified UTF-7 as needed for IMAP, see RFC 2192 ******************************************************************************/ + // UTF7 modified base64 alphabet static mut base64chars: [libc::c_char; 65] = [ 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 44, 0, ]; + pub unsafe fn dc_decode_modified_utf7( mut to_decode: *const libc::c_char, mut change_spaces: libc::c_int, ) -> *mut libc::c_char { - let mut c: libc::c_uint = 0i32 as libc::c_uint; - let mut i: libc::c_uint = 0i32 as libc::c_uint; - let mut bitcount: libc::c_uint = 0i32 as libc::c_uint; - let mut ucs4: libc::c_ulong = 0; - let mut utf16: libc::c_ulong = 0; - let mut bitbuf: libc::c_ulong = 0; + let mut c: libc::c_uint; + let mut i: libc::c_uint; + let mut bitcount: libc::c_uint; + let mut ucs4: libc::c_ulong; + let mut utf16: libc::c_ulong; + let mut bitbuf: libc::c_ulong; let mut base64: [libc::c_uchar; 256] = [0; 256]; - let mut src: *const libc::c_char = 0 as *const libc::c_char; - let mut dst: *mut libc::c_char = 0 as *mut libc::c_char; - let mut res: *mut libc::c_char = 0 as *mut libc::c_char; + let mut src: *const libc::c_char; + let mut dst: *mut libc::c_char; + let mut res: *mut libc::c_char; if to_decode.is_null() { return dc_strdup(b"\x00" as *const u8 as *const libc::c_char); } @@ -599,8 +612,11 @@ pub unsafe fn dc_decode_modified_utf7( } } *dst = '\u{0}' as i32 as libc::c_char; - return res; + + res } + +// TODO should be bool /rtn pub unsafe fn dc_needs_ext_header(mut to_check: *const libc::c_char) -> libc::c_int { if !to_check.is_null() { while 0 != *to_check { @@ -615,8 +631,10 @@ pub unsafe fn dc_needs_ext_header(mut to_check: *const libc::c_char) -> libc::c_ to_check = to_check.offset(1isize) } } - return 0i32; + + 0 } + pub unsafe fn dc_encode_ext_header(mut to_encode: *const libc::c_char) -> *mut libc::c_char { let mut pstr: *const libc::c_char = to_encode; if to_encode.is_null() { @@ -657,12 +675,14 @@ pub unsafe fn dc_encode_ext_header(mut to_encode: *const libc::c_char) -> *mut l pstr = pstr.offset(1isize) } *pbuf = '\u{0}' as i32 as libc::c_char; - return buf; + + buf } + pub unsafe fn dc_decode_ext_header(mut to_decode: *const libc::c_char) -> *mut libc::c_char { let mut decoded: *mut libc::c_char = 0 as *mut libc::c_char; let mut charset: *mut libc::c_char = 0 as *mut libc::c_char; - let mut p2: *const libc::c_char = 0 as *const libc::c_char; + let mut p2: *const libc::c_char; if !to_decode.is_null() { // get char set p2 = strchr(to_decode, '\'' as i32); diff --git a/src/dc_token.rs b/src/dc_token.rs index 37881ae6f..6818a6d79 100644 --- a/src/dc_token.rs +++ b/src/dc_token.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_context::dc_context_t; use crate::dc_sqlite3::*; use crate::dc_tools::*; @@ -39,8 +37,8 @@ pub unsafe fn dc_token_lookup( mut namespc: dc_tokennamespc_t, mut foreign_id: uint32_t, ) -> *mut libc::c_char { - let mut token: *mut libc::c_char = 0 as *mut libc::c_char; - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; + let mut token: *mut libc::c_char; + let mut stmt: *mut sqlite3_stmt; stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), diff --git a/src/dc_tools.rs b/src/dc_tools.rs index 60729efd7..39ff4f342 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -1,6 +1,6 @@ use std::fs; -use libc; +use mmime::mailimf_types::*; use rand::{thread_rng, Rng}; use crate::dc_array::*; @@ -18,10 +18,11 @@ no references to dc_context_t and other "larger" classes here. */ pub unsafe fn dc_exactly_one_bit_set(mut v: libc::c_int) -> libc::c_int { return (0 != v && 0 == v & v - 1i32) as libc::c_int; } + /* string tools */ /* dc_strdup() returns empty string if NULL is given, never returns NULL (exits on errors) */ pub unsafe fn dc_strdup(mut s: *const libc::c_char) -> *mut libc::c_char { - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; + let mut ret: *mut libc::c_char; if !s.is_null() { ret = strdup(s); if ret.is_null() { @@ -33,8 +34,10 @@ pub unsafe fn dc_strdup(mut s: *const libc::c_char) -> *mut libc::c_char { exit(17i32); } } - return ret; + + ret } + /* strdup(NULL) is undefined, safe_strdup_keep_null(NULL) returns NULL in this case */ pub unsafe fn dc_strdup_keep_null(mut s: *const libc::c_char) -> *mut libc::c_char { return if !s.is_null() { @@ -43,9 +46,11 @@ pub unsafe fn dc_strdup_keep_null(mut s: *const libc::c_char) -> *mut libc::c_ch 0 as *mut libc::c_char }; } + pub unsafe fn dc_atoi_null_is_0(mut s: *const libc::c_char) -> libc::c_int { return if !s.is_null() { atoi(s) } else { 0i32 }; } + pub unsafe fn dc_atof(mut str: *const libc::c_char) -> libc::c_double { // hack around atof() that may accept only `,` as decimal point on mac let mut test: *mut libc::c_char = @@ -60,8 +65,10 @@ pub unsafe fn dc_atof(mut str: *const libc::c_char) -> libc::c_double { let mut f: libc::c_double = atof(str_locale); free(test as *mut libc::c_void); free(str_locale as *mut libc::c_void); - return f; + + f } + pub unsafe fn dc_str_replace( mut haystack: *mut *mut libc::c_char, mut needle: *const libc::c_char, @@ -69,8 +76,8 @@ pub unsafe fn dc_str_replace( ) -> libc::c_int { let mut replacements: libc::c_int = 0i32; let mut start_search_pos: libc::c_int = 0i32; - let mut needle_len: libc::c_int = 0i32; - let mut replacement_len: libc::c_int = 0i32; + let mut needle_len: libc::c_int; + let mut replacement_len: libc::c_int; if haystack.is_null() || (*haystack).is_null() || needle.is_null() @@ -108,7 +115,8 @@ pub unsafe fn dc_str_replace( *haystack = new_string; replacements += 1 } - return replacements; + + replacements } pub unsafe fn dc_ftoa(mut f: libc::c_double) -> *mut libc::c_char { @@ -123,12 +131,13 @@ pub unsafe fn dc_ftoa(mut f: libc::c_double) -> *mut libc::c_char { b".\x00" as *const u8 as *const libc::c_char, ); free(test as *mut libc::c_void); - return str; + + str } pub unsafe fn dc_ltrim(mut buf: *mut libc::c_char) { - let mut len: size_t = 0i32 as size_t; - let mut cur: *const libc::c_uchar = 0 as *const libc::c_uchar; + let mut len: size_t; + let mut cur: *const libc::c_uchar; if !buf.is_null() && 0 != *buf as libc::c_int { len = strlen(buf); cur = buf as *const libc::c_uchar; @@ -147,8 +156,8 @@ pub unsafe fn dc_ltrim(mut buf: *mut libc::c_char) { } pub unsafe fn dc_rtrim(mut buf: *mut libc::c_char) { - let mut len: size_t = 0i32 as size_t; - let mut cur: *mut libc::c_uchar = 0 as *mut libc::c_uchar; + let mut len: size_t; + let mut cur: *mut libc::c_uchar; if !buf.is_null() && 0 != *buf as libc::c_int { len = strlen(buf); cur = (buf as *mut libc::c_uchar) @@ -178,18 +187,21 @@ pub unsafe fn dc_strlower(mut in_0: *const libc::c_char) -> *mut libc::c_char { let mut out: *mut libc::c_char = dc_strdup(in_0); let mut p: *mut libc::c_char = out; while 0 != *p { - *p = tolower(*p as libc::c_int) as libc::c_char; + *p = libc::tolower(*p as libc::c_int) as libc::c_char; p = p.offset(1isize) } - return out; + + out } + pub unsafe fn dc_strlower_in_place(mut in_0: *mut libc::c_char) { let mut p: *mut libc::c_char = in_0; while 0 != *p { - *p = tolower(*p as libc::c_int) as libc::c_char; + *p = libc::tolower(*p as libc::c_int) as libc::c_char; p = p.offset(1isize) } } + pub unsafe fn dc_str_contains( mut haystack: *const libc::c_char, mut needle: *const libc::c_char, @@ -209,8 +221,10 @@ pub unsafe fn dc_str_contains( }; free(haystack_lower as *mut libc::c_void); free(needle_lower as *mut libc::c_void); - return ret; + + ret } + /* the result must be free()'d */ pub unsafe fn dc_null_terminate( mut in_0: *const libc::c_char, @@ -224,11 +238,13 @@ pub unsafe fn dc_null_terminate( strncpy(out, in_0, bytes as usize); } *out.offset(bytes as isize) = 0i32 as libc::c_char; - return out; + + out } + pub unsafe fn dc_binary_to_uc_hex(mut buf: *const uint8_t, mut bytes: size_t) -> *mut libc::c_char { let mut hex: *mut libc::c_char = 0 as *mut libc::c_char; - let mut i = 0; + let mut i; if !(buf.is_null() || bytes <= 0) { hex = calloc( ::std::mem::size_of::(), @@ -247,8 +263,10 @@ pub unsafe fn dc_binary_to_uc_hex(mut buf: *const uint8_t, mut bytes: size_t) -> } } } - return hex; + + hex } + /* remove all \r characters from string */ pub unsafe extern "C" fn dc_remove_cr_chars(mut buf: *mut libc::c_char) { /* search for first `\r` */ @@ -270,9 +288,11 @@ pub unsafe extern "C" fn dc_remove_cr_chars(mut buf: *mut libc::c_char) { } *p2 = 0i32 as libc::c_char; } + pub unsafe fn dc_unify_lineends(mut buf: *mut libc::c_char) { dc_remove_cr_chars(buf); } + /* replace bad UTF-8 characters by sequences of `_` (to avoid problems in filenames, we do not use eg. `?`) the function is useful if strings are unexpectingly encoded eg. as ISO-8859-1 */ pub unsafe fn dc_replace_bad_utf8_chars(mut buf: *mut libc::c_char) { let mut current_block: u64; @@ -282,11 +302,11 @@ pub unsafe fn dc_replace_bad_utf8_chars(mut buf: *mut libc::c_char) { /* force unsigned - otherwise the `> ' '` comparison will fail */ let mut p1: *mut libc::c_uchar = buf as *mut libc::c_uchar; let mut p1len: libc::c_int = strlen(buf) as libc::c_int; - let mut c: libc::c_int = 0i32; - let mut i: libc::c_int = 0i32; - let mut ix: libc::c_int = 0i32; - let mut n: libc::c_int = 0i32; - let mut j: libc::c_int = 0i32; + let mut c: libc::c_int; + let mut i: libc::c_int; + let mut ix: libc::c_int; + let mut n: libc::c_int; + let mut j: libc::c_int; i = 0i32; ix = p1len; 's_36: loop { @@ -341,6 +361,7 @@ pub unsafe fn dc_replace_bad_utf8_chars(mut buf: *mut libc::c_char) { } }; } + pub unsafe fn dc_utf8_strlen(mut s: *const libc::c_char) -> size_t { if s.is_null() { return 0i32 as size_t; @@ -353,8 +374,10 @@ pub unsafe fn dc_utf8_strlen(mut s: *const libc::c_char) -> size_t { } i = i.wrapping_add(1) } - return j; + + j } + pub unsafe fn dc_truncate_str(mut buf: *mut libc::c_char, mut approx_chars: libc::c_int) { if approx_chars > 0 && strlen(buf) @@ -375,6 +398,7 @@ pub unsafe fn dc_truncate_str(mut buf: *mut libc::c_char, mut approx_chars: libc strcat(p, b"[...]\x00" as *const u8 as *const libc::c_char); }; } + pub unsafe fn dc_truncate_n_unwrap_str( mut buf: *mut libc::c_char, mut approx_characters: libc::c_int, @@ -418,6 +442,7 @@ pub unsafe fn dc_truncate_n_unwrap_str( dc_remove_cr_chars(buf); }; } + unsafe fn dc_utf8_strnlen(mut s: *const libc::c_char, mut n: size_t) -> size_t { if s.is_null() { return 0i32 as size_t; @@ -430,8 +455,10 @@ unsafe fn dc_utf8_strnlen(mut s: *const libc::c_char, mut n: size_t) -> size_t { } i = i.wrapping_add(1) } - return j; + + j } + /* split string into lines*/ pub unsafe fn dc_split_into_lines(mut buf_terminated: *const libc::c_char) -> *mut carray { let mut lines: *mut carray = carray_new(1024i32 as libc::c_uint); @@ -459,11 +486,13 @@ pub unsafe fn dc_split_into_lines(mut buf_terminated: *const libc::c_char) -> *m strndup(line_start, line_chars) as *mut libc::c_void, &mut l_indx, ); - return lines; + + lines } + pub unsafe fn dc_free_splitted_lines(mut lines: *mut carray) { if !lines.is_null() { - let mut i: libc::c_int = 0; + let mut i: libc::c_int; let mut cnt: libc::c_int = carray_count(lines) as libc::c_int; i = 0i32; while i < cnt { @@ -473,6 +502,7 @@ pub unsafe fn dc_free_splitted_lines(mut lines: *mut carray) { carray_free(lines); }; } + /* insert a break every n characters, the return must be free()'d */ pub unsafe fn dc_insert_breaks( mut in_0: *const libc::c_char, @@ -506,8 +536,10 @@ pub unsafe fn dc_insert_breaks( } } *o = 0i32 as libc::c_char; - return out; + + out } + pub unsafe fn dc_str_from_clist( mut list: *const clist, mut delimiter: *const libc::c_char, @@ -536,12 +568,14 @@ pub unsafe fn dc_str_from_clist( cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } - return str.buf; + + str.buf } + pub unsafe fn dc_str_to_clist( mut str: *const libc::c_char, mut delimiter: *const libc::c_char, @@ -570,8 +604,10 @@ pub unsafe fn dc_str_to_clist( } } } - return list; + + list } + pub unsafe fn dc_str_to_color(mut str: *const libc::c_char) -> libc::c_int { let mut str_lower: *mut libc::c_char = dc_strlower(str); /* the colors must fulfill some criterions as: @@ -611,8 +647,10 @@ pub unsafe fn dc_str_to_color(mut str: *const libc::c_char) -> libc::c_int { .wrapping_div(::std::mem::size_of::() as libc::c_ulong), ) as libc::c_int; free(str_lower as *mut libc::c_void); - return colors[color_index as usize] as libc::c_int; + + colors[color_index as usize] as libc::c_int } + /* clist tools */ /* calls free() for each item content */ pub unsafe fn clist_free_content(mut haystack: *const clist) { @@ -623,10 +661,11 @@ pub unsafe fn clist_free_content(mut haystack: *const clist) { iter = if !iter.is_null() { (*iter).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } + pub unsafe fn clist_search_string_nocase( mut haystack: *const clist, mut needle: *const libc::c_char, @@ -639,11 +678,13 @@ pub unsafe fn clist_search_string_nocase( iter = if !iter.is_null() { (*iter).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } - return 0i32; + + 0 } + /* date/time tools */ /* the result is UTC or DC_INVALID_TIMESTAMP */ pub unsafe fn dc_timestamp_from_date(mut date_time: *mut mailimf_date_time) -> time_t { @@ -660,9 +701,9 @@ pub unsafe fn dc_timestamp_from_date(mut date_time: *mut mailimf_date_time) -> t tm_gmtoff: 0, tm_zone: 0 as *mut libc::c_char, }; - let mut timeval: time_t = 0i32 as time_t; - let mut zone_min: libc::c_int = 0i32; - let mut zone_hour: libc::c_int = 0i32; + let mut timeval: time_t; + let mut zone_min: libc::c_int; + let mut zone_hour: libc::c_int; memset( &mut tmval as *mut tm as *mut libc::c_void, 0, @@ -687,26 +728,16 @@ pub unsafe fn dc_timestamp_from_date(mut date_time: *mut mailimf_date_time) -> t zone_min = -(-(*date_time).dt_zone % 100i32) } timeval -= (zone_hour * 3600i32 + zone_min * 60i32) as libc::c_long; - return timeval; + + timeval } + pub unsafe fn mkgmtime(mut tmp: *mut tm) -> time_t { - let mut dir: libc::c_int = 0i32; - let mut bits: libc::c_int = 0i32; - let mut saved_seconds: libc::c_int = 0i32; - let mut t: time_t = 0i32 as time_t; - let mut yourtm: tm = tm { - tm_sec: 0, - tm_min: 0, - tm_hour: 0, - tm_mday: 0, - tm_mon: 0, - tm_year: 0, - tm_wday: 0, - tm_yday: 0, - tm_isdst: 0, - tm_gmtoff: 0, - tm_zone: 0 as *mut libc::c_char, - }; + let mut dir: libc::c_int; + let mut bits: libc::c_int; + let mut saved_seconds: libc::c_int; + let mut t: time_t; + let mut yourtm: tm; let mut mytm: tm = tm { tm_sec: 0, tm_min: 0, @@ -757,13 +788,15 @@ pub unsafe fn mkgmtime(mut tmp: *mut tm) -> time_t { } } t += saved_seconds as libc::c_long; - return t; + + t } + /* ****************************************************************************** * date/time tools ******************************************************************************/ unsafe fn tmcomp(mut atmp: *mut tm, mut btmp: *mut tm) -> libc::c_int { - let mut result: libc::c_int = 0i32; + let mut result: libc::c_int; result = (*atmp).tm_year - (*btmp).tm_year; if result == 0i32 && { @@ -785,8 +818,10 @@ unsafe fn tmcomp(mut atmp: *mut tm, mut btmp: *mut tm) -> libc::c_int { { result = (*atmp).tm_sec - (*btmp).tm_sec } - return result; + + result } + /* the return value must be free()'d */ pub unsafe fn dc_timestamp_to_str(mut wanted: time_t) -> *mut libc::c_char { let mut wanted_struct: tm = tm { @@ -817,62 +852,7 @@ pub unsafe fn dc_timestamp_to_str(mut wanted: time_t) -> *mut libc::c_char { wanted_struct.tm_sec as libc::c_int, ); } -pub unsafe fn dc_timestamp_to_mailimap_date_time(mut timeval: time_t) -> *mut mailimap_date_time { - let mut gmt: tm = tm { - tm_sec: 0, - tm_min: 0, - tm_hour: 0, - tm_mday: 0, - tm_mon: 0, - tm_year: 0, - tm_wday: 0, - tm_yday: 0, - tm_isdst: 0, - tm_gmtoff: 0, - tm_zone: 0 as *mut libc::c_char, - }; - let mut lt: tm = tm { - tm_sec: 0, - tm_min: 0, - tm_hour: 0, - tm_mday: 0, - tm_mon: 0, - tm_year: 0, - tm_wday: 0, - tm_yday: 0, - tm_isdst: 0, - tm_gmtoff: 0, - tm_zone: 0 as *mut libc::c_char, - }; - let mut off: libc::c_int = 0i32; - let mut date_time: *mut mailimap_date_time = 0 as *mut mailimap_date_time; - let mut sign: libc::c_int = 0i32; - let mut hour: libc::c_int = 0i32; - let mut min: libc::c_int = 0i32; - gmtime_r(&mut timeval, &mut gmt); - localtime_r(&mut timeval, &mut lt); - off = ((mkgmtime(&mut lt) - mkgmtime(&mut gmt)) / 60i32 as libc::c_long) as libc::c_int; - if off < 0i32 { - sign = -1i32 - } else { - sign = 1i32 - } - off = off * sign; - min = off % 60i32; - hour = off / 60i32; - off = hour * 100i32 + min; - off = off * sign; - date_time = mailimap_date_time_new( - lt.tm_mday, - lt.tm_mon + 1i32, - lt.tm_year + 1900i32, - lt.tm_hour, - lt.tm_min, - lt.tm_sec, - off, - ); - return date_time; -} + pub unsafe fn dc_gm2local_offset() -> libc::c_long { /* returns the offset that must be _added_ to an UTC/GMT-time to create the localtime. the function may return nagative values. */ @@ -891,8 +871,10 @@ pub unsafe fn dc_gm2local_offset() -> libc::c_long { tm_zone: 0 as *mut libc::c_char, }; localtime_r(&mut gmtime, &mut timeinfo); - return timeinfo.tm_gmtoff; + + timeinfo.tm_gmtoff } + /* timesmearing */ pub unsafe fn dc_smeared_time(mut context: &dc_context_t) -> time_t { /* function returns a corrected time(NULL) */ @@ -901,6 +883,7 @@ pub unsafe fn dc_smeared_time(mut context: &dc_context_t) -> time_t { if ts >= now { now = ts + 1; } + now } @@ -915,7 +898,7 @@ pub unsafe fn dc_create_smeared_timestamp(mut context: &dc_context_t) -> time_t ret = now + 5 } } - ts = ret; + ret } @@ -930,7 +913,7 @@ pub unsafe fn dc_create_smeared_timestamps( let mut ts = *context.last_smeared_timestamp.clone().write().unwrap(); start = if ts + 1 > start { ts + 1 } else { start }; - ts = start + ((count - 1) as time_t); + start } @@ -949,8 +932,10 @@ pub unsafe fn dc_create_id() -> *mut libc::c_char { let mut rng = thread_rng(); let mut buf: [uint32_t; 3] = [rng.gen(), rng.gen(), rng.gen()]; - return encode_66bits_as_base64(buf[0usize], buf[1usize], buf[2usize]); + + encode_66bits_as_base64(buf[0usize], buf[1usize], buf[2usize]) } + /* ****************************************************************************** * generate Message-IDs ******************************************************************************/ @@ -987,8 +972,10 @@ unsafe fn encode_66bits_as_base64( *ret.offset(10isize) = chars[(v2 << 2i32 & 0x3ci32 as libc::c_uint | fill & 0x3i32 as libc::c_uint) as usize]; *ret.offset(11isize) = 0i32 as libc::c_char; - return ret; + + ret } + pub unsafe fn dc_create_incoming_rfc724_mid( mut message_timestamp: time_t, mut contact_id_from: uint32_t, @@ -1001,7 +988,6 @@ pub unsafe fn dc_create_incoming_rfc724_mid( let mut i: size_t = 0i32 as size_t; let mut icnt: size_t = dc_array_get_cnt(contact_ids_to); let mut largest_id_to: uint32_t = 0i32 as uint32_t; - i = 0i32 as size_t; while i < icnt { let mut cur_id: uint32_t = dc_array_get_id(contact_ids_to, i); if cur_id > largest_id_to { @@ -1009,13 +995,15 @@ pub unsafe fn dc_create_incoming_rfc724_mid( } i = i.wrapping_add(1) } - return dc_mprintf( + + dc_mprintf( b"%lu-%lu-%lu@stub\x00" as *const u8 as *const libc::c_char, message_timestamp as libc::c_ulong, contact_id_from as libc::c_ulong, largest_id_to as libc::c_ulong, - ); + ) } + pub unsafe fn dc_create_outgoing_rfc724_mid( mut grpid: *const libc::c_char, mut from_addr: *const libc::c_char, @@ -1026,7 +1014,7 @@ pub unsafe fn dc_create_outgoing_rfc724_mid( - do not add a counter or any private data as as this may give unneeded information to the receiver */ let mut rand1: *mut libc::c_char = 0 as *mut libc::c_char; let mut rand2: *mut libc::c_char = dc_create_id(); - let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; + let mut ret: *mut libc::c_char; let mut at_hostname: *const libc::c_char = strchr(from_addr, '@' as i32); if at_hostname.is_null() { at_hostname = b"@nohost\x00" as *const u8 as *const libc::c_char @@ -1049,14 +1037,16 @@ pub unsafe fn dc_create_outgoing_rfc724_mid( } free(rand1 as *mut libc::c_void); free(rand2 as *mut libc::c_void); - return ret; + + ret } + pub unsafe fn dc_extract_grpid_from_rfc724_mid(mut mid: *const libc::c_char) -> *mut libc::c_char { /* extract our group ID from Message-IDs as `Gr.12345678901.morerandom@domain.de`; "12345678901" is the wanted ID in this example. */ let mut success: libc::c_int = 0i32; let mut grpid: *mut libc::c_char = 0 as *mut libc::c_char; - let mut p1: *mut libc::c_char = 0 as *mut libc::c_char; - let mut grpid_len: libc::c_int = 0i32; + let mut p1: *mut libc::c_char; + let mut grpid_len: libc::c_int; if !(mid.is_null() || strlen(mid) < 8 || *mid.offset(0isize) as libc::c_int != 'G' as i32 @@ -1084,6 +1074,7 @@ pub unsafe fn dc_extract_grpid_from_rfc724_mid(mut mid: *const libc::c_char) -> 0 as *mut libc::c_char }; } + pub unsafe fn dc_extract_grpid_from_rfc724_mid_list(mut list: *const clist) -> *mut libc::c_char { if !list.is_null() { let mut cur: *mut clistiter = (*list).first; @@ -1100,12 +1091,14 @@ pub unsafe fn dc_extract_grpid_from_rfc724_mid_list(mut list: *const clist) -> * cur = if !cur.is_null() { (*cur).next } else { - 0 as *mut clistcell_s + 0 as *mut clistcell } } } - return 0 as *mut libc::c_char; + + 0 as *mut libc::c_char } + /* file tools */ pub unsafe fn dc_ensure_no_slash(mut pathNfilename: *mut libc::c_char) { let mut path_len: libc::c_int = strlen(pathNfilename) as libc::c_int; @@ -1117,6 +1110,7 @@ pub unsafe fn dc_ensure_no_slash(mut pathNfilename: *mut libc::c_char) { } }; } + pub unsafe fn dc_validate_filename(mut filename: *mut libc::c_char) { /* function modifies the given buffer and replaces all characters not valid in filenames by a "-" */ let mut p1: *mut libc::c_char = filename; @@ -1130,6 +1124,7 @@ pub unsafe fn dc_validate_filename(mut filename: *mut libc::c_char) { p1 = p1.offset(1isize) } } + pub unsafe fn dc_get_filename(mut pathNfilename: *const libc::c_char) -> *mut libc::c_char { let mut p: *const libc::c_char = strrchr(pathNfilename, '/' as i32); if p.is_null() { @@ -1142,6 +1137,7 @@ pub unsafe fn dc_get_filename(mut pathNfilename: *const libc::c_char) -> *mut li return dc_strdup(pathNfilename); }; } + // the case of the suffix is preserved pub unsafe fn dc_split_filename( mut pathNfilename: *const libc::c_char, @@ -1154,7 +1150,7 @@ pub unsafe fn dc_split_filename( - if there is no suffix, the returned suffix string is empty, eg. "/path/foobar" is split into "foobar" and "" - the case of the returned suffix is preserved; this is to allow reconstruction of (similar) names */ let mut basename: *mut libc::c_char = dc_get_filename(pathNfilename); - let mut suffix: *mut libc::c_char = 0 as *mut libc::c_char; + let mut suffix: *mut libc::c_char; let mut p1: *mut libc::c_char = strrchr(basename, '.' as i32); if !p1.is_null() { suffix = dc_strdup(p1); @@ -1173,6 +1169,7 @@ pub unsafe fn dc_split_filename( free(suffix as *mut libc::c_void); }; } + // the returned suffix is lower-case pub unsafe fn dc_get_filesuffix_lc(mut pathNfilename: *const libc::c_char) -> *mut libc::c_char { if !pathNfilename.is_null() { @@ -1182,8 +1179,10 @@ pub unsafe fn dc_get_filesuffix_lc(mut pathNfilename: *const libc::c_char) -> *m return dc_strlower(p); } } - return 0 as *mut libc::c_char; + + 0 as *mut libc::c_char } + pub unsafe fn dc_get_filemeta( mut buf_start: *const libc::c_void, mut buf_bytes: size_t, @@ -1264,8 +1263,10 @@ pub unsafe fn dc_get_filemeta( + ((*buf.offset(23isize) as libc::c_int) << 0i32)) as uint32_t; return 1i32; } - return 0i32; + + 0 } + pub unsafe fn dc_get_abs_path( mut context: &dc_context_t, mut pathNfilename: *const libc::c_char, @@ -1281,13 +1282,13 @@ pub unsafe fn dc_get_abs_path( 8, ) == 0i32 { - if context.blobdir.is_null() { + if !context.has_blobdir() { current_block = 3805228753452640762; } else { dc_str_replace( &mut pathNfilename_abs, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, - context.blobdir, + context.get_blobdir(), ); current_block = 6937071982253665452; } @@ -1303,8 +1304,10 @@ pub unsafe fn dc_get_abs_path( free(pathNfilename_abs as *mut libc::c_void); pathNfilename_abs = 0 as *mut libc::c_char } - return pathNfilename_abs; + + pathNfilename_abs } + pub unsafe fn dc_file_exist( mut context: &dc_context_t, mut pathNfilename: *const libc::c_char, @@ -1324,7 +1327,6 @@ pub unsafe fn dc_file_exist( }; free(pathNfilename_abs as *mut libc::c_void); - exist as libc::c_int } @@ -1437,7 +1439,7 @@ pub unsafe fn dc_create_folder( .unwrap(), ); if !p.exists() { - if mkdir(pathNfilename_abs, 0o755i32 as mode_t) != 0i32 { + if mkdir(pathNfilename_abs, 0o755i32 as libc::mode_t) != 0i32 { dc_log_warning( context, 0i32, @@ -1545,8 +1547,8 @@ pub unsafe fn dc_get_fine_pathNfilename( mut desired_filenameNsuffix__: *const libc::c_char, ) -> *mut libc::c_char { let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; - let mut pathNfolder_wo_slash: *mut libc::c_char = 0 as *mut libc::c_char; - let mut filenameNsuffix: *mut libc::c_char = 0 as *mut libc::c_char; + let mut pathNfolder_wo_slash: *mut libc::c_char; + let mut filenameNsuffix: *mut libc::c_char; let mut basename: *mut libc::c_char = 0 as *mut libc::c_char; let mut dotNSuffix: *mut libc::c_char = 0 as *mut libc::c_char; let mut now: time_t = time(0 as *mut time_t); @@ -1556,7 +1558,6 @@ pub unsafe fn dc_get_fine_pathNfilename( filenameNsuffix = dc_strdup(desired_filenameNsuffix__); dc_validate_filename(filenameNsuffix); dc_split_filename(filenameNsuffix, &mut basename, &mut dotNSuffix); - i = 0i32; while i < 1000i32 { /*no deadlocks, please*/ if 0 != i { @@ -1593,34 +1594,38 @@ pub unsafe fn dc_get_fine_pathNfilename( free(basename as *mut libc::c_void); free(dotNSuffix as *mut libc::c_void); free(pathNfolder_wo_slash as *mut libc::c_void); - return ret; + + ret } +// TODO should return bool /rtn pub unsafe fn dc_is_blobdir_path( mut context: &dc_context_t, mut path: *const libc::c_char, ) -> libc::c_int { - if strncmp(path, context.blobdir, strlen(context.blobdir)) == 0i32 + if strncmp(path, context.get_blobdir(), strlen(context.get_blobdir())) == 0i32 || strncmp(path, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, 8) == 0i32 { return 1i32; } - return 0i32; + + 0 } pub unsafe fn dc_make_rel_path(mut context: &dc_context_t, mut path: *mut *mut libc::c_char) { if path.is_null() || (*path).is_null() { return; } - if strncmp(*path, context.blobdir, strlen(context.blobdir)) == 0i32 { + if strncmp(*path, context.get_blobdir(), strlen(context.get_blobdir())) == 0i32 { dc_str_replace( path, - context.blobdir, + context.get_blobdir(), b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, ); }; } +// TODO should return bool /rtn pub unsafe fn dc_make_rel_and_copy( mut context: &dc_context_t, mut path: *mut *mut libc::c_char, @@ -1655,7 +1660,8 @@ pub unsafe fn dc_make_rel_and_copy( } free(blobdir_path as *mut libc::c_void); free(filename as *mut libc::c_void); - return success; + + success } #[cfg(test)] @@ -1668,8 +1674,7 @@ mod tests { unsafe { let mut html: *const libc::c_char = b"\r\r\nline1
\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char; - let mut out: *mut libc::c_char = 0 as *mut libc::c_char; - out = strndup(html, strlen(html) as libc::c_ulong); + let mut out: *mut libc::c_char = strndup(html, strlen(html) as libc::c_ulong); dc_ltrim(out); @@ -1685,8 +1690,7 @@ mod tests { unsafe { let mut html: *const libc::c_char = b"\r\r\nline1
\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char; - let mut out: *mut libc::c_char = 0 as *mut libc::c_char; - out = strndup(html, strlen(html) as libc::c_ulong); + let mut out: *mut libc::c_char = strndup(html, strlen(html) as libc::c_ulong); dc_rtrim(out); @@ -1702,8 +1706,7 @@ mod tests { unsafe { let mut html: *const libc::c_char = b"\r\r\nline1
\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char; - let mut out: *mut libc::c_char = 0 as *mut libc::c_char; - out = strndup(html, strlen(html) as libc::c_ulong); + let mut out: *mut libc::c_char = strndup(html, strlen(html) as libc::c_ulong); dc_trim(out); diff --git a/src/lib.rs b/src/lib.rs index 6b2fe8638..a4545f434 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,6 @@ non_camel_case_types, non_snake_case, non_upper_case_globals, - unused_assignments, unused_mut, unused_attributes, non_upper_case_globals, @@ -21,6 +20,8 @@ extern crate failure; #[macro_use] extern crate num_derive; +// #[macro_use] +// extern crate rental; mod pgp; diff --git a/src/types.rs b/src/types.rs index 2b5b139f5..e0575ee88 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,19 +1,74 @@ -use c2rust_bitfields::BitfieldStruct; -use libc; - use crate::constants::Event; use crate::dc_context::dc_context_t; -use crate::x::*; pub use libc::{dirent, tm, DIR, FILE}; pub use libsqlite3_sys::*; +pub use mmime::carray::*; +pub use mmime::clist::*; -extern "C" { - pub type __sFILEX; - - pub type _telldir; - pub type mailstream_cancel; +pub type __builtin_va_list = [__va_list_tag; 1]; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct __va_list_tag { + pub gp_offset: libc::c_uint, + pub fp_offset: libc::c_uint, + pub overflow_arg_area: *mut libc::c_void, + pub reg_save_area: *mut libc::c_void, } +pub type va_list = __builtin_va_list; +pub type __int64_t = libc::c_longlong; +pub type __darwin_ct_rune_t = libc::c_int; +pub type __darwin_wchar_t = libc::c_int; +pub type __darwin_rune_t = __darwin_wchar_t; +pub type uint64_t = libc::c_ulonglong; +pub type uid_t = libc::uid_t; +pub type gid_t = libc::gid_t; +pub type dev_t = libc::dev_t; +pub type blkcnt_t = libc::blkcnt_t; +pub type blksize_t = libc::blksize_t; +pub type nlink_t = __uint16_t; + +/** + * Callback function that should be given to dc_context_new(). + * + * @memberof dc_context_t + * @param context The context object as returned by dc_context_new(). + * @param event one of the @ref DC_EVENT constants + * @param data1 depends on the event parameter + * @param data2 depends on the event parameter + * @return return 0 unless stated otherwise in the event parameter documentation + */ +pub type dc_callback_t = + unsafe extern "C" fn(_: &dc_context_t, _: Event, _: uintptr_t, _: uintptr_t) -> uintptr_t; + +pub type dc_move_state_t = u32; + +pub type dc_receive_imf_t = unsafe fn( + _: &dc_context_t, + _: *const libc::c_char, + _: size_t, + _: *const libc::c_char, + _: uint32_t, + _: 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 = unsafe fn( + _: &dc_context_t, + _: *const libc::c_char, + _: *const libc::c_char, + _: u32, +) -> libc::c_int; +pub type dc_set_config_t = + unsafe fn(_: &dc_context_t, _: *const libc::c_char, _: *const libc::c_char) -> (); +pub type dc_get_config_t = unsafe fn( + _: &dc_context_t, + _: *const libc::c_char, + _: *const libc::c_char, +) -> *mut libc::c_char; pub type sqlite_int64 = libc::int64_t; pub type sqlite3_int64 = sqlite_int64; @@ -36,1481 +91,3 @@ pub type uint8_t = libc::c_uchar; pub type uint16_t = libc::c_ushort; pub type __uint32_t = libc::c_uint; - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct carray_s { - pub array: *mut *mut libc::c_void, - pub len: libc::c_uint, - pub max: libc::c_uint, -} -pub type carray = carray_s; - -pub type mailimap_msg_att_handler = - unsafe extern "C" fn(_: *mut mailimap_msg_att, _: *mut libc::c_void) -> (); -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap { - pub imap_response: *mut libc::c_char, - pub imap_stream: *mut mailstream, - pub imap_progr_rate: size_t, - pub imap_progr_fun: Option ()>, - pub imap_stream_buffer: *mut MMAPString, - pub imap_response_buffer: *mut MMAPString, - pub imap_state: libc::c_int, - pub imap_tag: libc::c_int, - pub imap_connection_info: *mut mailimap_connection_info, - pub imap_selection_info: *mut mailimap_selection_info, - pub imap_response_info: *mut mailimap_response_info, - pub imap_sasl: unnamed_2, - pub imap_idle_timestamp: time_t, - pub imap_idle_maxdelay: time_t, - pub imap_body_progress_fun: - Option ()>, - pub imap_items_progress_fun: - Option ()>, - pub imap_progress_context: *mut libc::c_void, - pub imap_msg_att_handler: - Option ()>, - pub imap_msg_att_handler_context: *mut libc::c_void, - pub imap_msg_body_handler: Option< - unsafe extern "C" fn( - _: libc::c_int, - _: *mut mailimap_msg_att_body_section, - _: *const libc::c_char, - _: size_t, - _: *mut libc::c_void, - ) -> bool, - >, - pub imap_msg_body_handler_context: *mut libc::c_void, - pub imap_timeout: time_t, - pub imap_logger: Option< - unsafe extern "C" fn( - _: *mut mailimap, - _: libc::c_int, - _: *const libc::c_char, - _: size_t, - _: *mut libc::c_void, - ) -> (), - >, - pub imap_logger_context: *mut libc::c_void, - pub is_163_workaround_enabled: libc::c_int, - pub is_rambler_workaround_enabled: libc::c_int, - pub is_qip_workaround_enabled: libc::c_int, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _mailstream { - pub buffer_max_size: size_t, - pub write_buffer: *mut libc::c_char, - pub write_buffer_len: size_t, - pub read_buffer: *mut libc::c_char, - pub read_buffer_len: size_t, - pub low: *mut mailstream_low, - pub idle: *mut mailstream_cancel, - pub idling: libc::c_int, - pub logger: Option< - unsafe extern "C" fn( - _: *mut mailstream, - _: libc::c_int, - _: *const libc::c_char, - _: size_t, - _: *mut libc::c_void, - ) -> (), - >, - pub logger_context: *mut libc::c_void, -} -pub type mailstream = _mailstream; -pub type mailstream_low = _mailstream_low; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _mailstream_low { - pub data: *mut libc::c_void, - pub driver: *mut mailstream_low_driver, - pub privacy: libc::c_int, - pub identifier: *mut libc::c_char, - pub timeout: libc::c_ulong, - pub logger: Option< - unsafe extern "C" fn( - _: *mut mailstream_low, - _: libc::c_int, - _: *const libc::c_char, - _: size_t, - _: *mut libc::c_void, - ) -> (), - >, - pub logger_context: *mut libc::c_void, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailstream_low_driver { - pub mailstream_read: Option< - unsafe extern "C" fn(_: *mut mailstream_low, _: *mut libc::c_void, _: size_t) -> ssize_t, - >, - pub mailstream_write: Option< - unsafe extern "C" fn(_: *mut mailstream_low, _: *const libc::c_void, _: size_t) -> ssize_t, - >, - pub mailstream_close: Option libc::c_int>, - pub mailstream_get_fd: Option libc::c_int>, - pub mailstream_free: Option ()>, - pub mailstream_cancel: Option ()>, - pub mailstream_get_cancel: - Option *mut mailstream_cancel>, - pub mailstream_get_certificate_chain: - Option *mut carray>, - pub mailstream_setup_idle: Option libc::c_int>, - pub mailstream_unsetup_idle: - Option libc::c_int>, - pub mailstream_interrupt_idle: - Option libc::c_int>, -} -pub type progress_function = unsafe extern "C" fn(_: size_t, _: size_t) -> (); -pub type mailprogress_function = - unsafe extern "C" fn(_: size_t, _: size_t, _: *mut libc::c_void) -> (); -pub type unnamed = libc::c_uint; -pub const MAILSTREAM_IDLE_CANCELLED: unnamed = 4; -pub const MAILSTREAM_IDLE_TIMEOUT: unnamed = 3; -pub const MAILSTREAM_IDLE_HASDATA: unnamed = 2; -pub const MAILSTREAM_IDLE_INTERRUPTED: unnamed = 1; -pub const MAILSTREAM_IDLE_ERROR: unnamed = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _MMAPString { - pub str_0: *mut libc::c_char, - pub len: size_t, - pub allocated_len: size_t, - pub fd: libc::c_int, - pub mmapped_size: size_t, -} -pub type MMAPString = _MMAPString; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct clistcell_s { - pub data: *mut libc::c_void, - pub previous: *mut clistcell_s, - pub next: *mut clistcell_s, -} -pub type clistcell = clistcell_s; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct clist_s { - pub first: *mut clistcell, - pub last: *mut clistcell, - pub count: libc::c_int, -} -pub type clist = clist_s; -pub type clistiter = clistcell; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailsmtp { - pub stream: *mut mailstream, - pub progr_rate: size_t, - pub progr_fun: Option ()>, - pub response: *mut libc::c_char, - pub line_buffer: *mut MMAPString, - pub response_buffer: *mut MMAPString, - pub esmtp: libc::c_int, - pub auth: libc::c_int, - pub smtp_sasl: unnamed_0, - pub smtp_max_msg_size: size_t, - pub smtp_progress_fun: - Option ()>, - pub smtp_progress_context: *mut libc::c_void, - pub response_code: libc::c_int, - pub smtp_timeout: time_t, - pub smtp_logger: Option< - unsafe extern "C" fn( - _: *mut mailsmtp, - _: libc::c_int, - _: *const libc::c_char, - _: size_t, - _: *mut libc::c_void, - ) -> (), - >, - pub smtp_logger_context: *mut libc::c_void, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct unnamed_0 { - pub sasl_conn: *mut libc::c_void, - pub sasl_server_fqdn: *const libc::c_char, - pub sasl_login: *const libc::c_char, - pub sasl_auth_name: *const libc::c_char, - pub sasl_password: *const libc::c_char, - pub sasl_realm: *const libc::c_char, - pub sasl_secret: *mut libc::c_void, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body { - pub bd_type: libc::c_int, - pub bd_data: unnamed_1, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_1 { - pub bd_body_1part: *mut mailimap_body_type_1part, - pub bd_body_mpart: *mut mailimap_body_type_mpart, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_type_mpart { - pub bd_list: *mut clist, - pub bd_media_subtype: *mut libc::c_char, - pub bd_ext_mpart: *mut mailimap_body_ext_mpart, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_ext_mpart { - pub bd_parameter: *mut mailimap_body_fld_param, - pub bd_disposition: *mut mailimap_body_fld_dsp, - pub bd_language: *mut mailimap_body_fld_lang, - pub bd_loc: *mut libc::c_char, - pub bd_extension_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_fld_lang { - pub lg_type: libc::c_int, - pub lg_data: unnamed_2, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_2 { - pub lg_single: *mut libc::c_char, - pub lg_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_fld_dsp { - pub dsp_type: *mut libc::c_char, - pub dsp_attributes: *mut mailimap_body_fld_param, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_fld_param { - pub pa_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_type_1part { - pub bd_type: libc::c_int, - pub bd_data: unnamed_3, - pub bd_ext_1part: *mut mailimap_body_ext_1part, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_ext_1part { - pub bd_md5: *mut libc::c_char, - pub bd_disposition: *mut mailimap_body_fld_dsp, - pub bd_language: *mut mailimap_body_fld_lang, - pub bd_loc: *mut libc::c_char, - pub bd_extension_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_3 { - pub bd_type_basic: *mut mailimap_body_type_basic, - pub bd_type_msg: *mut mailimap_body_type_msg, - pub bd_type_text: *mut mailimap_body_type_text, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_type_text { - pub bd_media_text: *mut libc::c_char, - pub bd_fields: *mut mailimap_body_fields, - pub bd_lines: uint32_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_fields { - pub bd_parameter: *mut mailimap_body_fld_param, - pub bd_id: *mut libc::c_char, - pub bd_description: *mut libc::c_char, - pub bd_encoding: *mut mailimap_body_fld_enc, - pub bd_size: uint32_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_fld_enc { - pub enc_type: libc::c_int, - pub enc_value: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_type_msg { - pub bd_fields: *mut mailimap_body_fields, - pub bd_envelope: *mut mailimap_envelope, - pub bd_body: *mut mailimap_body, - pub bd_lines: uint32_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_envelope { - pub env_date: *mut libc::c_char, - pub env_subject: *mut libc::c_char, - pub env_from: *mut mailimap_env_from, - pub env_sender: *mut mailimap_env_sender, - pub env_reply_to: *mut mailimap_env_reply_to, - pub env_to: *mut mailimap_env_to, - pub env_cc: *mut mailimap_env_cc, - pub env_bcc: *mut mailimap_env_bcc, - pub env_in_reply_to: *mut libc::c_char, - pub env_message_id: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_env_bcc { - pub bcc_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_env_cc { - pub cc_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_env_to { - pub to_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_env_reply_to { - pub rt_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_env_sender { - pub snd_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_env_from { - pub frm_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_body_type_basic { - pub bd_media_basic: *mut mailimap_media_basic, - pub bd_fields: *mut mailimap_body_fields, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_media_basic { - pub med_type: libc::c_int, - pub med_basic_type: *mut libc::c_char, - pub med_subtype: *mut libc::c_char, -} -pub type unnamed_4 = libc::c_uint; -pub const MAILIMAP_CAPABILITY_NAME: unnamed_4 = 1; -pub const MAILIMAP_CAPABILITY_AUTH_TYPE: unnamed_4 = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_capability { - pub cap_type: libc::c_int, - pub cap_data: unnamed_5, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_5 { - pub cap_auth_type: *mut libc::c_char, - pub cap_name: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_capability_data { - pub cap_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_extension_data { - pub ext_extension: *mut mailimap_extension_api, - pub ext_type: libc::c_int, - pub ext_data: *mut libc::c_void, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_extension_api { - pub ext_name: *mut libc::c_char, - pub ext_id: libc::c_int, - pub ext_parser: Option< - unsafe extern "C" fn( - _: libc::c_int, - _: *mut mailstream, - _: *mut MMAPString, - _: *mut mailimap_parser_context, - _: *mut size_t, - _: *mut *mut mailimap_extension_data, - _: size_t, - _: Option ()>, - ) -> libc::c_int, - >, - pub ext_free: Option ()>, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_parser_context { - pub is_rambler_workaround_enabled: libc::c_int, - pub is_qip_workaround_enabled: libc::c_int, - pub msg_body_handler: Option< - unsafe extern "C" fn( - _: libc::c_int, - _: *mut mailimap_msg_att_body_section, - _: *const libc::c_char, - _: size_t, - _: *mut libc::c_void, - ) -> bool, - >, - pub msg_body_handler_context: *mut libc::c_void, - pub msg_body_section: *mut mailimap_msg_att_body_section, - pub msg_body_att_type: libc::c_int, - pub msg_body_parse_in_progress: bool, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_msg_att_body_section { - pub sec_section: *mut mailimap_section, - pub sec_origin_octet: uint32_t, - pub sec_body_part: *mut libc::c_char, - pub sec_length: size_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_section { - pub sec_spec: *mut mailimap_section_spec, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_section_spec { - pub sec_type: libc::c_int, - pub sec_data: unnamed_6, - pub sec_text: *mut mailimap_section_text, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_section_text { - pub sec_type: libc::c_int, - pub sec_msgtext: *mut mailimap_section_msgtext, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_section_msgtext { - pub sec_type: libc::c_int, - pub sec_header_list: *mut mailimap_header_list, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_header_list { - pub hdr_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_6 { - pub sec_msgtext: *mut mailimap_section_msgtext, - pub sec_part: *mut mailimap_section_part, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_section_part { - pub sec_id: *mut clist, -} -pub type mailimap_msg_body_handler = unsafe fn( - _: libc::c_int, - _: *mut mailimap_msg_att_body_section, - _: *const libc::c_char, - _: size_t, - _: *mut libc::c_void, -) -> bool; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_date_time { - pub dt_day: libc::c_int, - pub dt_month: libc::c_int, - pub dt_year: libc::c_int, - pub dt_hour: libc::c_int, - pub dt_min: libc::c_int, - pub dt_sec: libc::c_int, - pub dt_zone: libc::c_int, -} -pub type unnamed_7 = libc::c_uint; -pub const MAILIMAP_FLAG_EXTENSION: unnamed_7 = 6; -pub const MAILIMAP_FLAG_KEYWORD: unnamed_7 = 5; -pub const MAILIMAP_FLAG_DRAFT: unnamed_7 = 4; -pub const MAILIMAP_FLAG_SEEN: unnamed_7 = 3; -pub const MAILIMAP_FLAG_DELETED: unnamed_7 = 2; -pub const MAILIMAP_FLAG_FLAGGED: unnamed_7 = 1; -pub const MAILIMAP_FLAG_ANSWERED: unnamed_7 = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_flag { - pub fl_type: libc::c_int, - pub fl_data: unnamed_8, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_8 { - pub fl_keyword: *mut libc::c_char, - pub fl_extension: *mut libc::c_char, -} -pub type unnamed_9 = libc::c_uint; -pub const MAILIMAP_FLAG_FETCH_OTHER: unnamed_9 = 2; -pub const MAILIMAP_FLAG_FETCH_RECENT: unnamed_9 = 1; -pub const MAILIMAP_FLAG_FETCH_ERROR: unnamed_9 = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_flag_fetch { - pub fl_type: libc::c_int, - pub fl_flag: *mut mailimap_flag, -} -pub type unnamed_10 = libc::c_uint; -pub const MAILIMAP_FLAG_PERM_ALL: unnamed_10 = 2; -pub const MAILIMAP_FLAG_PERM_FLAG: unnamed_10 = 1; -pub const MAILIMAP_FLAG_PERM_ERROR: unnamed_10 = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_flag_perm { - pub fl_type: libc::c_int, - pub fl_flag: *mut mailimap_flag, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_flag_list { - pub fl_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_mailbox_data_status { - pub st_mailbox: *mut libc::c_char, - pub st_info_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_msg_att { - pub att_list: *mut clist, - pub att_number: uint32_t, -} -pub type unnamed_11 = libc::c_uint; -pub const MAILIMAP_MSG_ATT_ITEM_EXTENSION: unnamed_11 = 3; -pub const MAILIMAP_MSG_ATT_ITEM_STATIC: unnamed_11 = 2; -pub const MAILIMAP_MSG_ATT_ITEM_DYNAMIC: unnamed_11 = 1; -pub const MAILIMAP_MSG_ATT_ITEM_ERROR: unnamed_11 = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_msg_att_item { - pub att_type: libc::c_int, - pub att_data: unnamed_12, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_12 { - pub att_dyn: *mut mailimap_msg_att_dynamic, - pub att_static: *mut mailimap_msg_att_static, - pub att_extension_data: *mut mailimap_extension_data, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_msg_att_static { - pub att_type: libc::c_int, - pub att_data: unnamed_13, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_13 { - pub att_env: *mut mailimap_envelope, - pub att_internal_date: *mut mailimap_date_time, - pub att_rfc822: unnamed_16, - pub att_rfc822_header: unnamed_15, - pub att_rfc822_text: unnamed_14, - pub att_rfc822_size: uint32_t, - pub att_bodystructure: *mut mailimap_body, - pub att_body: *mut mailimap_body, - pub att_body_section: *mut mailimap_msg_att_body_section, - pub att_uid: uint32_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct unnamed_14 { - pub att_content: *mut libc::c_char, - pub att_length: size_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct unnamed_15 { - pub att_content: *mut libc::c_char, - pub att_length: size_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct unnamed_16 { - pub att_content: *mut libc::c_char, - pub att_length: size_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_msg_att_dynamic { - pub att_list: *mut clist, -} -pub type unnamed_17 = libc::c_uint; -pub const MAILIMAP_MSG_ATT_UID: unnamed_17 = 10; -pub const MAILIMAP_MSG_ATT_BODY_SECTION: unnamed_17 = 9; -pub const MAILIMAP_MSG_ATT_BODYSTRUCTURE: unnamed_17 = 8; -pub const MAILIMAP_MSG_ATT_BODY: unnamed_17 = 7; -pub const MAILIMAP_MSG_ATT_RFC822_SIZE: unnamed_17 = 6; -pub const MAILIMAP_MSG_ATT_RFC822_TEXT: unnamed_17 = 5; -pub const MAILIMAP_MSG_ATT_RFC822_HEADER: unnamed_17 = 4; -pub const MAILIMAP_MSG_ATT_RFC822: unnamed_17 = 3; -pub const MAILIMAP_MSG_ATT_INTERNALDATE: unnamed_17 = 2; -pub const MAILIMAP_MSG_ATT_ENVELOPE: unnamed_17 = 1; -pub const MAILIMAP_MSG_ATT_ERROR: unnamed_17 = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_set_item { - pub set_first: uint32_t, - pub set_last: uint32_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_set { - pub set_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_fetch_att { - pub att_type: libc::c_int, - pub att_section: *mut mailimap_section, - pub att_offset: uint32_t, - pub att_size: uint32_t, - pub att_extension: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_fetch_type { - pub ft_type: libc::c_int, - pub ft_data: unnamed_18, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_18 { - pub ft_fetch_att: *mut mailimap_fetch_att, - pub ft_fetch_att_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_store_att_flags { - pub fl_sign: libc::c_int, - pub fl_silent: libc::c_int, - pub fl_flag_list: *mut mailimap_flag_list, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct unnamed_19 { - pub sasl_conn: *mut libc::c_void, - pub sasl_server_fqdn: *const libc::c_char, - pub sasl_login: *const libc::c_char, - pub sasl_auth_name: *const libc::c_char, - pub sasl_password: *const libc::c_char, - pub sasl_realm: *const libc::c_char, - pub sasl_secret: *mut libc::c_void, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_response_info { - pub rsp_alert: *mut libc::c_char, - pub rsp_parse: *mut libc::c_char, - pub rsp_badcharset: *mut clist, - pub rsp_trycreate: libc::c_int, - pub rsp_mailbox_list: *mut clist, - pub rsp_mailbox_lsub: *mut clist, - pub rsp_search_result: *mut clist, - pub rsp_status: *mut mailimap_mailbox_data_status, - pub rsp_expunged: *mut clist, - pub rsp_fetch_list: *mut clist, - pub rsp_extension_list: *mut clist, - pub rsp_atom: *mut libc::c_char, - pub rsp_value: *mut libc::c_char, -} -#[derive(BitfieldStruct, Clone, Copy)] -#[repr(C)] -pub struct mailimap_selection_info { - pub sel_perm_flags: *mut clist, - pub sel_perm: libc::c_int, - pub sel_uidnext: uint32_t, - pub sel_uidvalidity: uint32_t, - pub sel_first_unseen: uint32_t, - pub sel_flags: *mut mailimap_flag_list, - pub sel_exists: uint32_t, - pub sel_recent: uint32_t, - pub sel_unseen: uint32_t, - #[bitfield(name = "sel_has_exists", ty = "uint8_t", bits = "0..=0")] - #[bitfield(name = "sel_has_recent", ty = "uint8_t", bits = "1..=1")] - pub sel_has_exists_sel_has_recent: [u8; 1], - pub _pad: [u8; 3], -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_connection_info { - pub imap_capability: *mut mailimap_capability_data, -} -pub type unnamed_20 = libc::c_uint; -pub const MAILIMAP_ERROR_CLIENTID: unnamed_20 = 46; -pub const MAILIMAP_ERROR_CUSTOM_COMMAND: unnamed_20 = 45; -pub const MAILIMAP_ERROR_NEEDS_MORE_DATA: unnamed_20 = 44; -pub const MAILIMAP_ERROR_SSL: unnamed_20 = 43; -pub const MAILIMAP_ERROR_SASL: unnamed_20 = 42; -pub const MAILIMAP_ERROR_EXTENSION: unnamed_20 = 41; -pub const MAILIMAP_ERROR_INVAL: unnamed_20 = 40; -pub const MAILIMAP_ERROR_STARTTLS: unnamed_20 = 39; -pub const MAILIMAP_ERROR_UNSUBSCRIBE: unnamed_20 = 38; -pub const MAILIMAP_ERROR_SUBSCRIBE: unnamed_20 = 37; -pub const MAILIMAP_ERROR_UID_STORE: unnamed_20 = 36; -pub const MAILIMAP_ERROR_STORE: unnamed_20 = 35; -pub const MAILIMAP_ERROR_STATUS: unnamed_20 = 34; -pub const MAILIMAP_ERROR_SELECT: unnamed_20 = 33; -pub const MAILIMAP_ERROR_UID_SEARCH: unnamed_20 = 32; -pub const MAILIMAP_ERROR_SEARCH: unnamed_20 = 31; -pub const MAILIMAP_ERROR_RENAME: unnamed_20 = 30; -pub const MAILIMAP_ERROR_LSUB: unnamed_20 = 29; -pub const MAILIMAP_ERROR_LOGIN: unnamed_20 = 28; -pub const MAILIMAP_ERROR_LIST: unnamed_20 = 27; -pub const MAILIMAP_ERROR_UID_FETCH: unnamed_20 = 26; -pub const MAILIMAP_ERROR_FETCH: unnamed_20 = 25; -pub const MAILIMAP_ERROR_EXAMINE: unnamed_20 = 24; -pub const MAILIMAP_ERROR_DELETE: unnamed_20 = 23; -pub const MAILIMAP_ERROR_CREATE: unnamed_20 = 22; -pub const MAILIMAP_ERROR_UID_MOVE: unnamed_20 = 21; -pub const MAILIMAP_ERROR_MOVE: unnamed_20 = 20; -pub const MAILIMAP_ERROR_UID_COPY: unnamed_20 = 19; -pub const MAILIMAP_ERROR_COPY: unnamed_20 = 18; -pub const MAILIMAP_ERROR_EXPUNGE: unnamed_20 = 17; -pub const MAILIMAP_ERROR_CLOSE: unnamed_20 = 16; -pub const MAILIMAP_ERROR_CHECK: unnamed_20 = 15; -pub const MAILIMAP_ERROR_CAPABILITY: unnamed_20 = 14; -pub const MAILIMAP_ERROR_LOGOUT: unnamed_20 = 13; -pub const MAILIMAP_ERROR_NOOP: unnamed_20 = 12; -pub const MAILIMAP_ERROR_APPEND: unnamed_20 = 11; -pub const MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION: unnamed_20 = 10; -pub const MAILIMAP_ERROR_PROTOCOL: unnamed_20 = 9; -pub const MAILIMAP_ERROR_FATAL: unnamed_20 = 8; -pub const MAILIMAP_ERROR_MEMORY: unnamed_20 = 7; -pub const MAILIMAP_ERROR_CONNECTION_REFUSED: unnamed_20 = 6; -pub const MAILIMAP_ERROR_PARSE: unnamed_20 = 5; -pub const MAILIMAP_ERROR_STREAM: unnamed_20 = 4; -pub const MAILIMAP_ERROR_BAD_STATE: unnamed_20 = 3; -pub const MAILIMAP_NO_ERROR_NON_AUTHENTICATED: unnamed_20 = 2; -pub const MAILIMAP_NO_ERROR_AUTHENTICATED: unnamed_20 = 1; -pub const MAILIMAP_NO_ERROR: unnamed_20 = 0; - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_fields { - pub fld_list: *mut clist, -} - -pub const MAILIMF_FIELD_OPTIONAL_FIELD: unnamed = 22; -pub const MAILIMF_FIELD_KEYWORDS: unnamed = 21; -pub const MAILIMF_FIELD_COMMENTS: unnamed = 20; -pub const MAILIMF_FIELD_SUBJECT: unnamed = 19; -pub const MAILIMF_FIELD_REFERENCES: unnamed = 18; -pub const MAILIMF_FIELD_IN_REPLY_TO: unnamed = 17; -pub const MAILIMF_FIELD_MESSAGE_ID: unnamed = 16; -pub const MAILIMF_FIELD_BCC: unnamed = 15; -pub const MAILIMF_FIELD_CC: unnamed = 14; -pub const MAILIMF_FIELD_TO: unnamed = 13; -pub const MAILIMF_FIELD_REPLY_TO: unnamed = 12; -pub const MAILIMF_FIELD_SENDER: unnamed = 11; -pub const MAILIMF_FIELD_FROM: unnamed = 10; -pub const MAILIMF_FIELD_ORIG_DATE: unnamed = 9; -pub const MAILIMF_FIELD_RESENT_MSG_ID: unnamed = 8; -pub const MAILIMF_FIELD_RESENT_BCC: unnamed = 7; -pub const MAILIMF_FIELD_RESENT_CC: unnamed = 6; -pub const MAILIMF_FIELD_RESENT_TO: unnamed = 5; -pub const MAILIMF_FIELD_RESENT_SENDER: unnamed = 4; -pub const MAILIMF_FIELD_RESENT_FROM: unnamed = 3; -pub const MAILIMF_FIELD_RESENT_DATE: unnamed = 2; -pub const MAILIMF_FIELD_RETURN_PATH: unnamed = 1; -pub const MAILIMF_FIELD_NONE: unnamed = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_field { - pub fld_type: libc::c_int, - pub fld_data: field_data, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union field_data { - pub fld_return_path: *mut mailimf_return, - pub fld_resent_date: *mut mailimf_orig_date, - pub fld_resent_from: *mut mailimf_from, - pub fld_resent_sender: *mut mailimf_sender, - pub fld_resent_to: *mut mailimf_to, - pub fld_resent_cc: *mut mailimf_cc, - pub fld_resent_bcc: *mut mailimf_bcc, - pub fld_resent_msg_id: *mut mailimf_message_id, - pub fld_orig_date: *mut mailimf_orig_date, - pub fld_from: *mut mailimf_from, - pub fld_sender: *mut mailimf_sender, - pub fld_reply_to: *mut mailimf_reply_to, - pub fld_to: *mut mailimf_to, - pub fld_cc: *mut mailimf_cc, - pub fld_bcc: *mut mailimf_bcc, - pub fld_message_id: *mut mailimf_message_id, - pub fld_in_reply_to: *mut mailimf_in_reply_to, - pub fld_references: *mut mailimf_references, - pub fld_subject: *mut mailimf_subject, - pub fld_comments: *mut mailimf_comments, - pub fld_keywords: *mut mailimf_keywords, - pub fld_optional_field: *mut mailimf_optional_field, -} - -pub const MAILMIME_MESSAGE: unnamed_11 = 3; -pub const MAILMIME_MULTIPLE: unnamed_11 = 2; -pub const MAILMIME_SINGLE: unnamed_11 = 1; -pub const MAILMIME_NONE: unnamed_11 = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime { - pub mm_parent_type: libc::c_int, - pub mm_parent: *mut mailmime, - pub mm_multipart_pos: *mut clistiter, - pub mm_type: libc::c_int, - pub mm_mime_start: *const libc::c_char, - pub mm_length: size_t, - pub mm_mime_fields: *mut mailmime_fields, - pub mm_content_type: *mut mailmime_content, - pub mm_body: *mut mailmime_data, - pub mm_data: unnamed_12n, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_12n { - pub mm_single: *mut mailmime_data, - pub mm_multipart: unnamed_14n, - pub mm_message: unnamed_13n, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct unnamed_13n { - pub mm_fields: *mut mailimf_fields, - pub mm_msg_mime: *mut mailmime, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct unnamed_14n { - pub mm_preamble: *mut mailmime_data, - pub mm_epilogue: *mut mailmime_data, - pub mm_mp_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_fields { - pub fld_list: *mut clist, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_content { - pub ct_type: *mut mailmime_type, - pub ct_subtype: *mut libc::c_char, - pub ct_parameters: *mut clist, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_data { - pub dt_type: libc::c_int, - pub dt_encoding: libc::c_int, - pub dt_encoded: libc::c_int, - pub dt_data: unnamed_9n, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_9n { - pub dt_text: unnamed_10n, - pub dt_filename: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct unnamed_10n { - pub dt_data: *const libc::c_char, - pub dt_length: size_t, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_type { - pub tp_type: libc::c_int, - pub tp_data: unnamed_3n, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_3n { - pub tp_discrete_type: *mut mailmime_discrete_type, - pub tp_composite_type: *mut mailmime_composite_type, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_discrete_type { - pub dt_type: libc::c_int, - pub dt_extension: *mut libc::c_char, -} - -pub const MAILMIME_COMPOSITE_TYPE_EXTENSION: libc::c_int = 3; -pub const MAILMIME_COMPOSITE_TYPE_MULTIPART: libc::c_int = 2; -pub const MAILMIME_COMPOSITE_TYPE_MESSAGE: libc::c_int = 1; -pub const MAILMIME_COMPOSITE_TYPE_ERROR: libc::c_int = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_composite_type { - pub ct_type: libc::c_int, - pub ct_token: *mut libc::c_char, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_optional_field { - pub fld_name: *mut libc::c_char, - pub fld_value: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_keywords { - pub kw_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_comments { - pub cm_value: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_subject { - pub sbj_value: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_references { - pub mid_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_in_reply_to { - pub mid_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_message_id { - pub mid_value: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_bcc { - pub bcc_addr_list: *mut mailimf_address_list, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_cc { - pub cc_addr_list: *mut mailimf_address_list, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_to { - pub to_addr_list: *mut mailimf_address_list, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_reply_to { - pub rt_addr_list: *mut mailimf_address_list, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_sender { - pub snd_mb: *mut mailimf_mailbox, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_from { - pub frm_mb_list: *mut mailimf_mailbox_list, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_orig_date { - pub dt_date_time: *mut mailimf_date_time, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_return { - pub ret_path: *mut mailimf_path, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_path { - pub pt_addr_spec: *mut libc::c_char, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_mailbox { - pub mb_display_name: *mut libc::c_char, - pub mb_addr_spec: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_address_list { - pub ad_list: *mut clist, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_mailbox_list { - pub mb_list: *mut clist, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_date_time { - pub dt_day: libc::c_int, - pub dt_month: libc::c_int, - pub dt_year: libc::c_int, - pub dt_hour: libc::c_int, - pub dt_min: libc::c_int, - pub dt_sec: libc::c_int, - pub dt_zone: libc::c_int, -} - -pub type __builtin_va_list = [__va_list_tag; 1]; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct __va_list_tag { - pub gp_offset: libc::c_uint, - pub fp_offset: libc::c_uint, - pub overflow_arg_area: *mut libc::c_void, - pub reg_save_area: *mut libc::c_void, -} -pub type va_list = __builtin_va_list; -pub type __int64_t = libc::c_longlong; -pub type __darwin_ct_rune_t = libc::c_int; -pub type __darwin_wchar_t = libc::c_int; -pub type __darwin_rune_t = __darwin_wchar_t; - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct timespec { - pub tv_sec: libc::time_t, - pub tv_nsec: libc::c_long, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _RuneEntry { - pub __min: __darwin_rune_t, - pub __max: __darwin_rune_t, - pub __map: __darwin_rune_t, - pub __types: *mut __uint32_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _RuneRange { - pub __nranges: libc::c_int, - pub __ranges: *mut _RuneEntry, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _RuneCharClass { - pub __name: [libc::c_char; 14], - pub __mask: __uint32_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct _RuneLocale { - pub __magic: [libc::c_char; 8], - pub __encoding: [libc::c_char; 32], - pub __sgetrune: Option< - unsafe extern "C" fn( - _: *const libc::c_char, - _: size_t, - _: *mut *const libc::c_char, - ) -> __darwin_rune_t, - >, - pub __sputrune: Option< - unsafe extern "C" fn( - _: __darwin_rune_t, - _: *mut libc::c_char, - _: size_t, - _: *mut *mut libc::c_char, - ) -> libc::c_int, - >, - pub __invalid_rune: __darwin_rune_t, - pub __runetype: [__uint32_t; 256], - pub __maplower: [__darwin_rune_t; 256], - pub __mapupper: [__darwin_rune_t; 256], - pub __runetype_ext: _RuneRange, - pub __maplower_ext: _RuneRange, - pub __mapupper_ext: _RuneRange, - pub __variable: *mut libc::c_void, - pub __variable_len: libc::c_int, - pub __ncharclasses: libc::c_int, - pub __charclasses: *mut _RuneCharClass, -} -pub type mode_t = libc::mode_t; -pub type off_t = libc::off_t; - -pub type uint64_t = libc::c_ulonglong; -pub type uid_t = libc::uid_t; -pub type gid_t = libc::gid_t; -pub type dev_t = libc::dev_t; -pub type blkcnt_t = libc::blkcnt_t; -pub type blksize_t = libc::blksize_t; -pub type nlink_t = __uint16_t; - -pub type dc_receive_imf_t = Option< - unsafe fn( - _: &dc_context_t, - _: *const libc::c_char, - _: size_t, - _: *const libc::c_char, - _: uint32_t, - _: 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( - _: &dc_context_t, - _: *const libc::c_char, - _: *const libc::c_char, - _: uint32_t, - ) -> libc::c_int, ->; -pub type dc_set_config_t = - Option ()>; -pub type dc_get_config_t = Option< - unsafe fn( - _: &dc_context_t, - _: *const libc::c_char, - _: *const libc::c_char, - ) -> *mut libc::c_char, ->; - -#[inline] -pub unsafe fn isascii(mut _c: libc::c_int) -> libc::c_int { - return (_c & !0x7fi32 == 0i32) as libc::c_int; -} - -#[inline] -pub unsafe fn tolower(mut _c: libc::c_int) -> libc::c_int { - return __tolower(_c); -} - -#[inline] -pub unsafe fn carray_count(mut array: *mut carray) -> libc::c_uint { - return (*array).len; -} - -#[inline] -pub unsafe fn carray_get(mut array: *mut carray, mut indx: libc::c_uint) -> *mut libc::c_void { - return *(*array).array.offset(indx as isize); -} - -/* * - * Callback function that should be given to dc_context_new(). - * - * @memberof dc_context_t - * @param context The context object as returned by dc_context_new(). - * @param event one of the @ref DC_EVENT constants - * @param data1 depends on the event parameter - * @param data2 depends on the event parameter - * @return return 0 unless stated otherwise in the event parameter documentation - */ -pub type dc_callback_t = - unsafe extern "C" fn(_: &dc_context_t, _: Event, _: uintptr_t, _: uintptr_t) -> uintptr_t; - -pub const DC_MOVE_STATE_MOVING: libc::c_uint = 3; -pub const DC_MOVE_STATE_STAY: libc::c_uint = 2; -pub const DC_MOVE_STATE_PENDING: libc::c_uint = 1; -pub const DC_MOVE_STATE_UNDEFINED: libc::c_uint = 0; - -pub const MAILIMF_ERROR_FILE: libc::c_uint = 4; -pub const MAILIMF_ERROR_INVAL: libc::c_uint = 3; -pub const MAILIMF_ERROR_MEMORY: libc::c_uint = 2; -pub const MAILIMF_ERROR_PARSE: libc::c_uint = 1; -pub const MAILIMF_NO_ERROR: libc::c_uint = 0; - -pub const MAIL_CHARCONV_ERROR_CONV: libc::c_uint = 3; -pub const MAIL_CHARCONV_ERROR_MEMORY: libc::c_uint = 2; -pub const MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET: libc::c_uint = 1; -pub const MAIL_CHARCONV_NO_ERROR: libc::c_uint = 0; - -pub const MAILSMTP_ERROR_CLIENTID_NOT_SUPPORTED: libc::c_uint = 28; -pub const MAILSMTP_ERROR_SSL: libc::c_uint = 27; -pub const MAILSMTP_ERROR_AUTH_AUTHENTICATION_FAILED: libc::c_uint = 26; -pub const MAILSMTP_ERROR_CONNECTION_REFUSED: libc::c_uint = 25; -pub const MAILSMTP_ERROR_STARTTLS_NOT_SUPPORTED: libc::c_uint = 24; -pub const MAILSMTP_ERROR_STARTTLS_TEMPORARY_FAILURE: libc::c_uint = 23; -pub const MAILSMTP_ERROR_AUTH_ENCRYPTION_REQUIRED: libc::c_uint = 22; -pub const MAILSMTP_ERROR_AUTH_TEMPORARY_FAILTURE: libc::c_uint = 21; -pub const MAILSMTP_ERROR_AUTH_TRANSITION_NEEDED: libc::c_uint = 20; -pub const MAILSMTP_ERROR_AUTH_TOO_WEAK: libc::c_uint = 19; -pub const MAILSMTP_ERROR_AUTH_REQUIRED: libc::c_uint = 18; -pub const MAILSMTP_ERROR_AUTH_LOGIN: libc::c_uint = 17; -pub const MAILSMTP_ERROR_AUTH_NOT_SUPPORTED: libc::c_uint = 16; -pub const MAILSMTP_ERROR_MEMORY: libc::c_uint = 15; -pub const MAILSMTP_ERROR_TRANSACTION_FAILED: libc::c_uint = 14; -pub const MAILSMTP_ERROR_USER_NOT_LOCAL: libc::c_uint = 13; -pub const MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: libc::c_uint = 12; -pub const MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: libc::c_uint = 11; -pub const MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: libc::c_uint = 10; -pub const MAILSMTP_ERROR_INSUFFICIENT_SYSTEM_STORAGE: libc::c_uint = 9; -pub const MAILSMTP_ERROR_IN_PROCESSING: libc::c_uint = 8; -pub const MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: libc::c_uint = 7; -pub const MAILSMTP_ERROR_ACTION_NOT_TAKEN: libc::c_uint = 6; -pub const MAILSMTP_ERROR_NOT_IMPLEMENTED: libc::c_uint = 5; -pub const MAILSMTP_ERROR_HOSTNAME: libc::c_uint = 4; -pub const MAILSMTP_ERROR_STREAM: libc::c_uint = 3; -pub const MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: libc::c_uint = 2; -pub const MAILSMTP_ERROR_UNEXPECTED_CODE: libc::c_uint = 1; -pub const MAILSMTP_NO_ERROR: libc::c_uint = 0; -pub const MAILSMTP_AUTH_KERBEROS_V4: libc::c_uint = 256; -pub const MAILSMTP_AUTH_NTLM: libc::c_uint = 128; -pub const MAILSMTP_AUTH_SRP: libc::c_uint = 64; -pub const MAILSMTP_AUTH_GSSAPI: libc::c_uint = 32; -pub const MAILSMTP_AUTH_DIGEST_MD5: libc::c_uint = 16; -pub const MAILSMTP_AUTH_LOGIN: libc::c_uint = 8; -pub const MAILSMTP_AUTH_PLAIN: libc::c_uint = 4; -pub const MAILSMTP_AUTH_CRAM_MD5: libc::c_uint = 2; -pub const MAILSMTP_AUTH_CHECKED: libc::c_uint = 1; -pub const MAILSMTP_AUTH_NOT_CHECKED: libc::c_uint = 0; - -pub type dc_imap_res = libc::c_uint; -pub const DC_SUCCESS: dc_imap_res = 3; -pub const DC_ALREADY_DONE: dc_imap_res = 2; -pub const DC_RETRY_LATER: dc_imap_res = 1; -pub const DC_FAILED: dc_imap_res = 0; - -pub type dc_move_state_t = libc::c_uint; - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_disposition_parm { - pub pa_type: libc::c_int, - pub pa_data: unnamed_20n, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_20n { - pub pa_filename: *mut libc::c_char, - pub pa_creation_date: *mut libc::c_char, - pub pa_modification_date: *mut libc::c_char, - pub pa_read_date: *mut libc::c_char, - pub pa_size: size_t, - pub pa_parameter: *mut mailmime_parameter, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_field { - pub fld_type: libc::c_int, - pub fld_data: unnamed_5n, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_5n { - pub fld_content: *mut mailmime_content, - pub fld_encoding: *mut mailmime_mechanism, - pub fld_id: *mut libc::c_char, - pub fld_description: *mut libc::c_char, - pub fld_version: uint32_t, - pub fld_disposition: *mut mailmime_disposition, - pub fld_language: *mut mailmime_language, - pub fld_location: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_language { - pub lg_list: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_disposition { - pub dsp_type: *mut mailmime_disposition_type, - pub dsp_parms: *mut clist, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_disposition_type { - pub dsp_type: libc::c_int, - pub dsp_extension: *mut libc::c_char, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_mechanism { - pub enc_type: libc::c_int, - pub enc_token: *mut libc::c_char, -} - -pub const MAILIMF_ADDRESS_GROUP: libc::c_uint = 2; -pub const MAILIMF_ADDRESS_MAILBOX: libc::c_uint = 1; -pub const MAILIMF_ADDRESS_ERROR: libc::c_uint = 0; -pub const MAILMIME_MECHANISM_TOKEN: libc::c_uint = 6; -pub const MAILMIME_MECHANISM_BASE64: libc::c_uint = 5; -pub const MAILMIME_MECHANISM_QUOTED_PRINTABLE: libc::c_uint = 4; -pub const MAILMIME_MECHANISM_BINARY: libc::c_uint = 3; -pub const MAILMIME_MECHANISM_8BIT: libc::c_uint = 2; -pub const MAILMIME_MECHANISM_7BIT: libc::c_uint = 1; -pub const MAILMIME_MECHANISM_ERROR: libc::c_uint = 0; - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_group { - pub grp_display_name: *mut libc::c_char, - pub grp_mb_list: *mut mailimf_mailbox_list, -} - -pub const MAILMIME_DISPOSITION_TYPE_EXTENSION: libc::c_uint = 3; -pub const MAILMIME_DISPOSITION_TYPE_ATTACHMENT: libc::c_uint = 2; -pub const MAILMIME_DISPOSITION_TYPE_INLINE: libc::c_uint = 1; -pub const MAILMIME_DISPOSITION_TYPE_ERROR: libc::c_uint = 0; -pub const MAILMIME_DISPOSITION_PARM_PARAMETER: libc::c_uint = 5; -pub const MAILMIME_DISPOSITION_PARM_SIZE: libc::c_uint = 4; -pub const MAILMIME_DISPOSITION_PARM_READ_DATE: libc::c_uint = 3; -pub const MAILMIME_DISPOSITION_PARM_MODIFICATION_DATE: libc::c_uint = 2; -pub const MAILMIME_DISPOSITION_PARM_CREATION_DATE: libc::c_uint = 1; -pub const MAILMIME_DISPOSITION_PARM_FILENAME: libc::c_uint = 0; - -pub const MAILMIME_FIELD_LOCATION: libc::c_uint = 8; -pub const MAILMIME_FIELD_LANGUAGE: libc::c_uint = 7; -pub const MAILMIME_FIELD_DISPOSITION: libc::c_uint = 6; -pub const MAILMIME_FIELD_VERSION: libc::c_uint = 5; -pub const MAILMIME_FIELD_DESCRIPTION: libc::c_uint = 4; -pub const MAILMIME_FIELD_ID: libc::c_uint = 3; -pub const MAILMIME_FIELD_TRANSFER_ENCODING: libc::c_uint = 2; -pub const MAILMIME_FIELD_TYPE: libc::c_uint = 1; -pub const MAILMIME_FIELD_NONE: libc::c_uint = 0; - -pub const MAILMIME_TYPE_COMPOSITE_TYPE: libc::c_uint = 2; -pub const MAILMIME_TYPE_DISCRETE_TYPE: libc::c_uint = 1; -pub const MAILMIME_TYPE_ERROR: libc::c_uint = 0; -pub const MAILMIME_DATA_FILE: libc::c_uint = 1; -pub const MAILMIME_DATA_TEXT: libc::c_uint = 0; - -pub const MAIL_ERROR_SSL: libc::c_uint = 58; -pub const MAIL_ERROR_FOLDER: libc::c_uint = 57; -pub const MAIL_ERROR_UNABLE: libc::c_uint = 56; -pub const MAIL_ERROR_SYSTEM: libc::c_uint = 55; -pub const MAIL_ERROR_COMMAND: libc::c_uint = 54; -pub const MAIL_ERROR_SEND: libc::c_uint = 53; -pub const MAIL_ERROR_CHAR_ENCODING_FAILED: libc::c_uint = 52; -pub const MAIL_ERROR_SUBJECT_NOT_FOUND: libc::c_uint = 51; -pub const MAIL_ERROR_PROGRAM_ERROR: libc::c_uint = 50; -pub const MAIL_ERROR_NO_PERMISSION: libc::c_uint = 49; -pub const MAIL_ERROR_COMMAND_NOT_SUPPORTED: libc::c_uint = 48; -pub const MAIL_ERROR_NO_APOP: libc::c_uint = 47; -pub const MAIL_ERROR_READONLY: libc::c_uint = 46; -pub const MAIL_ERROR_FATAL: libc::c_uint = 45; -pub const MAIL_ERROR_CLOSE: libc::c_uint = 44; -pub const MAIL_ERROR_CAPABILITY: libc::c_uint = 43; -pub const MAIL_ERROR_PROTOCOL: libc::c_uint = 42; -pub const MAIL_ERROR_MISC: libc::c_uint = 41; -pub const MAIL_ERROR_EXPUNGE: libc::c_uint = 40; -pub const MAIL_ERROR_NO_TLS: libc::c_uint = 39; -pub const MAIL_ERROR_CACHE_MISS: libc::c_uint = 38; -pub const MAIL_ERROR_STARTTLS: libc::c_uint = 37; -pub const MAIL_ERROR_MOVE: libc::c_uint = 36; -pub const MAIL_ERROR_FOLDER_NOT_FOUND: libc::c_uint = 35; -pub const MAIL_ERROR_REMOVE: libc::c_uint = 34; -pub const MAIL_ERROR_PART_NOT_FOUND: libc::c_uint = 33; -pub const MAIL_ERROR_INVAL: libc::c_uint = 32; -pub const MAIL_ERROR_PARSE: libc::c_uint = 31; -pub const MAIL_ERROR_MSG_NOT_FOUND: libc::c_uint = 30; -pub const MAIL_ERROR_DISKSPACE: libc::c_uint = 29; -pub const MAIL_ERROR_SEARCH: libc::c_uint = 28; -pub const MAIL_ERROR_STORE: libc::c_uint = 27; -pub const MAIL_ERROR_FETCH: libc::c_uint = 26; -pub const MAIL_ERROR_COPY: libc::c_uint = 25; -pub const MAIL_ERROR_APPEND: libc::c_uint = 24; -pub const MAIL_ERROR_LSUB: libc::c_uint = 23; -pub const MAIL_ERROR_LIST: libc::c_uint = 22; -pub const MAIL_ERROR_UNSUBSCRIBE: libc::c_uint = 21; -pub const MAIL_ERROR_SUBSCRIBE: libc::c_uint = 20; -pub const MAIL_ERROR_STATUS: libc::c_uint = 19; -pub const MAIL_ERROR_MEMORY: libc::c_uint = 18; -pub const MAIL_ERROR_SELECT: libc::c_uint = 17; -pub const MAIL_ERROR_EXAMINE: libc::c_uint = 16; -pub const MAIL_ERROR_CHECK: libc::c_uint = 15; -pub const MAIL_ERROR_RENAME: libc::c_uint = 14; -pub const MAIL_ERROR_NOOP: libc::c_uint = 13; -pub const MAIL_ERROR_LOGOUT: libc::c_uint = 12; -pub const MAIL_ERROR_DELETE: libc::c_uint = 11; -pub const MAIL_ERROR_CREATE: libc::c_uint = 10; -pub const MAIL_ERROR_LOGIN: libc::c_uint = 9; -pub const MAIL_ERROR_STREAM: libc::c_uint = 8; -pub const MAIL_ERROR_FILE: libc::c_uint = 7; -pub const MAIL_ERROR_BAD_STATE: libc::c_uint = 6; -pub const MAIL_ERROR_CONNECT: libc::c_uint = 5; -pub const MAIL_ERROR_UNKNOWN: libc::c_uint = 4; -pub const MAIL_ERROR_NOT_IMPLEMENTED: libc::c_uint = 3; -pub const MAIL_NO_ERROR_NON_AUTHENTICATED: libc::c_uint = 2; -pub const MAIL_NO_ERROR_AUTHENTICATED: libc::c_uint = 1; -pub const MAIL_NO_ERROR: libc::c_uint = 0; - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimf_address { - pub ad_type: libc::c_int, - pub ad_data: unnamed_0n, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_0n { - pub ad_mailbox: *mut mailimf_mailbox, - pub ad_group: *mut mailimf_group, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailmime_parameter { - pub pa_name: *mut libc::c_char, - pub pa_value: *mut libc::c_char, -} - -pub const MAILMIME_DISCRETE_TYPE_EXTENSION: libc::c_uint = 6; -pub const MAILMIME_DISCRETE_TYPE_APPLICATION: libc::c_uint = 5; -pub const MAILMIME_DISCRETE_TYPE_VIDEO: libc::c_uint = 4; -pub const MAILMIME_DISCRETE_TYPE_AUDIO: libc::c_uint = 3; -pub const MAILMIME_DISCRETE_TYPE_IMAGE: libc::c_uint = 2; -pub const MAILMIME_DISCRETE_TYPE_TEXT: libc::c_uint = 1; -pub const MAILMIME_DISCRETE_TYPE_ERROR: libc::c_uint = 0; -pub const MAILIMAP_MBX_LIST_OFLAG_FLAG_EXT: libc::c_uint = 2; -pub const MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS: libc::c_uint = 1; -pub const MAILIMAP_MBX_LIST_OFLAG_ERROR: libc::c_uint = 0; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_mbx_list_oflag { - pub of_type: libc::c_int, - pub of_flag_ext: *mut libc::c_char, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_mailbox_list { - pub mb_flag: *mut mailimap_mbx_list_flags, - pub mb_delimiter: libc::c_char, - pub mb_name: *mut libc::c_char, -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct mailimap_mbx_list_flags { - pub mbf_type: libc::c_int, - pub mbf_oflags: *mut clist, - pub mbf_sflag: libc::c_int, -} diff --git a/src/x.rs b/src/x.rs index db0a9981f..9d74ddc11 100644 --- a/src/x.rs +++ b/src/x.rs @@ -1,5 +1,3 @@ -use libc; - use crate::dc_strbuilder::dc_strbuilder_t; use crate::types::*; @@ -13,7 +11,7 @@ pub use libc::{ }; extern "C" { - pub static mut _DefaultRuneLocale: _RuneLocale; + // pub static mut _DefaultRuneLocale: _RuneLocale; pub fn clock() -> libc::clock_t; pub fn qsort( @@ -55,452 +53,6 @@ extern "C" { _: *const libc::c_char, ) -> !; - // --charconv - - pub fn charconv( - tocode: *const libc::c_char, - fromcode: *const libc::c_char, - str: *const libc::c_char, - length: size_t, - result: *mut *mut libc::c_char, - ) -> libc::c_int; - pub fn charconv_buffer( - tocode: *const libc::c_char, - fromcode: *const libc::c_char, - str: *const libc::c_char, - length: size_t, - result: *mut *mut libc::c_char, - result_len: *mut size_t, - ) -> libc::c_int; - pub fn charconv_buffer_free(str: *mut libc::c_char); - - // -- libetpan Methods - - pub fn libetpan_get_version_major() -> libc::c_int; - pub fn libetpan_get_version_minor() -> libc::c_int; - pub fn gethostname(_: *mut libc::c_char, _: size_t) -> libc::c_int; - pub fn mailsmtp_socket_connect( - session: *mut mailsmtp, - server: *const libc::c_char, - port: uint16_t, - ) -> libc::c_int; - pub fn mailsmtp_socket_starttls(session: *mut mailsmtp) -> libc::c_int; - pub fn mailsmtp_ssl_connect( - session: *mut mailsmtp, - server: *const libc::c_char, - port: uint16_t, - ) -> libc::c_int; - pub fn mailsmtp_oauth2_authenticate( - session: *mut mailsmtp, - auth_user: *const libc::c_char, - access_token: *const libc::c_char, - ) -> libc::c_int; - pub fn mailsmtp_new( - progr_rate: size_t, - progr_fun: Option ()>, - ) -> *mut mailsmtp; - pub fn mailsmtp_free(session: *mut mailsmtp); - pub fn mailsmtp_set_timeout(session: *mut mailsmtp, timeout: time_t); - pub fn mailsmtp_auth( - session: *mut mailsmtp, - user: *const libc::c_char, - pass: *const libc::c_char, - ) -> libc::c_int; - pub fn mailsmtp_helo(session: *mut mailsmtp) -> libc::c_int; - pub fn mailsmtp_mail(session: *mut mailsmtp, from: *const libc::c_char) -> libc::c_int; - pub fn mailsmtp_rcpt(session: *mut mailsmtp, to: *const libc::c_char) -> libc::c_int; - pub fn mailsmtp_data(session: *mut mailsmtp) -> libc::c_int; - pub fn mailsmtp_data_message( - session: *mut mailsmtp, - message: *const libc::c_char, - size: size_t, - ) -> libc::c_int; - pub fn mailesmtp_ehlo(session: *mut mailsmtp) -> libc::c_int; - pub fn mailesmtp_mail( - session: *mut mailsmtp, - from: *const libc::c_char, - return_full: libc::c_int, - envid: *const libc::c_char, - ) -> libc::c_int; - pub fn mailesmtp_rcpt( - session: *mut mailsmtp, - to: *const libc::c_char, - notify: libc::c_int, - orcpt: *const libc::c_char, - ) -> libc::c_int; - pub fn mailsmtp_strerror(errnum: libc::c_int) -> *const libc::c_char; - pub fn mailesmtp_auth_sasl( - session: *mut mailsmtp, - auth_type: *const libc::c_char, - server_fqdn: *const libc::c_char, - local_ip_port: *const libc::c_char, - remote_ip_port: *const libc::c_char, - login: *const libc::c_char, - auth_name: *const libc::c_char, - password: *const libc::c_char, - realm: *const libc::c_char, - ) -> libc::c_int; - pub fn mailsmtp_set_progress_callback( - session: *mut mailsmtp, - progr_fun: Option ()>, - context: *mut libc::c_void, - ); - - pub fn mailmime_base64_body_parse( - message: *const libc::c_char, - length: size_t, - indx: *mut size_t, - result: *mut *mut libc::c_char, - result_len: *mut size_t, - ) -> libc::c_int; - pub fn mailimf_address_new( - ad_type: libc::c_int, - ad_mailbox: *mut mailimf_mailbox, - ad_group: *mut mailimf_group, - ) -> *mut mailimf_address; - pub fn mailimf_mailbox_new( - mb_display_name: *mut libc::c_char, - mb_addr_spec: *mut libc::c_char, - ) -> *mut mailimf_mailbox; - pub fn mailimf_field_new( - fld_type: libc::c_int, - fld_return_path: *mut mailimf_return, - fld_resent_date: *mut mailimf_orig_date, - fld_resent_from: *mut mailimf_from, - fld_resent_sender: *mut mailimf_sender, - fld_resent_to: *mut mailimf_to, - fld_resent_cc: *mut mailimf_cc, - fld_resent_bcc: *mut mailimf_bcc, - fld_resent_msg_id: *mut mailimf_message_id, - fld_orig_date: *mut mailimf_orig_date, - fld_from: *mut mailimf_from, - fld_sender: *mut mailimf_sender, - fld_reply_to: *mut mailimf_reply_to, - fld_to: *mut mailimf_to, - fld_cc: *mut mailimf_cc, - fld_bcc: *mut mailimf_bcc, - fld_message_id: *mut mailimf_message_id, - fld_in_reply_to: *mut mailimf_in_reply_to, - fld_references: *mut mailimf_references, - fld_subject: *mut mailimf_subject, - fld_comments: *mut mailimf_comments, - fld_keywords: *mut mailimf_keywords, - fld_optional_field: *mut mailimf_optional_field, - ) -> *mut mailimf_field; - pub fn mailimf_subject_new(sbj_value: *mut libc::c_char) -> *mut mailimf_subject; - pub fn mailimf_mailbox_list_new_empty() -> *mut mailimf_mailbox_list; - pub fn mailimf_mailbox_list_add( - mailbox_list: *mut mailimf_mailbox_list, - mb: *mut mailimf_mailbox, - ) -> libc::c_int; - pub fn mailimf_address_list_new_empty() -> *mut mailimf_address_list; - pub fn mailimf_address_list_add( - address_list: *mut mailimf_address_list, - addr: *mut mailimf_address, - ) -> libc::c_int; - pub fn mailimf_fields_add( - fields: *mut mailimf_fields, - field: *mut mailimf_field, - ) -> libc::c_int; - pub fn mailimf_fields_new_with_data_all( - date: *mut mailimf_date_time, - from: *mut mailimf_mailbox_list, - sender: *mut mailimf_mailbox, - reply_to: *mut mailimf_address_list, - to: *mut mailimf_address_list, - cc: *mut mailimf_address_list, - bcc: *mut mailimf_address_list, - message_id: *mut libc::c_char, - in_reply_to: *mut clist, - references: *mut clist, - subject: *mut libc::c_char, - ) -> *mut mailimf_fields; - pub fn mailimf_get_date(time_0: time_t) -> *mut mailimf_date_time; - pub fn mailimf_field_new_custom( - name: *mut libc::c_char, - value: *mut libc::c_char, - ) -> *mut mailimf_field; - pub fn mailmime_parameter_new( - pa_name: *mut libc::c_char, - pa_value: *mut libc::c_char, - ) -> *mut mailmime_parameter; - pub fn mailmime_free(mime: *mut mailmime); - pub fn mailmime_disposition_parm_new( - pa_type: libc::c_int, - pa_filename: *mut libc::c_char, - pa_creation_date: *mut libc::c_char, - pa_modification_date: *mut libc::c_char, - pa_read_date: *mut libc::c_char, - pa_size: size_t, - pa_parameter: *mut mailmime_parameter, - ) -> *mut mailmime_disposition_parm; - pub fn mailmime_new_message_data(msg_mime: *mut mailmime) -> *mut mailmime; - pub fn mailmime_new_empty( - content: *mut mailmime_content, - mime_fields: *mut mailmime_fields, - ) -> *mut mailmime; - pub fn mailmime_set_body_file( - build_info: *mut mailmime, - filename: *mut libc::c_char, - ) -> libc::c_int; - pub fn mailmime_set_body_text( - build_info: *mut mailmime, - data_str: *mut libc::c_char, - length: size_t, - ) -> libc::c_int; - pub fn mailmime_add_part(build_info: *mut mailmime, part: *mut mailmime) -> libc::c_int; - pub fn mailmime_set_imf_fields(build_info: *mut mailmime, fields: *mut mailimf_fields); - pub fn mailmime_smart_add_part(mime: *mut mailmime, mime_sub: *mut mailmime) -> libc::c_int; - pub fn mailmime_content_new_with_str(str: *const libc::c_char) -> *mut mailmime_content; - pub fn mailmime_fields_new_encoding(type_0: libc::c_int) -> *mut mailmime_fields; - pub fn mailmime_multiple_new(type_0: *const libc::c_char) -> *mut mailmime; - pub fn mailmime_fields_new_filename( - dsp_type: libc::c_int, - filename: *mut libc::c_char, - encoding_type: libc::c_int, - ) -> *mut mailmime_fields; - pub fn mailmime_param_new_with_data( - name: *mut libc::c_char, - value: *mut libc::c_char, - ) -> *mut mailmime_parameter; - pub fn mailmime_write_mem( - f: *mut MMAPString, - col: *mut libc::c_int, - build_info: *mut mailmime, - ) -> libc::c_int; - pub fn mailimf_fields_free(fields: *mut mailimf_fields); - pub fn mailimf_fields_new_empty() -> *mut mailimf_fields; - pub fn mailimf_envelope_and_optional_fields_parse( - message: *const libc::c_char, - length: size_t, - indx: *mut size_t, - result: *mut *mut mailimf_fields, - ) -> libc::c_int; - pub fn mailmime_content_free(content: *mut mailmime_content); - pub fn mailmime_mechanism_new( - enc_type: libc::c_int, - enc_token: *mut libc::c_char, - ) -> *mut mailmime_mechanism; - pub fn mailmime_mechanism_free(mechanism: *mut mailmime_mechanism); - pub fn mailmime_fields_free(fields: *mut mailmime_fields); - pub fn mailmime_new( - mm_type: libc::c_int, - mm_mime_start: *const libc::c_char, - mm_length: size_t, - mm_mime_fields: *mut mailmime_fields, - mm_content_type: *mut mailmime_content, - mm_body: *mut mailmime_data, - mm_preamble: *mut mailmime_data, - mm_epilogue: *mut mailmime_data, - mm_mp_list: *mut clist, - mm_fields: *mut mailimf_fields, - mm_msg_mime: *mut mailmime, - ) -> *mut mailmime; - pub fn mailmime_fields_new_empty() -> *mut mailmime_fields; - pub fn mailmime_fields_new_with_data( - encoding: *mut mailmime_mechanism, - id: *mut libc::c_char, - description: *mut libc::c_char, - disposition: *mut mailmime_disposition, - language: *mut mailmime_language, - ) -> *mut mailmime_fields; - pub fn mailmime_get_content_message() -> *mut mailmime_content; - pub fn mailmime_parse( - message: *const libc::c_char, - length: size_t, - indx: *mut size_t, - result: *mut *mut mailmime, - ) -> libc::c_int; - pub fn mailmime_part_parse( - message: *const libc::c_char, - length: size_t, - indx: *mut size_t, - encoding: libc::c_int, - result: *mut *mut libc::c_char, - result_len: *mut size_t, - ) -> libc::c_int; - pub fn mailmime_substitute(old_mime: *mut mailmime, new_mime: *mut mailmime) -> libc::c_int; - pub fn mailprivacy_prepare_mime(mime: *mut mailmime); - pub fn mailmime_encoded_phrase_parse( - default_fromcode: *const libc::c_char, - message: *const libc::c_char, - length: size_t, - indx: *mut size_t, - tocode: *const libc::c_char, - result: *mut *mut libc::c_char, - ) -> libc::c_int; - pub fn mailimap_date_time_new( - dt_day: libc::c_int, - dt_month: libc::c_int, - dt_year: libc::c_int, - dt_hour: libc::c_int, - dt_min: libc::c_int, - dt_sec: libc::c_int, - dt_zone: libc::c_int, - ) -> *mut mailimap_date_time; - pub fn mailimap_xlist( - session: *mut mailimap, - mb: *const libc::c_char, - list_mb: *const libc::c_char, - result: *mut *mut clist, - ) -> libc::c_int; - pub fn mailimap_create(session: *mut mailimap, mb: *const libc::c_char) -> libc::c_int; - pub fn mailimap_list( - session: *mut mailimap, - mb: *const libc::c_char, - list_mb: *const libc::c_char, - result: *mut *mut clist, - ) -> libc::c_int; - pub fn mailimap_list_result_free(list: *mut clist); - pub fn mailimap_subscribe(session: *mut mailimap, mb: *const libc::c_char) -> libc::c_int; - pub fn mailstream_close(s: *mut mailstream) -> libc::c_int; - pub fn mailstream_wait_idle(s: *mut mailstream, max_idle_delay: libc::c_int) -> libc::c_int; - pub fn mailstream_setup_idle(s: *mut mailstream) -> libc::c_int; - pub fn mailstream_unsetup_idle(s: *mut mailstream); - pub fn mailstream_interrupt_idle(s: *mut mailstream); - pub fn mailimap_section_new(sec_spec: *mut mailimap_section_spec) -> *mut mailimap_section; - pub fn mailimap_set_free(set: *mut mailimap_set); - pub fn mailimap_fetch_type_free(fetch_type: *mut mailimap_fetch_type); - pub fn mailimap_store_att_flags_free(store_att_flags: *mut mailimap_store_att_flags); - pub fn mailimap_set_new_interval(first: uint32_t, last: uint32_t) -> *mut mailimap_set; - pub fn mailimap_set_new_single(indx: uint32_t) -> *mut mailimap_set; - pub fn mailimap_fetch_att_new_envelope() -> *mut mailimap_fetch_att; - pub fn mailimap_fetch_att_new_flags() -> *mut mailimap_fetch_att; - pub fn mailimap_fetch_att_new_uid() -> *mut mailimap_fetch_att; - pub fn mailimap_fetch_att_new_body_peek_section( - section: *mut mailimap_section, - ) -> *mut mailimap_fetch_att; - pub fn mailimap_fetch_type_new_fetch_att_list_empty() -> *mut mailimap_fetch_type; - pub fn mailimap_fetch_type_new_fetch_att_list_add( - fetch_type: *mut mailimap_fetch_type, - fetch_att: *mut mailimap_fetch_att, - ) -> libc::c_int; - pub fn mailimap_store_att_flags_new_add_flags( - flags: *mut mailimap_flag_list, - ) -> *mut mailimap_store_att_flags; - pub fn mailimap_flag_list_new_empty() -> *mut mailimap_flag_list; - pub fn mailimap_flag_list_add( - flag_list: *mut mailimap_flag_list, - f: *mut mailimap_flag, - ) -> libc::c_int; - pub fn mailimap_flag_new_deleted() -> *mut mailimap_flag; - pub fn mailimap_flag_new_seen() -> *mut mailimap_flag; - pub fn mailimap_flag_new_flag_keyword(flag_keyword: *mut libc::c_char) -> *mut mailimap_flag; - pub fn mailimap_socket_connect( - f: *mut mailimap, - server: *const libc::c_char, - port: uint16_t, - ) -> libc::c_int; - pub fn mailimap_socket_starttls(f: *mut mailimap) -> libc::c_int; - pub fn mailimap_ssl_connect( - f: *mut mailimap, - server: *const libc::c_char, - port: uint16_t, - ) -> libc::c_int; - pub fn mailimap_uidplus_uid_copy( - session: *mut mailimap, - set: *mut mailimap_set, - mb: *const libc::c_char, - uidvalidity_result: *mut uint32_t, - source_result: *mut *mut mailimap_set, - dest_result: *mut *mut mailimap_set, - ) -> libc::c_int; - pub fn mailimap_uidplus_uid_move( - session: *mut mailimap, - set: *mut mailimap_set, - mb: *const libc::c_char, - uidvalidity_result: *mut uint32_t, - source_result: *mut *mut mailimap_set, - dest_result: *mut *mut mailimap_set, - ) -> libc::c_int; - pub fn mailimap_idle(session: *mut mailimap) -> libc::c_int; - pub fn mailimap_idle_done(session: *mut mailimap) -> libc::c_int; - pub fn mailimap_has_idle(session: *mut mailimap) -> libc::c_int; - pub fn mailimap_has_xlist(session: *mut mailimap) -> libc::c_int; - pub fn mailimap_oauth2_authenticate( - session: *mut mailimap, - auth_user: *const libc::c_char, - access_token: *const libc::c_char, - ) -> libc::c_int; - pub fn mailimap_close(session: *mut mailimap) -> libc::c_int; - pub fn mailimap_fetch( - session: *mut mailimap, - set: *mut mailimap_set, - fetch_type: *mut mailimap_fetch_type, - result: *mut *mut clist, - ) -> libc::c_int; - pub fn mailimap_uid_fetch( - session: *mut mailimap, - set: *mut mailimap_set, - fetch_type: *mut mailimap_fetch_type, - result: *mut *mut clist, - ) -> libc::c_int; - pub fn mailimap_fetch_list_free(fetch_list: *mut clist); - pub fn mailimap_login( - session: *mut mailimap, - userid: *const libc::c_char, - password: *const libc::c_char, - ) -> libc::c_int; - pub fn mailimap_select(session: *mut mailimap, mb: *const libc::c_char) -> libc::c_int; - pub fn mailimap_uid_store( - session: *mut mailimap, - set: *mut mailimap_set, - store_att_flags: *mut mailimap_store_att_flags, - ) -> libc::c_int; - pub fn mailimap_new( - imap_progr_rate: size_t, - imap_progr_fun: Option ()>, - ) -> *mut mailimap; - pub fn mailimap_free(session: *mut mailimap); - pub fn mailimap_set_timeout(session: *mut mailimap, timeout: time_t); - pub fn mailimf_msg_id_parse( - message: *const libc::c_char, - length: size_t, - indx: *mut size_t, - result: *mut *mut libc::c_char, - ) -> libc::c_int; - pub fn mailimf_mailbox_list_free(mb_list: *mut mailimf_mailbox_list); - pub fn mailimf_mailbox_list_parse( - message: *const libc::c_char, - length: size_t, - indx: *mut size_t, - result: *mut *mut mailimf_mailbox_list, - ) -> libc::c_int; - pub fn mailmime_content_charset_get(content: *mut mailmime_content) -> *mut libc::c_char; - - pub fn carray_new(initsize: libc::c_uint) -> *mut carray; - pub fn carray_add( - array: *mut carray, - data: *mut libc::c_void, - indx: *mut libc::c_uint, - ) -> libc::c_int; - pub fn carray_set_size(array: *mut carray, new_size: libc::c_uint); - pub fn carray_free(array: *mut carray); - pub fn carray_delete_slow(array: *mut carray, indx: libc::c_uint) -> libc::c_int; - - pub fn mmap_string_unref(str: *mut libc::c_char) -> libc::c_int; - pub fn mmap_string_new(init: *const libc::c_char) -> *mut MMAPString; - pub fn mmap_string_free(string: *mut MMAPString); - pub fn mmap_string_append(string: *mut MMAPString, val: *const libc::c_char) - -> *mut MMAPString; - pub fn mmap_string_append_len( - string: *mut MMAPString, - val: *const libc::c_char, - len: size_t, - ) -> *mut MMAPString; - pub fn mmap_string_append_c(string: *mut MMAPString, c: libc::c_char) -> *mut MMAPString; - - pub fn clist_free(_: *mut clist); - pub fn clist_insert_after( - _: *mut clist, - _: *mut clistiter, - _: *mut libc::c_void, - ) -> libc::c_int; - pub fn clist_new() -> *mut clist; - pub fn clist_delete(_: *mut clist, _: *mut clistiter) -> *mut clistiter; - - pub fn encode_base64(in_0: *const libc::c_char, len: libc::c_int) -> *mut libc::c_char; - // -- DC Methods pub fn dc_strbuilder_catf(_: *mut dc_strbuilder_t, format: *const libc::c_char, _: ...); diff --git a/tests/stress.rs b/tests/stress.rs index 5e3af825a..82d7c8155 100644 --- a/tests/stress.rs +++ b/tests/stress.rs @@ -1,6 +1,11 @@ //! Stress some functions for testing; if used as a lib, this file is obsolete. use std::ffi::{CStr, CString}; + +use mmime::mailimf_types::*; +use mmime::mailmime_content::*; +use mmime::mailmime_types::*; +use mmime::other::*; use tempfile::tempdir; use deltachat::constants::*; @@ -57,7 +62,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { b"\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char; let mut plain: *mut libc::c_char = @@ -125,10 +130,10 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; free(plain as *mut libc::c_void); dc_simplify_unref(simplify); - let mut xml: *const libc::c_char = + let xml: *const libc::c_char = b"\n\n\n2019-03-06T21:09:57Z9.423110,53.790302\n\n \n\t2018-12-13T22:11:12Z\t 19.423110 \t , \n 63.790302\n \n\n\x00" as *const u8 as *const libc::c_char; - let mut kml: *mut dc_kml_t = dc_kml_parse(context, xml, strlen(xml)); + let kml: *mut dc_kml_t = dc_kml_parse(context, xml, strlen(xml)); if 0 != !(!(*kml).addr.is_null() && strcmp( (*kml).addr, @@ -336,9 +341,9 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - let mut abs_path: *mut libc::c_char = dc_mprintf( + let abs_path: *mut libc::c_char = dc_mprintf( b"%s/%s\x00" as *const u8 as *const libc::c_char, - (*context).blobdir, + context.get_blobdir(), b"foobar\x00" as *const u8 as *const libc::c_char, ); if 0 != (0 == dc_is_blobdir_path(context, abs_path)) as libc::c_int as libc::c_long { @@ -526,7 +531,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - let mut fn0: *mut libc::c_char = dc_get_fine_pathNfilename( + let fn0: *mut libc::c_char = dc_get_fine_pathNfilename( context, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, b"foobar.dadada\x00" as *const u8 as *const libc::c_char, @@ -562,7 +567,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { b"content\x00" as *const u8 as *const libc::c_char as *const libc::c_void, 7i32 as size_t, ); - let mut fn1: *mut libc::c_char = dc_get_fine_pathNfilename( + let fn1: *mut libc::c_char = dc_get_fine_pathNfilename( context, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, b"foobar.dadada\x00" as *const u8 as *const libc::c_char, @@ -605,7 +610,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { free(fn0 as *mut libc::c_void); free(fn1 as *mut libc::c_void); } - let mut txt: *const libc::c_char = + let txt: *const libc::c_char = b"FieldA: ValueA\nFieldB: ValueB\n\x00" as *const u8 as *const libc::c_char; let mut mime: *mut mailmime = 0 as *mut mailmime; let mut dummy: size_t = 0i32 as size_t; @@ -632,7 +637,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - let mut fields: *mut mailimf_fields = mailmime_find_mailimf_fields(mime); + let fields: *mut mailimf_fields = mailmime_find_mailimf_fields(mime); if 0 != fields.is_null() as libc::c_int as libc::c_long { __assert_rtn( (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) @@ -722,7 +727,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - let mut of_b: *mut mailimf_optional_field = + let of_b: *mut mailimf_optional_field = mailimf_find_optional_field(fields, b"FieldB\x00" as *const u8 as *const libc::c_char); if 0 != !(!of_b.is_null() && !(*of_b).fld_value.is_null()) as libc::c_int as libc::c_long { __assert_rtn( @@ -749,8 +754,8 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; mailmime_free(mime); - let mut mimeparser: *mut dc_mimeparser_t = dc_mimeparser_new((*context).blobdir, context); - let mut raw: *const libc::c_char = + let mimeparser: *mut dc_mimeparser_t = dc_mimeparser_new(context.get_blobdir(), context); + let raw: *const libc::c_char = b"Content-Type: multipart/mixed; boundary=\"==break==\";\nSubject: outer-subject\nX-Special-A: special-a\nFoo: Bar\nChat-Version: 0.0\n\n--==break==\nContent-Type: text/plain; protected-headers=\"v1\";\nSubject: inner-subject\nX-Special-B: special-b\nFoo: Xy\nChat-Version: 1.0\n\ntest1\n\n--==break==--\n\n\x00" as *const u8 as *const libc::c_char; dc_mimeparser_parse(mimeparser, raw, strlen(raw)); @@ -902,7 +907,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - let mut f: libc::c_double = dc_atof(b"1.23\x00" as *const u8 as *const libc::c_char); + let f: libc::c_double = dc_atof(b"1.23\x00" as *const u8 as *const libc::c_char); if 0 != !(f > 1.22f64 && f < 1.24f64) as libc::c_int as libc::c_long { __assert_rtn( (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) @@ -913,7 +918,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - let mut s: *mut libc::c_char = dc_ftoa(1.23f64); + let s: *mut libc::c_char = dc_ftoa(1.23f64); if 0 != !(dc_atof(s) > 1.22f64 && dc_atof(s) < 1.24f64) as libc::c_int as libc::c_long { __assert_rtn( (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) @@ -1080,7 +1085,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; let mut str: *mut libc::c_char = strdup(b"aaa\x00" as *const u8 as *const libc::c_char); - let mut replacements: libc::c_int = dc_str_replace( + let replacements: libc::c_int = dc_str_replace( &mut str, b"a\x00" as *const u8 as *const libc::c_char, b"ab\x00" as *const u8 as *const libc::c_char, @@ -2404,7 +2409,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - let mut arr = dc_array_new(7i32 as size_t); + let arr = dc_array_new(7i32 as size_t); if 0 != !(dc_array_get_cnt(arr) == 0) as libc::c_int as libc::c_long { __assert_rtn( (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) @@ -2416,7 +2421,6 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; let mut i: libc::c_int = 0; - i = 0i32; while i < 1000i32 { dc_array_add_id(arr, (i + 1i32 * 2i32) as uint32_t); i += 1 @@ -2632,7 +2636,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; dc_array_unref(arr); - let mut p1: *mut dc_param_t = dc_param_new(); + let p1: *mut dc_param_t = dc_param_new(); dc_param_set_packed( p1, b"\r\n\r\na=1\nb=2\n\nc = 3 \x00" as *const u8 as *const libc::c_char, @@ -3171,14 +3175,11 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; free(keys as *mut libc::c_void); - let mut ah: *mut dc_aheader_t = dc_aheader_new(); - let mut rendered: *mut libc::c_char = 0 as *mut libc::c_char; - let mut ah_ok: libc::c_int = 0; - ah_ok = dc_aheader_set_from_string( - ah, - b"addr=a@b.example.org; prefer-encrypt=mutual; keydata=RGVsdGEgQ2hhdA==\x00" as *const u8 - as *const libc::c_char, - ); + let ah: *mut dc_aheader_t = dc_aheader_new(); + let rendered: *mut libc::c_char; + let mut ah_ok: libc::c_int; + let fixed_header = b"addr=a@b.example.org; prefer-encrypt=mutual; keydata= xsBNBFzG3j0BCAC6iNhT8zydvCXi8LI/gFnkadMbfmSE/rTJskRRra/utGbLyDta/yTrJgWL7O3y/g 4HdDW/dN2z26Y6W13IMzx9gLInn1KQZChtqWAcr/ReUucXcymwcfg1mdkBGk3TSLeLihN6CJx8Wsv8 ig+kgAzte4f5rqEEAJVQ9WZHuti7UiYs6oRzqTo06CRe9owVXxzdMf0VDQtf7ZFm9dpzKKbhH7Lu88 80iiotQ9/yRCkDGp9fNThsrLdZiK6OIAcIBAqi2rI89aS1dAmnRbktQieCx5izzyYkR1KvVL3gTTll HOzfKVEC2asmtWu2e4se/+O4WMIS1eGrn7GeWVb0Vwc5ABEBAAHNETxhQEBiLmV4YW1wbGUuZGU+ws CJBBABCAAzAhkBBQJcxt5FAhsDBAsJCAcGFQgJCgsCAxYCARYhBI4xxYKBgH3ANh5cufaKrc9mtiML AAoJEPaKrc9mtiML938H/18F+3Wf9/JaAy/8hCO1v4S2PVBhxaKCokaNFtkfaMRne2l087LscCFPiF Nyb4mv6Z3YeK8Xpxlp2sI0ecvdiqLUOGfnxS6tQrj+83EjtIrZ/hXOk1h121QFWH9Zg2VNHtODXjAg dLDC0NWUrclR0ZOqEDQHeo0ibTILdokVfXFN25wakPmGaYJP2y729cb1ve7RzvIvwn+Dddfxo3ao72 rBfLi7l4NQ4S0KsY4cw+/6l5bRCKYCP77wZtvCwUvfVVosLdT43agtSiBI49+ayqvZ8OCvSJa61i+v 81brTiEy9GBod4eAp45Ibsuemkw+gon4ZOvUXHTjwFB+h63MrozOwE0EXMbePQEIAL/vauf1zK8JgC u3V+G+SOX0iWw5xUlCPX+ERpBbWfwu3uAqn4wYXD3JDE/fVAF668xiV4eTPtlSUd5h0mn+G7uXMMOt kb+20SoEt50f8zw8TrL9t+ZsV11GKZWJpCar5AhXWsn6EEi8I2hLL5vn55ZZmHuGgN4jjmkRl3ToKC LhaXwTBjCJem7N5EH7F75wErEITa55v4Lb4Nfca7vnvtYrI1OA446xa8gHra0SINelTD09/JM/Fw4s WVPBaRZmJK/Tnu79N23No9XBUubmFPv1pNexZsQclicnTpt/BEWhiun7d6lfGB63K1aoHRTR1pcrWv BuALuuz0gqar2zlI0AEQEAAcLAdgQYAQgAIAUCXMbeRQIbDBYhBI4xxYKBgH3ANh5cufaKrc9mtiML AAoJEPaKrc9mtiMLKSEIAIyLCRO2OyZ0IYRvRPpMn4p7E+7Pfcz/0mSkOy+1hshgJnqivXurm8zwGr wdMqeV4eslKR9H1RUdWGUQJNbtwmmjrt5DHpIhYHl5t3FpCBaGbV20Omo00Q38lBl9MtrmZkZw+ktE k6X+0xCKssMF+2MADkSOIufbR5HrDVB89VZOHCO9DeXvCUUAw2hyJiL/LHmLzJ40zYoTmb+F//f0k0 j+tRdbkefyRoCmwG7YGiT+2hnCdgcezswnzah5J3ZKlrg7jOGo1LxtbvNUzxNBbC6S/aNgwm6qxo7x egRhmEl5uZ16zwyj4qz+xkjGy25Of5mWfUDoNw7OT7sjUbHOOMc="; + ah_ok = dc_aheader_set_from_string(ah, fixed_header as *const u8 as *const libc::c_char); if 0 != !(ah_ok == 1i32) as libc::c_int as libc::c_long { __assert_rtn( (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) @@ -3205,21 +3206,9 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - if 0 != !((*(*ah).public_key).bytes == 10i32 - && strncmp( - (*(*ah).public_key).binary as *mut libc::c_char, - b"Delta Chat\x00" as *const u8 as *const libc::c_char, - 10, - ) == 0i32) as libc::c_int as libc::c_long - { - __assert_rtn((*::std::mem::transmute::<&[u8; 17], - &[libc::c_char; 17]>(b"stress_functions\x00")).as_ptr(), - b"../cmdline/stress.c\x00" as *const u8 as - *const libc::c_char, 771i32, - b"ah->public_key->bytes==10 && strncmp((char*)ah->public_key->binary, \"Delta Chat\", 10)==0\x00" - as *const u8 as *const libc::c_char); - } else { - }; + + assert_eq!((*(*ah).public_key).bytes, 1212); + if 0 != !((*ah).prefer_encrypt == 1i32) as libc::c_int as libc::c_long { __assert_rtn( (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) @@ -3230,22 +3219,15 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; + rendered = dc_aheader_render(ah); - if 0 != !(!rendered.is_null() - && strcmp( - rendered, - b"addr=a@b.example.org; prefer-encrypt=mutual; keydata= RGVsdGEgQ2hhdA==\x00" - as *const u8 as *const libc::c_char, - ) == 0i32) as libc::c_int as libc::c_long - { - __assert_rtn((*::std::mem::transmute::<&[u8; 17], - &[libc::c_char; 17]>(b"stress_functions\x00")).as_ptr(), - b"../cmdline/stress.c\x00" as *const u8 as - *const libc::c_char, 775i32, - b"rendered && strcmp(rendered, \"addr=a@b.example.org; prefer-encrypt=mutual; keydata= RGVsdGEgQ2hhdA==\")==0\x00" - as *const u8 as *const libc::c_char); - } else { - }; + + assert!(!rendered.is_null()); + assert_eq!( + std::ffi::CStr::from_ptr(rendered).to_str().unwrap(), + std::str::from_utf8(fixed_header).unwrap() + ); + ah_ok = dc_aheader_set_from_string(ah, b" _foo; __FOO=BAR ;;; addr = a@b.example.org ;\r\n prefer-encrypt = mutual ; keydata = RG VsdGEgQ\r\n2hhdA==\x00" @@ -3276,6 +3258,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; + if 0 != !((*(*ah).public_key).bytes == 10i32 && strncmp( (*(*ah).public_key).binary as *mut libc::c_char, @@ -3402,8 +3385,8 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { }; dc_aheader_unref(ah); free(rendered as *mut libc::c_void); - let mut ok: libc::c_int = 0; - let mut buf_0: *mut libc::c_char = 0 as *mut libc::c_char; + let mut ok: libc::c_int; + let mut buf_0: *mut libc::c_char; let mut headerline: *const libc::c_char = 0 as *const libc::c_char; let mut setupcodebegin: *const libc::c_char = 0 as *const libc::c_char; let mut preferencrypt: *const libc::c_char = 0 as *const libc::c_char; @@ -3767,7 +3750,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; free(norm as *mut libc::c_void); - let mut buf_1: *mut libc::c_char = 0 as *mut libc::c_char; + let mut buf_1: *mut libc::c_char; let mut headerline_0: *const libc::c_char = 0 as *const libc::c_char; let mut setupcodebegin_0: *const libc::c_char = 0 as *const libc::c_char; let mut preferencrypt_0: *const libc::c_char = 0 as *const libc::c_char; @@ -3906,8 +3889,8 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { }; free(buf_1 as *mut libc::c_void); if 0 != dc_is_configured(context) { - let mut setupcode: *mut libc::c_char = 0 as *mut libc::c_char; - let mut setupfile: *mut libc::c_char = 0 as *mut libc::c_char; + let setupcode: *mut libc::c_char; + let setupfile: *mut libc::c_char; setupcode = dc_create_setup_code(context); if 0 != setupcode.is_null() as libc::c_int as libc::c_long { __assert_rtn( @@ -3960,7 +3943,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - let mut buf_2: *mut libc::c_char = dc_strdup(setupfile); + let buf_2: *mut libc::c_char = dc_strdup(setupfile); let mut headerline_1: *const libc::c_char = 0 as *const libc::c_char; let mut setupcodebegin_1: *const libc::c_char = 0 as *const libc::c_char; if 0 != (0 @@ -4012,7 +3995,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; free(buf_2 as *mut libc::c_void); - let mut payload: *mut libc::c_char = 0 as *mut libc::c_char; + let payload: *mut libc::c_char; let mut headerline_2: *const libc::c_char = 0 as *const libc::c_char; payload = dc_decrypt_setup_file(context, setupcode, setupfile); if 0 != payload.is_null() as libc::c_int as libc::c_long { @@ -4063,7 +4046,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { free(setupfile as *mut libc::c_void); free(setupcode as *mut libc::c_void); } - let mut bad_key: *mut dc_key_t = dc_key_new(); + let bad_key: *mut dc_key_t = dc_key_new(); let mut bad_data: [libc::c_uchar; 4096] = [0; 4096]; let mut i_0: libc::c_int = 0i32; while i_0 < 4096i32 { @@ -4092,8 +4075,8 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { j += 1 } dc_key_unref(bad_key); - let mut public_key: *mut dc_key_t = dc_key_new(); - let mut private_key: *mut dc_key_t = dc_key_new(); + let public_key: *mut dc_key_t = dc_key_new(); + let private_key: *mut dc_key_t = dc_key_new(); dc_pgp_create_keypair( context, b"foo@bar.de\x00" as *const u8 as *const libc::c_char, @@ -4120,7 +4103,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - let mut test_key: *mut dc_key_t = dc_key_new(); + let test_key: *mut dc_key_t = dc_key_new(); if 0 != (0 == dc_pgp_split_key(context, private_key, test_key)) as libc::c_int as libc::c_long { __assert_rtn( (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) @@ -4133,8 +4116,8 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; dc_key_unref(test_key); - let mut public_key2: *mut dc_key_t = dc_key_new(); - let mut private_key2: *mut dc_key_t = dc_key_new(); + let public_key2: *mut dc_key_t = dc_key_new(); + let private_key2: *mut dc_key_t = dc_key_new(); dc_pgp_create_keypair( context, b"two@zwo.de\x00" as *const u8 as *const libc::c_char, @@ -4151,14 +4134,14 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - let mut original_text: *const libc::c_char = + let original_text: *const libc::c_char = b"This is a test\x00" as *const u8 as *const libc::c_char; let mut ctext_signed: *mut libc::c_void = 0 as *mut libc::c_void; let mut ctext_unsigned: *mut libc::c_void = 0 as *mut libc::c_void; let mut ctext_signed_bytes: size_t = 0i32 as size_t; let mut ctext_unsigned_bytes: size_t = 0; let mut plain_bytes: size_t = 0i32 as size_t; - let mut keyring: *mut dc_keyring_t = dc_keyring_new(); + let keyring: *mut dc_keyring_t = dc_keyring_new(); dc_keyring_add(keyring, public_key); dc_keyring_add(keyring, public_key2); let mut ok_0: libc::c_int = dc_pgp_pk_encrypt( @@ -4263,11 +4246,11 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; dc_keyring_unref(keyring); - let mut keyring_0: *mut dc_keyring_t = dc_keyring_new(); + let keyring_0: *mut dc_keyring_t = dc_keyring_new(); dc_keyring_add(keyring_0, private_key); - let mut public_keyring: *mut dc_keyring_t = dc_keyring_new(); + let public_keyring: *mut dc_keyring_t = dc_keyring_new(); dc_keyring_add(public_keyring, public_key); - let mut public_keyring2: *mut dc_keyring_t = dc_keyring_new(); + let public_keyring2: *mut dc_keyring_t = dc_keyring_new(); dc_keyring_add(public_keyring2, public_key2); let mut plain_0: *mut libc::c_void = 0 as *mut libc::c_void; let mut valid_signatures: dc_hash_t = dc_hash_t { @@ -4279,7 +4262,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ht: 0 as *mut _ht, }; dc_hash_init(&mut valid_signatures, 3i32, 1i32); - let mut ok_1: libc::c_int = 0; + let mut ok_1: libc::c_int; ok_1 = dc_pgp_pk_decrypt( context, ctext_signed, @@ -4519,17 +4502,16 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; free(plain_0); - plain_0 = 0 as *mut libc::c_void; dc_hash_clear(&mut valid_signatures); dc_keyring_unref(keyring_0); dc_keyring_unref(public_keyring); dc_keyring_unref(public_keyring2); - let mut keyring_1: *mut dc_keyring_t = dc_keyring_new(); + let keyring_1: *mut dc_keyring_t = dc_keyring_new(); dc_keyring_add(keyring_1, private_key2); - let mut public_keyring_0: *mut dc_keyring_t = dc_keyring_new(); + let public_keyring_0: *mut dc_keyring_t = dc_keyring_new(); dc_keyring_add(public_keyring_0, public_key); let mut plain_1: *mut libc::c_void = 0 as *mut libc::c_void; - let mut ok_2: libc::c_int = dc_pgp_pk_decrypt( + let ok_2: libc::c_int = dc_pgp_pk_decrypt( context, ctext_signed, ctext_signed_bytes, @@ -4582,7 +4564,7 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { dc_key_unref(private_key2); dc_key_unref(public_key); dc_key_unref(private_key); - let mut fingerprint: *mut libc::c_char = dc_normalize_fingerprint( + let fingerprint: *mut libc::c_char = dc_normalize_fingerprint( b" 1234 567890 \n AbcD abcdef ABCDEF \x00" as *const u8 as *const libc::c_char, ); if 0 != fingerprint.is_null() as libc::c_int as libc::c_long {