libcwtch-rs/src/bindings_go.rs

264 lines
14 KiB
Rust
Raw Normal View History

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
use std::ffi::CStr;
use std::ffi::CString;
use super::CwtchLib;
use crate::cwtchlib_go::bindings;
2022-07-15 19:43:27 +00:00
use crate::{ConversationID, CwtchError, FileKey, ProfileIdentity, ServerIdentity, structs::*};
2022-07-07 06:52:30 +00:00
use crate::event::Event;
struct c_str_wrap {
raw: *mut i8,
len: i32,
}
impl c_str_wrap {
pub fn new(str: &str) -> c_str_wrap {
let cs = match CString::new(str) {
Ok(s) => s,
Err(_) => CString::new("").unwrap(),
};
c_str_wrap {
len: cs.as_bytes().len() as i32,
raw: cs.into_raw(),
}
}
}
impl Drop for c_str_wrap {
fn drop(&mut self) {
unsafe {
2022-01-15 14:10:55 +00:00
drop(CString::from_raw(self.raw));
}
}
}
// c_bind handles setting up c string arguments and freeing them
2022-01-15 14:10:55 +00:00
// c_bind!( $fn_name ( [ $string_args: &str],* [ $non_string_args : $type ],* ) $c_function -> $return_type? )
macro_rules! c_bind {
2022-01-15 14:10:55 +00:00
// No return
($func_name:ident ($($str1:ident: &str),* ; $($args:ident: $t:ty),* ; $($str2:ident: &str),*) $bind_fn:ident) => {
fn $func_name(&self, $($str1: &str, )* $($args: $t, )* $($str2: &str, )*) {
$(let $str1 = c_str_wrap::new($str1);)*
$(let $str2 = c_str_wrap::new($str2);)*
unsafe {
2022-01-15 14:10:55 +00:00
bindings::$bind_fn($( $str1.raw, $str1.len, )* $($args,)* $( $str2.raw, $str2.len, )*);
}
}
};
2022-01-15 14:10:55 +00:00
// String return
($func_name:ident ($($str1:ident: &str),* ; $($args:ident: $t:ty),* ; $($str2:ident: &str),*) $bind_fn:ident -> String) => {
fn $func_name(&self, $($str1: &str, )* $($args: $t, )* $($str2: &str, )*) -> String {
$(let $str1 = c_str_wrap::new($str1);)*
$(let $str2 = c_str_wrap::new($str2);)*
unsafe {
2022-01-15 14:10:55 +00:00
let result_ptr = bindings::$bind_fn($( $str1.raw, $str1.len, )* $($args,)* $( $str2.raw, $str2.len, )*);
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
bindings::c_FreePointer(result_ptr);
result
}
}
};
2022-01-15 14:10:55 +00:00
// Non String return
($func_name:ident ($($str1:ident: &str),* ; $($args:ident: $t:ty),* ; $($str2:ident: &str),*) $bind_fn:ident -> $bind_fn_ty:ty) => {
fn $func_name(&self, $($str1: &str, )* $($args: t, )* $($str2: &str, )*) -> $bind_fn_ty {
$(let $str1 = c_str_wrap::new($str1);)*
$(let $str2 = c_str_wrap::new($str2);)*
unsafe {
2022-01-15 14:10:55 +00:00
let result = bindings::$bind_fn($( $str1.raw, $str1.len, )* $($args,)* $( $str2.raw, $str2.len, )*);
result
}
}
};
}
pub struct CwtchLibGo {}
// Some bindings are going to be wrapped so we can handle their returns and give most rust idiomatic returns (esp for json returning apis)
// so we pre define the real binding here as a _helper function and in the impl for CwtchLib define the wrapper
impl CwtchLibGo {
c_bind!(_get_profile_attribute(profile: &str, key: &str;;) c_GetProfileAttribute -> String);
c_bind!(_get_conversation_attribute(profile: &str; conversation_id: i32; key: &str) c_GetConversationAttribute -> String);
2022-07-07 06:52:30 +00:00
c_bind!(_get_appbus_event(;;) c_GetAppBusEvent -> String);
2022-07-15 19:23:13 +00:00
c_bind!(_send_profile_event(profile: &str, event_jason: &str;;) c_SendProfileEvent);
c_bind!(_accept_conversation(profile: &str ; conversation_id: i32 ;) c_AcceptConversation);
c_bind!(_block_contact(profile: &str ; conversation_id: i32; ) c_BlockContact);
c_bind!(_unblock_contact(profile: &str ; conversation_id: i32; ) c_UnblockContact);
c_bind!(_get_message(profile: &str; conversation_id: i32, message_index: i32 ;) c_GetMessage -> String);
c_bind!(_get_message_by_id(profile: &str ; conversation_id: i32, message_id: i32 ;) c_GetMessageByID -> String);
c_bind!(_get_message_by_content_hash(profile: &str ; conversation_id: i32 ; hash: &str) c_GetMessagesByContentHash -> String);
c_bind!(_get_messages(profile: &str; conversation_id: i32, message_index: i32, count: i32 ;) c_GetMessages -> String);
c_bind!(_send_message(profile: &str; conversation_id: i32; msg: &str) c_SendMessage -> String);
c_bind!(_send_invitation(profile: &str; conversation_id: i32, target_id: i32;) c_SendInvitation -> String);
c_bind!(_share_file(profile: &str; conversation_id: i32; file_path: &str) c_ShareFile -> String);
c_bind!(_download_file(profile: &str; conversation_id: i32; file_path: &str, manifest_path: &str, file_key: &str) c_DownloadFile);
c_bind!(_check_download_status(profile: &str, file_key: &str;;) c_CheckDownloadStatus);
c_bind!(_verify_or_resume_download(profile: &str; conversation_id: i32; file_key: &str) c_VerifyOrResumeDownload);
c_bind!(_create_group(profile: &str, server: &str, name: &str;;) c_CreateGroup);
c_bind!(_delete_profile(profile: &str, pass: &str;;) c_DeleteProfile);
c_bind!(_archive_conversation(profile: &str; conversation_id: i32;) c_ArchiveConversation);
c_bind!(_delete_contact(profile: &str; conversation_id: i32;) c_DeleteContact);
c_bind!(_import_bundle(profile: &str, bundle: &str;;) c_ImportBundle);
c_bind!(_set_profile_attribute(profile: &str, key: &str, val: &str;;) c_SetProfileAttribute);
c_bind!(_set_conversation_attribute(profile: &str; conversation_id: i32; key: &str, val: &str) c_SetConversationAttribute);
c_bind!(_set_message_attribute(profile: &str; conversation_id: i32, channel_id: i32, message_id: i32; key: &str, val: &str) c_SetMessageAttribute);
c_bind!(_change_password(profile: &str, old_pass: &str, new_pass: &str, new_pass_again: &str;;) c_ChangePassword);
c_bind!(_export_profile(profile: &str, filename: &str;;) c_ExportProfile);
c_bind!(_delete_server(server: &str, current_password: &str;;) c_DeleteServer);
c_bind!(_launch_server(server: &str;;) c_LaunchServer);
c_bind!(_stop_server(server: &str;;) c_StopServer);
c_bind!(_set_server_attribute(server: &str, key: &str, val: &str;;) c_SetServerAttribute);
}
2022-01-16 19:06:30 +00:00
impl CwtchLib for CwtchLibGo {
2022-01-15 14:10:55 +00:00
c_bind!(start_cwtch(app_dir: &str, tor_path: &str;;) c_StartCwtch -> i32);
c_bind!(started(;;) c_Started -> i32);
2022-01-15 14:10:55 +00:00
c_bind!(send_app_event(event_json: &str;;) c_SendAppEvent);
fn send_profile_event(&self, profile: &ProfileIdentity, event_jason: &str) {
self._send_profile_event(profile.as_str(), event_jason)
2022-07-15 19:23:13 +00:00
}
2022-01-15 14:10:55 +00:00
c_bind!(create_profile(nick: &str, pass: &str;;) c_CreateProfile);
c_bind!(load_profiles(pass: &str;;) c_LoadProfiles);
fn accept_conversation(&self, profile: &ProfileIdentity, conversation_id: ConversationID) {
self._accept_conversation(profile.as_str(), conversation_id.into())
2022-07-15 19:23:13 +00:00
}
fn block_contact(&self, profile: &ProfileIdentity, conversation_id: ConversationID) {
2022-07-15 19:43:27 +00:00
self._block_contact(String::from(profile).as_str(), conversation_id.into())
2022-07-15 19:23:13 +00:00
}
fn unblock_contact(&self, profile: &ProfileIdentity, conversation_id: ConversationID) {
2022-07-15 19:43:27 +00:00
self._unblock_contact(String::from(profile).as_str(), conversation_id.into())
2022-07-15 19:23:13 +00:00
}
fn get_message(&self, profile: &ProfileIdentity, conversation_id: ConversationID, message_index: i32) -> String {
2022-07-15 19:43:27 +00:00
self._get_message(String::from(profile).as_str(), conversation_id.into(), message_index)
2022-07-15 19:23:13 +00:00
}
fn get_message_by_id(&self, profile: &ProfileIdentity, conversation_id: ConversationID, message_id: i32) -> String {
2022-07-15 19:43:27 +00:00
self._get_message_by_id(String::from(profile).as_str(), conversation_id.into(), message_id)
2022-07-15 19:23:13 +00:00
}
fn get_message_by_content_hash(&self, profile: &ProfileIdentity, conversation_id: ConversationID, hash: &str) -> String {
2022-07-15 19:43:27 +00:00
self._get_message_by_content_hash(String::from(profile).as_str(), conversation_id.into(), hash)
2022-07-15 19:23:13 +00:00
}
fn get_messages(&self, profile: &ProfileIdentity, conversation_id: ConversationID, message_index: i32, count: i32) -> String {
2022-07-15 19:43:27 +00:00
self._get_messages(String::from(profile).as_str(), conversation_id.into(), message_index, count)
2022-07-15 19:23:13 +00:00
}
fn send_message(&self, profile: &ProfileIdentity, conversation_id: ConversationID, msg: &str) -> String {
2022-07-15 19:43:27 +00:00
self._send_message(String::from(profile).as_str(), conversation_id.into(), msg)
2022-07-15 19:23:13 +00:00
}
fn send_invitation(&self, profile: &ProfileIdentity, conversation_id: ConversationID, target_id: i32) -> String {
2022-07-15 19:43:27 +00:00
self._send_invitation(String::from(profile).as_str(), conversation_id.into(), target_id)
2022-07-15 19:23:13 +00:00
}
fn share_file(&self, profile: &ProfileIdentity, conversation_id: ConversationID, file_path: &str) -> String {
2022-07-15 19:43:27 +00:00
self._share_file(String::from(profile).as_str(), conversation_id.into(), file_path)
2022-07-15 19:23:13 +00:00
}
fn download_file(&self, profile: &ProfileIdentity, conversation_id: ConversationID, file_path: &str, manifest_path: &str, file_key: FileKey) {
2022-07-15 19:43:27 +00:00
self._download_file(String::from(profile).as_str(), conversation_id.into(), file_path, manifest_path, String::from(file_key).as_str())
2022-07-15 19:23:13 +00:00
}
fn check_download_status(&self, profile: &ProfileIdentity, file_key: FileKey) {
2022-07-15 19:43:27 +00:00
self._check_download_status(String::from(profile).as_str(), String::from(file_key).as_str())
2022-07-15 19:23:13 +00:00
}
fn verify_or_resume_download(&self, profile: &ProfileIdentity, conversation_id: ConversationID, file_key: FileKey) {
2022-07-15 19:43:27 +00:00
self._verify_or_resume_download(String::from(profile).as_str(), conversation_id.into(), String::from(file_key).as_str())
2022-07-15 19:23:13 +00:00
}
fn reset_tor(&self) {
unsafe {
bindings::c_ResetTor();
}
}
fn create_group(&self, profile: &ProfileIdentity, server: &str, name: &str) {
2022-07-15 19:43:27 +00:00
self._create_group(String::from(profile).as_str(), server, name)
2022-07-15 19:23:13 +00:00
}
fn delete_profile(&self, profile: &ProfileIdentity, pass: &str) {
2022-07-15 19:43:27 +00:00
self._delete_profile(String::from(profile).as_str(), pass)
2022-07-15 19:23:13 +00:00
}
fn archive_conversation(&self, profile: &ProfileIdentity, conversation_id: ConversationID) {
2022-07-15 19:43:27 +00:00
self._archive_conversation(String::from(profile).as_str(), conversation_id.into())
2022-07-15 19:23:13 +00:00
}
fn delete_contact(&self, profile: &ProfileIdentity, conversation_id: ConversationID) {
self._delete_contact(profile.as_str(), conversation_id.into())
2022-07-15 19:23:13 +00:00
}
fn import_bundle(&self, profile: &ProfileIdentity, bundle: &str) {
2022-07-15 19:43:27 +00:00
self._import_bundle(String::from(profile).as_str(), bundle)
2022-07-15 19:23:13 +00:00
}
fn set_profile_attribute(&self, profile: &ProfileIdentity, key: &str, val: &str) {
2022-07-15 19:43:27 +00:00
self._set_profile_attribute(String::from(profile).as_str(), key, val)
2022-07-15 19:23:13 +00:00
}
fn get_profile_attribute(&self, profile: &ProfileIdentity, key: &str) -> Result<Option<String>, CwtchError> {
2022-07-15 19:43:27 +00:00
let resp = self._get_profile_attribute(String::from(profile).as_str(), key);
let attr: Attribute = match serde_json::from_str(&resp) {
Ok(attr) => attr,
Err(e) => return Err(e.to_string()),
};
match attr.exists {
true => Ok(Some(attr.value)),
false => Ok(None),
}
}
fn set_conversation_attribute(&self, profile: &ProfileIdentity, conversation_id: ConversationID, key: &str, val: &str) {
2022-07-15 19:43:27 +00:00
self._set_conversation_attribute(String::from(profile).as_str(), conversation_id.into(), key, val)
2022-07-15 19:23:13 +00:00
}
fn get_conversation_attribute(&self, profile: &ProfileIdentity, conversation_id: ConversationID, key: &str) -> Result<Option<String>, CwtchError> {
2022-07-15 19:43:27 +00:00
let resp = self._get_conversation_attribute(String::from(profile).as_str(), conversation_id.into(), key);
let attr: Attribute = match serde_json::from_str(&resp) {
Ok(attr) => attr,
Err(e) => return Err(e.to_string()),
};
match attr.exists {
true => Ok(Some(attr.value)),
false => Ok(None),
}
}
fn set_message_attribute(&self, profile: &ProfileIdentity, conversation_id: ConversationID, channel_id: i32, message_id: i32, key: &str, val: &str) {
2022-07-15 19:43:27 +00:00
self._set_message_attribute(String::from(profile).as_str(), conversation_id.into(), channel_id, message_id, key, val)
2022-07-15 19:23:13 +00:00
}
fn change_password(&self, profile: &ProfileIdentity, old_pass: &str, new_pass: &str, new_pass_again: &str) {
2022-07-15 19:43:27 +00:00
self._change_password(String::from(profile).as_str(), old_pass, new_pass, new_pass_again)
2022-07-15 19:23:13 +00:00
}
fn export_profile(&self, profile: &ProfileIdentity, filename: &str) {
2022-07-15 19:43:27 +00:00
self._export_profile(String::from(profile).as_str(), filename)
2022-07-15 19:23:13 +00:00
}
c_bind!(import_profile(filename: &str, password: &str;;) c_ImportProfile -> String);
2022-07-15 19:23:13 +00:00
fn shutdown_cwtch(&self) {
unsafe {
bindings::c_ShutdownCwtch();
}
}
2022-01-15 14:10:55 +00:00
c_bind!(load_servers(password: &str;;) c_LoadServers);
c_bind!(create_server(password: &str, description: &str; autostart: i8;) c_CreateServer);
2022-07-15 19:23:13 +00:00
fn delete_server(&self, server: ServerIdentity, current_password: &str) {
2022-07-15 19:43:27 +00:00
self._delete_server(String::from(server).as_str(), current_password)
2022-07-15 19:23:13 +00:00
}
2022-01-15 14:10:55 +00:00
c_bind!(launch_servers(;;) c_LaunchServers);
2022-07-15 19:23:13 +00:00
fn launch_server(&self, server: ServerIdentity) {
2022-07-15 19:43:27 +00:00
self._launch_server(String::from(server).as_str())
2022-07-15 19:23:13 +00:00
}
fn stop_server(&self, server: ServerIdentity) {
2022-07-15 19:43:27 +00:00
self._stop_server(String::from(server).as_str())
2022-07-15 19:23:13 +00:00
}
2022-01-15 14:10:55 +00:00
c_bind!(stop_servers(;;) c_StopServers);
c_bind!(destroy_servers(;;) c_DestroyServers);
2022-07-15 19:23:13 +00:00
fn set_server_attribute(&self, server: ServerIdentity, key: &str, val: &str) {
2022-07-15 19:43:27 +00:00
self._set_server_attribute(String::from(server).as_str(), key, val)
2022-07-15 19:23:13 +00:00
}
2022-04-22 00:07:51 +00:00
c_bind!(get_debug_info(;;) c_GetDebugInfo -> String);
2022-07-07 06:52:30 +00:00
fn get_appbus_event(&self) -> Event {
let event_json = self._get_appbus_event();
let cwtch_event: CwtchEvent = serde_json::from_str(&event_json).expect("Error parsing Cwtch event");
Event::from(&cwtch_event)
}
}