/
config.go
131 lines (99 loc) · 2.22 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
//Copyright 2010 Cory Kolbeck <ckolbeck@gmail.com>.
//So long as this notice remains in place, you are welcome
//to do whatever you like to or with this code. This code is
//provided 'As-Is' with no warrenty expressed or implied.
//If you like it, and we happen to meet, buy me a beer sometime
package main
import (
"cbeck/ircbot"
"json"
"io/ioutil"
"os"
)
type botConfig struct {
BotName string
AttnChar byte
IdentPW string
Server string
Port int
Channels []string
Version string
SourceLoc string
Help map[string]string
Trusted map[string]bool
TitleWhitelist map[string]bool
Ignores map[string]bool
}
func parseConfig(filename string) (*botConfig, os.Error){
raw, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
c := &botConfig{}
err = json.Unmarshal(raw, c)
if err != nil {
return nil, err
}
return c, nil
}
func initParseConfig() {
var err os.Error
config, err = parseConfig(configPath)
if err != nil {
errors.Printf("Failed to parse config file: %s\n --exiting--", err.String())
os.Exit(1)
}
if config.BotName == "" || config.Server == "" {
errors.Printf("Malformed config file or missing item,\n%#v\n exiting", config)
os.Exit(1)
}
helpList = "I understand the following commands: "
for k := range config.Help {
helpList += k + ", "
}
helpList += "source"
}
func saveConfig(bc *botConfig) os.Error {
_, err := json.MarshalIndent(bc, "", " ")
if err != nil {
return err
}
//mv old to path.bak
//attempt to write
//Fail: attempt to undo mv
//success:
return nil
}
func reparseConfig(bot *ircbot.Bot) bool {
c, err := parseConfig(configPath)
if err != nil {
return false
}
err = bot.Send(c.Server, &ircbot.Message{
Command : "NICK",
Args : []string{c.BotName},
})
if err != nil {
return false
}
if c.IdentPW != "" {
bot.Send(c.Server, &ircbot.Message{
Command : "PRIVMSG",
Args : []string{"NickServ"},
Trailing : "identify " + c.IdentPW,
})
}
for _, channel := range c.Channels {
bot.Send(c.Server, &ircbot.Message{
Command : "JOIN",
Args : []string{channel},
})
}
helpList = "I understand the following commands: "
for k := range c.Help {
helpList += k + ", "
}
helpList += "source"
config = c
return true
}