chatcommand_leavechatroom.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "chatcommand_leavechatroom.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. // leaves a chatroom.
  9. // Sends a message to all users in the chatroom that a user left.
  10. bool chatcommand_leavechatroom::processmessage(char first_letter,message *received_message,chatclient *client)
  11. {
  12. if ((first_letter != 'l')
  13. || (received_message->actual_message.substr(0,14)!="leavechatroom(")) {
  14. return false;
  15. }
  16. Debug debug(__FILE__,__func__,__LINE__);
  17. chatroom *chatroom_to_leave;
  18. message *new_message;
  19. datastring method_parameters;
  20. parameters parameters_parsed;
  21. bool parameter_success = true;
  22. datastring error_message;
  23. int64_t messageid;
  24. int64_t chatroomid;
  25. debug = __LINE__;
  26. method_parameters = received_message->actual_message.substr(14,received_message->actual_message.length-15);
  27. parameters_parsed.long_parameter(method_parameters,parameter_success);
  28. parameters_parsed.long_parameter(method_parameters,parameter_success);
  29. debug = __LINE__;
  30. if (parameter_success) {
  31. // Give the parameters nice names.
  32. debug = __LINE__;
  33. messageid = parameters_parsed.long_parameters[0];
  34. chatroomid = parameters_parsed.long_parameters[1];
  35. // Find this chatroom.
  36. debug = __LINE__;
  37. chatroom_to_leave = chatroom::find(&the_websocket->chatrooms,chatroomid);
  38. if (chatroom_to_leave == nullptr) {
  39. debug = __LINE__;
  40. error_message = "That chatroom wasn't found.";
  41. error(client,error_message,messageid);
  42. return true;
  43. }
  44. debug = __LINE__;
  45. // Is this client currently in the room?
  46. if (!chatroom_to_leave->clients.find(client)) {
  47. debug = __LINE__;
  48. error_message = "You're not in this chatroom.";
  49. error(client,error_message,messageid);
  50. return true;
  51. }
  52. debug = __LINE__;
  53. // leave chat room.
  54. debug = __LINE__;
  55. chatroom_to_leave->remove_client(client);
  56. debug = __LINE__;
  57. client->remove_chatroom(chatroom_to_leave);
  58. debug = __LINE__;
  59. if (chatroom_to_leave->has_clients()) {
  60. // Send a message to all the users in the chatroom that a person left.
  61. debug = __LINE__;
  62. new_message = userleftchatroom(messageid,chatroomid,client->chatclientid, client->logged_in_user);
  63. debug = __LINE__;
  64. chatroom_to_leave->send_message_to_clients(new_message);
  65. // Send the same message back to the client that sent this request.
  66. client->push_message(&new_message);
  67. } else {
  68. // Delete this chatroom if it's empty and delete_if_unused.
  69. if (chatroom_to_leave->delete_if_unused) {
  70. debug = __LINE__;
  71. the_websocket->chatrooms.remove(chatroom_to_leave);
  72. debug = __LINE__;
  73. delete chatroom_to_leave;
  74. chatroom_to_leave = nullptr;
  75. // Tell all users the chatroom was deleted.
  76. debug = __LINE__;
  77. new_message = chatroomwasdeleted(messageid,chatroomid);
  78. debug = __LINE__;
  79. chatclient::send_message_to_clients(&(the_websocket->chatclients),new_message);
  80. idisposable::dereference((idisposable**)&new_message);
  81. }
  82. }
  83. } else {
  84. debug = __LINE__;
  85. parameters_not_correct(client,received_message);
  86. }
  87. debug = __LINE__;
  88. return true;
  89. }