chatcommand_logout.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "chatcommand_logout.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "chatroom.h"
  6. #include "biglist.h"
  7. // logout(messageid,disconnect)
  8. // Logs out this user.
  9. // Sends a success or error message.
  10. // Sends a message to all users in all chatrooms this person is in that they're out.
  11. bool chatcommand_logout::processmessage(char first_letter,message *received_message,chatclient *client)
  12. {
  13. if ((first_letter != 'l')
  14. || (received_message->actual_message.substr(0,7)!="logout(")) {
  15. return false;
  16. }
  17. datastring method_parameters;
  18. parameters parameters_parsed;
  19. datastring error_message;
  20. user *logged_in_user;
  21. bool parameter_success = true;
  22. bool disconnect;
  23. method_parameters = received_message->actual_message.substr(7,received_message->actual_message.length-8);
  24. parameters_parsed.long_parameter(method_parameters,parameter_success);
  25. parameters_parsed.long_parameter(method_parameters,parameter_success);
  26. if (parameter_success) {
  27. disconnect = (parameters_parsed.long_parameters[1] != 0);
  28. if (!(disconnect) && (client->logged_in_user == nullptr)) {
  29. error_message = "You are already logged out.";
  30. error(client,error_message,parameters_parsed.long_parameters[0]);
  31. } else {
  32. exit_all_chatrooms(parameters_parsed.long_parameters[0],client, true);
  33. if (disconnect) {
  34. // disconnect.
  35. client->disconnect();
  36. } else {
  37. // Send a success message.
  38. client->logged_in_user = nullptr;
  39. success_message(client,parameters_parsed.long_parameters[0]);
  40. }
  41. }
  42. } else {
  43. parameters_not_correct(client,received_message);
  44. }
  45. return true;
  46. }
  47. void chatcommand_logout::exit_all_chatrooms(int64_t messageid,chatclient *client, bool send_message)
  48. {
  49. message *new_message;
  50. chatroom *room;
  51. if (client->chatrooms != nullptr) {
  52. biglist_iterator<chatroom *>chatroom_loop(client->chatrooms);
  53. while (!chatroom_loop.eof()) {
  54. room = chatroom_loop.item;
  55. if (send_message) {
  56. new_message = userleftchatroom(messageid,room->chatroomid,client->chatclientid,client->logged_in_user);
  57. chatroom_loop.item->send_message_to_clients(new_message);
  58. idisposable::dereference((idisposable**)&new_message);
  59. }
  60. room->remove_client(client);
  61. if ((room->delete_if_unused) && (!room->has_clients())) {
  62. // Delete this chatroom.
  63. if (send_message) {
  64. new_message = chatroomwasdeleted(messageid,room->chatroomid);
  65. chatclient::send_message_to_clients(&(the_websocket->chatclients),new_message);
  66. idisposable::dereference((idisposable**)&new_message);
  67. }
  68. the_websocket->chatrooms.remove(room);
  69. delete room;
  70. room = nullptr;
  71. }
  72. chatroom_loop.row->used = false; // Remove this item from the list.
  73. chatroom_loop.movenext();
  74. }
  75. }
  76. return;
  77. }
  78. void chatcommand_logout::exit_chatroom(
  79. int64_t messageid,chatclient *client,chatroom *room
  80. ,bool send_message, bool remove_from_list)
  81. {
  82. message *new_message;
  83. if (send_message) {
  84. new_message = userleftchatroom(messageid,room->chatroomid
  85. ,client->chatclientid,client->logged_in_user);
  86. room->send_message_to_clients(new_message);
  87. idisposable::dereference((idisposable**)&new_message);
  88. }
  89. if (remove_from_list) {
  90. room->remove_client(client);
  91. if ((room->delete_if_unused) && (!room->has_clients())) {
  92. // Delete this chatroom.
  93. new_message = chatroomwasdeleted(messageid,room->chatroomid);
  94. chatclient::send_message_to_clients(&(the_websocket->chatclients),new_message);
  95. idisposable::dereference((idisposable**)&new_message);
  96. the_websocket->chatrooms.remove(room);
  97. delete room;
  98. room = nullptr;
  99. }
  100. }
  101. return;
  102. }