aboutsummaryrefslogtreecommitdiff
path: root/ui/view-main.go
blob: 9f4f247362e7cf4ac5021861b684236c06f3dbdc (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// 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 ui

import (
	"fmt"
	"strings"
	"time"
	"unicode"

	"maunium.net/go/gomatrix"
	"maunium.net/go/gomuks/config"
	"maunium.net/go/gomuks/debug"
	"maunium.net/go/gomuks/interface"
	"maunium.net/go/gomuks/lib/notification"
	"maunium.net/go/gomuks/matrix/pushrules"
	"maunium.net/go/gomuks/matrix/rooms"
	"maunium.net/go/gomuks/ui/messages/parser"
	"maunium.net/go/gomuks/ui/widget"
	"maunium.net/go/tcell"
	"maunium.net/go/tview"
	"bufio"
	"os"
)

type MainView struct {
	*tview.Flex

	roomList *RoomList
	roomView *tview.Pages
	rooms    map[string]*RoomView

	lastFocusTime time.Time

	matrix ifc.MatrixContainer
	gmx    ifc.Gomuks
	config *config.Config
	parent *GomuksUI

	hideUserList bool
	hideRoomList bool
	bareMessages bool
}

func (ui *GomuksUI) NewMainView() tview.Primitive {
	prefs := ui.gmx.Config().Preferences
	mainView := &MainView{
		Flex:     tview.NewFlex(),
		roomList: NewRoomList(),
		roomView: tview.NewPages(),
		rooms:    make(map[string]*RoomView),

		matrix: ui.gmx.Matrix(),
		gmx:    ui.gmx,
		config: ui.gmx.Config(),
		parent: ui,

		hideUserList: prefs.HideUserList,
		hideRoomList: prefs.HideRoomList,
		bareMessages: prefs.BareMessageView,
	}

	mainView.
		SetDirection(tview.FlexColumn).
		AddItem(mainView.roomList, 25, 0, false).
		AddItem(widget.NewBorder(), 1, 0, false).
		AddItem(mainView.roomView, 0, 1, true)
	mainView.BumpFocus(nil)

	ui.mainView = mainView

	return mainView
}

func (view *MainView) Draw(screen tcell.Screen) {
	if view.hideRoomList {
		view.roomView.SetRect(view.GetRect())
		view.roomView.Draw(screen)
	} else {
		view.Flex.Draw(screen)
	}
}

func (view *MainView) BumpFocus(roomView *RoomView) {
	view.lastFocusTime = time.Now()
	view.MarkRead(roomView)
}

func (view *MainView) MarkRead(roomView *RoomView) {
	if roomView != nil && roomView.Room.HasNewMessages() && roomView.MessageView().ScrollOffset == 0 {
		msgList := roomView.MessageView().messages
		msg := msgList[len(msgList)-1]
		roomView.Room.MarkRead(msg.ID())
		view.matrix.MarkRead(roomView.Room.ID, msg.ID())
	}
}

func (view *MainView) InputChanged(roomView *RoomView, text string) {
	if len(text) == 0 {
		go view.matrix.SendTyping(roomView.Room.ID, false)
	} else if text[0] != '/' {
		go view.matrix.SendTyping(roomView.Room.ID, true)
	}
}

func findWordToTabComplete(text string) string {
	output := ""
	runes := []rune(text)
	for i := len(runes) - 1; i >= 0; i-- {
		if unicode.IsSpace(runes[i]) {
			break
		}
		output = string(runes[i]) + output
	}
	return output
}

func (view *MainView) InputSubmit(roomView *RoomView, text string) {
	if len(text) == 0 {
		return
	} else if text[0] == '/' {
		args := strings.SplitN(text, " ", 2)
		command := strings.ToLower(args[0])
		args = args[1:]
		go view.HandleCommand(roomView, command, args)
	} else {
		view.SendMessage(roomView, text)
	}
	roomView.SetInputText("")
}

func (view *MainView) SendMessage(roomView *RoomView, text string) {
	tempMessage := roomView.NewTempMessage("m.text", text)
	go view.sendTempMessage(roomView, tempMessage, text)
}

func (view *MainView) sendTempMessage(roomView *RoomView, tempMessage ifc.Message, text string) {
	defer debug.Recover()
	debug.Print("Sending message", tempMessage.Type(), text)
	eventID, err := view.matrix.SendMarkdownMessage(roomView.Room.ID, tempMessage.Type(), text)
	if err != nil {
		tempMessage.SetState(ifc.MessageStateFailed)
		if httpErr, ok := err.(gomatrix.HTTPError); ok {
			if respErr, ok := httpErr.WrappedError.(gomatrix.RespError); ok {
				// Show shorter version if available
				err = respErr
			}
		}
		roomView.AddServiceMessage(fmt.Sprintf("Failed to send message: %v", err))
		view.parent.Render()
	} else {
		debug.Print("Event ID received:", eventID)
		roomView.MessageView().UpdateMessageID(tempMessage, eventID)
	}
}

func (view *MainView) HandleCommand(roomView *RoomView, command string, args []string) {
	defer debug.Recover()
	debug.Print("Handling command", command, args)
	switch command {
	case "/me":
		text := strings.Join(args, " ")
		tempMessage := roomView.NewTempMessage("m.emote", text)
		go view.sendTempMessage(roomView, tempMessage, text)
		view.parent.Render()
	case "/quit":
		view.gmx.Stop()
	case "/clearcache":
		view.config.Clear()
		view.gmx.Stop()
	case "/panic":
		panic("This is a test panic.")
	case "/part", "/leave":
		err := view.matrix.LeaveRoom(roomView.Room.ID)
		debug.Print("Leave room error:", err)
		if err == nil {
			view.RemoveRoom(roomView.Room)
		}
	case "/uitoggle":
		if len(args) == 0 {
			roomView.AddServiceMessage("Usage: /uitoggle <rooms/users/baremessages>")
			break
		}
		switch args[0] {
		case "rooms":
			view.hideRoomList = !view.hideRoomList
			view.config.Preferences.HideRoomList = view.hideRoomList
		case "users":
			view.hideUserList = !view.hideUserList
			view.config.Preferences.HideUserList = view.hideUserList
		case "baremessages":
			view.bareMessages = !view.bareMessages
			view.config.Preferences.BareMessageView = view.bareMessages
		default:
			roomView.AddServiceMessage("Usage: /uitoggle <rooms/users/baremessages>")
			return
		}
		view.parent.Render()
		view.parent.Render()
		go view.matrix.SendPreferencesToMatrix()
	case "/join":
		if len(args) == 0 {
			roomView.AddServiceMessage("Usage: /join <room>")
			break
		}
		identifer := args[0]
		server := ""
		if len(args) > 1 {
			server = args[1]
		}
		room, err := view.matrix.JoinRoom(identifer, server)
		debug.Print("Join room error:", err)
		if err == nil {
			view.AddRoom(room)
		}
	case "/logout":
		view.matrix.Logout()
	default:
		roomView.AddServiceMessage("Unknown command.")
	}
}

func (view *MainView) ShowBare(roomView *RoomView) {
	_, height := view.parent.app.GetScreen().Size()
	view.parent.app.Suspend(func() {
		print("\033[2J\033[0;0H")
		// We don't know how much space there exactly is. Too few messages looks weird,
		// and too many messages shouldn't cause any problems, so we just show too many.
		height *= 2
		fmt.Println(roomView.MessageView().CapturePlaintext(height))
		fmt.Println("Press enter to return to normal mode.")
		reader := bufio.NewReader(os.Stdin)
		reader.ReadRune()
		print("\033[2J\033[0;0H")
	})
}

func (view *MainView) KeyEventHandler(roomView *RoomView, key *tcell.EventKey) *tcell.EventKey {
	view.BumpFocus(roomView)

	k := key.Key()
	c := key.Rune()
	if key.Modifiers() == tcell.ModCtrl || key.Modifiers() == tcell.ModAlt {
		switch {
		case k == tcell.KeyDown:
			view.SwitchRoom(view.roomList.Next())
		case k == tcell.KeyUp:
			view.SwitchRoom(view.roomList.Previous())
		case k == tcell.KeyEnter:
			searchModal := NewFuzzySearchModal(view, 42, 12)
			view.parent.views.AddPage("fuzzy-search-modal", searchModal, true, true)
			view.parent.app.SetFocus(searchModal)
		case c == 'l':
			view.ShowBare(roomView)
		default:
			return key
		}
	} else if k == tcell.KeyPgUp || k == tcell.KeyPgDn || k == tcell.KeyUp || k == tcell.KeyDown || k == tcell.KeyEnd || k == tcell.KeyHome {
		msgView := roomView.MessageView()

		if msgView.IsAtTop() && (k == tcell.KeyPgUp || k == tcell.KeyUp) {
			go view.LoadHistory(roomView.Room.ID)
		}

		switch k {
		case tcell.KeyPgUp:
			msgView.AddScrollOffset(msgView.Height() / 2)
		case tcell.KeyPgDn:
			msgView.AddScrollOffset(-msgView.Height() / 2)
		case tcell.KeyUp:
			msgView.AddScrollOffset(1)
		case tcell.KeyDown:
			msgView.AddScrollOffset(-1)
		case tcell.KeyHome:
			msgView.AddScrollOffset(msgView.TotalHeight())
		case tcell.KeyEnd:
			msgView.AddScrollOffset(-msgView.TotalHeight())
		}
	} else {
		return key
	}
	return nil
}

const WheelScrollOffsetDiff = 3

func isInArea(x, y int, p tview.Primitive) bool {
	rx, ry, rw, rh := p.GetRect()
	return x >= rx && y >= ry && x < rx+rw && y < ry+rh
}

func (view *MainView) MouseEventHandler(roomView *RoomView, event *tcell.EventMouse) *tcell.EventMouse {
	if event.Buttons() == tcell.ButtonNone || event.HasMotion() {
		return event
	}
	view.BumpFocus(roomView)

	msgView := roomView.MessageView()
	x, y := event.Position()

	switch {
	case isInArea(x, y, msgView):
		mx, my, _, _ := msgView.GetRect()
		switch event.Buttons() {
		case tcell.WheelUp:
			if msgView.IsAtTop() {
				go view.LoadHistory(roomView.Room.ID)
			} else {
				msgView.AddScrollOffset(WheelScrollOffsetDiff)

				view.parent.Render()
			}
		case tcell.WheelDown:
			msgView.AddScrollOffset(-WheelScrollOffsetDiff)
			view.parent.Render()
			view.MarkRead(roomView)
		default:
			if msgView.HandleClick(x-mx, y-my, event.Buttons()) {
				view.parent.Render()
			}
		}
	case isInArea(x, y, view.roomList):
		switch event.Buttons() {
		case tcell.WheelUp:
			view.roomList.AddScrollOffset(-WheelScrollOffsetDiff)
			view.parent.Render()
		case tcell.WheelDown:
			view.roomList.AddScrollOffset(WheelScrollOffsetDiff)
			view.parent.Render()
		case tcell.Button1:
			_, rly, _, _ := msgView.GetRect()
			line := y - rly + 1
			switchToTag, switchToRoom := view.roomList.HandleClick(x, line, event.Modifiers() == tcell.ModCtrl)
			if switchToRoom != nil {
				view.SwitchRoom(switchToTag, switchToRoom)
			} else {
				view.parent.Render()
			}
		}
	default:
		debug.Print("Unhandled mouse event:", event.Buttons(), event.Modifiers(), x, y)
	}
	return event
}

func (view *MainView) SwitchRoom(tag string, room *rooms.Room) {
	if room == nil {
		return
	}

	view.roomView.SwitchToPage(room.ID)
	roomView := view.rooms[room.ID]
	if roomView == nil {
		debug.Print("Tried to switch to non-nil room with nil roomView!")
		debug.Print(tag, room)
		return
	}
	view.MarkRead(roomView)
	view.roomList.SetSelected(tag, room)
	view.parent.app.SetFocus(view)
	view.parent.Render()
}

func (view *MainView) Focus(delegate func(p tview.Primitive)) {
	room := view.roomList.SelectedRoom()
	if room != nil {
		roomView, ok := view.rooms[room.ID]
		if ok {
			delegate(roomView)
		}
	}
}

func (view *MainView) SaveAllHistory() {
	for _, room := range view.rooms {
		err := room.SaveHistory(view.config.HistoryDir)
		if err != nil {
			debug.Printf("Failed to save history of %s: %v", room.Room.GetTitle(), err)
		}
	}
}

func (view *MainView) addRoomPage(room *rooms.Room) {
	if !view.roomView.HasPage(room.ID) {
		roomView := NewRoomView(view, room).
			SetInputSubmitFunc(view.InputSubmit).
			SetInputChangedFunc(view.InputChanged).
			SetInputCapture(view.KeyEventHandler).
			SetMouseCapture(view.MouseEventHandler)
		view.rooms[room.ID] = roomView
		view.roomView.AddPage(room.ID, roomView, true, false)
		roomView.UpdateUserList()

		_, err := roomView.LoadHistory(view.matrix, view.config.HistoryDir)
		if err != nil {
			debug.Printf("Failed to load history of %s: %v", roomView.Room.GetTitle(), err)
		}
	}
}

func (view *MainView) GetRoom(roomID string) ifc.RoomView {
	room, ok := view.rooms[roomID]
	if !ok {
		view.AddRoom(view.matrix.GetRoom(roomID))
		room, ok := view.rooms[roomID]
		if !ok {
			return nil
		}
		return room
	}
	return room
}

func (view *MainView) AddRoom(room *rooms.Room) {
	if view.roomList.Contains(room.ID) {
		debug.Print("Add aborted (room exists)", room.ID, room.GetTitle())
		return
	}
	debug.Print("Adding", room.ID, room.GetTitle())
	view.roomList.Add(room)
	view.addRoomPage(room)
	if !view.roomList.HasSelected() {
		view.SwitchRoom(view.roomList.First())
	}
}

func (view *MainView) RemoveRoom(room *rooms.Room) {
	roomView := view.GetRoom(room.ID)
	if roomView == nil {
		debug.Print("Remove aborted (not found)", room.ID, room.GetTitle())
		return
	}
	debug.Print("Removing", room.ID, room.GetTitle())

	view.roomList.Remove(room)
	view.SwitchRoom(view.roomList.Selected())

	view.roomView.RemovePage(room.ID)
	delete(view.rooms, room.ID)

	view.parent.Render()
}

func (view *MainView) SetRooms(rooms map[string]*rooms.Room) {
	view.roomList.Clear()
	view.roomView.Clear()
	view.rooms = make(map[string]*RoomView)
	for _, room := range rooms {
		if room.HasLeft {
			continue
		}
		view.roomList.Add(room)
		view.addRoomPage(room)
	}
	view.SwitchRoom(view.roomList.First())
}

func (view *MainView) UpdateTags(room *rooms.Room) {
	if !view.roomList.Contains(room.ID) {
		return
	}
	view.roomList.Remove(room)
	view.roomList.Add(room)
}

func (view *MainView) SetTyping(room string, users []string) {
	roomView, ok := view.rooms[room]
	if ok {
		roomView.SetTyping(users)
		view.parent.Render()
	}
}

func sendNotification(room *rooms.Room, sender, text string, critical, sound bool) {
	if room.GetTitle() != sender {
		sender = fmt.Sprintf("%s (%s)", sender, room.GetTitle())
	}
	debug.Printf("Sending notification with body \"%s\" from %s in room ID %s (critical=%v, sound=%v)", text, sender, room.ID, critical, sound)
	notification.Send(sender, text, critical, sound)
}

func (view *MainView) NotifyMessage(room *rooms.Room, message ifc.Message, should pushrules.PushActionArrayShould) {
	view.roomList.Bump(room)
	if message.SenderID() == view.config.UserID {
		return
	}
	// Whether or not the room where the message came is the currently shown room.
	isCurrent := room == view.roomList.SelectedRoom()
	// Whether or not the terminal window is focused.
	recentlyFocused := time.Now().Add(-30 * time.Second).Before(view.lastFocusTime)
	isFocused := time.Now().Add(-5 * time.Second).Before(view.lastFocusTime)

	// Whether or not the push rules say this message should be notified about.
	shouldNotify := should.Notify || !should.NotifySpecified

	if !isCurrent || !isFocused {
		// The message is not in the current room, show new message status in room list.
		room.AddUnread(message.ID(), shouldNotify, should.Highlight)
	} else {
		view.matrix.MarkRead(room.ID, message.ID())
	}

	if shouldNotify && !recentlyFocused {
		// Push rules say notify and the terminal is not focused, send desktop notification.
		shouldPlaySound := should.PlaySound && should.SoundName == "default"
		sendNotification(room, message.Sender(), message.NotificationContent(), should.Highlight, shouldPlaySound)
	}

	message.SetIsHighlight(should.Highlight)
}

func (view *MainView) InitialSyncDone() {
	view.roomList.Clear()
	for _, room := range view.rooms {
		view.roomList.Add(room.Room)
		room.UpdateUserList()
	}
}

func (view *MainView) LoadHistory(room string) {
	defer debug.Recover()
	roomView := view.rooms[room]

	batch := roomView.Room.PrevBatch
	lockTime := time.Now().Unix() + 1

	roomView.Room.LockHistory()
	roomView.MessageView().LoadingMessages = true
	defer func() {
		roomView.Room.UnlockHistory()
		roomView.MessageView().LoadingMessages = false
	}()

	// There's no clean way to try to lock a mutex, so we just check if we still
	// want to continue after we get the lock. This function should always be ran
	// in a goroutine, so the blocking doesn't matter.
	if time.Now().Unix() >= lockTime || batch != roomView.Room.PrevBatch {
		return
	}

	debug.Print("Fetching history for", room, "starting from", batch)
	history, prevBatch, err := view.matrix.GetHistory(roomView.Room.ID, batch, 50)
	if err != nil {
		roomView.AddServiceMessage("Failed to fetch history")
		debug.Print("Failed to fetch history for", roomView.Room.ID, err)
		return
	}
	roomView.Room.PrevBatch = prevBatch
	for _, evt := range history {
		message := view.ParseEvent(roomView, &evt)
		if message != nil {
			roomView.AddMessage(message, ifc.PrependMessage)
		}
	}
	err = roomView.SaveHistory(view.config.HistoryDir)
	if err != nil {
		debug.Printf("Failed to save history of %s: %v", roomView.Room.GetTitle(), err)
	}
	view.config.PutRoom(roomView.Room)
	view.parent.Render()
}

func (view *MainView) ParseEvent(roomView ifc.RoomView, evt *gomatrix.Event) ifc.Message {
	return parser.ParseEvent(view.matrix, roomView.MxRoom(), evt)
}