2024-01-01 16:52:18 +00:00
|
|
|
// Package variables contains an actor and database client to store
|
|
|
|
// handle variables
|
2022-09-10 11:39:07 +00:00
|
|
|
package variables
|
2021-06-29 12:08:26 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-28 15:27:24 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-06-29 12:08:26 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2023-11-25 19:23:34 +00:00
|
|
|
"gorm.io/gorm"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/internal/helpers"
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/database"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-06-29 12:08:26 +00:00
|
|
|
)
|
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
var (
|
|
|
|
db database.Connector
|
|
|
|
formatMessage plugins.MsgFormatter
|
2021-09-22 13:36:45 +00:00
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
ptrBoolFalse = func(b bool) *bool { return &b }(false)
|
|
|
|
ptrStringEmpty = func(s string) *string { return &s }("")
|
|
|
|
)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
|
|
|
//
|
2023-08-25 21:37:37 +00:00
|
|
|
//nolint:funlen // Function contains only documentation registration
|
2024-01-01 16:52:18 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) (err error) {
|
2022-09-10 11:39:07 +00:00
|
|
|
db = args.GetDatabaseConnector()
|
2024-01-01 16:52:18 +00:00
|
|
|
if err = db.DB().AutoMigrate(&variable{}); err != nil {
|
2022-09-10 11:39:07 +00:00
|
|
|
return errors.Wrap(err, "applying schema migration")
|
|
|
|
}
|
|
|
|
|
2023-11-25 19:23:34 +00:00
|
|
|
args.RegisterCopyDatabaseFunc("variable", func(src, target *gorm.DB) error {
|
2024-03-27 23:15:14 +00:00
|
|
|
return database.CopyObjects(src, target, &variable{})
|
2023-11-25 19:23:34 +00:00
|
|
|
})
|
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
args.RegisterActor("setvariable", func() plugins.Actor { return &actorSetVariable{} })
|
2022-09-10 11:39:07 +00:00
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
2021-09-22 13:36:45 +00:00
|
|
|
Description: "Modify variable contents",
|
|
|
|
Name: "Modify Variable",
|
|
|
|
Type: "setvariable",
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Name of the variable to update",
|
|
|
|
Key: "variable",
|
|
|
|
Name: "Variable",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "false",
|
|
|
|
Description: "Clear variable content and unset the variable",
|
|
|
|
Key: "clear",
|
|
|
|
Name: "Clear",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeBool,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Value to set the variable to",
|
|
|
|
Key: "set",
|
|
|
|
Name: "Set Content",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-08-28 15:27:24 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err = args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
|
2021-08-28 15:27:24 +00:00
|
|
|
Description: "Returns the value as a plain string",
|
|
|
|
HandlerFunc: routeActorSetVarGetValue,
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Module: "setvariable",
|
|
|
|
Name: "Get Variable Value",
|
|
|
|
Path: "/{name}",
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
|
|
|
|
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Name of the variable to query",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
},
|
2024-01-01 16:52:18 +00:00
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("registering API route: %w", err)
|
|
|
|
}
|
2021-08-28 15:27:24 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err = args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
|
2021-08-28 15:27:24 +00:00
|
|
|
Description: "Updates the value of the variable",
|
|
|
|
HandlerFunc: routeActorSetVarSetValue,
|
|
|
|
Method: http.MethodPatch,
|
|
|
|
Module: "setvariable",
|
|
|
|
Name: "Set Variable Value",
|
|
|
|
Path: "/{name}",
|
|
|
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Value to set for the given variable",
|
|
|
|
Name: "value",
|
|
|
|
Required: true,
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
2021-10-23 15:22:58 +00:00
|
|
|
RequiresWriteAuth: true,
|
2021-08-28 15:27:24 +00:00
|
|
|
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Name of the variable to update",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
},
|
2024-01-01 16:52:18 +00:00
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("registering API route: %w", err)
|
|
|
|
}
|
2022-09-10 11:39:07 +00:00
|
|
|
|
|
|
|
args.RegisterTemplateFunction("variable", plugins.GenericTemplateFunctionGetter(func(name string, defVal ...string) (string, error) {
|
2024-01-01 16:52:18 +00:00
|
|
|
value, err := getVariable(db, name)
|
2022-09-10 11:39:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "getting variable")
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == "" && len(defVal) > 0 {
|
|
|
|
return defVal[0], nil
|
|
|
|
}
|
|
|
|
return value, nil
|
2023-08-25 21:37:37 +00:00
|
|
|
}), plugins.TemplateFuncDocumentation{
|
|
|
|
Description: "Returns the variable value or default in case it is empty",
|
|
|
|
Syntax: "variable <name> [default]",
|
|
|
|
Example: &plugins.TemplateFuncDocumentationExample{
|
|
|
|
Template: `{{ variable "foo" "fallback" }} - {{ variable "unsetvar" "fallback" }}`,
|
|
|
|
FakedOutput: "test - fallback",
|
|
|
|
},
|
|
|
|
})
|
2022-09-10 11:39:07 +00:00
|
|
|
|
|
|
|
return nil
|
2021-06-29 12:08:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
type actorSetVariable struct{}
|
2021-06-29 12:08:26 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actorSetVariable) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
varName, err := formatMessage(attrs.MustString("variable", nil), m, r, eventData)
|
2021-06-29 12:08:26 +00:00
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing variable name")
|
2021-06-29 12:08:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
if attrs.MustBool("clear", ptrBoolFalse) {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2024-01-01 16:52:18 +00:00
|
|
|
removeVariable(db, varName),
|
2021-06-29 12:08:26 +00:00
|
|
|
"removing variable",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
value, err := formatMessage(attrs.MustString("set", ptrStringEmpty), m, r, eventData)
|
2021-06-29 12:08:26 +00:00
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing value")
|
2021-06-29 12:08:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2024-01-01 16:52:18 +00:00
|
|
|
setVariable(db, varName, value),
|
2021-06-29 12:08:26 +00:00
|
|
|
"setting variable",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actorSetVariable) IsAsync() bool { return false }
|
|
|
|
func (actorSetVariable) Name() string { return "setvariable" }
|
2021-08-28 15:27:24 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actorSetVariable) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
|
|
|
if err = attrs.ValidateSchema(
|
|
|
|
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "variable", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "clear", Type: fieldcollection.SchemaFieldTypeBool}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "set", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
2024-04-08 13:56:12 +00:00
|
|
|
fieldcollection.MustHaveNoUnknowFields,
|
2024-04-03 19:00:28 +00:00
|
|
|
helpers.SchemaValidateTemplateField(tplValidator, "set", "variable"),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
2022-10-31 16:26:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-28 15:27:24 +00:00
|
|
|
func routeActorSetVarGetValue(w http.ResponseWriter, r *http.Request) {
|
2024-01-01 16:52:18 +00:00
|
|
|
vc, err := getVariable(db, mux.Vars(r)["name"])
|
2022-09-10 11:39:07 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "getting value").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-28 15:27:24 +00:00
|
|
|
w.Header().Set("Content-Type", "text-plain")
|
2024-06-09 10:44:41 +00:00
|
|
|
http.Error(w, vc, http.StatusOK)
|
2021-08-28 15:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func routeActorSetVarSetValue(w http.ResponseWriter, r *http.Request) {
|
2024-01-01 16:52:18 +00:00
|
|
|
if err := setVariable(db, mux.Vars(r)["name"], r.FormValue("value")); err != nil {
|
2021-08-28 15:27:24 +00:00
|
|
|
http.Error(w, errors.Wrap(err, "updating value").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|