Files
bbs/internal/imageboard/ids.go
T
Claude 7399e0fa36 Point repository references at the D-Modem organisation
The repositories moved from Neo-Desktop to the D-Modem GitHub
organisation: D-Modem-RE is now D-Modem/D-Modem, pico-serial is
D-Modem/hardware, modemmenu (PBX-Modem) is D-Modem/menu, bbs-sec is
D-Modem/bbs and go3270-sec is D-Modem/go3270.  Update links, module
paths, submodule URLs and source-location strings to match.  Copyright
notices still name Neo-Desktop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRUwD2JJ5d6fgHmpVTw
2026-10-29 09:02:40 +00:00

61 lines
1.7 KiB
Go

package imageboard
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"hash/fnv"
"net"
"strconv"
"strings"
"github.com/D-Modem/bbs/internal/geoip"
)
// Poster IDs and country flags, on the boards that have them.
// posterID is a poster's ID in a thread: eight characters of a keyed hash
// of their address, the board and the thread, as 4chan's: the same all
// through a thread, another in the next, and nothing of the address to be
// had from it.
func (s *Server) posterID(addr, dir string, thread int64) string {
m := hmac.New(sha256.New, s.key)
m.Write([]byte("id\x00" + addr + "\x00" + dir + "\x00" + strconv.FormatInt(thread, 10)))
return base64.StdEncoding.EncodeToString(m.Sum(nil)[:6])
}
// idColors are how many colours IDs are shown in (idc-0 ... in chan.css).
const idColors = 24
// idClass is an ID's colour's class: the same ID, the same colour.
func idClass(id string) string {
h := fnv.New32a()
h.Write([]byte(id))
return "idc-" + strconv.Itoa(int(h.Sum32()%idColors))
}
// country is an address's country (code, name), if the boards have a
// database and it says. The BBS's, if it is the same file.
func (s *Server) country(addr string) (string, string) {
path := s.conf().GeoIP
if path == "" {
return "", ""
}
var g *geoip.File
if s.B.Conf().GeoIP == path {
g = s.B.Geo() // the same file: read once
} else {
s.geoMu.Lock()
if s.geo == nil || s.geo.Path != path {
s.geo = &geoip.File{Path: path, Log: func(err error) { s.B.Log.Printf("chan: the country database: %v", err) }}
}
g = s.geo
s.geoMu.Unlock()
}
code, name, _ := g.Country(net.ParseIP(addr))
return code, name
}
// flagOf is a code's flag, for the templates.
func flagOf(code string) string { return geoip.Flag(strings.ToUpper(code)) }