2022-01-18 17:26:37 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-19 19:51:08 +00:00
|
|
|
"github.com/ChronosX88/yans/internal/backend"
|
2022-01-18 17:26:37 +00:00
|
|
|
"github.com/ChronosX88/yans/internal/protocol"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
handlers map[string]func(s *Session, arguments []string) error
|
2022-01-19 19:51:08 +00:00
|
|
|
backend backend.StorageBackend
|
2022-01-18 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 19:51:08 +00:00
|
|
|
func NewHandler(b backend.StorageBackend) *Handler {
|
2022-01-18 17:26:37 +00:00
|
|
|
h := &Handler{}
|
2022-01-19 19:51:08 +00:00
|
|
|
h.backend = b
|
2022-01-18 17:26:37 +00:00
|
|
|
h.handlers = map[string]func(s *Session, arguments []string) error{
|
|
|
|
protocol.CommandCapabilities: h.handleCapabilities,
|
|
|
|
protocol.CommandDate: h.handleDate,
|
|
|
|
protocol.CommandQuit: h.handleQuit,
|
|
|
|
protocol.CommandList: h.handleList,
|
2022-01-18 17:43:05 +00:00
|
|
|
protocol.CommandMode: h.handleModeReader,
|
2022-01-18 17:26:37 +00:00
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleCapabilities(s *Session, arguments []string) error {
|
2022-01-18 17:43:05 +00:00
|
|
|
return s.tconn.PrintfLine(s.capabilities.String())
|
2022-01-18 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleDate(s *Session, arguments []string) error {
|
|
|
|
return s.tconn.PrintfLine("111 %s", time.Now().UTC().Format("20060102150405"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleQuit(s *Session, arguments []string) error {
|
|
|
|
s.tconn.PrintfLine(protocol.MessageNNTPServiceExitsNormally)
|
|
|
|
s.conn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleList(s *Session, arguments []string) error {
|
|
|
|
sb := strings.Builder{}
|
|
|
|
|
|
|
|
listType := ""
|
|
|
|
if len(arguments) != 0 {
|
|
|
|
listType = arguments[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
switch listType {
|
|
|
|
case "":
|
|
|
|
fallthrough
|
|
|
|
case "ACTIVE":
|
|
|
|
{
|
2022-01-19 19:51:08 +00:00
|
|
|
groups, err := h.backend.ListGroups()
|
2022-01-18 17:26:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sb.Write([]byte(protocol.MessageListOfNewsgroupsFollows + protocol.CRLF))
|
|
|
|
for _, v := range groups {
|
|
|
|
// TODO set high/low mark and posting status to actual values
|
|
|
|
sb.Write([]byte(fmt.Sprintf("%s 0 0 n"+protocol.CRLF, v.GroupName)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "NEWSGROUPS":
|
|
|
|
{
|
2022-01-19 19:51:08 +00:00
|
|
|
groups, err := h.backend.ListGroups()
|
2022-01-18 17:26:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, v := range groups {
|
|
|
|
desc := ""
|
|
|
|
if v.Description == nil {
|
|
|
|
desc = "No description"
|
|
|
|
} else {
|
|
|
|
desc = *v.Description
|
|
|
|
}
|
|
|
|
sb.Write([]byte(fmt.Sprintf("%s %s"+protocol.CRLF, v.GroupName, desc)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2022-01-18 17:43:05 +00:00
|
|
|
return s.tconn.PrintfLine(protocol.MessageSyntaxError)
|
2022-01-18 17:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.Write([]byte(protocol.MultilineEnding))
|
|
|
|
|
|
|
|
return s.tconn.PrintfLine(sb.String())
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:43:05 +00:00
|
|
|
func (h *Handler) handleModeReader(s *Session, arguments []string) error {
|
|
|
|
if len(arguments) == 0 || arguments[0] != "READER" {
|
|
|
|
return s.tconn.PrintfLine(protocol.MessageSyntaxError)
|
|
|
|
}
|
|
|
|
|
|
|
|
(&s.capabilities).Remove(protocol.ModeReaderCapability)
|
2022-01-18 17:47:32 +00:00
|
|
|
(&s.capabilities).Remove(protocol.ListCapability)
|
2022-01-18 17:43:05 +00:00
|
|
|
(&s.capabilities).Add(protocol.Capability{Type: protocol.ReaderCapability})
|
2022-01-18 17:47:32 +00:00
|
|
|
(&s.capabilities).Add(protocol.Capability{Type: protocol.ListCapability, Params: "ACTIVE NEWSGROUPS"})
|
2022-01-18 17:43:05 +00:00
|
|
|
s.mode = SessionModeReader
|
|
|
|
|
|
|
|
return s.tconn.PrintfLine(protocol.MessageReaderModePostingProhibited) // TODO vary on auth status
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:26:37 +00:00
|
|
|
func (h *Handler) Handle(s *Session, message string) error {
|
|
|
|
splittedMessage := strings.Split(message, " ")
|
|
|
|
for i, v := range splittedMessage {
|
|
|
|
splittedMessage[i] = strings.TrimSpace(v)
|
|
|
|
}
|
|
|
|
cmdName := splittedMessage[0]
|
|
|
|
handler, ok := h.handlers[cmdName]
|
|
|
|
if !ok {
|
|
|
|
return s.tconn.PrintfLine(protocol.MessageUnknownCommand)
|
|
|
|
}
|
|
|
|
return handler(s, splittedMessage[1:])
|
|
|
|
}
|