Fill event values with matched command stuff
This commit is contained in:
parent
4536fcfe62
commit
ad5e2ef72f
3 changed files with 75 additions and 9 deletions
10
matrix.go
10
matrix.go
|
@ -110,6 +110,8 @@ type Unsigned struct {
|
||||||
PrevSender string `json:"prev_sender,omitempty"`
|
PrevSender string `json:"prev_sender,omitempty"`
|
||||||
ReplacesState string `json:"replaces_state,omitempty"`
|
ReplacesState string `json:"replaces_state,omitempty"`
|
||||||
Age int64 `json:"age"`
|
Age int64 `json:"age"`
|
||||||
|
|
||||||
|
PassiveCommand MatchedPassiveCommand `json:"m.passive_command,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (unsigned Unsigned) Equals(otherUnsigned *Unsigned) bool {
|
func (unsigned Unsigned) Equals(otherUnsigned *Unsigned) bool {
|
||||||
|
@ -171,6 +173,14 @@ type MatchedCommand struct {
|
||||||
Arguments map[string]string `json:"arguments"`
|
Arguments map[string]string `json:"arguments"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MatchedPassiveCommand struct {
|
||||||
|
Matched string `json:"matched"`
|
||||||
|
Values []string `json:"captured"`
|
||||||
|
|
||||||
|
BackCompatCommand string `json:"command"`
|
||||||
|
BackCompatArguments map[string]string `json:"arguments"`
|
||||||
|
}
|
||||||
|
|
||||||
type RelatesTo struct {
|
type RelatesTo struct {
|
||||||
InReplyTo InReplyTo `json:"m.in_reply_to,omitempty"`
|
InReplyTo InReplyTo `json:"m.in_reply_to,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,8 @@ import (
|
||||||
|
|
||||||
type ParsedCommand struct {
|
type ParsedCommand struct {
|
||||||
Name string
|
Name string
|
||||||
|
IsPassive bool
|
||||||
|
Arguments []string
|
||||||
StartsWith string
|
StartsWith string
|
||||||
Matches *regexp.Regexp
|
Matches *regexp.Regexp
|
||||||
MatchAgainst string
|
MatchAgainst string
|
||||||
|
@ -43,12 +45,14 @@ func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error {
|
||||||
words := strings.Split(command.Syntax, " ")
|
words := strings.Split(command.Syntax, " ")
|
||||||
for i, word := range words {
|
for i, word := range words {
|
||||||
argument, ok := command.Arguments[word]
|
argument, ok := command.Arguments[word]
|
||||||
if ok {
|
// TODO enable $ check?
|
||||||
|
if ok && len(word) > 0 /*&& word[0] == '$'*/ {
|
||||||
argumentEncountered = true
|
argumentEncountered = true
|
||||||
regex := argument.Matches
|
regex := argument.Matches
|
||||||
if argument.Required {
|
if argument.Required {
|
||||||
regex = fmt.Sprintf("(?:%s)?", regex)
|
regex = fmt.Sprintf("(?:%s)?", regex)
|
||||||
}
|
}
|
||||||
|
pc.Arguments = append(pc.Arguments, word)
|
||||||
regexBuilder.WriteString(regex)
|
regexBuilder.WriteString(regex)
|
||||||
} else {
|
} else {
|
||||||
if !argumentEncountered {
|
if !argumentEncountered {
|
||||||
|
@ -78,7 +82,7 @@ func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error {
|
||||||
func (pc *ParsedCommand) parsePassiveCommandSyntax(command maubot.PassiveCommand) error {
|
func (pc *ParsedCommand) parsePassiveCommandSyntax(command maubot.PassiveCommand) error {
|
||||||
pc.MatchAgainst = command.MatchAgainst
|
pc.MatchAgainst = command.MatchAgainst
|
||||||
var err error
|
var err error
|
||||||
pc.Matches, err = regexp.Compile(command.Matches)
|
pc.Matches, err = regexp.Compile(fmt.Sprintf("(%s)", command.Matches))
|
||||||
pc.MatchesEvent = command.MatchEvent
|
pc.MatchesEvent = command.MatchEvent
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -87,6 +91,7 @@ func ParseSpec(spec *maubot.CommandSpec) (commands []*ParsedCommand) {
|
||||||
for _, command := range spec.Commands {
|
for _, command := range spec.Commands {
|
||||||
parsing := &ParsedCommand{
|
parsing := &ParsedCommand{
|
||||||
Name: command.Syntax,
|
Name: command.Syntax,
|
||||||
|
IsPassive: false,
|
||||||
}
|
}
|
||||||
err := parsing.parseCommandSyntax(command)
|
err := parsing.parseCommandSyntax(command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -98,6 +103,7 @@ func ParseSpec(spec *maubot.CommandSpec) (commands []*ParsedCommand) {
|
||||||
for _, command := range spec.PassiveCommands {
|
for _, command := range spec.PassiveCommands {
|
||||||
parsing := &ParsedCommand{
|
parsing := &ParsedCommand{
|
||||||
Name: command.Name,
|
Name: command.Name,
|
||||||
|
IsPassive: true,
|
||||||
}
|
}
|
||||||
err := parsing.parsePassiveCommandSyntax(command)
|
err := parsing.parsePassiveCommandSyntax(command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -126,13 +132,62 @@ func deepGet(from map[string]interface{}, path string) interface{} {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pc *ParsedCommand) Match(evt *maubot.Event) bool {
|
func (pc *ParsedCommand) MatchActive(evt *maubot.Event) bool {
|
||||||
|
if !strings.HasPrefix(evt.Content.Body, pc.StartsWith) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
match := pc.Matches.FindStringSubmatch(evt.Content.Body)
|
||||||
|
if match == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// First element is whole content
|
||||||
|
match = match[1:]
|
||||||
|
|
||||||
|
evt.Content.Command.Arguments = make(map[string]string)
|
||||||
|
for i, value := range match {
|
||||||
|
if i >= len(pc.Arguments) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
key := pc.Arguments[i]
|
||||||
|
evt.Content.Command.Arguments[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
evt.Content.Command.Matched = pc.Name
|
||||||
|
// TODO add evt.Content.Command.Target?
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *ParsedCommand) MatchPassive(evt *maubot.Event) bool {
|
||||||
matchAgainst, ok := deepGet(evt.Content.Raw, pc.MatchAgainst).(string)
|
matchAgainst, ok := deepGet(evt.Content.Raw, pc.MatchAgainst).(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
matchAgainst = evt.Content.Body
|
matchAgainst = evt.Content.Body
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.HasPrefix(matchAgainst, pc.StartsWith) &&
|
if pc.MatchesEvent != nil && !pc.MatchesEvent.Equals(evt) {
|
||||||
pc.Matches.MatchString(matchAgainst) &&
|
return false
|
||||||
(pc.MatchesEvent == nil || pc.MatchesEvent.Equals(evt))
|
}
|
||||||
|
|
||||||
|
matches := pc.Matches.FindAllStringSubmatch(matchAgainst, -1)
|
||||||
|
if matches == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
values := make([]string, len(matches))
|
||||||
|
for i, match := range matches {
|
||||||
|
values[i] = match[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
evt.Unsigned.PassiveCommand.Matched = pc.Name
|
||||||
|
evt.Unsigned.PassiveCommand.Values = values
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *ParsedCommand) Match(evt *maubot.Event) bool {
|
||||||
|
if pc.IsPassive {
|
||||||
|
return pc.MatchPassive(evt)
|
||||||
|
} else {
|
||||||
|
return pc.MatchActive(evt)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,6 +104,7 @@ func (client *Client) TriggerCommand(command *ParsedCommand, evt *maubot.Event)
|
||||||
log.Warnf("Command %s triggered by %s doesn't have any handlers.", command.Name, evt.Sender)
|
log.Warnf("Command %s triggered by %s doesn't have any handlers.", command.Name, evt.Sender)
|
||||||
return maubot.Continue
|
return maubot.Continue
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("Command %s on client %s triggered by %s\n", command.Name, client.UserID, evt.Sender)
|
log.Debugf("Command %s on client %s triggered by %s\n", command.Name, client.UserID, evt.Sender)
|
||||||
for _, handler := range handlers {
|
for _, handler := range handlers {
|
||||||
result := handler(evt)
|
result := handler(evt)
|
||||||
|
|
Loading…
Reference in a new issue