From 557b17879805a42c06ac0b2c6a9c52a2115880bb Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Sun, 17 Jul 2011 13:14:25 +0100 Subject: [PATCH] NFI where i was up to, store state. --- server.cfg | 43 ++++++++++-------------- server.go | 18 +++++++++-- server/config/Makefile | 5 --- server/config/config.go | 72 +++++++++++++++++++++++++++-------------- 4 files changed, 80 insertions(+), 58 deletions(-) diff --git a/server.cfg b/server.cfg index facc633..d62e0bf 100644 --- a/server.cfg +++ b/server.cfg @@ -1,27 +1,18 @@ -port 6667 - -port 7009 { - class = server +{ + "Ports": [ + { + "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 -} - diff --git a/server.go b/server.go index cb3e38b..19bddbe 100644 --- a/server.go +++ b/server.go @@ -6,8 +6,20 @@ import ( ) func main() { - cfg := config.LoadConfig("server.cfg") - for e, v := range(cfg.Errors) { - fmt.Println(e, v) + cfg := config.NewConfig() + cfg.Ports[6667] = config.DefaultPort() + 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()) } } diff --git a/server/config/Makefile b/server/config/Makefile index 52b0f22..fbe5886 100644 --- a/server/config/Makefile +++ b/server/config/Makefile @@ -7,10 +7,5 @@ include $(GOROOT)/src/Make.inc TARG=irc/server/config GOFILES=\ config.go\ - parser.go\ include $(GOROOT)/src/Make.pkg - -parser.go: parser.rl - ragel-svn -Z -G2 -o parser.go parser.rl - diff --git a/server/config/config.go b/server/config/config.go index 0d44082..3f88d04 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -1,9 +1,12 @@ package config import ( + "bufio" + "bytes" "os" "fmt" "net" + "json" "strings" ) @@ -29,17 +32,22 @@ type Config struct { Errors []os.Error } - -func LoadConfig(filename string) *Config { +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 { - conf.Parse(fh) + // Cheat and use bufio.ReadBytes to slurp the file. + rdr := bufio.NewReader(fh) + data, err := rdr.ReadBytes('\000') fh.Close() - } else { - conf.Errors = append(conf.Errors, err) + if err != os.EOF { + return nil, err + } + if err = json.Unmarshal(data, conf); err != nil { + return nil, err + } } - return conf + return conf, nil } func (conf *Config) initialise() { @@ -52,13 +60,36 @@ func (conf *Config) initialise() { conf.Errors = make([]os.Error, 0) } -func (conf *Config) Rehash() { - neu := LoadConfig(conf.fn) - if len(neu.Errors) > 0 { - conf.Errors = neu.Errors - } else { - conf = neu +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 + } + buf.WriteString("{\"Ports\":") + buf.Write(b) + buf.WriteByte('}') + return buf.Bytes(), nil } /* Port configuration */ @@ -71,7 +102,7 @@ type cPort struct { SSL, Zip bool } -func defaultPort() *cPort { +func DefaultPort() *cPort { return &cPort{ BindIP: nil, Class: "client", SSL: false, Zip: false, @@ -79,18 +110,11 @@ func defaultPort() *cPort { } func (p *cPort) String() string { - str := []string{fmt.Sprintf("port %d {", p.Port)} - if p.BindIP != nil { - str = append(str, - fmt.Sprintf("\tbind_ip = %s", p.BindIP.String())) + str, err := json.MarshalIndent(p, "" , " ") + if err == nil { + return string(str) } - str = append(str, - fmt.Sprintf("\tclass = %s", p.Class), - fmt.Sprintf("\tssl = %t", p.SSL), - fmt.Sprintf("\tzip = %t", p.Zip), - "}", - ) - return strings.Join(str, "\n") + return fmt.Sprintf("marshal error: %s", err) } /* Oper configuration */