mirror of
https://github.com/maubot/maubot
synced 2025-08-29 23:30:39 +00:00
Add command spec parsing/handler execution
This commit is contained in:
parent
307a32e0c0
commit
4536fcfe62
9 changed files with 282 additions and 37 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue