mirror of
https://github.com/42wim/matterbridge
synced 2024-11-07 09:20:23 +00:00
1bb39eba87
TengoModifyMessage allows you to specify the location of a tengo (https://github.com/d5/tengo/) script. This script will receive every incoming message and can be used to modify the Username and the Text of that message. The script will have the following global variables: to modify: msgUsername and msgText to read: msgChannel and msgAccount The script is reloaded on every message, so you can modify the script on the fly. Example script can be found in https://github.com/42wim/matterbridge/tree/master/gateway/bench.tengo and https://github.com/42wim/matterbridge/tree/master/contrib/example.tengo The example below will check if the text contains blah and if so, it'll replace the text and the username of that message. text := import("text") if text.re_match("blah",msgText) { msgText="replaced by this" msgUsername="fakeuser" } More information about tengo on: https://github.com/d5/tengo/blob/master/docs/tutorial.md and https://github.com/d5/tengo/blob/master/docs/stdlib.md
196 lines
4.5 KiB
Go
196 lines
4.5 KiB
Go
package stdlib
|
|
|
|
import (
|
|
"regexp"
|
|
|
|
"github.com/d5/tengo/objects"
|
|
)
|
|
|
|
func makeTextRegexp(re *regexp.Regexp) *objects.ImmutableMap {
|
|
return &objects.ImmutableMap{
|
|
Value: map[string]objects.Object{
|
|
// match(text) => bool
|
|
"match": &objects.UserFunction{
|
|
Value: func(args ...objects.Object) (ret objects.Object, err error) {
|
|
if len(args) != 1 {
|
|
err = objects.ErrWrongNumArguments
|
|
return
|
|
}
|
|
|
|
s1, ok := objects.ToString(args[0])
|
|
if !ok {
|
|
err = objects.ErrInvalidArgumentType{
|
|
Name: "first",
|
|
Expected: "string(compatible)",
|
|
Found: args[0].TypeName(),
|
|
}
|
|
return
|
|
}
|
|
|
|
if re.MatchString(s1) {
|
|
ret = objects.TrueValue
|
|
} else {
|
|
ret = objects.FalseValue
|
|
}
|
|
|
|
return
|
|
},
|
|
},
|
|
|
|
// find(text) => array(array({text:,begin:,end:}))/undefined
|
|
// find(text, maxCount) => array(array({text:,begin:,end:}))/undefined
|
|
"find": &objects.UserFunction{
|
|
Value: func(args ...objects.Object) (ret objects.Object, err error) {
|
|
numArgs := len(args)
|
|
if numArgs != 1 && numArgs != 2 {
|
|
err = objects.ErrWrongNumArguments
|
|
return
|
|
}
|
|
|
|
s1, ok := objects.ToString(args[0])
|
|
if !ok {
|
|
err = objects.ErrInvalidArgumentType{
|
|
Name: "first",
|
|
Expected: "string(compatible)",
|
|
Found: args[0].TypeName(),
|
|
}
|
|
return
|
|
}
|
|
|
|
if numArgs == 1 {
|
|
m := re.FindStringSubmatchIndex(s1)
|
|
if m == nil {
|
|
ret = objects.UndefinedValue
|
|
return
|
|
}
|
|
|
|
arr := &objects.Array{}
|
|
for i := 0; i < len(m); i += 2 {
|
|
arr.Value = append(arr.Value, &objects.ImmutableMap{Value: map[string]objects.Object{
|
|
"text": &objects.String{Value: s1[m[i]:m[i+1]]},
|
|
"begin": &objects.Int{Value: int64(m[i])},
|
|
"end": &objects.Int{Value: int64(m[i+1])},
|
|
}})
|
|
}
|
|
|
|
ret = &objects.Array{Value: []objects.Object{arr}}
|
|
|
|
return
|
|
}
|
|
|
|
i2, ok := objects.ToInt(args[1])
|
|
if !ok {
|
|
err = objects.ErrInvalidArgumentType{
|
|
Name: "second",
|
|
Expected: "int(compatible)",
|
|
Found: args[1].TypeName(),
|
|
}
|
|
return
|
|
}
|
|
m := re.FindAllStringSubmatchIndex(s1, i2)
|
|
if m == nil {
|
|
ret = objects.UndefinedValue
|
|
return
|
|
}
|
|
|
|
arr := &objects.Array{}
|
|
for _, m := range m {
|
|
subMatch := &objects.Array{}
|
|
for i := 0; i < len(m); i += 2 {
|
|
subMatch.Value = append(subMatch.Value, &objects.ImmutableMap{Value: map[string]objects.Object{
|
|
"text": &objects.String{Value: s1[m[i]:m[i+1]]},
|
|
"begin": &objects.Int{Value: int64(m[i])},
|
|
"end": &objects.Int{Value: int64(m[i+1])},
|
|
}})
|
|
}
|
|
|
|
arr.Value = append(arr.Value, subMatch)
|
|
}
|
|
|
|
ret = arr
|
|
|
|
return
|
|
},
|
|
},
|
|
|
|
// replace(src, repl) => string
|
|
"replace": &objects.UserFunction{
|
|
Value: func(args ...objects.Object) (ret objects.Object, err error) {
|
|
if len(args) != 2 {
|
|
err = objects.ErrWrongNumArguments
|
|
return
|
|
}
|
|
|
|
s1, ok := objects.ToString(args[0])
|
|
if !ok {
|
|
err = objects.ErrInvalidArgumentType{
|
|
Name: "first",
|
|
Expected: "string(compatible)",
|
|
Found: args[0].TypeName(),
|
|
}
|
|
return
|
|
}
|
|
|
|
s2, ok := objects.ToString(args[1])
|
|
if !ok {
|
|
err = objects.ErrInvalidArgumentType{
|
|
Name: "second",
|
|
Expected: "string(compatible)",
|
|
Found: args[1].TypeName(),
|
|
}
|
|
return
|
|
}
|
|
|
|
ret = &objects.String{Value: re.ReplaceAllString(s1, s2)}
|
|
|
|
return
|
|
},
|
|
},
|
|
|
|
// split(text) => array(string)
|
|
// split(text, maxCount) => array(string)
|
|
"split": &objects.UserFunction{
|
|
Value: func(args ...objects.Object) (ret objects.Object, err error) {
|
|
numArgs := len(args)
|
|
if numArgs != 1 && numArgs != 2 {
|
|
err = objects.ErrWrongNumArguments
|
|
return
|
|
}
|
|
|
|
s1, ok := objects.ToString(args[0])
|
|
if !ok {
|
|
err = objects.ErrInvalidArgumentType{
|
|
Name: "first",
|
|
Expected: "string(compatible)",
|
|
Found: args[0].TypeName(),
|
|
}
|
|
return
|
|
}
|
|
|
|
var i2 = -1
|
|
if numArgs > 1 {
|
|
i2, ok = objects.ToInt(args[1])
|
|
if !ok {
|
|
err = objects.ErrInvalidArgumentType{
|
|
Name: "second",
|
|
Expected: "int(compatible)",
|
|
Found: args[1].TypeName(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
arr := &objects.Array{}
|
|
for _, s := range re.Split(s1, i2) {
|
|
arr.Value = append(arr.Value, &objects.String{Value: s})
|
|
}
|
|
|
|
ret = arr
|
|
|
|
return
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|