chatcommand_adduser.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "chatcommand_adduser.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "chatroom.h"
  6. // adduser(messageid,userid,username,password,server_password)
  7. // Creates a new chatroom. Joins this user to the chatroom.
  8. // Sends a message to all users that a chatroom was created.
  9. bool chatcommand_adduser::processmessage(char first_letter,message *received_message,chatclient *client)
  10. {
  11. if ((first_letter != 'a')
  12. || (received_message->actual_message.substr(0,8)!="adduser(")) {
  13. return false;
  14. }
  15. user *new_user;
  16. datastring method_parameters;
  17. parameters parameters_parsed;
  18. datastring error_message;
  19. bool parameter_success = true;
  20. bool secure;
  21. int64_t messageid;
  22. method_parameters = received_message->actual_message.substr(8,received_message->actual_message.length-9);
  23. parameters_parsed.long_parameter(method_parameters,parameter_success);
  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. parameters_parsed.string_parameter(method_parameters,parameter_success);
  28. if (parameter_success) {
  29. messageid = parameters_parsed.long_parameters[0];
  30. secure = (parameters_parsed.string_parameters[4] == *(the_websocket->server_password));
  31. if (!secure) {
  32. incorrect_system_password(client,messageid);
  33. } else {
  34. if (user::find(&the_websocket->users,parameters_parsed.string_parameters[1])!=nullptr) {
  35. error_message = "That userid is already in use.";
  36. error(client,error_message,messageid);
  37. } else {
  38. if (user::find(&the_websocket->users,parameters_parsed.string_parameters[2])!=nullptr) {
  39. error_message = "That user name is already in use.";
  40. error(client,error_message,messageid);
  41. } else {
  42. // Add the user.
  43. new_user = new user();
  44. new_user->userid = parameters_parsed.long_parameters[1];
  45. new_user->username = new datablock(parameters_parsed.string_parameters[2]);
  46. // Save the hash of the password.
  47. // Ideally the password was already hashed before it was sent.
  48. // So, this is the hash of the hash of the password.
  49. new_user->password = new datablock(parameters_parsed.string_parameters[3]);
  50. the_websocket->users.add(new_user);
  51. success_message(client,messageid);
  52. }
  53. }
  54. }
  55. } else {
  56. parameters_not_correct(client,received_message);
  57. }
  58. return true;
  59. }