mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
Gopher: no files from areas with a download requirement
Gopher has no login, but it listed and sent every file area's files, those with a download requirement too. It now offers only areas anyone may download from (the requirement a caller not logged in meets). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
This commit is contained in:
2 files changed
+39
-5
No files matched your search
@@ -292,12 +292,20 @@ func (s *Server) fileAreas(c io.Writer) {
|
||||
fas, _ := s.B.Store.FileAreas()
|
||||
items := []item{{'i', "File areas of " + s.B.Conf().Name, ""}, {'i', "", ""}}
|
||||
for _, fa := range fas {
|
||||
items = append(items, item{'1', fmt.Sprintf("%s (%d) %s", fa.Name, fa.Files, fa.Description),
|
||||
fmt.Sprintf("/files/%d", fa.ID)})
|
||||
if openFiles(fa) {
|
||||
items = append(items, item{'1', fmt.Sprintf("%s (%d) %s", fa.Name, fa.Files, fa.Description),
|
||||
fmt.Sprintf("/files/%d", fa.ID)})
|
||||
}
|
||||
}
|
||||
s.menu(c, items)
|
||||
}
|
||||
|
||||
// openFiles reports whether a file area is gopher's: gopher has no login,
|
||||
// so only an area anyone may download from (no download requirement).
|
||||
func openFiles(fa *store.FileArea) bool {
|
||||
return bbs.MustRequire(fa.DownloadReq).Allows(nil)
|
||||
}
|
||||
|
||||
// kind is a file's gopher type, by its name.
|
||||
func kind(name string) byte {
|
||||
n := strings.ToLower(name)
|
||||
@@ -317,7 +325,7 @@ func (s *Server) files(c io.Writer, id int64) {
|
||||
return
|
||||
}
|
||||
fa, err := s.B.Store.FileArea(id)
|
||||
if err != nil {
|
||||
if err != nil || !openFiles(fa) {
|
||||
notFound(s, c)
|
||||
return
|
||||
}
|
||||
@@ -333,8 +341,8 @@ func (s *Server) files(c io.Writer, id int64) {
|
||||
s.menu(c, items)
|
||||
}
|
||||
|
||||
// download sends a file's bytes as they are, counting a download if all of
|
||||
// it went.
|
||||
// download sends a file's bytes as they are, if its area is gopher's,
|
||||
// counting a download if all of it went.
|
||||
func (s *Server) download(c io.Writer, id int64) {
|
||||
if s.B.Files == nil || id < 1 {
|
||||
notFound(s, c)
|
||||
@@ -345,6 +353,10 @@ func (s *Server) download(c io.Writer, id int64) {
|
||||
notFound(s, c)
|
||||
return
|
||||
}
|
||||
if fa, err := s.B.Store.FileArea(f.AreaID); err != nil || !openFiles(fa) {
|
||||
notFound(s, c)
|
||||
return
|
||||
}
|
||||
r, err := s.B.Files.OpenFile(f)
|
||||
if err != nil {
|
||||
notFound(s, c)
|
||||
|
||||
@@ -3,6 +3,7 @@ package gopher
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
@@ -159,3 +160,24 @@ func TestCurl(t *testing.T) {
|
||||
t.Errorf("curl binary: %v %q", err, out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGopherRestrictedFiles: a file area with a download requirement is not
|
||||
// gopher's (gopher has no login): not listed, its files neither listed nor
|
||||
// sent nor counted.
|
||||
func TestGopherRestrictedFiles(t *testing.T) {
|
||||
s := setup(t, "general", true)
|
||||
if err := s.b.Store.SetFileAreaRequire(s.file.AreaID, "20", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := s.get(t, "/files"); strings.Contains(got, "Uploads") {
|
||||
t.Errorf("a restricted area listed:\n%s", got)
|
||||
}
|
||||
for _, sel := range []string{fmt.Sprintf("/files/%d", s.file.AreaID), fmt.Sprintf("/file/%d", s.file.ID)} {
|
||||
if got := s.get(t, sel); !strings.HasPrefix(got, "3Not here.") {
|
||||
t.Errorf("%s: %q", sel, got)
|
||||
}
|
||||
}
|
||||
if f, _ := s.b.Store.FileByID(s.file.ID); f.Downloads != 0 {
|
||||
t.Errorf("downloads %d", f.Downloads)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user