Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/news/nntpd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ type Server struct {
// ErrLog, when non-nil, receives connection-level errors. Protocol verbs
// are never logged (a public server should not log every command).
ErrLog *log.Logger
group *nntp.Group
}

// NewServer builds a server bound to a backend.
Expand Down Expand Up @@ -371,6 +370,9 @@ func handleIHave(args []string, s *session, c *textproto.Conn) error {
return ErrSyntax
}
article, err := s.backend.GetArticle(nil, args[0])
if err != nil && err != ErrInvalidMessageID && err != ErrInvalidArticleNumber {
return err
}
if article != nil {
return ErrNotWanted
}
Expand Down
48 changes: 46 additions & 2 deletions internal/news/nntpd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
)

type whitespaceBackend struct {
group *nntp.Group
allowPost bool
group *nntp.Group
allowPost bool
articleErr error
postCalls int
}

func (b *whitespaceBackend) ListGroups(max int) ([]*nntp.Group, error) {
Expand All @@ -28,6 +30,9 @@ func (b *whitespaceBackend) GetGroup(name string) (*nntp.Group, error) {
}

func (b *whitespaceBackend) GetArticle(group *nntp.Group, id string) (*nntp.Article, error) {
if b.articleErr != nil {
return nil, b.articleErr
}
return nil, ErrInvalidArticleNumber
}

Expand All @@ -44,6 +49,7 @@ func (b *whitespaceBackend) Authenticate(user, pass string) (Backend, error) {
func (b *whitespaceBackend) AllowPost() bool { return b.allowPost }

func (b *whitespaceBackend) Post(article *nntp.Article) error {
b.postCalls++
return errors.New("posting disabled")
}

Expand Down Expand Up @@ -127,3 +133,41 @@ func TestIHaveWithoutMessageIDReturnsSyntaxError(t *testing.T) {
t.Fatal("server did not close after QUIT")
}
}

func TestIHaveStopsWhenDuplicateCheckFails(t *testing.T) {
backend := &whitespaceBackend{
group: &nntp.Group{Name: "pfs.general", Posting: nntp.PostingPermitted},
allowPost: true,
articleErr: errors.New("backend unavailable"),
}
server := NewServer(backend)
clientConn, serverConn := net.Pipe()
defer clientConn.Close()

done := make(chan struct{})
go func() {
server.Process(serverConn)
close(done)
}()

client := textproto.NewConn(clientConn)
defer client.Close()
if line, err := client.ReadLine(); err != nil || !strings.HasPrefix(line, "200 ") {
t.Fatalf("greeting = %q, %v", line, err)
}
if err := client.PrintfLine("IHAVE <new@example.test>"); err != nil {
t.Fatalf("send IHAVE: %v", err)
}
if line, err := client.ReadLine(); err == nil {
t.Fatalf("IHAVE duplicate-check failure returned %q; want connection close", line)
}
if backend.postCalls != 0 {
t.Fatalf("Post called %d times after duplicate-check failure; want 0", backend.postCalls)
}

select {
case <-done:
case <-time.After(time.Second):
t.Fatal("server did not close after duplicate-check failure")
}
}
1 change: 0 additions & 1 deletion internal/news/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var (
nSel = lipgloss.NewStyle().Foreground(lipgloss.Color("#0b1020")).Background(ui.Cyan)
nMeta = ui.Dim
nFrom = lipgloss.NewStyle().Foreground(ui.Green)
nErr = ui.Danger
)

// RunReader connects the member to the loopback NNTP server and drives the
Expand Down