3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-23 03:02:48 +01:00
ergo/irc/config.go

88 lines
1.5 KiB
Go
Raw Normal View History

2014-02-09 16:53:42 +01:00
package irc
import (
2014-02-24 07:21:39 +01:00
"encoding/base64"
2014-02-09 16:53:42 +01:00
"encoding/json"
2014-02-24 07:21:39 +01:00
"log"
2014-02-09 16:53:42 +01:00
"os"
2014-02-24 07:21:39 +01:00
"path/filepath"
2014-02-09 16:53:42 +01:00
)
2014-02-24 07:21:39 +01: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 16:53:42 +01:00
type Config struct {
2014-02-12 01:35:32 +01:00
Debug map[string]bool
2014-02-10 04:41:00 +01:00
Listeners []ListenerConfig
2014-02-12 01:35:32 +01:00
MOTD string
Name string
2014-02-09 19:07:40 +01:00
Operators []OperatorConfig
2014-02-12 01:35:32 +01:00
Password string
2014-02-09 19:07:40 +01:00
}
2014-02-24 07:21:39 +01: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 19:07:40 +01:00
type OperatorConfig struct {
2014-02-09 16:53:42 +01:00
Name string
Password string
}
2014-02-24 07:21:39 +01:00
func (conf *OperatorConfig) PasswordBytes() []byte {
return decodePassword(conf.Password)
}
2014-02-10 04:41:00 +01:00
type ListenerConfig struct {
2014-02-10 22:52:28 +01:00
Net string
2014-02-10 04:41:00 +01:00
Address string
Key string
Certificate string
}
func (config *ListenerConfig) IsTLS() bool {
return (config.Key != "") && (config.Certificate != "")
}
2014-02-24 07:21:39 +01:00
func LoadConfig(filename string) (config *Config, err error) {
2014-02-09 16:53:42 +01:00
config = &Config{}
2014-02-24 07:21:39 +01:00
file, err := os.Open(filename)
2014-02-09 16:53:42 +01:00
if err != nil {
return
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
2014-02-10 22:52:28 +01:00
if err != nil {
return
}
2014-02-24 07:21:39 +01:00
dir := filepath.Dir(filename)
config.MOTD = filepath.Join(dir, config.MOTD)
2014-02-10 22:52:28 +01:00
for _, lconf := range config.Listeners {
if lconf.Net == "" {
lconf.Net = "tcp"
}
}
2014-02-09 16:53:42 +01:00
return
}