diff --git a/.gitignore b/.gitignore index 859b7a20..42a7a3a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ pkg bin ergonomadic.db +ergonomadic.json diff --git a/ergonomadic.go b/ergonomadic.go index 457a8f53..88cebbe7 100644 --- a/ergonomadic.go +++ b/ergonomadic.go @@ -1,17 +1,27 @@ package main import ( - "flag" "github.com/jlatt/ergonomadic/irc" + "log" + "sync" ) func main() { - name := flag.String("name", "ergonomadic", "A name for the server") - listen := flag.String("listen", ":6667", "interface to listen on") - flag.BoolVar(&irc.DEBUG_NET, "dnet", false, "debug net") - flag.BoolVar(&irc.DEBUG_CLIENT, "dclient", false, "debug client") - flag.BoolVar(&irc.DEBUG_CHANNEL, "dchannel", false, "debug channel") - flag.BoolVar(&irc.DEBUG_SERVER, "dserver", false, "debug server") - flag.Parse() - irc.NewServer(*name).Listen(*listen) + config, err := irc.LoadConfig() + if err != nil { + log.Fatal(err) + return + } + + irc.DEBUG_NET = config.Debug["net"] + irc.DEBUG_CLIENT = config.Debug["client"] + irc.DEBUG_CHANNEL = config.Debug["channel"] + irc.DEBUG_SERVER = config.Debug["server"] + + irc.NewServer(config) + + // never finishes + wg := sync.WaitGroup{} + wg.Add(1) + wg.Wait() } diff --git a/irc/config.go b/irc/config.go new file mode 100644 index 00000000..5df7c267 --- /dev/null +++ b/irc/config.go @@ -0,0 +1,27 @@ +package irc + +import ( + "encoding/json" + "os" +) + +type Config struct { + Name string + Listen string + Password string + Debug map[string]bool +} + +func LoadConfig() (config *Config, err error) { + config = &Config{} + + file, err := os.Open("ergonomadic.json") + if err != nil { + return + } + defer file.Close() + + decoder := json.NewDecoder(file) + err = decoder.Decode(config) + return +} diff --git a/irc/server.go b/irc/server.go index 5ea4fb51..009400ac 100644 --- a/irc/server.go +++ b/irc/server.go @@ -19,16 +19,17 @@ type Server struct { clients ClientNameMap } -func NewServer(name string) *Server { +func NewServer(config *Config) *Server { commands := make(chan Command) server := &Server{ ctime: time.Now(), - name: name, + name: config.Name, commands: commands, clients: make(ClientNameMap), channels: make(ChannelNameMap), } go server.receiveCommands(commands) + go server.listen(config.Listen) return server } @@ -42,7 +43,7 @@ func (server *Server) receiveCommands(commands <-chan Command) { } } -func (s *Server) Listen(addr string) { +func (s *Server) listen(addr string) { listener, err := net.Listen("tcp", addr) if err != nil { log.Fatal("Server.Listen: ", err)