mirror of
https://github.com/anotherhadi/spilltea.git
synced 2026-05-20 01:32:33 +02:00
Change plugins behavior
Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
This commit is contained in:
+83
-2
@@ -26,8 +26,9 @@ func registerUtilities(L *lua.LState, mgr *Manager, p *Plugin) {
|
||||
L.SetGlobal("notif", L.NewFunction(func(L *lua.LState) int {
|
||||
title := L.CheckString(1)
|
||||
body := L.CheckString(2)
|
||||
kind := L.OptString(3, "info")
|
||||
select {
|
||||
case mgr.Notifs <- PluginNotifMsg{Title: title, Body: body}:
|
||||
case mgr.Notifs <- PluginNotifMsg{Title: title, Body: body, Kind: kind}:
|
||||
default:
|
||||
}
|
||||
return 0
|
||||
@@ -64,7 +65,87 @@ func registerUtilities(L *lua.LState, mgr *Manager, p *Plugin) {
|
||||
return 0
|
||||
}))
|
||||
|
||||
L.SetGlobal("quit", L.NewFunction(func(L *lua.LState) int {
|
||||
L.SetGlobal("db_query", L.NewFunction(func(L *lua.LState) int {
|
||||
if mgr.db == nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString("db not available"))
|
||||
return 2
|
||||
}
|
||||
query := L.CheckString(1)
|
||||
var args []any
|
||||
for i := 2; i <= L.GetTop(); i++ {
|
||||
switch v := L.Get(i).(type) {
|
||||
case lua.LString:
|
||||
args = append(args, string(v))
|
||||
case lua.LNumber:
|
||||
args = append(args, float64(v))
|
||||
case lua.LBool:
|
||||
args = append(args, bool(v))
|
||||
default:
|
||||
args = append(args, nil)
|
||||
}
|
||||
}
|
||||
rows, err := mgr.db.Query(query, args...)
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
defer rows.Close()
|
||||
cols, err := rows.Columns()
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
result := L.NewTable()
|
||||
rowIdx := 1
|
||||
for rows.Next() {
|
||||
vals := make([]any, len(cols))
|
||||
ptrs := make([]any, len(cols))
|
||||
for i := range vals {
|
||||
ptrs[i] = &vals[i]
|
||||
}
|
||||
if err := rows.Scan(ptrs...); err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
row := L.NewTable()
|
||||
for i, col := range cols {
|
||||
switch v := vals[i].(type) {
|
||||
case int64:
|
||||
L.SetField(row, col, lua.LNumber(v))
|
||||
case float64:
|
||||
L.SetField(row, col, lua.LNumber(v))
|
||||
case string:
|
||||
L.SetField(row, col, lua.LString(v))
|
||||
case []byte:
|
||||
L.SetField(row, col, lua.LString(string(v)))
|
||||
case bool:
|
||||
if v {
|
||||
L.SetField(row, col, lua.LTrue)
|
||||
} else {
|
||||
L.SetField(row, col, lua.LFalse)
|
||||
}
|
||||
case nil:
|
||||
L.SetField(row, col, lua.LNil)
|
||||
}
|
||||
}
|
||||
L.RawSetInt(result, rowIdx, row)
|
||||
rowIdx++
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
L.Push(result)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}))
|
||||
|
||||
L.SetGlobal("quit", L.NewFunction(func(L *lua.LState) int {
|
||||
reason := L.OptString(1, "plugin requested quit")
|
||||
select {
|
||||
case mgr.Quit <- reason:
|
||||
|
||||
+97
-39
@@ -5,6 +5,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -27,12 +28,13 @@ type Manager struct {
|
||||
|
||||
func NewManager(broker *intercept.Broker) *Manager {
|
||||
mgr := &Manager{
|
||||
broker: broker,
|
||||
broker: broker,
|
||||
Notifs: make(chan PluginNotifMsg, 64),
|
||||
Quit: make(chan string, 4),
|
||||
}
|
||||
if broker != nil {
|
||||
broker.SetOnNewEntry(mgr.RunOnHistoryEntry)
|
||||
broker.SetOnBeforeNewEntry(mgr.RunSyncOnHistoryEntry)
|
||||
broker.SetOnNewEntry(mgr.RunAsyncOnHistoryEntry)
|
||||
}
|
||||
return mgr
|
||||
}
|
||||
@@ -107,27 +109,41 @@ func (m *Manager) loadPlugin(path string) (*Plugin, error) {
|
||||
p.Name = strings.TrimSuffix(filepath.Base(path), ".lua")
|
||||
}
|
||||
|
||||
// Defaults when not overridden by the Plugin table.
|
||||
hookDefaults := map[string]bool{
|
||||
"on_start": true, // always sync
|
||||
"on_request": false, // async
|
||||
"on_response": false, // async
|
||||
"on_quit": true, // always sync
|
||||
"on_history_entry": false, // always async
|
||||
if s, ok := pluginTable.RawGetString("description").(lua.LString); ok {
|
||||
p.Description = string(s)
|
||||
}
|
||||
for hookName, defaultSync := range hookDefaults {
|
||||
// Plugin table entry overrides the default (except on_start/on_quit/on_history_entry which are fixed).
|
||||
if hookName != "on_start" && hookName != "on_quit" && hookName != "on_history_entry" {
|
||||
if tbl, ok := pluginTable.RawGetString(hookName).(*lua.LTable); ok {
|
||||
p.hooks[hookName] = HookConfig{Sync: tbl.RawGetString("sync") == lua.LTrue}
|
||||
continue
|
||||
}
|
||||
|
||||
if n, ok := pluginTable.RawGetString("priority").(lua.LNumber); ok {
|
||||
p.Priority = int(n)
|
||||
}
|
||||
|
||||
// Hooks configurable via the Plugin table (sync field).
|
||||
configurableHooks := map[string]bool{
|
||||
"on_start": false, // async by default
|
||||
"on_request": false,
|
||||
"on_response": false,
|
||||
"on_history_entry": false,
|
||||
}
|
||||
// Fixed-sync hooks: always sync, not configurable.
|
||||
fixedSyncHooks := map[string]struct{}{
|
||||
"on_config": {},
|
||||
"on_quit": {},
|
||||
}
|
||||
|
||||
for hookName, defaultSync := range configurableHooks {
|
||||
if tbl, ok := pluginTable.RawGetString(hookName).(*lua.LTable); ok {
|
||||
p.hooks[hookName] = HookConfig{Sync: tbl.RawGetString("sync") == lua.LTrue}
|
||||
continue
|
||||
}
|
||||
// Auto-detect: register the hook if the function exists as a global.
|
||||
if p.L.GetGlobal(hookName) != lua.LNil {
|
||||
p.hooks[hookName] = HookConfig{Sync: defaultSync}
|
||||
}
|
||||
}
|
||||
for hookName := range fixedSyncHooks {
|
||||
if p.L.GetGlobal(hookName) != lua.LNil {
|
||||
p.hooks[hookName] = HookConfig{Sync: true}
|
||||
}
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
@@ -137,6 +153,7 @@ func (m *Manager) GetPlugins() []*Plugin {
|
||||
defer m.mu.RUnlock()
|
||||
out := make([]*Plugin, len(m.plugins))
|
||||
copy(out, m.plugins)
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Priority > out[j].Priority })
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -179,46 +196,62 @@ func (m *Manager) SaveConfig(name, configText string) {
|
||||
found.mu.Lock()
|
||||
found.ConfigText = configText
|
||||
enabled := found.Enabled
|
||||
hc, hasOnStart := found.hooks["on_start"]
|
||||
_, hasOnConfig := found.hooks["on_config"]
|
||||
found.mu.Unlock()
|
||||
if m.db != nil {
|
||||
_ = m.db.SavePluginState(name, enabled, configText)
|
||||
}
|
||||
if !hasOnStart {
|
||||
if !hasOnConfig {
|
||||
return
|
||||
}
|
||||
// Re-run on_start so the plugin can re-parse the new config.
|
||||
if hc.Sync {
|
||||
found.mu.Lock()
|
||||
if _, err := callHook(found, "on_start", lua.LString(configText)); err != nil {
|
||||
log.Printf("plugin %s on_start (config reload): %v", name, err)
|
||||
}
|
||||
found.mu.Unlock()
|
||||
} else {
|
||||
go func() {
|
||||
found.mu.Lock()
|
||||
if _, err := callHook(found, "on_start", lua.LString(configText)); err != nil {
|
||||
log.Printf("plugin %s on_start (config reload): %v", name, err)
|
||||
}
|
||||
found.mu.Unlock()
|
||||
}()
|
||||
// on_config is always sync.
|
||||
found.mu.Lock()
|
||||
if _, err := callHook(found, "on_config", lua.LString(configText)); err != nil {
|
||||
log.Printf("plugin %s on_config (config reload): %v", name, err)
|
||||
}
|
||||
found.mu.Unlock()
|
||||
}
|
||||
|
||||
func (m *Manager) RunOnStart() {
|
||||
// on_config runs first, always sync, for every enabled plugin that has it.
|
||||
for _, p := range m.GetPlugins() {
|
||||
if !p.Enabled {
|
||||
continue
|
||||
}
|
||||
if _, ok := p.hooks["on_start"]; !ok {
|
||||
if _, ok := p.hooks["on_config"]; !ok {
|
||||
continue
|
||||
}
|
||||
p.mu.Lock()
|
||||
if _, err := callHook(p, "on_start", lua.LString(p.ConfigText)); err != nil {
|
||||
log.Printf("plugin %s on_start: %v", p.Name, err)
|
||||
if _, err := callHook(p, "on_config", lua.LString(p.ConfigText)); err != nil {
|
||||
log.Printf("plugin %s on_config: %v", p.Name, err)
|
||||
}
|
||||
p.mu.Unlock()
|
||||
}
|
||||
// on_start runs after, sync or async depending on plugin config.
|
||||
for _, p := range m.GetPlugins() {
|
||||
if !p.Enabled {
|
||||
continue
|
||||
}
|
||||
hc, ok := p.hooks["on_start"]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if hc.Sync {
|
||||
p.mu.Lock()
|
||||
if _, err := callHook(p, "on_start"); err != nil {
|
||||
log.Printf("plugin %s on_start: %v", p.Name, err)
|
||||
}
|
||||
p.mu.Unlock()
|
||||
} else {
|
||||
go func(p *Plugin) {
|
||||
p.mu.Lock()
|
||||
if _, err := callHook(p, "on_start"); err != nil {
|
||||
log.Printf("plugin %s on_start: %v", p.Name, err)
|
||||
}
|
||||
p.mu.Unlock()
|
||||
}(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) RunOnQuit() {
|
||||
@@ -327,12 +360,37 @@ func (m *Manager) RunAsyncOnResponse(f *goproxy.Flow) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) RunOnHistoryEntry(e db.Entry) {
|
||||
// RunSyncOnHistoryEntry is called before DB insert; returns false to skip saving.
|
||||
func (m *Manager) RunSyncOnHistoryEntry(e db.Entry) bool {
|
||||
for _, p := range m.GetPlugins() {
|
||||
if !p.Enabled {
|
||||
continue
|
||||
}
|
||||
if _, ok := p.hooks["on_history_entry"]; !ok {
|
||||
hc, ok := p.hooks["on_history_entry"]
|
||||
if !ok || !hc.Sync {
|
||||
continue
|
||||
}
|
||||
p.mu.Lock()
|
||||
result, err := callHook(p, "on_history_entry", pushEntry(p.L, e))
|
||||
p.mu.Unlock()
|
||||
if err != nil {
|
||||
log.Printf("plugin %s on_history_entry: %v", p.Name, err)
|
||||
continue
|
||||
}
|
||||
if result == "skip" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *Manager) RunAsyncOnHistoryEntry(e db.Entry) {
|
||||
for _, p := range m.GetPlugins() {
|
||||
if !p.Enabled {
|
||||
continue
|
||||
}
|
||||
hc, ok := p.hooks["on_history_entry"]
|
||||
if !ok || hc.Sync {
|
||||
continue
|
||||
}
|
||||
go func(p *Plugin) {
|
||||
|
||||
+26
-14
@@ -11,10 +11,12 @@ type HookConfig struct {
|
||||
}
|
||||
|
||||
type Plugin struct {
|
||||
Name string
|
||||
FilePath string
|
||||
Enabled bool
|
||||
ConfigText string
|
||||
Name string
|
||||
Description string
|
||||
FilePath string
|
||||
Enabled bool
|
||||
ConfigText string
|
||||
Priority int
|
||||
|
||||
L *lua.LState
|
||||
mu sync.Mutex
|
||||
@@ -35,30 +37,40 @@ func (p *Plugin) HookConfig(name string) (HookConfig, bool) {
|
||||
}
|
||||
|
||||
type Info struct {
|
||||
Name string
|
||||
FilePath string
|
||||
Enabled bool
|
||||
ConfigText string
|
||||
Hooks map[string]HookConfig
|
||||
Name string
|
||||
Description string
|
||||
FilePath string
|
||||
Enabled bool
|
||||
ConfigText string
|
||||
Priority int
|
||||
Hooks map[string]HookConfig
|
||||
}
|
||||
|
||||
func (p *Plugin) Info() Info {
|
||||
p.mu.Lock()
|
||||
enabled := p.Enabled
|
||||
configText := p.ConfigText
|
||||
p.mu.Unlock()
|
||||
|
||||
hooks := make(map[string]HookConfig, len(p.hooks))
|
||||
for k, v := range p.hooks {
|
||||
hooks[k] = v
|
||||
}
|
||||
return Info{
|
||||
Name: p.Name,
|
||||
FilePath: p.FilePath,
|
||||
Enabled: p.Enabled,
|
||||
ConfigText: p.ConfigText,
|
||||
Hooks: hooks,
|
||||
Name: p.Name,
|
||||
Description: p.Description,
|
||||
FilePath: p.FilePath,
|
||||
Enabled: enabled,
|
||||
ConfigText: configText,
|
||||
Priority: p.Priority,
|
||||
Hooks: hooks,
|
||||
}
|
||||
}
|
||||
|
||||
type PluginNotifMsg struct {
|
||||
Title string
|
||||
Body string
|
||||
Kind string // "info", "success", "warning", "error"
|
||||
}
|
||||
|
||||
type PluginQuitMsg struct {
|
||||
|
||||
Reference in New Issue
Block a user