From c72ee3e4959ed7f41e68e0f5833281bb3d1c0eee Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 19 Aug 2019 18:30:59 -0500 Subject: [PATCH] initial --- Makefile | 6 ++++ src/main.go | 12 +++++++ src/sircd/sircd.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 Makefile create mode 100644 src/main.go create mode 100644 src/sircd/sircd.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fb27478 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +GOPATH := $(shell pwd) + +default: run + +run: src/*.go + go run src/*.go diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..adeef39 --- /dev/null +++ b/src/main.go @@ -0,0 +1,12 @@ +package main + +import "sircd" +import "time" + +func main() { + s := sircd.NewSircd() + s.Start() + for s.Running { + time.Sleep(100 * time.Millisecond) + } +} diff --git a/src/sircd/sircd.go b/src/sircd/sircd.go new file mode 100644 index 0000000..f844d5f --- /dev/null +++ b/src/sircd/sircd.go @@ -0,0 +1,79 @@ +package sircd + +import ( + "fmt" + "net" + "os" +) + +type sircd struct { + Running bool + netName string + ircPort uint16 + httpPort uint16 + ircListener net.Listener + ircClients []*clientIRCConnection +} + +type clientIRCConnection struct { + conn net.Conn +} + +const ( + CONN_HOST = "localhost" + CONN_PORT = "6667" + CONN_TYPE = "tcp" +) + +func NewSircd() *sircd { + s := new(sircd) + s.Running = true + return s +} + +func (s *sircd) startServer() { + var err error + s.ircListener, err = net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT) + if err != nil { + fmt.Println("Error listening:", err.Error()) + os.Exit(1) + } + defer s.ircListener.Close() + fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT) + fmt.Printf("%T\n",s.ircListener) + + for { + // Listen for an incoming connection. + conn, err := s.ircListener.Accept() + if err != nil { + fmt.Println("Error accepting: ", err.Error()) + os.Exit(1) + } + // Handle connections in a new goroutine. + go handleRequest(conn) + } + + +} + +func (s *sircd) Start() { + go func() { + s.startServer() + }() +} + +// Handles incoming requests. +func handleRequest(conn net.Conn) { + // Make a buffer to hold incoming data. + buf := make([]byte, 1024) + // Read the incoming connection into the buffer. + byteLen, err := conn.Read(buf) + if err != nil { + fmt.Println("Error reading:", err.Error()) + } + // Send a response back to person contacting us. + conn.Write([]byte("Message received.")) + fmt.Println("Received message (%i bytes): %s", byteLen, buf) + // Close the connection when you're done with it. + conn.Close() +}