commit 8e31fde0abe644456f0a30b8d7fe0a0799312397 Author: Simon Ser Date: Tue Feb 4 10:46:22 2020 +0100 Add basic IRC listener diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee64510 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/jounce diff --git a/cmd/jounce/main.go b/cmd/jounce/main.go new file mode 100644 index 0000000..2b4f6a2 --- /dev/null +++ b/cmd/jounce/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "log" + "net" + + "git.sr.ht/~emersion/jounce" +) + +func main() { + addr := ":6667" + + ln, err := net.Listen("tcp", addr) + if err != nil { + log.Fatalf("failed to start listener: %v", err) + } + + log.Printf("Server listening on %v", addr) + log.Fatal(jounce.Serve(ln)) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ffe4e12 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.sr.ht/~emersion/jounce + +go 1.13 + +require gopkg.in/irc.v3 v3.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9d45687 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +gopkg.in/irc.v3 v3.1.0 h1:AeDaEhQ/78gHfpbj/3mSi8FfiNIsFiVrWEgLzOwHWnU= +gopkg.in/irc.v3 v3.1.0/go.mod h1:qE0DWv0j8Z8wCbFhA9783JBO0bufi3rttcV1Sjin8io= diff --git a/server.go b/server.go new file mode 100644 index 0000000..400fe22 --- /dev/null +++ b/server.go @@ -0,0 +1,38 @@ +package jounce + +import ( + "fmt" + "log" + "net" + + "gopkg.in/irc.v3" +) + +func handleConn(conn net.Conn) error { + defer conn.Close() + + ircConn := irc.NewConn(conn) + for { + msg, err := ircConn.ReadMessage() + if err != nil { + return err + } + + log.Println(msg) + } +} + +func Serve(ln net.Listener) error { + for { + conn, err := ln.Accept() + if err != nil { + return fmt.Errorf("failed to accept connection: %v", err) + } + + go func() { + if err := handleConn(conn); err != nil { + log.Printf("error handling connection: %v", err) + } + }() + } +}