From 6d4581a6d0b6aa2391ad084b2cc9d7e88a3e3681 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 4 Feb 2020 11:25:53 +0100 Subject: [PATCH] Send "unknown command" replies --- cmd/jounce/main.go | 4 +++- server.go | 53 +++++++++++++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/cmd/jounce/main.go b/cmd/jounce/main.go index 2b4f6a2..5a0b61a 100644 --- a/cmd/jounce/main.go +++ b/cmd/jounce/main.go @@ -15,6 +15,8 @@ func main() { log.Fatalf("failed to start listener: %v", err) } + s := jounce.Server{} + log.Printf("Server listening on %v", addr) - log.Fatal(jounce.Serve(ln)) + log.Fatal(s.Serve(ln)) } diff --git a/server.go b/server.go index 400fe22..b93be96 100644 --- a/server.go +++ b/server.go @@ -2,35 +2,58 @@ package jounce import ( "fmt" + "io" "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) - } +type conn struct { + net net.Conn + irc *irc.Conn } -func Serve(ln net.Listener) error { +type Server struct{} + +func (s *Server) handleConn(netConn net.Conn) error { + defer netConn.Close() + + conn := conn{netConn, irc.NewConn(netConn)} for { - conn, err := ln.Accept() + msg, err := conn.irc.ReadMessage() + if err == io.EOF { + break + } else if err != nil { + return err + } + log.Println(msg) + + switch msg.Command { + default: + err = conn.irc.WriteMessage(&irc.Message{ + Command: irc.ERR_UNKNOWNCOMMAND, + Params: []string{ + "*", + msg.Command, + "Unknown command", + }, + }) + } + } + + return netConn.Close() +} + +func (s *Server) Serve(ln net.Listener) error { + for { + c, err := ln.Accept() if err != nil { return fmt.Errorf("failed to accept connection: %v", err) } go func() { - if err := handleConn(conn); err != nil { + if err := s.handleConn(c); err != nil { log.Printf("error handling connection: %v", err) } }()