public void HandleJoinRequest(TcpMessage msg, Socket s) { bool validJoinRequest = true; TcpMessage reply = new TcpMessage(); reply.id = TcpConst.JOIN; reply.type = TcpConst.REPLY; reply.source = "SERVER"; reply.destination = msg.source; string suggested_username = (msg.text_attributes.ElementAt(0)); string suggested_password = (msg.text_attributes.ElementAt(1)); string suggested_email = (msg.text_attributes.ElementAt(2)); reply.AddTextAttribute(suggested_username); reply.AddTextAttribute(suggested_password); reply.AddTextAttribute(suggested_email); if (!UsernameExist(suggested_username)) { reply.AddBoolAttribute(true); } else { validJoinRequest = false; reply.AddBoolAttribute(false); } if (PasswordFormatIsValid(suggested_password)) { reply.AddBoolAttribute(true); } else { validJoinRequest = false; reply.AddBoolAttribute(false); } if (EmailIsValid(suggested_email)) { reply.AddBoolAttribute(true); } else { validJoinRequest = false; reply.AddBoolAttribute(false); } //Add username, password and email to database. //Generate a confirmation code and save it in database, and send it to the user's email. if (validJoinRequest == true) { string code = null; code = GenerateConfirmationCode(); SendEmailTo(suggested_email, "Login and give this code: " + code, "Account verification"); db.AddNewUser(suggested_username, suggested_password, suggested_email, code); } ServerSend(reply, s); }