Send "unknown command" replies
This commit is contained in:
parent
fb0825a100
commit
6d4581a6d0
@ -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))
|
||||
}
|
||||
|
41
server.go
41
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()
|
||||
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)
|
||||
}
|
||||
}()
|
||||
|
Loading…
Reference in New Issue
Block a user