Send "unknown command" replies

This commit is contained in:
Simon Ser 2020-02-04 11:25:53 +01:00
parent fb0825a100
commit 6d4581a6d0
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 41 additions and 16 deletions

View File

@ -15,6 +15,8 @@ func main() {
log.Fatalf("failed to start listener: %v", err) log.Fatalf("failed to start listener: %v", err)
} }
s := jounce.Server{}
log.Printf("Server listening on %v", addr) log.Printf("Server listening on %v", addr)
log.Fatal(jounce.Serve(ln)) log.Fatal(s.Serve(ln))
} }

View File

@ -2,35 +2,58 @@ package jounce
import ( import (
"fmt" "fmt"
"io"
"log" "log"
"net" "net"
"gopkg.in/irc.v3" "gopkg.in/irc.v3"
) )
func handleConn(conn net.Conn) error { type conn struct {
defer conn.Close() net net.Conn
irc *irc.Conn
ircConn := irc.NewConn(conn)
for {
msg, err := ircConn.ReadMessage()
if err != nil {
return err
}
log.Println(msg)
}
} }
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 { 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 { if err != nil {
return fmt.Errorf("failed to accept connection: %v", err) return fmt.Errorf("failed to accept connection: %v", err)
} }
go func() { go func() {
if err := handleConn(conn); err != nil { if err := s.handleConn(c); err != nil {
log.Printf("error handling connection: %v", err) log.Printf("error handling connection: %v", err)
} }
}() }()