2018-06-13 18:41:05 -04: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 04:11:24 -04:00
|
|
|
"maubot.xyz"
|
2018-06-13 18:41:05 -04:00
|
|
|
"maubot.xyz/database"
|
|
|
|
"maunium.net/go/gomatrix"
|
|
|
|
log "maunium.net/go/maulogger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
*gomatrix.Client
|
2018-06-14 04:11:24 -04:00
|
|
|
syncer *MaubotSyncer
|
2018-06-13 18:41:05 -04:00
|
|
|
|
|
|
|
DB *database.MatrixClient
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
Client: mxClient,
|
|
|
|
DB: db,
|
|
|
|
}
|
|
|
|
|
2018-06-14 04:11:24 -04:00
|
|
|
client.syncer = NewMaubotSyncer(client, client.Store)
|
|
|
|
client.Client.Syncer = client.syncer
|
2018-06-14 03:29:37 -04:00
|
|
|
|
2018-06-14 04:11:24 -04:00
|
|
|
client.AddEventHandler(maubot.StateMember, client.onJoin)
|
2018-06-13 18:41:05 -04:00
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2018-06-14 03:29:37 -04:00
|
|
|
func (client *Client) ParseEvent(evt *gomatrix.Event) *Event {
|
|
|
|
return &Event{
|
|
|
|
Client: client,
|
2018-06-14 04:11:24 -04:00
|
|
|
Event: evt,
|
2018-06-14 03:29:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 04:11:24 -04:00
|
|
|
func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) {
|
2018-06-14 06:36:53 -04:00
|
|
|
client.syncer.OnEventType(evt, func(evt *maubot.Event) bool {
|
|
|
|
if evt.Sender == client.UserID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return handler(evt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) GetEvent(roomID, eventID string) *maubot.Event {
|
|
|
|
evt, err := client.Client.GetEvent(roomID, eventID)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to get event %s @ %s: %v", eventID, roomID, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return client.ParseEvent(evt).Interface()
|
2018-06-13 18:41:05 -04:00
|
|
|
}
|
|
|
|
|
2018-06-14 04:11:24 -04:00
|
|
|
func (client *Client) onJoin(evt *maubot.Event) bool {
|
2018-06-14 03:29:37 -04:00
|
|
|
if !client.DB.AutoJoinRooms || evt.StateKey != client.DB.UserID {
|
|
|
|
return true
|
2018-06-13 18:41:05 -04:00
|
|
|
}
|
2018-06-14 04:11:24 -04:00
|
|
|
if evt.Content.Membership == "invite" {
|
2018-06-13 18:41:05 -04:00
|
|
|
client.JoinRoom(evt.RoomID)
|
2018-06-14 03:29:37 -04:00
|
|
|
return false
|
2018-06-13 18:41:05 -04:00
|
|
|
}
|
2018-06-14 03:29:37 -04:00
|
|
|
return true
|
2018-06-13 18:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) JoinRoom(roomID string) {
|
|
|
|
client.Client.JoinRoom(roomID, "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Sync() {
|
|
|
|
go func() {
|
|
|
|
err := client.Client.Sync()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Sync() in client", client.UserID, "errored:", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|