feat: add parser manage command
This commit is contained in:
97
client/bot/handlers/parser.go
Normal file
97
client/bot/handlers/parser.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"github.com/celestix/gotgproto/dispatcher"
|
||||
"github.com/celestix/gotgproto/ext"
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/krau/SaveAny-Bot/config"
|
||||
"github.com/krau/SaveAny-Bot/parsers"
|
||||
)
|
||||
|
||||
func handleParserCmd(ctx *ext.Context, u *ext.Update) error {
|
||||
args := strings.Split(u.EffectiveMessage.Text, " ")
|
||||
help := `
|
||||
用法:
|
||||
|
||||
/parser install <回复一个文件> - 安装解析器
|
||||
`
|
||||
if len(args) < 2 {
|
||||
ctx.Reply(u, ext.ReplyTextString(help), nil)
|
||||
return nil
|
||||
}
|
||||
switch args[1] {
|
||||
// case "list":
|
||||
// return handleParserListCmd(ctx, u)
|
||||
case "install":
|
||||
return handleParserInstallCmd(ctx, u)
|
||||
// case "uninstall":
|
||||
// return handleParserUninstallCmd(ctx, u)
|
||||
default:
|
||||
}
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
|
||||
func handleParserInstallCmd(ctx *ext.Context, u *ext.Update) error {
|
||||
if !config.C().Parser.PluginEnable {
|
||||
ctx.Reply(u, ext.ReplyTextString("解析器插件功能未启用"), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
if u.EffectiveMessage.ReplyToMessage == nil || u.EffectiveMessage.ReplyToMessage.Media == nil {
|
||||
ctx.Reply(u, ext.ReplyTextString("请回复一个包含解析器文件的消息"), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
media := u.EffectiveMessage.ReplyToMessage.Media
|
||||
document, ok := media.(*tg.MessageMediaDocument)
|
||||
if !ok {
|
||||
ctx.Reply(u, ext.ReplyTextString("回复的消息不包含有效的文件"), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
value, ok := document.GetDocument()
|
||||
if !ok {
|
||||
ctx.Reply(u, ext.ReplyTextString("回复的消息不包含有效的文件"), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
doc, ok := value.AsNotEmpty()
|
||||
if !ok {
|
||||
ctx.Reply(u, ext.ReplyTextString("回复的消息不包含有效的文件"), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
if !strings.HasPrefix(doc.MimeType, "text/") {
|
||||
ctx.Reply(u, ext.ReplyTextString("错误的文件类型"), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
if doc.Size > 1024*1024*10 {
|
||||
ctx.Reply(u, ext.ReplyTextString("文件过大"), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
var fileName string
|
||||
for _, attr := range doc.Attributes {
|
||||
if fileNameAttr, ok := attr.(*tg.DocumentAttributeFilename); ok {
|
||||
fileName = fileNameAttr.FileName
|
||||
break
|
||||
}
|
||||
}
|
||||
if fileName == "" {
|
||||
ctx.Reply(u, ext.ReplyTextString("无法获取文件名"), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
if !strings.HasSuffix(fileName, ".js") {
|
||||
ctx.Reply(u, ext.ReplyTextString("仅支持 .js 文件作为解析器"), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
data := bytes.NewBuffer(nil)
|
||||
_, err := ctx.DownloadMedia(media, ext.DownloadOutputStream{Writer: data}, nil)
|
||||
if err != nil {
|
||||
ctx.Reply(u, ext.ReplyTextString("文件下载失败: "+err.Error()), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
if err := parsers.AddPlugin(ctx, data.String(), fileName); err != nil {
|
||||
ctx.Reply(u, ext.ReplyTextString("插件安装失败: "+err.Error()), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
ctx.Reply(u, ext.ReplyTextString("插件安装成功: "+fileName), nil)
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
@@ -34,6 +34,7 @@ var CommandHandlers = []DescCommandHandler{
|
||||
{"fnametmpl", "设置文件命名模板", handleConfigFnameTmpl},
|
||||
{"update", "检查更新", handleUpdateCmd},
|
||||
{"help", "显示帮助", handleHelpCmd},
|
||||
{"parser", "管理解析器", handleParserCmd},
|
||||
}
|
||||
|
||||
func Register(disp dispatcher.Dispatcher) {
|
||||
|
||||
@@ -6,9 +6,11 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/krau/SaveAny-Bot/config"
|
||||
"github.com/krau/SaveAny-Bot/pkg/parser"
|
||||
)
|
||||
|
||||
@@ -98,6 +100,7 @@ func newJSParser(vm *goja.Runtime, canHandleFunc, parseFunc goja.Value, metadata
|
||||
return p
|
||||
}
|
||||
|
||||
// 加载指定文件夹下的所有 JS 解析器插件
|
||||
func LoadPlugins(ctx context.Context, dir string) error {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
@@ -130,3 +133,40 @@ func LoadPlugins(ctx context.Context, dir string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
pluginNameMu sync.Map
|
||||
)
|
||||
|
||||
func AddPlugin(ctx context.Context, code string, name string) error {
|
||||
value, _ := pluginNameMu.LoadOrStore(name, &sync.Mutex{})
|
||||
mu := value.(*sync.Mutex)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
return addPlugin(ctx, code, name)
|
||||
}
|
||||
|
||||
func addPlugin(ctx context.Context, code string, name string) error {
|
||||
logger := log.FromContext(ctx).WithPrefix(fmt.Sprintf("[plugin|parser]/%s", name))
|
||||
vm := goja.New()
|
||||
vm.Set("registerParser", jsRegisterParser(vm))
|
||||
vm.Set("console", jsConsole(logger))
|
||||
vm.Set("ghttp", jsGhttp(vm))
|
||||
vm.Set("playwright", jsPlaywright(vm, logger))
|
||||
if _, err := vm.RunString(code); err != nil {
|
||||
return fmt.Errorf("error loading plugin %s: %w", name, err)
|
||||
}
|
||||
dir := "plugins"
|
||||
configuredDirs := config.C().Parser.PluginDirs
|
||||
if len(configuredDirs) > 0 {
|
||||
dir = configuredDirs[0]
|
||||
}
|
||||
if err := os.MkdirAll(dir, 0755); err == nil {
|
||||
pluginPath := filepath.Join(dir, name)
|
||||
if err := os.WriteFile(pluginPath, []byte(code), 0644); err != nil {
|
||||
logger.Warn("Failed to save plugin file: " + err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package parsers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
@@ -19,45 +20,44 @@ func jsRegisterParser(vm *goja.Runtime) func(call goja.FunctionCall) goja.Value
|
||||
return func(call goja.FunctionCall) goja.Value {
|
||||
jsObj := call.Argument(0)
|
||||
if jsObj == nil || goja.IsUndefined(jsObj) || goja.IsNull(jsObj) {
|
||||
panic("registerParser expects an object { canHandle, parse }")
|
||||
return vm.NewGoError(errors.New("registerParser expects an object { canHandle, parse }"))
|
||||
}
|
||||
|
||||
obj := jsObj.ToObject(vm)
|
||||
if obj == nil {
|
||||
panic("registerParser: cannot convert argument to object")
|
||||
return vm.NewGoError(errors.New("registerParser expects an object { canHandle, parse }"))
|
||||
}
|
||||
metaValue := obj.Get("metadata")
|
||||
if metaValue == nil || goja.IsUndefined(metaValue) {
|
||||
panic("parser must provide metadata")
|
||||
return vm.NewGoError(errors.New("parser must provide metadata"))
|
||||
}
|
||||
var metadata PluginMeta
|
||||
if exported := metaValue.Export(); exported != nil {
|
||||
data, err := json.Marshal(exported)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to marshal metadata to JSON: %v", err))
|
||||
return vm.NewGoError(fmt.Errorf("failed to marshal metadata to JSON: %w", err))
|
||||
}
|
||||
if err := json.Unmarshal(data, &metadata); err != nil {
|
||||
panic(fmt.Sprintf("failed to unmarshal JSON to PluginMeta: %v", err))
|
||||
return vm.NewGoError(fmt.Errorf("failed to unmarshal JSON to PluginMeta: %w", err))
|
||||
}
|
||||
} else {
|
||||
panic("metadata cannot be null or undefined")
|
||||
return vm.NewGoError(errors.New("metadata cannot be null or undefined"))
|
||||
}
|
||||
|
||||
pluginV := semver.MustParse(metadata.Version)
|
||||
if pluginV.LT(MinimumParserVersion) {
|
||||
panic(fmt.Sprintf("parser version %s is not supported, must be at least %s", metadata.Version, MinimumParserVersion))
|
||||
return vm.NewGoError(fmt.Errorf("parser version %s is not supported, must be at least %s", metadata.Version, MinimumParserVersion))
|
||||
}
|
||||
if pluginV.Major > LatestParserVersion.Major {
|
||||
panic(fmt.Sprintf("parser major version %d is too new, latest supported major version is %d", pluginV.Major, LatestParserVersion.Major))
|
||||
log.Printf("warning: parser major version %d is newer than latest supported major version %d", pluginV.Major, LatestParserVersion.Major)
|
||||
}
|
||||
|
||||
handleFn := obj.Get("canHandle")
|
||||
parseFn := obj.Get("parse")
|
||||
if parseFn == nil || goja.IsUndefined(parseFn) {
|
||||
panic("parser must provide a parse function")
|
||||
return vm.NewGoError(errors.New("parser must provide a parse function"))
|
||||
}
|
||||
|
||||
parsers = append(parsers, newJSParser(vm, handleFn, parseFn, metadata))
|
||||
AddParser(newJSParser(vm, handleFn, parseFn, metadata))
|
||||
return goja.Undefined()
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,16 @@ var jsConsole = func(logger *log.Logger) map[string]any {
|
||||
logger.Info(args[0])
|
||||
}
|
||||
},
|
||||
"error": func(args ...any) {
|
||||
if len(args) == 0 {
|
||||
return
|
||||
}
|
||||
if len(args) > 1 {
|
||||
logger.Error(fmt.Sprint(args[0]), args[1:]...)
|
||||
} else {
|
||||
logger.Error(fmt.Sprint(args[0]))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user