~kris/9p

disco

ref: b5fb6e409f55ce18ad3b5d9f9d384de47a9b38e2 disco/config.go -rw-r--r-- 3.8 KiB
b5fb6e40 — Sean Hinchee add 2.0 to .hgtags 7 years ago
                                                                                
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main

import (
	"bitbucket.org/mischief/libauth"
	"bufio"
	"fmt"
	"github.com/mischief/ndb"
	"log"
	"os"
	"os/user"
	sc "strconv"
	"strings"
)

// Types of auth we can do ­ add handling for new modes to atoam()
type AuthModes int

const (
	Pass AuthModes = iota
	Factotum
	Unknown // Placeholder
)

//Configuration is a struct that contains all configuration fields
type Configuration struct {
	AuthMode      AuthModes
	Username      string
	LoadBacklog   bool
	Messages      int
	PromptChar    string
	TimestampChar string
	password      string
}

// Config is the global configuration of discord-cli
var Config Configuration

var ConfigPath string = "/lib/disco.ndb"

// Convert a string such as "true" into true
func atob(s string) bool {
	s = strings.ToLower(s)

	if s == "true" {
		return true
	}

	return false
}

// Convert a string such as "factotum" into factotum
func atoam(s string) AuthModes {
	s = strings.ToLower(s)

	if s == "pass" {
		return Pass
	}

	if s == "factotum" {
		return Factotum
	}

	return Unknown
}

//GetConfig retrieves configuration file from $home/lib/disco.ndb, if it doesn't exist it calls CreateConfig()
func GetConfig() {
	//Get User
Start:
	usr, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}

	// Get File
	file, err := os.Open(usr.HomeDir + ConfigPath)

	if err != nil {
		log.Println("Creating new config file")
		CreateConfig()
		goto Start
	}

	file.Close()

	// Decode File
	ndb, err := ndb.Open(usr.HomeDir + ConfigPath)
	if err != nil {
		log.Fatal("error: Could not ")
	}

	Config.Username = ndb.Search("username", "").Search("username")
	Config.AuthMode = atoam(ndb.Search("auth", "").Search("auth"))

	if Config.AuthMode == Pass {
		Config.password = ndb.Search("username", "").Search("password")
	}

	Config.LoadBacklog = atob(ndb.Search("loadbacklog", "").Search("loadbacklog"))
	Config.Messages, _ = sc.Atoi(ndb.Search("messages", "").Search("messages"))
	Config.PromptChar = ndb.Search("promptchar", "").Search("promptchar")
	Config.TimestampChar = ndb.Search("timestampchar", "").Search("timestampchar")

}

//CreateConfig creates folder inside $home and makes a new empty configuration file
func CreateConfig() {
	// Get User
	usr, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}

	// Set Default values
	fmt.Print("Input your email: ")
	scan := bufio.NewScanner(os.Stdin)
	scan.Scan()

	raw := fmt.Sprintf("auth=pass\nloadbacklog=true\nmessages=10\npromptchar=→\ntimestampchar=>\n\nusername=%s	password=\n", scan.Text())

	// Create File
	os.Mkdir(usr.HomeDir+"/lib", 0775)

	file, err := os.Create(usr.HomeDir + ConfigPath)

	if err != nil {
		log.Fatalln(err)
	}

	file.Chmod(0600)

	// PrintToFile
	_, err = file.Write([]byte(raw))

	if err != nil {
		log.Fatalln(err)
	}

	file.Close()
}

// CheckState checks the current state for essential missing information, errors will fail the program
func CheckState() {
	//Get User
	usr, err := user.Current()

	if err != nil {
		log.Fatal(err)
	}

	if Config.Username == "" {
		log.Fatalln("Error: No Username Specified, please edit " + usr.HomeDir + ConfigPath)
	}

	// Check and handle password loading
	switch Config.AuthMode {
	case Factotum:
		// Acquire password from factotum
		userPwd, err := libauth.Getuserpasswd("proto=pass service=discord user=%s server=discordapp.com", Config.Username)

		if err != nil {
			// Factotum didn't get anything
			fmt.Fprintln(os.Stderr, "Warning: No success getting key from factotum, consider disabling it in ndb.")
			fmt.Fprintln(os.Stderr, "Libauth gave: ", err)
		} else {
			Config.password = userPwd.Password
		}

	case Unknown:
		log.Fatalln("Error: incorrect, or no, authmode specified via auth= config tuple. Consider: auth=(pass factotum).")

	default:
		// Password is already loaded in auth=pass mode
	}

	if Config.password == "" {
		log.Fatalln("Error: No password loaded, cannot auth. Are you missing a factotum key or password= tuple?")
	}
}