Exemple #1
0
func newUser(conn *wrappedConnection) types.User {
	for {
		name := utils.GetUserInput(conn, "Desired username: "******"" {
			return nil
		}

		user := model.GetUserByName(name)
		password := ""

		if user != nil {
			utils.WriteLine(conn, "That name is unavailable", types.ColorModeNone)
		} else if err := utils.ValidateName(name); err != nil {
			utils.WriteLine(conn, err.Error(), types.ColorModeNone)
		} else {
			conn.WillEcho()
			for {
				pass1 := utils.GetRawUserInputSuffix(conn, "Desired password: "******"\r\n", types.ColorModeNone)

				if len(pass1) < 7 {
					utils.WriteLine(conn, "Passwords must be at least 7 letters in length", types.ColorModeNone)
					continue
				}

				pass2 := utils.GetRawUserInputSuffix(conn, "Confirm password: "******"\r\n", types.ColorModeNone)

				if pass1 != pass2 {
					utils.WriteLine(conn, "Passwords do not match", types.ColorModeNone)
					continue
				}

				password = pass1

				break
			}
			conn.WontEcho()

			admin := model.UserCount() == 0
			user = model.CreateUser(name, password, admin)
			return user
		}
	}
}
Exemple #2
0
func login(conn *wrappedConnection) types.User {
	for {
		username := utils.GetUserInput(conn, "Username: "******"" {
			return nil
		}

		user := model.GetUserByName(username)

		if user == nil {
			utils.WriteLine(conn, "User not found", types.ColorModeNone)
		} else if user.IsOnline() {
			utils.WriteLine(conn, "That user is already online", types.ColorModeNone)
		} else {
			attempts := 1
			conn.WillEcho()
			for {
				password := utils.GetRawUserInputSuffix(conn, "Password: "******"\r\n", types.ColorModeNone)

				// TODO - Disabling password verification to make development easier
				if user.VerifyPassword(password) || true {
					break
				}

				if attempts >= 3 {
					utils.WriteLine(conn, "Too many failed login attempts", types.ColorModeNone)
					conn.Close()
					panic("Booted user due to too many failed logins (" + user.GetName() + ")")
				}

				attempts++

				time.Sleep(2 * time.Second)
				utils.WriteLine(conn, "Invalid password", types.ColorModeNone)
			}
			conn.WontEcho()

			return user
		}
	}
}
Exemple #3
0
func (self *connectionHandler) mainMenu() {
	utils.ExecMenu(
		"MUD",
		self,
		func(menu *utils.Menu) {
			menu.AddAction("l", "Login", func() bool {
				self.user = login(self.conn)
				return false
			})

			menu.AddAction("n", "New user", func() bool {
				self.user = newUser(self.conn)
				return false
			})

			menu.OnExit(func() {
				utils.WriteLine(self.conn, "Take luck!", types.ColorModeNone)
				self.conn.Close()
			})
		})
}
Exemple #4
0
func (self *connectionHandler) WriteLine(line string, a ...interface{}) {
	utils.WriteLine(self.conn, fmt.Sprintf(line, a...), types.ColorModeNone)
}
Exemple #5
0
func (self *User) WriteLine(line string, a ...interface{}) {
	utils.WriteLine(self.conn, fmt.Sprintf(line, a...), self.GetColorMode())
}