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>
218 lines
6.3 KiB
Go
218 lines
6.3 KiB
Go
// Command dibs is a personal restock watcher.
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"runtime"
|
|
"runtime/debug"
|
|
"slices"
|
|
"strings"
|
|
|
|
"git.security-command.org/Overlord/dibs/internal/config"
|
|
"git.security-command.org/Overlord/dibs/internal/plugin"
|
|
)
|
|
|
|
// version is set at build time with -ldflags "-X main.version=...".
|
|
var version = "dev"
|
|
|
|
const usage = `Usage:
|
|
dibs [--config FILE] [--headless] watch items (TUI unless --headless)
|
|
dibs check [--shop NAME] [--chrome] <url> fetch and parse one URL, print JSON
|
|
dibs solve [--shop NAME] <url> pass a bot check in a Chrome window
|
|
dibs login [--browser] <shop> [url] sign in to a shop for cart_mode = "session"
|
|
dibs logout <shop> delete a shop's signed-in cookies
|
|
dibs plugin test [--status CODE] <file.lua> <fixture.html> [url]
|
|
dibs config validate [--config FILE]
|
|
dibs notify test [--config FILE] [--item NAME] send a sample alert to every channel
|
|
dibs fixtures [--dir testdata] [--only NAME] fetch real pages for the plugin tests
|
|
dibs version
|
|
`
|
|
|
|
func main() {
|
|
os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
|
|
}
|
|
|
|
// commands are the subcommands run dispatches on.
|
|
var commands = []string{"config", "version", "check", "plugin", "fixtures", "notify", "solve", "login", "logout", "help"}
|
|
|
|
// commandFirst moves a --config given before the command behind it, so
|
|
// that "dibs --config f.toml logout amazon" works like "dibs logout
|
|
// --config f.toml amazon".
|
|
func commandFirst(args []string) []string {
|
|
n := 0
|
|
|
|
switch {
|
|
case len(args) > 0 && (strings.HasPrefix(args[0], "--config=") || strings.HasPrefix(args[0], "-config=")):
|
|
n = 1
|
|
case len(args) > 1 && (args[0] == "--config" || args[0] == "-config"):
|
|
n = 2
|
|
}
|
|
|
|
if n == 0 || len(args) <= n || !slices.Contains(commands, args[n]) { return args }
|
|
|
|
out := []string{args[n]}
|
|
out = append(out, args[:n]...)
|
|
|
|
return append(out, args[n+1:]...)
|
|
}
|
|
|
|
func run(args []string, stdout, stderr io.Writer) int {
|
|
args = commandFirst(args)
|
|
|
|
if len(args) > 0 {
|
|
switch args[0] {
|
|
case "config":
|
|
return runConfig(args[1:], stdout, stderr)
|
|
case "version", "-v", "--version":
|
|
printVersion(stdout)
|
|
return 0
|
|
case "check":
|
|
return runCheck(args[1:], stdout, stderr)
|
|
case "plugin":
|
|
return runPlugin(args[1:], stdout, stderr)
|
|
case "fixtures":
|
|
return runFixtures(args[1:], stdout, stderr)
|
|
case "notify":
|
|
return runNotify(args[1:], stdout, stderr)
|
|
case "solve":
|
|
return runSolve(args[1:], stdout, stderr)
|
|
case "login":
|
|
return runLogin(args[1:], stdout, stderr)
|
|
case "logout":
|
|
return runLogout(args[1:], stdout, stderr)
|
|
case "help", "-h", "--help":
|
|
_, _ = fmt.Fprint(stdout, usage)
|
|
return 0
|
|
}
|
|
}
|
|
|
|
flags, configPath := newFlagSet("dibs", stderr)
|
|
headless := flags.Bool("headless", false, "run without the TUI, log JSON to stdout")
|
|
|
|
if err := flags.Parse(args); err != nil { return 2 }
|
|
|
|
if flags.NArg() > 0 {
|
|
_, _ = fmt.Fprintf(stderr, "dibs: unknown command %q\n\n%s", flags.Arg(0), usage)
|
|
return 2
|
|
}
|
|
|
|
cfg, ok := loadConfig(*configPath, stderr)
|
|
if !ok { return 1 }
|
|
|
|
return runWatch(cfg, *headless, stdout, stderr)
|
|
}
|
|
|
|
func runConfig(args []string, stdout, stderr io.Writer) int {
|
|
if len(args) == 0 || args[0] != "validate" {
|
|
_, _ = fmt.Fprintf(stderr, "dibs config: expected \"validate\"\n\n%s", usage)
|
|
return 2
|
|
}
|
|
|
|
flags, configPath := newFlagSet("dibs config validate", stderr)
|
|
if err := flags.Parse(args[1:]); err != nil { return 2 }
|
|
|
|
cfg, ok := loadConfig(*configPath, stderr)
|
|
if !ok { return 1 }
|
|
|
|
urls := 0
|
|
for _, it := range cfg.Items {
|
|
urls += len(it.URLs)
|
|
}
|
|
|
|
_, _ = fmt.Fprintf(stdout, "%s: OK (%s, %s, %s, %s)\n", cfg.Path,
|
|
plural(len(cfg.Items), "item"), plural(urls, "URL"),
|
|
plural(len(cfg.Accounts), "account"), plural(len(cfg.Plugins), "plugin"))
|
|
|
|
return 0
|
|
}
|
|
|
|
func plural(n int, noun string) string {
|
|
if n == 1 { return "1 " + noun }
|
|
return fmt.Sprintf("%d %ss", n, noun)
|
|
}
|
|
|
|
// newFlagSet returns a flag set with the --config flag every command takes.
|
|
func newFlagSet(name string, stderr io.Writer) (*flag.FlagSet, *string) {
|
|
flags := flag.NewFlagSet(name, flag.ContinueOnError)
|
|
flags.SetOutput(stderr)
|
|
path := flags.String("config", "", "config file (default: dibs.toml in the user config dir)")
|
|
|
|
return flags, path
|
|
}
|
|
|
|
func loadConfig(path string, stderr io.Writer) (*config.Config, bool) {
|
|
if path == "" {
|
|
var err error
|
|
if path, err = config.DefaultPath(); err != nil {
|
|
_, _ = fmt.Fprintf(stderr, "dibs: no config dir: %v\n", err)
|
|
return nil, false
|
|
}
|
|
}
|
|
|
|
cfg, err := config.Load(path, config.Options{Builtins: plugin.Builtins()})
|
|
if err != nil {
|
|
var errs config.Errors
|
|
switch {
|
|
case errors.As(err, &errs):
|
|
_, _ = fmt.Fprintln(stderr, errs)
|
|
_, _ = fmt.Fprintf(stderr, "%s in config\n", plural(len(errs), "problem"))
|
|
case errors.Is(err, fs.ErrNotExist):
|
|
_, _ = fmt.Fprintf(stderr, "dibs: no config at %s: create it or pass --config\n", path)
|
|
default:
|
|
_, _ = fmt.Fprintf(stderr, "dibs: %v\n", err)
|
|
}
|
|
|
|
return nil, false
|
|
}
|
|
|
|
return cfg, true
|
|
}
|
|
|
|
// loadOptionalConfig is loadConfig for commands that also work without a
|
|
// config: with no --config and no file at the default path it returns nil.
|
|
func loadOptionalConfig(path string, stderr io.Writer) (*config.Config, bool) {
|
|
if path == "" {
|
|
def, err := config.DefaultPath()
|
|
if err != nil { return nil, true }
|
|
if _, err := os.Stat(def); errors.Is(err, fs.ErrNotExist) { return nil, true }
|
|
}
|
|
|
|
return loadConfig(path, stderr)
|
|
}
|
|
|
|
// parseArgs parses flags anywhere among the arguments, so that
|
|
// "dibs check URL --shop amazon" works too, and returns the positional ones.
|
|
func parseArgs(flags *flag.FlagSet, args []string) ([]string, bool) {
|
|
var pos []string
|
|
|
|
for {
|
|
if err := flags.Parse(args); err != nil { return nil, false }
|
|
if flags.NArg() == 0 { return pos, true }
|
|
|
|
pos = append(pos, flags.Arg(0))
|
|
args = flags.Args()[1:]
|
|
}
|
|
}
|
|
|
|
func printVersion(w io.Writer) {
|
|
v, rev := version, ""
|
|
if info, ok := debug.ReadBuildInfo(); ok {
|
|
if v == "dev" && info.Main.Version != "" && info.Main.Version != "(devel)" {
|
|
v = info.Main.Version
|
|
}
|
|
|
|
for _, s := range info.Settings {
|
|
if s.Key == "vcs.revision" && len(s.Value) >= 12 {
|
|
rev = " " + s.Value[:12]
|
|
}
|
|
}
|
|
}
|
|
|
|
_, _ = fmt.Fprintf(w, "dibs %s%s (%s, %s/%s)\n", v, rev, runtime.Version(), runtime.GOOS, runtime.GOARCH)
|
|
}
|