chatcommand_getchatroomlist.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "chatcommand_getchatroomlist.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "chatroom.h"
  6. #include "biglist.h"
  7. #include <stdio.h>
  8. // getchatroomlist(messageid)
  9. // Gets the list of chatrooms.
  10. bool chatcommand_getchatroomlist::processmessage(char first_letter,message *received_message,chatclient *client)
  11. {
  12. if ((first_letter != 'g')
  13. || (received_message->actual_message.substr(0,16)!="getchatroomlist(")) {
  14. return false;
  15. }
  16. chatroom *new_chatroom;
  17. message *new_message;
  18. datastring method_parameters;
  19. parameters parameters_parsed;
  20. bool parameter_success = true;
  21. stringbuilder output_message;
  22. bool show_chatroom;
  23. int64_t userid;
  24. method_parameters = received_message->actual_message.substr(16,received_message->actual_message.length-17);
  25. parameters_parsed.long_parameter(method_parameters,parameter_success);
  26. if (parameter_success) {
  27. userid = client->logged_in_user == nullptr ? 0 : client->logged_in_user->userid;
  28. // Build the output message.
  29. // This should have some thread locking of the chatrooms list in the future.
  30. // Currently, it doesn't need locking because there's only one work thread.
  31. //chatroomlist(messageid,number_of_chatrooms,[chatroomid,room_name])
  32. output_message += "chatroomlist(";
  33. output_message.addparameter(parameters_parsed.long_parameters[0]);
  34. biglist_iterator<chatroom *>loop(&the_websocket->chatrooms);
  35. while(!loop.eof()) {
  36. auto chatroom = loop.item;
  37. // Should this chat room be shown to this user?
  38. show_chatroom = false;
  39. if (chatroom->everyone_can_join) {
  40. show_chatroom = true;
  41. } else {
  42. if ((userid != 0)
  43. && (user::find(&chatroom->allowed_users,userid) != nullptr)) {
  44. show_chatroom = true;
  45. }
  46. }
  47. if (show_chatroom) {
  48. // Add this chat room to the list of chatrooms to show.
  49. output_message.addparameter(chatroom->chatroomid);
  50. output_message.addparameter(*chatroom->name);
  51. output_message.addparameter(chatroom->info(client));
  52. }
  53. loop.movenext();
  54. }
  55. output_message += ")";
  56. new_message = new message();
  57. *new_message = output_message;
  58. output_message.clear();
  59. // Send the message.
  60. client->add_message(new_message);
  61. client->callback_on_writable();
  62. idisposable::dereference((idisposable**)&new_message);
  63. } else {
  64. parameters_not_correct(client,received_message);
  65. }
  66. return true;
  67. }