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)
}
s := jounce.Server{}
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 (
"fmt"
"io"
"log"
"net"
"gopkg.in/irc.v3"
)
func handleConn(conn net.Conn) error {
defer conn.Close()
type conn struct {
net net.Conn
irc *irc.Conn
}
ircConn := irc.NewConn(conn)
type Server struct{}
func (s *Server) handleConn(netConn net.Conn) error {
defer netConn.Close()
conn := conn{netConn, irc.NewConn(netConn)}
for {
msg, err := ircConn.ReadMessage()
if err != nil {
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",
},
})
}
}
func Serve(ln net.Listener) error {
return netConn.Close()
}
func (s *Server) Serve(ln net.Listener) error {
for {
conn, err := ln.Accept()
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)
}
}()