chatcommand_login.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "chatcommand_login.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "chatroom.h"
  6. // login(messageid,username,password)
  7. // Logs this connection in to the user.
  8. // Sends a success or error message.
  9. bool chatcommand_login::processmessage(char first_letter,message *received_message,chatclient *client)
  10. {
  11. if ((first_letter != 'l')
  12. || (received_message->actual_message.substr(0,6)!="login(")) {
  13. return false;
  14. }
  15. message *new_message;
  16. user *new_user;
  17. datastring method_parameters;
  18. parameters parameters_parsed;
  19. datastring error_message;
  20. user *logged_in_user;
  21. bool parameter_success = true;
  22. stringbuilder output;
  23. method_parameters = received_message->actual_message.substr(6,received_message->actual_message.length-7);
  24. parameters_parsed.long_parameter(method_parameters,parameter_success);
  25. parameters_parsed.string_parameter(method_parameters,parameter_success);
  26. parameters_parsed.string_parameter(method_parameters,parameter_success);
  27. if (parameter_success) {
  28. logged_in_user = user::find(&the_websocket->users,parameters_parsed.string_parameters[1]);
  29. if ((logged_in_user == nullptr)
  30. || (*logged_in_user->password != parameters_parsed.string_parameters[2])) {
  31. error_message = "Incorrect login.";
  32. error(client,error_message,parameters_parsed.long_parameters[0]);
  33. } else {
  34. client->logged_in_user = logged_in_user;
  35. if (client->chatrooms != nullptr) {
  36. // Send a message to all chatrooms that this user logged in.
  37. biglist_iterator<chatroom *>chatroom_loop(client->chatrooms);
  38. while (!chatroom_loop.eof()) {
  39. // Send a message to all clients that this user logged in.
  40. new_message = userjoinedchatroom(parameters_parsed.long_parameters[0],chatroom_loop.item->chatroomid,client->chatclientid,logged_in_user);
  41. chatclient::send_message_to_clients(&chatroom_loop.item->clients,new_message);
  42. idisposable::dereference((idisposable**)&new_message);
  43. chatroom_loop.movenext();
  44. }
  45. }
  46. success_message(client,parameters_parsed.long_parameters[0]);
  47. }
  48. } else {
  49. parameters_not_correct(client,received_message);
  50. }
  51. return true;
  52. }