chatcommand_getusers.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "chatcommand_getusers.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. // getusers(messageid,status,server_password)
  10. // Gets the list of users in ram. Server_password is required.
  11. // If status == 1, only return users that are logged in.
  12. // If status == 2, only return users that are logged off.
  13. // If status == 3, return both users logged in and users logged off.
  14. // Outputs a message like users(messageid,[userid,username,status])
  15. // status is 1 for logged in, 2 for logged out.
  16. bool chatcommand_getusers::processmessage(char first_letter,message *received_message,chatclient *client)
  17. {
  18. if ((first_letter != 'g')
  19. || (received_message->actual_message.substr(0,9)!="getusers(")) {
  20. return false;
  21. }
  22. Debug debug(__FILE__,__func__,__LINE__);
  23. message *new_message;
  24. datastring method_parameters;
  25. parameters parameters_parsed;
  26. bool parameter_success = true;
  27. stringbuilder output_message;
  28. int64_t messageid;
  29. int64_t input_status;
  30. int64_t output_status;
  31. datastring error_message;
  32. user *this_user;
  33. user *logged_in_user;
  34. bool found_user_range;
  35. bool secure;
  36. int64_t userid;
  37. int64_t smallest_userid;
  38. int64_t largest_userid;
  39. const int LOGGED_IN = 1;
  40. const int LOGGED_OUT = 2;
  41. const int RETURN_LOGGED_IN = 1;
  42. const int RETURN_LOGGED_OUT = 2;
  43. const int RETURN_ALL = 3;
  44. debug = __LINE__;
  45. method_parameters = received_message->actual_message.substr(9,received_message->actual_message.length-10);
  46. parameters_parsed.long_parameter(method_parameters,parameter_success);
  47. parameters_parsed.long_parameter(method_parameters,parameter_success);
  48. parameters_parsed.string_parameter(method_parameters,parameter_success);
  49. debug = __LINE__;
  50. if (parameter_success) {
  51. debug = __LINE__;
  52. messageid = parameters_parsed.long_parameters[0];
  53. input_status = parameters_parsed.long_parameters[1];
  54. secure = (parameters_parsed.string_parameters[2] == *(the_websocket->server_password));
  55. if (!secure) {
  56. incorrect_system_password(client,messageid);
  57. } else {
  58. output_message += "users(";
  59. output_message.addparameter(messageid);
  60. if ((input_status < RETURN_LOGGED_IN) || (input_status > RETURN_ALL)) {
  61. error_message = "Invalid status code.";
  62. error(client,error_message,messageid);
  63. return true;
  64. }
  65. debug = __LINE__;
  66. biglist_iterator<chatclient *>client_loop(&the_websocket->chatclients);
  67. debug = __LINE__;
  68. biglist_iterator<user *>user_loop(&the_websocket->users);
  69. debug = __LINE__;
  70. found_user_range = false;
  71. smallest_userid = 9223372036854775807L; // 2^63-1
  72. smallest_userid = (smallest_userid - 1) + smallest_userid;
  73. largest_userid = 0;
  74. debug = __LINE__;
  75. // Loop through all the users.
  76. while (!user_loop.eof()) {
  77. debug = __LINE__;
  78. this_user = user_loop.item;
  79. userid = this_user->userid;
  80. // Figure out if this user is logged in.
  81. output_status = LOGGED_OUT;
  82. if ((!found_user_range) || ((userid >= smallest_userid) && (userid <= largest_userid))) {
  83. client_loop.movefirst();
  84. while (!client_loop.eof()) {
  85. logged_in_user = client_loop.item->logged_in_user;
  86. if ((logged_in_user != nullptr)
  87. && (logged_in_user->userid == userid)) {
  88. output_status = LOGGED_IN;
  89. break;
  90. }
  91. client_loop.movenext();
  92. }
  93. }
  94. // Figure out if this user should be returned.
  95. if ((input_status == RETURN_ALL)
  96. || ((input_status == RETURN_LOGGED_IN)
  97. && (output_status == LOGGED_IN))
  98. || ((input_status == RETURN_LOGGED_OUT)
  99. && (output_status == LOGGED_OUT))) {
  100. // Add this user to the output list.
  101. output_message.addparameter(this_user->userid);
  102. output_message.addparameter(*this_user->username);
  103. output_message.addparameter(output_status);
  104. }
  105. user_loop.movenext();
  106. }
  107. debug = __LINE__;
  108. output_message += ")";
  109. new_message = new message();
  110. *new_message = output_message;
  111. debug = __LINE__;
  112. output_message.clear();
  113. // Send the message.
  114. client->push_message(&new_message);
  115. debug = __LINE__;
  116. }
  117. } else {
  118. parameters_not_correct(client,received_message);
  119. }
  120. debug = __LINE__;
  121. return true;
  122. }