-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
419 lines (400 loc) · 12 KB
/
config.go
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"sync"
"time"
"golang.org/x/crypto/ssh/terminal"
"github.com/antonholmquist/jason"
)
type Config struct {
// The JSON object of what was read
LoadedJson jason.Object
Options jason.Object
FeatureTPHome bool
FeatureTPVisit bool
FeatureTP bool
FeatureDayNight bool
LoggedInMCUsers []MCUser
Whitelist []string
Ops []string
dir string
model *Model
}
var c *Config
var StopServer = false
var mu sync.Mutex
var message_manager *MessageManager
func LoadConfig(mm *MessageManager, dir string) {
message_manager = mm
c = new(Config)
c.dir = dir
c.model = InitializeModel()
// If we don't have any web users yet, we need to create one
allUsers := c.model.getAllWebUsers()
if len(allUsers) == 0 {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Create new Web User")
fmt.Print("Username: ")
uName, _ := reader.ReadString('\n')
uName = strings.TrimSpace(uName)
var pw1, pw2 []byte
for string(pw1) != string(pw2) || string(pw1) == "" {
fmt.Print("Password: ")
pw1, _ = terminal.ReadPassword(0)
fmt.Println("")
fmt.Print("Repeat Password: ")
pw2, _ = terminal.ReadPassword(0)
fmt.Println("")
if string(pw1) != string(pw2) {
fmt.Println("Entered Passwords didn't match!")
}
}
if err := c.model.updateWebUser(uName, string(pw1)); err != nil {
log.Fatal(err)
}
}
// Load the whitelist
whitelist_rd, err := ioutil.ReadFile(c.dir + "/whitelist.json")
// We have to make it an object to read it...
whitelist_rd = append(append([]byte("{\"whitelist\":"), whitelist_rd...), '}')
if err == nil {
j, _ := jason.NewObjectFromBytes(whitelist_rd)
jo, _ := j.GetObjectArray("whitelist")
for _, wl_u := range jo {
n, _ := wl_u.GetString("name")
fmt.Print("> Whitelisted User ", n, "\n")
c.Whitelist = append(c.Whitelist, n)
}
}
// Load the Op list
oplist_rd, err := ioutil.ReadFile(c.dir + "/ops.json")
// We have to make it an object to read it...
oplist_rd = append(append([]byte("{\"ops\":"), oplist_rd...), '}')
if err == nil {
j, _ := jason.NewObjectFromBytes(oplist_rd)
jo, _ := j.GetObjectArray("ops")
for _, ol_u := range jo {
n, _ := ol_u.GetString("name")
fmt.Print("> Opped User ", n, "\n")
c.Ops = append(c.Ops, n)
}
}
config_str, err := ioutil.ReadFile(c.dir + "/mcman.config")
if err == nil {
j, _ := jason.NewObjectFromBytes(config_str)
o, _ := j.GetObjectArray("options")
// Add the "Stop" listener
fmt.Println("> Activating 'stop' listener")
AddListener(func(i *Message) bool {
if i.MCUser != nil && i.Text == "!stop\n" {
DoStopServer()
return true
}
return false
})
c.FeatureTPHome = c.model.mcFeatureIsEnabled("tphome")
if c.FeatureTPHome {
fmt.Println("> Activating 'home' listeners")
// Add !set home listener
AddListener(func(i *Message) bool {
if i.MCUser != nil && i.Text == "!set home\n" {
AddTempListener(func(inp *Message) bool {
listen_for := "Teleported " + i.MCUser.Name + " to "
if inp.MCUser == nil && strings.Contains(inp.Text, listen_for) {
// Found the text
r := strings.Split(inp.Text, listen_for)
if len(r) > 0 {
p_str := r[1]
p_str = strings.Replace(p_str, ",", "", -1)
p_str = strings.Replace(p_str, "\n", "", -1)
i.MCUser.Home = p_str
c.model.updateMCUser(i.MCUser)
mm.Tell(i.MCUser.Name, "Set your home to "+p_str, "blue")
return true
}
}
return false
})
mm.Output("tp " + i.MCUser.Name + " ~ ~ ~")
return true
}
return false
})
// Add !home listener
AddListener(func(i *Message) bool {
if i.MCUser != nil && i.Text == "!home\n" {
if i.MCUser.Home != "" {
mm.Output("tp " + i.MCUser.Name + " " + i.MCUser.Home)
} else {
mm.Tell(i.MCUser.Name, "I don't know where your home is. Set it to your current position by typing '!set home'", "red")
}
}
return false
})
}
for _, option := range o {
opt_name, _ := option.GetString("name")
opt_enabled, _ := option.GetBoolean("enabled")
if opt_name == "visit" {
c.FeatureTPVisit = opt_enabled
if opt_enabled {
fmt.Println("> Activating 'visit' listeners")
// Add !set porch listener
AddListener(func(i *Message) bool {
if i.MCUser.Name != "" && i.Text == "!set porch\n" {
AddTempListener(func(inp *Message) bool {
listen_for := "Teleported " + i.MCUser.Name + " to "
if inp.MCUser.Name == "" && strings.Contains(inp.Text, listen_for) {
// Found the text
r := strings.Split(inp.Text, listen_for)
if len(r) > 0 {
p_str := r[1]
p_str = strings.Replace(p_str, ",", "", -1)
// TODO: Set Porch in DB
mm.Tell(i.MCUser.Name, "Set your porch to "+p_str, "blue")
return true
}
}
return false
})
mm.Output("tp " + i.MCUser.Name + " ~ ~ ~")
return true
}
return false
})
// Add !visit listener
AddListener(func(i *Message) bool {
if i.MCUser.Name != "" && strings.HasPrefix(i.Text, "!visit ") {
// Find the user we're trying to visit
r := strings.Split(strings.Replace(i.Text, "\n", "", -1), "!visit ")
if len(r) > 0 {
username := r[1]
// TODO: Get Porch from DB
porch_str, found := "", false
if found {
mm.Output("tp " + i.MCUser.Name + " " + porch_str)
} else {
mm.Tell(i.MCUser.Name, "I don't know where "+username+"'s porch is. They can set it to their current position by typing '!set porch'", "red")
}
}
}
return false
})
}
} else if opt_name == "teleport" {
c.FeatureTP = opt_enabled
if opt_enabled {
fmt.Println("> Activating 'teleport' listener")
// Add !tp listener
AddListener(func(i *Message) bool {
if i.MCUser.Name != "" && strings.HasPrefix(i.Text, "!tp ") {
tp_name := strings.Split(i.Text, " ")[1]
// TODO: Get Teleport Point from DB
tp_str, found := "", false
if found {
mm.Output("tp " + i.MCUser.Name + " " + tp_str)
} else {
mm.Tell(i.MCUser.Name, "I don't know where "+tp_name+".", "red")
}
}
return false
})
// Add !tpset listener
AddListener(func(i *Message) bool {
if i.MCUser.IsOp && strings.HasPrefix(i.Text, "!tpset ") {
tp_name := strings.Split(i.Text, " ")[1]
// Save user's current position as tp_name point
AddTempListener(func(inp *Message) bool {
listen_for := "Teleported " + i.MCUser.Name + " to "
if inp.MCUser.Name == "" && strings.Contains(inp.Text, listen_for) {
// Found the text
r := strings.Split(inp.Text, listen_for)
if len(r) > 0 {
p_str := r[1]
p_str = strings.Replace(p_str, ",", "", -1)
// TODO: Set the Teleport Point in the DB
mm.Tell(i.MCUser.Name, "Added TP Point "+tp_name+" at "+p_str, "blue")
return true
}
}
return false
})
mm.Output("tp " + i.MCUser.Name + " ~ ~ ~")
return true
}
return false
})
}
} else if opt_name == "daynight" {
c.FeatureDayNight = opt_enabled
if opt_enabled {
fmt.Println("> Activating 'time' listeners")
// Add !time day listener
AddListener(func(i *Message) bool {
if i.MCUser.Name != "" && i.Text == "!time day\n" {
// TODO: Start vote
mm.Output("time set day")
mm.Tell("@a", "Day Time time initiated by "+i.MCUser.Name, "yellow")
return true
}
return false
})
// Add !time night listener
AddListener(func(i *Message) bool {
if i.MCUser.Name != "" && i.Text == "!time night\n" {
// TODO: Start vote
mm.Output("time set night")
mm.Tell("@a", "Night Time time initiated by "+i.MCUser.Name, "blue")
return true
}
return false
})
}
}
// Add login listener
AddListener(func(i *Message) bool {
if i.MCUser == nil && strings.Contains(i.Text, " logged in with entity id ") {
// TODO: User Logged in Function
// Find the user that just logged in
r := strings.Split(i.Text, "]: ")
find := ""
if len(r) > 0 {
find = r[1]
r := strings.Split(find, "[/")
if len(r) > 0 {
find = r[0]
// find should be the user name now
var u *MCUser
var err error
if u, err = c.model.getMCUser(find); err != nil {
// user doesn't exist, create it
u = new(MCUser)
u.Name = find
}
u.LoginTime = time.Now()
c.model.updateMCUser(u)
return true
}
}
}
return false
})
// Add logout listener
AddListener(func(i *Message) bool {
if i.MCUser == nil && strings.Contains(i.Text, " lost connection: ") {
// Find the user that just logged out
r := strings.Split(i.Text, "]: ")
find := ""
if len(r) > 0 {
find = r[1]
r := strings.Split(find, " lost connection: ")
if len(r) > 0 {
find = r[0]
// find should be the user name now
var u *MCUser
var err error
if u, err = c.model.getMCUser(find); err != nil {
// user doesn't exist, create it
u = new(MCUser)
u.Name = find
}
u.LogoutTime = time.Now()
c.model.updateMCUser(u)
return true
}
}
}
return false
})
// Add !help listener
AddListener(func(i *Message) bool {
if i.MCUser != nil && i.Text == "!help\n" {
mm.Tell(i.MCUser.Name, "-=( mcman Manager Help )=-", "blue")
numFeatures := 0
if c.FeatureTPHome == true {
numFeatures++
mm.Tell(i.MCUser.Name, "!set home -- Set your 'home' to your current position.", "white")
mm.Tell(i.MCUser.Name, "!home -- Request a teleport to your 'home' position.", "white")
}
if c.FeatureTPVisit == true {
numFeatures++
mm.Tell(i.MCUser.Name, "!set porch -- Set your 'porch' to your current position.", "white")
mm.Tell(i.MCUser.Name, "!visit <username> -- Request a teleport to <username>'s 'porch' position.", "white")
}
if c.FeatureDayNight == true {
numFeatures++
mm.Tell(i.MCUser.Name, "!time day -- Ask the server to time the time to 'day'.", "white")
mm.Tell(i.MCUser.Name, "!time night -- Ask the server to time the time to 'night'.", "white")
}
if numFeatures == 0 {
mm.Tell(i.MCUser.Name, "mcman currently has no user features loaded.", "white")
}
mm.Tell(i.MCUser.Name, "-=========================-", "blue")
return true
}
return false
})
}
if allUsers, err := c.model.getAllMCUsers(); err != nil {
fmt.Printf("> Error loading users: " + err.Error())
} else {
fmt.Printf("> Loaded %d Users\n", len(allUsers))
}
}
}
func DoStopServer() {
mu.Lock()
message_manager.Output("stop")
// Mark all Online users as Logged Out
if ou, err := c.model.getOnlineMCUsers(); err == nil {
for i := range ou {
ou[i].LogoutTime = time.Now()
c.model.updateMCUser(&ou[i])
}
}
WriteConfig()
StopServer = true
mu.Unlock()
}
func WriteConfig() {
c.model.mcSaveFeature("tphome", c.FeatureTPHome)
c.model.mcSaveFeature("tpvisit", c.FeatureTPVisit)
c.model.mcSaveFeature("tp", c.FeatureTP)
c.model.mcSaveFeature("daynight", c.FeatureDayNight)
// TODO: Make mcman aware of the world
// Generate the JSON string for the config file
d := "{\"options\":["
// Output options array
d = d + "{\"name\":\"home\",\"enabled\":"
if c.FeatureTPHome {
d = d + "true"
} else {
d = d + "false"
}
d = d + "},{\"name\":\"visit\",\"enabled\":"
if c.FeatureTPVisit {
d = d + "true"
} else {
d = d + "false"
}
d = d + "},{\"name\":\"teleport\",\"enabled\":"
if c.FeatureTP {
d = d + "true"
} else {
d = d + "false"
}
d = d + "},{\"name\":\"daynight\",\"enabled\":"
if c.FeatureDayNight {
d = d + "true"
} else {
d = d + "false"
}
d = d + "}]}"
do := []byte(d)
ioutil.WriteFile(c.dir+"/mcman.config", do, 0664)
}