chatcommand_send.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "chatcommand_send.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "stringbuilder.h"
  6. #include "message.h"
  7. #include "Debug.h"
  8. #include <inttypes.h>
  9. // send(messageid,chatroomid,message)
  10. // Send a chat message to everybody in the chatroom.
  11. // Returns chat(messageid,chatroomid,chatclientid,userid,message) or error(message,messageid);
  12. // If user isn't logged in, userid = 0.
  13. bool chatcommand_send::processmessage(char first_letter,message *received_message,chatclient *client)
  14. {
  15. if ((first_letter != 's')
  16. || (received_message->actual_message.substr(0,5)!="send(")) {
  17. return false;
  18. }
  19. Debug debug(__FILE__,__func__,__LINE__);
  20. message *new_message;
  21. datastring method_parameters;
  22. parameters parameters_parsed;
  23. bool parameter_success = true;
  24. chatroom *chat_room;
  25. int64_t messageid;
  26. int64_t chatroomid;
  27. datastring message_to_send;
  28. int64_t userid;
  29. datastring error_message;
  30. debug = __LINE__;
  31. method_parameters = received_message->actual_message.substr(5,received_message->actual_message.length-6);
  32. method_parameters.null_terminate(); // Used to comply with snprintf.
  33. parameters_parsed.long_parameter(method_parameters,parameter_success);
  34. parameters_parsed.long_parameter(method_parameters,parameter_success);
  35. parameters_parsed.string_parameter(method_parameters,parameter_success);
  36. debug = __LINE__;
  37. if (parameter_success) {
  38. // Give the parameters nice names.
  39. debug = __LINE__;
  40. messageid = parameters_parsed.long_parameters[0];
  41. chatroomid = parameters_parsed.long_parameters[1];
  42. message_to_send = parameters_parsed.string_parameters[2];
  43. userid = client->logged_in_user == nullptr ? 0 : client->logged_in_user->userid;
  44. // Find the chatroom.
  45. debug = __LINE__;
  46. //received_message->actual_message.println();
  47. //printf("chatroomid=%" PRId64 "\n",chatroomid);
  48. chat_room = chatroom::find(client->chatrooms,chatroomid);
  49. if (chat_room == nullptr) {
  50. // Chat room wasn't found.
  51. debug = __LINE__;
  52. error_message = "You're not in that chatroom.";
  53. error(client,error_message,messageid);
  54. } else {
  55. // Build the output message.
  56. debug = __LINE__;
  57. new_message = chat(messageid,chatroomid,client->chatclientid,userid,message_to_send);
  58. // send the output message.
  59. debug = __LINE__;
  60. chat_room->send_message_to_clients(new_message);
  61. idisposable::dereference((idisposable**)&new_message);
  62. }
  63. } else {
  64. debug = __LINE__;
  65. parameters_not_correct(client,received_message);
  66. }
  67. debug = __LINE__;
  68. return true;
  69. }