grim/gobnc

2ecd482b625d
Parents bd772f3d3c7f
Children d166328a1bc8
Created a config object and the start of the server
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/client/client.go Wed Aug 08 23:02:04 2018 -0500
@@ -0,0 +1,3 @@
+package client
+
+type Client struct{}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/irc/irc.go Wed Aug 08 23:02:04 2018 -0500
@@ -0,0 +1,13 @@
+package irc
+
+import (
+ "regexp"
+)
+
+var (
+ regexNick = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9-[\]\^{}]*`)
+)
+
+func NickValid(nick string) bool {
+ return regexNick.MatchString(nick)
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/irc/irc_test.go Wed Aug 08 23:02:04 2018 -0500
@@ -0,0 +1,29 @@
+package irc
+
+import (
+ "testing"
+
+ . "github.com/onsi/gomega"
+)
+
+func TestNickValid(t *testing.T) {
+ g := NewGomegaWithT(t)
+ tests := map[string]bool{
+ "": false,
+ "a": true,
+ "A": true,
+ "*": false,
+ "aa": true,
+ "Aa": true,
+ "aA": true,
+ "1a": false,
+ "1A": false,
+ "a1": true,
+ "A1": true,
+ "A^": true,
+ }
+
+ for nick, exp := range tests {
+ g.Expect(NickValid(nick)).To(Equal(exp))
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/config.go Wed Aug 08 23:02:04 2018 -0500
@@ -0,0 +1,32 @@
+package server
+
+import (
+ "fmt"
+
+ "bitbucket.org/rw_grim/gobnc/irc"
+)
+
+// Config handles the configuration data for a server. It may be marshalled to
+// yaml.
+type Config struct {
+ ListenAddr string `yaml:"listen-addr"`
+ Port int `yaml:"port"`
+ Nick string `yaml:"nick"`
+ Password string `yaml:"password"`
+}
+
+func (c *Config) Valid() error {
+ if c.ListenAddr == "" {
+ c.ListenAddr = "127.0.0.1"
+ }
+
+ if c.Port <= 0 || c.Port > 65535 {
+ return fmt.Errorf("Invalid port %d: must be between 1 and 65535", c.Port)
+ }
+
+ if !irc.NickValid(c.Nick) {
+ return fmt.Errorf("Invalid nick '%s'", c.Nick)
+ }
+
+ return nil
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/config_test.go Wed Aug 08 23:02:04 2018 -0500
@@ -0,0 +1,33 @@
+package server
+
+import (
+ "testing"
+
+ . "github.com/onsi/gomega"
+)
+
+func TestConfigValidListenAddr(t *testing.T) {
+ g := NewGomegaWithT(t)
+
+ cfg := &Config{
+ Port: 6667,
+ Nick: "foo",
+ }
+ g.Expect(cfg.Valid()).To(BeNil())
+ g.Expect(cfg.ListenAddr).To(Equal("127.0.0.1"))
+}
+
+func TestConfigValidPort(t *testing.T) {
+ g := NewGomegaWithT(t)
+
+ var cfg *Config
+
+ cfg = &Config{Nick: "foo"}
+ g.Expect(cfg.Valid()).To(MatchError("Invalid port 0: must be between 1 and 65535"))
+
+ cfg = &Config{Port: -1, Nick: "foo"}
+ g.Expect(cfg.Valid()).To(MatchError("Invalid port -1: must be between 1 and 65535"))
+
+ cfg = &Config{Port: 65536, Nick: "foo"}
+ g.Expect(cfg.Valid()).To(MatchError("Invalid port 65536: must be between 1 and 65535"))
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/server/server.go Wed Aug 08 23:02:04 2018 -0500
@@ -0,0 +1,7 @@
+package server
+
+type Server struct{}
+
+func (s *Server) Listen() {
+
+}