2018-06-20 19:25:33 +00:00
|
|
|
// maubot - A plugin-based Matrix bot system written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package matrix
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
"maunium.net/go/gomatrix"
|
2018-06-20 19:25:33 +00:00
|
|
|
log "maunium.net/go/maulogger"
|
|
|
|
|
|
|
|
"maubot.xyz"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ParsedCommand struct {
|
|
|
|
Name string
|
2018-06-20 20:28:01 +00:00
|
|
|
IsPassive bool
|
|
|
|
Arguments []string
|
2018-06-20 19:25:33 +00:00
|
|
|
StartsWith string
|
|
|
|
Matches *regexp.Regexp
|
|
|
|
MatchAgainst string
|
2018-09-19 22:16:13 +00:00
|
|
|
MatchesEvent interface{}
|
2018-06-20 19:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error {
|
|
|
|
regexBuilder := &strings.Builder{}
|
|
|
|
swBuilder := &strings.Builder{}
|
|
|
|
argumentEncountered := false
|
|
|
|
|
2018-06-21 19:08:33 +00:00
|
|
|
regexBuilder.WriteString("^!")
|
2018-06-20 19:25:33 +00:00
|
|
|
words := strings.Split(command.Syntax, " ")
|
|
|
|
for i, word := range words {
|
|
|
|
argument, ok := command.Arguments[word]
|
2018-06-20 20:28:01 +00:00
|
|
|
// TODO enable $ check?
|
|
|
|
if ok && len(word) > 0 /*&& word[0] == '$'*/ {
|
2018-06-20 19:25:33 +00:00
|
|
|
argumentEncountered = true
|
|
|
|
regex := argument.Matches
|
|
|
|
if argument.Required {
|
|
|
|
regex = fmt.Sprintf("(?:%s)?", regex)
|
|
|
|
}
|
2018-06-20 20:28:01 +00:00
|
|
|
pc.Arguments = append(pc.Arguments, word)
|
2018-06-20 19:25:33 +00:00
|
|
|
regexBuilder.WriteString(regex)
|
|
|
|
} else {
|
|
|
|
if !argumentEncountered {
|
|
|
|
swBuilder.WriteString(word)
|
|
|
|
}
|
|
|
|
regexBuilder.WriteString(regexp.QuoteMeta(word))
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:28:01 +00:00
|
|
|
if i < len(words)-1 {
|
2018-06-20 19:25:33 +00:00
|
|
|
if !argumentEncountered {
|
|
|
|
swBuilder.WriteRune(' ')
|
|
|
|
}
|
|
|
|
regexBuilder.WriteRune(' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
regexBuilder.WriteRune('$')
|
|
|
|
|
|
|
|
var err error
|
|
|
|
pc.StartsWith = swBuilder.String()
|
|
|
|
// Trim the extra space at the end added in the parse loop
|
|
|
|
pc.StartsWith = pc.StartsWith[:len(pc.StartsWith)-1]
|
|
|
|
pc.Matches, err = regexp.Compile(regexBuilder.String())
|
|
|
|
pc.MatchAgainst = "body"
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *ParsedCommand) parsePassiveCommandSyntax(command maubot.PassiveCommand) error {
|
|
|
|
pc.MatchAgainst = command.MatchAgainst
|
|
|
|
var err error
|
2018-06-20 20:28:01 +00:00
|
|
|
pc.Matches, err = regexp.Compile(fmt.Sprintf("(%s)", command.Matches))
|
2018-06-20 19:25:33 +00:00
|
|
|
pc.MatchesEvent = command.MatchEvent
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseSpec(spec *maubot.CommandSpec) (commands []*ParsedCommand) {
|
|
|
|
for _, command := range spec.Commands {
|
|
|
|
parsing := &ParsedCommand{
|
2018-06-20 20:28:01 +00:00
|
|
|
Name: command.Syntax,
|
|
|
|
IsPassive: false,
|
2018-06-20 19:25:33 +00:00
|
|
|
}
|
|
|
|
err := parsing.parseCommandSyntax(command)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to parse regex of command %s: %v\n", command.Syntax, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
commands = append(commands, parsing)
|
|
|
|
}
|
|
|
|
for _, command := range spec.PassiveCommands {
|
|
|
|
parsing := &ParsedCommand{
|
2018-06-20 20:28:01 +00:00
|
|
|
Name: command.Name,
|
|
|
|
IsPassive: true,
|
2018-06-20 19:25:33 +00:00
|
|
|
}
|
|
|
|
err := parsing.parsePassiveCommandSyntax(command)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to parse regex of passive command %s: %v\n", command.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
commands = append(commands, parsing)
|
|
|
|
}
|
|
|
|
return commands
|
|
|
|
}
|
|
|
|
|
|
|
|
func deepGet(from map[string]interface{}, path string) interface{} {
|
|
|
|
for {
|
|
|
|
dotIndex := strings.IndexRune(path, '.')
|
|
|
|
if dotIndex == -1 {
|
|
|
|
return from[path]
|
|
|
|
}
|
|
|
|
|
|
|
|
var key string
|
|
|
|
key, path = path[:dotIndex], path[dotIndex+1:]
|
|
|
|
var ok bool
|
|
|
|
from, ok = from[key].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (pc *ParsedCommand) MatchActive(evt *gomatrix.Event) bool {
|
2018-06-20 20:28:01 +00:00
|
|
|
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:]
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
command := &gomatrix.MatchedCommand{
|
|
|
|
Arguments: make(map[string]string),
|
|
|
|
}
|
2018-06-20 20:28:01 +00:00
|
|
|
for i, value := range match {
|
|
|
|
if i >= len(pc.Arguments) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
key := pc.Arguments[i]
|
2018-09-19 22:16:13 +00:00
|
|
|
command.Arguments[key] = value
|
2018-06-20 20:28:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
command.Matched = pc.Name
|
2018-06-20 20:28:01 +00:00
|
|
|
// TODO add evt.Content.Command.Target?
|
2018-09-19 22:16:13 +00:00
|
|
|
evt.Content.Command = command
|
2018-06-20 20:28:01 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (pc *ParsedCommand) MatchPassive(evt *gomatrix.Event) bool {
|
2018-06-20 19:25:33 +00:00
|
|
|
matchAgainst, ok := deepGet(evt.Content.Raw, pc.MatchAgainst).(string)
|
|
|
|
if !ok {
|
|
|
|
matchAgainst = evt.Content.Body
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
if pc.MatchesEvent != nil && !maubot.JSONLeftEquals(pc.MatchesEvent, evt) {
|
2018-06-20 20:28:01 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := pc.Matches.FindAllStringSubmatch(matchAgainst, -1)
|
|
|
|
if matches == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
if evt.Unsigned.PassiveCommand == nil {
|
|
|
|
evt.Unsigned.PassiveCommand = make(map[string]*gomatrix.MatchedPassiveCommand)
|
2018-06-20 20:28:01 +00:00
|
|
|
}
|
2018-09-19 22:16:13 +00:00
|
|
|
evt.Unsigned.PassiveCommand[pc.Name] = &gomatrix.MatchedPassiveCommand{
|
|
|
|
Captured: matches,
|
|
|
|
}
|
|
|
|
//evt.Unsigned.PassiveCommand.Matched = pc.Name
|
|
|
|
//evt.Unsigned.PassiveCommand.Captured = matches
|
2018-06-20 20:28:01 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (pc *ParsedCommand) Match(evt *gomatrix.Event) bool {
|
2018-06-20 20:28:01 +00:00
|
|
|
if pc.IsPassive {
|
|
|
|
return pc.MatchPassive(evt)
|
|
|
|
} else {
|
|
|
|
return pc.MatchActive(evt)
|
|
|
|
}
|
2018-06-20 19:25:33 +00:00
|
|
|
}
|