chatcommand_getusersinchatroom.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "chatcommand_getusersinchatroom.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "chatroom.h"
  6. #include "biglist.h"
  7. #include "Debug.h"
  8. #include <stdio.h>
  9. // getusersinchatroom(messageid,chatroomid)
  10. // Gets the list of users in a chatroom.
  11. bool chatcommand_getusersinchatroom::processmessage(char first_letter,message *received_message,chatclient *client)
  12. {
  13. if ((first_letter != 'g')
  14. || (received_message->actual_message.substr(0,19)!="getusersinchatroom(")) {
  15. return false;
  16. }
  17. Debug debug(__FILE__,__func__,__LINE__);
  18. chatroom *this_chatroom;
  19. chatclient *loop_client;
  20. message *new_message;
  21. datastring method_parameters;
  22. parameters parameters_parsed;
  23. bool parameter_success = true;
  24. stringbuilder output_message;
  25. int64_t messageid;
  26. int64_t chatroomid;
  27. datastring error_message;
  28. user *this_user;
  29. bool is_user_in_chatroom;
  30. int64_t chatclientid;
  31. int64_t userid;
  32. debug = __LINE__;
  33. method_parameters = received_message->actual_message.substr(19,received_message->actual_message.length-20);
  34. parameters_parsed.long_parameter(method_parameters,parameter_success);
  35. parameters_parsed.long_parameter(method_parameters,parameter_success);
  36. debug = __LINE__;
  37. if (parameter_success) {
  38. debug = __LINE__;
  39. messageid = parameters_parsed.long_parameters[0];
  40. chatroomid = parameters_parsed.long_parameters[1];
  41. chatclientid = client->chatclientid;
  42. this_user = client->logged_in_user;
  43. userid = this_user == nullptr ? 0 : this_user->userid;
  44. // Find this chatroom.
  45. debug = __LINE__;
  46. this_chatroom = chatroom::find(client->chatrooms,chatroomid);
  47. if (this_chatroom == nullptr) {
  48. debug = __LINE__;
  49. error_message = "That chatroom wasn't found.";
  50. error(client,error_message,messageid);
  51. return true;
  52. }
  53. // Is this user in the chatroom?
  54. debug = __LINE__;
  55. is_user_in_chatroom = false;
  56. biglist_iterator<chatclient *>client_loop(&this_chatroom->clients);
  57. while (!client_loop.eof()) {
  58. if (client_loop.item->chatclientid == chatclientid) {
  59. is_user_in_chatroom = true;
  60. break;
  61. }
  62. if ((userid != 0)
  63. && (client_loop.item->logged_in_user != nullptr)
  64. && (client_loop.item->logged_in_user->userid == userid)) {
  65. is_user_in_chatroom = true;
  66. break;
  67. }
  68. client_loop.movenext();
  69. }
  70. debug = __LINE__;
  71. if (!is_user_in_chatroom) {
  72. error_message = "You are not in that chatroom.";
  73. error(client,error_message,messageid);
  74. return true;
  75. }
  76. // Build the output message.
  77. // usersinchatroom(messageid,chatroomid,[chatclientid,userid,username])
  78. debug = __LINE__;
  79. output_message += "usersinchatroom(";
  80. output_message.addparameter(messageid);
  81. output_message.addparameter(chatroomid);
  82. debug = __LINE__;
  83. client_loop.movefirst();
  84. debug = __LINE__;
  85. while(!client_loop.eof()) {
  86. debug = __LINE__;
  87. loop_client = client_loop.item;
  88. output_message.addparameter(loop_client->chatclientid);
  89. debug = __LINE__;
  90. this_user = loop_client->logged_in_user;
  91. if (this_user == nullptr) {
  92. output_message += "0,0,";
  93. } else {
  94. output_message.addparameter(this_user->userid);
  95. output_message.addparameter(*this_user->username);
  96. }
  97. client_loop.movenext();
  98. }
  99. debug = __LINE__;
  100. output_message += ")";
  101. new_message = new message();
  102. *new_message = output_message;
  103. debug = __LINE__;
  104. output_message.clear();
  105. // Send the message.
  106. client->push_message(&new_message);
  107. debug = __LINE__;
  108. } else {
  109. parameters_not_correct(client,received_message);
  110. }
  111. debug = __LINE__;
  112. return true;
  113. }