mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
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
362 lines
9.6 KiB
Go
362 lines
9.6 KiB
Go
package forum
|
|
|
|
import (
|
|
"fmt"
|
|
"hash/fnv"
|
|
"html"
|
|
"html/template"
|
|
"net/url"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
|
|
"github.com/D-Modem/bbs/internal/geoip"
|
|
"github.com/D-Modem/bbs/internal/store"
|
|
)
|
|
|
|
var funcs = template.FuncMap{
|
|
"body": renderBody,
|
|
"when": when,
|
|
"date": func(t time.Time) string { return t.Format("02 January 2006") },
|
|
"longdate": longDate,
|
|
"odd": func(n int) bool { return n%2 == 1 },
|
|
"ago": ago,
|
|
"vbdate": func(t time.Time) string { return t.Format("01-02-2006") },
|
|
"vbtime": func(t time.Time) string { return t.Format("03:04 PM") },
|
|
"smfdate": func(t time.Time) string { return t.Format("January 02, 2006, 03:04:05 PM") },
|
|
"ymd": func(t time.Time) string { return t.Format("2006-01-02") },
|
|
"yabbdate": func(t time.Time) string { return t.Format("01/02/06 at 15:04:05") },
|
|
"yabb2date": yabb2Date,
|
|
"mod5": func(n int) int { return n % 5 },
|
|
"shortdate": func(t time.Time) string { return t.Format("Jan. 02, 2006") },
|
|
"clock": func(t time.Time) string { return t.Format("03:04 PM") },
|
|
"stamp": func(t time.Time) string { return t.Format("2006-01-02 15:04") },
|
|
"monthyear": func(t time.Time) string { return t.Format("Jan 2006") },
|
|
"subtime": func(t time.Time) string { return t.Format("Mon Jan 02, 2006 3:04 pm") },
|
|
"subdate": func(t time.Time) string { return t.Format("02 Jan 2006") },
|
|
"initial": initial,
|
|
"avatar": avatarClass,
|
|
"group": groupTitle,
|
|
"urlq": url.QueryEscape,
|
|
"path": url.PathEscape,
|
|
"add": func(a, b int) int { return a + b },
|
|
"plural": plural,
|
|
"num": func(n int) string { return groupDigits(n) },
|
|
"isSysop": func(u *store.User) bool { return u != nil && u.Sysop },
|
|
"truncate": truncate,
|
|
"snippet": snippet,
|
|
"av": func(name, size string) map[string]string { return map[string]string{"Name": name, "Size": size} },
|
|
"seq": func(n int) []int {
|
|
out := make([]int, n)
|
|
for i := range out {
|
|
out[i] = i + 1
|
|
}
|
|
return out
|
|
},
|
|
}
|
|
|
|
// when is a time as boards show it: today, yesterday, or the date.
|
|
func when(t time.Time) string {
|
|
if t.IsZero() {
|
|
return "never"
|
|
}
|
|
now := time.Now()
|
|
y, m, d := now.Date()
|
|
today := time.Date(y, m, d, 0, 0, 0, 0, now.Location())
|
|
switch {
|
|
case !t.Before(today):
|
|
return "Today, " + t.Format("03:04 PM")
|
|
case !t.Before(today.AddDate(0, 0, -1)):
|
|
return "Yesterday, " + t.Format("03:04 PM")
|
|
case t.Year() == now.Year():
|
|
return t.Format("02 January - 03:04 PM")
|
|
}
|
|
return t.Format("02 January 2006 - 03:04 PM")
|
|
}
|
|
|
|
// ago is a time as the boards of the 2010s and after told it: "2 hours
|
|
// ago", "Yesterday at 11:21 PM", "Monday at 9:02 AM", "October 12".
|
|
func ago(t time.Time) string {
|
|
if t.IsZero() {
|
|
return "never"
|
|
}
|
|
now := time.Now()
|
|
d := now.Sub(t)
|
|
y, m, dd := now.Date()
|
|
today := time.Date(y, m, dd, 0, 0, 0, 0, now.Location())
|
|
switch {
|
|
case d < time.Minute:
|
|
return "Just now"
|
|
case d < time.Hour:
|
|
return plural(int(d/time.Minute), "minute", "minutes") + " ago"
|
|
case d < 12*time.Hour && !t.Before(today):
|
|
return plural(int(d/time.Hour), "hour", "hours") + " ago"
|
|
case !t.Before(today):
|
|
return "Today at " + t.Format("3:04 PM")
|
|
case !t.Before(today.AddDate(0, 0, -1)):
|
|
return "Yesterday at " + t.Format("3:04 PM")
|
|
case !t.Before(today.AddDate(0, 0, -6)):
|
|
return t.Format("Monday at 3:04 PM")
|
|
case t.Year() == now.Year():
|
|
return t.Format("January 2")
|
|
}
|
|
return t.Format("January 2, 2006")
|
|
}
|
|
|
|
// longDate is a time in full, as "12th October 2026 - 11:21 PM".
|
|
func longDate(t time.Time) string {
|
|
d := t.Day()
|
|
suffix := "th"
|
|
if d < 11 || d > 13 {
|
|
switch d % 10 {
|
|
case 1:
|
|
suffix = "st"
|
|
case 2:
|
|
suffix = "nd"
|
|
case 3:
|
|
suffix = "rd"
|
|
}
|
|
}
|
|
return strconv.Itoa(d) + suffix + t.Format(" January 2006 - 03:04 PM")
|
|
}
|
|
|
|
// plural is n, its digits grouped, with one or many after it: "1 post",
|
|
// "1,204 posts".
|
|
func plural(n int, one, many string) string {
|
|
if n == 1 {
|
|
return groupDigits(n) + " " + one
|
|
}
|
|
return groupDigits(n) + " " + many
|
|
}
|
|
|
|
// groupDigits is n with commas between its thousands ("1,234,567"); a
|
|
// negative n is left as it is.
|
|
func groupDigits(n int) string {
|
|
s := strconv.Itoa(n)
|
|
if n < 0 || len(s) <= 3 {
|
|
return s
|
|
}
|
|
var b strings.Builder
|
|
for i, c := range s {
|
|
if i > 0 && (len(s)-i)%3 == 0 {
|
|
b.WriteByte(',')
|
|
}
|
|
b.WriteRune(c)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// truncate is s cut to its first n runes, trailing space trimmed and an
|
|
// ellipsis put on; s as it is if it is no longer than that.
|
|
func truncate(s string, n int) string {
|
|
if utf8.RuneCountInString(s) <= n {
|
|
return s
|
|
}
|
|
r := []rune(s)
|
|
return strings.TrimSpace(string(r[:n])) + "…"
|
|
}
|
|
|
|
// snippet is the start of a message's own words: what it quotes left out.
|
|
func snippet(body string, n int) string {
|
|
var words []string
|
|
for _, l := range strings.Split(body, "\n") {
|
|
t := strings.TrimSpace(l)
|
|
if t == "" || quoteLine.MatchString(l) || wroteLine.MatchString(t) {
|
|
continue
|
|
}
|
|
words = append(words, t)
|
|
}
|
|
return truncate(strings.Join(words, " "), n)
|
|
}
|
|
|
|
// initial is the letter a name's picture shows.
|
|
func initial(name string) string {
|
|
for _, r := range name {
|
|
if unicode.IsLetter(r) || unicode.IsDigit(r) {
|
|
return strings.ToUpper(string(r))
|
|
}
|
|
}
|
|
return "?"
|
|
}
|
|
|
|
// avatarClass is a name's picture's color (one of a palette, by the name).
|
|
func avatarClass(name string) string {
|
|
h := fnv.New32a()
|
|
h.Write([]byte(strings.ToLower(name)))
|
|
return fmt.Sprintf("av%d", h.Sum32()%12)
|
|
}
|
|
|
|
// groupTitle is what a user is called under their name.
|
|
func groupTitle(u *store.User) string {
|
|
switch {
|
|
case u == nil:
|
|
return "Guests"
|
|
case u.Sysop:
|
|
return "Administrators"
|
|
case u.Locked:
|
|
return "Banned"
|
|
}
|
|
return "Members"
|
|
}
|
|
|
|
var (
|
|
quoteLine = regexp.MustCompile(`^ ?[A-Za-z]{0,3}>`)
|
|
link = regexp.MustCompile(`\b(?:https?://|www\.)[^\s<>"']+[^\s<>"'.,;:!?)\]]`)
|
|
wroteLine = regexp.MustCompile(`^(.{1,40}) wrote:$`)
|
|
)
|
|
|
|
// renderBody is a message's text as HTML: its quoted lines (> or XX>, as
|
|
// BBSes quote) in quote boxes, links made links, the rest as it is.
|
|
func renderBody(body string) template.HTML {
|
|
lines := strings.Split(strings.ReplaceAll(body, "\r\n", "\n"), "\n")
|
|
var b strings.Builder
|
|
inQuote := false
|
|
for i := 0; i < len(lines); i++ {
|
|
l := lines[i]
|
|
if quoteLine.MatchString(l) {
|
|
if !inQuote {
|
|
b.WriteString(`<blockquote class="quote">`)
|
|
inQuote = true
|
|
}
|
|
text := strings.TrimPrefix(strings.TrimSpace(l[strings.Index(l, ">")+1:]), " ")
|
|
b.WriteString(linkify(text))
|
|
b.WriteString("<br>")
|
|
continue
|
|
}
|
|
if inQuote {
|
|
b.WriteString(`</blockquote>`)
|
|
inQuote = false
|
|
if strings.TrimSpace(l) == "" {
|
|
continue // the blank line after a quote: the box is gap enough
|
|
}
|
|
}
|
|
// "Name wrote:" before a quote is its title
|
|
if m := wroteLine.FindStringSubmatch(strings.TrimSpace(l)); m != nil && i+1 < len(lines) && quoteLine.MatchString(lines[i+1]) {
|
|
b.WriteString(`<p class="citation">` + html.EscapeString(m[1]) + ` said:</p>`)
|
|
continue
|
|
}
|
|
b.WriteString(linkify(l))
|
|
if i < len(lines)-1 {
|
|
b.WriteString("<br>")
|
|
}
|
|
}
|
|
if inQuote {
|
|
b.WriteString(`</blockquote>`)
|
|
}
|
|
return template.HTML(b.String())
|
|
}
|
|
|
|
// linkify escapes text, its web addresses made links.
|
|
func linkify(s string) string {
|
|
var b strings.Builder
|
|
last := 0
|
|
for _, m := range link.FindAllStringIndex(s, -1) {
|
|
b.WriteString(html.EscapeString(s[last:m[0]]))
|
|
u := s[m[0]:m[1]]
|
|
href := u
|
|
if strings.HasPrefix(href, "www.") {
|
|
href = "http://" + href
|
|
}
|
|
fmt.Fprintf(&b, `<a href="%s" rel="nofollow ugc noopener" class="bbc_url">%s</a>`,
|
|
html.EscapeString(href), html.EscapeString(u))
|
|
last = m[1]
|
|
}
|
|
b.WriteString(html.EscapeString(s[last:]))
|
|
return b.String()
|
|
}
|
|
|
|
// pages is a pagination's links.
|
|
type pages struct {
|
|
Cur, Total int
|
|
Base string // the URL, to which page=N is put
|
|
}
|
|
|
|
// URL is the link to page n: Base itself for the first page, else Base with
|
|
// page=n put on.
|
|
func (p pages) URL(n int) string {
|
|
sep := "?"
|
|
if strings.Contains(p.Base, "?") {
|
|
sep = "&"
|
|
}
|
|
if n <= 1 {
|
|
return p.Base
|
|
}
|
|
return p.Base + sep + "page=" + strconv.Itoa(n)
|
|
}
|
|
|
|
// Shown are the page numbers to show (0: a gap).
|
|
func (p pages) Shown() []int {
|
|
var out []int
|
|
for i := 1; i <= p.Total; i++ {
|
|
if i == 1 || i == p.Total || i >= p.Cur-2 && i <= p.Cur+2 {
|
|
out = append(out, i)
|
|
} else if len(out) > 0 && out[len(out)-1] != 0 {
|
|
out = append(out, 0)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Prev is the page before the current one (0 on the first).
|
|
func (p pages) Prev() int { return p.Cur - 1 }
|
|
|
|
// Next is the page after the current one, 0 on the last.
|
|
func (p pages) Next() int {
|
|
if p.Cur < p.Total {
|
|
return p.Cur + 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// newPages is the pagination of count items at perPage a page, cur held to
|
|
// between 1 and the last page (at least one page, even of nothing).
|
|
func newPages(base string, count, cur int) pages {
|
|
total := (count + perPage - 1) / perPage
|
|
if total < 1 {
|
|
total = 1
|
|
}
|
|
if cur < 1 {
|
|
cur = 1
|
|
}
|
|
if cur > total {
|
|
cur = total
|
|
}
|
|
return pages{Cur: cur, Total: total, Base: base}
|
|
}
|
|
|
|
// urlEscape is s escaped for a URL's query.
|
|
func urlEscape(s string) string { return url.QueryEscape(s) }
|
|
|
|
// yabb2Date writes a time as YaBB 2 did by default: "Oct 12th, 2026, 11:12pm".
|
|
func yabb2Date(t time.Time) string {
|
|
d := t.Day()
|
|
suffix := "th"
|
|
if d < 11 || d > 13 {
|
|
switch d % 10 {
|
|
case 1:
|
|
suffix = "st"
|
|
case 2:
|
|
suffix = "nd"
|
|
case 3:
|
|
suffix = "rd"
|
|
}
|
|
}
|
|
return fmt.Sprintf("%s %d%s, %d, %s", t.Format("Jan"), d, suffix, t.Year(), t.Format("3:04pm"))
|
|
}
|
|
|
|
// flagOf is a country's flag and name, for beside a poster's name ("":
|
|
// none known).
|
|
func flagOf(code string) (string, string) {
|
|
if code == "" {
|
|
return "", ""
|
|
}
|
|
name, ok := geoip.Names[code]
|
|
if !ok {
|
|
name = code
|
|
}
|
|
return geoip.Flag(code), name
|
|
}
|