chatcommand_removechatroom.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "chatcommand_removechatroom.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "chatroom.h"
  6. #include "Debug.h"
  7. // removechatroom(messageid,chatroomid,server_password)
  8. // Removes a chatroom. Requires server password.
  9. // Sends a chatroomwasdeleted message to you and everybody in the chatroom.
  10. bool chatcommand_removechatroom::processmessage(char first_letter,message *received_message,chatclient *client)
  11. {
  12. if ((first_letter != 'r')
  13. || (received_message->actual_message.substr(0,15)!="removechatroom(")) {
  14. return false;
  15. }
  16. Debug debug(__FILE__,__func__,__LINE__);
  17. chatroom *this_chatroom;
  18. message *new_message;
  19. datastring method_parameters;
  20. parameters parameters_parsed;
  21. bool parameter_success = true;
  22. bool secure;
  23. datastring error_message;
  24. int64_t messageid;
  25. int64_t chatroomid;
  26. debug = __LINE__;
  27. method_parameters = received_message->actual_message.substr(15,received_message->actual_message.length-16);
  28. parameters_parsed.long_parameter(method_parameters,parameter_success);
  29. parameters_parsed.long_parameter(method_parameters,parameter_success);
  30. parameters_parsed.string_parameter(method_parameters,parameter_success);
  31. if (parameter_success) {
  32. // Copy parameters into permanent containers.
  33. debug = __LINE__;
  34. messageid = parameters_parsed.long_parameters[0];
  35. chatroomid = parameters_parsed.long_parameters[1];
  36. secure = (parameters_parsed.string_parameters[2] == *(the_websocket->server_password));
  37. if (!secure) {
  38. debug = __LINE__;
  39. error_message = "Incorrect server password.";
  40. error(client,error_message,messageid);
  41. return true;
  42. }
  43. // Find this chatroom.
  44. this_chatroom = nullptr;
  45. debug = __LINE__;
  46. biglist_iterator<chatroom *>chatroom_loop(&the_websocket->chatrooms);
  47. while (!chatroom_loop.eof()) {
  48. debug = __LINE__;
  49. if (chatroom_loop.item->chatroomid == chatroomid) {
  50. this_chatroom = chatroom_loop.item;
  51. chatroom_loop.row->used = false; // Remove this chatroom from the list of chatrooms.
  52. break;
  53. }
  54. debug = __LINE__;
  55. chatroom_loop.movenext();
  56. }
  57. debug = __LINE__;
  58. if (this_chatroom == nullptr) {
  59. debug = __LINE__;
  60. error_message = "That chatroom wasn't found.";
  61. error(client,error_message,messageid);
  62. return true;
  63. }
  64. // Finish removing this chatroom from all clients in the chatroom.
  65. debug = __LINE__;
  66. biglist_iterator<chatclient *>chatclient_loop(&this_chatroom->clients);
  67. while (!chatclient_loop.eof()) {
  68. chatclient_loop.item->remove_chatroom(this_chatroom);
  69. chatclient_loop.movenext();
  70. }
  71. delete this_chatroom;
  72. this_chatroom = nullptr;
  73. // Send a message to all users in this chatroom was deleted.
  74. debug = __LINE__;
  75. new_message = chatroomwasdeleted(messageid,chatroomid);
  76. chatclient::send_message_to_clients(&(the_websocket->chatclients),new_message);
  77. idisposable::dereference((idisposable**)&new_message);
  78. debug = __LINE__;
  79. } else {
  80. parameters_not_correct(client,received_message);
  81. }
  82. return true;
  83. }