diff --git a/docs/JSON-RPC.md b/docs/JSON-RPC.md index 74e884c613..35bf5dfa64 100644 --- a/docs/JSON-RPC.md +++ b/docs/JSON-RPC.md @@ -396,6 +396,24 @@ Results: | result.registrationStatus | string | The server registration status as string (see ESvrRegStatus and SerializeRegistrationStatus). | +### jamulusserver/privateChatMessage + +Sends a chat message to a single connected client. + +Parameters: + +| Name | Type | Description | +| --- | --- | --- | +| params.chatMessage | string | The chat message text. | +| params.id | number | The client's channel id. | + +Results: + +| Name | Type | Description | +| --- | --- | --- | +| result | string | Always "ok". | + + ### jamulusserver/restartRecording Restarts the recording into a new directory. diff --git a/src/server.cpp b/src/server.cpp index 4325e7dce5..004e86d15d 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1370,13 +1370,18 @@ void CServer::SendChatTextToAllConChannels ( const QString& strChatText ) } } -void CServer::SendChatTextToConChannel ( const int iCurChanID, const QString& strChatText ) +bool CServer::SendChatTextToConChannel ( const int iCurChanID, const QString& strChatText ) { if ( MathUtils::InRange ( iCurChanID, 0, iMaxNumChannels - 1 ) && vecChannels[iCurChanID].IsConnected() ) { // send message vecChannels[iCurChanID].CreateChatTextMes ( strChatText ); } + else + { + return true; + } + return false; } void CServer::CreateAndSendRecorderStateForAllConChannels() diff --git a/src/server.h b/src/server.h index 5819012bda..4ac82c35ee 100644 --- a/src/server.h +++ b/src/server.h @@ -192,7 +192,7 @@ class CServer : public QObject, public CServerSlots bool IsDelayPanningEnabled() { return bDelayPan; } void SendChatTextToAllConChannels ( const QString& strChatText ); - void SendChatTextToConChannel ( const int iCurChanID, const QString& strChatText ); + bool SendChatTextToConChannel ( const int iCurChanID, const QString& strChatText ); protected: // access functions for actual channels diff --git a/src/serverrpc.cpp b/src/serverrpc.cpp index 315a3e262c..7f88f953dc 100644 --- a/src/serverrpc.cpp +++ b/src/serverrpc.cpp @@ -47,6 +47,9 @@ #include "serverrpc.h" +/* Definitions ****************************************************************/ +#define INVALID_CLIENT_ID -1 + CServerRpc::CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* parent ) : QObject ( parent ) { // API doc already part of CClientRpc @@ -292,6 +295,30 @@ CServerRpc::CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* pare response["result"] = "acknowledged"; Q_UNUSED ( params ); } ); + + /// @rpc_method jamulusserver/privateChatMessage + /// @brief Sends a chat message to a single connected client. + /// @param {string} params.chatMessage - The chat message text. + /// @param {number} params.id - The client's channel id. + /// @result {string} result - Always "ok". + pRpcServer->HandleMethod ( "jamulusserver/privateChatMessage", [=] ( const QJsonObject& params, QJsonObject& response ) { + auto jsonChatMessage = params["chatMessage"]; + const int id = params["id"].toInt ( INVALID_CLIENT_ID ); + const QString chatMessage = jsonChatMessage.toString(); + if ( chatMessage.isEmpty() || chatMessage.size() > MAX_LEN_CHAT_TEXT ) + { + response["error"] = + CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: chatMessage is not a string or malformed" ); + return; + } + + if ( pServer->SendChatTextToConChannel ( id, chatMessage ) ) + { + response["error"] = "invalid channel ID"; + return; + } + response["result"] = "ok"; + } ); } #if defined( Q_OS_MACOS ) && QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )