Sandboxed gopher-lua with time, memory and call-stack limits from [sandbox], a goquery-backed doc API, price parsing, and the 'check', 'plugin test' and 'fixtures' commands. Sellers are first_party, fulfilled (shipped by the shop) or third_party, and items accept fulfilled by default; used-only offers are not available. Plugin tests run on real pages that 'dibs fixtures' fetches into an untracked directory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
2.4 KiB
Go
108 lines
2.4 KiB
Go
package plugin
|
|
|
|
import (
|
|
"log/slog"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
|
|
"git.security-command.org/Overlord/dibs/internal/price"
|
|
)
|
|
|
|
func registerHelpers(L *lua.LState, logger *slog.Logger) {
|
|
logFn := L.NewFunction(func(L *lua.LState) int {
|
|
parts := make([]string, L.GetTop())
|
|
for i := range parts { parts[i] = L.ToStringMeta(L.Get(i + 1)).String() }
|
|
|
|
logger.Info(strings.Join(parts, " "))
|
|
return 0
|
|
})
|
|
|
|
L.SetGlobal("log", logFn)
|
|
L.SetGlobal("print", logFn)
|
|
L.SetGlobal("contains_any", L.NewFunction(containsAny))
|
|
L.SetGlobal("parse_price", L.NewFunction(parsePrice))
|
|
L.SetGlobal("regex_match", L.NewFunction(regexMatch))
|
|
}
|
|
|
|
// contains_any(str, {phrases}) reports whether str contains any of the
|
|
// phrases, ignoring case. A nil str contains nothing.
|
|
func containsAny(L *lua.LState) int {
|
|
phrases := L.CheckTable(2)
|
|
if L.Get(1) == lua.LNil {
|
|
L.Push(lua.LFalse)
|
|
return 1
|
|
}
|
|
|
|
s := strings.ToLower(L.CheckString(1))
|
|
found := false
|
|
|
|
phrases.ForEach(func(_, v lua.LValue) {
|
|
if p, ok := v.(lua.LString); ok && !found && strings.Contains(s, strings.ToLower(string(p))) { found = true }
|
|
})
|
|
|
|
L.Push(lua.LBool(found))
|
|
return 1
|
|
}
|
|
|
|
// parse_price(str) returns the price in str and its currency code (or nil),
|
|
// or nil if str holds no price.
|
|
func parsePrice(L *lua.LState) int {
|
|
if L.Get(1) == lua.LNil {
|
|
L.Push(lua.LNil)
|
|
return 1
|
|
}
|
|
|
|
v, cur, ok := price.Parse(L.CheckString(1))
|
|
if !ok {
|
|
L.Push(lua.LNil)
|
|
return 1
|
|
}
|
|
|
|
L.Push(lua.LNumber(v))
|
|
if cur != "" { L.Push(lua.LString(cur)) } else { L.Push(lua.LNil) }
|
|
return 2
|
|
}
|
|
|
|
var regexps sync.Map // string -> *regexp.Regexp
|
|
|
|
// regex_match(str, pattern) matches a Go (RE2) regular expression. It
|
|
// returns a list of the capture groups, or of the whole match if the
|
|
// pattern has no groups, or nil if it does not match.
|
|
func regexMatch(L *lua.LState) int {
|
|
pattern := L.CheckString(2)
|
|
if L.Get(1) == lua.LNil {
|
|
L.Push(lua.LNil)
|
|
return 1
|
|
}
|
|
|
|
s := L.CheckString(1)
|
|
|
|
var re *regexp.Regexp
|
|
if cached, ok := regexps.Load(pattern); ok {
|
|
re = cached.(*regexp.Regexp)
|
|
} else {
|
|
compiled, err := regexp.Compile(pattern)
|
|
if err != nil { L.ArgError(2, "invalid pattern: "+err.Error()) }
|
|
|
|
re = compiled
|
|
regexps.Store(pattern, re)
|
|
}
|
|
|
|
m := re.FindStringSubmatch(s)
|
|
if m == nil {
|
|
L.Push(lua.LNil)
|
|
return 1
|
|
}
|
|
|
|
if len(m) > 1 { m = m[1:] }
|
|
|
|
t := L.NewTable()
|
|
for _, g := range m { t.Append(lua.LString(g)) }
|
|
|
|
L.Push(t)
|
|
return 1
|
|
}
|