Add session mode: signing in and adding to the cart.
With cart_mode = "session", dibs signs in to the shop's account and puts an item in the cart when it fires. The account's cookies are kept apart from the watcher's and stored encrypted, with the key in the OS keyring. Without a keyring, they're stored in a file only you can read. An alert now goes out right away, without a cart link, and a second one follows once the item is in the cart. If adding fails, the second alert carries the cart link instead. Amazon only accepts additions its page scripts have seen, so plugins can now press a button in headless Chrome with ctx:click. The TUI asks for one-time codes and shows the plugin's warning about account locks in a modal. 'dibs login' signs in from a terminal, or with --browser in a Chrome window, and 'dibs logout' deletes the cookies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e2cbc1a55c
commit
15a34e1f84
39 files changed
+3529
-163
No files matched your search
+50
-21
@@ -46,6 +46,9 @@ type Result struct {
|
||||
// hooks are the functions a plugin table may define. Only parse is required.
|
||||
var hooks = []string{"matches", "normalize", "parse", "cart_link", "login", "add_to_cart"}
|
||||
|
||||
// tableKeys are the other keys a plugin table may have.
|
||||
var tableKeys = []string{"name", "session_warning"}
|
||||
|
||||
var currencyRe = regexp.MustCompile(`^[A-Z]{3}$`)
|
||||
|
||||
// Plugin is a loaded plugin. It is safe for concurrent use; calls into one
|
||||
@@ -54,6 +57,9 @@ type Plugin struct {
|
||||
Name string
|
||||
// Source is the plugin's file, or "built-in".
|
||||
Source string
|
||||
// SessionWarning is shown once before the first login, such as a
|
||||
// warning that automated logins can get the account locked.
|
||||
SessionWarning string
|
||||
|
||||
chunk string
|
||||
code []byte
|
||||
@@ -86,7 +92,12 @@ func Load(name, source string, code []byte, limits Limits, logger *slog.Logger)
|
||||
shortCalls.Lock()
|
||||
defer shortCalls.Unlock()
|
||||
|
||||
if err := p.reset(); err != nil { return nil, fmt.Errorf("plugin %s (%s): %w", name, source, err) }
|
||||
L, t, err := p.load()
|
||||
if err != nil { return nil, fmt.Errorf("plugin %s (%s): %w", name, source, err) }
|
||||
|
||||
if t.name != "" { p.Name = t.name }
|
||||
p.SessionWarning = t.warning
|
||||
p.state, p.funcs = L, t.funcs
|
||||
|
||||
return p, nil
|
||||
}
|
||||
@@ -98,6 +109,24 @@ func (p *Plugin) reset() error {
|
||||
p.state = nil
|
||||
}
|
||||
|
||||
L, t, err := p.load()
|
||||
if err != nil { return err }
|
||||
|
||||
p.state, p.funcs = L, t.funcs
|
||||
return nil
|
||||
}
|
||||
|
||||
// table is what a plugin's table defines.
|
||||
type table struct {
|
||||
// name is the table's name, if the config did not fix it.
|
||||
name string
|
||||
warning string
|
||||
funcs map[string]*lua.LFunction
|
||||
}
|
||||
|
||||
// load runs the plugin's code in a new Lua state and reads its table.
|
||||
// Callers hold shortCalls.
|
||||
func (p *Plugin) load() (*lua.LState, *table, error) {
|
||||
L := newState(p.limits)
|
||||
registerNode(L)
|
||||
registerHelpers(L, p.logger.With("plugin", p.Name))
|
||||
@@ -105,38 +134,37 @@ func (p *Plugin) reset() error {
|
||||
fn, err := L.Load(bytes.NewReader(p.code), p.chunk)
|
||||
if err != nil {
|
||||
L.Close()
|
||||
return luaError(err)
|
||||
return nil, nil, luaError(err)
|
||||
}
|
||||
|
||||
var tbl *lua.LTable
|
||||
var t *table
|
||||
|
||||
err = run(L, context.Background(), p.limits.Timeout, p.limits.MemoryBytes, func() error {
|
||||
err = run(L, context.Background(), p.limits.Timeout, newMeter(p.limits.MemoryBytes), func() error {
|
||||
if err := L.CallByParam(lua.P{Fn: fn, NRet: 1, Protect: true}); err != nil { return err }
|
||||
|
||||
ret := L.Get(-1)
|
||||
L.Pop(1)
|
||||
|
||||
t, ok := ret.(*lua.LTable)
|
||||
tbl, ok := ret.(*lua.LTable)
|
||||
if !ok { return fmt.Errorf("must return a table, got %s", ret.Type()) }
|
||||
|
||||
tbl = t
|
||||
return nil
|
||||
read, err := p.readTable(tbl)
|
||||
t = read
|
||||
return err
|
||||
})
|
||||
if err == nil { err = p.readTable(tbl) }
|
||||
if err != nil {
|
||||
L.Close()
|
||||
return err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
p.state = L
|
||||
return nil
|
||||
return L, t, nil
|
||||
}
|
||||
|
||||
// readTable checks the plugin table and picks up its functions.
|
||||
func (p *Plugin) readTable(tbl *lua.LTable) error {
|
||||
func (p *Plugin) readTable(tbl *lua.LTable) (*table, error) {
|
||||
var problems []string
|
||||
|
||||
funcs := map[string]*lua.LFunction{}
|
||||
t := &table{funcs: map[string]*lua.LFunction{}}
|
||||
|
||||
tbl.ForEach(func(k, v lua.LValue) {
|
||||
key := k.String()
|
||||
@@ -148,28 +176,29 @@ func (p *Plugin) readTable(tbl *lua.LTable) error {
|
||||
case !ok || s == "":
|
||||
problems = append(problems, fmt.Sprintf("name must be a non-empty string, got %s", v.Type()))
|
||||
case !p.nameFixed:
|
||||
p.Name = string(s)
|
||||
t.name = string(s)
|
||||
case string(s) != p.Name:
|
||||
problems = append(problems, fmt.Sprintf("name is %q, but the plugin is configured as %q", s, p.Name))
|
||||
}
|
||||
case key == "session_warning":
|
||||
if s, ok := v.(lua.LString); ok { t.warning = strings.TrimSpace(string(s)) } else { problems = append(problems, fmt.Sprintf("session_warning must be a string, got %s", v.Type())) }
|
||||
case slices.Contains(hooks, key):
|
||||
if fn, ok := v.(*lua.LFunction); ok { funcs[key] = fn } else { problems = append(problems, fmt.Sprintf("%s must be a function, got %s", key, v.Type())) }
|
||||
if fn, ok := v.(*lua.LFunction); ok { t.funcs[key] = fn } else { problems = append(problems, fmt.Sprintf("%s must be a function, got %s", key, v.Type())) }
|
||||
default:
|
||||
problems = append(problems, fmt.Sprintf("unknown key %q (known: name, %s)", key, strings.Join(hooks, ", ")))
|
||||
problems = append(problems, fmt.Sprintf("unknown key %q (known: %s, %s)", key, strings.Join(tableKeys, ", "), strings.Join(hooks, ", ")))
|
||||
}
|
||||
})
|
||||
|
||||
if funcs["parse"] == nil && !slices.ContainsFunc(problems, func(s string) bool { return strings.HasPrefix(s, "parse ") }) {
|
||||
if t.funcs["parse"] == nil && !slices.ContainsFunc(problems, func(s string) bool { return strings.HasPrefix(s, "parse ") }) {
|
||||
problems = append(problems, "parse function is missing")
|
||||
}
|
||||
|
||||
if len(problems) > 0 {
|
||||
slices.Sort(problems)
|
||||
return errors.New(strings.Join(problems, "; "))
|
||||
return nil, errors.New(strings.Join(problems, "; "))
|
||||
}
|
||||
|
||||
p.funcs = funcs
|
||||
return nil
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (p *Plugin) Has(hook string) bool {
|
||||
@@ -197,7 +226,7 @@ func (p *Plugin) call(ctx context.Context, hook string, nret int, args func(L *l
|
||||
|
||||
var out []lua.LValue
|
||||
|
||||
err := run(L, ctx, p.limits.Timeout, p.limits.MemoryBytes, func() error {
|
||||
err := run(L, ctx, p.limits.Timeout, newMeter(p.limits.MemoryBytes), func() error {
|
||||
if err := L.CallByParam(lua.P{Fn: p.funcs[hook], NRet: nret, Protect: true}, args(L)...); err != nil { return err }
|
||||
|
||||
for i := nret; i > 0; i-- { out = append(out, L.Get(-i)) }
|
||||
|
||||
Reference in new issue
Block a user