2021-09-04 03:36:35 +00:00
|
|
|
#![allow(non_upper_case_globals)]
|
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
#![allow(non_snake_case)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2021-09-16 01:57:36 +00:00
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::ffi::CString;
|
2021-09-04 03:36:35 +00:00
|
|
|
|
2021-09-16 01:57:36 +00:00
|
|
|
use super::CwtchLib;
|
2021-09-08 07:32:56 +00:00
|
|
|
use crate::cwtchlib_go::bindings;
|
2021-09-04 03:36:35 +00:00
|
|
|
|
|
|
|
struct c_str_wrap {
|
|
|
|
raw: *mut i8,
|
2021-09-16 01:57:36 +00:00
|
|
|
len: i32,
|
2021-09-04 03:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl c_str_wrap {
|
|
|
|
pub fn new(str: &str) -> c_str_wrap {
|
|
|
|
let cs = match CString::new(str) {
|
|
|
|
Ok(s) => s,
|
2021-09-16 01:57:36 +00:00
|
|
|
Err(_) => CString::new("").unwrap(),
|
2021-09-04 03:36:35 +00:00
|
|
|
};
|
2021-09-16 01:57:36 +00:00
|
|
|
c_str_wrap {
|
|
|
|
len: cs.as_bytes().len() as i32,
|
|
|
|
raw: cs.into_raw(),
|
|
|
|
}
|
2021-09-04 03:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for c_str_wrap {
|
|
|
|
fn drop(&mut self) {
|
2021-09-16 01:57:36 +00:00
|
|
|
unsafe {
|
|
|
|
CString::from_raw(self.raw);
|
|
|
|
}
|
2021-09-04 03:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// c_bind handles setting up c string arguments and freeing them
|
|
|
|
// c_bind!( $fn_name ( [ $string_args ]* ; [ $non_string_args : $type ]* ) $c_function -> $return_type? )
|
|
|
|
macro_rules! c_bind {
|
|
|
|
// macro for returnless fns
|
|
|
|
($func_name:ident ($($str:ident),* ; $($arg:ident: $t:ty),*) $bind_fn:ident) => {
|
|
|
|
fn $func_name(&self, $($str: &str, )* $($arg: $t, )*) {
|
|
|
|
$(let $str = c_str_wrap::new($str);)*
|
|
|
|
unsafe {
|
2021-09-08 07:32:56 +00:00
|
|
|
bindings::$bind_fn($( $str.raw, $str.len, )* $($arg,)* );
|
2021-09-04 03:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// macro for str returning fns
|
|
|
|
($func_name:ident ($($str:ident),* ; $($arg:ident: $t:ty),* ) $bind_fn:ident -> String) => {
|
|
|
|
fn $func_name(&self, $($str: &str, )* $($arg: $t, )*) -> String {
|
|
|
|
$(let $str = c_str_wrap::new($str);)*
|
|
|
|
unsafe {
|
2021-09-08 07:32:56 +00:00
|
|
|
let result_ptr = bindings::$bind_fn($( $str.raw, $str.len, )* $($arg,)* );
|
2021-09-04 03:36:35 +00:00
|
|
|
let result = match CStr::from_ptr(result_ptr).to_str() {
|
|
|
|
Ok(s) => s.to_owned(),
|
|
|
|
Err(_) => "".to_string()
|
|
|
|
};
|
|
|
|
// return ownership of string memory and call the library to free it
|
2021-09-08 07:32:56 +00:00
|
|
|
bindings::c_FreePointer(result_ptr);
|
2021-09-04 03:36:35 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// macro for value returning fns
|
|
|
|
($func_name:ident ($($str:ident),* ; $($arg:ident: $t:ty),* ) $bind_fn:ident -> $bind_fn_ty:ty) => {
|
|
|
|
fn $func_name(&self, $($str: &str, )* $($arg: $t, )*) -> $bind_fn_ty {
|
|
|
|
$(let $str = c_str_wrap::new($str);)*
|
|
|
|
unsafe {
|
2021-09-08 07:32:56 +00:00
|
|
|
let result = bindings::$bind_fn($( $str.raw, $str.len, )* $($arg,)* );
|
2021-09-04 03:36:35 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-08 07:32:56 +00:00
|
|
|
pub struct CwtchLibGo {}
|
2021-09-04 03:36:35 +00:00
|
|
|
|
2021-09-08 07:32:56 +00:00
|
|
|
impl CwtchLibGo {
|
2021-09-04 03:36:35 +00:00
|
|
|
c_bind!(send_profile_event(profile, event_json;) c_SendProfileEvent);
|
|
|
|
}
|
|
|
|
|
2021-09-08 07:32:56 +00:00
|
|
|
impl CwtchLib for CwtchLibGo {
|
2021-09-04 03:36:35 +00:00
|
|
|
c_bind!(start_cwtch(app_dir, tor_path;) c_StartCwtch -> i32);
|
|
|
|
c_bind!(send_app_event(event_json;) c_SendAppEvent);
|
|
|
|
c_bind!(get_appbus_event(;) c_GetAppBusEvent -> String);
|
|
|
|
c_bind!(create_profile(nick, pass;) c_CreateProfile);
|
|
|
|
c_bind!(load_profiles(pass;) c_LoadProfiles);
|
|
|
|
c_bind!(accept_contact(profile, contact;) c_AcceptContact);
|
|
|
|
c_bind!(reject_invite(profile, contact;) c_RejectInvite);
|
|
|
|
c_bind!(block_contact(profile, contact;) c_BlockContact);
|
|
|
|
c_bind!(update_message_flags(profile, contact; message_id: i32, message_flags: u64) c_UpdateMessageFlags);
|
|
|
|
c_bind!(get_message(profile, contact; message_index: i32) c_GetMessage -> String);
|
|
|
|
c_bind!(get_message_by_content_hash(profile, contact, hash;) c_GetMessagesByContentHash -> String);
|
|
|
|
c_bind!(send_message(profile, contact, msg;) c_SendMessage);
|
|
|
|
c_bind!(send_invitation(profile, contact, target;) c_SendInvitation);
|
|
|
|
fn reset_tor(&self) {
|
2021-09-16 01:57:36 +00:00
|
|
|
unsafe {
|
|
|
|
bindings::c_ResetTor();
|
|
|
|
}
|
2021-09-04 03:36:35 +00:00
|
|
|
}
|
|
|
|
c_bind!(create_group(profile, server, name;) c_CreateGroup);
|
|
|
|
c_bind!(delete_profile(profile, pass;) c_DeleteProfile);
|
|
|
|
c_bind!(archive_conversation(profile, contact;) c_ArchiveConversation);
|
|
|
|
c_bind!(delete_contact(profile, group;) c_DeleteContact);
|
|
|
|
c_bind!(import_bundle(profile, bundle;) c_ImportBundle);
|
|
|
|
c_bind!(set_profile_attribute(profile, key, val;) c_SetProfileAttribute);
|
|
|
|
c_bind!(set_contact_attribute(profile, contact, key, val;) c_SetContactAttribute);
|
|
|
|
c_bind!(set_group_attribute(profile, group, key, val;) c_SetGroupAttribute);
|
|
|
|
|
|
|
|
fn shutdown_cwtch(&self) {
|
2021-09-16 01:57:36 +00:00
|
|
|
unsafe {
|
|
|
|
bindings::c_ShutdownCwtch();
|
|
|
|
}
|
2021-09-04 03:36:35 +00:00
|
|
|
}
|
2021-09-16 01:57:36 +00:00
|
|
|
}
|