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-06-14 08:11:24 +00:00
|
|
|
"maubot.xyz"
|
2018-06-13 22:41:05 +00:00
|
|
|
"maunium.net/go/gomatrix"
|
2018-09-19 22:16:13 +00:00
|
|
|
"maunium.net/go/gomatrix/format"
|
2018-06-13 22:41:05 +00:00
|
|
|
)
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
type EventFuncsImpl struct {
|
|
|
|
*gomatrix.Event
|
2018-06-13 22:41:05 +00:00
|
|
|
Client *Client
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (client *Client) ParseEvent(mxEvent *gomatrix.Event) *maubot.Event {
|
|
|
|
if mxEvent == nil {
|
|
|
|
return nil
|
2018-06-14 07:29:37 +00:00
|
|
|
}
|
2018-09-19 22:16:13 +00:00
|
|
|
mxEvent.Content.RemoveReplyFallback()
|
|
|
|
return &maubot.Event{
|
|
|
|
EventFuncs: &EventFuncsImpl{
|
|
|
|
Event: mxEvent,
|
|
|
|
Client: client,
|
2018-06-14 07:29:37 +00:00
|
|
|
},
|
2018-09-19 22:16:13 +00:00
|
|
|
Event: mxEvent,
|
2018-06-14 07:29:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (evt *EventFuncsImpl) MarkRead() error {
|
2018-06-14 10:36:53 +00:00
|
|
|
return evt.Client.MarkRead(evt.RoomID, evt.ID)
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (evt *EventFuncsImpl) Reply(text string) (string, error) {
|
|
|
|
return evt.ReplyContent(format.RenderMarkdown(text))
|
2018-06-13 22:41:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (evt *EventFuncsImpl) ReplyContent(content gomatrix.Content) (string, error) {
|
|
|
|
content.SetReply(evt.Event)
|
|
|
|
return evt.SendContent(content)
|
2018-06-14 08:11:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (evt *EventFuncsImpl) SendMessage(text string) (string, error) {
|
|
|
|
return evt.SendContent(format.RenderMarkdown(text))
|
2018-06-14 08:11:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (evt *EventFuncsImpl) SendContent(content gomatrix.Content) (string, error) {
|
|
|
|
return evt.SendRawEvent(gomatrix.EventMessage, content)
|
2018-06-13 22:41:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 22:16:13 +00:00
|
|
|
func (evt *EventFuncsImpl) SendRawEvent(evtType gomatrix.EventType, content interface{}) (string, error) {
|
|
|
|
resp, err := evt.Client.SendMessageEvent(evt.RoomID, evtType, content)
|
2018-06-13 22:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.EventID, nil
|
|
|
|
}
|