chatcommand_joinchatroom.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "chatcommand_joinchatroom.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "chatroom.h"
  6. #include "chatcommand_logout.h"
  7. #include "Debug.h"
  8. // joinchatroom(messageid,chatroomid,leave_other_chatrooms,answerhashed,server_password)
  9. // joins a chatroom.
  10. // Sends a message to all users in the chatroom that a user joined.
  11. bool chatcommand_joinchatroom::processmessage(char first_letter,message *received_message,chatclient *client)
  12. {
  13. if ((first_letter != 'j')
  14. || (received_message->actual_message.substr(0,13)!="joinchatroom(")) {
  15. return false;
  16. }
  17. Debug debug(__FILE__,__func__,__LINE__);
  18. chatroom *chatroom_to_join;
  19. message *new_message;
  20. datastring method_parameters;
  21. parameters parameters_parsed;
  22. bool parameter_success = true;
  23. bool secure;
  24. bool allowed;
  25. stringbuilder output_message;
  26. datastring error_message;
  27. int64_t messageid;
  28. int64_t chatroomid;
  29. int64_t leave_other_chatrooms;
  30. datastring answer_hashed;
  31. datastring server_password;
  32. debug = __LINE__;
  33. method_parameters = received_message->actual_message.substr(13,received_message->actual_message.length-14);
  34. parameters_parsed.long_parameter(method_parameters,parameter_success);
  35. parameters_parsed.long_parameter(method_parameters,parameter_success);
  36. parameters_parsed.long_parameter(method_parameters,parameter_success);
  37. parameters_parsed.string_parameter(method_parameters,parameter_success);
  38. parameters_parsed.string_parameter(method_parameters,parameter_success);
  39. debug = __LINE__;
  40. if (parameter_success) {
  41. // Give the parameters nice names.
  42. debug = __LINE__;
  43. messageid = parameters_parsed.long_parameters[0];
  44. chatroomid = parameters_parsed.long_parameters[1];
  45. leave_other_chatrooms = parameters_parsed.long_parameters[2];
  46. answer_hashed = parameters_parsed.string_parameters[3];
  47. server_password = parameters_parsed.string_parameters[4];
  48. debug = __LINE__;
  49. secure = (server_password == *(the_websocket->server_password));
  50. // Find this chatroom.
  51. debug = __LINE__;
  52. chatroom_to_join = chatroom::find(&the_websocket->chatrooms,chatroomid);
  53. if (chatroom_to_join == nullptr) {
  54. debug = __LINE__;
  55. error_message = "That chatroom wasn't found.";
  56. error(client,error_message,messageid);
  57. return true;
  58. }
  59. // Check security:
  60. debug = __LINE__;
  61. allowed = true;
  62. if ((!chatroom_to_join->everyone_can_join) && (!secure)) {
  63. allowed = false;
  64. }
  65. debug = __LINE__;
  66. if ((allowed)
  67. && (chatroom_to_join->answer_hashed != nullptr)
  68. && (chatroom_to_join->answer_hashed->length > 0)) {
  69. if (*chatroom_to_join->answer_hashed != answer_hashed) {
  70. allowed = false;
  71. }
  72. }
  73. debug = __LINE__;
  74. if (!allowed) {
  75. debug = __LINE__;
  76. error_message = "You're not allowed in that chatroom.";
  77. error(client,error_message,messageid);
  78. return true;
  79. }
  80. debug = __LINE__;
  81. // Is this client already in the room?
  82. if (chatroom_to_join->clients.find(client)) {
  83. debug = __LINE__;
  84. error_message = "You're already in this chatroom.";
  85. error(client,error_message,messageid);
  86. return true;
  87. }
  88. debug = __LINE__;
  89. if (leave_other_chatrooms != 0) {
  90. // Leave all other chatrooms.
  91. debug = __LINE__;
  92. chatcommand_logout::exit_all_chatrooms(messageid,client,true);
  93. }
  94. // Join chat room.
  95. debug = __LINE__;
  96. chatroom_to_join->add_client(client);
  97. client->add_chatroom(chatroom_to_join);
  98. // Send a message to all the users in the chatroom that a new person joined.
  99. debug = __LINE__;
  100. new_message = userjoinedchatroom(messageid,chatroom_to_join->chatroomid,client->chatclientid, client->logged_in_user);
  101. chatroom_to_join->send_message_to_clients(new_message);
  102. idisposable::dereference((idisposable**)&new_message);
  103. } else {
  104. debug = __LINE__;
  105. parameters_not_correct(client,received_message);
  106. }
  107. debug = __LINE__;
  108. return true;
  109. }