all repos — felt @ main

virtual tabletop for dungeons and dragons (and similar) using Go, MongoDB, and websockets

cmd/cmd.go (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cmd

import (
	"fmt"
	"hacklab.nilfm.cc/felt/register"
	"hacklab.nilfm.cc/quartzgun/auth"
	"strconv"
	"time"
)

func ProcessCmd(args []string, userStore auth.UserStore, crypto register.SymmetricCrypto) bool {
	if len(args) == 1 {
		return false
	}
	switch args[1] {
	case "invite":
		now := time.Now().UnixMicro()
		strNow := strconv.FormatInt(now, 10)
		self, err := crypto.Encrypt(strNow)
		if err == nil {
			fmt.Print("This is the registration key: ")
			fmt.Println(self)
		} else {
			fmt.Printf("%v\n", err)
		}
	case "adduser":
		if len(args) < 4 {
			return help(args[0])
		}
		userStore.AddUser(args[2], args[3])
	case "rmuser":
		if len(args) < 3 {
			return help(args[0])
		}
		userStore.DeleteUser(args[2])
	case "passwd":
		if len(args) < 5 {
			return help(args[0])
		}
		userStore.ChangePassword(args[2], args[3], args[4])
	default:
		return help(args[0])
	}
	return true
}

func help(prog string) bool {
	fmt.Println("usage: " + prog + " [invite | adduser NAME PW | rmuser NAME | passwd NAME OLDPW NEWPW]")
	fmt.Println("  commands:")
	fmt.Println("    invite: generates a token to be appended to the /register/ endpoint to create an admin account")
	fmt.Println("    adduser: adds a new admin user to the user.db")
	fmt.Println("    rmuser: removes named user from the user.db")
	fmt.Println("    passwd: change a user's password")
	fmt.Println("")
	fmt.Println("  adduser, rmuser, and passwd should be run while the server is offline")
	fmt.Println("  This is because external changes to the user.db will be overwritten if the server is running")
	fmt.Println("  Codes generated via the invite command are good for 15 minutes")
	return true
}