Browse Source

Add change password functionality with minimum testing

master
Matthew Faltys 5 years ago
parent
commit
a8f126fca7
  1. 90
      nethack-launcher.go

90
nethack-launcher.go

@ -7,7 +7,6 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strconv"
@ -15,7 +14,6 @@ import (
"sync"
"time"
"github.com/unixvoid/glogger"
"golang.org/x/crypto/sha3"
"gopkg.in/gcfg.v1"
"gopkg.in/redis.v5"
@ -45,20 +43,16 @@ var (
func main() {
// read conf file
readConf()
// init config file and logger
initLogger()
// init redis connection
redisClient, redisErr := initRedisConnection()
if redisErr != nil {
//glogger.Debug.Printf("redis connection cannot be made, trying again in %s second(s)\n", config.NethackLauncher.BootstrapDelay*time.Second)
time.Sleep(config.NethackLauncher.BootstrapDelay * time.Second)
redisClient, redisErr = initRedisConnection()
if redisErr != nil {
//glogger.Error.Println("redis connection cannot be made, exiting.")
panic(redisErr)
}
} else {
//glogger.Debug.Println("connection to redis succeeded.")
}
// create initial files needed by nethack
@ -80,19 +74,6 @@ func readConf() {
}
}
func initLogger() {
// init logger
if config.NethackLauncher.Loglevel == "debug" {
glogger.LogInit(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
} else if config.NethackLauncher.Loglevel == "cluster" {
glogger.LogInit(os.Stdout, os.Stdout, ioutil.Discard, os.Stderr)
} else if config.NethackLauncher.Loglevel == "info" {
glogger.LogInit(os.Stdout, ioutil.Discard, ioutil.Discard, os.Stderr)
} else {
glogger.LogInit(ioutil.Discard, ioutil.Discard, ioutil.Discard, os.Stderr)
}
}
func initRedisConnection() (*redis.Client, error) {
// init redis connection
redisClient := redis.NewClient(&redis.Options{
@ -108,14 +89,14 @@ func initRedisConnection() (*redis.Client, error) {
func checkFiles() {
// make sure record file exists
if _, err := os.Stat(fmt.Sprintf("%s/record", config.NethackLauncher.HackDir)); os.IsNotExist(err) {
glogger.Info.Printf("record file not found in %s/record\n", config.NethackLauncher.HackDir)
fmt.Printf("record file not found in %s/record\n", config.NethackLauncher.HackDir)
fmt.Printf("%s\n", err)
os.Exit(1)
}
// make sure initial rcfile exists
hackRCLoc := fmt.Sprintf("%s/.nethackrc", config.NethackLauncher.HackDir)
if _, err := os.Stat(hackRCLoc); os.IsNotExist(err) {
glogger.Info.Printf("initial config file not found at: %s\n", hackRCLoc)
fmt.Printf("initial config file not found at: %s\n", hackRCLoc)
fmt.Printf("%s\n", err)
os.Exit(1)
}
@ -182,6 +163,7 @@ func printUserScreen(redisClient *redis.Client, username string) string {
fmt.Printf(" Logged in as: %s\n", username)
println("")
println(" l) Logout")
println(" c) Change password")
println(" w) Watch games in progress")
println(" h) View highscores")
println(" e) Edit config")
@ -215,6 +197,9 @@ func printUserScreen(redisClient *redis.Client, username string) string {
nh.Run()
clearScreen()
printUserScreen(redisClient, username)
case "c":
printChangePasswordScreen(redisClient, username)
clearScreen()
case "w":
clearScreen()
printProgressScreen(redisClient, username)
@ -372,6 +357,67 @@ func printRegisterScreen(redisClient *redis.Client) {
}
}
func printChangePasswordScreen(redisClient *redis.Client, username string) {
// TODO : configure password restriction checking
clearScreen()
fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay)
println("")
fmt.Printf(" Welcome %s\n", username)
println(" Only characters and numbers are allowed, with no spaces.")
println(" 20 characters max. (blank entry aborts)")
println("")
//fmt.Printf(">> ")
scanner := bufio.NewScanner(os.Stdin)
//for scanner.Scan() {
// if scanner.Text() == "" {
// printUserScreen(redisClient, username)
// }
// turn off echo display
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
reader := bufio.NewReader(os.Stdin)
noPass := true
sec := ""
for noPass {
// pull pass the first time
fmt.Printf(" Please enter your password (blank entry aborts).\n>> ")
sec0, _ := reader.ReadString('\n')
sec0 = strings.Replace(sec0, "\n", "", -1)
if sec0 == "" {
printWelcomeScreen(redisClient)
}
// pull pass the second time
fmt.Printf("\n Please enter your password again.\n>> ")
sec1, _ := reader.ReadString('\n')
sec1 = strings.Replace(sec1, "\n", "", -1)
// make sure passwords match
if sec0 == sec1 {
sec = sec0
noPass = false
} else {
fmt.Println("Lets try that again.")
}
}
// reset display
exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run()
// set user in redis
secHash := sha3.Sum512([]byte(sec))
redisClient.Set(fmt.Sprintf("user:%s", username), fmt.Sprintf("%x", secHash), 0).Err()
// back to main screen
printUserScreen(redisClient, username)
//}
if err := scanner.Err(); err != nil {
fmt.Printf("%s\n", err)
}
}
func printProgressScreen(redisClient *redis.Client, username string) {
// print header
fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay)

Loading…
Cancel
Save