mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
A new user's "Where are you calling from?" starts filled in with where their caller ID (area code and exchange) or address (GeoIP) says they are, on the BBS and the web forum's sign-up; geoip_prefill = no turns it off. New users are asked whether the sysop may see where they call from (no unless they say yes), and can change it in their profile or the API (share_location). For those who share it the place, how it was found and the date are kept at each login, shown to sysops on the user's pages, in bbs config's user editor and the API's geo; stopping forgets it. geoip_users = no stops asking and keeping. A modem call by VoIP without a caller ID is placed by the address the call came from, if the modem server sends it (DMODEM_PEER / MODEM_PEER). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRSVXNfvCFNooYkqeMp
84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/D-Modem/bbs/internal/bbs"
|
|
)
|
|
|
|
// The session header, as modemmenu (server.go) and app_dmodem write it:
|
|
// KEY=value lines, then an empty line, then the caller's bytes.
|
|
|
|
const (
|
|
headerMaxLines = 64
|
|
headerMaxBytes = 8192
|
|
)
|
|
|
|
// ReadHeader reads a session header from c, a byte at a time so that
|
|
// nothing of the caller's is read with it.
|
|
func ReadHeader(c net.Conn, timeout time.Duration) (map[string]string, error) {
|
|
c.SetReadDeadline(time.Now().Add(timeout))
|
|
defer c.SetReadDeadline(time.Time{})
|
|
h := map[string]string{}
|
|
var line []byte
|
|
b := make([]byte, 1)
|
|
for total := 0; ; total++ {
|
|
if total >= headerMaxBytes {
|
|
return nil, errors.New("header: too long")
|
|
}
|
|
if _, err := c.Read(b); err != nil {
|
|
return nil, fmt.Errorf("header: %w", err)
|
|
}
|
|
if b[0] != '\n' {
|
|
line = append(line, b[0])
|
|
continue
|
|
}
|
|
s := strings.TrimSuffix(string(line), "\r")
|
|
line = line[:0]
|
|
if s == "" {
|
|
return h, nil
|
|
}
|
|
if len(h) >= headerMaxLines {
|
|
return nil, errors.New("header: too many lines")
|
|
}
|
|
k, v, ok := strings.Cut(s, "=")
|
|
if !ok {
|
|
return nil, fmt.Errorf("header: no = in %q", bbs.Clean(s, 40))
|
|
}
|
|
h[strings.ToUpper(strings.TrimSpace(k))] = v
|
|
}
|
|
}
|
|
|
|
// pick is the first of the keys the header has.
|
|
func pick(h map[string]string, keys ...string) string {
|
|
for _, k := range keys {
|
|
if v, ok := h[k]; ok {
|
|
return bbs.Clean(v, 64)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// applyHeader puts what a header says of the call into c; with trust its
|
|
// MODEM_USER is the caller, logged in already.
|
|
func applyHeader(c *bbs.Caller, h map[string]string, trust bool) {
|
|
c.Rate = pick(h, "DMODEM_RATE", "MODEM_RATE")
|
|
c.CallerID = pick(h, "DMODEM_CALLERID", "MODEM_CALLERID")
|
|
c.Dialed = pick(h, "DMODEM_DIALED", "MODEM_DIALED")
|
|
c.Channel = pick(h, "DMODEM_CHANNEL", "MODEM_CHANNEL")
|
|
c.Peer = pick(h, "DMODEM_PEER", "MODEM_PEER")
|
|
if c.Rate != "" || c.Channel != "" {
|
|
c.Via = "modem"
|
|
}
|
|
if c.CallerID != "" {
|
|
c.Remote = c.CallerID
|
|
}
|
|
if trust {
|
|
c.TrustedUser = strings.TrimSpace(pick(h, "MODEM_USER"))
|
|
}
|
|
}
|