-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
104 lines (91 loc) · 2.49 KB
/
conf.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
package stick
import (
"json"
"os"
"strconv"
)
type ChanActConf struct {
Match string "match"
Action string "action"
Parms string "parms"
}
type ChanConf struct {
Name string "name"
Actions map[string]ChanActConf "actions"
Users map[string]UserConf "users"
}
func (c ChanConf) String() string {
return string(c.Name)
}
type NetConf struct {
Nick string "nick"
Realname string "realname"
Channels []ChanConf "channels"
}
type NetsConf map[string] *NetConf
type PlugConf []string
type ActConf []string
type UserConf struct {
Role string "role"
Sha1pass string "sha1pass"
Email string "e-mail"
}
type StickConfJson struct {
Networks NetsConf "networks"
Plugins PlugConf "plugins"
Actions ActConf "actions"
Users map[string]UserConf "users"
}
type StickConf struct {
Conf StickConfJson
conffile string
}
func InitConf (conffile string) (*StickConf, os.Error) {
var c StickConf
c.conffile = conffile
err := c.RdConf()
return &c, err
}
func (c *StickConf) RdConf() os.Error {
f, err := os.Open(c.conffile, os.O_RDONLY, 0)
if err != nil {
return os.NewError("An error occurred while parsing the config file: " + err.String())
}
defer f.Close()
d := json.NewDecoder(f)
err = d.Decode(&c.Conf)
if err != nil {
return os.NewError("An error occurred while parsing the config file: " + err.String())
}
return nil
}
func (c *StickConf) WrConf() os.Error {
f, err := os.Open(c.conffile, os.O_WRONLY, 600)
if err != nil {
return os.NewError("An error occurred while parsing the config file: " + err.String())
}
defer f.Close()
jsonbytes, err := json.MarshalIndent(c.Conf, "", " ")
if err != nil {
return os.NewError("An error occurred while marshaling struct: " + err.String())
}
_, err = f.Write(jsonbytes)
if err != nil {
return os.NewError("An error occurred while parsing the config file: " + err.String())
}
return nil
}
func (c *StickConf) AddActionChan(ch string, action ChanActConf, net string) os.Error {
var i int
for i, _ = range c.Conf.Networks[net].Channels {
if c.Conf.Networks[net].Channels[i].Name == ch {
break
}
}
c.Conf.Networks[net].Channels[i].Actions[strconv.Itoa(len(c.Conf.Networks[net].Channels[i].Actions)+1)] = action
err := c.WrConf()
if err != nil {
return os.NewError("Couldn't write config to file: " + err.String())
}
return nil
}