Implement actual MODE READER, add syntax error message

This commit is contained in:
ChronosX88 2022-01-18 20:43:05 +03:00
parent 0e6cd28925
commit e85fb43d50
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
3 changed files with 26 additions and 3 deletions

View File

@ -33,4 +33,5 @@ const (
MessageUnknownCommand = "500 Unknown command" MessageUnknownCommand = "500 Unknown command"
MessageErrorHappened = "403 Failed to process command:" MessageErrorHappened = "403 Failed to process command:"
MessageListOfNewsgroupsFollows = "215 list of newsgroups follows" MessageListOfNewsgroupsFollows = "215 list of newsgroups follows"
MessageSyntaxError = "501 Syntax Error"
) )

View File

@ -22,12 +22,13 @@ func NewHandler(db *sqlx.DB) *Handler {
protocol.CommandDate: h.handleDate, protocol.CommandDate: h.handleDate,
protocol.CommandQuit: h.handleQuit, protocol.CommandQuit: h.handleQuit,
protocol.CommandList: h.handleList, protocol.CommandList: h.handleList,
protocol.CommandMode: h.handleModeReader,
} }
return h return h
} }
func (h *Handler) handleCapabilities(s *Session, arguments []string) error { func (h *Handler) handleCapabilities(s *Session, arguments []string) error {
return s.tconn.PrintfLine(Capabilities.String()) return s.tconn.PrintfLine(s.capabilities.String())
} }
func (h *Handler) handleDate(s *Session, arguments []string) error { func (h *Handler) handleDate(s *Session, arguments []string) error {
@ -81,7 +82,7 @@ func (h *Handler) handleList(s *Session, arguments []string) error {
} }
default: default:
{ {
return s.tconn.PrintfLine(protocol.MessageUnknownCommand) return s.tconn.PrintfLine(protocol.MessageSyntaxError)
} }
} }
@ -90,6 +91,18 @@ func (h *Handler) handleList(s *Session, arguments []string) error {
return s.tconn.PrintfLine(sb.String()) return s.tconn.PrintfLine(sb.String())
} }
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)
(&s.capabilities).Add(protocol.Capability{Type: protocol.ReaderCapability})
s.mode = SessionModeReader
return s.tconn.PrintfLine(protocol.MessageReaderModePostingProhibited) // TODO vary on auth status
}
func (h *Handler) Handle(s *Session, message string) error { func (h *Handler) Handle(s *Session, message string) error {
splittedMessage := strings.Split(message, " ") splittedMessage := strings.Split(message, " ")
for i, v := range splittedMessage { for i, v := range splittedMessage {

View File

@ -9,6 +9,13 @@ import (
"net/textproto" "net/textproto"
) )
type SessionMode int
const (
SessionModeTransit = iota
SessionModeReader
)
type Session struct { type Session struct {
ctx context.Context ctx context.Context
capabilities protocol.Capabilities capabilities protocol.Capabilities
@ -17,6 +24,7 @@ type Session struct {
id string id string
closed chan<- bool closed chan<- bool
h *Handler h *Handler
mode SessionMode
} }
func NewSession(ctx context.Context, conn net.Conn, caps protocol.Capabilities, id string, closed chan<- bool, handler *Handler) (*Session, error) { func NewSession(ctx context.Context, conn net.Conn, caps protocol.Capabilities, id string, closed chan<- bool, handler *Handler) (*Session, error) {
@ -37,6 +45,7 @@ func NewSession(ctx context.Context, conn net.Conn, caps protocol.Capabilities,
id: id, id: id,
closed: closed, closed: closed,
h: handler, h: handler,
mode: SessionModeTransit,
} }
go s.loop() go s.loop()
@ -49,7 +58,7 @@ func (s *Session) loop() {
close(s.closed) close(s.closed)
}() }()
err := s.tconn.PrintfLine(protocol.MessageReaderModePostingProhibited) // by default access mode is read-only err := s.tconn.PrintfLine(protocol.MessageNNTPServiceReadyPostingProhibited) // by default access mode is read-only
if err != nil { if err != nil {
s.conn.Close() s.conn.Close()
return return