From 31e83160f268a47776ee06f0ea72c7f0b09bdb87 Mon Sep 17 00:00:00 2001 From: link2xt Date: Wed, 22 Jul 2026 12:50:53 +0000 Subject: [PATCH] build: update all crates to Rust 2024 edition Largest change in the FFI crate. With 2024 (but not 2021) edition unsafe code inside unsafe functions should be marked separately so we can mark exactly the code that is unsafe. Some CFFI functions even have no unsafe code inside. Most interesting change is that .strdup() functions are not marked as unsafe anymore. They are allocating memory and return raw pointers, but there is nothing unsafe about it. Only using the returned raw pointers is unsafe. This way calls to .strdup() don't have to be marked with unsafe{} blocks. --- deltachat-contact-tools/Cargo.toml | 2 +- deltachat-contact-tools/src/lib.rs | 4 +- .../src/vcard/vcard_tests.rs | 20 +- deltachat-ffi/Cargo.toml | 2 +- deltachat-ffi/src/lib.rs | 3058 +++++++++-------- deltachat-ffi/src/string.rs | 58 +- deltachat-jsonrpc/Cargo.toml | 2 +- deltachat-jsonrpc/src/api.rs | 24 +- deltachat-jsonrpc/src/api/types/calls.rs | 2 +- deltachat-jsonrpc/src/api/types/chat.rs | 4 +- deltachat-jsonrpc/src/api/types/chat_list.rs | 2 +- deltachat-jsonrpc/src/api/types/http.rs | 2 +- deltachat-ratelimit/Cargo.toml | 2 +- deltachat-repl/Cargo.toml | 2 +- deltachat-repl/src/cmdline.rs | 11 +- deltachat-repl/src/main.rs | 4 +- deltachat-rpc-server/Cargo.toml | 2 +- deltachat-rpc-server/src/main.rs | 2 +- deltachat-time/Cargo.toml | 2 +- deltachat_derive/Cargo.toml | 2 +- deltachat_derive/src/lib.rs | 8 +- format-flowed/Cargo.toml | 2 +- format-flowed/src/lib.rs | 6 +- fuzz/Cargo.toml | 2 +- 24 files changed, 1648 insertions(+), 1577 deletions(-) diff --git a/deltachat-contact-tools/Cargo.toml b/deltachat-contact-tools/Cargo.toml index ec1f4c7561..6bfbe7a7a9 100644 --- a/deltachat-contact-tools/Cargo.toml +++ b/deltachat-contact-tools/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "deltachat-contact-tools" version = "0.0.0" # No semver-stable versioning -edition = "2021" +edition = "2024" description = "Contact-related tools, like parsing vcards and sanitizing name and address. Meant for internal use in the deltachat crate." license = "MPL-2.0" diff --git a/deltachat-contact-tools/src/lib.rs b/deltachat-contact-tools/src/lib.rs index 1a958d51f3..e7a78bb6e2 100644 --- a/deltachat-contact-tools/src/lib.rs +++ b/deltachat-contact-tools/src/lib.rs @@ -31,12 +31,12 @@ use std::fmt; use std::ops::Deref; use std::sync::LazyLock; -use anyhow::bail; use anyhow::Result; +use anyhow::bail; use regex::Regex; mod vcard; -pub use vcard::{make_vcard, parse_vcard, VcardContact}; +pub use vcard::{VcardContact, make_vcard, parse_vcard}; /// Valid contact address. #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/deltachat-contact-tools/src/vcard/vcard_tests.rs b/deltachat-contact-tools/src/vcard/vcard_tests.rs index 5ebc8d6165..21cfc4efac 100644 --- a/deltachat-contact-tools/src/vcard/vcard_tests.rs +++ b/deltachat-contact-tools/src/vcard/vcard_tests.rs @@ -220,7 +220,10 @@ END:VCARD assert_eq!(contacts[0].addr, "bob@example.org".to_string()); assert_eq!(contacts[0].authname, "Bob".to_string()); assert_eq!(contacts[0].key, None); - assert_eq!(contacts[0].profile_image.as_deref().unwrap(), "/9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAL8bRuAJYoZUYrI4ZY3VWwxw4Ay28AAGBISScmf/2Q=="); + assert_eq!( + contacts[0].profile_image.as_deref().unwrap(), + "/9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAL8bRuAJYoZUYrI4ZY3VWwxw4Ay28AAGBISScmf/2Q==" + ); } } @@ -244,7 +247,10 @@ END:VCARD", assert_eq!(contacts.len(), 1); assert_eq!(&contacts[0].addr, "alice@example.org"); assert_eq!(&contacts[0].authname, "Alice Wonderland"); - assert_eq!(contacts[0].key.as_ref().unwrap(), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + assert_eq!( + contacts[0].key.as_ref().unwrap(), + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + ); assert!(contacts[0].timestamp.is_err()); assert_eq!(contacts[0].profile_image, None); } @@ -272,9 +278,15 @@ END:VCARD", assert_eq!(contacts.len(), 1); assert_eq!(&contacts[0].addr, "alice@example.org"); assert_eq!(&contacts[0].authname, "Alice"); - assert_eq!(contacts[0].key.as_ref().unwrap(), "xsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=="); + assert_eq!( + contacts[0].key.as_ref().unwrap(), + "xsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa==" + ); assert!(contacts[0].timestamp.is_err()); - assert_eq!(contacts[0].profile_image.as_ref().unwrap(), "/9aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Z"); + assert_eq!( + contacts[0].profile_image.as_ref().unwrap(), + "/9aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Z" + ); } #[test] diff --git a/deltachat-ffi/Cargo.toml b/deltachat-ffi/Cargo.toml index 84c42c2bc6..08960f0b3a 100644 --- a/deltachat-ffi/Cargo.toml +++ b/deltachat-ffi/Cargo.toml @@ -2,7 +2,7 @@ name = "deltachat_ffi" version = "2.57.0-dev" description = "Deltachat FFI" -edition = "2018" +edition = "2024" readme = "README.md" license = "MPL-2.0" diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index d3ff6f42eb..9a4a6ec5fd 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -84,41 +84,43 @@ where RT.spawn(fut) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_context_new( _os_name: *const libc::c_char, dbfile: *const libc::c_char, blobdir: *const libc::c_char, ) -> *mut dc_context_t { - setup_panic!(); + unsafe { + setup_panic!(); - if dbfile.is_null() { - eprintln!("ignoring careless call to dc_context_new()"); - return ptr::null_mut(); - } + if dbfile.is_null() { + eprintln!("ignoring careless call to dc_context_new()"); + return ptr::null_mut(); + } - let ctx = if blobdir.is_null() || *blobdir == 0 { - // generate random ID as this functionality is not yet available on the C-api. - let id = rand::random(); - block_on( - ContextBuilder::new(as_path(dbfile).to_path_buf()) - .with_id(id) - .open(), - ) - } else { - eprintln!("blobdir can not be defined explicitly anymore"); - return ptr::null_mut(); - }; - match ctx { - Ok(ctx) => Box::into_raw(Box::new(ctx)), - Err(err) => { - eprintln!("failed to create context: {err:#}"); - ptr::null_mut() + let ctx = if blobdir.is_null() || *blobdir == 0 { + // generate random ID as this functionality is not yet available on the C-api. + let id = rand::random(); + block_on( + ContextBuilder::new(as_path(dbfile).to_path_buf()) + .with_id(id) + .open(), + ) + } else { + eprintln!("blobdir can not be defined explicitly anymore"); + return ptr::null_mut(); + }; + match ctx { + Ok(ctx) => Box::into_raw(Box::new(ctx)), + Err(err) => { + eprintln!("failed to create context: {err:#}"); + ptr::null_mut() + } } } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_context_new_closed(dbfile: *const libc::c_char) -> *mut dc_context_t { setup_panic!(); @@ -141,7 +143,7 @@ pub unsafe extern "C" fn dc_context_new_closed(dbfile: *const libc::c_char) -> * } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_context_open( context: *mut dc_context_t, passphrase: *const libc::c_char, @@ -151,7 +153,7 @@ pub unsafe extern "C" fn dc_context_open( return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; let passphrase = to_string_lossy(passphrase); block_on(ctx.open(passphrase)) .context("dc_context_open() failed") @@ -160,58 +162,66 @@ pub unsafe extern "C" fn dc_context_open( .unwrap_or(0) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_context_change_passphrase( context: *mut dc_context_t, passphrase: *const libc::c_char, ) -> libc::c_int { - if context.is_null() { - eprintln!("ignoring careless call to dc_context_change_passphrase()"); - return 0; - } + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_context_change_passphrase()"); + return 0; + } - let ctx = &*context; - let passphrase = to_string_lossy(passphrase); - block_on(ctx.change_passphrase(passphrase)) - .context("dc_context_change_passphrase() failed") - .log_err(ctx) - .is_ok() as libc::c_int + let ctx = &*context; + let passphrase = to_string_lossy(passphrase); + block_on(ctx.change_passphrase(passphrase)) + .context("dc_context_change_passphrase() failed") + .log_err(ctx) + .is_ok() as libc::c_int + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_context_is_open(context: *mut dc_context_t) -> libc::c_int { - if context.is_null() { - eprintln!("ignoring careless call to dc_context_is_open()"); - return 0; - } + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_context_is_open()"); + return 0; + } - let ctx = &*context; - block_on(ctx.is_open()) as libc::c_int + let ctx = &*context; + block_on(ctx.is_open()) as libc::c_int + } } /// Release the context structure. /// /// This function releases the memory of the `dc_context_t` structure. -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_context_unref(context: *mut dc_context_t) { - if context.is_null() { - eprintln!("ignoring careless call to dc_context_unref()"); - return; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_context_unref()"); + return; + } + drop(Box::from_raw(context)); } - drop(Box::from_raw(context)); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_blobdir(context: *mut dc_context_t) -> *mut libc::c_char { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_blobdir()"); - return "".strdup(); + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_blobdir()"); + return "".strdup(); + } + let ctx = &*context; + ctx.get_blobdir().to_string_lossy().strdup() } - let ctx = &*context; - ctx.get_blobdir().to_string_lossy().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_config( context: *mut dc_context_t, key: *const libc::c_char, @@ -221,7 +231,7 @@ pub unsafe extern "C" fn dc_set_config( eprintln!("ignoring careless call to dc_set_config()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; let key = to_string_lossy(key); let value = to_opt_string_lossy(value); @@ -251,7 +261,7 @@ pub unsafe extern "C" fn dc_set_config( }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_config( context: *mut dc_context_t, key: *const libc::c_char, @@ -260,7 +270,7 @@ pub unsafe extern "C" fn dc_get_config( eprintln!("ignoring careless call to dc_get_config()"); return "".strdup(); } - let ctx = &*context; + let ctx = unsafe { &*context }; let key = to_string_lossy(key); @@ -292,7 +302,7 @@ pub unsafe extern "C" fn dc_get_config( }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_stock_translation( context: *mut dc_context_t, stock_id: u32, @@ -303,7 +313,7 @@ pub unsafe extern "C" fn dc_set_stock_translation( return 0; } let msg = to_string_lossy(stock_msg); - let ctx = &*context; + let ctx = unsafe { &*context }; match StockMessage::from_u32(stock_id) .with_context(|| format!("Invalid stock message ID {stock_id}")) @@ -318,7 +328,7 @@ pub unsafe extern "C" fn dc_set_stock_translation( } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_config_from_qr( context: *mut dc_context_t, qr: *mut libc::c_char, @@ -327,8 +337,9 @@ pub unsafe extern "C" fn dc_set_config_from_qr( eprintln!("ignoring careless call to dc_set_config_from_qr"); return 0; } + let qr = to_string_lossy(qr); - let ctx = &*context; + let ctx = unsafe { &*context }; block_on(qr::set_config_from_qr(ctx, &qr)) .context("Failed to create account from QR code") @@ -336,18 +347,21 @@ pub unsafe extern "C" fn dc_set_config_from_qr( .is_ok() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_info(context: *const dc_context_t) -> *mut libc::c_char { if context.is_null() { eprintln!("ignoring careless call to dc_get_info()"); return "".strdup(); } - let ctx = &*context; + let ctx = unsafe { &*context }; match block_on(ctx.get_info()) .context("Failed to get info") .log_err(ctx) { - Ok(info) => render_info(info).unwrap_or_default().strdup(), + Ok(info) => { + let info = render_info(info).unwrap_or_default(); + info.strdup() + } Err(_) => "".strdup(), } } @@ -363,17 +377,17 @@ fn render_info( Ok(res) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_connectivity(context: *const dc_context_t) -> libc::c_int { if context.is_null() { eprintln!("ignoring careless call to dc_get_connectivity()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; ctx.get_connectivity() as u32 as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_connectivity_html( context: *const dc_context_t, ) -> *mut libc::c_char { @@ -381,7 +395,7 @@ pub unsafe extern "C" fn dc_get_connectivity_html( eprintln!("ignoring careless call to dc_get_connectivity_html()"); return "".strdup(); } - let ctx = &*context; + let ctx = unsafe { &*context }; match block_on(ctx.get_connectivity_html()) .context("Failed to get connectivity html") .log_err(ctx) @@ -391,13 +405,13 @@ pub unsafe extern "C" fn dc_get_connectivity_html( } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_push_state(context: *const dc_context_t) -> libc::c_int { if context.is_null() { eprintln!("ignoring careless call to dc_get_push_state()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; block_on(ctx.push_state()) as libc::c_int } @@ -410,24 +424,24 @@ fn spawn_configure(ctx: Context) { }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_configure(context: *mut dc_context_t) { if context.is_null() { eprintln!("ignoring careless call to dc_configure()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; spawn_configure(ctx.clone()); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_is_configured(context: *mut dc_context_t) -> libc::c_int { if context.is_null() { eprintln!("ignoring careless call to dc_is_configured()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; block_on(async move { ctx.is_configured() @@ -438,113 +452,115 @@ pub unsafe extern "C" fn dc_is_configured(context: *mut dc_context_t) -> libc::c }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_start_io(context: *mut dc_context_t) { if context.is_null() { return; } - let ctx = &mut *context; + let ctx = unsafe { &mut *context }; block_on(ctx.start_io()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_id(context: *mut dc_context_t) -> libc::c_int { if context.is_null() { return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; ctx.get_id() as libc::c_int } pub type dc_event_t = Event; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_unref(a: *mut dc_event_t) { if a.is_null() { eprintln!("ignoring careless call to dc_event_unref()"); return; } - drop(Box::from_raw(a)); + drop(unsafe { Box::from_raw(a) }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_get_id(event: *mut dc_event_t) -> libc::c_int { - if event.is_null() { - eprintln!("ignoring careless call to dc_event_get_id()"); - return 0; - } + unsafe { + if event.is_null() { + eprintln!("ignoring careless call to dc_event_get_id()"); + return 0; + } - let event = &*event; - match event.typ { - EventType::Info(_) => 100, - EventType::SmtpConnected(_) => 101, - EventType::ImapConnected(_) => 102, - EventType::SmtpMessageSent(_) => 103, - EventType::ImapMessageDeleted(_) => 104, - EventType::ImapMessageMoved(_) => 105, - EventType::ImapInboxIdle => 106, - EventType::NewBlobFile(_) => 150, - EventType::DeletedBlobFile(_) => 151, - EventType::Warning(_) => 300, - EventType::Error(_) => 400, - EventType::ErrorSelfNotInGroup(_) => 410, - EventType::MsgsChanged { .. } => 2000, - EventType::ReactionsChanged { .. } => 2001, - EventType::IncomingReaction { .. } => 2002, - EventType::IncomingWebxdcNotify { .. } => 2003, - EventType::IncomingMsg { .. } => 2005, - EventType::IncomingMsgBunch => 2006, - EventType::MsgsNoticed { .. } => 2008, - EventType::MsgDelivered { .. } => 2010, - EventType::MsgFailed { .. } => 2012, - EventType::MsgRead { .. } => 2015, - EventType::MsgDeleted { .. } => 2016, - EventType::MsgReadCountChanged { .. } => 2018, - EventType::ChatModified(_) => 2020, - EventType::ChatEphemeralTimerModified { .. } => 2021, - EventType::ChatDeleted { .. } => 2023, - EventType::ContactsChanged(_) => 2030, - EventType::LocationChanged(_) => 2035, - EventType::ConfigureProgress { .. } => 2041, - EventType::ImexProgress(_) => 2051, - EventType::ImexFileWritten(_) => 2052, - EventType::SecurejoinInviterProgress { .. } => 2060, - EventType::SecurejoinJoinerProgress { .. } => 2061, - EventType::ConnectivityChanged => 2100, - EventType::SelfavatarChanged => 2110, - EventType::ConfigSynced { .. } => 2111, - EventType::WebxdcStatusUpdate { .. } => 2120, - EventType::WebxdcInstanceDeleted { .. } => 2121, - EventType::WebxdcRealtimeData { .. } => 2150, - EventType::WebxdcRealtimeAdvertisementReceived { .. } => 2151, - EventType::AccountsBackgroundFetchDone => 2200, - EventType::ChatlistChanged => 2300, - EventType::ChatlistItemChanged { .. } => 2301, - EventType::AccountsChanged => 2302, - EventType::AccountsItemChanged => 2303, - EventType::EventChannelOverflow { .. } => 2400, - EventType::IncomingCall { .. } => 2550, - EventType::IncomingCallAccepted { .. } => 2560, - EventType::OutgoingCallAccepted { .. } => 2570, - EventType::CallEnded { .. } => 2580, - EventType::TransportsModified => 2600, - #[allow(unreachable_patterns)] - #[cfg(test)] - _ => unreachable!("This is just to silence a rust_analyzer false-positive"), + let event = &*event; + match event.typ { + EventType::Info(_) => 100, + EventType::SmtpConnected(_) => 101, + EventType::ImapConnected(_) => 102, + EventType::SmtpMessageSent(_) => 103, + EventType::ImapMessageDeleted(_) => 104, + EventType::ImapMessageMoved(_) => 105, + EventType::ImapInboxIdle => 106, + EventType::NewBlobFile(_) => 150, + EventType::DeletedBlobFile(_) => 151, + EventType::Warning(_) => 300, + EventType::Error(_) => 400, + EventType::ErrorSelfNotInGroup(_) => 410, + EventType::MsgsChanged { .. } => 2000, + EventType::ReactionsChanged { .. } => 2001, + EventType::IncomingReaction { .. } => 2002, + EventType::IncomingWebxdcNotify { .. } => 2003, + EventType::IncomingMsg { .. } => 2005, + EventType::IncomingMsgBunch => 2006, + EventType::MsgsNoticed { .. } => 2008, + EventType::MsgDelivered { .. } => 2010, + EventType::MsgFailed { .. } => 2012, + EventType::MsgRead { .. } => 2015, + EventType::MsgDeleted { .. } => 2016, + EventType::MsgReadCountChanged { .. } => 2018, + EventType::ChatModified(_) => 2020, + EventType::ChatEphemeralTimerModified { .. } => 2021, + EventType::ChatDeleted { .. } => 2023, + EventType::ContactsChanged(_) => 2030, + EventType::LocationChanged(_) => 2035, + EventType::ConfigureProgress { .. } => 2041, + EventType::ImexProgress(_) => 2051, + EventType::ImexFileWritten(_) => 2052, + EventType::SecurejoinInviterProgress { .. } => 2060, + EventType::SecurejoinJoinerProgress { .. } => 2061, + EventType::ConnectivityChanged => 2100, + EventType::SelfavatarChanged => 2110, + EventType::ConfigSynced { .. } => 2111, + EventType::WebxdcStatusUpdate { .. } => 2120, + EventType::WebxdcInstanceDeleted { .. } => 2121, + EventType::WebxdcRealtimeData { .. } => 2150, + EventType::WebxdcRealtimeAdvertisementReceived { .. } => 2151, + EventType::AccountsBackgroundFetchDone => 2200, + EventType::ChatlistChanged => 2300, + EventType::ChatlistItemChanged { .. } => 2301, + EventType::AccountsChanged => 2302, + EventType::AccountsItemChanged => 2303, + EventType::EventChannelOverflow { .. } => 2400, + EventType::IncomingCall { .. } => 2550, + EventType::IncomingCallAccepted { .. } => 2560, + EventType::OutgoingCallAccepted { .. } => 2570, + EventType::CallEnded { .. } => 2580, + EventType::TransportsModified => 2600, + #[allow(unreachable_patterns)] + #[cfg(test)] + _ => unreachable!("This is just to silence a rust_analyzer false-positive"), + } } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_get_data1_int(event: *mut dc_event_t) -> libc::c_int { if event.is_null() { eprintln!("ignoring careless call to dc_event_get_data1_int()"); return 0; } - let event = &(*event).typ; + let event = unsafe { &(*event).typ }; match event { EventType::Info(_) | EventType::SmtpConnected(_) @@ -611,14 +627,14 @@ pub unsafe extern "C" fn dc_event_get_data1_int(event: *mut dc_event_t) -> libc: } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_get_data2_int(event: *mut dc_event_t) -> libc::c_int { if event.is_null() { eprintln!("ignoring careless call to dc_event_get_data2_int()"); return 0; } - let event = &(*event).typ; + let event = unsafe { &(*event).typ }; match event { EventType::Info(_) @@ -685,14 +701,14 @@ pub unsafe extern "C" fn dc_event_get_data2_int(event: *mut dc_event_t) -> libc: } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_get_data1_str(event: *mut dc_event_t) -> *mut libc::c_char { if event.is_null() { eprintln!("ignoring careless call to dc_event_get_data1_str()"); return ptr::null_mut(); } - let event = &(*event).typ; + let event = unsafe { &(*event).typ }; match event { EventType::IncomingWebxdcNotify { href, .. } => { @@ -706,14 +722,14 @@ pub unsafe extern "C" fn dc_event_get_data1_str(event: *mut dc_event_t) -> *mut } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_get_data2_str(event: *mut dc_event_t) -> *mut libc::c_char { if event.is_null() { eprintln!("ignoring careless call to dc_event_get_data2_str()"); return ptr::null_mut(); } - let event = &(*event).typ; + let event = unsafe { &(*event).typ }; match event { EventType::Info(msg) @@ -790,8 +806,8 @@ pub unsafe extern "C" fn dc_event_get_data2_str(event: *mut dc_event_t) -> *mut data2.into_raw() } EventType::WebxdcRealtimeData { data, .. } => { - let ptr = libc::malloc(data.len()); - libc::memcpy(ptr, data.as_ptr() as *mut libc::c_void, data.len()); + let ptr = unsafe { libc::malloc(data.len()) }; + unsafe { libc::memcpy(ptr, data.as_ptr() as *mut libc::c_void, data.len()) }; ptr as *mut libc::c_char } EventType::IncomingReaction { reaction, .. } => reaction @@ -808,19 +824,19 @@ pub unsafe extern "C" fn dc_event_get_data2_str(event: *mut dc_event_t) -> *mut } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_get_account_id(event: *mut dc_event_t) -> u32 { if event.is_null() { eprintln!("ignoring careless call to dc_event_get_account_id()"); return 0; } - (*event).id + unsafe { (*event).id } } pub type dc_event_emitter_t = EventEmitter; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_event_emitter( context: *mut dc_context_t, ) -> *mut dc_event_emitter_t { @@ -828,27 +844,29 @@ pub unsafe extern "C" fn dc_get_event_emitter( eprintln!("ignoring careless call to dc_get_event_emitter()"); return ptr::null_mut(); } - let ctx = &*context; - Box::into_raw(Box::new(ctx.get_event_emitter())) + unsafe { + let ctx = &*context; + Box::into_raw(Box::new(ctx.get_event_emitter())) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_emitter_unref(emitter: *mut dc_event_emitter_t) { if emitter.is_null() { eprintln!("ignoring careless call to dc_event_emitter_unref()"); return; } - drop(Box::from_raw(emitter)); + drop(unsafe { Box::from_raw(emitter) }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_next_event(events: *mut dc_event_emitter_t) -> *mut dc_event_t { if events.is_null() { eprintln!("ignoring careless call to dc_get_next_event()"); return ptr::null_mut(); } - let events = &*events; + let events = unsafe { &*events }; block_on(async move { events @@ -859,31 +877,31 @@ pub unsafe extern "C" fn dc_get_next_event(events: *mut dc_event_emitter_t) -> * }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_stop_io(context: *mut dc_context_t) { if context.is_null() { eprintln!("ignoring careless call to dc_stop_io()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; block_on(async move { ctx.stop_io().await; }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_maybe_network(context: *mut dc_context_t) { if context.is_null() { eprintln!("ignoring careless call to dc_maybe_network()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; block_on(async move { ctx.maybe_network().await }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_preconfigure_keypair( context: *mut dc_context_t, secret_data: *const libc::c_char, @@ -892,7 +910,7 @@ pub unsafe extern "C" fn dc_preconfigure_keypair( eprintln!("ignoring careless call to dc_preconfigure_keypair()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; let secret_data = to_string_lossy(secret_data); block_on(preconfigure_keypair(ctx, &secret_data)) .context("Failed to save keypair") @@ -900,7 +918,7 @@ pub unsafe extern "C" fn dc_preconfigure_keypair( .is_ok() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_chatlist( context: *mut dc_context_t, flags: libc::c_int, @@ -911,7 +929,7 @@ pub unsafe extern "C" fn dc_get_chatlist( eprintln!("ignoring careless call to dc_get_chatlist()"); return ptr::null_mut(); } - let ctx = &*context; + let ctx = unsafe { &*context }; let qs = to_opt_string_lossy(query_str); let qi = if query_id == 0 { @@ -920,22 +938,24 @@ pub unsafe extern "C" fn dc_get_chatlist( Some(ContactId::new(query_id)) }; - block_on(async move { - match chatlist::Chatlist::try_load(ctx, flags as usize, qs.as_deref(), qi) - .await - .context("Failed to get chatlist") - .log_err(ctx) - { - Ok(list) => { - let ffi_list = ChatlistWrapper { context, list }; - Box::into_raw(Box::new(ffi_list)) - } - Err(_) => ptr::null_mut(), + match block_on(chatlist::Chatlist::try_load( + ctx, + flags as usize, + qs.as_deref(), + qi, + )) + .context("Failed to get chatlist") + .log_err(ctx) + { + Ok(list) => { + let ffi_list = ChatlistWrapper { context, list }; + Box::into_raw(Box::new(ffi_list)) } - }) + Err(_) => ptr::null_mut(), + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_create_chat_by_contact_id( context: *mut dc_context_t, contact_id: u32, @@ -944,19 +964,16 @@ pub unsafe extern "C" fn dc_create_chat_by_contact_id( eprintln!("ignoring careless call to dc_create_chat_by_contact_id()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; - block_on(async move { - ChatId::create_for_contact(ctx, ContactId::new(contact_id)) - .await - .context("Failed to create chat from contact_id") - .log_err(ctx) - .map(|id| id.to_u32()) - .unwrap_or(0) - }) + block_on(ChatId::create_for_contact(ctx, ContactId::new(contact_id))) + .context("Failed to create chat from contact_id") + .log_err(ctx) + .map(|id| id.to_u32()) + .unwrap_or(0) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_chat_id_by_contact_id( context: *mut dc_context_t, contact_id: u32, @@ -965,20 +982,17 @@ pub unsafe extern "C" fn dc_get_chat_id_by_contact_id( eprintln!("ignoring careless call to dc_get_chat_id_by_contact_id()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; - block_on(async move { - ChatId::lookup_by_contact(ctx, ContactId::new(contact_id)) - .await - .context("Failed to get chat for contact_id") - .log_err(ctx) - .unwrap_or_default() // unwraps the Result - .map(|id| id.to_u32()) - .unwrap_or(0) // unwraps the Option - }) + block_on(ChatId::lookup_by_contact(ctx, ContactId::new(contact_id))) + .context("Failed to get chat for contact_id") + .log_err(ctx) + .unwrap_or_default() // unwraps the Result + .map(|id| id.to_u32()) + .unwrap_or(0) // unwraps the Option } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_send_msg( context: *mut dc_context_t, chat_id: u32, @@ -988,18 +1002,19 @@ pub unsafe extern "C" fn dc_send_msg( eprintln!("ignoring careless call to dc_send_msg()"); return 0; } - let ctx = &mut *context; - let ffi_msg = &mut *msg; + let ctx = unsafe { &mut *context }; + let ffi_msg = unsafe { &mut *msg }; - block_on(async move { - chat::send_msg(ctx, ChatId::new(chat_id), &mut ffi_msg.message) - .await - .unwrap_or_log_default(ctx, "Failed to send message") - }) + block_on(chat::send_msg( + ctx, + ChatId::new(chat_id), + &mut ffi_msg.message, + )) + .unwrap_or_log_default(ctx, "Failed to send message") .to_u32() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_send_msg_sync( context: *mut dc_context_t, chat_id: u32, @@ -1009,18 +1024,19 @@ pub unsafe extern "C" fn dc_send_msg_sync( eprintln!("ignoring careless call to dc_send_msg_sync()"); return 0; } - let ctx = &mut *context; - let ffi_msg = &mut *msg; + let ctx = unsafe { &mut *context }; + let ffi_msg = unsafe { &mut *msg }; - block_on(async move { - chat::send_msg_sync(ctx, ChatId::new(chat_id), &mut ffi_msg.message) - .await - .unwrap_or_log_default(ctx, "Failed to send message") - }) + block_on(chat::send_msg_sync( + ctx, + ChatId::new(chat_id), + &mut ffi_msg.message, + )) + .unwrap_or_log_default(ctx, "Failed to send message") .to_u32() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_send_text_msg( context: *mut dc_context_t, chat_id: u32, @@ -1030,18 +1046,15 @@ pub unsafe extern "C" fn dc_send_text_msg( eprintln!("ignoring careless call to dc_send_text_msg()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; let text_to_send = to_string_lossy(text_to_send); - block_on(async move { - chat::send_text_msg(ctx, ChatId::new(chat_id), text_to_send) - .await - .map(|msg_id| msg_id.to_u32()) - .unwrap_or_log_default(ctx, "Failed to send text message") - }) + block_on(chat::send_text_msg(ctx, ChatId::new(chat_id), text_to_send)) + .map(|msg_id| msg_id.to_u32()) + .unwrap_or_log_default(ctx, "Failed to send text message") } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_send_edit_request( context: *mut dc_context_t, msg_id: u32, @@ -1051,14 +1064,14 @@ pub unsafe extern "C" fn dc_send_edit_request( eprintln!("ignoring careless call to dc_send_edit_request()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; let new_text = to_string_lossy(new_text); block_on(chat::send_edit_request(ctx, MsgId::new(msg_id), new_text)) .unwrap_or_log_default(ctx, "Failed to send text edit") } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_send_delete_request( context: *mut dc_context_t, msg_ids: *const u32, @@ -1068,7 +1081,7 @@ pub unsafe extern "C" fn dc_send_delete_request( eprintln!("ignoring careless call to dc_send_delete_request()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); block_on(message::delete_msgs_ex(ctx, &msg_ids, true)) @@ -1077,7 +1090,7 @@ pub unsafe extern "C" fn dc_send_delete_request( .ok(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_send_webxdc_status_update( context: *mut dc_context_t, msg_id: u32, @@ -1088,7 +1101,7 @@ pub unsafe extern "C" fn dc_send_webxdc_status_update( eprintln!("ignoring careless call to dc_send_webxdc_status_update()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; block_on(ctx.send_webxdc_status_update(MsgId::new(msg_id), &to_string_lossy(json))) .context("Failed to send webxdc update") @@ -1096,7 +1109,7 @@ pub unsafe extern "C" fn dc_send_webxdc_status_update( .is_ok() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_webxdc_status_updates( context: *mut dc_context_t, msg_id: u32, @@ -1106,7 +1119,7 @@ pub unsafe extern "C" fn dc_get_webxdc_status_updates( eprintln!("ignoring careless call to dc_get_webxdc_status_updates()"); return "".strdup(); } - let ctx = &*context; + let ctx = unsafe { &*context }; block_on(ctx.get_webxdc_status_updates( MsgId::new(msg_id), @@ -1116,7 +1129,7 @@ pub unsafe extern "C" fn dc_get_webxdc_status_updates( .strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_webxdc_integration( context: *mut dc_context_t, file: *const libc::c_char, @@ -1125,13 +1138,13 @@ pub unsafe extern "C" fn dc_set_webxdc_integration( eprintln!("ignoring careless call to dc_set_webxdc_integration()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; block_on(ctx.set_webxdc_integration(&to_string_lossy(file))) .log_err(ctx) .unwrap_or_default(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_init_webxdc_integration( context: *mut dc_context_t, chat_id: u32, @@ -1140,7 +1153,7 @@ pub unsafe extern "C" fn dc_init_webxdc_integration( eprintln!("ignoring careless call to dc_init_webxdc_integration()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; let chat_id = if chat_id == 0 { None } else { @@ -1153,7 +1166,7 @@ pub unsafe extern "C" fn dc_init_webxdc_integration( .unwrap_or(0) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_place_outgoing_call( context: *mut dc_context_t, chat_id: u32, @@ -1164,7 +1177,7 @@ pub unsafe extern "C" fn dc_place_outgoing_call( eprintln!("ignoring careless call to dc_place_outgoing_call()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; let chat_id = ChatId::new(chat_id); let place_call_info = to_string_lossy(place_call_info); @@ -1175,7 +1188,7 @@ pub unsafe extern "C" fn dc_place_outgoing_call( .unwrap_or_log_default(ctx, "Failed to place call") } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accept_incoming_call( context: *mut dc_context_t, msg_id: u32, @@ -1185,7 +1198,7 @@ pub unsafe extern "C" fn dc_accept_incoming_call( eprintln!("ignoring careless call to dc_accept_incoming_call()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; let msg_id = MsgId::new(msg_id); let accept_call_info = to_string_lossy(accept_call_info); @@ -1194,13 +1207,13 @@ pub unsafe extern "C" fn dc_accept_incoming_call( .is_ok() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_end_call(context: *mut dc_context_t, msg_id: u32) -> libc::c_int { if context.is_null() || msg_id == 0 { eprintln!("ignoring careless call to dc_end_call()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; let msg_id = MsgId::new(msg_id); block_on(ctx.end_call(msg_id)) @@ -1209,7 +1222,7 @@ pub unsafe extern "C" fn dc_end_call(context: *mut dc_context_t, msg_id: u32) -> .is_ok() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_draft( context: *mut dc_context_t, chat_id: u32, @@ -1219,23 +1232,19 @@ pub unsafe extern "C" fn dc_set_draft( eprintln!("ignoring careless call to dc_set_draft()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; let msg = if msg.is_null() { None } else { - let ffi_msg: &mut MessageWrapper = &mut *msg; + let ffi_msg: &mut MessageWrapper = unsafe { &mut *msg }; Some(&mut ffi_msg.message) }; - block_on(async move { - ChatId::new(chat_id) - .set_draft(ctx, msg) - .await - .unwrap_or_log_default(ctx, "failed to set draft"); - }); + block_on(ChatId::new(chat_id).set_draft(ctx, msg)) + .unwrap_or_log_default(ctx, "failed to set draft"); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_add_device_msg( context: *mut dc_context_t, label: *const libc::c_char, @@ -1245,23 +1254,24 @@ pub unsafe extern "C" fn dc_add_device_msg( eprintln!("ignoring careless call to dc_add_device_msg()"); return 0; } - let ctx = &mut *context; + let ctx = unsafe { &mut *context }; let msg = if msg.is_null() { None } else { - let ffi_msg: &mut MessageWrapper = &mut *msg; + let ffi_msg: &mut MessageWrapper = unsafe { &mut *msg }; Some(&mut ffi_msg.message) }; - block_on(async move { - chat::add_device_msg(ctx, to_opt_string_lossy(label).as_deref(), msg) - .await - .unwrap_or_log_default(ctx, "Failed to add device message") - }) + block_on(chat::add_device_msg( + ctx, + to_opt_string_lossy(label).as_deref(), + msg, + )) + .unwrap_or_log_default(ctx, "Failed to add device message") .to_u32() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_was_device_msg_ever_added( context: *mut dc_context_t, label: *const libc::c_char, @@ -1270,22 +1280,22 @@ pub unsafe extern "C" fn dc_was_device_msg_ever_added( eprintln!("ignoring careless call to dc_was_device_msg_ever_added()"); return 0; } - let ctx = &mut *context; + let ctx = unsafe { &mut *context }; - block_on(async move { - chat::was_device_msg_ever_added(ctx, &to_string_lossy(label)) - .await - .unwrap_or(false) as libc::c_int - }) + block_on(chat::was_device_msg_ever_added( + ctx, + &to_string_lossy(label), + )) + .unwrap_or(false) as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_draft(context: *mut dc_context_t, chat_id: u32) -> *mut dc_msg_t { if context.is_null() { eprintln!("ignoring careless call to dc_get_draft()"); return ptr::null_mut(); // NULL explicitly defined as "no draft" } - let ctx = &*context; + let ctx = unsafe { &*context }; match block_on(ChatId::new(chat_id).get_draft(ctx)) .with_context(|| format!("Failed to get draft for chat #{chat_id}")) @@ -1302,7 +1312,7 @@ pub unsafe extern "C" fn dc_get_draft(context: *mut dc_context_t, chat_id: u32) } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_chat_msgs( context: *mut dc_context_t, chat_id: u32, @@ -1313,40 +1323,33 @@ pub unsafe extern "C" fn dc_get_chat_msgs( eprintln!("ignoring careless call to dc_get_chat_msgs()"); return ptr::null_mut(); } - let ctx = &*context; + let ctx = unsafe { &*context }; let add_daymarker = (flags & DC_GCM_ADDDAYMARKER) != 0; - block_on(async move { - Box::into_raw(Box::new( - chat::get_chat_msgs_ex( - ctx, - ChatId::new(chat_id), - MessageListOptions { add_daymarker }, - ) - .await - .unwrap_or_log_default(ctx, "failed to get chat msgs") - .into(), + Box::into_raw(Box::new( + block_on(chat::get_chat_msgs_ex( + ctx, + ChatId::new(chat_id), + MessageListOptions { add_daymarker }, )) - }) + .unwrap_or_log_default(ctx, "failed to get chat msgs") + .into(), + )) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_msg_cnt(context: *mut dc_context_t, chat_id: u32) -> libc::c_int { if context.is_null() { eprintln!("ignoring careless call to dc_get_msg_cnt()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; - block_on(async move { - ChatId::new(chat_id) - .get_msg_cnt(ctx) - .await - .unwrap_or_log_default(ctx, "failed to get msg count") as libc::c_int - }) + block_on(ChatId::new(chat_id).get_msg_cnt(ctx)) + .unwrap_or_log_default(ctx, "failed to get msg count") as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_fresh_msg_cnt( context: *mut dc_context_t, chat_id: u32, @@ -1355,17 +1358,13 @@ pub unsafe extern "C" fn dc_get_fresh_msg_cnt( eprintln!("ignoring careless call to dc_get_fresh_msg_cnt()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; - block_on(async move { - ChatId::new(chat_id) - .get_fresh_msg_cnt(ctx) - .await - .unwrap_or_log_default(ctx, "failed to get fresh msg cnt") as libc::c_int - }) + block_on(ChatId::new(chat_id).get_fresh_msg_cnt(ctx)) + .unwrap_or_log_default(ctx, "failed to get fresh msg cnt") as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_similar_chatlist( context: *mut dc_context_t, chat_id: u32, @@ -1374,7 +1373,7 @@ pub unsafe extern "C" fn dc_get_similar_chatlist( eprintln!("ignoring careless call to dc_get_similar_chatlist()"); return ptr::null_mut(); } - let ctx = &*context; + let ctx = unsafe { &*context }; let chat_id = ChatId::new(chat_id); match block_on(chat_id.get_similar_chatlist(ctx)) @@ -1389,7 +1388,7 @@ pub unsafe extern "C" fn dc_get_similar_chatlist( } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_estimate_deletion_cnt( context: *mut dc_context_t, from_server: libc::c_int, @@ -1399,15 +1398,16 @@ pub unsafe extern "C" fn dc_estimate_deletion_cnt( eprintln!("ignoring careless call to dc_estimate_deletion_cnt()"); return 0; } - let ctx = &*context; - block_on(async move { - message::estimate_deletion_cnt(ctx, from_server != 0, seconds) - .await - .unwrap_or(0) as libc::c_int - }) + let ctx = unsafe { &*context }; + block_on(message::estimate_deletion_cnt( + ctx, + from_server != 0, + seconds, + )) + .unwrap_or(0) as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_fresh_msgs( context: *mut dc_context_t, ) -> *mut dc_array::dc_array_t { @@ -1415,7 +1415,7 @@ pub unsafe extern "C" fn dc_get_fresh_msgs( eprintln!("ignoring careless call to dc_get_fresh_msgs()"); return ptr::null_mut(); } - let ctx = &*context; + let ctx = unsafe { &*context }; block_on(async move { let arr = dc_array_t::from( @@ -1432,13 +1432,13 @@ pub unsafe extern "C" fn dc_get_fresh_msgs( }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_next_msgs(context: *mut dc_context_t) -> *mut dc_array::dc_array_t { if context.is_null() { eprintln!("ignoring careless call to dc_get_next_msgs()"); return ptr::null_mut(); } - let ctx = &*context; + let ctx = unsafe { &*context }; let msg_ids = block_on(ctx.get_next_msgs()) .context("failed to get next messages") @@ -1453,7 +1453,7 @@ pub unsafe extern "C" fn dc_get_next_msgs(context: *mut dc_context_t) -> *mut dc Box::into_raw(Box::new(arr)) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_wait_next_msgs( context: *mut dc_context_t, ) -> *mut dc_array::dc_array_t { @@ -1461,7 +1461,7 @@ pub unsafe extern "C" fn dc_wait_next_msgs( eprintln!("ignoring careless call to dc_wait_next_msgs()"); return ptr::null_mut(); } - let ctx = &*context; + let ctx = unsafe { &*context }; let msg_ids = block_on(ctx.wait_next_msgs()) .context("failed to wait for next messages") @@ -1476,38 +1476,32 @@ pub unsafe extern "C" fn dc_wait_next_msgs( Box::into_raw(Box::new(arr)) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_marknoticed_chat(context: *mut dc_context_t, chat_id: u32) { if context.is_null() { eprintln!("ignoring careless call to dc_marknoticed_chat()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; - block_on(async move { - chat::marknoticed_chat(ctx, ChatId::new(chat_id)) - .await - .context("Failed marknoticed chat") - .log_err(ctx) - .unwrap_or(()) - }) + block_on(chat::marknoticed_chat(ctx, ChatId::new(chat_id))) + .context("Failed marknoticed chat") + .log_err(ctx) + .unwrap_or(()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_markfresh_chat(context: *mut dc_context_t, chat_id: u32) { if context.is_null() { eprintln!("ignoring careless call to dc_markfresh_chat()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; - block_on(async move { - chat::markfresh_chat(ctx, ChatId::new(chat_id)) - .await - .context("Failed markfresh chat") - .log_err(ctx) - .unwrap_or(()) - }) + block_on(chat::markfresh_chat(ctx, ChatId::new(chat_id))) + .context("Failed markfresh chat") + .log_err(ctx) + .unwrap_or(()) } fn from_prim(s: S) -> Option @@ -1518,7 +1512,7 @@ where FromPrimitive::from_i64(s.into()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_chat_media( context: *mut dc_context_t, chat_id: u32, @@ -1530,7 +1524,7 @@ pub unsafe extern "C" fn dc_get_chat_media( eprintln!("ignoring careless call to dc_get_chat_media()"); return ptr::null_mut(); } - let ctx = &*context; + let ctx = unsafe { &*context }; let chat_id = if chat_id == 0 { None } else { @@ -1542,17 +1536,20 @@ pub unsafe extern "C" fn dc_get_chat_media( let or_msg_type3 = from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {or_msg_type3}")); - block_on(async move { - Box::into_raw(Box::new( - chat::get_chat_media(ctx, chat_id, msg_type, or_msg_type2, or_msg_type3) - .await - .unwrap_or_log_default(ctx, "Failed get_chat_media") - .into(), + Box::into_raw(Box::new( + block_on(chat::get_chat_media( + ctx, + chat_id, + msg_type, + or_msg_type2, + or_msg_type3, )) - }) + .unwrap_or_log_default(ctx, "Failed get_chat_media") + .into(), + )) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_chat_visibility( context: *mut dc_context_t, chat_id: u32, @@ -1562,7 +1559,7 @@ pub unsafe extern "C" fn dc_set_chat_visibility( eprintln!("ignoring careless call to dc_set_chat_visibility()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; let visibility = match archive { 0 => ChatVisibility::Normal, 1 => ChatVisibility::Archived, @@ -1573,551 +1570,597 @@ pub unsafe extern "C" fn dc_set_chat_visibility( } }; - block_on(async move { - ChatId::new(chat_id) - .set_visibility(ctx, visibility) - .await - .context("Failed setting chat visibility") - .log_err(ctx) - .unwrap_or(()) - }) + block_on(ChatId::new(chat_id).set_visibility(ctx, visibility)) + .context("Failed setting chat visibility") + .log_err(ctx) + .unwrap_or(()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_delete_chat(context: *mut dc_context_t, chat_id: u32) { if context.is_null() { eprintln!("ignoring careless call to dc_delete_chat()"); return; } - let ctx = &*context; + let ctx = unsafe { &*context }; - block_on(async move { - ChatId::new(chat_id) - .delete(ctx) - .await - .context("Failed chat delete") - .log_err(ctx) - .ok(); - }) + block_on(ChatId::new(chat_id).delete(ctx)) + .context("Failed chat delete") + .log_err(ctx) + .ok(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_block_chat(context: *mut dc_context_t, chat_id: u32) { - if context.is_null() { - eprintln!("ignoring careless call to dc_block_chat()"); - return; - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_block_chat()"); + return; + } + let ctx = &*context; - block_on(async move { - ChatId::new(chat_id) - .block(ctx) - .await - .context("Failed chat block") - .log_err(ctx) - .ok(); - }) + block_on(async move { + ChatId::new(chat_id) + .block(ctx) + .await + .context("Failed chat block") + .log_err(ctx) + .ok(); + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accept_chat(context: *mut dc_context_t, chat_id: u32) { - if context.is_null() { - eprintln!("ignoring careless call to dc_accept_chat()"); - return; - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_accept_chat()"); + return; + } + let ctx = &*context; - block_on(async move { - ChatId::new(chat_id) - .accept(ctx) - .await - .context("Failed chat accept") - .log_err(ctx) - .ok(); - }) + block_on(async move { + ChatId::new(chat_id) + .accept(ctx) + .await + .context("Failed chat accept") + .log_err(ctx) + .ok(); + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_chat_contacts( context: *mut dc_context_t, chat_id: u32, ) -> *mut dc_array::dc_array_t { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_chat_contacts()"); - return ptr::null_mut(); - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_chat_contacts()"); + return ptr::null_mut(); + } + let ctx = &*context; - block_on(async move { - let arr = dc_array_t::from( - chat::get_chat_contacts(ctx, ChatId::new(chat_id)) - .await - .unwrap_or_log_default(ctx, "Failed get_chat_contacts") - .iter() - .map(|id| id.to_u32()) - .collect::>(), - ); - Box::into_raw(Box::new(arr)) - }) + block_on(async move { + let arr = dc_array_t::from( + chat::get_chat_contacts(ctx, ChatId::new(chat_id)) + .await + .unwrap_or_log_default(ctx, "Failed get_chat_contacts") + .iter() + .map(|id| id.to_u32()) + .collect::>(), + ); + Box::into_raw(Box::new(arr)) + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_search_msgs( context: *mut dc_context_t, chat_id: u32, query: *const libc::c_char, ) -> *mut dc_array::dc_array_t { - if context.is_null() || query.is_null() { - eprintln!("ignoring careless call to dc_search_msgs()"); - return ptr::null_mut(); - } - let ctx = &*context; - let chat_id = if chat_id == 0 { - None - } else { - Some(ChatId::new(chat_id)) - }; + unsafe { + if context.is_null() || query.is_null() { + eprintln!("ignoring careless call to dc_search_msgs()"); + return ptr::null_mut(); + } + let ctx = &*context; + let chat_id = if chat_id == 0 { + None + } else { + Some(ChatId::new(chat_id)) + }; - block_on(async move { - let arr = dc_array_t::from( - ctx.search_msgs(chat_id, &to_string_lossy(query)) - .await - .unwrap_or_log_default(ctx, "Failed search_msgs") - .iter() - .map(|msg_id| msg_id.to_u32()) - .collect::>(), - ); - Box::into_raw(Box::new(arr)) - }) + block_on(async move { + let arr = dc_array_t::from( + ctx.search_msgs(chat_id, &to_string_lossy(query)) + .await + .unwrap_or_log_default(ctx, "Failed search_msgs") + .iter() + .map(|msg_id| msg_id.to_u32()) + .collect::>(), + ); + Box::into_raw(Box::new(arr)) + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_chat(context: *mut dc_context_t, chat_id: u32) -> *mut dc_chat_t { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_chat()"); - return ptr::null_mut(); - } - let ctx = &*context; - let context: Context = ctx.clone(); - - block_on(async move { - match chat::Chat::load_from_db(ctx, ChatId::new(chat_id)).await { - Ok(chat) => { - let ffi_chat = ChatWrapper { context, chat }; - Box::into_raw(Box::new(ffi_chat)) - } - Err(_) => ptr::null_mut(), + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_chat()"); + return ptr::null_mut(); } - }) + let ctx = &*context; + let context: Context = ctx.clone(); + + block_on(async move { + match chat::Chat::load_from_db(ctx, ChatId::new(chat_id)).await { + Ok(chat) => { + let ffi_chat = ChatWrapper { context, chat }; + Box::into_raw(Box::new(ffi_chat)) + } + Err(_) => ptr::null_mut(), + } + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_create_group_chat( context: *mut dc_context_t, _protect: libc::c_int, name: *const libc::c_char, ) -> u32 { - if context.is_null() || name.is_null() { - eprintln!("ignoring careless call to dc_create_group_chat()"); - return 0; - } - let ctx = &*context; + unsafe { + if context.is_null() || name.is_null() { + eprintln!("ignoring careless call to dc_create_group_chat()"); + return 0; + } + let ctx = &*context; - block_on(chat::create_group(ctx, &to_string_lossy(name))) - .context("Failed to create group chat") - .log_err(ctx) - .map(|id| id.to_u32()) - .unwrap_or(0) + block_on(chat::create_group(ctx, &to_string_lossy(name))) + .context("Failed to create group chat") + .log_err(ctx) + .map(|id| id.to_u32()) + .unwrap_or(0) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_create_broadcast_list(context: *mut dc_context_t) -> u32 { - if context.is_null() { - eprintln!("ignoring careless call to dc_create_broadcast_list()"); - return 0; - } - let ctx = &*context; - block_on(chat::create_broadcast(ctx, "Channel".to_string())) - .context("Failed to create broadcast channel") - .log_err(ctx) - .map(|id| id.to_u32()) - .unwrap_or(0) + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_create_broadcast_list()"); + return 0; + } + let ctx = &*context; + block_on(chat::create_broadcast(ctx, "Channel".to_string())) + .context("Failed to create broadcast channel") + .log_err(ctx) + .map(|id| id.to_u32()) + .unwrap_or(0) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_is_contact_in_chat( context: *mut dc_context_t, chat_id: u32, contact_id: u32, ) -> libc::c_int { - if context.is_null() { - eprintln!("ignoring careless call to dc_is_contact_in_chat()"); - return 0; - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_is_contact_in_chat()"); + return 0; + } + let ctx = &*context; - block_on(chat::is_contact_in_chat( - ctx, - ChatId::new(chat_id), - ContactId::new(contact_id), - )) - .context("is_contact_in_chat failed") - .log_err(ctx) - .unwrap_or_default() as libc::c_int + block_on(chat::is_contact_in_chat( + ctx, + ChatId::new(chat_id), + ContactId::new(contact_id), + )) + .context("is_contact_in_chat failed") + .log_err(ctx) + .unwrap_or_default() as libc::c_int + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_add_contact_to_chat( context: *mut dc_context_t, chat_id: u32, contact_id: u32, ) -> libc::c_int { - if context.is_null() { - eprintln!("ignoring careless call to dc_add_contact_to_chat()"); - return 0; - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_add_contact_to_chat()"); + return 0; + } + let ctx = &*context; - block_on(chat::add_contact_to_chat( - ctx, - ChatId::new(chat_id), - ContactId::new(contact_id), - )) - .context("Failed to add contact") - .log_err(ctx) - .is_ok() as libc::c_int + block_on(chat::add_contact_to_chat( + ctx, + ChatId::new(chat_id), + ContactId::new(contact_id), + )) + .context("Failed to add contact") + .log_err(ctx) + .is_ok() as libc::c_int + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_remove_contact_from_chat( context: *mut dc_context_t, chat_id: u32, contact_id: u32, ) -> libc::c_int { - if context.is_null() { - eprintln!("ignoring careless call to dc_remove_contact_from_chat()"); - return 0; - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_remove_contact_from_chat()"); + return 0; + } + let ctx = &*context; - block_on(chat::remove_contact_from_chat( - ctx, - ChatId::new(chat_id), - ContactId::new(contact_id), - )) - .context("Failed to remove contact") - .log_err(ctx) - .is_ok() as libc::c_int + block_on(chat::remove_contact_from_chat( + ctx, + ChatId::new(chat_id), + ContactId::new(contact_id), + )) + .context("Failed to remove contact") + .log_err(ctx) + .is_ok() as libc::c_int + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_chat_name( context: *mut dc_context_t, chat_id: u32, name: *const libc::c_char, ) -> libc::c_int { - if context.is_null() || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() || name.is_null() - { - eprintln!("ignoring careless call to dc_set_chat_name()"); - return 0; - } - let ctx = &*context; + unsafe { + if context.is_null() + || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() + || name.is_null() + { + eprintln!("ignoring careless call to dc_set_chat_name()"); + return 0; + } + let ctx = &*context; - block_on(async move { - chat::set_chat_name(ctx, ChatId::new(chat_id), &to_string_lossy(name)) - .await - .map(|_| 1) - .unwrap_or_log_default(ctx, "Failed to set chat name") - }) + block_on(async move { + chat::set_chat_name(ctx, ChatId::new(chat_id), &to_string_lossy(name)) + .await + .map(|_| 1) + .unwrap_or_log_default(ctx, "Failed to set chat name") + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_chat_profile_image( context: *mut dc_context_t, chat_id: u32, image: *const libc::c_char, ) -> libc::c_int { - if context.is_null() || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() { - eprintln!("ignoring careless call to dc_set_chat_profile_image()"); - return 0; - } - let ctx = &*context; + unsafe { + if context.is_null() || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() { + eprintln!("ignoring careless call to dc_set_chat_profile_image()"); + return 0; + } + let ctx = &*context; - block_on(async move { - chat::set_chat_profile_image(ctx, ChatId::new(chat_id), &to_string_lossy(image)) - .await - .map(|_| 1) - .unwrap_or_log_default(ctx, "Failed to set profile image") - }) + block_on(async move { + chat::set_chat_profile_image(ctx, ChatId::new(chat_id), &to_string_lossy(image)) + .await + .map(|_| 1) + .unwrap_or_log_default(ctx, "Failed to set profile image") + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_chat_mute_duration( context: *mut dc_context_t, chat_id: u32, duration: i64, ) -> libc::c_int { - if context.is_null() { - eprintln!("ignoring careless call to dc_set_chat_mute_duration()"); - return 0; - } - let ctx = &*context; - let mute_duration = match duration { - 0 => MuteDuration::NotMuted, - -1 => MuteDuration::Forever, - n if n > 0 => SystemTime::now() - .checked_add(Duration::from_secs(duration as u64)) - .map_or(MuteDuration::Forever, MuteDuration::Until), - _ => { - eprintln!("dc_chat_set_mute_duration(): Can not use negative duration other than -1"); + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_set_chat_mute_duration()"); return 0; } - }; + let ctx = &*context; + let mute_duration = match duration { + 0 => MuteDuration::NotMuted, + -1 => MuteDuration::Forever, + n if n > 0 => SystemTime::now() + .checked_add(Duration::from_secs(duration as u64)) + .map_or(MuteDuration::Forever, MuteDuration::Until), + _ => { + eprintln!( + "dc_chat_set_mute_duration(): Can not use negative duration other than -1" + ); + return 0; + } + }; - block_on(async move { - chat::set_muted(ctx, ChatId::new(chat_id), mute_duration) - .await - .map(|_| 1) - .unwrap_or_log_default(ctx, "Failed to set mute duration") - }) + block_on(async move { + chat::set_muted(ctx, ChatId::new(chat_id), mute_duration) + .await + .map(|_| 1) + .unwrap_or_log_default(ctx, "Failed to set mute duration") + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_chat_encrinfo( context: *mut dc_context_t, chat_id: u32, ) -> *mut libc::c_char { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_chat_encrinfo()"); - return "".strdup(); - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_chat_encrinfo()"); + return "".strdup(); + } + let ctx = &*context; - block_on(ChatId::new(chat_id).get_encryption_info(ctx)) - .map(|s| s.strdup()) - .log_err(ctx) - .unwrap_or(ptr::null_mut()) + block_on(ChatId::new(chat_id).get_encryption_info(ctx)) + .map(|s| s.strdup()) + .log_err(ctx) + .unwrap_or(ptr::null_mut()) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_chat_ephemeral_timer( context: *mut dc_context_t, chat_id: u32, ) -> u32 { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_chat_ephemeral_timer()"); - return 0; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_chat_ephemeral_timer()"); + return 0; + } + let ctx = &*context; + + // Timer value 0 is returned in the rare case of a database error, + // but it is not dangerous since it is only meant to be used as a + // default when changing the value. Such errors should not be + // ignored when ephemeral timer value is used to construct + // message headers. + block_on(async move { ChatId::new(chat_id).get_ephemeral_timer(ctx).await }) + .context("Failed to get ephemeral timer") + .log_err(ctx) + .unwrap_or_default() + .to_u32() } - let ctx = &*context; - - // Timer value 0 is returned in the rare case of a database error, - // but it is not dangerous since it is only meant to be used as a - // default when changing the value. Such errors should not be - // ignored when ephemeral timer value is used to construct - // message headers. - block_on(async move { ChatId::new(chat_id).get_ephemeral_timer(ctx).await }) - .context("Failed to get ephemeral timer") - .log_err(ctx) - .unwrap_or_default() - .to_u32() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_chat_ephemeral_timer( context: *mut dc_context_t, chat_id: u32, timer: u32, ) -> libc::c_int { - if context.is_null() { - eprintln!("ignoring careless call to dc_set_chat_ephemeral_timer()"); - return 0; - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_set_chat_ephemeral_timer()"); + return 0; + } + let ctx = &*context; - block_on(async move { - ChatId::new(chat_id) - .set_ephemeral_timer(ctx, EphemeralTimer::from_u32(timer)) - .await - .context("Failed to set ephemeral timer") - .log_err(ctx) - .is_ok() as libc::c_int - }) + block_on(async move { + ChatId::new(chat_id) + .set_ephemeral_timer(ctx, EphemeralTimer::from_u32(timer)) + .await + .context("Failed to set ephemeral timer") + .log_err(ctx) + .is_ok() as libc::c_int + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_msg_info( context: *mut dc_context_t, msg_id: u32, ) -> *mut libc::c_char { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_msg_info()"); - return "".strdup(); + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_msg_info()"); + return "".strdup(); + } + let ctx = &*context; + let msg_id = MsgId::new(msg_id); + block_on(msg_id.get_info(ctx)) + .unwrap_or_log_default(ctx, "failed to get msg id") + .strdup() } - let ctx = &*context; - let msg_id = MsgId::new(msg_id); - block_on(msg_id.get_info(ctx)) - .unwrap_or_log_default(ctx, "failed to get msg id") - .strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_msg_html( context: *mut dc_context_t, msg_id: u32, ) -> *mut libc::c_char { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_msg_html()"); - return ptr::null_mut(); - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_msg_html()"); + return ptr::null_mut(); + } + let ctx = &*context; - block_on(MsgId::new(msg_id).get_html(ctx)) - .unwrap_or_log_default(ctx, "Failed get_msg_html") - .strdup() + block_on(MsgId::new(msg_id).get_html(ctx)) + .unwrap_or_log_default(ctx, "Failed get_msg_html") + .strdup() + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_delete_msgs( context: *mut dc_context_t, msg_ids: *const u32, msg_cnt: libc::c_int, ) { - if context.is_null() || msg_ids.is_null() || msg_cnt <= 0 { - eprintln!("ignoring careless call to dc_delete_msgs()"); - return; - } - let ctx = &*context; - let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); + unsafe { + if context.is_null() || msg_ids.is_null() || msg_cnt <= 0 { + eprintln!("ignoring careless call to dc_delete_msgs()"); + return; + } + let ctx = &*context; + let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); - block_on(message::delete_msgs(ctx, &msg_ids)) - .context("failed dc_delete_msgs() call") - .log_err(ctx) - .ok(); + block_on(message::delete_msgs(ctx, &msg_ids)) + .context("failed dc_delete_msgs() call") + .log_err(ctx) + .ok(); + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_forward_msgs( context: *mut dc_context_t, msg_ids: *const u32, msg_cnt: libc::c_int, chat_id: u32, ) { - if context.is_null() - || msg_ids.is_null() - || msg_cnt <= 0 - || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() - { - eprintln!("ignoring careless call to dc_forward_msgs()"); - return; - } - let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); - let ctx = &*context; + unsafe { + if context.is_null() + || msg_ids.is_null() + || msg_cnt <= 0 + || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() + { + eprintln!("ignoring careless call to dc_forward_msgs()"); + return; + } + let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); + let ctx = &*context; - block_on(async move { - chat::forward_msgs(ctx, &msg_ids[..], ChatId::new(chat_id)) - .await - .unwrap_or_log_default(ctx, "Failed to forward message") - }) + block_on(async move { + chat::forward_msgs(ctx, &msg_ids[..], ChatId::new(chat_id)) + .await + .unwrap_or_log_default(ctx, "Failed to forward message") + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_save_msgs( context: *mut dc_context_t, msg_ids: *const u32, msg_cnt: libc::c_int, ) { - if context.is_null() || msg_ids.is_null() || msg_cnt <= 0 { - eprintln!("ignoring careless call to dc_save_msgs()"); - return; - } - let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); - let ctx = &*context; + unsafe { + if context.is_null() || msg_ids.is_null() || msg_cnt <= 0 { + eprintln!("ignoring careless call to dc_save_msgs()"); + return; + } + let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); + let ctx = &*context; - block_on(async move { - chat::save_msgs(ctx, &msg_ids[..]) - .await - .unwrap_or_log_default(ctx, "Failed to save message") - }) + block_on(async move { + chat::save_msgs(ctx, &msg_ids[..]) + .await + .unwrap_or_log_default(ctx, "Failed to save message") + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_resend_msgs( context: *mut dc_context_t, msg_ids: *const u32, msg_cnt: libc::c_int, ) -> libc::c_int { - if context.is_null() || msg_ids.is_null() || msg_cnt <= 0 { - eprintln!("ignoring careless call to dc_resend_msgs()"); - return 0; - } - let ctx = &*context; - let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); + unsafe { + if context.is_null() || msg_ids.is_null() || msg_cnt <= 0 { + eprintln!("ignoring careless call to dc_resend_msgs()"); + return 0; + } + let ctx = &*context; + let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); - block_on(chat::resend_msgs(ctx, &msg_ids)) - .context("Resending failed") - .log_err(ctx) - .is_ok() as libc::c_int + block_on(chat::resend_msgs(ctx, &msg_ids)) + .context("Resending failed") + .log_err(ctx) + .is_ok() as libc::c_int + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_markseen_msgs( context: *mut dc_context_t, msg_ids: *const u32, msg_cnt: libc::c_int, ) { - if context.is_null() || msg_ids.is_null() || msg_cnt <= 0 { - eprintln!("ignoring careless call to dc_markseen_msgs()"); - return; - } - let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); - let ctx = &*context; + unsafe { + if context.is_null() || msg_ids.is_null() || msg_cnt <= 0 { + eprintln!("ignoring careless call to dc_markseen_msgs()"); + return; + } + let msg_ids = convert_and_prune_message_ids(msg_ids, msg_cnt); + let ctx = &*context; - block_on(message::markseen_msgs(ctx, msg_ids)) - .context("failed dc_markseen_msgs() call") - .log_err(ctx) - .ok(); + block_on(message::markseen_msgs(ctx, msg_ids)) + .context("failed dc_markseen_msgs() call") + .log_err(ctx) + .ok(); + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_msg(context: *mut dc_context_t, msg_id: u32) -> *mut dc_msg_t { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_msg()"); - return ptr::null_mut(); - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_msg()"); + return ptr::null_mut(); + } + let ctx = &*context; - let message = match block_on(message::Message::load_from_db(ctx, MsgId::new(msg_id))) - .with_context(|| format!("dc_get_msg could not rectieve msg_id {msg_id}")) - .log_err(ctx) - { - Ok(msg) => msg, - Err(_) => { - if msg_id <= constants::DC_MSG_ID_LAST_SPECIAL { - // C-core API returns empty messages, do the same - message::Message::new(Viewtype::default()) - } else { - return ptr::null_mut(); + let message = match block_on(message::Message::load_from_db(ctx, MsgId::new(msg_id))) + .with_context(|| format!("dc_get_msg could not rectieve msg_id {msg_id}")) + .log_err(ctx) + { + Ok(msg) => msg, + Err(_) => { + if msg_id <= constants::DC_MSG_ID_LAST_SPECIAL { + // C-core API returns empty messages, do the same + message::Message::new(Viewtype::default()) + } else { + return ptr::null_mut(); + } } - } - }; - let ffi_msg = MessageWrapper { context, message }; - Box::into_raw(Box::new(ffi_msg)) + }; + let ffi_msg = MessageWrapper { context, message }; + Box::into_raw(Box::new(ffi_msg)) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_download_full_msg(context: *mut dc_context_t, msg_id: u32) { - if context.is_null() { - eprintln!("ignoring careless call to dc_download_full_msg()"); - return; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_download_full_msg()"); + return; + } + let ctx = &*context; + block_on(MsgId::new(msg_id).download_full(ctx)) + .context("Failed to download message fully.") + .log_err(ctx) + .ok(); } - let ctx = &*context; - block_on(MsgId::new(msg_id).download_full(ctx)) - .context("Failed to download message fully.") - .log_err(ctx) - .ok(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_may_be_valid_addr(addr: *const libc::c_char) -> libc::c_int { if addr.is_null() { eprintln!("ignoring careless call to dc_may_be_valid_addr()"); @@ -2127,235 +2170,257 @@ pub unsafe extern "C" fn dc_may_be_valid_addr(addr: *const libc::c_char) -> libc contact::may_be_valid_addr(&to_string_lossy(addr)) as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_lookup_contact_id_by_addr( context: *mut dc_context_t, addr: *const libc::c_char, ) -> u32 { - if context.is_null() || addr.is_null() { - eprintln!("ignoring careless call to dc_lookup_contact_id_by_addr()"); - return 0; - } - let ctx = &*context; + unsafe { + if context.is_null() || addr.is_null() { + eprintln!("ignoring careless call to dc_lookup_contact_id_by_addr()"); + return 0; + } + let ctx = &*context; - block_on(async move { - Contact::lookup_id_by_addr(ctx, &to_string_lossy(addr), Origin::IncomingReplyTo) - .await - .unwrap_or_log_default(ctx, "failed to lookup id") - .map(|id| id.to_u32()) - .unwrap_or_default() - }) + block_on(async move { + Contact::lookup_id_by_addr(ctx, &to_string_lossy(addr), Origin::IncomingReplyTo) + .await + .unwrap_or_log_default(ctx, "failed to lookup id") + .map(|id| id.to_u32()) + .unwrap_or_default() + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_create_contact( context: *mut dc_context_t, name: *const libc::c_char, addr: *const libc::c_char, ) -> u32 { - if context.is_null() || addr.is_null() { - eprintln!("ignoring careless call to dc_create_contact()"); - return 0; - } - let ctx = &*context; - let name = to_string_lossy(name); + unsafe { + if context.is_null() || addr.is_null() { + eprintln!("ignoring careless call to dc_create_contact()"); + return 0; + } + let ctx = &*context; + let name = to_string_lossy(name); - block_on(Contact::create(ctx, &name, &to_string_lossy(addr))) - .context("Cannot create contact") - .log_err(ctx) - .map(|id| id.to_u32()) - .unwrap_or(0) + block_on(Contact::create(ctx, &name, &to_string_lossy(addr))) + .context("Cannot create contact") + .log_err(ctx) + .map(|id| id.to_u32()) + .unwrap_or(0) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_add_address_book( context: *mut dc_context_t, addr_book: *const libc::c_char, ) -> libc::c_int { - if context.is_null() || addr_book.is_null() { - eprintln!("ignoring careless call to dc_add_address_book()"); - return 0; - } - let ctx = &*context; - - block_on(async move { - match Contact::add_address_book(ctx, &to_string_lossy(addr_book)).await { - Ok(cnt) => cnt as libc::c_int, - Err(_) => 0, + unsafe { + if context.is_null() || addr_book.is_null() { + eprintln!("ignoring careless call to dc_add_address_book()"); + return 0; } - }) + let ctx = &*context; + + block_on(async move { + match Contact::add_address_book(ctx, &to_string_lossy(addr_book)).await { + Ok(cnt) => cnt as libc::c_int, + Err(_) => 0, + } + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_make_vcard( context: *mut dc_context_t, contact_id: u32, ) -> *mut libc::c_char { - if context.is_null() { - eprintln!("ignoring careless call to dc_make_vcard()"); - return ptr::null_mut(); - } - let ctx = &*context; - let contact_id = ContactId::new(contact_id); + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_make_vcard()"); + return ptr::null_mut(); + } + let ctx = &*context; + let contact_id = ContactId::new(contact_id); - block_on(contact::make_vcard(ctx, &[contact_id])) - .unwrap_or_log_default(ctx, "dc_make_vcard failed") - .strdup() + block_on(contact::make_vcard(ctx, &[contact_id])) + .unwrap_or_log_default(ctx, "dc_make_vcard failed") + .strdup() + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_import_vcard( context: *mut dc_context_t, vcard: *const libc::c_char, ) -> *mut dc_array::dc_array_t { - if context.is_null() || vcard.is_null() { - eprintln!("ignoring careless call to dc_import_vcard()"); - return ptr::null_mut(); - } - let ctx = &*context; + unsafe { + if context.is_null() || vcard.is_null() { + eprintln!("ignoring careless call to dc_import_vcard()"); + return ptr::null_mut(); + } + let ctx = &*context; - match block_on(contact::import_vcard(ctx, &to_string_lossy(vcard))) - .context("dc_import_vcard failed") - .log_err(ctx) - { - Ok(contact_ids) => Box::into_raw(Box::new(dc_array_t::from( - contact_ids - .iter() - .map(|id| id.to_u32()) - .collect::>(), - ))), - Err(_) => ptr::null_mut(), + match block_on(contact::import_vcard(ctx, &to_string_lossy(vcard))) + .context("dc_import_vcard failed") + .log_err(ctx) + { + Ok(contact_ids) => Box::into_raw(Box::new(dc_array_t::from( + contact_ids + .iter() + .map(|id| id.to_u32()) + .collect::>(), + ))), + Err(_) => ptr::null_mut(), + } } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_contacts( context: *mut dc_context_t, flags: u32, query: *const libc::c_char, ) -> *mut dc_array::dc_array_t { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_contacts()"); - return ptr::null_mut(); - } - let ctx = &*context; - let query = to_opt_string_lossy(query); - - block_on(async move { - match Contact::get_all(ctx, flags, query.as_deref()).await { - Ok(contacts) => Box::into_raw(Box::new(dc_array_t::from( - contacts.iter().map(|id| id.to_u32()).collect::>(), - ))), - Err(_) => ptr::null_mut(), + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_contacts()"); + return ptr::null_mut(); } - }) + let ctx = &*context; + let query = to_opt_string_lossy(query); + + block_on(async move { + match Contact::get_all(ctx, flags, query.as_deref()).await { + Ok(contacts) => Box::into_raw(Box::new(dc_array_t::from( + contacts.iter().map(|id| id.to_u32()).collect::>(), + ))), + Err(_) => ptr::null_mut(), + } + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_blocked_contacts( context: *mut dc_context_t, ) -> *mut dc_array::dc_array_t { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_blocked_contacts()"); - return ptr::null_mut(); - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_blocked_contacts()"); + return ptr::null_mut(); + } + let ctx = &*context; - block_on(async move { - Box::into_raw(Box::new(dc_array_t::from( - Contact::get_all_blocked(ctx) - .await - .context("Can't get blocked contacts") - .log_err(ctx) - .unwrap_or_default() - .iter() - .map(|id| id.to_u32()) - .collect::>(), - ))) - }) + block_on(async move { + Box::into_raw(Box::new(dc_array_t::from( + Contact::get_all_blocked(ctx) + .await + .context("Can't get blocked contacts") + .log_err(ctx) + .unwrap_or_default() + .iter() + .map(|id| id.to_u32()) + .collect::>(), + ))) + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_block_contact( context: *mut dc_context_t, contact_id: u32, block: libc::c_int, ) { - let contact_id = ContactId::new(contact_id); - if context.is_null() || contact_id.is_special() { - eprintln!("ignoring careless call to dc_block_contact()"); - return; - } - let ctx = &*context; - block_on(async move { - if block == 0 { - Contact::unblock(ctx, contact_id) - .await - .context("Can't unblock contact") - .log_err(ctx) - .ok(); - } else { - Contact::block(ctx, contact_id) - .await - .context("Can't block contact") - .log_err(ctx) - .ok(); + unsafe { + let contact_id = ContactId::new(contact_id); + if context.is_null() || contact_id.is_special() { + eprintln!("ignoring careless call to dc_block_contact()"); + return; } - }); + let ctx = &*context; + block_on(async move { + if block == 0 { + Contact::unblock(ctx, contact_id) + .await + .context("Can't unblock contact") + .log_err(ctx) + .ok(); + } else { + Contact::block(ctx, contact_id) + .await + .context("Can't block contact") + .log_err(ctx) + .ok(); + } + }); + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_contact_encrinfo( context: *mut dc_context_t, contact_id: u32, ) -> *mut libc::c_char { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_contact_encrinfo()"); - return "".strdup(); - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_contact_encrinfo()"); + return "".strdup(); + } + let ctx = &*context; - block_on(Contact::get_encrinfo(ctx, ContactId::new(contact_id))) - .map(|s| s.strdup()) - .log_err(ctx) - .unwrap_or(ptr::null_mut()) + block_on(Contact::get_encrinfo(ctx, ContactId::new(contact_id))) + .map(|s| s.strdup()) + .log_err(ctx) + .unwrap_or(ptr::null_mut()) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_delete_contact( context: *mut dc_context_t, contact_id: u32, ) -> libc::c_int { - let contact_id = ContactId::new(contact_id); - if context.is_null() || contact_id.is_special() { - eprintln!("ignoring careless call to dc_delete_contact()"); - return 0; - } - let ctx = &*context; + unsafe { + let contact_id = ContactId::new(contact_id); + if context.is_null() || contact_id.is_special() { + eprintln!("ignoring careless call to dc_delete_contact()"); + return 0; + } + let ctx = &*context; - block_on(Contact::delete(ctx, contact_id)) - .context("Cannot delete contact") - .log_err(ctx) - .is_ok() as libc::c_int + block_on(Contact::delete(ctx, contact_id)) + .context("Cannot delete contact") + .log_err(ctx) + .is_ok() as libc::c_int + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_contact( context: *mut dc_context_t, contact_id: u32, ) -> *mut dc_contact_t { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_contact()"); - return ptr::null_mut(); - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_contact()"); + return ptr::null_mut(); + } + let ctx = &*context; - block_on(async move { - Contact::get_by_id(ctx, ContactId::new(contact_id)) - .await - .map(|contact| Box::into_raw(Box::new(ContactWrapper { context, contact }))) - .unwrap_or_else(|_| ptr::null_mut()) - }) + block_on(async move { + Contact::get_by_id(ctx, ContactId::new(contact_id)) + .await + .map(|contact| Box::into_raw(Box::new(ContactWrapper { context, contact }))) + .unwrap_or_else(|_| ptr::null_mut()) + }) + } } fn spawn_imex(ctx: Context, what: imex::ImexMode, param1: String, passphrase: Option) { @@ -2367,207 +2432,231 @@ fn spawn_imex(ctx: Context, what: imex::ImexMode, param1: String, passphrase: Op }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_imex( context: *mut dc_context_t, what_raw: libc::c_int, param1: *const libc::c_char, param2: *const libc::c_char, ) { - if context.is_null() { - eprintln!("ignoring careless call to dc_imex()"); - return; - } - let what = match imex::ImexMode::from_i32(what_raw) { - Some(what) => what, - None => { - eprintln!("ignoring invalid argument {what_raw} to dc_imex"); + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_imex()"); return; } - }; - let passphrase = to_opt_string_lossy(param2); + let what = match imex::ImexMode::from_i32(what_raw) { + Some(what) => what, + None => { + eprintln!("ignoring invalid argument {what_raw} to dc_imex"); + return; + } + }; + let passphrase = to_opt_string_lossy(param2); - let ctx = &*context; + let ctx = &*context; - if let Some(param1) = to_opt_string_lossy(param1) { - spawn_imex(ctx.clone(), what, param1, passphrase); - } else { - eprintln!("dc_imex called without a valid directory"); + if let Some(param1) = to_opt_string_lossy(param1) { + spawn_imex(ctx.clone(), what, param1, passphrase); + } else { + eprintln!("dc_imex called without a valid directory"); + } } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_imex_has_backup( context: *mut dc_context_t, dir: *const libc::c_char, ) -> *mut libc::c_char { - if context.is_null() || dir.is_null() { - eprintln!("ignoring careless call to dc_imex_has_backup()"); - return ptr::null_mut(); // NULL explicitly defined as "has no backup" - } - let ctx = &*context; + unsafe { + if context.is_null() || dir.is_null() { + eprintln!("ignoring careless call to dc_imex_has_backup()"); + return ptr::null_mut(); // NULL explicitly defined as "has no backup" + } + let ctx = &*context; - match block_on(imex::has_backup(ctx, to_string_lossy(dir).as_ref())) - .context("dc_imex_has_backup") - .log_err(ctx) - { - Ok(res) => res.strdup(), - Err(_) => ptr::null_mut(), + match block_on(imex::has_backup(ctx, to_string_lossy(dir).as_ref())) + .context("dc_imex_has_backup") + .log_err(ctx) + { + Ok(res) => res.strdup(), + Err(_) => ptr::null_mut(), + } } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_stop_ongoing_process(context: *mut dc_context_t) { - if context.is_null() { - eprintln!("ignoring careless call to dc_stop_ongoing_process()"); - return; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_stop_ongoing_process()"); + return; + } + let ctx = &*context; + block_on(ctx.stop_ongoing()); } - let ctx = &*context; - block_on(ctx.stop_ongoing()); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_check_qr( context: *mut dc_context_t, qr: *const libc::c_char, ) -> *mut dc_lot_t { - if context.is_null() || qr.is_null() { - eprintln!("ignoring careless call to dc_check_qr()"); - return ptr::null_mut(); - } - let ctx = &*context; + unsafe { + if context.is_null() || qr.is_null() { + eprintln!("ignoring careless call to dc_check_qr()"); + return ptr::null_mut(); + } + let ctx = &*context; - let lot = match block_on(qr::check_qr(ctx, &to_string_lossy(qr))) { - Ok(qr) => qr.into(), - Err(err) => err.into(), - }; - Box::into_raw(Box::new(lot)) + let lot = match block_on(qr::check_qr(ctx, &to_string_lossy(qr))) { + Ok(qr) => qr.into(), + Err(err) => err.into(), + }; + Box::into_raw(Box::new(lot)) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_securejoin_qr( context: *mut dc_context_t, chat_id: u32, ) -> *mut libc::c_char { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_securejoin_qr()"); - return "".strdup(); - } - let ctx = &*context; - let chat_id = if chat_id == 0 { - None - } else { - Some(ChatId::new(chat_id)) - }; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_securejoin_qr()"); + return "".strdup(); + } + let ctx = &*context; + let chat_id = if chat_id == 0 { + None + } else { + Some(ChatId::new(chat_id)) + }; - block_on(securejoin::get_securejoin_qr(ctx, chat_id)) - .unwrap_or_else(|_| "".to_string()) - .strdup() + block_on(securejoin::get_securejoin_qr(ctx, chat_id)) + .unwrap_or_else(|_| "".to_string()) + .strdup() + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_securejoin_qr_svg( context: *mut dc_context_t, chat_id: u32, ) -> *mut libc::c_char { - if context.is_null() { - eprintln!("ignoring careless call to generate_verification_qr()"); - return "".strdup(); - } - let ctx = &*context; - let chat_id = if chat_id == 0 { - None - } else { - Some(ChatId::new(chat_id)) - }; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to generate_verification_qr()"); + return "".strdup(); + } + let ctx = &*context; + let chat_id = if chat_id == 0 { + None + } else { + Some(ChatId::new(chat_id)) + }; - block_on(get_securejoin_qr_svg(ctx, chat_id)) - .unwrap_or_else(|_| "".to_string()) - .strdup() + block_on(get_securejoin_qr_svg(ctx, chat_id)) + .unwrap_or_else(|_| "".to_string()) + .strdup() + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_join_securejoin( context: *mut dc_context_t, qr: *const libc::c_char, ) -> u32 { - if context.is_null() || qr.is_null() { - eprintln!("ignoring careless call to dc_join_securejoin()"); - return 0; - } - let ctx = &*context; + unsafe { + if context.is_null() || qr.is_null() { + eprintln!("ignoring careless call to dc_join_securejoin()"); + return 0; + } + let ctx = &*context; - block_on(async move { - securejoin::join_securejoin(ctx, &to_string_lossy(qr)) - .await - .map(|chatid| chatid.to_u32()) - .context("failed dc_join_securejoin() call") - .log_err(ctx) - .unwrap_or_default() - }) + block_on(async move { + securejoin::join_securejoin(ctx, &to_string_lossy(qr)) + .await + .map(|chatid| chatid.to_u32()) + .context("failed dc_join_securejoin() call") + .log_err(ctx) + .unwrap_or_default() + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_send_locations_to_chat( context: *mut dc_context_t, chat_id: u32, seconds: libc::c_int, ) { - if context.is_null() || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() || seconds < 0 { - eprintln!("ignoring careless call to dc_send_locations_to_chat()"); - return; - } - let ctx = &*context; + unsafe { + if context.is_null() + || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL.to_u32() + || seconds < 0 + { + eprintln!("ignoring careless call to dc_send_locations_to_chat()"); + return; + } + let ctx = &*context; - block_on(location::send_to_chat( - ctx, - ChatId::new(chat_id), - seconds as i64, - )) - .context("Failed dc_send_locations_to_chat()") - .log_err(ctx) - .ok(); + block_on(location::send_to_chat( + ctx, + ChatId::new(chat_id), + seconds as i64, + )) + .context("Failed dc_send_locations_to_chat()") + .log_err(ctx) + .ok(); + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_is_sending_locations_to_chat( context: *mut dc_context_t, chat_id: u32, ) -> libc::c_int { - if context.is_null() { - eprintln!("ignoring careless call to dc_is_sending_locations_to_chat()"); - return 0; - } - let ctx = &*context; - if chat_id == 0 { - block_on(location::is_sending(ctx)) - .unwrap_or_log_default(ctx, "Failed is_sending_locations()") as libc::c_int - } else { - block_on(location::is_sending_to_chat(ctx, ChatId::new(chat_id))) - .unwrap_or_log_default(ctx, "Failed is_sending_locations_to_chat()") - as libc::c_int + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_is_sending_locations_to_chat()"); + return 0; + } + let ctx = &*context; + if chat_id == 0 { + block_on(location::is_sending(ctx)) + .unwrap_or_log_default(ctx, "Failed is_sending_locations()") + as libc::c_int + } else { + block_on(location::is_sending_to_chat(ctx, ChatId::new(chat_id))) + .unwrap_or_log_default(ctx, "Failed is_sending_locations_to_chat()") + as libc::c_int + } } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_set_location( context: *mut dc_context_t, latitude: libc::c_double, longitude: libc::c_double, accuracy: libc::c_double, ) -> libc::c_int { - if context.is_null() { - eprintln!("ignoring careless call to dc_set_location()"); - return 0; - } - let ctx = &*context; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_set_location()"); + return 0; + } + let ctx = &*context; - block_on(location::set(ctx, latitude, longitude, accuracy)) - .log_err(ctx) - .unwrap_or_default() as libc::c_int + block_on(location::set(ctx, latitude, longitude, accuracy)) + .log_err(ctx) + .unwrap_or_default() as libc::c_int + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_locations( context: *mut dc_context_t, chat_id: u32, @@ -2575,31 +2664,33 @@ pub unsafe extern "C" fn dc_get_locations( timestamp_begin: i64, timestamp_end: i64, ) -> *mut dc_array::dc_array_t { - if context.is_null() { - eprintln!("ignoring careless call to dc_get_locations()"); - return ptr::null_mut(); - } - let ctx = &*context; - let chat_id = if chat_id == 0 { - None - } else { - Some(ChatId::new(chat_id)) - }; - let contact_id = if contact_id == 0 { - None - } else { - Some(contact_id) - }; + unsafe { + if context.is_null() { + eprintln!("ignoring careless call to dc_get_locations()"); + return ptr::null_mut(); + } + let ctx = &*context; + let chat_id = if chat_id == 0 { + None + } else { + Some(ChatId::new(chat_id)) + }; + let contact_id = if contact_id == 0 { + None + } else { + Some(contact_id) + }; - block_on(async move { - let res = location::get_range(ctx, chat_id, contact_id, timestamp_begin, timestamp_end) - .await - .unwrap_or_log_default(ctx, "Failed get_locations"); - Box::into_raw(Box::new(dc_array_t::from(res))) - }) + block_on(async move { + let res = location::get_range(ctx, chat_id, contact_id, timestamp_begin, timestamp_end) + .await + .unwrap_or_log_default(ctx, "Failed get_locations"); + Box::into_raw(Box::new(dc_array_t::from(res))) + }) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_create_qr_svg(payload: *const libc::c_char) -> *mut libc::c_char { if payload.is_null() { eprintln!("ignoring careless call to dc_create_qr_svg()"); @@ -2611,13 +2702,13 @@ pub unsafe extern "C" fn dc_create_qr_svg(payload: *const libc::c_char) -> *mut .strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_get_last_error(context: *mut dc_context_t) -> *mut libc::c_char { if context.is_null() { eprintln!("ignoring careless call to dc_get_last_error()"); return "".strdup(); } - let ctx = &*context; + let ctx = unsafe { &*context }; ctx.get_last_error().strdup() } @@ -2625,35 +2716,35 @@ pub unsafe extern "C" fn dc_get_last_error(context: *mut dc_context_t) -> *mut l pub type dc_array_t = dc_array::dc_array_t; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_unref(a: *mut dc_array::dc_array_t) { if a.is_null() { eprintln!("ignoring careless call to dc_array_unref()"); return; } - drop(Box::from_raw(a)); + drop(unsafe { Box::from_raw(a) }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_get_cnt(array: *const dc_array_t) -> libc::size_t { if array.is_null() { eprintln!("ignoring careless call to dc_array_get_cnt()"); return 0; } - (*array).len() + unsafe { (*array).len() } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_get_id(array: *const dc_array_t, index: libc::size_t) -> u32 { if array.is_null() { eprintln!("ignoring careless call to dc_array_get_id()"); return 0; } - (*array).get_id(index) + unsafe { (*array).get_id(index) } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_get_latitude( array: *const dc_array_t, index: libc::size_t, @@ -2663,9 +2754,9 @@ pub unsafe extern "C" fn dc_array_get_latitude( return 0.0; } - (*array).get_location(index).latitude + unsafe { (*array).get_location(index).latitude } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_get_longitude( array: *const dc_array_t, index: libc::size_t, @@ -2675,9 +2766,9 @@ pub unsafe extern "C" fn dc_array_get_longitude( return 0.0; } - (*array).get_location(index).longitude + unsafe { (*array).get_location(index).longitude } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_get_accuracy( array: *const dc_array_t, index: libc::size_t, @@ -2687,9 +2778,9 @@ pub unsafe extern "C" fn dc_array_get_accuracy( return 0.0; } - (*array).get_location(index).accuracy + unsafe { (*array).get_location(index).accuracy } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_get_timestamp( array: *const dc_array_t, index: libc::size_t, @@ -2699,9 +2790,9 @@ pub unsafe extern "C" fn dc_array_get_timestamp( return 0; } - (*array).get_timestamp(index).unwrap_or_default() + unsafe { (*array).get_timestamp(index).unwrap_or_default() } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_get_chat_id( array: *const dc_array_t, index: libc::size_t, @@ -2710,9 +2801,10 @@ pub unsafe extern "C" fn dc_array_get_chat_id( eprintln!("ignoring careless call to dc_array_get_chat_id()"); return 0; } - (*array).get_location(index).chat_id.to_u32() + + unsafe { (*array).get_location(index).chat_id.to_u32() } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_get_contact_id( array: *const dc_array_t, index: libc::size_t, @@ -2722,9 +2814,9 @@ pub unsafe extern "C" fn dc_array_get_contact_id( return 0; } - (*array).get_location(index).contact_id.to_u32() + unsafe { (*array).get_location(index).contact_id.to_u32() } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_get_msg_id( array: *const dc_array_t, index: libc::size_t, @@ -2734,9 +2826,9 @@ pub unsafe extern "C" fn dc_array_get_msg_id( return 0; } - (*array).get_location(index).msg_id + unsafe { (*array).get_location(index).msg_id } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_get_marker( array: *const dc_array_t, index: libc::size_t, @@ -2746,14 +2838,14 @@ pub unsafe extern "C" fn dc_array_get_marker( return std::ptr::null_mut(); // NULL explicitly defined as "no markers" } - if let Some(s) = (*array).get_marker(index) { + if let Some(s) = unsafe { (*array).get_marker(index) } { s.strdup() } else { std::ptr::null_mut() } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_search_id( array: *const dc_array_t, needle: libc::c_uint, @@ -2764,9 +2856,9 @@ pub unsafe extern "C" fn dc_array_search_id( return 0; } - if let Some(i) = (*array).search_id(needle) { + if let Some(i) = unsafe { (*array).search_id(needle) } { if !ret_index.is_null() { - *ret_index = i + unsafe { *ret_index = i } } 1 } else { @@ -2778,7 +2870,7 @@ pub unsafe extern "C" fn dc_array_search_id( // Independent locations do not belong to the track of the user. // Returns 1 if location belongs to the track of the user, // 0 if location was reported independently. -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_array_is_independent( array: *const dc_array_t, index: libc::size_t, @@ -2788,7 +2880,7 @@ pub unsafe extern "C" fn dc_array_is_independent( return 0; } - (*array).get_location(index).independent as libc::c_int + unsafe { (*array).get_location(index).independent as libc::c_int } } // dc_chatlist_t @@ -2807,26 +2899,29 @@ pub struct ChatlistWrapper { pub type dc_chatlist_t = ChatlistWrapper; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chatlist_unref(chatlist: *mut dc_chatlist_t) { if chatlist.is_null() { eprintln!("ignoring careless call to dc_chatlist_unref()"); return; } - drop(Box::from_raw(chatlist)); + + unsafe { + drop(Box::from_raw(chatlist)); + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chatlist_get_cnt(chatlist: *mut dc_chatlist_t) -> libc::size_t { if chatlist.is_null() { eprintln!("ignoring careless call to dc_chatlist_get_cnt()"); return 0; } - let ffi_list = &*chatlist; + let ffi_list = unsafe { &*chatlist }; ffi_list.list.len() as libc::size_t } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chatlist_get_chat_id( chatlist: *mut dc_chatlist_t, index: libc::size_t, @@ -2835,8 +2930,8 @@ pub unsafe extern "C" fn dc_chatlist_get_chat_id( eprintln!("ignoring careless call to dc_chatlist_get_chat_id()"); return 0; } - let ffi_list = &*chatlist; - let ctx = &*ffi_list.context; + let ffi_list = unsafe { &*chatlist }; + let ctx = unsafe { &*ffi_list.context }; match ffi_list .list .get_chat_id(index) @@ -2848,7 +2943,7 @@ pub unsafe extern "C" fn dc_chatlist_get_chat_id( } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chatlist_get_msg_id( chatlist: *mut dc_chatlist_t, index: libc::size_t, @@ -2857,8 +2952,8 @@ pub unsafe extern "C" fn dc_chatlist_get_msg_id( eprintln!("ignoring careless call to dc_chatlist_get_msg_id()"); return 0; } - let ffi_list = &*chatlist; - let ctx = &*ffi_list.context; + let ffi_list = unsafe { &*chatlist }; + let ctx = unsafe { &*ffi_list.context }; match ffi_list .list .get_msg_id(index) @@ -2870,7 +2965,7 @@ pub unsafe extern "C" fn dc_chatlist_get_msg_id( } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chatlist_get_summary( chatlist: *mut dc_chatlist_t, index: libc::size_t, @@ -2883,25 +2978,20 @@ pub unsafe extern "C" fn dc_chatlist_get_summary( let maybe_chat = if chat.is_null() { None } else { - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; Some(&ffi_chat.chat) }; - let ffi_list = &*chatlist; - let ctx = &*ffi_list.context; + let ffi_list = unsafe { &*chatlist }; + let ctx = unsafe { &*ffi_list.context }; - block_on(async move { - let summary = ffi_list - .list - .get_summary(ctx, index, maybe_chat) - .await - .context("get_summary failed") - .log_err(ctx) - .unwrap_or_default(); - Box::into_raw(Box::new(summary.into())) - }) + let summary = block_on(ffi_list.list.get_summary(ctx, index, maybe_chat)) + .context("get_summary failed") + .log_err(ctx) + .unwrap_or_default(); + Box::into_raw(Box::new(summary.into())) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chatlist_get_summary2( context: *mut dc_context_t, chat_id: u32, @@ -2911,7 +3001,7 @@ pub unsafe extern "C" fn dc_chatlist_get_summary2( eprintln!("ignoring careless call to dc_chatlist_get_summary2()"); return ptr::null_mut(); } - let ctx = &*context; + let ctx = unsafe { &*context }; let msg_id = if msg_id == 0 { None } else { @@ -2929,7 +3019,7 @@ pub unsafe extern "C" fn dc_chatlist_get_summary2( Box::into_raw(Box::new(summary.into())) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chatlist_get_context( chatlist: *mut dc_chatlist_t, ) -> *const dc_context_t { @@ -2937,7 +3027,7 @@ pub unsafe extern "C" fn dc_chatlist_get_context( eprintln!("ignoring careless call to dc_chatlist_get_context()"); return ptr::null_mut(); } - let ffi_list = &*chatlist; + let ffi_list = unsafe { &*chatlist }; ffi_list.context } @@ -2957,53 +3047,53 @@ pub struct ChatWrapper { pub type dc_chat_t = ChatWrapper; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_unref(chat: *mut dc_chat_t) { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_unref()"); return; } - drop(Box::from_raw(chat)); + drop(unsafe { Box::from_raw(chat) }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_get_id(chat: *mut dc_chat_t) -> u32 { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_get_id()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; ffi_chat.chat.get_id().to_u32() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_get_type(chat: *mut dc_chat_t) -> libc::c_int { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_get_type()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; ffi_chat.chat.get_type() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_get_name(chat: *mut dc_chat_t) -> *mut libc::c_char { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_get_name()"); return "".strdup(); } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; ffi_chat.chat.get_name().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_get_mailinglist_addr(chat: *mut dc_chat_t) -> *mut libc::c_char { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_get_mailinglist_addr()"); return "".strdup(); } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; ffi_chat .chat .get_mailinglist_addr() @@ -3011,48 +3101,45 @@ pub unsafe extern "C" fn dc_chat_get_mailinglist_addr(chat: *mut dc_chat_t) -> * .strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_get_profile_image(chat: *mut dc_chat_t) -> *mut libc::c_char { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_get_profile_image()"); return ptr::null_mut(); // NULL explicitly defined as "no image" } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; - block_on(async move { - match ffi_chat - .chat - .get_profile_image(&ffi_chat.context) - .await - .context("Failed to get profile image") - .log_err(&ffi_chat.context) - .unwrap_or_default() - { - Some(p) => p.to_string_lossy().strdup(), - None => ptr::null_mut(), - } - }) + match block_on(ffi_chat.chat.get_profile_image(&ffi_chat.context)) + .context("Failed to get profile image") + .log_err(&ffi_chat.context) + .unwrap_or_default() + { + Some(p) => p.to_string_lossy().strdup(), + None => ptr::null_mut(), + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_get_color(chat: *mut dc_chat_t) -> u32 { - if chat.is_null() { - eprintln!("ignoring careless call to dc_chat_get_color()"); - return 0; - } - let ffi_chat = &*chat; + unsafe { + if chat.is_null() { + eprintln!("ignoring careless call to dc_chat_get_color()"); + return 0; + } + let ffi_chat = &*chat; - block_on(ffi_chat.chat.get_color(&ffi_chat.context)) - .unwrap_or_log_default(&ffi_chat.context, "Failed get_color") + block_on(ffi_chat.chat.get_color(&ffi_chat.context)) + .unwrap_or_log_default(&ffi_chat.context, "Failed get_color") + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_get_visibility(chat: *mut dc_chat_t) -> libc::c_int { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_get_visibility()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; match ffi_chat.chat.visibility { ChatVisibility::Normal => 0, ChatVisibility::Archived => 1, @@ -3060,103 +3147,103 @@ pub unsafe extern "C" fn dc_chat_get_visibility(chat: *mut dc_chat_t) -> libc::c } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_is_contact_request(chat: *mut dc_chat_t) -> libc::c_int { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_is_contact_request()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; ffi_chat.chat.is_contact_request() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_is_unpromoted(chat: *mut dc_chat_t) -> libc::c_int { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_is_unpromoted()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; ffi_chat.chat.is_unpromoted() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_is_self_talk(chat: *mut dc_chat_t) -> libc::c_int { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_is_self_talk()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; ffi_chat.chat.is_self_talk() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_is_device_talk(chat: *mut dc_chat_t) -> libc::c_int { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_is_device_talk()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; ffi_chat.chat.is_device_talk() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_can_send(chat: *mut dc_chat_t) -> libc::c_int { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_can_send()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; block_on(ffi_chat.chat.can_send(&ffi_chat.context)) .context("can_send failed") .log_err(&ffi_chat.context) .unwrap_or_default() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn dc_chat_is_protected(_chat: *mut dc_chat_t) -> libc::c_int { 0 } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_is_encrypted(chat: *mut dc_chat_t) -> libc::c_int { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_is_encrypted()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; block_on(ffi_chat.chat.is_encrypted(&ffi_chat.context)) .unwrap_or_log_default(&ffi_chat.context, "Failed dc_chat_is_encrypted") as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_is_sending_locations(chat: *mut dc_chat_t) -> libc::c_int { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_is_sending_locations()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; ffi_chat.chat.is_sending_locations() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_is_muted(chat: *mut dc_chat_t) -> libc::c_int { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_is_muted()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; ffi_chat.chat.is_muted() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_get_remaining_mute_duration(chat: *mut dc_chat_t) -> i64 { if chat.is_null() { eprintln!("ignoring careless call to dc_chat_get_remaining_mute_duration()"); return 0; } - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; if !ffi_chat.chat.is_muted() { return 0; } @@ -3171,7 +3258,7 @@ pub unsafe extern "C" fn dc_chat_get_remaining_mute_duration(chat: *mut dc_chat_ } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_chat_get_info_json( context: *mut dc_context_t, chat_id: u32, @@ -3180,28 +3267,23 @@ pub unsafe extern "C" fn dc_chat_get_info_json( eprintln!("ignoring careless call to dc_chat_get_info_json()"); return "".strdup(); } - let ctx = &*context; + let ctx = unsafe { &*context }; - block_on(async move { - let Ok(chat) = chat::Chat::load_from_db(ctx, ChatId::new(chat_id)) - .await - .context("dc_get_chat_info_json() failed to load chat") - .log_err(ctx) - else { - return "".strdup(); - }; - let Ok(info) = chat - .get_info(ctx) - .await - .context("dc_get_chat_info_json() failed to get chat info") - .log_err(ctx) - else { - return "".strdup(); - }; - serde_json::to_string(&info) - .unwrap_or_log_default(ctx, "dc_get_chat_info_json() failed to serialise to json") - .strdup() - }) + let Ok(chat) = block_on(chat::Chat::load_from_db(ctx, ChatId::new(chat_id))) + .context("dc_get_chat_info_json() failed to load chat") + .log_err(ctx) + else { + return "".strdup(); + }; + let Ok(info) = block_on(chat.get_info(ctx)) + .context("dc_get_chat_info_json() failed to get chat info") + .log_err(ctx) + else { + return "".strdup(); + }; + serde_json::to_string(&info) + .unwrap_or_log_default(ctx, "dc_get_chat_info_json() failed to serialise to json") + .strdup() } // dc_msg_t @@ -3220,7 +3302,7 @@ pub struct MessageWrapper { pub type dc_msg_t = MessageWrapper; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_new( context: *mut dc_context_t, viewtype: libc::c_int, @@ -3229,7 +3311,7 @@ pub unsafe extern "C" fn dc_msg_new( eprintln!("ignoring careless call to dc_msg_new()"); return ptr::null_mut(); } - let context = &*context; + let context = unsafe { &*context }; let viewtype = from_prim(viewtype).expect(&format!("invalid viewtype = {viewtype}")); let msg = MessageWrapper { context, @@ -3238,53 +3320,53 @@ pub unsafe extern "C" fn dc_msg_new( Box::into_raw(Box::new(msg)) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_unref(msg: *mut dc_msg_t) { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_unref()"); return; } - drop(Box::from_raw(msg)); + drop(unsafe { Box::from_raw(msg) }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_id(msg: *mut dc_msg_t) -> u32 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_id()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_id().to_u32() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_from_id(msg: *mut dc_msg_t) -> u32 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_from_id()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_from_id().to_u32() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_chat_id(msg: *mut dc_msg_t) -> u32 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_chat_id()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_chat_id().to_u32() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_viewtype(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_viewtype()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg .message .get_viewtype() @@ -3292,84 +3374,84 @@ pub unsafe extern "C" fn dc_msg_get_viewtype(msg: *mut dc_msg_t) -> libc::c_int .expect("impossible: Viewtype -> i64 conversion failed") as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_state(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_state()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_state() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_download_state(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_download_state()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.download_state() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_timestamp(msg: *mut dc_msg_t) -> i64 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_received_timestamp()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_timestamp() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_received_timestamp(msg: *mut dc_msg_t) -> i64 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_received_timestamp()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_received_timestamp() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_sort_timestamp(msg: *mut dc_msg_t) -> i64 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_sort_timestamp()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_sort_timestamp() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_text(msg: *mut dc_msg_t) -> *mut libc::c_char { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_text()"); return "".strdup(); } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_text().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_subject(msg: *mut dc_msg_t) -> *mut libc::c_char { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_subject()"); return "".strdup(); } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_subject().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_file(msg: *mut dc_msg_t) -> *mut libc::c_char { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_file()"); return "".strdup(); } - let ffi_msg = &*msg; - let ctx = &*ffi_msg.context; + let ffi_msg = unsafe { &*msg }; + let ctx = unsafe { &*ffi_msg.context }; ffi_msg .message .get_file(ctx) @@ -3377,7 +3459,7 @@ pub unsafe extern "C" fn dc_msg_get_file(msg: *mut dc_msg_t) -> *mut libc::c_cha .unwrap_or_else(|| "".strdup()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_save_file( msg: *mut dc_msg_t, path: *const libc::c_char, @@ -3386,8 +3468,8 @@ pub unsafe extern "C" fn dc_msg_save_file( eprintln!("ignoring careless call to dc_msg_save_file()"); return 0; } - let ffi_msg = &*msg; - let ctx = &*ffi_msg.context; + let ffi_msg = unsafe { &*msg }; + let ctx = unsafe { &*ffi_msg.context }; let path = to_string_lossy(path); let r = block_on( ffi_msg @@ -3405,17 +3487,17 @@ pub unsafe extern "C" fn dc_msg_save_file( } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_filename(msg: *mut dc_msg_t) -> *mut libc::c_char { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_filename()"); return "".strdup(); } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_filename().unwrap_or_default().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_webxdc_blob( msg: *mut dc_msg_t, filename: *const libc::c_char, @@ -3425,8 +3507,8 @@ pub unsafe extern "C" fn dc_msg_get_webxdc_blob( eprintln!("ignoring careless call to dc_msg_get_webxdc_blob()"); return ptr::null_mut(); } - let ffi_msg = &*msg; - let ctx = &*ffi_msg.context; + let ffi_msg = unsafe { &*msg }; + let ctx = unsafe { &*ffi_msg.context }; let blob = block_on(async move { ffi_msg .message @@ -3434,12 +3516,12 @@ pub unsafe extern "C" fn dc_msg_get_webxdc_blob( .await }); match blob { - Ok(blob) => { + Ok(blob) => unsafe { *ret_bytes = blob.len(); let ptr = libc::malloc(*ret_bytes); libc::memcpy(ptr, blob.as_ptr() as *mut libc::c_void, *ret_bytes); ptr as *mut libc::c_char - } + }, Err(err) => { eprintln!("failed read blob from archive: {err}"); ptr::null_mut() @@ -3447,14 +3529,14 @@ pub unsafe extern "C" fn dc_msg_get_webxdc_blob( } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_webxdc_info(msg: *mut dc_msg_t) -> *mut libc::c_char { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_webxdc_info()"); return "".strdup(); } - let ffi_msg = &*msg; - let ctx = &*ffi_msg.context; + let ffi_msg = unsafe { &*msg }; + let ctx = unsafe { &*ffi_msg.context }; let Ok(info) = block_on(ffi_msg.message.get_webxdc_info(ctx)) .context("dc_msg_get_webxdc_info() failed to get info") @@ -3467,13 +3549,13 @@ pub unsafe extern "C" fn dc_msg_get_webxdc_info(msg: *mut dc_msg_t) -> *mut libc .strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_filemime(msg: *mut dc_msg_t) -> *mut libc::c_char { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_filemime()"); return "".strdup(); } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; if let Some(x) = ffi_msg.message.get_filemime() { x.strdup() } else { @@ -3481,91 +3563,91 @@ pub unsafe extern "C" fn dc_msg_get_filemime(msg: *mut dc_msg_t) -> *mut libc::c } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_filebytes(msg: *mut dc_msg_t) -> u64 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_filebytes()"); return 0; } - let ffi_msg = &*msg; - let ctx = &*ffi_msg.context; + let ffi_msg = unsafe { &*msg }; + let ctx = unsafe { &*ffi_msg.context }; block_on(ffi_msg.message.get_filebytes(ctx)) .unwrap_or_log_default(ctx, "Cannot get file size") .unwrap_or_default() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_width(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_width()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_width() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_height(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_height()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_height() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_duration(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_duration()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_duration() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_showpadlock(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_showpadlock()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_showpadlock() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_is_bot(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_is_bot()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.is_bot() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_ephemeral_timer(msg: *mut dc_msg_t) -> u32 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_ephemeral_timer()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_ephemeral_timer().to_u32() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_ephemeral_timestamp(msg: *mut dc_msg_t) -> i64 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_ephemeral_timer()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_ephemeral_timestamp() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_summary( msg: *mut dc_msg_t, chat: *mut dc_chat_t, @@ -3577,11 +3659,11 @@ pub unsafe extern "C" fn dc_msg_get_summary( let maybe_chat = if chat.is_null() { None } else { - let ffi_chat = &*chat; + let ffi_chat = unsafe { &*chat }; Some(&ffi_chat.chat) }; - let ffi_msg = &mut *msg; - let ctx = &*ffi_msg.context; + let ffi_msg = unsafe { &mut *msg }; + let ctx = unsafe { &*ffi_msg.context }; let summary = block_on(ffi_msg.message.get_summary(ctx, maybe_chat)) .context("dc_msg_get_summary failed") @@ -3590,7 +3672,7 @@ pub unsafe extern "C" fn dc_msg_get_summary( Box::into_raw(Box::new(summary.into())) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_summarytext( msg: *mut dc_msg_t, approx_characters: libc::c_int, @@ -3599,8 +3681,8 @@ pub unsafe extern "C" fn dc_msg_get_summarytext( eprintln!("ignoring careless call to dc_msg_get_summarytext()"); return "".strdup(); } - let ffi_msg = &mut *msg; - let ctx = &*ffi_msg.context; + let ffi_msg = unsafe { &mut *msg }; + let ctx = unsafe { &*ffi_msg.context }; let summary = block_on(ffi_msg.message.get_summary(ctx, None)) .context("dc_msg_get_summarytext failed") @@ -3612,153 +3694,153 @@ pub unsafe extern "C" fn dc_msg_get_summarytext( } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_override_sender_name(msg: *mut dc_msg_t) -> *mut libc::c_char { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_override_sender_name()"); return "".strdup(); } - let ffi_msg = &mut *msg; + let ffi_msg = unsafe { &mut *msg }; ffi_msg.message.get_override_sender_name().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_has_deviating_timestamp(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_has_deviating_timestamp()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.has_deviating_timestamp().into() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_has_location(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_has_location()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.has_location() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_is_sent(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_is_sent()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.is_sent().into() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_is_forwarded(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_is_forwarded()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.is_forwarded().into() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_is_edited(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_is_edited()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.is_edited().into() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_is_info(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_is_info()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.is_info().into() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_info_type(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_info_type()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_info_type() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_info_contact_id(msg: *mut dc_msg_t) -> u32 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_info_contact_id()"); return 0; } - let ffi_msg = &*msg; - let context = &*ffi_msg.context; + let ffi_msg = unsafe { &*msg }; + let context = unsafe { &*ffi_msg.context }; block_on(ffi_msg.message.get_info_contact_id(context)) .unwrap_or_default() .map(|id| id.to_u32()) .unwrap_or_default() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_webxdc_href(msg: *mut dc_msg_t) -> *mut libc::c_char { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_webxdc_href()"); return "".strdup(); } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.get_webxdc_href().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_has_html(msg: *mut dc_msg_t) -> libc::c_int { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_has_html()"); return 0; } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; ffi_msg.message.has_html().into() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_set_text(msg: *mut dc_msg_t, text: *const libc::c_char) { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_set_text()"); return; } - let ffi_msg = &mut *msg; + let ffi_msg = unsafe { &mut *msg }; ffi_msg.message.set_text(to_string_lossy(text)) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_set_html(msg: *mut dc_msg_t, html: *const libc::c_char) { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_set_html()"); return; } - let ffi_msg = &mut *msg; + let ffi_msg = unsafe { &mut *msg }; ffi_msg.message.set_html(to_opt_string_lossy(html)) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_set_subject(msg: *mut dc_msg_t, subject: *const libc::c_char) { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_subject()"); return; } - let ffi_msg = &mut *msg; + let ffi_msg = unsafe { &mut *msg }; ffi_msg.message.set_subject(to_string_lossy(subject)); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_set_override_sender_name( msg: *mut dc_msg_t, name: *const libc::c_char, @@ -3767,13 +3849,13 @@ pub unsafe extern "C" fn dc_msg_set_override_sender_name( eprintln!("ignoring careless call to dc_msg_set_override_sender_name()"); return; } - let ffi_msg = &mut *msg; + let ffi_msg = unsafe { &mut *msg }; ffi_msg .message .set_override_sender_name(to_opt_string_lossy(name)) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_set_file_and_deduplicate( msg: *mut dc_msg_t, file: *const libc::c_char, @@ -3784,8 +3866,8 @@ pub unsafe extern "C" fn dc_msg_set_file_and_deduplicate( eprintln!("ignoring careless call to dc_msg_set_file_and_deduplicate()"); return; } - let ffi_msg = &mut *msg; - let ctx = &*ffi_msg.context; + let ffi_msg = unsafe { &mut *msg }; + let ctx = unsafe { &*ffi_msg.context }; ffi_msg .message @@ -3796,11 +3878,11 @@ pub unsafe extern "C" fn dc_msg_set_file_and_deduplicate( to_opt_string_lossy(filemime).as_deref(), ) .context("Failed to set file") - .log_err(&*ffi_msg.context) + .log_err(ctx) .ok(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_set_dimension( msg: *mut dc_msg_t, width: libc::c_int, @@ -3810,21 +3892,21 @@ pub unsafe extern "C" fn dc_msg_set_dimension( eprintln!("ignoring careless call to dc_msg_set_dimension()"); return; } - let ffi_msg = &mut *msg; + let ffi_msg = unsafe { &mut *msg }; ffi_msg.message.set_dimension(width, height) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_set_duration(msg: *mut dc_msg_t, duration: libc::c_int) { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_set_duration()"); return; } - let ffi_msg = &mut *msg; + let ffi_msg = unsafe { &mut *msg }; ffi_msg.message.set_duration(duration) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_set_location( msg: *mut dc_msg_t, latitude: libc::c_double, @@ -3834,11 +3916,11 @@ pub unsafe extern "C" fn dc_msg_set_location( eprintln!("ignoring careless call to dc_msg_set_location()"); return; } - let ffi_msg = &mut *msg; + let ffi_msg = unsafe { &mut *msg }; ffi_msg.message.set_location(latitude, longitude) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_latefiling_mediasize( msg: *mut dc_msg_t, width: libc::c_int, @@ -3849,8 +3931,8 @@ pub unsafe extern "C" fn dc_msg_latefiling_mediasize( eprintln!("ignoring careless call to dc_msg_latefiling_mediasize()"); return; } - let ffi_msg = &mut *msg; - let ctx = &*ffi_msg.context; + let ffi_msg = unsafe { &mut *msg }; + let ctx = unsafe { &*ffi_msg.context }; block_on({ ffi_msg @@ -3862,30 +3944,30 @@ pub unsafe extern "C" fn dc_msg_latefiling_mediasize( .ok(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_error(msg: *mut dc_msg_t) -> *mut libc::c_char { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_error()"); return ptr::null_mut(); } - let ffi_msg = &*msg; + let ffi_msg = unsafe { &*msg }; match ffi_msg.message.error() { Some(error) => error.strdup(), None => ptr::null_mut(), } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_set_quote(msg: *mut dc_msg_t, quote: *const dc_msg_t) { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_set_quote()"); return; } - let ffi_msg = &mut *msg; + let ffi_msg = unsafe { &mut *msg }; let quote_msg = if quote.is_null() { None } else { - let ffi_quote = &*quote; + let ffi_quote = unsafe { &*quote }; if ffi_msg.context != ffi_quote.context { eprintln!("ignoring attempt to quote message from a different context"); return; @@ -3893,47 +3975,38 @@ pub unsafe extern "C" fn dc_msg_set_quote(msg: *mut dc_msg_t, quote: *const dc_m Some(&ffi_quote.message) }; - block_on(async move { - ffi_msg - .message - .set_quote(&*ffi_msg.context, quote_msg) - .await - .context("failed to set quote") - .log_err(&*ffi_msg.context) - .ok(); - }); + let context = unsafe { &*ffi_msg.context }; + block_on(ffi_msg.message.set_quote(context, quote_msg)) + .context("failed to set quote") + .log_err(context) + .ok(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_quoted_text(msg: *const dc_msg_t) -> *mut libc::c_char { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_quoted_text()"); return ptr::null_mut(); } - let ffi_msg: &MessageWrapper = &*msg; + let ffi_msg: &MessageWrapper = unsafe { &*msg }; ffi_msg .message .quoted_text() .map_or_else(ptr::null_mut, |s| s.strdup()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_quoted_msg(msg: *const dc_msg_t) -> *mut dc_msg_t { if msg.is_null() { eprintln!("ignoring careless call to dc_get_quoted_msg()"); return ptr::null_mut(); } - let ffi_msg: &MessageWrapper = &*msg; - let context = &*ffi_msg.context; - let res = block_on(async move { - ffi_msg - .message - .quoted_message(context) - .await - .context("failed to get quoted message") - .log_err(context) - .unwrap_or(None) - }); + let ffi_msg: &MessageWrapper = unsafe { &*msg }; + let context = unsafe { &*ffi_msg.context }; + let res = block_on(ffi_msg.message.quoted_message(context)) + .context("failed to get quoted message") + .log_err(context) + .unwrap_or(None); match res { Some(message) => Box::into_raw(Box::new(MessageWrapper { context, message })), @@ -3941,23 +4014,18 @@ pub unsafe extern "C" fn dc_msg_get_quoted_msg(msg: *const dc_msg_t) -> *mut dc_ } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_parent(msg: *const dc_msg_t) -> *mut dc_msg_t { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_parent()"); return ptr::null_mut(); } - let ffi_msg: &MessageWrapper = &*msg; - let context = &*ffi_msg.context; - let res = block_on(async move { - ffi_msg - .message - .parent(context) - .await - .context("failed to get parent message") - .log_err(context) - .unwrap_or(None) - }); + let ffi_msg: &MessageWrapper = unsafe { &*msg }; + let context = unsafe { &*ffi_msg.context }; + let res = block_on(ffi_msg.message.parent(context)) + .context("failed to get parent message") + .log_err(context) + .unwrap_or(None); match res { Some(message) => Box::into_raw(Box::new(MessageWrapper { context, message })), @@ -3965,46 +4033,36 @@ pub unsafe extern "C" fn dc_msg_get_parent(msg: *const dc_msg_t) -> *mut dc_msg_ } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_original_msg_id(msg: *const dc_msg_t) -> u32 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_original_msg_id()"); return 0; } - let ffi_msg: &MessageWrapper = &*msg; - let context = &*ffi_msg.context; - block_on(async move { - ffi_msg - .message - .get_original_msg_id(context) - .await - .context("failed to get original message") - .log_err(context) - .unwrap_or_default() - .map(|id| id.to_u32()) - .unwrap_or(0) - }) + let ffi_msg: &MessageWrapper = unsafe { &*msg }; + let context = unsafe { &*ffi_msg.context }; + block_on(ffi_msg.message.get_original_msg_id(context)) + .context("failed to get original message") + .log_err(context) + .unwrap_or_default() + .map(|id| id.to_u32()) + .unwrap_or(0) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_msg_get_saved_msg_id(msg: *const dc_msg_t) -> u32 { if msg.is_null() { eprintln!("ignoring careless call to dc_msg_get_saved_msg_id()"); return 0; } - let ffi_msg: &MessageWrapper = &*msg; - let context = &*ffi_msg.context; - block_on(async move { - ffi_msg - .message - .get_saved_msg_id(context) - .await - .context("failed to get original message") - .log_err(context) - .unwrap_or_default() - .map(|id| id.to_u32()) - .unwrap_or(0) - }) + let ffi_msg: &MessageWrapper = unsafe { &*msg }; + let context = unsafe { &*ffi_msg.context }; + block_on(ffi_msg.message.get_saved_msg_id(context)) + .context("failed to get original message") + .log_err(context) + .unwrap_or_default() + .map(|id| id.to_u32()) + .unwrap_or(0) } // dc_contact_t @@ -4023,56 +4081,56 @@ pub struct ContactWrapper { pub type dc_contact_t = ContactWrapper; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_unref(contact: *mut dc_contact_t) { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_unref()"); return; } - drop(Box::from_raw(contact)); + drop(unsafe { Box::from_raw(contact) }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_id(contact: *mut dc_contact_t) -> u32 { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_get_id()"); return 0; } - let ffi_contact = &*contact; + let ffi_contact = unsafe { &*contact }; ffi_contact.contact.get_id().to_u32() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_addr(contact: *mut dc_contact_t) -> *mut libc::c_char { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_get_addr()"); return "".strdup(); } - let ffi_contact = &*contact; + let ffi_contact = unsafe { &*contact }; ffi_contact.contact.get_addr().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_name(contact: *mut dc_contact_t) -> *mut libc::c_char { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_get_name()"); return "".strdup(); } - let ffi_contact = &*contact; + let ffi_contact = unsafe { &*contact }; ffi_contact.contact.get_name().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_auth_name(contact: *mut dc_contact_t) -> *mut libc::c_char { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_get_auth_name()"); return "".strdup(); } - let ffi_contact = &*contact; + let ffi_contact = unsafe { &*contact }; ffi_contact.contact.get_authname().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_display_name( contact: *mut dc_contact_t, ) -> *mut libc::c_char { @@ -4080,11 +4138,11 @@ pub unsafe extern "C" fn dc_contact_get_display_name( eprintln!("ignoring careless call to dc_contact_get_display_name()"); return "".strdup(); } - let ffi_contact = &*contact; + let ffi_contact = unsafe { &*contact }; ffi_contact.contact.get_display_name().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_name_n_addr( contact: *mut dc_contact_t, ) -> *mut libc::c_char { @@ -4092,11 +4150,11 @@ pub unsafe extern "C" fn dc_contact_get_name_n_addr( eprintln!("ignoring careless call to dc_contact_get_name_n_addr()"); return "".strdup(); } - let ffi_contact = &*contact; + let ffi_contact = unsafe { &*contact }; ffi_contact.contact.get_name_n_addr().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_profile_image( contact: *mut dc_contact_t, ) -> *mut libc::c_char { @@ -4104,88 +4162,82 @@ pub unsafe extern "C" fn dc_contact_get_profile_image( eprintln!("ignoring careless call to dc_contact_get_profile_image()"); return ptr::null_mut(); // NULL explicitly defined as "no profile image" } - let ffi_contact = &*contact; - let ctx = &*ffi_contact.context; + let ffi_contact = unsafe { &*contact }; + let ctx = unsafe { &*ffi_contact.context }; - block_on(async move { - ffi_contact - .contact - .get_profile_image(ctx) - .await - .unwrap_or_log_default(ctx, "failed to get profile image") - .map(|p| p.to_string_lossy().strdup()) - .unwrap_or_else(std::ptr::null_mut) - }) + block_on(ffi_contact.contact.get_profile_image(ctx)) + .unwrap_or_log_default(ctx, "failed to get profile image") + .map(|p| p.to_string_lossy().strdup()) + .unwrap_or_else(std::ptr::null_mut) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_color(contact: *mut dc_contact_t) -> u32 { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_get_color()"); return 0; } - let ffi_contact = &*contact; - let ctx = &*ffi_contact.context; - block_on(async move { + let ffi_contact = unsafe { &*contact }; + let ctx = unsafe { &*ffi_contact.context }; + block_on( ffi_contact .contact // We don't want any UIs displaying gray self-color. - .get_or_gen_color(ctx) - .await - .context("Contact::get_color()") - .log_err(ctx) - .unwrap_or(0) - }) + .get_or_gen_color(ctx), + ) + .context("Contact::get_color()") + .log_err(ctx) + .unwrap_or(0) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_status(contact: *mut dc_contact_t) -> *mut libc::c_char { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_get_status()"); return "".strdup(); } - let ffi_contact = &*contact; + let ffi_contact = unsafe { &*contact }; ffi_contact.contact.get_status().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_last_seen(contact: *mut dc_contact_t) -> i64 { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_get_last_seen()"); return 0; } - let ffi_contact = &*contact; + let ffi_contact = unsafe { &*contact }; ffi_contact.contact.last_seen() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_was_seen_recently(contact: *mut dc_contact_t) -> libc::c_int { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_was_seen_recently()"); return 0; } - let ffi_contact = &*contact; + let ffi_contact = unsafe { &*contact }; ffi_contact.contact.was_seen_recently() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_is_blocked(contact: *mut dc_contact_t) -> libc::c_int { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_is_blocked()"); return 0; } - let ffi_contact = &*contact; + let ffi_contact = unsafe { &*contact }; ffi_contact.contact.is_blocked() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_is_verified(contact: *mut dc_contact_t) -> libc::c_int { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_is_verified()"); return 0; } - let ffi_contact = &*contact; - let ctx = &*ffi_contact.context; + let ffi_contact = unsafe { &*contact }; + let ctx = unsafe { &*ffi_contact.context }; if block_on(ffi_contact.contact.is_verified(ctx)) .context("is_verified failed") @@ -4200,32 +4252,32 @@ pub unsafe extern "C" fn dc_contact_is_verified(contact: *mut dc_contact_t) -> l } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_is_bot(contact: *mut dc_contact_t) -> libc::c_int { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_is_bot()"); return 0; } - (*contact).contact.is_bot() as libc::c_int + unsafe { (*contact).contact.is_bot() as libc::c_int } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_is_key_contact(contact: *mut dc_contact_t) -> libc::c_int { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_is_key_contact()"); return 0; } - (*contact).contact.is_key_contact() as libc::c_int + unsafe { (*contact).contact.is_key_contact() as libc::c_int } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_contact_get_verifier_id(contact: *mut dc_contact_t) -> u32 { if contact.is_null() { eprintln!("ignoring careless call to dc_contact_get_verifier_id()"); return 0; } - let ffi_contact = &*contact; - let ctx = &*ffi_contact.context; + let ffi_contact = unsafe { &*contact }; + let ctx = unsafe { &*ffi_contact.context }; let verifier_contact_id = block_on(ffi_contact.contact.get_verifier_id(ctx)) .context("failed to get verifier") .log_err(ctx) @@ -4239,85 +4291,85 @@ pub unsafe extern "C" fn dc_contact_get_verifier_id(contact: *mut dc_contact_t) pub type dc_lot_t = lot::Lot; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_lot_unref(lot: *mut dc_lot_t) { if lot.is_null() { eprintln!("ignoring careless call to dc_lot_unref()"); return; } - drop(Box::from_raw(lot)); + drop(unsafe { Box::from_raw(lot) }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_lot_get_text1(lot: *mut dc_lot_t) -> *mut libc::c_char { if lot.is_null() { eprintln!("ignoring careless call to dc_lot_get_text1()"); return ptr::null_mut(); // NULL explicitly defined as "there is no such text" } - let lot = &*lot; + let lot = unsafe { &*lot }; lot.get_text1().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_lot_get_text2(lot: *mut dc_lot_t) -> *mut libc::c_char { if lot.is_null() { eprintln!("ignoring careless call to dc_lot_get_text2()"); return ptr::null_mut(); // NULL explicitly defined as "there is no such text" } - let lot = &*lot; + let lot = unsafe { &*lot }; lot.get_text2().strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_lot_get_text1_meaning(lot: *mut dc_lot_t) -> libc::c_int { if lot.is_null() { eprintln!("ignoring careless call to dc_lot_get_text1_meaning()"); return 0; } - let lot = &*lot; + let lot = unsafe { &*lot }; lot.get_text1_meaning() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_lot_get_state(lot: *mut dc_lot_t) -> libc::c_int { if lot.is_null() { eprintln!("ignoring careless call to dc_lot_get_state()"); return 0; } - let lot = &*lot; + let lot = unsafe { &*lot }; lot.get_state() as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_lot_get_id(lot: *mut dc_lot_t) -> u32 { if lot.is_null() { eprintln!("ignoring careless call to dc_lot_get_id()"); return 0; } - let lot = &*lot; + let lot = unsafe { &*lot }; lot.get_id() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_lot_get_timestamp(lot: *mut dc_lot_t) -> i64 { if lot.is_null() { eprintln!("ignoring careless call to dc_lot_get_timestamp()"); return 0; } - let lot = &*lot; + let lot = unsafe { &*lot }; lot.get_timestamp() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_str_unref(s: *mut libc::c_char) { - libc::free(s as *mut _) + unsafe { libc::free(s as *mut _) } } pub struct BackupProviderWrapper { @@ -4327,7 +4379,7 @@ pub struct BackupProviderWrapper { pub type dc_backup_provider_t = BackupProviderWrapper; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_backup_provider_new( context: *mut dc_context_t, ) -> *mut dc_backup_provider_t { @@ -4335,7 +4387,7 @@ pub unsafe extern "C" fn dc_backup_provider_new( eprintln!("ignoring careless call to dc_backup_provider_new()"); return ptr::null_mut(); } - let ctx = &*context; + let ctx = unsafe { &*context }; block_on(BackupProvider::prepare(ctx)) .map(|provider| BackupProviderWrapper { context: ctx, @@ -4348,7 +4400,7 @@ pub unsafe extern "C" fn dc_backup_provider_new( .unwrap_or(ptr::null_mut()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_backup_provider_get_qr( provider: *const dc_backup_provider_t, ) -> *mut libc::c_char { @@ -4356,8 +4408,8 @@ pub unsafe extern "C" fn dc_backup_provider_get_qr( eprintln!("ignoring careless call to dc_backup_provider_qr"); return "".strdup(); } - let ffi_provider = &*provider; - let ctx = &*ffi_provider.context; + let ffi_provider = unsafe { &*provider }; + let ctx = unsafe { &*ffi_provider.context }; deltachat::qr::format_backup(&ffi_provider.provider.qr()) .context("BackupProvider get_qr failed") .log_err(ctx) @@ -4366,7 +4418,7 @@ pub unsafe extern "C" fn dc_backup_provider_get_qr( .strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_backup_provider_get_qr_svg( provider: *const dc_backup_provider_t, ) -> *mut libc::c_char { @@ -4374,8 +4426,8 @@ pub unsafe extern "C" fn dc_backup_provider_get_qr_svg( eprintln!("ignoring careless call to dc_backup_provider_qr_svg()"); return "".strdup(); } - let ffi_provider = &*provider; - let ctx = &*ffi_provider.context; + let ffi_provider = unsafe { &*provider }; + let ctx = unsafe { &*ffi_provider.context }; let provider = &ffi_provider.provider; block_on(generate_backup_qr(ctx, &provider.qr())) .context("BackupProvider get_qr_svg failed") @@ -4385,14 +4437,14 @@ pub unsafe extern "C" fn dc_backup_provider_get_qr_svg( .strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_backup_provider_wait(provider: *mut dc_backup_provider_t) { if provider.is_null() { eprintln!("ignoring careless call to dc_backup_provider_wait()"); return; } - let ffi_provider = &mut *provider; - let ctx = &*ffi_provider.context; + let ffi_provider = unsafe { &mut *provider }; + let ctx = unsafe { &*ffi_provider.context }; let provider = &mut ffi_provider.provider; block_on(provider) .context("Failed to await backup provider") @@ -4401,16 +4453,16 @@ pub unsafe extern "C" fn dc_backup_provider_wait(provider: *mut dc_backup_provid .ok(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_backup_provider_unref(provider: *mut dc_backup_provider_t) { if provider.is_null() { eprintln!("ignoring careless call to dc_backup_provider_unref()"); return; } - drop(Box::from_raw(provider)); + drop(unsafe { Box::from_raw(provider) }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_receive_backup( context: *mut dc_context_t, qr: *const libc::c_char, @@ -4419,7 +4471,7 @@ pub unsafe extern "C" fn dc_receive_backup( eprintln!("ignoring careless call to dc_receive_backup()"); return 0; } - let ctx = &*context; + let ctx = unsafe { &*context }; let qr_text = to_string_lossy(qr); receive_backup(ctx.clone(), qr_text) } @@ -4527,7 +4579,7 @@ fn convert_and_prune_message_ids(msg_ids: *const u32, msg_cnt: libc::c_int) -> V pub type dc_provider_t = provider::Provider; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_provider_new_from_email( context: *const dc_context_t, addr: *const libc::c_char, @@ -4538,7 +4590,7 @@ pub unsafe extern "C" fn dc_provider_new_from_email( } let addr = to_string_lossy(addr); - let ctx = &*context; + let ctx = unsafe { &*context }; match provider::get_provider_info_by_addr(addr.as_str()) .log_err(ctx) @@ -4549,7 +4601,7 @@ pub unsafe extern "C" fn dc_provider_new_from_email( } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_provider_new_from_email_with_dns( context: *const dc_context_t, addr: *const libc::c_char, @@ -4560,7 +4612,7 @@ pub unsafe extern "C" fn dc_provider_new_from_email_with_dns( } let addr = to_string_lossy(addr); - let ctx = &*context; + let ctx = unsafe { &*context }; match provider::get_provider_info_by_addr(addr.as_str()) .log_err(ctx) @@ -4571,7 +4623,7 @@ pub unsafe extern "C" fn dc_provider_new_from_email_with_dns( } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_provider_get_overview_page( provider: *const dc_provider_t, ) -> *mut libc::c_char { @@ -4579,11 +4631,11 @@ pub unsafe extern "C" fn dc_provider_get_overview_page( eprintln!("ignoring careless call to dc_provider_get_overview_page()"); return "".strdup(); } - let provider = &*provider; + let provider = unsafe { &*provider }; provider.overview_page.strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_provider_get_before_login_hint( provider: *const dc_provider_t, ) -> *mut libc::c_char { @@ -4591,21 +4643,21 @@ pub unsafe extern "C" fn dc_provider_get_before_login_hint( eprintln!("ignoring careless call to dc_provider_get_before_login_hint()"); return "".strdup(); } - let provider = &*provider; + let provider = unsafe { &*provider }; provider.before_login_hint.strdup() } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_provider_get_status(provider: *const dc_provider_t) -> libc::c_int { if provider.is_null() { eprintln!("ignoring careless call to dc_provider_get_status()"); return 0; } - let provider = &*provider; + let provider = unsafe { &*provider }; provider.status as libc::c_int } -#[no_mangle] +#[unsafe(no_mangle)] #[allow(clippy::needless_return)] pub unsafe extern "C" fn dc_provider_unref(provider: *mut dc_provider_t) { if provider.is_null() { @@ -4622,7 +4674,7 @@ pub unsafe extern "C" fn dc_provider_unref(provider: *mut dc_provider_t) { /// `dc_accounts_t` in multiple threads at once. pub type dc_accounts_t = RwLock; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_new( dir: *const libc::c_char, writable: libc::c_int, @@ -4648,7 +4700,7 @@ pub unsafe extern "C" fn dc_accounts_new( pub type dc_event_channel_t = Mutex>; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_channel_new() -> *mut dc_event_channel_t { Box::into_raw(Box::new(Mutex::new(Some(Events::new())))) } @@ -4659,16 +4711,16 @@ pub unsafe extern "C" fn dc_event_channel_new() -> *mut dc_event_channel_t { /// /// you can call it after calling dc_accounts_new_with_event_channel, /// which took the events channel out of it already, so this just frees the underlying option. -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_channel_unref(event_channel: *mut dc_event_channel_t) { if event_channel.is_null() { eprintln!("ignoring careless call to dc_event_channel_unref()"); return; } - drop(Box::from_raw(event_channel)) + drop(unsafe { Box::from_raw(event_channel) }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_event_channel_get_event_emitter( event_channel: *mut dc_event_channel_t, ) -> *mut dc_event_emitter_t { @@ -4677,63 +4729,67 @@ pub unsafe extern "C" fn dc_event_channel_get_event_emitter( return ptr::null_mut(); } - let Some(event_channel) = &*(*event_channel) - .lock() - .expect("call to dc_event_channel_get_event_emitter() failed: mutex is poisoned") - else { - eprintln!( + unsafe { + let Some(event_channel) = &*(*event_channel) + .lock() + .expect("call to dc_event_channel_get_event_emitter() failed: mutex is poisoned") + else { + eprintln!( "ignoring careless call to dc_event_channel_get_event_emitter() -> channel was already consumed, make sure you call this before dc_accounts_new_with_event_channel" ); - return ptr::null_mut(); - }; + return ptr::null_mut(); + }; - let emitter = event_channel.get_emitter(); + let emitter = event_channel.get_emitter(); - Box::into_raw(Box::new(emitter)) + Box::into_raw(Box::new(emitter)) + } } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_new_with_event_channel( dir: *const libc::c_char, writable: libc::c_int, event_channel: *mut dc_event_channel_t, ) -> *const dc_accounts_t { - setup_panic!(); + unsafe { + setup_panic!(); - if dir.is_null() || event_channel.is_null() { - eprintln!("ignoring careless call to dc_accounts_new_with_event_channel()"); - return ptr::null_mut(); - } + if dir.is_null() || event_channel.is_null() { + eprintln!("ignoring careless call to dc_accounts_new_with_event_channel()"); + return ptr::null_mut(); + } - // consuming channel enforce that you need to get the event emitter - // before initializing the account manager, - // so that you don't miss events/errors during initialisation. - // It also prevents you from using the same channel on multiple account managers. - let Some(event_channel) = (*event_channel) - .lock() - .expect("call to dc_event_channel_get_event_emitter() failed: mutex is poisoned") - .take() - else { - eprintln!( - "ignoring careless call to dc_accounts_new_with_event_channel() + // consuming channel enforce that you need to get the event emitter + // before initializing the account manager, + // so that you don't miss events/errors during initialisation. + // It also prevents you from using the same channel on multiple account managers. + let Some(event_channel) = (*event_channel) + .lock() + .expect("call to dc_event_channel_get_event_emitter() failed: mutex is poisoned") + .take() + else { + eprintln!( + "ignoring careless call to dc_accounts_new_with_event_channel() -> channel was already consumed" - ); - return ptr::null_mut(); - }; + ); + return ptr::null_mut(); + }; - let accs = block_on(Accounts::new_with_events( - as_path(dir).into(), - writable != 0, - event_channel, - )); + let accs = block_on(Accounts::new_with_events( + as_path(dir).into(), + writable != 0, + event_channel, + )); - match accs { - Ok(accs) => Arc::into_raw(Arc::new(RwLock::new(accs))), - Err(err) => { - // We are using Anyhow's .context() and to show the inner error, too, we need the {:#}: - eprintln!("failed to create accounts: {err:#}"); - ptr::null_mut() + match accs { + Ok(accs) => Arc::into_raw(Arc::new(RwLock::new(accs))), + Err(err) => { + // We are using Anyhow's .context() and to show the inner error, too, we need the {:#}: + eprintln!("failed to create accounts: {err:#}"); + ptr::null_mut() + } } } } @@ -4741,16 +4797,16 @@ pub unsafe extern "C" fn dc_accounts_new_with_event_channel( /// Release the accounts structure. /// /// This function releases the memory of the `dc_accounts_t` structure. -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_unref(accounts: *const dc_accounts_t) { if accounts.is_null() { eprintln!("ignoring careless call to dc_accounts_unref()"); return; } - drop(Arc::from_raw(accounts)); + drop(unsafe { Arc::from_raw(accounts) }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_get_account( accounts: *const dc_accounts_t, id: u32, @@ -4760,14 +4816,14 @@ pub unsafe extern "C" fn dc_accounts_get_account( return ptr::null_mut(); } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(accounts.read()) .get_account(id) .map(|ctx| Box::into_raw(Box::new(ctx))) .unwrap_or_else(std::ptr::null_mut) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_get_selected_account( accounts: *const dc_accounts_t, ) -> *mut dc_context_t { @@ -4776,14 +4832,14 @@ pub unsafe extern "C" fn dc_accounts_get_selected_account( return ptr::null_mut(); } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(accounts.read()) .get_selected_account() .map(|ctx| Box::into_raw(Box::new(ctx))) .unwrap_or_else(std::ptr::null_mut) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_select_account( accounts: *const dc_accounts_t, id: u32, @@ -4793,7 +4849,7 @@ pub unsafe extern "C" fn dc_accounts_select_account( return 0; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(async move { let mut accounts = accounts.write().await; match accounts.select_account(id).await { @@ -4808,14 +4864,14 @@ pub unsafe extern "C" fn dc_accounts_select_account( }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_add_account(accounts: *const dc_accounts_t) -> u32 { if accounts.is_null() { eprintln!("ignoring careless call to dc_accounts_add_account()"); return 0; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(async move { let mut accounts = accounts.write().await; @@ -4829,14 +4885,14 @@ pub unsafe extern "C" fn dc_accounts_add_account(accounts: *const dc_accounts_t) }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_add_closed_account(accounts: *const dc_accounts_t) -> u32 { if accounts.is_null() { eprintln!("ignoring careless call to dc_accounts_add_closed_account()"); return 0; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(async move { let mut accounts = accounts.write().await; @@ -4850,7 +4906,7 @@ pub unsafe extern "C" fn dc_accounts_add_closed_account(accounts: *const dc_acco }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_remove_account( accounts: *const dc_accounts_t, id: u32, @@ -4860,7 +4916,7 @@ pub unsafe extern "C" fn dc_accounts_remove_account( return 0; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(async move { let mut accounts = accounts.write().await; @@ -4876,7 +4932,7 @@ pub unsafe extern "C" fn dc_accounts_remove_account( }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_migrate_account( accounts: *const dc_accounts_t, dbfile: *const libc::c_char, @@ -4886,7 +4942,7 @@ pub unsafe extern "C" fn dc_accounts_migrate_account( return 0; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; let dbfile = to_string_lossy(dbfile); block_on(async move { @@ -4906,65 +4962,65 @@ pub unsafe extern "C" fn dc_accounts_migrate_account( }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_get_all(accounts: *const dc_accounts_t) -> *mut dc_array_t { if accounts.is_null() { eprintln!("ignoring careless call to dc_accounts_get_all()"); return ptr::null_mut(); } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; let list = block_on(accounts.read()).get_all(); let array: dc_array_t = list.into(); Box::into_raw(Box::new(array)) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_start_io(accounts: *const dc_accounts_t) { if accounts.is_null() { eprintln!("ignoring careless call to dc_accounts_start_io()"); return; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(async move { accounts.write().await.start_io().await }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_stop_io(accounts: *const dc_accounts_t) { if accounts.is_null() { eprintln!("ignoring careless call to dc_accounts_stop_io()"); return; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(async move { accounts.read().await.stop_io().await }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_maybe_network(accounts: *const dc_accounts_t) { if accounts.is_null() { eprintln!("ignoring careless call to dc_accounts_maybe_network()"); return; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(async move { accounts.read().await.maybe_network().await }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_maybe_network_lost(accounts: *const dc_accounts_t) { if accounts.is_null() { eprintln!("ignoring careless call to dc_accounts_maybe_network_lost()"); return; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(async move { accounts.read().await.maybe_network_lost().await }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_background_fetch( accounts: *const dc_accounts_t, timeout_in_seconds: u64, @@ -4974,7 +5030,7 @@ pub unsafe extern "C" fn dc_accounts_background_fetch( return 0; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; let background_fetch_future = { let lock = block_on(accounts.read()); lock.background_fetch(Duration::from_secs(timeout_in_seconds)) @@ -4984,18 +5040,18 @@ pub unsafe extern "C" fn dc_accounts_background_fetch( 1 } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_stop_background_fetch(accounts: *const dc_accounts_t) { if accounts.is_null() { eprintln!("ignoring careless call to dc_accounts_stop_background_fetch()"); return; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; block_on(accounts.read()).stop_background_fetch(); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_set_push_device_token( accounts: *const dc_accounts_t, token: *const libc::c_char, @@ -5005,7 +5061,7 @@ pub unsafe extern "C" fn dc_accounts_set_push_device_token( return; } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; let token = to_string_lossy(token); block_on(async move { @@ -5018,7 +5074,7 @@ pub unsafe extern "C" fn dc_accounts_set_push_device_token( }) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_accounts_get_event_emitter( accounts: *const dc_accounts_t, ) -> *mut dc_event_emitter_t { @@ -5027,7 +5083,7 @@ pub unsafe extern "C" fn dc_accounts_get_event_emitter( return ptr::null_mut(); } - let accounts = &*accounts; + let accounts = unsafe { &*accounts }; let emitter = block_on(accounts.read()).get_event_emitter(); Box::into_raw(Box::new(emitter)) @@ -5038,7 +5094,7 @@ pub struct dc_jsonrpc_instance_t { handle: RpcSession, } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_jsonrpc_init( account_manager: *const dc_accounts_t, ) -> *mut dc_jsonrpc_instance_t { @@ -5047,7 +5103,7 @@ pub unsafe extern "C" fn dc_jsonrpc_init( return ptr::null_mut(); } - let account_manager = ManuallyDrop::new(Arc::from_raw(account_manager)); + let account_manager = ManuallyDrop::new(unsafe { Arc::from_raw(account_manager) }); let cmd_api = block_on(deltachat_jsonrpc::api::CommandApi::from_arc(Arc::clone( &account_manager, ))); @@ -5060,13 +5116,13 @@ pub unsafe extern "C" fn dc_jsonrpc_init( Box::into_raw(Box::new(instance)) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_jsonrpc_unref(jsonrpc_instance: *mut dc_jsonrpc_instance_t) { if jsonrpc_instance.is_null() { eprintln!("ignoring careless call to dc_jsonrpc_unref()"); return; } - drop(Box::from_raw(jsonrpc_instance)); + drop(unsafe { Box::from_raw(jsonrpc_instance) }); } fn spawn_handle_jsonrpc_request(handle: RpcSession, request: String) { @@ -5075,7 +5131,7 @@ fn spawn_handle_jsonrpc_request(handle: RpcSession, request: String) }); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_jsonrpc_request( jsonrpc_instance: *mut dc_jsonrpc_instance_t, request: *const libc::c_char, @@ -5085,12 +5141,12 @@ pub unsafe extern "C" fn dc_jsonrpc_request( return; } - let handle = &(*jsonrpc_instance).handle; + let handle = unsafe { &(*jsonrpc_instance).handle }; let request = to_string_lossy(request); spawn_handle_jsonrpc_request(handle.clone(), request); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_jsonrpc_next_response( jsonrpc_instance: *mut dc_jsonrpc_instance_t, ) -> *mut libc::c_char { @@ -5098,13 +5154,13 @@ pub unsafe extern "C" fn dc_jsonrpc_next_response( eprintln!("ignoring careless call to dc_jsonrpc_next_response()"); return ptr::null_mut(); } - let api = &*jsonrpc_instance; + let api = unsafe { &*jsonrpc_instance }; block_on(api.receiver.recv()) .map(|result| serde_json::to_string(&result).unwrap_or_default().strdup()) .unwrap_or(ptr::null_mut()) } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn dc_jsonrpc_blocking_call( jsonrpc_instance: *mut dc_jsonrpc_instance_t, input: *const libc::c_char, @@ -5113,7 +5169,7 @@ pub unsafe extern "C" fn dc_jsonrpc_blocking_call( eprintln!("ignoring careless call to dc_jsonrpc_blocking_call()"); return ptr::null_mut(); } - let api = &*jsonrpc_instance; + let api = unsafe { &*jsonrpc_instance }; let input = to_string_lossy(input); let res = block_on(api.handle.process_incoming(&input)); match res { diff --git a/deltachat-ffi/src/string.rs b/deltachat-ffi/src/string.rs index 296be1421a..6041121eff 100644 --- a/deltachat-ffi/src/string.rs +++ b/deltachat-ffi/src/string.rs @@ -17,13 +17,15 @@ use std::ptr; /// } /// ``` unsafe fn dc_strdup(s: *const libc::c_char) -> *mut libc::c_char { - let ret: *mut libc::c_char = if !s.is_null() { - libc::strdup(s) - } else { - libc::calloc(1, 1) as *mut libc::c_char - }; - assert!(!ret.is_null()); - ret + unsafe { + let ret: *mut libc::c_char = if !s.is_null() { + libc::strdup(s) + } else { + libc::calloc(1, 1) as *mut libc::c_char + }; + assert!(!ret.is_null()); + ret + } } /// Error type for the [OsStrExt] trait @@ -164,34 +166,40 @@ pub(crate) trait Strdup { /// This function will panic when the original string contains an /// interior null byte as this can not be represented in raw C /// strings. - unsafe fn strdup(&self) -> *mut libc::c_char; + fn strdup(&self) -> *mut libc::c_char; } impl Strdup for str { - unsafe fn strdup(&self) -> *mut libc::c_char { - let tmp = CString::new_lossy(self); - dc_strdup(tmp.as_ptr()) + fn strdup(&self) -> *mut libc::c_char { + unsafe { + let tmp = CString::new_lossy(self); + dc_strdup(tmp.as_ptr()) + } } } impl Strdup for String { - unsafe fn strdup(&self) -> *mut libc::c_char { + fn strdup(&self) -> *mut libc::c_char { let s: &str = self; s.strdup() } } impl Strdup for std::path::Path { - unsafe fn strdup(&self) -> *mut libc::c_char { - let tmp = self.to_c_string().unwrap_or_else(|_| CString::default()); - dc_strdup(tmp.as_ptr()) + fn strdup(&self) -> *mut libc::c_char { + unsafe { + let tmp = self.to_c_string().unwrap_or_else(|_| CString::default()); + dc_strdup(tmp.as_ptr()) + } } } impl Strdup for [u8] { - unsafe fn strdup(&self) -> *mut libc::c_char { - let tmp = CString::new_lossy(self); - dc_strdup(tmp.as_ptr()) + fn strdup(&self) -> *mut libc::c_char { + unsafe { + let tmp = CString::new_lossy(self); + dc_strdup(tmp.as_ptr()) + } } } @@ -209,15 +217,15 @@ pub(crate) trait OptStrdup { /// Allocate a new raw C `*char` version of this string, or NULL. /// /// See [Strdup::strdup] for details. - unsafe fn strdup(&self) -> *mut libc::c_char; + fn strdup(&self) -> *mut libc::c_char; } impl> OptStrdup for Option { - unsafe fn strdup(&self) -> *mut libc::c_char { + fn strdup(&self) -> *mut libc::c_char { match self { Some(s) => { let tmp = CString::new_lossy(s.as_ref()); - dc_strdup(tmp.as_ptr()) + unsafe { dc_strdup(tmp.as_ptr()) } } None => ptr::null_mut(), } @@ -258,11 +266,9 @@ pub(crate) fn to_opt_string_lossy(s: *const libc::c_char) -> Option { pub(crate) fn as_path<'a>(s: *const libc::c_char) -> &'a std::path::Path { assert!(!s.is_null(), "cannot be used on null pointers"); use std::os::unix::ffi::OsStrExt; - unsafe { - let c_str = std::ffi::CStr::from_ptr(s).to_bytes(); - let os_str = std::ffi::OsStr::from_bytes(c_str); - std::path::Path::new(os_str) - } + let c_str = unsafe { std::ffi::CStr::from_ptr(s) }.to_bytes(); + let os_str = std::ffi::OsStr::from_bytes(c_str); + std::path::Path::new(os_str) } // as_path() implementation for windows, documented above. diff --git a/deltachat-jsonrpc/Cargo.toml b/deltachat-jsonrpc/Cargo.toml index 5e98c4f1be..9757cda0de 100644 --- a/deltachat-jsonrpc/Cargo.toml +++ b/deltachat-jsonrpc/Cargo.toml @@ -2,7 +2,7 @@ name = "deltachat-jsonrpc" version = "2.57.0-dev" description = "DeltaChat JSON-RPC API" -edition = "2021" +edition = "2024" license = "MPL-2.0" repository = "https://github.com/chatmail/core" diff --git a/deltachat-jsonrpc/src/api.rs b/deltachat-jsonrpc/src/api.rs index 788a220f70..d36d4efab2 100644 --- a/deltachat-jsonrpc/src/api.rs +++ b/deltachat-jsonrpc/src/api.rs @@ -5,26 +5,27 @@ use std::sync::Arc; use std::time::Duration; use std::{collections::HashMap, str::FromStr}; -use anyhow::{anyhow, bail, ensure, Context, Result}; +use anyhow::{Context, Result, anyhow, bail, ensure}; +use deltachat::EventEmitter; pub use deltachat::accounts::Accounts; use deltachat::blob::BlobObject; use deltachat::calls::ice_servers; use deltachat::chat::{ - self, add_contact_to_chat, forward_msgs, forward_msgs_2ctx, get_chat_media, get_chat_msgs, - get_chat_msgs_ex, markfresh_chat, marknoticed_all_chats, marknoticed_chat, - remove_contact_from_chat, Chat, ChatId, ChatItem, MessageListOptions, + self, Chat, ChatId, ChatItem, MessageListOptions, add_contact_to_chat, forward_msgs, + forward_msgs_2ctx, get_chat_media, get_chat_msgs, get_chat_msgs_ex, markfresh_chat, + marknoticed_all_chats, marknoticed_chat, remove_contact_from_chat, }; use deltachat::chatlist::Chatlist; -use deltachat::config::{get_all_ui_config_keys, Config}; +use deltachat::config::{Config, get_all_ui_config_keys}; use deltachat::constants::DC_MSG_ID_DAYMARKER; -use deltachat::contact::{may_be_valid_addr, Contact, ContactId, Origin}; +use deltachat::contact::{Contact, ContactId, Origin, may_be_valid_addr}; use deltachat::context::get_info; use deltachat::ephemeral::Timer; use deltachat::imex; use deltachat::location; use deltachat::message::{ - self, delete_msgs_ex, get_existing_msg_ids, get_msg_read_receipt_count, get_msg_read_receipts, - markseen_msgs, Message, MessageState, MsgId, Viewtype, + self, Message, MessageState, MsgId, Viewtype, delete_msgs_ex, get_existing_msg_ids, + get_msg_read_receipt_count, get_msg_read_receipts, markseen_msgs, }; use deltachat::peer_channels::{ leave_webxdc_realtime, send_webxdc_realtime_advertisement, send_webxdc_realtime_data, @@ -37,10 +38,9 @@ use deltachat::securejoin; use deltachat::stock_str::StockMessage; use deltachat::storage_usage::{get_blobdir_storage_usage, get_storage_usage}; use deltachat::webxdc::StatusUpdateSerial; -use deltachat::EventEmitter; use sanitize_filename::is_sanitized; use tokio::fs; -use tokio::sync::{watch, Mutex, RwLock}; +use tokio::sync::{Mutex, RwLock, watch}; use types::login_param::EnteredLoginParam; use yerpc::rpc; @@ -67,7 +67,7 @@ use self::types::{ JsonrpcMessageListItem, MessageNotificationInfo, MessageSearchResult, MessageViewtype, }, }; -use crate::api::types::chat_list::{get_chat_list_item_by_id, ChatListItemFetchResult}; +use crate::api::types::chat_list::{ChatListItemFetchResult, get_chat_list_item_by_id}; use crate::api::types::login_param::TransportListEntry; use crate::api::types::qr::{QrObject, SecurejoinSource, SecurejoinUiPath}; @@ -2295,7 +2295,7 @@ impl CommandApi { let message = Message::load_from_db(&ctx, MsgId::new(instance_msg_id)).await?; let blob = message.get_webxdc_blob(&ctx, &path).await?; - use base64::{engine::general_purpose, Engine as _}; + use base64::{Engine as _, engine::general_purpose}; Ok(general_purpose::STANDARD_NO_PAD.encode(blob)) } diff --git a/deltachat-jsonrpc/src/api/types/calls.rs b/deltachat-jsonrpc/src/api/types/calls.rs index f8e0b68652..220fae7efa 100644 --- a/deltachat-jsonrpc/src/api/types/calls.rs +++ b/deltachat-jsonrpc/src/api/types/calls.rs @@ -1,6 +1,6 @@ use anyhow::{Context as _, Result}; -use deltachat::calls::{call_state, CallState}; +use deltachat::calls::{CallState, call_state}; use deltachat::context::Context; use deltachat::message::MsgId; use serde::Serialize; diff --git a/deltachat-jsonrpc/src/api/types/chat.rs b/deltachat-jsonrpc/src/api/types/chat.rs index 42d69ffa60..9d1f0adbc6 100644 --- a/deltachat-jsonrpc/src/api/types/chat.rs +++ b/deltachat-jsonrpc/src/api/types/chat.rs @@ -1,7 +1,7 @@ use std::time::{Duration, SystemTime}; -use anyhow::{bail, Context as _, Result}; -use deltachat::chat::{self, get_chat_contacts, get_past_chat_contacts, ChatVisibility}; +use anyhow::{Context as _, Result, bail}; +use deltachat::chat::{self, ChatVisibility, get_chat_contacts, get_past_chat_contacts}; use deltachat::chat::{Chat, ChatId}; use deltachat::constants::Chattype; use deltachat::contact::{Contact, ContactId}; diff --git a/deltachat-jsonrpc/src/api/types/chat_list.rs b/deltachat-jsonrpc/src/api/types/chat_list.rs index a07641b2b2..f302c64f40 100644 --- a/deltachat-jsonrpc/src/api/types/chat_list.rs +++ b/deltachat-jsonrpc/src/api/types/chat_list.rs @@ -4,7 +4,7 @@ use deltachat::chatlist::get_last_message_for_chat; use deltachat::constants::*; use deltachat::contact::Contact; use deltachat::{ - chat::{get_chat_contacts, ChatVisibility}, + chat::{ChatVisibility, get_chat_contacts}, chatlist::Chatlist, }; use num_traits::cast::ToPrimitive; diff --git a/deltachat-jsonrpc/src/api/types/http.rs b/deltachat-jsonrpc/src/api/types/http.rs index 9121a677ec..d370ba8f7c 100644 --- a/deltachat-jsonrpc/src/api/types/http.rs +++ b/deltachat-jsonrpc/src/api/types/http.rs @@ -16,7 +16,7 @@ pub struct HttpResponse { impl From for HttpResponse { fn from(response: CoreHttpResponse) -> Self { - use base64::{engine::general_purpose, Engine as _}; + use base64::{Engine as _, engine::general_purpose}; let blob = general_purpose::STANDARD_NO_PAD.encode(response.blob); let mimetype = response.mimetype; let encoding = response.encoding; diff --git a/deltachat-ratelimit/Cargo.toml b/deltachat-ratelimit/Cargo.toml index 04f2a91077..9556177175 100644 --- a/deltachat-ratelimit/Cargo.toml +++ b/deltachat-ratelimit/Cargo.toml @@ -2,7 +2,7 @@ name = "ratelimit" version = "1.0.0" description = "Token bucket implementation" -edition = "2021" +edition = "2024" license = "MPL-2.0" [dependencies] diff --git a/deltachat-repl/Cargo.toml b/deltachat-repl/Cargo.toml index eda3ed19ca..fd4fec4390 100644 --- a/deltachat-repl/Cargo.toml +++ b/deltachat-repl/Cargo.toml @@ -2,7 +2,7 @@ name = "deltachat-repl" version = "2.57.0-dev" license = "MPL-2.0" -edition = "2021" +edition = "2024" repository = "https://github.com/chatmail/core" [dependencies] diff --git a/deltachat-repl/src/cmdline.rs b/deltachat-repl/src/cmdline.rs index a8e07e4f69..0eb07a672b 100644 --- a/deltachat-repl/src/cmdline.rs +++ b/deltachat-repl/src/cmdline.rs @@ -5,7 +5,7 @@ use std::path::Path; use std::str::FromStr; use std::time::Duration; -use anyhow::{bail, ensure, Result}; +use anyhow::{Result, bail, ensure}; use deltachat::chat::{self, Chat, ChatId, ChatItem, ChatVisibility, MuteDuration}; use deltachat::chatlist::*; use deltachat::constants::*; @@ -1225,12 +1225,11 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu "fileinfo" => { ensure!(!arg1.is_empty(), "Argument missing."); - if let Ok(buf) = read_file(&context, Path::new(arg1)).await { - let (width, height) = get_filemeta(&buf)?; - println!("width={width}, height={height}"); - } else { + let Ok(buf) = read_file(&context, Path::new(arg1)).await else { bail!("Command failed."); - } + }; + let (width, height) = get_filemeta(&buf)?; + println!("width={width}, height={height}"); } "estimatedeletion" => { ensure!(!arg1.is_empty(), "Argument missing"); diff --git a/deltachat-repl/src/main.rs b/deltachat-repl/src/main.rs index b082a79556..547d8b6ef1 100644 --- a/deltachat-repl/src/main.rs +++ b/deltachat-repl/src/main.rs @@ -9,12 +9,12 @@ extern crate deltachat; use std::borrow::Cow::{self, Borrowed, Owned}; -use anyhow::{bail, Error}; +use anyhow::{Error, bail}; +use deltachat::EventType; use deltachat::chat::ChatId; use deltachat::context::*; use deltachat::qr_code_generator::get_securejoin_qr_svg; use deltachat::securejoin::*; -use deltachat::EventType; use log::{error, info, warn}; use nu_ansi_term::Color; use rustyline::completion::{Completer, FilenameCompleter, Pair}; diff --git a/deltachat-rpc-server/Cargo.toml b/deltachat-rpc-server/Cargo.toml index bc291aa2ab..87b7bbb41a 100644 --- a/deltachat-rpc-server/Cargo.toml +++ b/deltachat-rpc-server/Cargo.toml @@ -2,7 +2,7 @@ name = "deltachat-rpc-server" version = "2.57.0-dev" description = "DeltaChat JSON-RPC server" -edition = "2021" +edition = "2024" readme = "README.md" license = "MPL-2.0" diff --git a/deltachat-rpc-server/src/main.rs b/deltachat-rpc-server/src/main.rs index 2db7f05354..87f2a47c8d 100644 --- a/deltachat-rpc-server/src/main.rs +++ b/deltachat-rpc-server/src/main.rs @@ -6,7 +6,7 @@ use std::env; use std::path::PathBuf; use std::sync::Arc; -use anyhow::{anyhow, Context as _, Result}; +use anyhow::{Context as _, Result, anyhow}; use deltachat::constants::DC_VERSION_STR; use deltachat_jsonrpc::api::{Accounts, CommandApi}; use futures_lite::stream::StreamExt; diff --git a/deltachat-time/Cargo.toml b/deltachat-time/Cargo.toml index a2931d7263..7a055f629b 100644 --- a/deltachat-time/Cargo.toml +++ b/deltachat-time/Cargo.toml @@ -2,7 +2,7 @@ name = "deltachat-time" version = "1.0.0" description = "Time-related tools" -edition = "2021" +edition = "2024" license = "MPL-2.0" [dependencies] diff --git a/deltachat_derive/Cargo.toml b/deltachat_derive/Cargo.toml index 2ea978c5d6..ef2b4015a0 100644 --- a/deltachat_derive/Cargo.toml +++ b/deltachat_derive/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "deltachat_derive" version = "2.0.0" -edition = "2018" +edition = "2024" license = "MPL-2.0" [lib] diff --git a/deltachat_derive/src/lib.rs b/deltachat_derive/src/lib.rs index 0ab30254d2..dea09f3896 100644 --- a/deltachat_derive/src/lib.rs +++ b/deltachat_derive/src/lib.rs @@ -14,7 +14,7 @@ pub fn to_sql_derive(input: TokenStream) -> TokenStream { let ast: syn::DeriveInput = syn::parse(input).unwrap(); let name = &ast.ident; - let gen = quote! { + let r#gen = quote! { impl rusqlite::types::ToSql for #name { fn to_sql(&self) -> rusqlite::Result { let num = *self as i64; @@ -24,7 +24,7 @@ pub fn to_sql_derive(input: TokenStream) -> TokenStream { } } }; - gen.into() + r#gen.into() } #[proc_macro_derive(FromSql)] @@ -32,7 +32,7 @@ pub fn from_sql_derive(input: TokenStream) -> TokenStream { let ast: syn::DeriveInput = syn::parse(input).unwrap(); let name = &ast.ident; - let gen = quote! { + let r#gen = quote! { impl rusqlite::types::FromSql for #name { fn column_result(col: rusqlite::types::ValueRef) -> rusqlite::types::FromSqlResult { let inner = rusqlite::types::FromSql::column_result(col)?; @@ -44,5 +44,5 @@ pub fn from_sql_derive(input: TokenStream) -> TokenStream { } } }; - gen.into() + r#gen.into() } diff --git a/format-flowed/Cargo.toml b/format-flowed/Cargo.toml index 4807042151..a7a67e40e7 100644 --- a/format-flowed/Cargo.toml +++ b/format-flowed/Cargo.toml @@ -2,7 +2,7 @@ name = "format-flowed" version = "1.0.0" description = "format=flowed support" -edition = "2021" +edition = "2024" license = "MPL-2.0" keywords = ["email"] diff --git a/format-flowed/src/lib.rs b/format-flowed/src/lib.rs index 4d246b9c07..d9f9512f00 100644 --- a/format-flowed/src/lib.rs +++ b/format-flowed/src/lib.rs @@ -224,8 +224,7 @@ mod tests { fn test_unformat_flowed() { let text = "this is a very long message that should be wrapped using format=flowed and \n\ unwrapped on the receiver"; - let expected = - "this is a very long message that should be wrapped using format=flowed and \ + let expected = "this is a very long message that should be wrapped using format=flowed and \ unwrapped on the receiver"; assert_eq!(unformat_flowed(text, false), expected); @@ -255,8 +254,7 @@ mod tests { assert_eq!(format_flowed_quote(quote), expected); let quote = "this is a very long quote that should be wrapped using format=flowed and unwrapped on the receiver"; - let expected = - "> this is a very long quote that should be wrapped using format=flowed and \r\n\ + let expected = "> this is a very long quote that should be wrapped using format=flowed and \r\n\ > unwrapped on the receiver"; assert_eq!(format_flowed_quote(quote), expected); } diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index eeb4ff82a1..236346d21d 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -2,7 +2,7 @@ name = "deltachat-fuzz" version = "0.0.0" publish = false -edition = "2021" +edition = "2024" license = "MPL-2.0" [dev-dependencies]