ircd/irc/config.go

88 lines
1.5 KiB
Go
Raw Normal View History

2014-02-09 15:53:42 +00:00
package irc
import (
2014-02-24 06:21:39 +00:00
"encoding/base64"
2014-02-09 15:53:42 +00:00
"encoding/json"
2014-02-24 06:21:39 +00:00
"log"
2014-02-09 15:53:42 +00:00
"os"
2014-02-24 06:21:39 +00:00
"path/filepath"
2014-02-09 15:53:42 +00:00
)
2014-02-24 06:21:39 +00:00
func decodePassword(password string) []byte {
if password == "" {
return nil
}
bytes, err := base64.StdEncoding.DecodeString(password)
if err != nil {
log.Fatal(err)
}
return bytes
}
2014-02-09 15:53:42 +00:00
type Config struct {
2014-02-12 00:35:32 +00:00
Debug map[string]bool
2014-02-10 03:41:00 +00:00
Listeners []ListenerConfig
2014-02-12 00:35:32 +00:00
MOTD string
Name string
2014-02-09 18:07:40 +00:00
Operators []OperatorConfig
2014-02-12 00:35:32 +00:00
Password string
2014-02-09 18:07:40 +00:00
}
2014-02-24 06:21:39 +00:00
func (conf *Config) PasswordBytes() []byte {
return decodePassword(conf.Password)
}
func (conf *Config) OperatorsMap() map[string][]byte {
operators := make(map[string][]byte)
for _, opConf := range conf.Operators {
operators[opConf.Name] = opConf.PasswordBytes()
}
return operators
}
2014-02-09 18:07:40 +00:00
type OperatorConfig struct {
2014-02-09 15:53:42 +00:00
Name string
Password string
}
2014-02-24 06:21:39 +00:00
func (conf *OperatorConfig) PasswordBytes() []byte {
return decodePassword(conf.Password)
}
2014-02-10 03:41:00 +00:00
type ListenerConfig struct {
2014-02-10 21:52:28 +00:00
Net string
2014-02-10 03:41:00 +00:00
Address string
Key string
Certificate string
}
func (config *ListenerConfig) IsTLS() bool {
return (config.Key != "") && (config.Certificate != "")
}
2014-02-24 06:21:39 +00:00
func LoadConfig(filename string) (config *Config, err error) {
2014-02-09 15:53:42 +00:00
config = &Config{}
2014-02-24 06:21:39 +00:00
file, err := os.Open(filename)
2014-02-09 15:53:42 +00:00
if err != nil {
return
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
2014-02-10 21:52:28 +00:00
if err != nil {
return
}
2014-02-24 06:21:39 +00:00
dir := filepath.Dir(filename)
config.MOTD = filepath.Join(dir, config.MOTD)
2014-02-10 21:52:28 +00:00
for _, lconf := range config.Listeners {
if lconf.Net == "" {
lconf.Net = "tcp"
}
}
2014-02-09 15:53:42 +00:00
return
}