soju/server.go

132 lines
2.4 KiB
Go
Raw Normal View History

2020-02-04 09:46:22 +00:00
package jounce
import (
"fmt"
2020-02-04 10:25:53 +00:00
"io"
2020-02-04 09:46:22 +00:00
"log"
"net"
"gopkg.in/irc.v3"
)
2020-02-04 11:19:18 +00:00
type ircError struct {
Message *irc.Message
}
func newUnknownCommandError(cmd string) ircError {
return ircError{&irc.Message{
Command: irc.ERR_UNKNOWNCOMMAND,
Params: []string{
"*",
cmd,
"Unknown command",
},
}}
}
func newNeedMoreParamsError(cmd string) ircError {
return ircError{&irc.Message{
Command: irc.ERR_NEEDMOREPARAMS,
Params: []string{
"*",
cmd,
"Not enough parameters",
},
}}
}
func (err ircError) Error() string {
return err.Message.String()
}
2020-02-04 10:25:53 +00:00
type conn struct {
2020-02-04 11:19:18 +00:00
net net.Conn
irc *irc.Conn
registered bool
nick string
username string
realname string
}
func (c *conn) handleMessageUnregistered(msg *irc.Message) error {
switch msg.Command {
case "NICK":
if len(msg.Params) != 1 {
return newNeedMoreParamsError(msg.Command)
}
c.nick = msg.Params[0]
case "USER":
if len(msg.Params) != 4 {
return newNeedMoreParamsError(msg.Command)
}
c.username = "~" + msg.Params[0]
c.realname = msg.Params[3]
c.registered = true
default:
return newUnknownCommandError(msg.Command)
}
return nil
}
func (c *conn) handleMessage(msg *irc.Message) error {
switch msg.Command {
case "NICK", "USER":
return ircError{&irc.Message{
Command: irc.ERR_ALREADYREGISTERED,
Params: []string{
c.nick,
"You may not reregister",
},
}}
default:
return newUnknownCommandError(msg.Command)
}
2020-02-04 10:25:53 +00:00
}
type Server struct{}
2020-02-04 09:46:22 +00:00
2020-02-04 10:25:53 +00:00
func (s *Server) handleConn(netConn net.Conn) error {
defer netConn.Close()
2020-02-04 11:19:18 +00:00
c := conn{net: netConn, irc: irc.NewConn(netConn)}
2020-02-04 09:46:22 +00:00
for {
2020-02-04 11:19:18 +00:00
msg, err := c.irc.ReadMessage()
2020-02-04 10:25:53 +00:00
if err == io.EOF {
break
} else if err != nil {
2020-02-04 11:19:18 +00:00
return fmt.Errorf("failed to read IRC command: %v", err)
2020-02-04 09:46:22 +00:00
}
log.Println(msg)
2020-02-04 10:25:53 +00:00
2020-02-04 11:19:18 +00:00
if c.registered {
err = c.handleMessage(msg)
} else {
err = c.handleMessageUnregistered(msg)
}
if ircErr, ok := err.(ircError); ok {
if err := c.irc.WriteMessage(ircErr.Message); err != nil {
return fmt.Errorf("failed to write IRC reply: %v", err)
}
} else if err != nil {
return fmt.Errorf("failed to handle IRC command %q: %v", msg.Command, err)
2020-02-04 10:25:53 +00:00
}
2020-02-04 09:46:22 +00:00
}
2020-02-04 10:25:53 +00:00
return netConn.Close()
2020-02-04 09:46:22 +00:00
}
2020-02-04 10:25:53 +00:00
func (s *Server) Serve(ln net.Listener) error {
2020-02-04 09:46:22 +00:00
for {
2020-02-04 10:25:53 +00:00
c, err := ln.Accept()
2020-02-04 09:46:22 +00:00
if err != nil {
return fmt.Errorf("failed to accept connection: %v", err)
}
go func() {
2020-02-04 10:25:53 +00:00
if err := s.handleConn(c); err != nil {
2020-02-04 09:46:22 +00:00
log.Printf("error handling connection: %v", err)
}
}()
}
}