2018-03-13 14:27:12 +01:00
|
|
|
// gomuks - A terminal Matrix client 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 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 General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
package matrix
|
2018-03-13 14:27:12 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-03-16 15:24:11 +01:00
|
|
|
"strings"
|
2018-03-14 23:14:39 +01:00
|
|
|
"time"
|
2018-03-13 14:27:12 +01:00
|
|
|
|
2018-03-15 17:25:16 +01:00
|
|
|
"maunium.net/go/gomatrix"
|
2018-03-18 20:24:03 +01:00
|
|
|
"maunium.net/go/gomuks/config"
|
|
|
|
"maunium.net/go/gomuks/interface"
|
|
|
|
rooms "maunium.net/go/gomuks/matrix/room"
|
|
|
|
"maunium.net/go/gomuks/ui/debug"
|
|
|
|
"maunium.net/go/gomuks/ui/widget"
|
2018-03-13 14:27:12 +01:00
|
|
|
)
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
type Container struct {
|
2018-03-13 20:58:43 +01:00
|
|
|
client *gomatrix.Client
|
2018-03-18 20:24:03 +01:00
|
|
|
gmx ifc.Gomuks
|
|
|
|
ui ifc.GomuksUI
|
|
|
|
config *config.Config
|
2018-03-13 14:27:12 +01:00
|
|
|
running bool
|
|
|
|
stop chan bool
|
2018-03-14 23:14:39 +01:00
|
|
|
|
|
|
|
typing int64
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func NewMatrixContainer(gmx ifc.Gomuks) *Container {
|
|
|
|
c := &Container{
|
2018-03-13 20:58:43 +01:00
|
|
|
config: gmx.Config(),
|
|
|
|
ui: gmx.UI(),
|
|
|
|
gmx: gmx,
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-13 20:58:43 +01:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) InitClient() error {
|
2018-03-13 14:27:12 +01:00
|
|
|
if len(c.config.HS) == 0 {
|
|
|
|
return fmt.Errorf("no homeserver in config")
|
|
|
|
}
|
|
|
|
|
2018-03-13 20:58:43 +01:00
|
|
|
if c.client != nil {
|
|
|
|
c.Stop()
|
|
|
|
c.client = nil
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:27:12 +01:00
|
|
|
var mxid, accessToken string
|
|
|
|
if c.config.Session != nil {
|
|
|
|
accessToken = c.config.Session.AccessToken
|
|
|
|
mxid = c.config.MXID
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2018-03-13 20:58:43 +01:00
|
|
|
c.client, err = gomatrix.NewClient(c.config.HS, mxid, accessToken)
|
2018-03-13 14:27:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.stop = make(chan bool, 1)
|
|
|
|
|
2018-03-20 11:16:32 +01:00
|
|
|
if c.config.Session != nil && len(c.config.Session.AccessToken) > 0 {
|
2018-03-13 14:27:12 +01:00
|
|
|
go c.Start()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) Initialized() bool {
|
2018-03-13 20:58:43 +01:00
|
|
|
return c.client != nil
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) Login(user, password string) error {
|
2018-03-13 20:58:43 +01:00
|
|
|
resp, err := c.client.Login(&gomatrix.ReqLogin{
|
2018-03-13 14:27:12 +01:00
|
|
|
Type: "m.login.password",
|
|
|
|
User: user,
|
|
|
|
Password: password,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-13 20:58:43 +01:00
|
|
|
c.client.SetCredentials(resp.UserID, resp.AccessToken)
|
2018-03-13 14:27:12 +01:00
|
|
|
c.config.MXID = resp.UserID
|
|
|
|
c.config.Save()
|
|
|
|
|
|
|
|
c.config.Session = c.config.NewSession(resp.UserID)
|
|
|
|
c.config.Session.AccessToken = resp.AccessToken
|
|
|
|
c.config.Session.Save()
|
|
|
|
|
|
|
|
go c.Start()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) Stop() {
|
2018-03-15 20:28:21 +01:00
|
|
|
if c.running {
|
|
|
|
c.stop <- true
|
|
|
|
c.client.StopSync()
|
|
|
|
}
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) Client() *gomatrix.Client {
|
|
|
|
return c.client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) UpdateRoomList() {
|
|
|
|
resp, err := c.client.JoinedRooms()
|
2018-03-13 20:58:43 +01:00
|
|
|
if err != nil {
|
2018-03-20 11:16:32 +01:00
|
|
|
respErr, _ := err.(gomatrix.HTTPError).WrappedError.(gomatrix.RespError)
|
|
|
|
if respErr.ErrCode == "M_UNKNOWN_TOKEN" {
|
|
|
|
c.OnLogout()
|
|
|
|
return
|
|
|
|
}
|
2018-03-18 20:24:03 +01:00
|
|
|
debug.Print("Error fetching room list:", err)
|
2018-03-18 16:34:42 +01:00
|
|
|
return
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
c.ui.MainView().SetRooms(resp.JoinedRooms)
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 11:16:32 +01:00
|
|
|
func (c *Container) OnLogout() {
|
|
|
|
c.Stop()
|
|
|
|
c.ui.SetView(ifc.ViewLogin)
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) OnLogin() {
|
2018-03-13 20:58:43 +01:00
|
|
|
c.client.Store = c.config.Session
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
syncer := NewGomuksSyncer(c.config.Session)
|
2018-03-13 14:27:12 +01:00
|
|
|
syncer.OnEventType("m.room.message", c.HandleMessage)
|
2018-03-16 15:24:11 +01:00
|
|
|
syncer.OnEventType("m.room.member", c.HandleMembership)
|
2018-03-14 23:14:39 +01:00
|
|
|
syncer.OnEventType("m.typing", c.HandleTyping)
|
2018-03-17 14:48:31 +01:00
|
|
|
c.client.Syncer = syncer
|
2018-03-13 14:27:12 +01:00
|
|
|
|
2018-03-17 14:48:31 +01:00
|
|
|
c.UpdateRoomList()
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) Start() {
|
2018-03-18 16:34:42 +01:00
|
|
|
defer c.gmx.Recover()
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
c.ui.SetView(ifc.ViewMain)
|
2018-03-18 16:34:42 +01:00
|
|
|
c.OnLogin()
|
2018-03-17 14:48:31 +01:00
|
|
|
|
2018-03-20 11:16:32 +01:00
|
|
|
if c.client == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
debug.Print("Starting sync...")
|
2018-03-17 14:48:31 +01:00
|
|
|
c.running = true
|
2018-03-13 14:27:12 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.stop:
|
2018-03-18 20:24:03 +01:00
|
|
|
debug.Print("Stopping sync...")
|
2018-03-13 14:27:12 +01:00
|
|
|
c.running = false
|
|
|
|
return
|
|
|
|
default:
|
2018-03-13 20:58:43 +01:00
|
|
|
if err := c.client.Sync(); err != nil {
|
2018-03-18 20:24:03 +01:00
|
|
|
debug.Print("Sync() errored", err)
|
2018-03-13 20:58:43 +01:00
|
|
|
} else {
|
2018-03-18 20:24:03 +01:00
|
|
|
debug.Print("Sync() returned without error")
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) HandleMessage(evt *gomatrix.Event) {
|
2018-03-18 16:34:42 +01:00
|
|
|
room, message := c.ui.MainView().ProcessMessageEvent(evt)
|
2018-03-17 14:48:31 +01:00
|
|
|
if room != nil {
|
2018-03-18 20:24:03 +01:00
|
|
|
room.AddMessage(message, widget.AppendMessage)
|
2018-03-20 11:16:32 +01:00
|
|
|
c.ui.Render()
|
2018-03-17 14:48:31 +01:00
|
|
|
}
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) HandleMembership(evt *gomatrix.Event) {
|
2018-03-18 16:34:42 +01:00
|
|
|
const Hour = 1 * 60 * 60 * 1000
|
|
|
|
if evt.Unsigned.Age > Hour {
|
2018-03-17 14:48:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
room, message := c.ui.MainView().ProcessMembershipEvent(evt, true)
|
2018-03-17 14:48:31 +01:00
|
|
|
if room != nil {
|
2018-03-18 16:34:42 +01:00
|
|
|
// TODO this shouldn't be necessary
|
2018-03-18 20:24:03 +01:00
|
|
|
room.Room.UpdateState(evt)
|
2018-03-18 16:34:42 +01:00
|
|
|
// TODO This should probably also be in a different place
|
2018-03-17 14:48:31 +01:00
|
|
|
room.UpdateUserList()
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
room.AddMessage(message, widget.AppendMessage)
|
2018-03-20 11:16:32 +01:00
|
|
|
c.ui.Render()
|
2018-03-16 15:24:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) HandleTyping(evt *gomatrix.Event) {
|
2018-03-15 17:21:14 +01:00
|
|
|
users := evt.Content["user_ids"].([]interface{})
|
|
|
|
|
|
|
|
strUsers := make([]string, len(users))
|
|
|
|
for i, user := range users {
|
|
|
|
strUsers[i] = user.(string)
|
|
|
|
}
|
2018-03-15 19:53:04 +01:00
|
|
|
c.ui.MainView().SetTyping(evt.RoomID, strUsers)
|
2018-03-14 23:14:39 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:39 +01:00
|
|
|
func (c *Container) SendMessage(roomID, text string) (string, error) {
|
2018-03-19 00:18:36 +01:00
|
|
|
defer c.gmx.Recover()
|
2018-03-15 18:45:52 +01:00
|
|
|
c.SendTyping(roomID, false)
|
2018-03-20 18:14:39 +01:00
|
|
|
resp, err := c.client.SendText(roomID, text)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.EventID, nil
|
2018-03-13 14:27:12 +01:00
|
|
|
}
|
2018-03-14 23:14:39 +01:00
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) SendTyping(roomID string, typing bool) {
|
2018-03-19 00:18:36 +01:00
|
|
|
defer c.gmx.Recover()
|
2018-03-18 20:24:03 +01:00
|
|
|
ts := time.Now().Unix()
|
|
|
|
if c.typing > ts && typing {
|
2018-03-14 23:14:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:45:52 +01:00
|
|
|
if typing {
|
|
|
|
c.client.UserTyping(roomID, true, 5000)
|
2018-03-18 20:24:03 +01:00
|
|
|
c.typing = ts + 5
|
2018-03-15 18:45:52 +01:00
|
|
|
} else {
|
|
|
|
c.client.UserTyping(roomID, false, 0)
|
|
|
|
c.typing = 0
|
|
|
|
}
|
2018-03-14 23:14:39 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) JoinRoom(roomID string) error {
|
2018-03-16 15:24:11 +01:00
|
|
|
if len(roomID) == 0 {
|
|
|
|
return fmt.Errorf("invalid room ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
server := ""
|
|
|
|
if roomID[0] == '!' {
|
|
|
|
server = roomID[strings.Index(roomID, ":")+1:]
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
_, err := c.client.JoinRoom(roomID, server, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) LeaveRoom(roomID string) error {
|
|
|
|
if len(roomID) == 0 {
|
|
|
|
return fmt.Errorf("invalid room ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := c.client.LeaveRoom(roomID)
|
2018-03-16 15:24:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) getState(roomID string) []*gomatrix.Event {
|
2018-03-15 18:45:52 +01:00
|
|
|
content := make([]*gomatrix.Event, 0)
|
|
|
|
err := c.client.StateEvent(roomID, "", "", &content)
|
|
|
|
if err != nil {
|
2018-03-18 20:24:03 +01:00
|
|
|
debug.Print("Error getting state of", roomID, err)
|
2018-03-14 23:14:39 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-03-15 18:45:52 +01:00
|
|
|
return content
|
2018-03-14 23:14:39 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.Event, string, error) {
|
2018-03-18 16:34:42 +01:00
|
|
|
resp, err := c.client.Messages(roomID, prevBatch, "", 'b', limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
return resp.Chunk, resp.End, nil
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (c *Container) GetRoom(roomID string) *rooms.Room {
|
2018-03-18 16:34:42 +01:00
|
|
|
room := c.config.Session.GetRoom(roomID)
|
2018-03-17 14:48:31 +01:00
|
|
|
if room != nil && len(room.State) == 0 {
|
2018-03-16 15:24:11 +01:00
|
|
|
events := c.getState(room.ID)
|
|
|
|
if events != nil {
|
|
|
|
for _, event := range events {
|
|
|
|
room.UpdateState(event)
|
|
|
|
}
|
2018-03-15 18:45:52 +01:00
|
|
|
}
|
2018-03-14 23:14:39 +01:00
|
|
|
}
|
2018-03-16 15:24:11 +01:00
|
|
|
return room
|
2018-03-14 23:14:39 +01:00
|
|
|
}
|