Files
bbs/internal/textui/transfer.go
T
Claude f57f4d32ff File transfers on the text BBS: XMODEM, YMODEM, ZMODEM, Kermit
internal/xfer has the protocols, both ways:

- XMODEM: CRC or checksum as the receiver asks, 128 or 1K blocks
- YMODEM batch: names and sizes in block 0, sizes kept exactly
- ZMODEM: hex and binary headers, CRC-16/32, ZDLE escaping; sent in
  32 KB segments ended by ZCRCW, so a ZRPOS costs at most a segment
  and nothing is read while streaming; refused files are skipped
- Kermit: short packets, control quoting, 8th-bit prefixing and repeat
  counts as agreed, block checks 1, 2 and 3

Each has timeouts and retry limits, and takes no more than the upload
behind it allows.  Tests run them against lrzsz (sx/rx, sb/rb, sz/rz)
and C-Kermit (over a raw socket), both ways, and ZMODEM over a line
that damages bytes.

In the file areas: D downloads a file, U uploads into an area (names a
terminal program sends lose their paths and odd characters; XMODEM
asks for one), then asks for descriptions.  The transfer uses the
terminal's buffered reader, so nothing typed ahead is lost, and the
line is let settle afterwards.  Telnet in binary mode now keeps a NUL
after a CR (it is data there); a telnet client that refused binary
mode is warned that X/Y/ZMODEM will likely fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
2026-10-10 18:02:13 +00:00

211 lines
5.3 KiB
Go

package textui
import (
"errors"
"fmt"
"strings"
"time"
"unicode"
"github.com/Neo-Desktop/bbs-sec/internal/bbs"
"github.com/Neo-Desktop/bbs-sec/internal/files"
"github.com/Neo-Desktop/bbs-sec/internal/store"
"github.com/Neo-Desktop/bbs-sec/internal/xfer"
)
// chooseProtocol asks for a transfer protocol; nil if the caller gave up.
func (s *session) chooseProtocol() (*xfer.Protocol, error) {
t := s.t
t.Println("")
if !t.Clean8 {
t.Println(t.C(Red, "Your telnet client is not in binary mode: X/Y/ZMODEM will likely fail; Kermit may work."))
}
for _, p := range xfer.Protocols {
t.Println(" " + t.C(Yellow, "["+p.Key+"]") + " " + p.Name)
}
t.Print("Protocol, [Q]uit: ")
for {
k, err := t.Key()
if err != nil {
return nil, err
}
if k == 'Q' || k == '\r' {
t.Println("")
return nil, nil
}
if p := xfer.Lookup(string(k)); p != nil {
t.Println(p.Name)
return p, nil
}
}
}
func startHint(p *xfer.Protocol, upload bool) string {
dir := "download"
if upload {
dir = "upload"
}
return fmt.Sprintf("Start your terminal program's %s %s now; Ctrl-X five times cancels.", p.Name, dir)
}
// download sends a file.
func (s *session) download(a *store.FileArea, f *store.File) error {
t := s.t
p, err := s.chooseProtocol()
if err != nil || p == nil {
return err
}
r, err := s.b.Files.OpenFile(f)
if err != nil {
return err
}
defer r.Close()
t.Println(t.C(Cyan, startHint(p, false)))
port, err := t.Port()
if err != nil {
return err
}
s.n.Doing("Downloading " + f.Name)
start := time.Now()
err = p.Send(port, []xfer.Outgoing{{Name: f.Name, Size: f.Size, ModTime: f.Created, Data: r}})
t.Settle(time.Second)
t.Println("")
if err != nil {
s.b.Log.Printf("node %d: %s: %s download of %q failed: %v", s.n.Num, s.user.Name, p.Name, f.Name, err)
t.Println(t.C(Red, "The transfer failed: "+err.Error()+"."))
return nil
}
s.b.Store.CountDownload(f.ID)
s.b.Log.Printf("node %d: %s downloaded %q by %s (%d bytes, %v)", s.n.Num, s.user.Name, f.Name,
p.Name, f.Size, time.Since(start).Round(time.Second))
t.Println(t.C(Green, "Sent "+f.Name+"."))
return nil
}
// incoming is an upload, for a transfer protocol.
type incoming struct {
up *files.Upload
done func(*store.File)
}
func (in *incoming) Write(p []byte) (int, error) { return in.up.Write(p) }
func (in *incoming) Abort() { in.up.Abort() }
func (in *incoming) Commit() error {
f, err := in.up.Commit()
if err == nil {
in.done(f)
}
return err
}
// safeName makes a name a terminal program sent into one the file areas
// take: its last path element, characters they do not take as _, no
// leading dots or dashes, at most 64 long. "" if nothing is left.
func safeName(name string) string {
if i := strings.LastIndexAny(name, `/\:`); i >= 0 {
name = name[i+1:]
}
name = strings.Map(func(r rune) rune {
switch {
case r < 0x80 && (unicode.IsLetter(r) || unicode.IsDigit(r)):
return r
case strings.ContainsRune(" ._+()[]!,#&'@~=-", r):
return r
}
return '_'
}, name)
for strings.Contains(name, "..") {
name = strings.ReplaceAll(name, "..", ".")
}
name = strings.TrimLeft(name, ".- _")
if len(name) > 64 {
name = name[:64]
}
name = strings.TrimRight(name, ". ")
if files.ValidName(name) != "" {
return ""
}
return name
}
// upload receives files into an area.
func (s *session) upload(a *store.FileArea) error {
t := s.t
if !files.CanUpload(s.user, a) {
t.Println(t.C(Red, "Only the sysop uploads here."))
return nil
}
p, err := s.chooseProtocol()
if err != nil || p == nil {
return err
}
given := ""
if !p.Batch {
// XMODEM sends no name
for {
given, err = t.Input("Name of the file: ", 64)
if err != nil || given == "" {
return err
}
if msg := files.ValidName(given); msg != "" {
t.Println(t.C(Red, msg))
continue
}
break
}
}
var got []*store.File
var refused []string
sink := func(name string, size int64) (xfer.Incoming, error) {
if given != "" {
name = given
} else if clean := safeName(name); clean != "" {
name = clean
} else {
refused = append(refused, bbs.Clean(name, 64))
return nil, errors.New("a name the file areas do not take")
}
if size > s.b.Files.MaxSize {
refused = append(refused, name)
return nil, files.ErrTooLarge
}
up, err := s.b.Files.Upload(s.user, a, name, "")
if err != nil {
refused = append(refused, name+" ("+err.Error()+")")
return nil, err
}
return &incoming{up: up, done: func(f *store.File) { got = append(got, f) }}, nil
}
t.Println(t.C(Cyan, startHint(p, true)))
port, err := t.Port()
if err != nil {
return err
}
s.n.Doing("Uploading to " + a.Name)
err = p.Receive(port, sink)
t.Settle(time.Second)
t.Println("")
for _, f := range got {
s.b.Log.Printf("node %d: %s uploaded %q to %s by %s (%d bytes, sha256 %s)", s.n.Num,
s.user.Name, f.Name, a.Name, p.Name, f.Size, f.SHA256)
}
if err != nil {
s.b.Log.Printf("node %d: %s: %s upload failed: %v", s.n.Num, s.user.Name, p.Name, err)
t.Println(t.C(Red, "The transfer failed: "+err.Error()+"."))
}
for _, r := range refused {
t.Println(t.C(Red, "Not taken: "+r))
}
for _, f := range got {
t.Println(t.C(Green, fmt.Sprintf("Received %s, %s.", f.Name, files.Size(f.Size))))
desc, err := t.Input("Describe it: ", 60)
if err != nil {
return err
}
if desc = bbs.Clean(desc, 60); desc != "" {
s.b.Store.SetFileDescription(f.ID, desc)
}
}
return nil
}