@@ -35,6 +35,8 @@ use deltachat::stock_str::StockMessage;
35
35
use deltachat:: webxdc:: StatusUpdateSerial ;
36
36
use deltachat:: * ;
37
37
use deltachat:: { accounts:: Accounts , log:: LogExt } ;
38
+ use deltachat_jsonrpc:: api:: CommandApi ;
39
+ use deltachat_jsonrpc:: yerpc:: { OutReceiver , RpcClient , RpcSession } ;
38
40
use num_traits:: { FromPrimitive , ToPrimitive } ;
39
41
use once_cell:: sync:: Lazy ;
40
42
use rand:: Rng ;
@@ -4930,105 +4932,97 @@ pub unsafe extern "C" fn dc_accounts_get_event_emitter(
4930
4932
Box :: into_raw ( Box :: new ( emitter) )
4931
4933
}
4932
4934
4933
- #[ cfg( feature = "jsonrpc" ) ]
4934
- mod jsonrpc {
4935
- use deltachat_jsonrpc:: api:: CommandApi ;
4936
- use deltachat_jsonrpc:: yerpc:: { OutReceiver , RpcClient , RpcSession } ;
4937
-
4938
- use super :: * ;
4935
+ pub struct dc_jsonrpc_instance_t {
4936
+ receiver : OutReceiver ,
4937
+ handle : RpcSession < CommandApi > ,
4938
+ }
4939
4939
4940
- pub struct dc_jsonrpc_instance_t {
4941
- receiver : OutReceiver ,
4942
- handle : RpcSession < CommandApi > ,
4940
+ #[ no_mangle]
4941
+ pub unsafe extern "C" fn dc_jsonrpc_init (
4942
+ account_manager : * mut dc_accounts_t ,
4943
+ ) -> * mut dc_jsonrpc_instance_t {
4944
+ if account_manager. is_null ( ) {
4945
+ eprintln ! ( "ignoring careless call to dc_jsonrpc_init()" ) ;
4946
+ return ptr:: null_mut ( ) ;
4943
4947
}
4944
4948
4945
- #[ no_mangle]
4946
- pub unsafe extern "C" fn dc_jsonrpc_init (
4947
- account_manager : * mut dc_accounts_t ,
4948
- ) -> * mut dc_jsonrpc_instance_t {
4949
- if account_manager. is_null ( ) {
4950
- eprintln ! ( "ignoring careless call to dc_jsonrpc_init()" ) ;
4951
- return ptr:: null_mut ( ) ;
4952
- }
4949
+ let account_manager = & * account_manager;
4950
+ let cmd_api = block_on ( deltachat_jsonrpc:: api:: CommandApi :: from_arc (
4951
+ account_manager. inner . clone ( ) ,
4952
+ ) ) ;
4953
4953
4954
- let account_manager = & * account_manager;
4955
- let cmd_api = block_on ( deltachat_jsonrpc:: api:: CommandApi :: from_arc (
4956
- account_manager. inner . clone ( ) ,
4957
- ) ) ;
4954
+ let ( request_handle, receiver) = RpcClient :: new ( ) ;
4955
+ let handle = RpcSession :: new ( request_handle, cmd_api) ;
4958
4956
4959
- let ( request_handle, receiver) = RpcClient :: new ( ) ;
4960
- let handle = RpcSession :: new ( request_handle, cmd_api) ;
4957
+ let instance = dc_jsonrpc_instance_t { receiver, handle } ;
4961
4958
4962
- let instance = dc_jsonrpc_instance_t { receiver, handle } ;
4959
+ Box :: into_raw ( Box :: new ( instance) )
4960
+ }
4963
4961
4964
- Box :: into_raw ( Box :: new ( instance) )
4962
+ #[ no_mangle]
4963
+ pub unsafe extern "C" fn dc_jsonrpc_unref ( jsonrpc_instance : * mut dc_jsonrpc_instance_t ) {
4964
+ if jsonrpc_instance. is_null ( ) {
4965
+ eprintln ! ( "ignoring careless call to dc_jsonrpc_unref()" ) ;
4966
+ return ;
4965
4967
}
4968
+ drop ( Box :: from_raw ( jsonrpc_instance) ) ;
4969
+ }
4966
4970
4967
- #[ no_mangle]
4968
- pub unsafe extern "C" fn dc_jsonrpc_unref ( jsonrpc_instance : * mut dc_jsonrpc_instance_t ) {
4969
- if jsonrpc_instance. is_null ( ) {
4970
- eprintln ! ( "ignoring careless call to dc_jsonrpc_unref()" ) ;
4971
- return ;
4972
- }
4973
- drop ( Box :: from_raw ( jsonrpc_instance) ) ;
4974
- }
4971
+ fn spawn_handle_jsonrpc_request ( handle : RpcSession < CommandApi > , request : String ) {
4972
+ spawn ( async move {
4973
+ handle. handle_incoming ( & request) . await ;
4974
+ } ) ;
4975
+ }
4975
4976
4976
- fn spawn_handle_jsonrpc_request ( handle : RpcSession < CommandApi > , request : String ) {
4977
- spawn ( async move {
4978
- handle. handle_incoming ( & request) . await ;
4979
- } ) ;
4977
+ #[ no_mangle]
4978
+ pub unsafe extern "C" fn dc_jsonrpc_request (
4979
+ jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
4980
+ request : * const libc:: c_char ,
4981
+ ) {
4982
+ if jsonrpc_instance. is_null ( ) || request. is_null ( ) {
4983
+ eprintln ! ( "ignoring careless call to dc_jsonrpc_request()" ) ;
4984
+ return ;
4980
4985
}
4981
4986
4982
- #[ no_mangle]
4983
- pub unsafe extern "C" fn dc_jsonrpc_request (
4984
- jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
4985
- request : * const libc:: c_char ,
4986
- ) {
4987
- if jsonrpc_instance. is_null ( ) || request. is_null ( ) {
4988
- eprintln ! ( "ignoring careless call to dc_jsonrpc_request()" ) ;
4989
- return ;
4990
- }
4987
+ let handle = & ( * jsonrpc_instance) . handle ;
4988
+ let request = to_string_lossy ( request) ;
4989
+ spawn_handle_jsonrpc_request ( handle. clone ( ) , request) ;
4990
+ }
4991
4991
4992
- let handle = & ( * jsonrpc_instance) . handle ;
4993
- let request = to_string_lossy ( request) ;
4994
- spawn_handle_jsonrpc_request ( handle. clone ( ) , request) ;
4992
+ #[ no_mangle]
4993
+ pub unsafe extern "C" fn dc_jsonrpc_next_response (
4994
+ jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
4995
+ ) -> * mut libc:: c_char {
4996
+ if jsonrpc_instance. is_null ( ) {
4997
+ eprintln ! ( "ignoring careless call to dc_jsonrpc_next_response()" ) ;
4998
+ return ptr:: null_mut ( ) ;
4995
4999
}
5000
+ let api = & * jsonrpc_instance;
5001
+ block_on ( api. receiver . recv ( ) )
5002
+ . map ( |result| serde_json:: to_string ( & result) . unwrap_or_default ( ) . strdup ( ) )
5003
+ . unwrap_or ( ptr:: null_mut ( ) )
5004
+ }
4996
5005
4997
- #[ no_mangle]
4998
- pub unsafe extern "C" fn dc_jsonrpc_next_response (
4999
- jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
5000
- ) -> * mut libc:: c_char {
5001
- if jsonrpc_instance. is_null ( ) {
5002
- eprintln ! ( "ignoring careless call to dc_jsonrpc_next_response()" ) ;
5003
- return ptr:: null_mut ( ) ;
5004
- }
5005
- let api = & * jsonrpc_instance;
5006
- block_on ( api. receiver . recv ( ) )
5007
- . map ( |result| serde_json:: to_string ( & result) . unwrap_or_default ( ) . strdup ( ) )
5008
- . unwrap_or ( ptr:: null_mut ( ) )
5009
- }
5010
-
5011
- #[ no_mangle]
5012
- pub unsafe extern "C" fn dc_jsonrpc_blocking_call (
5013
- jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
5014
- input : * const libc:: c_char ,
5015
- ) -> * mut libc:: c_char {
5016
- if jsonrpc_instance. is_null ( ) {
5017
- eprintln ! ( "ignoring careless call to dc_jsonrpc_blocking_call()" ) ;
5018
- return ptr:: null_mut ( ) ;
5019
- }
5020
- let api = & * jsonrpc_instance;
5021
- let input = to_string_lossy ( input) ;
5022
- let res = block_on ( api. handle . process_incoming ( & input) ) ;
5023
- match res {
5024
- Some ( message) => {
5025
- if let Ok ( message) = serde_json:: to_string ( & message) {
5026
- message. strdup ( )
5027
- } else {
5028
- ptr:: null_mut ( )
5029
- }
5006
+ #[ no_mangle]
5007
+ pub unsafe extern "C" fn dc_jsonrpc_blocking_call (
5008
+ jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
5009
+ input : * const libc:: c_char ,
5010
+ ) -> * mut libc:: c_char {
5011
+ if jsonrpc_instance. is_null ( ) {
5012
+ eprintln ! ( "ignoring careless call to dc_jsonrpc_blocking_call()" ) ;
5013
+ return ptr:: null_mut ( ) ;
5014
+ }
5015
+ let api = & * jsonrpc_instance;
5016
+ let input = to_string_lossy ( input) ;
5017
+ let res = block_on ( api. handle . process_incoming ( & input) ) ;
5018
+ match res {
5019
+ Some ( message) => {
5020
+ if let Ok ( message) = serde_json:: to_string ( & message) {
5021
+ message. strdup ( )
5022
+ } else {
5023
+ ptr:: null_mut ( )
5030
5024
}
5031
- None => ptr:: null_mut ( ) ,
5032
5025
}
5026
+ None => ptr:: null_mut ( ) ,
5033
5027
}
5034
5028
}
0 commit comments