goirc/server/config/config.go

216 lines
4.3 KiB
Go
Raw Permalink Normal View History

package config
import (
2011-07-17 12:14:25 +00:00
"bufio"
"bytes"
"os"
"fmt"
2010-11-28 16:39:25 +00:00
"net"
2011-07-17 12:14:25 +00:00
"json"
2010-11-28 16:39:25 +00:00
"strings"
)
type Config struct {
fn string
// Ports we listen on.
Ports map[int]*cPort
// People with teh p0wer.
Opers map[string]*cOper
// Servers we link to on the network.
Links map[string]*cLink
// Servers/nickmasks/IPs that are unwanted.
Bans []*cBan
// Server info (name, admins, etc.)
Info *cInfo
// Server settings
Settings *cSettings
// Parse errors
Errors []os.Error
}
2011-07-17 12:14:25 +00:00
func ConfigFromFile(filename string) (*Config, os.Error) {
conf := &Config{fn: filename}
conf.initialise()
if fh, err := os.Open(conf.fn, os.O_RDONLY, 0644); err == nil {
2011-07-17 12:14:25 +00:00
// Cheat and use bufio.ReadBytes to slurp the file.
rdr := bufio.NewReader(fh)
data, err := rdr.ReadBytes('\000')
fh.Close()
2011-07-17 12:14:25 +00:00
if err != os.EOF {
return nil, err
}
if err = json.Unmarshal(data, conf); err != nil {
return nil, err
}
}
2011-07-17 12:14:25 +00:00
return conf, nil
}
func (conf *Config) initialise() {
conf.Ports = make(map[int]*cPort)
conf.Opers = make(map[string]*cOper)
conf.Links = make(map[string]*cLink)
conf.Bans = make([]*cBan, 0)
conf.Info = &cInfo{}
conf.Settings = &cSettings{}
conf.Errors = make([]os.Error, 0)
}
2011-07-17 12:14:25 +00:00
func NewConfig() *Config {
conf := new(Config)
conf.initialise()
return conf
}
func (conf *Config) String() string {
str, err := json.MarshalIndent(conf, "" , " ")
if err == nil {
return string(str)
}
return fmt.Sprintf("marshal error: %s", err)
}
func (conf *Config) MarshalJSON() ([]byte, os.Error) {
buf := &bytes.Buffer{}
ports := make([]*cPort, len(conf.Ports))
i := 0
for _, p := range(conf.Ports) {
ports[i] = p
i++
}
b, err := json.Marshal(ports)
if err != nil {
return nil, err
}
2011-07-17 12:14:25 +00:00
buf.WriteString("{\"Ports\":")
buf.Write(b)
buf.WriteByte('}')
return buf.Bytes(), nil
}
2010-11-28 16:39:25 +00:00
/* Port configuration */
type cPort struct {
Port int
BindIP net.IP // bind to a specific IP for listen port
Class string // "server" or "client"
// Is port a tls.Listener? Does it support compression (no)?
SSL, Zip bool
}
2011-07-17 12:14:25 +00:00
func DefaultPort() *cPort {
2010-11-28 16:39:25 +00:00
return &cPort{
BindIP: nil, Class: "client",
SSL: false, Zip: false,
}
2010-11-28 16:39:25 +00:00
}
func (p *cPort) String() string {
2011-07-17 12:14:25 +00:00
str, err := json.MarshalIndent(p, "" , " ")
if err == nil {
return string(str)
}
2011-07-17 12:14:25 +00:00
return fmt.Sprintf("marshal error: %s", err)
}
2010-11-28 16:39:25 +00:00
/* Oper configuration */
type cOper struct {
Username, Password string
HostMask []string
// Permissions for oper
CanKill, CanBan, CanRenick, CanLink bool
}
2010-11-28 16:39:25 +00:00
func defaultOper() *cOper {
return &cOper{
HostMask: []string{},
CanKill: true, CanBan: true,
CanRenick: false, CanLink: false,
}
}
2010-11-28 16:39:25 +00:00
func (o *cOper) String() string {
str := []string{fmt.Sprintf("oper %s {", o.Username)}
str = append(str, fmt.Sprintf("\tpassword = %s", o.Password))
for _, h := range o.HostMask {
str = append(str, fmt.Sprintf("\thostmask = %s", h))
}
2010-11-28 16:39:25 +00:00
str = append(str,
fmt.Sprintf("\tkill = %t", o.CanKill),
fmt.Sprintf("\tban = %t", o.CanBan),
fmt.Sprintf("\trenick = %t", o.CanRenick),
fmt.Sprintf("\tlink = %t", o.CanLink),
"}",
)
return strings.Join(str, "\n")
}
2010-11-28 16:39:25 +00:00
/* Link configuration */
type cLink struct {
Server string // Server name for link
Address string // {ip,ip6,host}:port
ReceivePass string // Password when server connects to us
ConnectPass string // Password when we connect to server
// Do we use tls.Dial? or compression (no)? Do we auto-connect on start?
SSL, Zip, Auto bool
}
func defaultLink() *cLink {
return &cLink{
SSL: false, Zip: false, Auto: false,
}
}
2010-11-28 16:39:25 +00:00
/* Static ban configuration */
type cBan interface {
Match(string) bool
Reason() string
}
2010-11-28 16:39:25 +00:00
// G-Line etc;
type cBanNick struct {
NickMask string // nick!ident@host
Reason string
}
// Z-Line
type cBanIP struct {
Address string // ip (or hostname), plus optional CIDR netmask
Reason string
ip string // parsed into these
cidr int
}
// CTCP version ban
type cBanVersion struct {
VersionRegex string // regex to match against version reply
Reason string
}
// Ban server from linking to network
type cBanServer struct {
ServerMask string // matched against name of linked server
Reason string
}
2010-11-28 16:39:25 +00:00
/* IRCd settings */
type cSettings struct {
SSLKey, SSLCert, SSLCACert string
MaxChans, MaxConnsPerIP int
LogFile string
}
2010-11-28 16:39:25 +00:00
/* IRCd information */
type cInfo struct {
Name, Network, Info, MOTDFile string
Admins []string
Numeric int
}