NFI where i was up to, store state.

This commit is contained in:
Alex Bramley 2011-07-17 13:14:25 +01:00
parent 762ba1e599
commit 557b178798
4 changed files with 80 additions and 58 deletions

View File

@ -1,27 +1,18 @@
port 6667 {
"Ports": [
port 7009 { {
class = server "Port": 6667
},
{
"Port": 6697,
"Class": "client",
"SSL": true,
"Zip": true
},
{
"Port": 7011,
"Class": "server",
"SSL": true
} }
port 6697 { ssl = true } ]
port 7011 {
ssl = true
class = server
bind_ip = 87.237.63.85
} }
oper fluffle {
password = foobar
hostmask = *camelid@*
link = true
}
oper bb101 {
password = dongs
hostmask = *bb10@*.enta.net
hostmask = *bb@*.dongs.com
renick = true
}

View File

@ -6,8 +6,20 @@ import (
) )
func main() { func main() {
cfg := config.LoadConfig("server.cfg") cfg := config.NewConfig()
for e, v := range(cfg.Errors) { cfg.Ports[6667] = config.DefaultPort()
fmt.Println(e, v) cfg.Ports[6667].Port = 6667
// cfg.Ports[6697] = &config.cPort{Port: 6697, SSL: true}
fmt.Println(cfg.String())
/*
cfg, err := config.ConfigFromFile("server.cfg")
if err != nil {
fmt.Println(err)
return
}
*/
for i, p := range cfg.Ports {
fmt.Printf("port %d\n", i)
fmt.Println(p.String())
} }
} }

View File

@ -7,10 +7,5 @@ include $(GOROOT)/src/Make.inc
TARG=irc/server/config TARG=irc/server/config
GOFILES=\ GOFILES=\
config.go\ config.go\
parser.go\
include $(GOROOT)/src/Make.pkg include $(GOROOT)/src/Make.pkg
parser.go: parser.rl
ragel-svn -Z -G2 -o parser.go parser.rl

View File

@ -1,9 +1,12 @@
package config package config
import ( import (
"bufio"
"bytes"
"os" "os"
"fmt" "fmt"
"net" "net"
"json"
"strings" "strings"
) )
@ -29,17 +32,22 @@ type Config struct {
Errors []os.Error Errors []os.Error
} }
func ConfigFromFile(filename string) (*Config, os.Error) {
func LoadConfig(filename string) *Config {
conf := &Config{fn: filename} conf := &Config{fn: filename}
conf.initialise() conf.initialise()
if fh, err := os.Open(conf.fn, os.O_RDONLY, 0644); err == nil { if fh, err := os.Open(conf.fn, os.O_RDONLY, 0644); err == nil {
conf.Parse(fh) // Cheat and use bufio.ReadBytes to slurp the file.
rdr := bufio.NewReader(fh)
data, err := rdr.ReadBytes('\000')
fh.Close() fh.Close()
} else { if err != os.EOF {
conf.Errors = append(conf.Errors, err) return nil, err
} }
return conf if err = json.Unmarshal(data, conf); err != nil {
return nil, err
}
}
return conf, nil
} }
func (conf *Config) initialise() { func (conf *Config) initialise() {
@ -52,13 +60,36 @@ func (conf *Config) initialise() {
conf.Errors = make([]os.Error, 0) conf.Errors = make([]os.Error, 0)
} }
func (conf *Config) Rehash() { func NewConfig() *Config {
neu := LoadConfig(conf.fn) conf := new(Config)
if len(neu.Errors) > 0 { conf.initialise()
conf.Errors = neu.Errors return conf
} else {
conf = neu
} }
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
}
buf.WriteString("{\"Ports\":")
buf.Write(b)
buf.WriteByte('}')
return buf.Bytes(), nil
} }
/* Port configuration */ /* Port configuration */
@ -71,7 +102,7 @@ type cPort struct {
SSL, Zip bool SSL, Zip bool
} }
func defaultPort() *cPort { func DefaultPort() *cPort {
return &cPort{ return &cPort{
BindIP: nil, Class: "client", BindIP: nil, Class: "client",
SSL: false, Zip: false, SSL: false, Zip: false,
@ -79,18 +110,11 @@ func defaultPort() *cPort {
} }
func (p *cPort) String() string { func (p *cPort) String() string {
str := []string{fmt.Sprintf("port %d {", p.Port)} str, err := json.MarshalIndent(p, "" , " ")
if p.BindIP != nil { if err == nil {
str = append(str, return string(str)
fmt.Sprintf("\tbind_ip = %s", p.BindIP.String()))
} }
str = append(str, return fmt.Sprintf("marshal error: %s", err)
fmt.Sprintf("\tclass = %s", p.Class),
fmt.Sprintf("\tssl = %t", p.SSL),
fmt.Sprintf("\tzip = %t", p.Zip),
"}",
)
return strings.Join(str, "\n")
} }
/* Oper configuration */ /* Oper configuration */