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"
|
|
|
|
|
2021-06-29 12:08:26 +00:00
|
|
|
"github.com/go-irc/irc"
|
2021-08-28 15:27:24 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-06-29 12:08:26 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
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 }("")
|
|
|
|
)
|
|
|
|
|
2023-08-25 21:37:37 +00:00
|
|
|
//nolint:funlen // Function contains only documentation registration
|
2022-09-10 11:39:07 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
db = args.GetDatabaseConnector()
|
2022-10-22 22:08:02 +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")
|
|
|
|
}
|
|
|
|
|
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
|
|
|
args.RegisterActor("setvariable", func() plugins.Actor { return &ActorSetVariable{} })
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-09-10 11:39:07 +00:00
|
|
|
|
|
|
|
args.RegisterTemplateFunction("variable", plugins.GenericTemplateFunctionGetter(func(name string, defVal ...string) (string, error) {
|
2022-10-22 22:08:02 +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
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
type ActorSetVariable struct{}
|
2021-06-29 12:08:26 +00:00
|
|
|
|
2023-03-24 21:32:00 +00:00
|
|
|
func (a ActorSetVariable) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.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(
|
2022-10-22 22:08:02 +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(
|
2022-10-22 22:08:02 +00:00
|
|
|
SetVariable(db, varName, value),
|
2021-06-29 12:08:26 +00:00
|
|
|
"setting variable",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ActorSetVariable) IsAsync() bool { return false }
|
|
|
|
func (a ActorSetVariable) Name() string { return "setvariable" }
|
2021-08-28 15:27:24 +00:00
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
func (a ActorSetVariable) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *plugins.FieldCollection) (err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
if v, err := attrs.String("variable"); err != nil || v == "" {
|
|
|
|
return errors.New("variable name must be non-empty string")
|
|
|
|
}
|
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
for _, field := range []string{"set", "variable"} {
|
|
|
|
if err = tplValidator(attrs.MustString(field, ptrStringEmpty)); err != nil {
|
|
|
|
return errors.Wrapf(err, "validating %s template", field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-10-22 22:08:02 +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")
|
2022-09-10 11:39:07 +00:00
|
|
|
fmt.Fprint(w, vc)
|
2021-08-28 15:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func routeActorSetVarSetValue(w http.ResponseWriter, r *http.Request) {
|
2022-10-22 22:08:02 +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)
|
|
|
|
}
|