diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index b92920657..68a05a48f 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -1,4 +1,5 @@ use std::ffi::CString; +use std::sync::{Arc, RwLock}; use deltachat::constants::*; use deltachat::dc_aheader::*; @@ -60,9 +61,6 @@ pub unsafe extern "C" fn dc_reset_tables( mut context: &dc_context_t, mut bits: libc::c_int, ) -> libc::c_int { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return 0i32; - } dc_log_info( context, 0i32, @@ -71,7 +69,8 @@ pub unsafe extern "C" fn dc_reset_tables( ); if 0 != bits & 1i32 { dc_sqlite3_execute( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"DELETE FROM jobs;\x00" as *const u8 as *const libc::c_char, ); dc_log_info( @@ -82,7 +81,8 @@ pub unsafe extern "C" fn dc_reset_tables( } if 0 != bits & 2i32 { dc_sqlite3_execute( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"DELETE FROM acpeerstates;\x00" as *const u8 as *const libc::c_char, ); dc_log_info( @@ -93,7 +93,8 @@ pub unsafe extern "C" fn dc_reset_tables( } if 0 != bits & 4i32 { dc_sqlite3_execute( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"DELETE FROM keypairs;\x00" as *const u8 as *const libc::c_char, ); dc_log_info( @@ -104,28 +105,34 @@ pub unsafe extern "C" fn dc_reset_tables( } if 0 != bits & 8i32 { dc_sqlite3_execute( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"DELETE FROM contacts WHERE id>9;\x00" as *const u8 as *const libc::c_char, ); dc_sqlite3_execute( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"DELETE FROM chats WHERE id>9;\x00" as *const u8 as *const libc::c_char, ); dc_sqlite3_execute( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"DELETE FROM chats_contacts;\x00" as *const u8 as *const libc::c_char, ); dc_sqlite3_execute( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"DELETE FROM msgs WHERE id>9;\x00" as *const u8 as *const libc::c_char, ); dc_sqlite3_execute( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"DELETE FROM config WHERE keyname LIKE \'imap.%\' OR keyname LIKE \'configured%\';\x00" as *const u8 as *const libc::c_char, ); dc_sqlite3_execute( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"DELETE FROM leftgrps;\x00" as *const u8 as *const libc::c_char, ); dc_log_info( @@ -134,7 +141,7 @@ pub unsafe extern "C" fn dc_reset_tables( b"(8) Rest but server config reset.\x00" as *const u8 as *const libc::c_char, ); } - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -153,15 +160,12 @@ pub unsafe extern "C" fn dc_reset_tables( * auto-added contacts, unless they are used in a chat or for blocking purpose. */ unsafe extern "C" fn dc_cleanup_contacts(mut context: &dc_context_t) -> libc::c_int { - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return 0i32; - } dc_log_info( context, 0i32, b"Cleaning up contacts ...\x00" as *const u8 as *const libc::c_char, ); - dc_sqlite3_execute((*context).sql, + dc_sqlite3_execute(context, &mut context.sql.clone().lock().unwrap(), b"DELETE FROM contacts WHERE id>9 AND blocked=0 AND NOT EXISTS (SELECT contact_id FROM chats_contacts where contacts.id = chats_contacts.contact_id) AND NOT EXISTS (select from_id from msgs WHERE msgs.from_id = contacts.id);\x00" as *const u8 as *const libc::c_char); return 1i32; @@ -174,9 +178,6 @@ unsafe extern "C" fn dc_poke_eml_file( let mut success: libc::c_int = 0i32; let mut data: *mut libc::c_char = 0 as *mut libc::c_char; let mut data_bytes: size_t = 0; - if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { - return 0i32; - } if !(dc_read_file( context, filename, @@ -220,12 +221,16 @@ unsafe extern "C" fn poke_public_key( public_key_file, ); } else { - if 0 != dc_apeerstate_load_by_addr(peerstate, (*context).sql, addr) { + if 0 != dc_apeerstate_load_by_addr( + peerstate, + &mut context.sql.clone().lock().unwrap(), + addr, + ) { dc_apeerstate_apply_header(peerstate, header, time(0 as *mut time_t)); - dc_apeerstate_save_to_db(peerstate, (*context).sql, 0i32); + dc_apeerstate_save_to_db(peerstate, &mut context.sql.clone().lock().unwrap(), 0i32); } else { dc_apeerstate_init_from_header(peerstate, header, time(0 as *mut time_t)); - dc_apeerstate_save_to_db(peerstate, (*context).sql, 1i32); + dc_apeerstate_save_to_db(peerstate, &mut context.sql.clone().lock().unwrap(), 1i32); } success = 1i32 } @@ -256,10 +261,7 @@ unsafe extern "C" fn poke_spec( let mut dir_entry: *mut dirent = 0 as *mut dirent; let mut read_cnt: libc::c_int = 0i32; let mut name: *mut libc::c_char = 0 as *mut libc::c_char; - if context.is_null() { - return 0i32; - } - if 0 == dc_sqlite3_is_open((*context).sql) { + if 0 == dc_sqlite3_is_open(&mut context.sql.clone().lock().unwrap()) { dc_log_error( context, 0i32, @@ -270,14 +272,16 @@ unsafe extern "C" fn poke_spec( if !spec.is_null() { real_spec = dc_strdup(spec); dc_sqlite3_set_config( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"import_spec\x00" as *const u8 as *const libc::c_char, real_spec, ); current_block = 7149356873433890176; } else { real_spec = dc_sqlite3_get_config( - (*context).sql, + context, + &mut context.sql.clone().lock().unwrap(), b"import_spec\x00" as *const u8 as *const libc::c_char, 0 as *const libc::c_char, ); @@ -382,7 +386,7 @@ unsafe extern "C" fn poke_spec( real_spec, ); if read_cnt > 0i32 { - ((*context).cb)( + (context.cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -539,8 +543,11 @@ unsafe extern "C" fn log_contactlist(mut context: &dc_context_t, mut contacts: * b"addr unset\x00" as *const u8 as *const libc::c_char }, ); - let mut peerstate_ok: libc::c_int = - dc_apeerstate_load_by_addr(peerstate, (*context).sql, addr); + let mut peerstate_ok: libc::c_int = dc_apeerstate_load_by_addr( + peerstate, + &mut context.sql.clone().lock().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; match (*peerstate).prefer_encrypt { @@ -597,451 +604,538 @@ unsafe extern "C" fn chat_prefix(mut chat: *const dc_chat_t) -> *const libc::c_c }; } #[no_mangle] -pub unsafe extern "C" fn dc_cmdline(context: &dc_context_t, cmdline: &str) -> *mut libc::c_char { - let mut cmd: &libc::c_char = 0 as *mut libc::c_char; +pub unsafe extern "C" fn dc_cmdline( + ctx: Arc>, + 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 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; - if !context.is_null() { - if 0 != (*context).cmdline_sel_chat_id { - sel_chat = dc_get_chat(context, (*context).cmdline_sel_chat_id) - } - cmd = dc_strdup(CString::new(cmdline).unwrap().as_ptr()); - arg1 = strchr(cmd, ' ' as i32); - if !arg1.is_null() { - *arg1 = 0i32 as libc::c_char; - arg1 = arg1.offset(1isize) - } - if strcmp(cmd, b"help\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(cmd, b"?\x00" as *const u8 as *const libc::c_char) == 0i32 + + let ctx_a = ctx.clone(); + let context = ctx_a.read().unwrap(); + if 0 != context.cmdline_sel_chat_id { + sel_chat = dc_get_chat(&context, context.cmdline_sel_chat_id) + } + cmd = dc_strdup(CString::new(cmdline).unwrap().as_ptr()); + arg1 = strchr(cmd, ' ' as i32); + if !arg1.is_null() { + *arg1 = 0i32 as libc::c_char; + arg1 = arg1.offset(1isize) + } + if strcmp(cmd, b"help\x00" as *const u8 as *const libc::c_char) == 0i32 + || strcmp(cmd, b"?\x00" as *const u8 as *const libc::c_char) == 0i32 + { + if !arg1.is_null() && strcmp(arg1, b"imex\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() - && strcmp(arg1, b"imex\x00" as *const u8 as *const libc::c_char) == 0i32 - { - ret = + ret = dc_strdup(b"====================Import/Export commands==\ninitiate-key-transfer\nget-setupcodebegin \ncontinue-key-transfer \nhas-backup\nexport-backup\nimport-backup \nexport-keys\nimport-keys\nexport-setup\npoke [|| ]\nreset \nstop\n=============================================\x00" as *const u8 as *const libc::c_char) - } else { - ret = + } else { + ret = dc_strdup(b"==========================Database commands==\ninfo\nopen \nclose\nset []\nget \noauth2\nconfigure\nconnect\ndisconnect\nmaybenetwork\nhousekeeping\nhelp imex (Import/Export)\n==============================Chat commands==\nlistchats []\nlistarchived\nchat [|0]\ncreatechat \ncreatechatbymsg \ncreategroup \ncreateverified \naddmember \nremovemember \ngroupname \ngroupimage []\nchatinfo\nsendlocations \nsetlocation \ndellocations\ngetlocations []\nsend \nsendimage []\nsendfile \ndraft []\nlistmedia\narchive \nunarchive \ndelchat \n===========================Message commands==\nlistmsgs \nmsginfo \nlistfresh\nforward \nmarkseen \nstar \nunstar \ndelmsg \n===========================Contact commands==\nlistcontacts []\nlistverified []\naddcontact [] \ncontactinfo \ndelcontact \ncleanupcontacts\n======================================Misc.==\ngetqr []\ngetbadqr\ncheckqr \nevent \nfileinfo \nclear -- clear screen\nexit\n=============================================\x00" as *const u8 as *const libc::c_char) - } - } else if 0 == s_is_auth { - if strcmp(cmd, b"auth\x00" as *const u8 as *const libc::c_char) == 0i32 { - let mut is_pw: *mut libc::c_char = - dc_get_config(context, b"mail_pw\x00" as *const u8 as *const libc::c_char); - if strcmp(arg1, is_pw) == 0i32 { - s_is_auth = 1i32; - ret = 2i32 as *mut libc::c_char - } else { - ret = b"Bad password.\x00" as *const u8 as *const libc::c_char - as *mut libc::c_char - } - } else { - ret = dc_strdup( - b"Please authorize yourself using: auth \x00" as *const u8 - as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"auth\x00" as *const u8 as *const libc::c_char) == 0i32 { - ret = dc_strdup(b"Already authorized.\x00" as *const u8 as *const libc::c_char) - } else if strcmp(cmd, b"open\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - dc_close(context); - ret = if 0 != dc_open(context, arg1, 0 as *const libc::c_char) { - 2i32 as *mut libc::c_char - } else { - 1i32 as *mut libc::c_char - } - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"close\x00" as *const u8 as *const libc::c_char) == 0i32 { - dc_close(context); - ret = 2i32 as *mut libc::c_char - } else if strcmp( - cmd, - b"initiate-key-transfer\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { - let mut setup_code: *mut libc::c_char = dc_initiate_key_transfer(context); - ret = if !setup_code.is_null() { - dc_mprintf( - b"Setup code for the transferred setup message: %s\x00" as *const u8 - as *const libc::c_char, - setup_code, - ) - } else { - 1i32 as *mut libc::c_char - }; - free(setup_code as *mut libc::c_void); - } else if strcmp( - cmd, - b"get-setupcodebegin\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { - if !arg1.is_null() { - let mut msg_id: uint32_t = atoi(arg1) as uint32_t; - let mut msg: *mut dc_msg_t = dc_get_msg(context, msg_id); - if 0 != dc_msg_is_setupmessage(msg) { - let mut setupcodebegin: *mut libc::c_char = dc_msg_get_setupcodebegin(msg); - ret = dc_mprintf( - b"The setup code for setup message Msg#%i starts with: %s\x00" as *const u8 - as *const libc::c_char, - msg_id, - setupcodebegin, - ); - free(setupcodebegin as *mut libc::c_void); - } else { - ret = dc_mprintf( - b"ERROR: Msg#%i is no setup message.\x00" as *const u8 - as *const libc::c_char, - msg_id, - ) - } - dc_msg_unref(msg); - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp( - cmd, - b"continue-key-transfer\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { - let mut arg2: *mut libc::c_char = 0 as *mut libc::c_char; - if !arg1.is_null() { - arg2 = strrchr(arg1, ' ' as i32) - } - if !arg1.is_null() && !arg2.is_null() { - *arg2 = 0i32 as libc::c_char; - arg2 = arg2.offset(1isize); - ret = if 0 != dc_continue_key_transfer(context, atoi(arg1) as uint32_t, arg2) { - 2i32 as *mut libc::c_char - } else { - 1i32 as *mut libc::c_char - } - } else { - ret = dc_strdup( - b"ERROR: Arguments expected.\x00" as *const u8 - as *const libc::c_char, - ) - } - } 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); - if ret.is_null() { - ret = dc_strdup(b"No backup found.\x00" as *const u8 as *const libc::c_char) - } - } else if strcmp( - cmd, - 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); - ret = 2i32 as *mut libc::c_char - } else if strcmp( - cmd, - b"import-backup\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { - if !arg1.is_null() { - dc_imex(context, 12i32, arg1, 0 as *const libc::c_char); + } + } else if 0 == s_is_auth { + if strcmp(cmd, b"auth\x00" as *const u8 as *const libc::c_char) == 0i32 { + let mut is_pw: *mut libc::c_char = + dc_get_config(&context, b"mail_pw\x00" as *const u8 as *const libc::c_char); + if strcmp(arg1, is_pw) == 0i32 { + s_is_auth = 1i32; ret = 2i32 as *mut libc::c_char } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 - as *const libc::c_char, - ) + ret = b"Bad password.\x00" as *const u8 as *const libc::c_char as *mut libc::c_char } - } 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); - 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); - 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, - ); - let mut file_content: *mut libc::c_char = 0 as *mut libc::c_char; - file_content = dc_render_setup_file(context, setup_code_0); - if !file_content.is_null() - && 0 != dc_write_file( - context, - file_name, - file_content as *const libc::c_void, - strlen(file_content), - ) - { - ret = dc_mprintf( - b"Setup message written to: %s\nSetup code: %s\x00" as *const u8 - as *const libc::c_char, - file_name, - setup_code_0, - ) - } else { - ret = 1i32 as *mut libc::c_char - } - free(file_content as *mut libc::c_void); - free(file_name as *mut libc::c_void); - free(setup_code_0 as *mut libc::c_void); - } else if strcmp(cmd, b"poke\x00" as *const u8 as *const libc::c_char) == 0i32 { - ret = if 0 != poke_spec(context, arg1) { + } else { + ret = dc_strdup( + b"Please authorize yourself using: auth \x00" as *const u8 + as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"auth\x00" as *const u8 as *const libc::c_char) == 0i32 { + ret = dc_strdup(b"Already authorized.\x00" as *const u8 as *const libc::c_char) + } else if strcmp(cmd, b"open\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let ctx_a = ctx.clone(); + let mut context = ctx_a.write().unwrap(); + dc_close(&mut context); + ret = if 0 != dc_open(&mut context, arg1, 0 as *const libc::c_char) { 2i32 as *mut libc::c_char } else { 1i32 as *mut libc::c_char } - } else if strcmp(cmd, b"reset\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut bits: libc::c_int = atoi(arg1); - if bits > 15i32 { - ret = dc_strdup( - b"ERROR: must be lower than 16.\x00" as *const u8 - as *const libc::c_char, - ) - } else { - ret = if 0 != dc_reset_tables(context, bits) { - 2i32 as *mut libc::c_char - } else { - 1i32 as *mut libc::c_char - } - } + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"close\x00" as *const u8 as *const libc::c_char) == 0i32 { + let ctx_a = ctx.clone(); + let mut context = ctx_a.write().unwrap(); + dc_close(&mut context); + ret = 2i32 as *mut libc::c_char + } else if strcmp( + cmd, + b"initiate-key-transfer\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + let mut setup_code: *mut libc::c_char = dc_initiate_key_transfer(&context); + ret = if !setup_code.is_null() { + dc_mprintf( + b"Setup code for the transferred setup message: %s\x00" as *const u8 + as *const libc::c_char, + setup_code, + ) + } else { + 1i32 as *mut libc::c_char + }; + free(setup_code as *mut libc::c_void); + } else if strcmp( + cmd, + b"get-setupcodebegin\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + if !arg1.is_null() { + let mut msg_id: uint32_t = atoi(arg1) as uint32_t; + let mut msg: *mut dc_msg_t = dc_get_msg(&context, msg_id); + if 0 != dc_msg_is_setupmessage(msg) { + let mut setupcodebegin: *mut libc::c_char = dc_msg_get_setupcodebegin(msg); + ret = dc_mprintf( + b"The setup code for setup message Msg#%i starts with: %s\x00" as *const u8 + as *const libc::c_char, + msg_id, + setupcodebegin, + ); + free(setupcodebegin as *mut libc::c_void); } else { - ret = - dc_strdup(b"ERROR: Argument missing: 1=jobs, 2=peerstates, 4=private keys, 8=rest but server config\x00" - as *const u8 as *const libc::c_char) + ret = dc_mprintf( + b"ERROR: Msg#%i is no setup message.\x00" as *const u8 as *const libc::c_char, + msg_id, + ) } - } else if strcmp(cmd, b"stop\x00" as *const u8 as *const libc::c_char) == 0i32 { - dc_stop_ongoing_process(context); + dc_msg_unref(msg); + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp( + cmd, + b"continue-key-transfer\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + let mut arg2: *mut libc::c_char = 0 as *mut libc::c_char; + if !arg1.is_null() { + arg2 = strrchr(arg1, ' ' as i32) + } + if !arg1.is_null() && !arg2.is_null() { + *arg2 = 0i32 as libc::c_char; + arg2 = arg2.offset(1isize); + ret = if 0 != dc_continue_key_transfer(&context, atoi(arg1) as uint32_t, arg2) { + 2i32 as *mut libc::c_char + } else { + 1i32 as *mut libc::c_char + } + } else { + ret = dc_strdup( + b"ERROR: Arguments expected.\x00" as *const u8 + as *const libc::c_char, + ) + } + } 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); + if ret.is_null() { + ret = dc_strdup(b"No backup found.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp( + cmd, + 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); + ret = 2i32 as *mut libc::c_char + } else if strcmp( + cmd, + b"import-backup\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + if !arg1.is_null() { + dc_imex(&context, 12i32, arg1, 0 as *const libc::c_char); ret = 2i32 as *mut libc::c_char - } else if strcmp(cmd, b"set\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut arg2_0: *mut libc::c_char = strchr(arg1, ' ' as i32); - if !arg2_0.is_null() { - *arg2_0 = 0i32 as libc::c_char; - arg2_0 = arg2_0.offset(1isize) - } - ret = if 0 != dc_set_config(context, arg1, arg2_0) { + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } 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); + 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); + 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, + ); + let mut file_content: *mut libc::c_char = 0 as *mut libc::c_char; + file_content = dc_render_setup_file(&context, setup_code_0); + if !file_content.is_null() + && 0 != dc_write_file( + &context, + file_name, + file_content as *const libc::c_void, + strlen(file_content), + ) + { + ret = dc_mprintf( + b"Setup message written to: %s\nSetup code: %s\x00" as *const u8 + as *const libc::c_char, + file_name, + setup_code_0, + ) + } else { + ret = 1i32 as *mut libc::c_char + } + free(file_content as *mut libc::c_void); + free(file_name as *mut libc::c_void); + free(setup_code_0 as *mut libc::c_void); + } else if strcmp(cmd, b"poke\x00" as *const u8 as *const libc::c_char) == 0i32 { + ret = if 0 != poke_spec(&context, arg1) { + 2i32 as *mut libc::c_char + } else { + 1i32 as *mut libc::c_char + } + } else if strcmp(cmd, b"reset\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut bits: libc::c_int = atoi(arg1); + if bits > 15i32 { + ret = dc_strdup( + b"ERROR: must be lower than 16.\x00" as *const u8 as *const libc::c_char, + ) + } else { + ret = if 0 != dc_reset_tables(&context, bits) { 2i32 as *mut libc::c_char } else { 1i32 as *mut libc::c_char } + } + } else { + ret = + dc_strdup(b"ERROR: Argument missing: 1=jobs, 2=peerstates, 4=private keys, 8=rest but server config\x00" + as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"stop\x00" as *const u8 as *const libc::c_char) == 0i32 { + dc_stop_ongoing_process(&context); + ret = 2i32 as *mut libc::c_char + } else if strcmp(cmd, b"set\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut arg2_0: *mut libc::c_char = strchr(arg1, ' ' as i32); + if !arg2_0.is_null() { + *arg2_0 = 0i32 as libc::c_char; + arg2_0 = arg2_0.offset(1isize) + } + ret = if 0 != dc_set_config(&context, arg1, arg2_0) { + 2i32 as *mut libc::c_char } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) + 1i32 as *mut libc::c_char } - } else if strcmp(cmd, b"get\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut val: *mut libc::c_char = dc_get_config(context, arg1); - ret = dc_mprintf(b"%s=%s\x00" as *const u8 as *const libc::c_char, arg1, val); - free(val as *mut libc::c_void); + } else { + ret = + dc_strdup(b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"get\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut val: *mut libc::c_char = dc_get_config(&context, arg1); + ret = dc_mprintf(b"%s=%s\x00" as *const u8 as *const libc::c_char, arg1, val); + free(val as *mut libc::c_void); + } else { + ret = + dc_strdup(b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"info\x00" as *const u8 as *const libc::c_char) == 0i32 { + ret = dc_get_info(&context); + if ret.is_null() { + ret = 1i32 as *mut libc::c_char + } + } else if strcmp(cmd, b"maybenetwork\x00" as *const u8 as *const libc::c_char) == 0i32 { + dc_maybe_network(&context); + ret = 2i32 as *mut libc::c_char + } else if strcmp(cmd, b"housekeeping\x00" as *const u8 as *const libc::c_char) == 0i32 { + dc_housekeeping(&context); + ret = 2i32 as *mut libc::c_char + } else if strcmp(cmd, b"listchats\x00" as *const u8 as *const libc::c_char) == 0i32 + || strcmp(cmd, b"listarchived\x00" as *const u8 as *const libc::c_char) == 0i32 + || strcmp(cmd, b"chats\x00" as *const u8 as *const libc::c_char) == 0i32 + { + let mut listflags: libc::c_int = + if strcmp(cmd, b"listarchived\x00" as *const u8 as *const libc::c_char) == 0i32 { + 0x1i32 } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"info\x00" as *const u8 as *const libc::c_char) == 0i32 { - ret = dc_get_info(context); - if ret.is_null() { - ret = 1i32 as *mut libc::c_char - } - } else if strcmp(cmd, b"maybenetwork\x00" as *const u8 as *const libc::c_char) == 0i32 { - dc_maybe_network(context); - ret = 2i32 as *mut libc::c_char - } else if strcmp(cmd, b"housekeeping\x00" as *const u8 as *const libc::c_char) == 0i32 { - dc_housekeeping(context); - ret = 2i32 as *mut libc::c_char - } else if strcmp(cmd, b"listchats\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(cmd, b"listarchived\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(cmd, b"chats\x00" as *const u8 as *const libc::c_char) == 0i32 - { - let mut listflags: libc::c_int = - if strcmp(cmd, b"listarchived\x00" as *const u8 as *const libc::c_char) == 0i32 { - 0x1i32 - } else { - 0i32 - }; - 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 cnt: libc::c_int = dc_chatlist_get_cnt(chatlist) as libc::c_int; - if cnt > 0i32 { - dc_log_info(context, 0i32, + 0i32 + }; + 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 cnt: libc::c_int = dc_chatlist_get_cnt(chatlist) as libc::c_int; + if cnt > 0i32 { + dc_log_info(&context, 0i32, b"================================================================================\x00" as *const u8 as *const libc::c_char); - i = cnt - 1i32; - while i >= 0i32 { - let mut chat: *mut dc_chat_t = - dc_get_chat(context, dc_chatlist_get_chat_id(chatlist, i as size_t)); - let mut temp_subtitle: *mut libc::c_char = dc_chat_get_subtitle(chat); - let mut temp_name: *mut libc::c_char = dc_chat_get_name(chat); - dc_log_info( - context, - 0i32, - b"%s#%i: %s [%s] [%i fresh]\x00" as *const u8 as *const libc::c_char, - chat_prefix(chat), - dc_chat_get_id(chat) as libc::c_int, - temp_name, - temp_subtitle, - dc_get_fresh_msg_cnt(context, dc_chat_get_id(chat)) as libc::c_int, - ); - free(temp_subtitle as *mut libc::c_void); - free(temp_name as *mut libc::c_void); - let mut lot: *mut dc_lot_t = - dc_chatlist_get_summary(chatlist, i as size_t, chat); - let mut statestr: *const libc::c_char = - b"\x00" as *const u8 as *const libc::c_char; - if 0 != dc_chat_get_archived(chat) { - statestr = b" [Archived]\x00" as *const u8 as *const libc::c_char - } else { - match dc_lot_get_state(lot) { - 20 => statestr = b" o\x00" as *const u8 as *const libc::c_char, - 26 => { - statestr = - b" \xe2\x88\x9a\x00" as *const u8 as *const libc::c_char - } - 28 => { - statestr = b" \xe2\x88\x9a\xe2\x88\x9a\x00" as *const u8 - as *const libc::c_char - } - 24 => statestr = b" !!\x00" as *const u8 as *const libc::c_char, - _ => {} + i = cnt - 1i32; + while i >= 0i32 { + let mut chat: *mut dc_chat_t = + dc_get_chat(&context, dc_chatlist_get_chat_id(chatlist, i as size_t)); + let mut temp_subtitle: *mut libc::c_char = dc_chat_get_subtitle(chat); + let mut temp_name: *mut libc::c_char = dc_chat_get_name(chat); + dc_log_info( + &context, + 0i32, + b"%s#%i: %s [%s] [%i fresh]\x00" as *const u8 as *const libc::c_char, + chat_prefix(chat), + dc_chat_get_id(chat) as libc::c_int, + temp_name, + temp_subtitle, + dc_get_fresh_msg_cnt(&context, dc_chat_get_id(chat)) as libc::c_int, + ); + free(temp_subtitle as *mut libc::c_void); + free(temp_name as *mut libc::c_void); + let mut lot: *mut dc_lot_t = + dc_chatlist_get_summary(chatlist, i as size_t, chat); + let mut statestr: *const libc::c_char = + b"\x00" as *const u8 as *const libc::c_char; + if 0 != dc_chat_get_archived(chat) { + statestr = b" [Archived]\x00" as *const u8 as *const libc::c_char + } else { + match dc_lot_get_state(lot) { + 20 => statestr = b" o\x00" as *const u8 as *const libc::c_char, + 26 => { + statestr = b" \xe2\x88\x9a\x00" as *const u8 as *const libc::c_char } + 28 => { + statestr = b" \xe2\x88\x9a\xe2\x88\x9a\x00" as *const u8 + as *const libc::c_char + } + 24 => statestr = b" !!\x00" as *const u8 as *const libc::c_char, + _ => {} } - let mut timestr: *mut libc::c_char = - dc_timestamp_to_str(dc_lot_get_timestamp(lot)); - let mut text1: *mut libc::c_char = dc_lot_get_text1(lot); - let mut text2: *mut libc::c_char = dc_lot_get_text2(lot); - dc_log_info( - context, - 0i32, - b"%s%s%s%s [%s]%s\x00" as *const u8 as *const libc::c_char, - if !text1.is_null() { - text1 - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - if !text1.is_null() { - b": \x00" as *const u8 as *const libc::c_char - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - if !text2.is_null() { - text2 - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - statestr, - timestr, - if 0 != dc_chat_is_sending_locations(chat) { - b"\xf0\x9f\x93\x8d\x00" as *const u8 as *const libc::c_char - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - ); - free(text1 as *mut libc::c_void); - free(text2 as *mut libc::c_void); - free(timestr as *mut libc::c_void); - dc_lot_unref(lot); - dc_chat_unref(chat); - dc_log_info(context, 0i32, + } + let mut timestr: *mut libc::c_char = + dc_timestamp_to_str(dc_lot_get_timestamp(lot)); + let mut text1: *mut libc::c_char = dc_lot_get_text1(lot); + let mut text2: *mut libc::c_char = dc_lot_get_text2(lot); + dc_log_info( + &context, + 0i32, + b"%s%s%s%s [%s]%s\x00" as *const u8 as *const libc::c_char, + if !text1.is_null() { + text1 + } else { + b"\x00" as *const u8 as *const libc::c_char + }, + if !text1.is_null() { + b": \x00" as *const u8 as *const libc::c_char + } else { + b"\x00" as *const u8 as *const libc::c_char + }, + if !text2.is_null() { + text2 + } else { + b"\x00" as *const u8 as *const libc::c_char + }, + statestr, + timestr, + if 0 != dc_chat_is_sending_locations(chat) { + b"\xf0\x9f\x93\x8d\x00" as *const u8 as *const libc::c_char + } else { + b"\x00" as *const u8 as *const libc::c_char + }, + ); + free(text1 as *mut libc::c_void); + free(text2 as *mut libc::c_void); + free(timestr as *mut libc::c_void); + dc_lot_unref(lot); + dc_chat_unref(chat); + dc_log_info(&context, 0i32, b"================================================================================\x00" as *const u8 as *const libc::c_char); - i -= 1 - } + i -= 1 } - if 0 != dc_is_sending_locations_to_chat(context, 0i32 as uint32_t) { - dc_log_info( - context, - 0i32, - b"Location streaming enabled.\x00" as *const u8 as *const libc::c_char, - ); - } - ret = dc_mprintf( - b"%i chats.\x00" as *const u8 as *const libc::c_char, - cnt as libc::c_int, + } + if 0 != dc_is_sending_locations_to_chat(&context, 0i32 as uint32_t) { + dc_log_info( + &context, + 0i32, + b"Location streaming enabled.\x00" as *const u8 as *const libc::c_char, ); - dc_chatlist_unref(chatlist); + } + ret = dc_mprintf( + b"%i chats.\x00" as *const u8 as *const libc::c_char, + cnt as libc::c_int, + ); + dc_chatlist_unref(chatlist); + } else { + ret = 1i32 as *mut libc::c_char + } + } else if strcmp(cmd, b"chat\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { + if !sel_chat.is_null() { + dc_chat_unref(sel_chat); + sel_chat = 0 as *mut dc_chat_t + } + ctx.clone().write().unwrap().cmdline_sel_chat_id = atoi(arg1) as uint32_t; + sel_chat = dc_get_chat(&context, context.cmdline_sel_chat_id); + if sel_chat.is_null() { + ctx.clone().write().unwrap().cmdline_sel_chat_id = 0i32 as uint32_t + } + } + if !sel_chat.is_null() { + let mut msglist: *mut dc_array_t = dc_get_chat_msgs( + &context, + dc_chat_get_id(sel_chat), + 0x1i32 as uint32_t, + 0i32 as uint32_t, + ); + let mut temp2: *mut libc::c_char = dc_chat_get_subtitle(sel_chat); + let mut temp_name_0: *mut libc::c_char = dc_chat_get_name(sel_chat); + dc_log_info( + &context, + 0i32, + b"%s#%i: %s [%s]%s\x00" as *const u8 as *const libc::c_char, + chat_prefix(sel_chat), + dc_chat_get_id(sel_chat), + temp_name_0, + temp2, + if 0 != dc_chat_is_sending_locations(sel_chat) { + b"\xf0\x9f\x93\x8d\x00" as *const u8 as *const libc::c_char + } else { + b"\x00" as *const u8 as *const libc::c_char + }, + ); + free(temp_name_0 as *mut libc::c_void); + free(temp2 as *mut libc::c_void); + if !msglist.is_null() { + log_msglist(&context, msglist); + dc_array_unref(msglist); + } + let mut draft: *mut dc_msg_t = dc_get_draft(&context, dc_chat_get_id(sel_chat)); + if !draft.is_null() { + log_msg( + &context, + b"Draft\x00" as *const u8 as *const libc::c_char, + draft, + ); + dc_msg_unref(draft); + } + ret = dc_mprintf( + b"%i messages.\x00" as *const u8 as *const libc::c_char, + dc_get_msg_cnt(&context, dc_chat_get_id(sel_chat)), + ); + dc_marknoticed_chat(&context, dc_chat_get_id(sel_chat)); + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"createchat\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut contact_id: libc::c_int = atoi(arg1); + let mut chat_id: libc::c_int = + dc_create_chat_by_contact_id(&context, contact_id as uint32_t) as libc::c_int; + ret = if chat_id != 0i32 { + dc_mprintf( + b"Single#%lu created successfully.\x00" as *const u8 as *const libc::c_char, + chat_id, + ) + } else { + 1i32 as *mut libc::c_char + } + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp( + cmd, + b"createchatbymsg\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + if !arg1.is_null() { + let mut msg_id_0: libc::c_int = atoi(arg1); + let mut chat_id_0: libc::c_int = + dc_create_chat_by_msg_id(&context, msg_id_0 as uint32_t) as libc::c_int; + if chat_id_0 != 0i32 { + let mut chat_0: *mut dc_chat_t = dc_get_chat(&context, chat_id_0 as uint32_t); + ret = dc_mprintf( + b"%s#%lu created successfully.\x00" as *const u8 as *const libc::c_char, + chat_prefix(chat_0), + chat_id_0, + ); + dc_chat_unref(chat_0); } else { ret = 1i32 as *mut libc::c_char } - } else if strcmp(cmd, b"chat\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { - if !sel_chat.is_null() { - dc_chat_unref(sel_chat); - sel_chat = 0 as *mut dc_chat_t - } - (*context).cmdline_sel_chat_id = atoi(arg1) as uint32_t; - sel_chat = dc_get_chat(context, (*context).cmdline_sel_chat_id); - if sel_chat.is_null() { - (*context).cmdline_sel_chat_id = 0i32 as uint32_t - } - } - if !sel_chat.is_null() { - let mut msglist: *mut dc_array_t = dc_get_chat_msgs( - context, - dc_chat_get_id(sel_chat), - 0x1i32 as uint32_t, - 0i32 as uint32_t, - ); - let mut temp2: *mut libc::c_char = dc_chat_get_subtitle(sel_chat); - let mut temp_name_0: *mut libc::c_char = dc_chat_get_name(sel_chat); - dc_log_info( - context, - 0i32, - b"%s#%i: %s [%s]%s\x00" as *const u8 as *const libc::c_char, - chat_prefix(sel_chat), - dc_chat_get_id(sel_chat), - temp_name_0, - temp2, - if 0 != dc_chat_is_sending_locations(sel_chat) { - b"\xf0\x9f\x93\x8d\x00" as *const u8 as *const libc::c_char - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - ); - free(temp_name_0 as *mut libc::c_void); - free(temp2 as *mut libc::c_void); - if !msglist.is_null() { - log_msglist(context, msglist); - dc_array_unref(msglist); - } - let mut draft: *mut dc_msg_t = dc_get_draft(context, dc_chat_get_id(sel_chat)); - if !draft.is_null() { - log_msg( - context, - b"Draft\x00" as *const u8 as *const libc::c_char, - draft, - ); - dc_msg_unref(draft); - } - ret = dc_mprintf( - b"%i messages.\x00" as *const u8 as *const libc::c_char, - dc_get_msg_cnt(context, dc_chat_get_id(sel_chat)), - ); - dc_marknoticed_chat(context, dc_chat_get_id(sel_chat)); + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"creategroup\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut chat_id_1: libc::c_int = + dc_create_group_chat(&context, 0i32, arg1) as libc::c_int; + ret = if chat_id_1 != 0i32 { + dc_mprintf( + b"Group#%lu created successfully.\x00" as *const u8 as *const libc::c_char, + chat_id_1, + ) } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + 1i32 as *mut libc::c_char } - } else if strcmp(cmd, b"createchat\x00" as *const u8 as *const libc::c_char) == 0i32 { + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp( + cmd, + b"createverified\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + if !arg1.is_null() { + let mut chat_id_2: libc::c_int = + dc_create_group_chat(&context, 1i32, arg1) as libc::c_int; + ret = if chat_id_2 != 0i32 { + dc_mprintf( + b"VerifiedGroup#%lu created successfully.\x00" as *const u8 + as *const libc::c_char, + chat_id_2, + ) + } else { + 1i32 as *mut libc::c_char + } + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"addmember\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !sel_chat.is_null() { if !arg1.is_null() { - let mut contact_id: libc::c_int = atoi(arg1); - let mut chat_id: libc::c_int = - dc_create_chat_by_contact_id(context, contact_id as uint32_t) as libc::c_int; - ret = if chat_id != 0i32 { - dc_mprintf( - b"Single#%lu created successfully.\x00" as *const u8 as *const libc::c_char, - chat_id, - ) + let mut contact_id_0: libc::c_int = atoi(arg1); + if 0 != dc_add_contact_to_chat( + &context, + dc_chat_get_id(sel_chat), + contact_id_0 as uint32_t, + ) { + ret = + dc_strdup(b"Contact added to chat.\x00" as *const u8 as *const libc::c_char) } else { - 1i32 as *mut libc::c_char + ret = dc_strdup( + b"ERROR: Cannot add contact to chat.\x00" as *const u8 + as *const libc::c_char, + ) } } else { ret = dc_strdup( @@ -1049,746 +1143,640 @@ pub unsafe extern "C" fn dc_cmdline(context: &dc_context_t, cmdline: &str) -> *m as *const libc::c_char, ) } - } else if strcmp( - cmd, - b"createchatbymsg\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"removemember\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !sel_chat.is_null() { if !arg1.is_null() { - let mut msg_id_0: libc::c_int = atoi(arg1); - let mut chat_id_0: libc::c_int = - dc_create_chat_by_msg_id(context, msg_id_0 as uint32_t) as libc::c_int; - if chat_id_0 != 0i32 { - let mut chat_0: *mut dc_chat_t = dc_get_chat(context, chat_id_0 as uint32_t); - ret = dc_mprintf( - b"%s#%lu created successfully.\x00" as *const u8 as *const libc::c_char, - chat_prefix(chat_0), - chat_id_0, - ); - dc_chat_unref(chat_0); - } else { - ret = 1i32 as *mut libc::c_char - } - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"creategroup\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut chat_id_1: libc::c_int = - dc_create_group_chat(context, 0i32, arg1) as libc::c_int; - ret = if chat_id_1 != 0i32 { - dc_mprintf( - b"Group#%lu created successfully.\x00" as *const u8 as *const libc::c_char, - chat_id_1, - ) - } else { - 1i32 as *mut libc::c_char - } - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp( - cmd, - b"createverified\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { - if !arg1.is_null() { - let mut chat_id_2: libc::c_int = - dc_create_group_chat(context, 1i32, arg1) as libc::c_int; - ret = if chat_id_2 != 0i32 { - dc_mprintf( - b"VerifiedGroup#%lu created successfully.\x00" as *const u8 - as *const libc::c_char, - chat_id_2, - ) - } else { - 1i32 as *mut libc::c_char - } - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"addmember\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !sel_chat.is_null() { - if !arg1.is_null() { - let mut contact_id_0: libc::c_int = atoi(arg1); - if 0 != dc_add_contact_to_chat( - context, - dc_chat_get_id(sel_chat), - contact_id_0 as uint32_t, - ) { - ret = dc_strdup( - b"Contact added to chat.\x00" as *const u8 as *const libc::c_char, - ) - } else { - ret = dc_strdup( - b"ERROR: Cannot add contact to chat.\x00" as *const u8 - as *const libc::c_char, - ) - } + let mut contact_id_1: libc::c_int = atoi(arg1); + if 0 != dc_remove_contact_from_chat( + &context, + dc_chat_get_id(sel_chat), + contact_id_1 as uint32_t, + ) { + ret = + dc_strdup(b"Contact added to chat.\x00" as *const u8 as *const libc::c_char) } else { ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 + b"ERROR: Cannot remove member from chat.\x00" as *const u8 as *const libc::c_char, ) } } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 + as *const libc::c_char, + ) } - } else if strcmp(cmd, b"removemember\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !sel_chat.is_null() { - if !arg1.is_null() { - let mut contact_id_1: libc::c_int = atoi(arg1); - if 0 != dc_remove_contact_from_chat( - context, - dc_chat_get_id(sel_chat), - contact_id_1 as uint32_t, - ) { - ret = dc_strdup( - b"Contact added to chat.\x00" as *const u8 as *const libc::c_char, - ) - } else { - ret = dc_strdup( - b"ERROR: Cannot remove member from chat.\x00" as *const u8 - as *const libc::c_char, - ) - } - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 - as *const libc::c_char, - ) - } - } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) - } - } else if strcmp(cmd, b"groupname\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !sel_chat.is_null() { - if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { - ret = if 0 != dc_set_chat_name(context, dc_chat_get_id(sel_chat), arg1) { - 2i32 as *mut libc::c_char - } else { - 1i32 as *mut libc::c_char - } - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) - } - } else if strcmp(cmd, b"groupimage\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !sel_chat.is_null() { - ret = if 0 - != dc_set_chat_profile_image( - context, - dc_chat_get_id(sel_chat), - if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { - arg1 - } else { - 0 as *mut libc::c_char - }, - ) { + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"groupname\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !sel_chat.is_null() { + if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { + ret = if 0 != dc_set_chat_name(&context, dc_chat_get_id(sel_chat), arg1) { 2i32 as *mut libc::c_char } else { 1i32 as *mut libc::c_char } } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) } - } else if strcmp(cmd, b"chatinfo\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !sel_chat.is_null() { - let mut contacts: *mut dc_array_t = - dc_get_chat_contacts(context, dc_chat_get_id(sel_chat)); - if !contacts.is_null() { - dc_log_info( - context, - 0i32, - b"Memberlist:\x00" as *const u8 as *const libc::c_char, - ); - log_contactlist(context, contacts); - ret = dc_mprintf( - b"%i contacts\nLocation streaming: %i\x00" as *const u8 - as *const libc::c_char, - dc_array_get_cnt(contacts) as libc::c_int, - dc_is_sending_locations_to_chat(context, dc_chat_get_id(sel_chat)), - ); - dc_array_unref(contacts); - } else { - ret = 1i32 as *mut libc::c_char - } - } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) - } - } else if strcmp(cmd, b"getlocations\x00" as *const u8 as *const libc::c_char) == 0i32 { - let mut contact_id_2: libc::c_int = if !arg1.is_null() { atoi(arg1) } else { 0i32 }; - let mut loc: *mut dc_array_t = dc_get_locations( - context, - dc_chat_get_id(sel_chat), - contact_id_2 as uint32_t, - 0i32 as time_t, - 0i32 as time_t, - ); - let mut j = 0; - while j < dc_array_get_cnt(loc) { - let mut timestr_0: *mut libc::c_char = - dc_timestamp_to_str(dc_array_get_timestamp(loc, j as size_t)); - let mut marker: *mut libc::c_char = dc_array_get_marker(loc, j as size_t); - dc_log_info( - context, - 0i32, - b"Loc#%i: %s: lat=%f lng=%f acc=%f Chat#%i Contact#%i Msg#%i %s\x00" - as *const u8 as *const libc::c_char, - dc_array_get_id(loc, j as size_t), - timestr_0, - dc_array_get_latitude(loc, j as size_t), - dc_array_get_longitude(loc, j as size_t), - dc_array_get_accuracy(loc, j as size_t), - dc_array_get_chat_id(loc, j as size_t), - dc_array_get_contact_id(loc, j as size_t), - dc_array_get_msg_id(loc, j as size_t), - if !marker.is_null() { - marker + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"groupimage\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !sel_chat.is_null() { + ret = if 0 + != dc_set_chat_profile_image( + &context, + dc_chat_get_id(sel_chat), + if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { + arg1 } else { - b"-\x00" as *const u8 as *const libc::c_char + 0 as *mut libc::c_char }, - ); - free(timestr_0 as *mut libc::c_void); - free(marker as *mut libc::c_void); - j += 1 + ) { + 2i32 as *mut libc::c_char + } else { + 1i32 as *mut libc::c_char } - if dc_array_get_cnt(loc) == 0 { + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"chatinfo\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !sel_chat.is_null() { + let mut contacts: *mut dc_array_t = + dc_get_chat_contacts(&context, dc_chat_get_id(sel_chat)); + if !contacts.is_null() { dc_log_info( - context, + &context, 0i32, - b"No locations.\x00" as *const u8 as *const libc::c_char, + b"Memberlist:\x00" as *const u8 as *const libc::c_char, ); + log_contactlist(&context, contacts); + ret = dc_mprintf( + b"%i contacts\nLocation streaming: %i\x00" as *const u8 as *const libc::c_char, + dc_array_get_cnt(contacts) as libc::c_int, + dc_is_sending_locations_to_chat(&context, dc_chat_get_id(sel_chat)), + ); + dc_array_unref(contacts); + } else { + ret = 1i32 as *mut libc::c_char } - dc_array_unref(loc); - ret = 2i32 as *mut libc::c_char - } else if strcmp( - cmd, - b"sendlocations\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { - if !sel_chat.is_null() { - if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { - let mut seconds: libc::c_int = atoi(arg1); - dc_send_locations_to_chat(context, dc_chat_get_id(sel_chat), seconds); - ret = + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"getlocations\x00" as *const u8 as *const libc::c_char) == 0i32 { + let mut contact_id_2: libc::c_int = if !arg1.is_null() { atoi(arg1) } else { 0i32 }; + let mut loc: *mut dc_array_t = dc_get_locations( + &context, + dc_chat_get_id(sel_chat), + contact_id_2 as uint32_t, + 0i32 as time_t, + 0i32 as time_t, + ); + let mut j = 0; + while j < dc_array_get_cnt(loc) { + let mut timestr_0: *mut libc::c_char = + dc_timestamp_to_str(dc_array_get_timestamp(loc, j as size_t)); + let mut marker: *mut libc::c_char = dc_array_get_marker(loc, j as size_t); + dc_log_info( + &context, + 0i32, + b"Loc#%i: %s: lat=%f lng=%f acc=%f Chat#%i Contact#%i Msg#%i %s\x00" as *const u8 + as *const libc::c_char, + dc_array_get_id(loc, j as size_t), + timestr_0, + dc_array_get_latitude(loc, j as size_t), + dc_array_get_longitude(loc, j as size_t), + dc_array_get_accuracy(loc, j as size_t), + dc_array_get_chat_id(loc, j as size_t), + dc_array_get_contact_id(loc, j as size_t), + dc_array_get_msg_id(loc, j as size_t), + if !marker.is_null() { + marker + } else { + b"-\x00" as *const u8 as *const libc::c_char + }, + ); + free(timestr_0 as *mut libc::c_void); + free(marker as *mut libc::c_void); + j += 1 + } + if dc_array_get_cnt(loc) == 0 { + dc_log_info( + &context, + 0i32, + b"No locations.\x00" as *const u8 as *const libc::c_char, + ); + } + dc_array_unref(loc); + ret = 2i32 as *mut libc::c_char + } else if strcmp( + cmd, + b"sendlocations\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + if !sel_chat.is_null() { + if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { + let mut seconds: libc::c_int = atoi(arg1); + dc_send_locations_to_chat(&context, dc_chat_get_id(sel_chat), seconds); + ret = dc_mprintf(b"Locations will be sent to Chat#%i for %i seconds. Use \'setlocation \' to play around.\x00" as *const u8 as *const libc::c_char, dc_chat_get_id(sel_chat), seconds) - } else { - ret = dc_strdup( - b"ERROR: No timeout given.\x00" as *const u8 as *const libc::c_char, - ) - } } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + ret = dc_strdup(b"ERROR: No timeout given.\x00" as *const u8 as *const libc::c_char) } - } else if strcmp(cmd, b"setlocation\x00" as *const u8 as *const libc::c_char) == 0i32 { - let mut arg2_1: *mut libc::c_char = 0 as *mut libc::c_char; - if !arg1.is_null() { - arg2_1 = strrchr(arg1, ' ' as i32) - } - if !arg1.is_null() && !arg2_1.is_null() { - *arg2_1 = 0i32 as libc::c_char; - arg2_1 = arg2_1.offset(1isize); - let mut latitude: libc::c_double = atof(arg1); - let mut longitude: libc::c_double = atof(arg2_1); - let mut continue_streaming: libc::c_int = - dc_set_location(context, latitude, longitude, 0.0f64); - ret = dc_strdup(if 0 != continue_streaming { - b"Success, streaming should be continued.\x00" as *const u8 - as *const libc::c_char - } else { - b"Success, streaming can be stoppped.\x00" as *const u8 as *const libc::c_char - }) + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"setlocation\x00" as *const u8 as *const libc::c_char) == 0i32 { + let mut arg2_1: *mut libc::c_char = 0 as *mut libc::c_char; + if !arg1.is_null() { + arg2_1 = strrchr(arg1, ' ' as i32) + } + if !arg1.is_null() && !arg2_1.is_null() { + *arg2_1 = 0i32 as libc::c_char; + arg2_1 = arg2_1.offset(1isize); + let mut latitude: libc::c_double = atof(arg1); + let mut longitude: libc::c_double = atof(arg2_1); + let mut continue_streaming: libc::c_int = + dc_set_location(&context, latitude, longitude, 0.0f64); + ret = dc_strdup(if 0 != continue_streaming { + b"Success, streaming should be continued.\x00" as *const u8 as *const libc::c_char } else { - ret = dc_strdup( - b"ERROR: Latitude or longitude not given.\x00" as *const u8 - as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"dellocations\x00" as *const u8 as *const libc::c_char) == 0i32 { - dc_delete_all_locations(context); - ret = 2i32 as *mut libc::c_char - } else if strcmp(cmd, b"send\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !sel_chat.is_null() { - if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { - if 0 != dc_send_text_msg(context, dc_chat_get_id(sel_chat), arg1) { - ret = dc_strdup(b"Message sent.\x00" as *const u8 as *const libc::c_char) - } else { - ret = dc_strdup( - b"ERROR: Sending failed.\x00" as *const u8 as *const libc::c_char, - ) - } - } else { - ret = dc_strdup( - b"ERROR: No message text given.\x00" as *const u8 as *const libc::c_char, - ) - } - } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) - } - } else if strcmp(cmd, b"sendempty\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !sel_chat.is_null() { - if 0 != dc_send_text_msg( - context, - dc_chat_get_id(sel_chat), - b"\x00" as *const u8 as *const libc::c_char, - ) { + b"Success, streaming can be stoppped.\x00" as *const u8 as *const libc::c_char + }) + } else { + ret = dc_strdup( + b"ERROR: Latitude or longitude not given.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"dellocations\x00" as *const u8 as *const libc::c_char) == 0i32 { + dc_delete_all_locations(&context); + ret = 2i32 as *mut libc::c_char + } else if strcmp(cmd, b"send\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !sel_chat.is_null() { + if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { + if 0 != dc_send_text_msg(&context, dc_chat_get_id(sel_chat), arg1) { ret = dc_strdup(b"Message sent.\x00" as *const u8 as *const libc::c_char) } else { ret = dc_strdup(b"ERROR: Sending failed.\x00" as *const u8 as *const libc::c_char) } - } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) - } - } else if strcmp(cmd, b"sendimage\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(cmd, b"sendfile\x00" as *const u8 as *const libc::c_char) == 0i32 - { - if !sel_chat.is_null() { - if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { - let mut arg2_2: *mut libc::c_char = strchr(arg1, ' ' as i32); - if !arg2_2.is_null() { - *arg2_2 = 0i32 as libc::c_char; - arg2_2 = arg2_2.offset(1isize) - } - let mut msg_0: *mut dc_msg_t = dc_msg_new( - context, - if strcmp(cmd, b"sendimage\x00" as *const u8 as *const libc::c_char) == 0i32 - { - 20i32 - } else { - 60i32 - }, - ); - dc_msg_set_file(msg_0, arg1, 0 as *const libc::c_char); - dc_msg_set_text(msg_0, arg2_2); - dc_send_msg(context, dc_chat_get_id(sel_chat), msg_0); - dc_msg_unref(msg_0); - ret = 2i32 as *mut libc::c_char - } else { - ret = - dc_strdup(b"ERROR: No file given.\x00" as *const u8 as *const libc::c_char) - } - } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) - } - } else if strcmp(cmd, b"listmsgs\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut msglist_0: *mut dc_array_t = dc_search_msgs( - context, - if !sel_chat.is_null() { - dc_chat_get_id(sel_chat) - } else { - 0i32 as libc::c_uint - }, - arg1, - ); - if !msglist_0.is_null() { - log_msglist(context, msglist_0); - ret = dc_mprintf( - b"%i messages.\x00" as *const u8 as *const libc::c_char, - dc_array_get_cnt(msglist_0) as libc::c_int, - ); - dc_array_unref(msglist_0); - } } else { ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + b"ERROR: No message text given.\x00" as *const u8 as *const libc::c_char, ) } - } else if strcmp(cmd, b"draft\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !sel_chat.is_null() { - if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { - let mut draft_0: *mut dc_msg_t = dc_msg_new(context, 10i32); - dc_msg_set_text(draft_0, arg1); - dc_set_draft(context, dc_chat_get_id(sel_chat), draft_0); - dc_msg_unref(draft_0); - ret = dc_strdup(b"Draft saved.\x00" as *const u8 as *const libc::c_char) - } else { - dc_set_draft(context, dc_chat_get_id(sel_chat), 0 as *mut dc_msg_t); - ret = dc_strdup(b"Draft deleted.\x00" as *const u8 as *const libc::c_char) - } + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"sendempty\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !sel_chat.is_null() { + if 0 != dc_send_text_msg( + &context, + dc_chat_get_id(sel_chat), + b"\x00" as *const u8 as *const libc::c_char, + ) { + ret = dc_strdup(b"Message sent.\x00" as *const u8 as *const libc::c_char) } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + ret = dc_strdup(b"ERROR: Sending failed.\x00" as *const u8 as *const libc::c_char) } - } else if strcmp(cmd, b"listmedia\x00" as *const u8 as *const libc::c_char) == 0i32 { - 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 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, - icnt, - ); - i_0 = 0i32; - while i_0 < icnt { - let mut temp: *mut libc::c_char = dc_mprintf( - b"%s%sMsg#%i\x00" as *const u8 as *const libc::c_char, - if 0 != i_0 { - b", \x00" as *const u8 as *const libc::c_char - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - ret, - dc_array_get_id(images, i_0 as size_t) as libc::c_int, - ); - free(ret as *mut libc::c_void); - ret = temp; - i_0 += 1 + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"sendimage\x00" as *const u8 as *const libc::c_char) == 0i32 + || strcmp(cmd, b"sendfile\x00" as *const u8 as *const libc::c_char) == 0i32 + { + if !sel_chat.is_null() { + if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { + let mut arg2_2: *mut libc::c_char = strchr(arg1, ' ' as i32); + if !arg2_2.is_null() { + *arg2_2 = 0i32 as libc::c_char; + arg2_2 = arg2_2.offset(1isize) } - dc_array_unref(images); - } else { - ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) - } - } else if strcmp(cmd, b"archive\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(cmd, b"unarchive\x00" as *const u8 as *const libc::c_char) == 0i32 - { - if !arg1.is_null() { - let mut chat_id_3: libc::c_int = atoi(arg1); - dc_archive_chat( - context, - chat_id_3 as uint32_t, - if strcmp(cmd, b"archive\x00" as *const u8 as *const libc::c_char) == 0i32 { - 1i32 + let mut msg_0: *mut dc_msg_t = dc_msg_new( + &context, + if strcmp(cmd, b"sendimage\x00" as *const u8 as *const libc::c_char) == 0i32 { + 20i32 } else { - 0i32 + 60i32 }, ); + dc_msg_set_file(msg_0, arg1, 0 as *const libc::c_char); + dc_msg_set_text(msg_0, arg2_2); + dc_send_msg(&context, dc_chat_get_id(sel_chat), msg_0); + dc_msg_unref(msg_0); ret = 2i32 as *mut libc::c_char } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) + ret = dc_strdup(b"ERROR: No file given.\x00" as *const u8 as *const libc::c_char) } - } else if strcmp(cmd, b"delchat\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut chat_id_4: libc::c_int = atoi(arg1); - dc_delete_chat(context, chat_id_4 as uint32_t); - ret = 2i32 as *mut libc::c_char - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"msginfo\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut id: libc::c_int = atoi(arg1); - ret = dc_get_msg_info(context, id as uint32_t) - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"listfresh\x00" as *const u8 as *const libc::c_char) == 0i32 { - let mut msglist_1: *mut dc_array_t = dc_get_fresh_msgs(context); - if !msglist_1.is_null() { - log_msglist(context, msglist_1); - ret = dc_mprintf( - b"%i fresh messages.\x00" as *const u8 as *const libc::c_char, - dc_array_get_cnt(msglist_1) as libc::c_int, - ); - dc_array_unref(msglist_1); - } - } else if strcmp(cmd, b"forward\x00" as *const u8 as *const libc::c_char) == 0i32 { - let mut arg2_3: *mut libc::c_char = 0 as *mut libc::c_char; - if !arg1.is_null() { - arg2_3 = strrchr(arg1, ' ' as i32) - } - if !arg1.is_null() && !arg2_3.is_null() { - *arg2_3 = 0i32 as libc::c_char; - arg2_3 = arg2_3.offset(1isize); - let mut msg_ids: [uint32_t; 1] = [0; 1]; - let mut chat_id_5: uint32_t = atoi(arg2_3) as uint32_t; - msg_ids[0usize] = atoi(arg1) as uint32_t; - dc_forward_msgs(context, msg_ids.as_mut_ptr(), 1i32, chat_id_5); - ret = 2i32 as *mut libc::c_char - } else { - ret = dc_strdup( - b"ERROR: Arguments expected.\x00" as *const u8 - as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"markseen\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut msg_ids_0: [uint32_t; 1] = [0; 1]; - msg_ids_0[0usize] = atoi(arg1) as uint32_t; - dc_markseen_msgs(context, msg_ids_0.as_mut_ptr(), 1i32); - ret = 2i32 as *mut libc::c_char - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"star\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(cmd, b"unstar\x00" as *const u8 as *const libc::c_char) == 0i32 - { - if !arg1.is_null() { - let mut msg_ids_1: [uint32_t; 1] = [0; 1]; - msg_ids_1[0usize] = atoi(arg1) as uint32_t; - dc_star_msgs( - context, - msg_ids_1.as_mut_ptr(), - 1i32, - if strcmp(cmd, b"star\x00" as *const u8 as *const libc::c_char) == 0i32 { - 1i32 - } else { - 0i32 - }, - ); - ret = 2i32 as *mut libc::c_char - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"delmsg\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut ids: [uint32_t; 1] = [0; 1]; - ids[0usize] = atoi(arg1) as uint32_t; - dc_delete_msgs(context, ids.as_mut_ptr(), 1i32); - ret = 2i32 as *mut libc::c_char - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"listcontacts\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(cmd, b"contacts\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(cmd, b"listverified\x00" as *const u8 as *const libc::c_char) == 0i32 - { - let mut contacts_0: *mut dc_array_t = dc_get_contacts( - context, - (if strcmp(cmd, b"listverified\x00" as *const u8 as *const libc::c_char) == 0i32 { - 0x1i32 | 0x2i32 + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"listmsgs\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut msglist_0: *mut dc_array_t = dc_search_msgs( + &context, + if !sel_chat.is_null() { + dc_chat_get_id(sel_chat) } else { - 0x2i32 - }) as uint32_t, + 0i32 as libc::c_uint + }, arg1, ); - if !contacts_0.is_null() { - log_contactlist(context, contacts_0); + if !msglist_0.is_null() { + log_msglist(&context, msglist_0); ret = dc_mprintf( - b"%i contacts.\x00" as *const u8 as *const libc::c_char, - dc_array_get_cnt(contacts_0) as libc::c_int, + b"%i messages.\x00" as *const u8 as *const libc::c_char, + dc_array_get_cnt(msglist_0) as libc::c_int, ); - dc_array_unref(contacts_0); + dc_array_unref(msglist_0); + } + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"draft\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !sel_chat.is_null() { + if !arg1.is_null() && 0 != *arg1.offset(0isize) as libc::c_int { + let mut draft_0: *mut dc_msg_t = dc_msg_new(&context, 10i32); + dc_msg_set_text(draft_0, arg1); + dc_set_draft(&context, dc_chat_get_id(sel_chat), draft_0); + dc_msg_unref(draft_0); + ret = dc_strdup(b"Draft saved.\x00" as *const u8 as *const libc::c_char) } else { - ret = 1i32 as *mut libc::c_char + dc_set_draft(&context, dc_chat_get_id(sel_chat), 0 as *mut dc_msg_t); + ret = dc_strdup(b"Draft deleted.\x00" as *const u8 as *const libc::c_char) } - } else if strcmp(cmd, b"addcontact\x00" as *const u8 as *const libc::c_char) == 0i32 { - let mut arg2_4: *mut libc::c_char = 0 as *mut libc::c_char; - if !arg1.is_null() { - arg2_4 = strrchr(arg1, ' ' as i32) - } - if !arg1.is_null() && !arg2_4.is_null() { - *arg2_4 = 0i32 as libc::c_char; - arg2_4 = arg2_4.offset(1isize); - let mut book: *mut libc::c_char = dc_mprintf( - b"%s\n%s\x00" as *const u8 as *const libc::c_char, - arg1, - arg2_4, + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"listmedia\x00" as *const u8 as *const libc::c_char) == 0i32 { + 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 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, + icnt, + ); + i_0 = 0i32; + while i_0 < icnt { + let mut temp: *mut libc::c_char = dc_mprintf( + b"%s%sMsg#%i\x00" as *const u8 as *const libc::c_char, + if 0 != i_0 { + b", \x00" as *const u8 as *const libc::c_char + } else { + b"\x00" as *const u8 as *const libc::c_char + }, + ret, + dc_array_get_id(images, i_0 as size_t) as libc::c_int, ); - dc_add_address_book(context, book); - ret = 2i32 as *mut libc::c_char; - free(book as *mut libc::c_void); - } else if !arg1.is_null() { - ret = if 0 != dc_create_contact(context, 0 as *const libc::c_char, arg1) { - 2i32 as *mut libc::c_char + free(ret as *mut libc::c_void); + ret = temp; + i_0 += 1 + } + dc_array_unref(images); + } else { + ret = dc_strdup(b"No chat selected.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"archive\x00" as *const u8 as *const libc::c_char) == 0i32 + || strcmp(cmd, b"unarchive\x00" as *const u8 as *const libc::c_char) == 0i32 + { + if !arg1.is_null() { + let mut chat_id_3: libc::c_int = atoi(arg1); + dc_archive_chat( + &context, + chat_id_3 as uint32_t, + if strcmp(cmd, b"archive\x00" as *const u8 as *const libc::c_char) == 0i32 { + 1i32 } else { - 1i32 as *mut libc::c_char - } - } else { - ret = dc_strdup( - b"ERROR: Arguments [] expected.\x00" as *const u8 - as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"contactinfo\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut contact_id_3: libc::c_int = atoi(arg1); - let mut strbuilder: 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 strbuilder, 0i32); - let mut contact: *mut dc_contact_t = - dc_get_contact(context, contact_id_3 as uint32_t); - let mut nameNaddr: *mut libc::c_char = dc_contact_get_name_n_addr(contact); - dc_strbuilder_catf( - &mut strbuilder as *mut dc_strbuilder_t, - b"Contact info for: %s:\n\n\x00" as *const u8 as *const libc::c_char, - nameNaddr, - ); - free(nameNaddr as *mut libc::c_void); - dc_contact_unref(contact); - let mut encrinfo: *mut libc::c_char = - dc_get_contact_encrinfo(context, contact_id_3 as uint32_t); - dc_strbuilder_cat(&mut strbuilder, encrinfo); - free(encrinfo as *mut libc::c_void); - let mut chatlist_0: *mut dc_chatlist_t = dc_get_chatlist( - context, - 0i32, - 0 as *const libc::c_char, - contact_id_3 as uint32_t, - ); - let mut chatlist_cnt: libc::c_int = dc_chatlist_get_cnt(chatlist_0) as libc::c_int; - if chatlist_cnt > 0i32 { - dc_strbuilder_catf( - &mut strbuilder as *mut dc_strbuilder_t, - b"\n\n%i chats shared with Contact#%i: \x00" as *const u8 - as *const libc::c_char, - chatlist_cnt, - contact_id_3, - ); - let mut i_1: libc::c_int = 0i32; - while i_1 < chatlist_cnt { - if 0 != i_1 { - dc_strbuilder_cat( - &mut strbuilder, - b", \x00" as *const u8 as *const libc::c_char, - ); - } - let mut chat_1: *mut dc_chat_t = dc_get_chat( - context, - dc_chatlist_get_chat_id(chatlist_0, i_1 as size_t), - ); - dc_strbuilder_catf( - &mut strbuilder as *mut dc_strbuilder_t, - b"%s#%i\x00" as *const u8 as *const libc::c_char, - chat_prefix(chat_1), - dc_chat_get_id(chat_1), - ); - dc_chat_unref(chat_1); - i_1 += 1 - } - } - dc_chatlist_unref(chatlist_0); - ret = strbuilder.buf - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 - as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"delcontact\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - ret = if 0 != dc_delete_contact(context, atoi(arg1) as uint32_t) { - 2i32 as *mut libc::c_char + 0i32 + }, + ); + ret = 2i32 as *mut libc::c_char + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"delchat\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut chat_id_4: libc::c_int = atoi(arg1); + dc_delete_chat(&context, chat_id_4 as uint32_t); + ret = 2i32 as *mut libc::c_char + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"msginfo\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut id: libc::c_int = atoi(arg1); + ret = dc_get_msg_info(&context, id as uint32_t) + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"listfresh\x00" as *const u8 as *const libc::c_char) == 0i32 { + let mut msglist_1: *mut dc_array_t = dc_get_fresh_msgs(&context); + if !msglist_1.is_null() { + log_msglist(&context, msglist_1); + ret = dc_mprintf( + b"%i fresh messages.\x00" as *const u8 as *const libc::c_char, + dc_array_get_cnt(msglist_1) as libc::c_int, + ); + dc_array_unref(msglist_1); + } + } else if strcmp(cmd, b"forward\x00" as *const u8 as *const libc::c_char) == 0i32 { + let mut arg2_3: *mut libc::c_char = 0 as *mut libc::c_char; + if !arg1.is_null() { + arg2_3 = strrchr(arg1, ' ' as i32) + } + if !arg1.is_null() && !arg2_3.is_null() { + *arg2_3 = 0i32 as libc::c_char; + arg2_3 = arg2_3.offset(1isize); + let mut msg_ids: [uint32_t; 1] = [0; 1]; + let mut chat_id_5: uint32_t = atoi(arg2_3) as uint32_t; + msg_ids[0usize] = atoi(arg1) as uint32_t; + dc_forward_msgs(&context, msg_ids.as_mut_ptr(), 1i32, chat_id_5); + ret = 2i32 as *mut libc::c_char + } else { + ret = dc_strdup( + b"ERROR: Arguments expected.\x00" as *const u8 + as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"markseen\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut msg_ids_0: [uint32_t; 1] = [0; 1]; + msg_ids_0[0usize] = atoi(arg1) as uint32_t; + dc_markseen_msgs(&context, msg_ids_0.as_mut_ptr(), 1i32); + ret = 2i32 as *mut libc::c_char + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"star\x00" as *const u8 as *const libc::c_char) == 0i32 + || strcmp(cmd, b"unstar\x00" as *const u8 as *const libc::c_char) == 0i32 + { + if !arg1.is_null() { + let mut msg_ids_1: [uint32_t; 1] = [0; 1]; + msg_ids_1[0usize] = atoi(arg1) as uint32_t; + dc_star_msgs( + &context, + msg_ids_1.as_mut_ptr(), + 1i32, + if strcmp(cmd, b"star\x00" as *const u8 as *const libc::c_char) == 0i32 { + 1i32 } else { - 1i32 as *mut libc::c_char - } + 0i32 + }, + ); + ret = 2i32 as *mut libc::c_char + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"delmsg\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut ids: [uint32_t; 1] = [0; 1]; + ids[0usize] = atoi(arg1) as uint32_t; + dc_delete_msgs(&context, ids.as_mut_ptr(), 1i32); + ret = 2i32 as *mut libc::c_char + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"listcontacts\x00" as *const u8 as *const libc::c_char) == 0i32 + || strcmp(cmd, b"contacts\x00" as *const u8 as *const libc::c_char) == 0i32 + || strcmp(cmd, b"listverified\x00" as *const u8 as *const libc::c_char) == 0i32 + { + let mut contacts_0: *mut dc_array_t = dc_get_contacts( + &context, + (if strcmp(cmd, b"listverified\x00" as *const u8 as *const libc::c_char) == 0i32 { + 0x1i32 | 0x2i32 } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 - as *const libc::c_char, - ) - } - } else if strcmp( - cmd, - b"cleanupcontacts\x00" as *const u8 as *const libc::c_char, - ) == 0i32 - { - ret = if 0 != dc_cleanup_contacts(context) { + 0x2i32 + }) as uint32_t, + arg1, + ); + if !contacts_0.is_null() { + log_contactlist(&context, contacts_0); + ret = dc_mprintf( + b"%i contacts.\x00" as *const u8 as *const libc::c_char, + dc_array_get_cnt(contacts_0) as libc::c_int, + ); + dc_array_unref(contacts_0); + } else { + ret = 1i32 as *mut libc::c_char + } + } else if strcmp(cmd, b"addcontact\x00" as *const u8 as *const libc::c_char) == 0i32 { + let mut arg2_4: *mut libc::c_char = 0 as *mut libc::c_char; + if !arg1.is_null() { + arg2_4 = strrchr(arg1, ' ' as i32) + } + if !arg1.is_null() && !arg2_4.is_null() { + *arg2_4 = 0i32 as libc::c_char; + arg2_4 = arg2_4.offset(1isize); + let mut book: *mut libc::c_char = dc_mprintf( + b"%s\n%s\x00" as *const u8 as *const libc::c_char, + arg1, + arg2_4, + ); + dc_add_address_book(&context, book); + ret = 2i32 as *mut libc::c_char; + free(book as *mut libc::c_void); + } else if !arg1.is_null() { + ret = if 0 != dc_create_contact(&context, 0 as *const libc::c_char, arg1) { 2i32 as *mut libc::c_char } else { 1i32 as *mut libc::c_char } - } else if strcmp(cmd, b"getqr\x00" as *const u8 as *const libc::c_char) == 0i32 { - ret = dc_get_securejoin_qr( - context, - (if !arg1.is_null() { atoi(arg1) } else { 0i32 }) as uint32_t, + } else { + ret = dc_strdup( + b"ERROR: Arguments [] expected.\x00" as *const u8 + as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"contactinfo\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut contact_id_3: libc::c_int = atoi(arg1); + let mut strbuilder: 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 strbuilder, 0i32); + let mut contact: *mut dc_contact_t = dc_get_contact(&context, contact_id_3 as uint32_t); + let mut nameNaddr: *mut libc::c_char = dc_contact_get_name_n_addr(contact); + dc_strbuilder_catf( + &mut strbuilder as *mut dc_strbuilder_t, + b"Contact info for: %s:\n\n\x00" as *const u8 as *const libc::c_char, + nameNaddr, ); - if ret.is_null() || *ret.offset(0isize) as libc::c_int == 0i32 { - free(ret as *mut libc::c_void); - ret = 1i32 as *mut libc::c_char - } - } else if strcmp(cmd, b"checkqr\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut res: *mut dc_lot_t = dc_check_qr(context, arg1); - ret = dc_mprintf( - b"state=%i, id=%i, text1=%s, text2=%s\x00" as *const u8 as *const libc::c_char, - (*res).state as libc::c_int, - (*res).id, - if !(*res).text1.is_null() { - (*res).text1 - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - if !(*res).text2.is_null() { - (*res).text2 - } else { - b"\x00" as *const u8 as *const libc::c_char - }, - ); - dc_lot_unref(res); - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 + free(nameNaddr as *mut libc::c_void); + dc_contact_unref(contact); + let mut encrinfo: *mut libc::c_char = + dc_get_contact_encrinfo(&context, contact_id_3 as uint32_t); + dc_strbuilder_cat(&mut strbuilder, encrinfo); + free(encrinfo as *mut libc::c_void); + let mut chatlist_0: *mut dc_chatlist_t = dc_get_chatlist( + &context, + 0i32, + 0 as *const libc::c_char, + contact_id_3 as uint32_t, + ); + let mut chatlist_cnt: libc::c_int = dc_chatlist_get_cnt(chatlist_0) as libc::c_int; + if chatlist_cnt > 0i32 { + dc_strbuilder_catf( + &mut strbuilder as *mut dc_strbuilder_t, + b"\n\n%i chats shared with Contact#%i: \x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"event\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut event = Event::from_u32(atoi(arg1) as u32).unwrap(); - let mut r: uintptr_t = - ((*context).cb)(context, event, 0i32 as uintptr_t, 0i32 as uintptr_t); - ret = dc_mprintf( - b"Sending event %i, received value %i.\x00" as *const u8 as *const libc::c_char, - event as libc::c_int, - r as libc::c_int, - ) - } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) - } - } else if strcmp(cmd, b"fileinfo\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !arg1.is_null() { - let mut buf: *mut libc::c_uchar = 0 as *mut libc::c_uchar; - let mut buf_bytes: size_t = 0; - let mut w: uint32_t = 0; - let mut h: uint32_t = 0; - if 0 != dc_read_file( - context, - arg1, - &mut buf as *mut *mut libc::c_uchar as *mut *mut libc::c_void, - &mut buf_bytes, - ) { - dc_get_filemeta(buf as *const libc::c_void, buf_bytes, &mut w, &mut h); - ret = dc_mprintf( - b"width=%i, height=%i\x00" as *const u8 as *const libc::c_char, - w as libc::c_int, - h as libc::c_int, - ) - } else { - ret = - dc_strdup(b"ERROR: Command failed.\x00" as *const u8 as *const libc::c_char) + chatlist_cnt, + contact_id_3, + ); + let mut i_1: libc::c_int = 0i32; + while i_1 < chatlist_cnt { + if 0 != i_1 { + dc_strbuilder_cat( + &mut strbuilder, + b", \x00" as *const u8 as *const libc::c_char, + ); + } + let mut chat_1: *mut dc_chat_t = + dc_get_chat(&context, dc_chatlist_get_chat_id(chatlist_0, i_1 as size_t)); + dc_strbuilder_catf( + &mut strbuilder as *mut dc_strbuilder_t, + b"%s#%i\x00" as *const u8 as *const libc::c_char, + chat_prefix(chat_1), + dc_chat_get_id(chat_1), + ); + dc_chat_unref(chat_1); + i_1 += 1 } - free(buf as *mut libc::c_void); + } + dc_chatlist_unref(chatlist_0); + ret = strbuilder.buf + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"delcontact\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + ret = if 0 != dc_delete_contact(&context, atoi(arg1) as uint32_t) { + 2i32 as *mut libc::c_char } else { - ret = dc_strdup( - b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, - ) + 1i32 as *mut libc::c_char } } else { - ret = 3i32 as *mut libc::c_char + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) } + } else if strcmp( + cmd, + b"cleanupcontacts\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + ret = if 0 != dc_cleanup_contacts(&context) { + 2i32 as *mut libc::c_char + } else { + 1i32 as *mut libc::c_char + } + } else if strcmp(cmd, b"getqr\x00" as *const u8 as *const libc::c_char) == 0i32 { + ret = dc_get_securejoin_qr( + &context, + (if !arg1.is_null() { atoi(arg1) } else { 0i32 }) as uint32_t, + ); + if ret.is_null() || *ret.offset(0isize) as libc::c_int == 0i32 { + free(ret as *mut libc::c_void); + ret = 1i32 as *mut libc::c_char + } + } else if strcmp(cmd, b"checkqr\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut res: *mut dc_lot_t = dc_check_qr(&context, arg1); + ret = dc_mprintf( + b"state=%i, id=%i, text1=%s, text2=%s\x00" as *const u8 as *const libc::c_char, + (*res).state as libc::c_int, + (*res).id, + if !(*res).text1.is_null() { + (*res).text1 + } else { + b"\x00" as *const u8 as *const libc::c_char + }, + if !(*res).text2.is_null() { + (*res).text2 + } else { + b"\x00" as *const u8 as *const libc::c_char + }, + ); + dc_lot_unref(res); + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else if strcmp(cmd, b"event\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut event = Event::from_u32(atoi(arg1) as u32).unwrap(); + let mut r: uintptr_t = + (&context.cb)(&context, event, 0i32 as uintptr_t, 0i32 as uintptr_t); + ret = dc_mprintf( + b"Sending event %i, received value %i.\x00" as *const u8 as *const libc::c_char, + event as libc::c_int, + r as libc::c_int, + ) + } else { + ret = + dc_strdup(b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char) + } + } else if strcmp(cmd, b"fileinfo\x00" as *const u8 as *const libc::c_char) == 0i32 { + if !arg1.is_null() { + let mut buf: *mut libc::c_uchar = 0 as *mut libc::c_uchar; + let mut buf_bytes: size_t = 0; + let mut w: uint32_t = 0; + let mut h: uint32_t = 0; + if 0 != dc_read_file( + &context, + arg1, + &mut buf as *mut *mut libc::c_uchar as *mut *mut libc::c_void, + &mut buf_bytes, + ) { + dc_get_filemeta(buf as *const libc::c_void, buf_bytes, &mut w, &mut h); + ret = dc_mprintf( + b"width=%i, height=%i\x00" as *const u8 as *const libc::c_char, + w as libc::c_int, + h as libc::c_int, + ) + } else { + ret = dc_strdup(b"ERROR: Command failed.\x00" as *const u8 as *const libc::c_char) + } + free(buf as *mut libc::c_void); + } else { + ret = dc_strdup( + b"ERROR: Argument missing.\x00" as *const u8 as *const libc::c_char, + ) + } + } else { + ret = 3i32 as *mut libc::c_char } + if ret == 2i32 as *mut libc::c_char { ret = dc_strdup(b"Command executed successfully.\x00" as *const u8 as *const libc::c_char) } else if ret == 1i32 as *mut libc::c_char { diff --git a/examples/repl/main.rs b/examples/repl/main.rs index 35e1bfb4e..fc709fc3b 100644 --- a/examples/repl/main.rs +++ b/examples/repl/main.rs @@ -1,561 +1,566 @@ -// //! This is a CLI program and a little testing frame. This file must not be -// //! included when using Delta Chat Core as a library. -// //! -// //! Usage: messenger-backend -// //! (for "Code::Blocks, use Project / Set programs' arguments") -// //! all further options can be set using the set-command (type ? for help). +//! This is a CLI program and a little testing frame. This file must not be +//! included when using Delta Chat Core as a library. +//! +//! Usage: messenger-backend +//! (for "Code::Blocks, use Project / Set programs' arguments") +//! all further options can be set using the set-command (type ? for help). -// #![allow( -// unused_imports, -// mutable_transmutes, -// non_camel_case_types, -// non_snake_case, -// non_upper_case_globals, -// unused_assignments, -// unused_mut, -// unused_attributes, -// non_upper_case_globals, -// non_camel_case_types, -// non_snake_case -// )] +#![allow( + unused_imports, + mutable_transmutes, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut, + unused_attributes, + non_upper_case_globals, + non_camel_case_types, + non_snake_case +)] -// use std::ffi::CString; -// use std::io::{self, Write}; +use std::ffi::CString; +use std::io::{self, Write}; +use std::sync::{Arc, RwLock}; -// use deltachat::constants::*; -// use deltachat::dc_aheader::*; -// use deltachat::dc_apeerstate::*; -// use deltachat::dc_array::*; -// use deltachat::dc_chat::*; -// use deltachat::dc_chatlist::*; -// use deltachat::dc_configure::*; -// use deltachat::dc_contact::*; -// use deltachat::dc_context::*; -// use deltachat::dc_dehtml::*; -// use deltachat::dc_e2ee::*; -// use deltachat::dc_hash::*; -// use deltachat::dc_imap::*; -// use deltachat::dc_imex::*; -// use deltachat::dc_job::*; -// use deltachat::dc_jobthread::*; -// use deltachat::dc_jsmn::*; -// use deltachat::dc_key::*; -// use deltachat::dc_keyhistory::*; -// use deltachat::dc_keyring::*; -// use deltachat::dc_location::*; -// use deltachat::dc_log::*; -// use deltachat::dc_loginparam::*; -// use deltachat::dc_lot::*; -// use deltachat::dc_mimefactory::*; -// use deltachat::dc_mimeparser::*; -// use deltachat::dc_move::*; -// use deltachat::dc_msg::*; -// use deltachat::dc_oauth2::*; -// use deltachat::dc_param::*; -// use deltachat::dc_pgp::*; -// use deltachat::dc_qr::*; -// use deltachat::dc_receive_imf::*; -// use deltachat::dc_saxparser::*; -// use deltachat::dc_securejoin::*; -// use deltachat::dc_simplify::*; -// use deltachat::dc_smtp::*; -// use deltachat::dc_sqlite3::*; -// use deltachat::dc_stock::*; -// use deltachat::dc_strbuilder::*; -// use deltachat::dc_strencode::*; -// use deltachat::dc_token::*; -// use deltachat::dc_tools::*; -// use deltachat::types::*; -// use deltachat::x::*; -// use libc; +use deltachat::constants::*; +use deltachat::dc_aheader::*; +use deltachat::dc_apeerstate::*; +use deltachat::dc_array::*; +use deltachat::dc_chat::*; +use deltachat::dc_chatlist::*; +use deltachat::dc_configure::*; +use deltachat::dc_contact::*; +use deltachat::dc_context::*; +use deltachat::dc_dehtml::*; +use deltachat::dc_e2ee::*; +use deltachat::dc_hash::*; +use deltachat::dc_imap::*; +use deltachat::dc_imex::*; +use deltachat::dc_job::*; +use deltachat::dc_jobthread::*; +use deltachat::dc_jsmn::*; +use deltachat::dc_key::*; +use deltachat::dc_keyhistory::*; +use deltachat::dc_keyring::*; +use deltachat::dc_location::*; +use deltachat::dc_log::*; +use deltachat::dc_loginparam::*; +use deltachat::dc_lot::*; +use deltachat::dc_mimefactory::*; +use deltachat::dc_mimeparser::*; +use deltachat::dc_move::*; +use deltachat::dc_msg::*; +use deltachat::dc_oauth2::*; +use deltachat::dc_param::*; +use deltachat::dc_pgp::*; +use deltachat::dc_qr::*; +use deltachat::dc_receive_imf::*; +use deltachat::dc_saxparser::*; +use deltachat::dc_securejoin::*; +use deltachat::dc_simplify::*; +use deltachat::dc_smtp::*; +use deltachat::dc_sqlite3::*; +use deltachat::dc_stock::*; +use deltachat::dc_strbuilder::*; +use deltachat::dc_strencode::*; +use deltachat::dc_token::*; +use deltachat::dc_tools::*; +use deltachat::types::*; +use deltachat::x::*; +use libc; -// mod cmdline; -// mod stress; +mod cmdline; +mod stress; -// use self::cmdline::*; -// use self::stress::*; +use self::cmdline::*; +use self::stress::*; -// /* ****************************************************************************** -// * Event Handler -// ******************************************************************************/ -// static mut s_do_log_info: libc::c_int = 1i32; +/* ****************************************************************************** + * Event Handler + ******************************************************************************/ +static mut s_do_log_info: libc::c_int = 1i32; -// unsafe extern "C" fn receive_event( -// mut context: &dc_context_t, -// mut event: Event, -// mut data1: uintptr_t, -// mut data2: uintptr_t, -// ) -> uintptr_t { -// match event as u32 { -// 2091 => {} -// 100 => { -// /* do not show the event as this would fill the screen */ -// if 0 != s_do_log_info { -// printf( -// b"%s\n\x00" as *const u8 as *const libc::c_char, -// data2 as *mut libc::c_char, -// ); -// } -// } -// 101 => { -// printf( -// b"[DC_EVENT_SMTP_CONNECTED] %s\n\x00" as *const u8 as *const libc::c_char, -// data2 as *mut libc::c_char, -// ); -// } -// 102 => { -// printf( -// b"[DC_EVENT_IMAP_CONNECTED] %s\n\x00" as *const u8 as *const libc::c_char, -// data2 as *mut libc::c_char, -// ); -// } -// 103 => { -// printf( -// b"[DC_EVENT_SMTP_MESSAGE_SENT] %s\n\x00" as *const u8 as *const libc::c_char, -// data2 as *mut libc::c_char, -// ); -// } -// 300 => { -// printf( -// b"[Warning] %s\n\x00" as *const u8 as *const libc::c_char, -// data2 as *mut libc::c_char, -// ); -// } -// 400 => { -// printf( -// b"\x1b[31m[DC_EVENT_ERROR] %s\x1b[0m\n\x00" as *const u8 as *const libc::c_char, -// data2 as *mut libc::c_char, -// ); -// } -// 401 => { -// printf( -// b"\x1b[31m[DC_EVENT_ERROR_NETWORK] first=%i, msg=%s\x1b[0m\n\x00" as *const u8 -// as *const libc::c_char, -// data1 as libc::c_int, -// data2 as *mut libc::c_char, -// ); -// } -// 410 => { -// printf( -// b"\x1b[31m[DC_EVENT_ERROR_SELF_NOT_IN_GROUP] %s\x1b[0m\n\x00" as *const u8 -// as *const libc::c_char, -// data2 as *mut libc::c_char, -// ); -// } -// 2100 | 2110 => { -// let mut url: *mut libc::c_char = dc_strdup(data1 as *mut libc::c_char); -// let mut param: *mut libc::c_char = strchr(url, '?' as i32); -// if !param.is_null() { -// *param = 0i32 as libc::c_char; -// param = param.offset(1isize) -// } else { -// param = b"\x00" as *const u8 as *const libc::c_char as *mut libc::c_char -// } -// 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, -// b"curl.result\x00" as *const u8 as *const libc::c_char, -// ); -// let mut cmd: *mut libc::c_char = if event == Event::HTTP_GET { -// dc_mprintf( -// b"curl --silent --location --fail --insecure %s%s%s > %s\x00" as *const u8 -// as *const libc::c_char, -// url, -// if 0 != *param.offset(0isize) as libc::c_int { -// b"?\x00" as *const u8 as *const libc::c_char -// } else { -// b"\x00" as *const u8 as *const libc::c_char -// }, -// param, -// tempFile, -// ) -// } else { -// dc_mprintf( -// b"curl --silent -d \"%s\" %s > %s\x00" as *const u8 as *const libc::c_char, -// param, -// url, -// tempFile, -// ) -// }; -// let mut error: libc::c_int = system(cmd); -// if error == 0i32 { -// let mut bytes: size_t = 0i32 as size_t; -// dc_read_file( -// context, -// tempFile, -// &mut ret as *mut *mut libc::c_char as *mut *mut libc::c_void, -// &mut bytes, -// ); -// } -// free(cmd as *mut libc::c_void); -// free(tempFile as *mut libc::c_void); -// free(url as *mut libc::c_void); -// return ret as uintptr_t; -// } -// 2081 => { -// printf( -// b"\x1b[33m{{Received DC_EVENT_IS_OFFLINE()}}\n\x1b[0m\x00" as *const u8 -// as *const libc::c_char, -// ); -// } -// 2000 => { -// printf( -// b"\x1b[33m{{Received DC_EVENT_MSGS_CHANGED(%i, %i)}}\n\x1b[0m\x00" as *const u8 -// as *const libc::c_char, -// data1 as libc::c_int, -// data2 as libc::c_int, -// ); -// } -// 2030 => { -// printf( -// b"\x1b[33m{{Received DC_EVENT_CONTACTS_CHANGED()}}\n\x1b[0m\x00" as *const u8 -// as *const libc::c_char, -// ); -// } -// 2035 => { -// printf( -// b"\x1b[33m{{Received DC_EVENT_LOCATION_CHANGED(contact=%i)}}\n\x1b[0m\x00" -// as *const u8 as *const libc::c_char, -// data1 as libc::c_int, -// ); -// } -// 2041 => { -// printf( -// b"\x1b[33m{{Received DC_EVENT_CONFIGURE_PROGRESS(%i \xe2\x80\xb0)}}\n\x1b[0m\x00" -// as *const u8 as *const libc::c_char, -// data1 as libc::c_int, -// ); -// } -// 2051 => { -// printf( -// b"\x1b[33m{{Received DC_EVENT_IMEX_PROGRESS(%i \xe2\x80\xb0)}}\n\x1b[0m\x00" -// as *const u8 as *const libc::c_char, -// data1 as libc::c_int, -// ); -// } -// 2052 => { -// printf( -// b"\x1b[33m{{Received DC_EVENT_IMEX_FILE_WRITTEN(%s)}}\n\x1b[0m\x00" as *const u8 -// as *const libc::c_char, -// data1 as *mut libc::c_char, -// ); -// } -// 2055 => { -// printf( -// b"\x1b[33m{{Received DC_EVENT_FILE_COPIED(%s)}}\n\x1b[0m\x00" as *const u8 -// as *const libc::c_char, -// data1 as *mut libc::c_char, -// ); -// } -// 2020 => { -// printf( -// b"\x1b[33m{{Received DC_EVENT_CHAT_MODIFIED(%i)}}\n\x1b[0m\x00" as *const u8 -// as *const libc::c_char, -// data1 as libc::c_int, -// ); -// } -// _ => { -// printf( -// b"\x1b[33m{{Received DC_EVENT_%i(%i, %i)}}\n\x1b[0m\x00" as *const u8 -// as *const libc::c_char, -// event as libc::c_int, -// data1 as libc::c_int, -// data2 as libc::c_int, -// ); -// } -// } -// return 0i32 as uintptr_t; -// } -// /* ****************************************************************************** -// * Threads for waiting for messages and for jobs -// ******************************************************************************/ -// static mut inbox_thread: pthread_t = 0 as pthread_t; -// static mut run_threads: libc::c_int = 0i32; +unsafe extern "C" fn receive_event( + context: &dc_context_t, + event: Event, + data1: uintptr_t, + data2: uintptr_t, +) -> uintptr_t { + match event as u32 { + 2091 => {} + 100 => { + /* do not show the event as this would fill the screen */ + if 0 != s_do_log_info { + printf( + b"%s\n\x00" as *const u8 as *const libc::c_char, + data2 as *mut libc::c_char, + ); + } + } + 101 => { + printf( + b"[DC_EVENT_SMTP_CONNECTED] %s\n\x00" as *const u8 as *const libc::c_char, + data2 as *mut libc::c_char, + ); + } + 102 => { + printf( + b"[DC_EVENT_IMAP_CONNECTED] %s\n\x00" as *const u8 as *const libc::c_char, + data2 as *mut libc::c_char, + ); + } + 103 => { + printf( + b"[DC_EVENT_SMTP_MESSAGE_SENT] %s\n\x00" as *const u8 as *const libc::c_char, + data2 as *mut libc::c_char, + ); + } + 300 => { + printf( + b"[Warning] %s\n\x00" as *const u8 as *const libc::c_char, + data2 as *mut libc::c_char, + ); + } + 400 => { + printf( + b"\x1b[31m[DC_EVENT_ERROR] %s\x1b[0m\n\x00" as *const u8 as *const libc::c_char, + data2 as *mut libc::c_char, + ); + } + 401 => { + printf( + b"\x1b[31m[DC_EVENT_ERROR_NETWORK] first=%i, msg=%s\x1b[0m\n\x00" as *const u8 + as *const libc::c_char, + data1 as libc::c_int, + data2 as *mut libc::c_char, + ); + } + 410 => { + printf( + b"\x1b[31m[DC_EVENT_ERROR_SELF_NOT_IN_GROUP] %s\x1b[0m\n\x00" as *const u8 + as *const libc::c_char, + data2 as *mut libc::c_char, + ); + } + 2100 | 2110 => { + let mut url: *mut libc::c_char = dc_strdup(data1 as *mut libc::c_char); + let mut param: *mut libc::c_char = strchr(url, '?' as i32); + if !param.is_null() { + *param = 0i32 as libc::c_char; + param = param.offset(1isize) + } else { + param = b"\x00" as *const u8 as *const libc::c_char as *mut libc::c_char + } + 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, + b"curl.result\x00" as *const u8 as *const libc::c_char, + ); + let mut cmd: *mut libc::c_char = if event == Event::HTTP_GET { + dc_mprintf( + b"curl --silent --location --fail --insecure %s%s%s > %s\x00" as *const u8 + as *const libc::c_char, + url, + if 0 != *param.offset(0isize) as libc::c_int { + b"?\x00" as *const u8 as *const libc::c_char + } else { + b"\x00" as *const u8 as *const libc::c_char + }, + param, + tempFile, + ) + } else { + dc_mprintf( + b"curl --silent -d \"%s\" %s > %s\x00" as *const u8 as *const libc::c_char, + param, + url, + tempFile, + ) + }; + let mut error: libc::c_int = system(cmd); + if error == 0i32 { + let mut bytes: size_t = 0i32 as size_t; + dc_read_file( + context, + tempFile, + &mut ret as *mut *mut libc::c_char as *mut *mut libc::c_void, + &mut bytes, + ); + } + free(cmd as *mut libc::c_void); + free(tempFile as *mut libc::c_void); + free(url as *mut libc::c_void); + return ret as uintptr_t; + } + 2081 => { + printf( + b"\x1b[33m{{Received DC_EVENT_IS_OFFLINE()}}\n\x1b[0m\x00" as *const u8 + as *const libc::c_char, + ); + } + 2000 => { + printf( + b"\x1b[33m{{Received DC_EVENT_MSGS_CHANGED(%i, %i)}}\n\x1b[0m\x00" as *const u8 + as *const libc::c_char, + data1 as libc::c_int, + data2 as libc::c_int, + ); + } + 2030 => { + printf( + b"\x1b[33m{{Received DC_EVENT_CONTACTS_CHANGED()}}\n\x1b[0m\x00" as *const u8 + as *const libc::c_char, + ); + } + 2035 => { + printf( + b"\x1b[33m{{Received DC_EVENT_LOCATION_CHANGED(contact=%i)}}\n\x1b[0m\x00" + as *const u8 as *const libc::c_char, + data1 as libc::c_int, + ); + } + 2041 => { + printf( + b"\x1b[33m{{Received DC_EVENT_CONFIGURE_PROGRESS(%i \xe2\x80\xb0)}}\n\x1b[0m\x00" + as *const u8 as *const libc::c_char, + data1 as libc::c_int, + ); + } + 2051 => { + printf( + b"\x1b[33m{{Received DC_EVENT_IMEX_PROGRESS(%i \xe2\x80\xb0)}}\n\x1b[0m\x00" + as *const u8 as *const libc::c_char, + data1 as libc::c_int, + ); + } + 2052 => { + printf( + b"\x1b[33m{{Received DC_EVENT_IMEX_FILE_WRITTEN(%s)}}\n\x1b[0m\x00" as *const u8 + as *const libc::c_char, + data1 as *mut libc::c_char, + ); + } + 2055 => { + printf( + b"\x1b[33m{{Received DC_EVENT_FILE_COPIED(%s)}}\n\x1b[0m\x00" as *const u8 + as *const libc::c_char, + data1 as *mut libc::c_char, + ); + } + 2020 => { + printf( + b"\x1b[33m{{Received DC_EVENT_CHAT_MODIFIED(%i)}}\n\x1b[0m\x00" as *const u8 + as *const libc::c_char, + data1 as libc::c_int, + ); + } + _ => { + printf( + b"\x1b[33m{{Received DC_EVENT_%i(%i, %i)}}\n\x1b[0m\x00" as *const u8 + as *const libc::c_char, + event as libc::c_int, + data1 as libc::c_int, + data2 as libc::c_int, + ); + } + } + return 0i32 as uintptr_t; +} +/* ****************************************************************************** + * Threads for waiting for messages and for jobs + ******************************************************************************/ +static mut run_threads: libc::c_int = 0i32; -// unsafe extern "C" fn inbox_thread_entry_point( -// mut entry_arg: *mut libc::c_void, -// ) -> *mut libc::c_void { -// let mut context: &dc_context_t = entry_arg as *mut dc_context_t; -// while 0 != run_threads { -// dc_perform_imap_jobs(context); -// dc_perform_imap_fetch(context); -// if 0 != run_threads { -// dc_perform_imap_idle(context); -// } -// } -// return 0 as *mut libc::c_void; -// } -// static mut mvbox_thread: pthread_t = 0 as pthread_t; -// unsafe extern "C" fn mvbox_thread_entry_point( -// mut entry_arg: *mut libc::c_void, -// ) -> *mut libc::c_void { -// let mut context: &dc_context_t = entry_arg as *mut dc_context_t; -// while 0 != run_threads { -// dc_perform_mvbox_fetch(context); -// if 0 != run_threads { -// dc_perform_mvbox_idle(context); -// } -// } -// return 0 as *mut libc::c_void; -// } -// static mut sentbox_thread: pthread_t = 0 as pthread_t; -// unsafe extern "C" fn sentbox_thread_entry_point( -// mut entry_arg: *mut libc::c_void, -// ) -> *mut libc::c_void { -// let mut context: &dc_context_t = entry_arg as *mut dc_context_t; -// while 0 != run_threads { -// dc_perform_sentbox_fetch(context); -// if 0 != run_threads { -// dc_perform_sentbox_idle(context); -// } -// } -// return 0 as *mut libc::c_void; -// } -// static mut smtp_thread: pthread_t = 0 as pthread_t; -// unsafe extern "C" fn smtp_thread_entry_point( -// mut entry_arg: *mut libc::c_void, -// ) -> *mut libc::c_void { -// let mut context: &dc_context_t = entry_arg as *mut dc_context_t; -// while 0 != run_threads { -// dc_perform_smtp_jobs(context); -// if 0 != run_threads { -// dc_perform_smtp_idle(context); -// } -// } -// return 0 as *mut libc::c_void; -// } -// unsafe extern "C" fn start_threads(mut context: &dc_context_t) { -// run_threads = 1i32; -// if inbox_thread == 0 { -// pthread_create( -// &mut inbox_thread, -// 0 as *const pthread_attr_t, -// Some(inbox_thread_entry_point), -// context as *mut libc::c_void, -// ); -// } -// if mvbox_thread == 0 { -// pthread_create( -// &mut mvbox_thread, -// 0 as *const pthread_attr_t, -// Some(mvbox_thread_entry_point), -// context as *mut libc::c_void, -// ); -// } -// if sentbox_thread == 0 { -// pthread_create( -// &mut sentbox_thread, -// 0 as *const pthread_attr_t, -// Some(sentbox_thread_entry_point), -// context as *mut libc::c_void, -// ); -// } -// if smtp_thread == 0 { -// pthread_create( -// &mut smtp_thread, -// 0 as *const pthread_attr_t, -// Some(smtp_thread_entry_point), -// context as *mut libc::c_void, -// ); -// }; -// } -// unsafe extern "C" fn stop_threads(mut context: &dc_context_t) { -// run_threads = 0i32; -// dc_interrupt_imap_idle(context); -// dc_interrupt_mvbox_idle(context); -// dc_interrupt_sentbox_idle(context); -// dc_interrupt_smtp_idle(context); -// pthread_join(inbox_thread, 0 as *mut *mut libc::c_void); -// pthread_join(mvbox_thread, 0 as *mut *mut libc::c_void); -// pthread_join(sentbox_thread, 0 as *mut *mut libc::c_void); -// pthread_join(smtp_thread, 0 as *mut *mut libc::c_void); -// inbox_thread = 0 as pthread_t; -// mvbox_thread = 0 as pthread_t; -// sentbox_thread = 0 as pthread_t; -// smtp_thread = 0 as pthread_t; -// } -// /* ****************************************************************************** -// * The main loop -// ******************************************************************************/ -// fn read_cmd() -> String { -// print!("> "); -// io::stdout().flush().unwrap(); +unsafe fn start_threads( + c: Arc>, +) -> ( + std::thread::JoinHandle<()>, + std::thread::JoinHandle<()>, + std::thread::JoinHandle<()>, + std::thread::JoinHandle<()>, +) { + println!("staring thread"); + 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 { + dc_perform_imap_jobs(&context); + dc_perform_imap_fetch(&context); + if 0 != run_threads { + dc_perform_imap_idle(&context); + } + } + }); -// let mut input = String::new(); -// io::stdin().read_line(&mut input).unwrap(); -// input.trim_end().to_string() -// } + 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); + } + } + }); -// #[cfg(not(target_os = "android"))] -// unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> libc::c_int { -// let mut cmd: *mut libc::c_char = 0 as *mut libc::c_char; -// let mut context: &dc_context_t = dc_context_new( -// receive_event, -// 0 as *mut libc::c_void, -// b"CLI\x00" as *const u8 as *const libc::c_char, -// ); -// let mut stresstest_only: libc::c_int = 0i32; -// dc_cmdline_skip_auth(); -// if argc == 2i32 { -// if strcmp( -// *argv.offset(1isize), -// b"--stress\x00" as *const u8 as *const libc::c_char, -// ) == 0i32 -// { -// stresstest_only = 1i32 -// } else if 0 == dc_open(context, *argv.offset(1isize), 0 as *const libc::c_char) { -// printf( -// b"ERROR: Cannot open %s.\n\x00" as *const u8 as *const libc::c_char, -// *argv.offset(1isize), -// ); -// } -// } else if argc != 1i32 { -// printf(b"ERROR: Bad arguments\n\x00" as *const u8 as *const libc::c_char); -// } -// s_do_log_info = 0i32; -// stress_functions(context); -// s_do_log_info = 1i32; -// if 0 != stresstest_only { -// return 0i32; -// } -// printf(b"Delta Chat Core is awaiting your commands.\n\x00" as *const u8 as *const libc::c_char); -// loop { -// /* read command */ -// let cmdline = read_cmd(); -// free(cmd as *mut libc::c_void); -// cmd = dc_strdup(CString::new(cmdline.clone()).unwrap().as_ptr()); -// let mut arg1: *mut libc::c_char = strchr(cmd, ' ' as i32); -// if !arg1.is_null() { -// *arg1 = 0i32 as libc::c_char; -// arg1 = arg1.offset(1isize) -// } -// if strcmp(cmd, b"connect\x00" as *const u8 as *const libc::c_char) == 0i32 { -// start_threads(context); -// } else if strcmp(cmd, b"disconnect\x00" as *const u8 as *const libc::c_char) == 0i32 { -// stop_threads(context); -// } else if strcmp(cmd, b"smtp-jobs\x00" as *const u8 as *const libc::c_char) == 0i32 { -// if 0 != run_threads { -// printf( -// b"smtp-jobs are already running in a thread.\n\x00" as *const u8 -// as *const libc::c_char, -// ); -// } else { -// dc_perform_smtp_jobs(context); -// } -// } else if strcmp(cmd, b"imap-jobs\x00" as *const u8 as *const libc::c_char) == 0i32 { -// if 0 != run_threads { -// printf( -// b"imap-jobs are already running in a thread.\n\x00" as *const u8 -// as *const libc::c_char, -// ); -// } else { -// dc_perform_imap_jobs(context); -// } -// } else if strcmp(cmd, b"configure\x00" as *const u8 as *const libc::c_char) == 0i32 { -// start_threads(context); -// dc_configure(context); -// } 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(context, b"addr\x00" as *const u8 as *const libc::c_char); -// if addr.is_null() || *addr.offset(0isize) as libc::c_int == 0i32 { -// printf(b"oauth2: set addr first.\n\x00" as *const u8 as *const libc::c_char); -// } else { -// let mut oauth2_url: *mut libc::c_char = dc_get_oauth2_url( -// context, -// addr, -// b"chat.delta:/com.b44t.messenger\x00" as *const u8 as *const libc::c_char, -// ); -// if oauth2_url.is_null() { -// printf( -// b"OAuth2 not available for %s.\n\x00" as *const u8 as *const libc::c_char, -// addr, -// ); -// } else { -// printf(b"Open the following url, set mail_pw to the generated token and server_flags to 2:\n%s\n\x00" -// as *const u8 as *const libc::c_char, -// oauth2_url); -// } -// free(oauth2_url as *mut libc::c_void); -// } -// free(addr as *mut libc::c_void); -// } else if strcmp(cmd, b"clear\x00" as *const u8 as *const libc::c_char) == 0i32 { -// printf(b"\n\n\n\n\x00" as *const u8 as *const libc::c_char); -// printf(b"\x1b[1;1H\x1b[2J\x00" as *const u8 as *const libc::c_char); -// } 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 -// { -// start_threads(context); -// let mut qrstr: *mut libc::c_char = dc_get_securejoin_qr( -// context, -// (if !arg1.is_null() { atoi(arg1) } else { 0i32 }) as uint32_t, -// ); -// if !qrstr.is_null() && 0 != *qrstr.offset(0isize) as libc::c_int { -// if strcmp(cmd, b"getbadqr\x00" as *const u8 as *const libc::c_char) == 0i32 -// && strlen(qrstr) > 40 -// { -// let mut i: libc::c_int = 12i32; -// while i < 22i32 { -// *qrstr.offset(i as isize) = '0' as i32 as libc::c_char; -// i += 1 -// } -// } -// printf(b"%s\n\x00" as *const u8 as *const libc::c_char, qrstr); -// let mut syscmd: *mut libc::c_char = dc_mprintf( -// b"qrencode -t ansiutf8 \"%s\" -o -\x00" as *const u8 as *const libc::c_char, -// qrstr, -// ); -// system(syscmd); -// free(syscmd as *mut libc::c_void); -// } -// free(qrstr as *mut libc::c_void); -// } else if strcmp(cmd, b"joinqr\x00" as *const u8 as *const libc::c_char) == 0i32 { -// start_threads(context); -// if !arg1.is_null() { -// dc_join_securejoin(context, arg1); -// } -// } else { -// if strcmp(cmd, b"exit\x00" as *const u8 as *const libc::c_char) == 0i32 { -// break; -// } -// if !(*cmd.offset(0isize) as libc::c_int == 0i32) { -// let mut execute_result: *mut libc::c_char = dc_cmdline(context, &cmdline); -// if !execute_result.is_null() { -// printf( -// b"%s\n\x00" as *const u8 as *const libc::c_char, -// execute_result, -// ); -// free(execute_result as *mut libc::c_void); -// } -// } -// } -// } -// free(cmd as *mut libc::c_void); -// stop_threads(context); -// dc_close(context); -// dc_context_unref(context); -// context = 0 as &dc_context_t; -// return 0i32; -// } + 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); + } + } + }); -// pub fn main() { -// let mut args: Vec<*mut libc::c_char> = Vec::new(); -// for arg in ::std::env::args() { -// args.push( -// ::std::ffi::CString::new(arg) -// .expect("Failed to convert argument into CString.") -// .into_raw(), -// ); -// } -// args.push(::std::ptr::null_mut()); + 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 res = unsafe { -// main_0( -// (args.len() - 1) as libc::c_int, -// args.as_mut_ptr() as *mut *mut libc::c_char, -// ) -// }; -// ::std::process::exit(res) -// } + (h1, h2, h3, h4) +} -fn main() {} +unsafe fn stop_threads( + context: &dc_context_t, + handles: Option<( + std::thread::JoinHandle<()>, + std::thread::JoinHandle<()>, + std::thread::JoinHandle<()>, + std::thread::JoinHandle<()>, + )>, +) { + run_threads = 0i32; + dc_interrupt_imap_idle(context); + dc_interrupt_mvbox_idle(context); + dc_interrupt_sentbox_idle(context); + dc_interrupt_smtp_idle(context); + if let Some((h1, h2, h3, h4)) = handles { + h1.join().unwrap(); + h2.join().unwrap(); + h3.join().unwrap(); + h4.join().unwrap(); + } +} + +/* ****************************************************************************** + * The main loop + ******************************************************************************/ +fn read_cmd() -> String { + print!("> "); + io::stdout().flush().unwrap(); + + let mut input = String::new(); + io::stdin().read_line(&mut input).unwrap(); + input.trim_end().to_string() +} + +unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> libc::c_int { + let mut cmd: *mut libc::c_char = 0 as *mut libc::c_char; + let mut context = dc_context_new( + receive_event, + 0 as *mut libc::c_void, + b"CLI\x00" as *const u8 as *const libc::c_char, + ); + let mut stresstest_only: libc::c_int = 0i32; + dc_cmdline_skip_auth(); + if argc == 2i32 { + if strcmp( + *argv.offset(1isize), + b"--stress\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + stresstest_only = 1i32 + } else if 0 == dc_open(&mut context, *argv.offset(1isize), 0 as *const libc::c_char) { + printf( + b"ERROR: Cannot open %s.\n\x00" as *const u8 as *const libc::c_char, + *argv.offset(1isize), + ); + } + } else if argc != 1i32 { + printf(b"ERROR: Bad arguments\n\x00" as *const u8 as *const libc::c_char); + } + s_do_log_info = 0i32; + stress_functions(&context); + s_do_log_info = 1i32; + if 0 != stresstest_only { + return 0i32; + } + printf(b"Delta Chat Core is awaiting your commands.\n\x00" as *const u8 as *const libc::c_char); + let mut handles = None; + + 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); + cmd = dc_strdup(CString::new(cmdline.clone()).unwrap().as_ptr()); + let mut arg1: *mut libc::c_char = strchr(cmd, ' ' as i32); + if !arg1.is_null() { + *arg1 = 0i32 as libc::c_char; + arg1 = arg1.offset(1isize) + } + if strcmp(cmd, b"connect\x00" as *const u8 as *const libc::c_char) == 0i32 { + let ctx = ctx.clone(); + handles = Some(start_threads(ctx)); + } else if strcmp(cmd, b"disconnect\x00" as *const u8 as *const libc::c_char) == 0i32 { + stop_threads(&ctx.read().unwrap(), handles); + handles = None; + } else if strcmp(cmd, b"smtp-jobs\x00" as *const u8 as *const libc::c_char) == 0i32 { + if 0 != run_threads { + printf( + b"smtp-jobs are already running in a thread.\n\x00" as *const u8 + as *const libc::c_char, + ); + } else { + dc_perform_smtp_jobs(&ctx.read().unwrap()); + } + } else if strcmp(cmd, b"imap-jobs\x00" as *const u8 as *const libc::c_char) == 0i32 { + if 0 != run_threads { + printf( + b"imap-jobs are already running in a thread.\n\x00" as *const u8 + as *const libc::c_char, + ); + } else { + 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)) + }; + 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( + &ctx.read().unwrap(), + b"addr\x00" as *const u8 as *const libc::c_char, + ); + if addr.is_null() || *addr.offset(0isize) as libc::c_int == 0i32 { + printf(b"oauth2: set addr first.\n\x00" as *const u8 as *const libc::c_char); + } else { + let mut oauth2_url: *mut libc::c_char = dc_get_oauth2_url( + &ctx.read().unwrap(), + addr, + b"chat.delta:/com.b44t.messenger\x00" as *const u8 as *const libc::c_char, + ); + if oauth2_url.is_null() { + printf( + b"OAuth2 not available for %s.\n\x00" as *const u8 as *const libc::c_char, + addr, + ); + } else { + printf(b"Open the following url, set mail_pw to the generated token and server_flags to 2:\n%s\n\x00" + as *const u8 as *const libc::c_char, + oauth2_url); + } + free(oauth2_url as *mut libc::c_void); + } + free(addr as *mut libc::c_void); + } else if strcmp(cmd, b"clear\x00" as *const u8 as *const libc::c_char) == 0i32 { + printf(b"\n\n\n\n\x00" as *const u8 as *const libc::c_char); + printf(b"\x1b[1;1H\x1b[2J\x00" as *const u8 as *const libc::c_char); + } 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)) + }; + 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, + ); + if !qrstr.is_null() && 0 != *qrstr.offset(0isize) as libc::c_int { + if strcmp(cmd, b"getbadqr\x00" as *const u8 as *const libc::c_char) == 0i32 + && strlen(qrstr) > 40 + { + let mut i: libc::c_int = 12i32; + while i < 22i32 { + *qrstr.offset(i as isize) = '0' as i32 as libc::c_char; + i += 1 + } + } + printf(b"%s\n\x00" as *const u8 as *const libc::c_char, qrstr); + let mut syscmd: *mut libc::c_char = dc_mprintf( + b"qrencode -t ansiutf8 \"%s\" -o -\x00" as *const u8 as *const libc::c_char, + qrstr, + ); + system(syscmd); + free(syscmd as *mut libc::c_void); + } + 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)) + }; + if !arg1.is_null() { + dc_join_securejoin(&ctx.read().unwrap(), arg1); + } + } else { + if strcmp(cmd, b"exit\x00" as *const u8 as *const libc::c_char) == 0i32 { + break; + } + if !(*cmd.offset(0isize) as libc::c_int == 0i32) { + let ctx = ctx.clone(); + let mut execute_result: *mut libc::c_char = dc_cmdline(ctx, &cmdline); + if !execute_result.is_null() { + printf( + b"%s\n\x00" as *const u8 as *const libc::c_char, + execute_result, + ); + free(execute_result as *mut libc::c_void); + } + } + } + } + + let ctx = ctx.clone(); + + { + 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); + } + 0 +} + +pub fn main() { + let mut args: Vec<*mut libc::c_char> = Vec::new(); + for arg in ::std::env::args() { + args.push( + ::std::ffi::CString::new(arg) + .expect("Failed to convert argument into CString.") + .into_raw(), + ); + } + args.push(::std::ptr::null_mut()); + + let res = unsafe { + main_0( + (args.len() - 1) as libc::c_int, + args.as_mut_ptr() as *mut *mut libc::c_char, + ) + }; + ::std::process::exit(res) +} diff --git a/examples/repl/stress.rs b/examples/repl/stress.rs index 676626012..738690708 100644 --- a/examples/repl/stress.rs +++ b/examples/repl/stress.rs @@ -2445,7 +2445,7 @@ pub unsafe extern "C" fn stress_functions(mut context: &dc_context_t) { ); } else { }; - let mut arr: &dc_array_t = dc_array_new(0 as *mut dc_context_t, 7i32 as size_t); + let mut 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"))