Add command spec parsing/handler execution

This commit is contained in:
Tulir Asokan 2018-06-20 22:25:33 +03:00
parent 307a32e0c0
commit 4536fcfe62
9 changed files with 282 additions and 37 deletions

138
matrix/commands.go Normal file
View file

@ -0,0 +1,138 @@
// 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"
log "maunium.net/go/maulogger"
"maubot.xyz"
)
type ParsedCommand struct {
Name string
StartsWith string
Matches *regexp.Regexp
MatchAgainst string
MatchesEvent *maubot.Event
}
func (pc *ParsedCommand) parseCommandSyntax(command maubot.Command) error {
regexBuilder := &strings.Builder{}
swBuilder := &strings.Builder{}
argumentEncountered := false
regexBuilder.WriteRune('^')
words := strings.Split(command.Syntax, " ")
for i, word := range words {
argument, ok := command.Arguments[word]
if ok {
argumentEncountered = true
regex := argument.Matches
if argument.Required {
regex = fmt.Sprintf("(?:%s)?", regex)
}
regexBuilder.WriteString(regex)
} else {
if !argumentEncountered {
swBuilder.WriteString(word)
}
regexBuilder.WriteString(regexp.QuoteMeta(word))
}
if i < len(words) - 1 {
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
pc.Matches, err = regexp.Compile(command.Matches)
pc.MatchesEvent = command.MatchEvent
return err
}
func ParseSpec(spec *maubot.CommandSpec) (commands []*ParsedCommand) {
for _, command := range spec.Commands {
parsing := &ParsedCommand{
Name: command.Syntax,
}
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{
Name: command.Name,
}
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
}
}
}
func (pc *ParsedCommand) Match(evt *maubot.Event) bool {
matchAgainst, ok := deepGet(evt.Content.Raw, pc.MatchAgainst).(string)
if !ok {
matchAgainst = evt.Content.Body
}
return strings.HasPrefix(matchAgainst, pc.StartsWith) &&
pc.Matches.MatchString(matchAgainst) &&
(pc.MatchesEvent == nil || pc.MatchesEvent.Equals(evt))
}

View file

@ -25,9 +25,10 @@ import (
type Client struct {
*gomatrix.Client
syncer *MaubotSyncer
DB *database.MatrixClient
syncer *MaubotSyncer
handlers map[string][]maubot.CommandHandler
commands []*ParsedCommand
DB *database.MatrixClient
}
func NewClient(db *database.MatrixClient) (*Client, error) {
@ -37,8 +38,10 @@ func NewClient(db *database.MatrixClient) (*Client, error) {
}
client := &Client{
Client: mxClient,
DB: db,
Client: mxClient,
handlers: make(map[string][]maubot.CommandHandler),
commands: ParseSpec(db.Commands()),
DB: db,
}
client.syncer = NewMaubotSyncer(client, client.Store)
@ -60,21 +63,29 @@ func (client *Client) Proxy(owner string) *ClientProxy {
func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) {
client.syncer.OnEventType(evt, func(evt *maubot.Event) maubot.EventHandlerResult {
if evt.Sender == client.UserID {
return maubot.StopPropagation
return maubot.StopEventPropagation
}
return handler(evt)
})
}
func (client *Client) AddCommandHandler(evt string, handler maubot.CommandHandler) {
// TODO add command handler
func (client *Client) AddCommandHandler(owner, evt string, handler maubot.CommandHandler) {
log.Debugln("Registering command handler for event", evt, "by", owner)
list, ok := client.handlers[evt]
if !ok {
list = []maubot.CommandHandler{handler}
} else {
list = append(list, handler)
}
client.handlers[evt] = list
}
func (client *Client) SetCommandSpec(owner string, spec *maubot.CommandSpec) {
log.Debugln("Registering command spec for", owner, "on", client.UserID)
changed := client.DB.SetCommandSpec(owner, spec)
if changed {
client.commands = ParseSpec(client.DB.Commands())
log.Debugln("Command spec of", owner, "on", client.UserID, "updated.")
// TODO
}
}
@ -87,15 +98,38 @@ func (client *Client) GetEvent(roomID, eventID string) *maubot.Event {
return client.ParseEvent(evt).Event
}
func (client *Client) TriggerCommand(command *ParsedCommand, evt *maubot.Event) maubot.CommandHandlerResult {
handlers, ok := client.handlers[command.Name]
if !ok {
log.Warnf("Command %s triggered by %s doesn't have any handlers.", command.Name, evt.Sender)
return maubot.Continue
}
log.Debugf("Command %s on client %s triggered by %s\n", command.Name, client.UserID, evt.Sender)
for _, handler := range handlers {
result := handler(evt)
if result == maubot.StopCommandPropagation {
break
} else if result != maubot.Continue {
return result
}
}
return maubot.Continue
}
func (client *Client) onMessage(evt *maubot.Event) maubot.EventHandlerResult {
// TODO call command handlers
for _, command := range client.commands {
if command.Match(evt) {
return client.TriggerCommand(command, evt)
}
}
return maubot.Continue
}
func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult {
if client.DB.AutoJoinRooms && evt.StateKey == client.DB.UserID && evt.Content.Membership == "invite" {
client.JoinRoom(evt.RoomID)
return maubot.StopPropagation
return maubot.StopEventPropagation
}
return maubot.Continue
}
@ -120,6 +154,10 @@ type ClientProxy struct {
owner string
}
func (cp *ClientProxy) AddCommandHandler(evt string, handler maubot.CommandHandler) {
cp.hiddenClient.AddCommandHandler(cp.owner, evt, handler)
}
func (cp *ClientProxy) SetCommandSpec(spec *maubot.CommandSpec) {
cp.hiddenClient.SetCommandSpec(cp.owner, spec)
}

View file

@ -135,7 +135,7 @@ func (s *MaubotSyncer) notifyListeners(mxEvent *gomatrix.Event) {
return
}
for _, fn := range listeners {
if fn(event.Event) {
if fn(event.Event) == maubot.StopEventPropagation {
break
}
}