@@ -10,8 +10,9 @@ use std::time::Duration;
1010use anyhow:: Result ;
1111use ma_core:: config:: Config ;
1212use ma_core:: {
13- Did , Inbox , IpfsGatewayResolver , MaEndpoint , Message , SigningKey , IPFS_PROTOCOL_ID ,
14- MESSAGE_TYPE_RPC , MESSAGE_TYPE_RPC_REPLY ,
13+ Did , Inbox , IpfsGatewayResolver , MaEndpoint , Message , SigningKey , INBOX_PROTOCOL_ID ,
14+ IPFS_PROTOCOL_ID , MESSAGE_TYPE_CRUD , MESSAGE_TYPE_CRUD_REPLY , MESSAGE_TYPE_IPFS_REQUEST ,
15+ MESSAGE_TYPE_IPFS_STORE , MESSAGE_TYPE_RPC , MESSAGE_TYPE_RPC_REPLY ,
1516} ;
1617use tokio:: sync:: mpsc:: { UnboundedReceiver , UnboundedSender } ;
1718use tokio:: sync:: RwLock ;
@@ -24,12 +25,27 @@ use crate::ipfs::IpfsServiceState;
2425use crate :: manifest:: ManifestWriter ;
2526use crate :: plugin:: EntityRegistry ;
2627use crate :: status:: SharedStats ;
27- use crate :: { bootstrap, crud, i18n, ipfs, rpc, status} ;
28+ use crate :: { bootstrap, crud, i18n, inbox, ipfs, rpc, status} ;
29+
30+ /// Map a `message_type` string to the iroh delivery protocol.
31+ ///
32+ /// Only RPC and its reply go to `/ma/rpc/0.0.1`; IPFS requests go to
33+ /// `/ma/ipfs/0.0.1`; CRUD goes to `/ma/crud/0.0.1`. Everything else
34+ /// (message, broadcast, chat, emote, unknown) falls back to `/ma/inbox/0.0.1`.
35+ fn protocol_for ( msg_type : & str ) -> & ' static str {
36+ match msg_type {
37+ MESSAGE_TYPE_RPC | MESSAGE_TYPE_RPC_REPLY => rpc:: RPC_PROTOCOL_ID ,
38+ MESSAGE_TYPE_IPFS_REQUEST | MESSAGE_TYPE_IPFS_STORE => IPFS_PROTOCOL_ID ,
39+ MESSAGE_TYPE_CRUD | MESSAGE_TYPE_CRUD_REPLY => crud:: CRUD_PROTOCOL_ID ,
40+ _ => INBOX_PROTOCOL_ID ,
41+ }
42+ }
2843
2944#[ allow( clippy:: too_many_arguments, clippy:: too_many_lines) ]
3045pub async fn run (
3146 mut endpoint : Arc < dyn MaEndpoint > ,
3247 rpc_messages : Inbox < Message > ,
48+ inbox_messages : Inbox < Message > ,
3349 mut crud_messages : Option < Inbox < Message > > ,
3450 mut ipfs_state : Option < IpfsServiceState > ,
3551 envelope_tx : UnboundedSender < ( String , SendEnvelope ) > ,
@@ -202,12 +218,40 @@ pub async fn run(
202218 }
203219 }
204220
221+ // Drain /ma/inbox/0.0.1
222+ while let Some ( mut message) = inbox_messages. pop( now) {
223+ debug!(
224+ from = %message. from,
225+ to = %message. to,
226+ message_type = %message. message_type,
227+ "{}" , i18n:: t( "inbox-message-received" )
228+ ) ;
229+ let ctx = inbox:: InboxHandlerCtx {
230+ our_did: Arc :: from( our_did. as_str( ) ) ,
231+ entity_registry: entity_registry. clone( ) ,
232+ kubo_rpc_url: Arc :: from( kubo_url. as_str( ) ) ,
233+ } ;
234+ tokio:: spawn( async move {
235+ if let Err ( err) = tokio:: time:: timeout(
236+ Duration :: from_secs( 30 ) ,
237+ inbox:: handle_inbox_message( & message, & ctx) ,
238+ )
239+ . await
240+ . unwrap_or_else( |_| Err ( anyhow:: anyhow!( "inbox handler timed out" ) ) )
241+ {
242+ warn!( error = %err, from = %message. from, "inbox message rejected" ) ;
243+ }
244+ message. content. zeroize( ) ;
245+ message. signature. zeroize( ) ;
246+ } ) ;
247+ }
248+
205249 // Drain plugin outbox — envelopes sent fire-and-forget by ma_send/ma_reply.
206250 while let Ok ( ( fragment, env) ) = envelope_rx. try_recv( ) {
207251 let msg_type = if env. reply_to. is_some( ) {
208252 MESSAGE_TYPE_RPC_REPLY
209253 } else {
210- MESSAGE_TYPE_RPC
254+ env . message_type . as_deref ( ) . unwrap_or ( MESSAGE_TYPE_RPC )
211255 } ;
212256 let sender_did_url = format!( "{our_did}#{fragment}" ) ;
213257 let recipient = match Did :: try_from( env. to. as_str( ) ) {
@@ -232,6 +276,7 @@ pub async fn run(
232276 }
233277 } ;
234278 msg. reply_to = env. reply_to;
279+ let protocol = protocol_for( msg_type) ;
235280 // Spawn each delivery independently so one unreachable peer
236281 // cannot block others. Cap the outbox-open at 5 seconds.
237282 let ep = Arc :: clone( & endpoint) ;
@@ -240,7 +285,7 @@ pub async fn run(
240285 tokio:: spawn( async move {
241286 match tokio:: time:: timeout(
242287 Duration :: from_secs( 5 ) ,
243- ep. outbox( res. as_ref( ) , & base, rpc :: RPC_PROTOCOL_ID ) ,
288+ ep. outbox( res. as_ref( ) , & base, protocol ) ,
244289 )
245290 . await
246291 {
0 commit comments