chatcommand_sendtoall.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "chatcommand_sendtoall.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "stringbuilder.h"
  6. // sendtoall(server_password,messageid,message)
  7. // Returns success(messageid) or error(message,messageid);
  8. // sends privatemessage(chatclientid,userid,username,message) to all users.
  9. // If user isn't logged in, userid = 0 and username is blank.
  10. bool chatcommand_sendtoall::processmessage(char first_letter,message *received_message,chatclient *client)
  11. {
  12. if ((first_letter != 's')
  13. || (received_message->actual_message.substr(0,10)!="sendtoall(")) {
  14. return false;
  15. }
  16. message *new_message;
  17. datastring method_parameters;
  18. parameters parameters_parsed;
  19. bool parameter_success = true;
  20. stringbuilder output_message;
  21. method_parameters = received_message->actual_message.substr(10,received_message->actual_message.length-11);
  22. method_parameters.null_terminate(); // Used to comply with snprintf.
  23. parameters_parsed.string_parameter(method_parameters,parameter_success);
  24. parameters_parsed.long_parameter(method_parameters,parameter_success);
  25. parameters_parsed.string_parameter(method_parameters,parameter_success);
  26. if (parameter_success) {
  27. if (parameters_parsed.string_parameters[0] != *(the_websocket->server_password)) {
  28. // Incorrect server password.
  29. incorrect_system_password(client,parameters_parsed.long_parameters[1]);
  30. } else {
  31. // Build the output message.
  32. output_message += "privatemessage(";
  33. output_message.addparameter(client->chatclientid);
  34. if (client->logged_in_user == nullptr) {
  35. output_message += "0,0,";
  36. } else {
  37. output_message.addparameter(client->logged_in_user->userid);
  38. output_message.addparameter(*client->logged_in_user->username);
  39. }
  40. output_message.addparameter(parameters_parsed.string_parameters[2]);
  41. output_message += ")";
  42. new_message = new message();
  43. *new_message = output_message;
  44. // send the output message to all clients.
  45. chatclient::send_message_to_clients(&(the_websocket->chatclients),new_message);
  46. idisposable::dereference((idisposable**)&new_message);
  47. // Send a success message be sent back to the client.
  48. success_message(client,parameters_parsed.long_parameters[1]);
  49. }
  50. } else {
  51. parameters_not_correct(client,received_message);
  52. }
  53. return true;
  54. }