2018-06-13 22:41:05 +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 (
|
2018-09-20 21:35:24 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-06-14 08:11:24 +00:00
|
|
|
"maubot.xyz"
|
2018-06-13 22:41:05 +00:00
|
|
|
"maubot.xyz/database"
|
|
|
|
"maunium.net/go/gomatrix"
|
2018-09-20 21:35:24 +00:00
|
|
|
"maunium.net/go/gomatrix/format"
|
2018-06-13 22:41:05 +00:00
|
|
|
log "maunium.net/go/maulogger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
*gomatrix.Client
|
2018-06-20 19:25:33 +00:00
|
|
|
syncer *MaubotSyncer
|
|
|
|
handlers map[string][]maubot.CommandHandler
|
|
|
|
commands []*ParsedCommand
|
|
|
|
DB *database.MatrixClient
|
2018-06-13 22:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(db *database.MatrixClient) (*Client, error) {
|
|
|
|
mxClient, err := gomatrix.NewClient(db.Homeserver, db.UserID, db.AccessToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &Client{
|
2018-06-20 19:25:33 +00:00
|
|
|
Client: mxClient,
|
|
|
|
handlers: make(map[string][]maubot.CommandHandler),
|
|
|
|
commands: ParseSpec(db.Commands()),
|
|
|
|
DB: db,
|
2018-06-13 22:41:05 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 08:11:24 +00:00
|
|
|
client.syncer = NewMaubotSyncer(client, client.Store)
|
|
|
|
client.Client.Syncer = client.syncer
|
2018-06-14 07:29:37 +00:00
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
client.AddEventHandler(gomatrix.StateMember, client.onJoin)
|
|
|
|
client.AddEventHandler(gomatrix.EventMessage, client.onMessage)
|
2018-06-13 22:41:05 +00:00
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2018-06-18 22:25:47 +00:00
|
|
|
func (client *Client) Proxy(owner string) *ClientProxy {
|
|
|
|
return &ClientProxy{
|
|
|
|
hiddenClient: client,
|
|
|
|
owner: owner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (client *Client) AddEventHandler(evt gomatrix.EventType, handler maubot.EventHandler) {
|
2018-06-14 17:21:44 +00:00
|
|
|
client.syncer.OnEventType(evt, func(evt *maubot.Event) maubot.EventHandlerResult {
|
2018-06-14 10:36:53 +00:00
|
|
|
if evt.Sender == client.UserID {
|
2018-06-20 19:25:33 +00:00
|
|
|
return maubot.StopEventPropagation
|
2018-06-14 10:36:53 +00:00
|
|
|
}
|
|
|
|
return handler(evt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-20 19:25:33 +00:00
|
|
|
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
|
2018-06-18 22:25:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) SetCommandSpec(owner string, spec *maubot.CommandSpec) {
|
2018-06-20 19:25:33 +00:00
|
|
|
log.Debugln("Registering command spec for", owner, "on", client.UserID)
|
2018-06-18 22:25:47 +00:00
|
|
|
changed := client.DB.SetCommandSpec(owner, spec)
|
|
|
|
if changed {
|
2018-06-20 19:25:33 +00:00
|
|
|
client.commands = ParseSpec(client.DB.Commands())
|
2018-06-18 22:25:47 +00:00
|
|
|
log.Debugln("Command spec of", owner, "on", client.UserID, "updated.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 10:36:53 +00:00
|
|
|
func (client *Client) GetEvent(roomID, eventID string) *maubot.Event {
|
|
|
|
evt, err := client.Client.GetEvent(roomID, eventID)
|
|
|
|
if err != nil {
|
2018-06-14 17:21:44 +00:00
|
|
|
log.Warnf("Failed to get event %s @ %s: %v\n", eventID, roomID, err)
|
2018-06-14 10:36:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-19 22:16:13 +00:00
|
|
|
return client.ParseEvent(evt)
|
2018-06-13 22:41:05 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 19:25:33 +00:00
|
|
|
func (client *Client) TriggerCommand(command *ParsedCommand, evt *maubot.Event) maubot.CommandHandlerResult {
|
|
|
|
handlers, ok := client.handlers[command.Name]
|
|
|
|
if !ok {
|
2018-09-21 20:33:37 +00:00
|
|
|
log.Warnf("Command `%s` triggered by %s doesn't have any handlers.\n", command.Name, evt.Sender)
|
2018-06-20 19:25:33 +00:00
|
|
|
return maubot.Continue
|
|
|
|
}
|
2018-06-20 20:28:01 +00:00
|
|
|
|
2018-09-21 20:33:37 +00:00
|
|
|
log.Debugf("Command `%s` on client %s triggered by %s\n", command.Name, client.UserID, evt.Sender)
|
2018-06-20 19:25:33 +00:00
|
|
|
for _, handler := range handlers {
|
|
|
|
result := handler(evt)
|
|
|
|
if result == maubot.StopCommandPropagation {
|
|
|
|
break
|
|
|
|
} else if result != maubot.Continue {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return maubot.Continue
|
|
|
|
}
|
|
|
|
|
2018-06-18 22:25:47 +00:00
|
|
|
func (client *Client) onMessage(evt *maubot.Event) maubot.EventHandlerResult {
|
2018-06-20 19:25:33 +00:00
|
|
|
for _, command := range client.commands {
|
2018-09-19 22:16:13 +00:00
|
|
|
if command.Match(evt.Event) {
|
2018-06-20 19:25:33 +00:00
|
|
|
return client.TriggerCommand(command, evt)
|
|
|
|
}
|
|
|
|
}
|
2018-06-18 22:25:47 +00:00
|
|
|
return maubot.Continue
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:21:44 +00:00
|
|
|
func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult {
|
2018-09-19 22:16:13 +00:00
|
|
|
if client.DB.AutoJoinRooms && evt.GetStateKey() == client.DB.UserID && evt.Content.Membership == "invite" {
|
2018-06-13 22:41:05 +00:00
|
|
|
client.JoinRoom(evt.RoomID)
|
2018-06-20 19:25:33 +00:00
|
|
|
return maubot.StopEventPropagation
|
2018-06-13 22:41:05 +00:00
|
|
|
}
|
2018-06-14 17:21:44 +00:00
|
|
|
return maubot.Continue
|
2018-06-13 22:41:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 21:35:24 +00:00
|
|
|
func (client *Client) JoinRoom(roomID string) (resp *gomatrix.RespJoinRoom, err error) {
|
|
|
|
return client.Client.JoinRoom(roomID, "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) SendMessage(roomID, text string) (string, error) {
|
2018-09-21 13:09:26 +00:00
|
|
|
content := format.RenderMarkdown(text)
|
|
|
|
content.MsgType = gomatrix.MsgNotice
|
|
|
|
return client.SendContent(roomID, content)
|
2018-09-20 21:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) SendMessagef(roomID, text string, args ...interface{}) (string, error) {
|
2018-09-21 13:09:26 +00:00
|
|
|
content := format.RenderMarkdown(fmt.Sprintf(text, args...))
|
|
|
|
content.MsgType = gomatrix.MsgNotice
|
|
|
|
return client.SendContent(roomID, content)
|
2018-09-20 21:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) SendContent(roomID string, content gomatrix.Content) (string, error) {
|
|
|
|
return client.SendMessageEvent(roomID, gomatrix.EventMessage, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) SendMessageEvent(roomID string, evtType gomatrix.EventType, content interface{}) (string, error) {
|
|
|
|
resp, err := client.Client.SendMessageEvent(roomID, evtType, content)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.EventID, nil
|
2018-06-13 22:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Sync() {
|
|
|
|
go func() {
|
|
|
|
err := client.Client.Sync()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Sync() in client", client.UserID, "errored:", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2018-06-18 22:25:47 +00:00
|
|
|
|
|
|
|
type hiddenClient = Client
|
|
|
|
|
|
|
|
type ClientProxy struct {
|
|
|
|
*hiddenClient
|
|
|
|
owner string
|
|
|
|
}
|
|
|
|
|
2018-06-20 19:25:33 +00:00
|
|
|
func (cp *ClientProxy) AddCommandHandler(evt string, handler maubot.CommandHandler) {
|
|
|
|
cp.hiddenClient.AddCommandHandler(cp.owner, evt, handler)
|
|
|
|
}
|
|
|
|
|
2018-06-18 22:25:47 +00:00
|
|
|
func (cp *ClientProxy) SetCommandSpec(spec *maubot.CommandSpec) {
|
|
|
|
cp.hiddenClient.SetCommandSpec(cp.owner, spec)
|
|
|
|
}
|