Skip to content

Commit 12d5c17

Browse files
Add jamulusserver/privateChatMessage RPC method for sending messages to single channels (#3731)
* add jamulusserver/privateChatMessage RPC method for sending messages to single channels * Check for invalid channel ID before sending private chat message and send corresponding response * Added sanity checks for private chat message string * update comment * Apply suggestions from code review Co-authored-by: Peter L Jones <pljones@users.noreply.github.com> --------- Co-authored-by: Peter L Jones <pljones@users.noreply.github.com>
1 parent e12a2e1 commit 12d5c17

4 files changed

Lines changed: 50 additions & 6 deletions

File tree

docs/JSON-RPC.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ Results:
344344

345345
### jamulusserver/broadcastChatMessage
346346

347-
Sends a message (as the server) to all connected clients. This can be used to broadcast messages from external sources (e.g. scripts or monitoring tools).
347+
Sends a message (as the server) to all connected clients. This can be used to broadcast messages from external sources (e.g. scripts or monitoring tools).
348348

349349
Parameters:
350350

@@ -430,6 +430,24 @@ Results:
430430
| result.registrationStatus | string | The server registration status as string (see ESvrRegStatus and SerializeRegistrationStatus). |
431431

432432

433+
### jamulusserver/privateChatMessage
434+
435+
Sends a chat message to a single connected client.
436+
437+
Parameters:
438+
439+
| Name | Type | Description |
440+
| --- | --- | --- |
441+
| params.chatMessage | string | The chat message text. |
442+
| params.id | number | The client's channel id. |
443+
444+
Results:
445+
446+
| Name | Type | Description |
447+
| --- | --- | --- |
448+
| result | string | "ok" or "error" if bad arguments. |
449+
450+
433451
### jamulusserver/restartRecording
434452

435453
Restarts the recording into a new directory.

src/server.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,13 +1379,15 @@ void CServer::SendChatTextToAllConChannels ( const int iSendingChanID, const QSt
13791379
emit sentChatMessage ( iSendingChanID, strChatText );
13801380
}
13811381

1382-
void CServer::SendChatTextToConChannel ( const int iCurChanID, const QString& strChatText )
1382+
bool CServer::SendChatTextToConChannel ( const int iCurChanID, const QString& strChatText )
13831383
{
1384-
if ( MathUtils::InRange<int> ( iCurChanID, 0, iMaxNumChannels - 1 ) && vecChannels[iCurChanID].IsConnected() )
1384+
if ( !MathUtils::InRange<int> ( iCurChanID, 0, iMaxNumChannels - 1 ) || !vecChannels[iCurChanID].IsConnected() )
13851385
{
1386-
// send message
1387-
vecChannels[iCurChanID].CreateChatTextMes ( strChatText );
1386+
return false;
13881387
}
1388+
// send message
1389+
vecChannels[iCurChanID].CreateChatTextMes ( strChatText );
1390+
return true;
13891391
}
13901392

13911393
void CServer::CreateAndSendRecorderStateForAllConChannels()

src/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
192192
bool IsDelayPanningEnabled() { return bDelayPan; }
193193

194194
void SendChatTextToAllConChannels ( const int iSendingChanID, const QString& strChatText );
195-
void SendChatTextToConChannel ( const int iCurChanID, const QString& strChatText );
195+
bool SendChatTextToConChannel ( const int iCurChanID, const QString& strChatText );
196196

197197
protected:
198198
// access functions for actual channels

src/serverrpc.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ CServerRpc::CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* pare
113113
response["result"] = "ok";
114114
} );
115115

116+
/// @rpc_method jamulusserver/privateChatMessage
117+
/// @brief Sends a chat message to a single connected client.
118+
/// @param {string} params.chatMessage - The chat message text.
119+
/// @param {number} params.id - The client's channel id.
120+
/// @result {string} result - "ok" or "error" if bad arguments.
121+
pRpcServer->HandleMethod ( "jamulusserver/privateChatMessage", [=] ( const QJsonObject& params, QJsonObject& response ) {
122+
auto jsonChatMessage = params["chatMessage"];
123+
const int id = params["id"].toInt ( INVALID_CLIENT_ID );
124+
const QString chatMessage = jsonChatMessage.toString();
125+
if ( chatMessage.isEmpty() || chatMessage.size() > MAX_LEN_CHAT_TEXT )
126+
{
127+
response["error"] =
128+
CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: chatMessage is not a string or malformed" );
129+
return;
130+
}
131+
132+
if ( !pServer->SendChatTextToConChannel ( id, chatMessage ) )
133+
{
134+
response["error"] = "invalid channel ID";
135+
return;
136+
}
137+
response["result"] = "ok";
138+
} );
139+
116140
/// @rpc_method jamulusserver/getRecorderStatus
117141
/// @brief Returns the recorder state.
118142
/// @param {object} params - No parameters (empty object).

0 commit comments

Comments
 (0)