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:
OverlordandClaude Bot committed 2026-10-14 17:26:26 +02:00
1 parent e2cbc1a55c
commit 15a34e1f84
39 files changed
+3529 -163

No files matched your search

+49
View File
@@ -50,6 +50,17 @@ type CheckMsg struct {
PriceErr error
}
// CartMsg is the outcome of adding an item to the cart in session mode.
type CartMsg struct {
Item string
// URL is the URL as configured.
URL string
// Link is the cart page, or the cart link when adding failed. It is
// what enter opens from now on.
Link string
Err error
}
type (
tickMsg time.Time
logMsg LogLine
@@ -109,6 +120,7 @@ type Model struct {
noteTime time.Time
reloading bool
solving string // the host whose bot check is open in a browser
modal *modal
}
// New returns the TUI for ctl. path is the config file, and logs is the
@@ -172,13 +184,50 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.rows = m.ctl.Statuses()
case openMsg:
if msg.err != nil { m.setNote("cannot open the browser: "+msg.err.Error(), true) } else { m.setNote("opened "+msg.url, false) }
case CartMsg:
m.carted(msg)
case promptMsg:
if m.modal != nil { m.answer(promptReply{}) }
var cmd tea.Cmd
m.modal, cmd = newModal(msg)
return m, tea.Batch(cmd, tea.Raw("\a"))
case closePromptMsg:
if m.modal != nil && m.modal.msg.id == msg.id { m.modal = nil }
case tea.KeyPressMsg:
if m.modal != nil { return m, m.modalKey(msg) }
return m, m.key(msg)
default:
// The modal's cursor blinks.
if m.modal != nil {
var cmd tea.Cmd
m.modal.input, cmd = m.modal.input.Update(msg)
return m, cmd
}
}
return m, nil
}
// carted records the outcome of adding an item to the cart.
func (m *Model) carted(c CartMsg) {
r := m.results[c.URL]
if r == nil {
r = &result{}
m.results[c.URL] = r
}
r.cartLink = c.Link
if c.Err != nil {
m.setNote("adding "+c.Item+" to the cart failed: "+c.Err.Error(), true)
return
}
m.setNote(c.Item+" is in the cart: press enter on its row and finish checkout", false)
}
// checked records a check. A new alert flashes its row and rings the bell.
func (m *Model) checked(c CheckMsg) tea.Cmd {
r := m.results[c.Result.URL]
+146
View File
@@ -0,0 +1,146 @@
package tui
import (
"context"
"errors"
"sync"
"charm.land/bubbles/v2/textinput"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
)
// ErrCancelled means the user closed a modal with esc.
var ErrCancelled = errors.New("cancelled in the TUI")
// Prompter asks the user in a modal, for session mode: one-time codes and
// the session warnings of plugins. One modal shows at a time; other
// questions wait for it.
type Prompter struct {
program *tea.Program
mu sync.Mutex
next int
}
func NewPrompter(p *tea.Program) *Prompter {
return &Prompter{program: p}
}
// Prompt asks for text. A secret is masked as it is typed.
func (pr *Prompter) Prompt(ctx context.Context, title, label string, secret bool) (string, error) {
r, err := pr.ask(ctx, promptMsg{title: title, label: label, secret: secret})
if err != nil { return "", err }
if !r.ok { return "", ErrCancelled }
return r.text, nil
}
// Confirm shows message until the user accepts it with enter or turns it
// down with esc.
func (pr *Prompter) Confirm(ctx context.Context, title, message string) (bool, error) {
r, err := pr.ask(ctx, promptMsg{title: title, label: message, confirm: true})
return r.ok, err
}
func (pr *Prompter) ask(ctx context.Context, msg promptMsg) (promptReply, error) {
pr.mu.Lock()
defer pr.mu.Unlock()
pr.next++
reply := make(chan promptReply, 1)
msg.id = pr.next
msg.reply = reply
pr.program.Send(msg)
select {
case r := <-reply:
return r, nil
case <-ctx.Done():
pr.program.Send(closePromptMsg{id: msg.id})
return promptReply{}, ctx.Err()
}
}
type (
promptMsg struct {
id int
title string
label string
secret bool
confirm bool
reply chan<- promptReply
}
promptReply struct {
text string
ok bool
}
// closePromptMsg closes a modal whose question timed out.
closePromptMsg struct{ id int }
)
type modal struct {
msg promptMsg
input textinput.Model
}
func newModal(msg promptMsg) (*modal, tea.Cmd) {
in := textinput.New()
in.Prompt = "> "
if msg.secret { in.EchoMode = textinput.EchoPassword }
return &modal{msg: msg, input: in}, in.Focus()
}
// answer replies and closes the modal.
func (m *Model) answer(r promptReply) {
m.modal.msg.reply <- r
m.modal = nil
}
func (m *Model) modalKey(msg tea.KeyPressMsg) tea.Cmd {
switch msg.String() {
case "ctrl+c":
m.answer(promptReply{})
return tea.Quit
case "esc":
m.answer(promptReply{})
return nil
case "enter":
m.answer(promptReply{text: m.modal.input.Value(), ok: true})
return nil
}
if m.modal.msg.confirm { return nil }
var cmd tea.Cmd
m.modal.input, cmd = m.modal.input.Update(msg)
return cmd
}
var modalBox = lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color("13")).Padding(1, 2)
// modalView draws the modal in the middle of screen, which is the rest of
// the view.
func (m *Model) modalView(screen string) string {
width := max(min(m.width-4, 72), 20)
help := "enter send · esc cancel"
if m.modal.msg.confirm { help = "enter continue · esc not now" }
body := bold.Render(m.modal.msg.title) + "\n\n" + m.modal.msg.label
if !m.modal.msg.confirm {
m.modal.input.SetWidth(width - 10)
body += "\n\n" + m.modal.input.View()
}
body += "\n\n" + faint.Render(help)
box := modalBox.Width(width).Render(body)
x := max((m.width-lipgloss.Width(box))/2, 0)
y := max((m.height-lipgloss.Height(box))/2, 0)
layers := lipgloss.NewCompositor(lipgloss.NewLayer(screen), lipgloss.NewLayer(box).X(x).Y(y).Z(1))
return lipgloss.NewCanvas(m.width, m.height).Compose(layers).Render()
}
+78
View File
@@ -0,0 +1,78 @@
package tui
import (
"log/slog"
"strings"
"testing"
tea "charm.land/bubbletea/v2"
"git.security-command.org/Overlord/dibs/internal/watch"
)
type idle struct{}
func (idle) Statuses() []watch.Status { return nil }
func (idle) Pause(string, bool) bool { return false }
func (idle) PauseAll(bool) {}
func (idle) CheckNow(string) bool { return false }
func (idle) Reload() ([]string, error) { return nil, nil }
func (idle) Solve(string) error { return nil }
func newTestModel() *Model {
m := New(idle{}, "/tmp/dibs.toml", NewLogHandler(slog.LevelInfo))
m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
return m
}
func ask(m *Model, msg promptMsg) <-chan promptReply {
reply := make(chan promptReply, 1)
msg.reply = reply
m.Update(msg)
return reply
}
func typeText(m *Model, s string) {
for _, r := range s { m.Update(tea.KeyPressMsg{Code: r, Text: string(r)}) }
}
func TestModalPrompt(t *testing.T) {
m := newTestModel()
reply := ask(m, promptMsg{id: 1, title: "amazon · www.amazon.de", label: "One-time code", secret: true})
typeText(m, "123456")
view := m.View().Content
if !strings.Contains(view, "One-time code") { t.Error("the modal is not shown") }
if strings.Contains(view, "123456") { t.Error("the secret shows as it is typed") }
m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
if r := <-reply; !r.ok || r.text != "123456" { t.Errorf("reply %+v", r) }
if m.modal != nil { t.Error("the modal is still open") }
}
func TestModalCancelAndConfirm(t *testing.T) {
m := newTestModel()
// Keys go to the modal, so q is typed instead of quitting.
reply := ask(m, promptMsg{id: 1, title: "t", label: "code"})
typeText(m, "q")
if got := m.modal.input.Value(); got != "q" { t.Errorf("typed %q", got) }
m.Update(tea.KeyPressMsg{Code: tea.KeyEscape})
if r := <-reply; r.ok { t.Errorf("esc replied %+v", r) }
reply = ask(m, promptMsg{id: 2, title: "Session mode for amazon", label: "Automated logins can lock the account.", confirm: true})
m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
if r := <-reply; !r.ok { t.Error("enter did not accept the warning") }
// A question that timed out closes its modal, but no newer one.
ask(m, promptMsg{id: 3, title: "t", label: "code"})
m.Update(closePromptMsg{id: 2})
if m.modal == nil { t.Error("an old timeout closed the modal") }
m.Update(closePromptMsg{id: 3})
if m.modal != nil { t.Error("the timed-out modal is still open") }
}
+6 -1
View File
@@ -82,6 +82,11 @@ func (m *Model) View() tea.View {
b.WriteString(m.statusBar() + "\n")
b.WriteString(fit(faint.Render(m.help()), m.width))
if m.modal != nil {
v.SetContent(m.modalView(b.String()))
return v
}
v.SetContent(b.String())
return v
}
@@ -196,7 +201,7 @@ func (m *Model) logLines() []string {
}
msg := l.Msg
if msg == "ALERT" { msg = green.Render(msg) }
if msg == "ALERT" || msg == "IN CART" { msg = green.Render(msg) }
out[i] = faint.Render(l.Time.Format(time.TimeOnly)) + " " + level + " " + msg + " " + faint.Render(l.Attrs)
}