aboutsummaryrefslogtreecommitdiff
path: root/matrix.go
blob: c7638527af7290697522e6f9019bea06b44dc900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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/>.

package main

import (
	"fmt"
	"time"

	"maunium.net/go/gomatrix"
)

type MatrixContainer struct {
	client  *gomatrix.Client
	gmx     Gomuks
	ui      *GomuksUI
	debug   DebugPrinter
	config  *Config
	running bool
	stop    chan bool

	typing int64
}

func NewMatrixContainer(gmx Gomuks) *MatrixContainer {
	c := &MatrixContainer{
		config: gmx.Config(),
		debug:  gmx.Debug(),
		ui:     gmx.UI(),
		gmx:    gmx,
	}

	return c
}

func (c *MatrixContainer) InitClient() error {
	if len(c.config.HS) == 0 {
		return fmt.Errorf("no homeserver in config")
	}

	if c.client != nil {
		c.Stop()
		c.client = nil
	}

	var mxid, accessToken string
	if c.config.Session != nil {
		accessToken = c.config.Session.AccessToken
		mxid = c.config.MXID
	}

	var err error
	c.client, err = gomatrix.NewClient(c.config.HS, mxid, accessToken)
	if err != nil {
		return err
	}

	c.stop = make(chan bool, 1)

	if c.config.Session != nil {
		go c.Start()
	}
	return nil
}

func (c *MatrixContainer) Initialized() bool {
	return c.client != nil
}

func (c *MatrixContainer) Login(user, password string) error {
	resp, err := c.client.Login(&gomatrix.ReqLogin{
		Type:     "m.login.password",
		User:     user,
		Password: password,
	})
	if err != nil {
		return err
	}
	c.client.SetCredentials(resp.UserID, resp.AccessToken)
	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
}

func (c *MatrixContainer) Stop() {
	c.stop <- true
	c.client.StopSync()
}

func (c *MatrixContainer) UpdateRoomList() {
	rooms, err := c.client.JoinedRooms()
	if err != nil {
		c.debug.Print(err)
	}

	c.ui.SetRoomList(rooms.JoinedRooms)
}

func (c *MatrixContainer) Start() {
	defer c.gmx.Recover()
	c.debug.Print("Starting sync...")
	c.running = true
	c.ui.SetView(ViewMain)
	c.client.Store = c.config.Session

	c.UpdateRoomList()

	syncer := c.client.Syncer.(*gomatrix.DefaultSyncer)
	syncer.OnEventType("m.room.message", c.HandleMessage)
	syncer.OnEventType("m.typing", c.HandleTyping)

	for {
		select {
		case <-c.stop:
			c.debug.Print("Stopping sync...")
			c.running = false
			return
		default:
			if err := c.client.Sync(); err != nil {
				c.debug.Print("Sync() errored", err)
			} else {
				c.debug.Print("Sync() returned without error")
			}
		}
	}
}

func (c *MatrixContainer) HandleMessage(evt *gomatrix.Event) {
	message, _ := evt.Content["body"].(string)
	c.ui.Append(evt.RoomID, evt.Sender, message)
}

func (c *MatrixContainer) HandleTyping(evt *gomatrix.Event) {
	users := evt.Content["user_ids"].([]interface{})
	c.debug.Print(users, "are typing")

	strUsers := make([]string, len(users))
	for i, user := range users {
		strUsers[i] = user.(string)
	}
	c.ui.SetTyping(evt.RoomID, strUsers...)
}

func (c *MatrixContainer) SendMessage(roomID, message string) {
	c.client.UserTyping(roomID, false, 0)
	c.client.SendText(roomID, message)
}

func (c *MatrixContainer) SendTyping(roomID string) {
	time := time.Now().Unix()
	if c.typing > time {
		return
	}

	c.client.UserTyping(roomID, true, 5000)
	c.typing = time + 5000
}

func (c *MatrixContainer) GetStateEvent(roomID, eventType, stateKey string) *gomatrix.Event {
	content := make(map[string]interface{})
	c.client.StateEvent(roomID, eventType, stateKey, &content)
	if len(content) == 0 {
		return nil
	}
	return &gomatrix.Event{
		StateKey: &stateKey,
		Sender: "",
		Type: eventType,
		Timestamp: 0,
		ID: "",
		RoomID: roomID,
		Content: content,
	}
}

func (c *MatrixContainer) GetAndUpdateStateEvent(room *gomatrix.Room, eventType, stateKey string) {
	if room == nil {
		return
	}
	event := c.GetStateEvent(room.ID, eventType, stateKey)
	if event != nil {
		room.UpdateState(event)
	}
}

func (c *MatrixContainer) UpdateRoomInfo(roomID string) {
	room := c.client.Store.LoadRoom(roomID)
	c.GetAndUpdateStateEvent(room, "m.room.name", "")
	c.GetAndUpdateStateEvent(room, "m.room.canonical_alias", "")
	c.GetAndUpdateStateEvent(room, "m.room.topic", "")
}