forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversation.go
741 lines (612 loc) · 17.9 KB
/
conversation.go
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
package gui
import (
"fmt"
"log"
"sync"
"time"
"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/twstrike/gotk3adapter/gdki"
"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/twstrike/gotk3adapter/glibi"
"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/twstrike/gotk3adapter/gtki"
"github.com/twstrike/coyim/client"
"github.com/twstrike/coyim/i18n"
rosters "github.com/twstrike/coyim/roster"
"github.com/twstrike/coyim/session/access"
"github.com/twstrike/coyim/ui"
)
var (
enableWindow glibi.Signal
disableWindow glibi.Signal
)
type conversationView interface {
showIdentityVerificationWarning(u *gtkUI)
updateSecurityWarning()
show(userInitiated bool)
appendStatus(from string, timestamp time.Time, show, showStatus string, gone bool)
appendMessage(from string, timestamp time.Time, encrypted bool, message []byte, outgoing bool)
displayNotification(notification string)
displayNotificationVerifiedOrNot(notificationV, notificationNV string)
setEnabled(enabled bool)
isVisible() bool
}
type conversationWindow struct {
*conversationPane
win gtki.Window
parentWin gtki.Window
}
type conversationPane struct {
to string
account *account
widget gtki.Box
menubar gtki.MenuBar
entry gtki.TextView
entryScroll gtki.ScrolledWindow
history gtki.TextView
scrollHistory gtki.ScrolledWindow
notificationArea gtki.Box
securityWarning gtki.InfoBar
fingerprintWarning gtki.InfoBar
// The window to set dialogs transient for
transientParent gtki.Window
sync.Mutex
marks []*timedMark
hidden bool
shiftEnterSends bool
afterNewMessage func()
currentPeer func() (*rosters.Peer, bool)
}
type tags struct {
table gtki.TextTagTable
}
func (u *gtkUI) getTags() *tags {
if u.tags == nil {
u.tags = u.newTags()
}
return u.tags
}
func (u *gtkUI) newTags() *tags {
cs := u.currentColorSet()
t := new(tags)
t.table, _ = g.gtk.TextTagTableNew()
outgoingUser, _ := g.gtk.TextTagNew("outgoingUser")
outgoingUser.SetProperty("foreground", cs.conversationOutgoingUserForeground)
incomingUser, _ := g.gtk.TextTagNew("incomingUser")
incomingUser.SetProperty("foreground", cs.conversationIncomingUserForeground)
outgoingText, _ := g.gtk.TextTagNew("outgoingText")
outgoingText.SetProperty("foreground", cs.conversationOutgoingTextForeground)
incomingText, _ := g.gtk.TextTagNew("incomingText")
outgoingText.SetProperty("foreground", cs.conversationIncomingTextForeground)
statusText, _ := g.gtk.TextTagNew("statusText")
statusText.SetProperty("foreground", cs.conversationStatusTextForeground)
t.table.Add(outgoingUser)
t.table.Add(incomingUser)
t.table.Add(outgoingText)
t.table.Add(incomingText)
t.table.Add(statusText)
return t
}
func (t *tags) createTextBuffer() gtki.TextBuffer {
buf, _ := g.gtk.TextBufferNew(t.table)
return buf
}
func getTextBufferFrom(e gtki.TextView) gtki.TextBuffer {
tb, _ := e.GetBuffer()
return tb
}
func getTextFrom(e gtki.TextView) string {
tb := getTextBufferFrom(e)
return tb.GetText(tb.GetStartIter(), tb.GetEndIter(), false)
}
func insertEnter(e gtki.TextView) {
tb := getTextBufferFrom(e)
tb.Insert(tb.GetEndIter(), "\n")
}
func clearIn(e gtki.TextView) {
tb := getTextBufferFrom(e)
tb.Delete(tb.GetStartIter(), tb.GetEndIter())
}
func (conv *conversationWindow) isVisible() bool {
return conv.win.HasToplevelFocus()
}
func (conv *conversationPane) onSendMessageSignal() {
conv.entry.SetEditable(false)
text := getTextFrom(conv.entry)
clearIn(conv.entry)
conv.entry.SetEditable(true)
if text != "" {
sendError := conv.sendMessage(text)
if sendError != nil {
log.Printf(i18n.Local("Failed to generate OTR message: %s\n"), sendError.Error())
}
}
conv.entry.GrabFocus()
}
func (conv *conversationPane) currentResource() string {
resource := ""
conv.withCurrentPeer(func(p *rosters.Peer) {
resource = p.ResourceToUse()
})
return resource
}
func (conv *conversationPane) onStartOtrSignal() {
//TODO: enable/disable depending on the conversation's encryption state
session := conv.account.session
c, _ := session.ConversationManager().EnsureConversationWith(conv.to, conv.currentResource())
err := c.StartEncryptedChat(session, conv.currentResource())
if err != nil {
//TODO: notify failure
}
}
func (conv *conversationPane) onEndOtrSignal() {
//TODO: errors
//TODO: enable/disable depending on the conversation's encryption state
session := conv.account.session
c, ok := session.ConversationManager().GetConversationWith(conv.to, conv.currentResource())
if !ok {
return
}
err := c.EndEncryptedChat(session, conv.currentResource())
if err != nil {
log.Printf(i18n.Local("Failed to terminate the encrypted chat: %s\n"), err.Error())
}
}
func (conv *conversationPane) onVerifyFpSignal() {
switch verifyFingerprintDialog(conv.account, conv.to, conv.currentResource(), conv.transientParent) {
case gtki.RESPONSE_YES:
conv.removeIdentityVerificationWarning()
}
}
func (conv *conversationPane) onConnect() {
conv.entry.SetEditable(true)
conv.entry.SetSensitive(true)
}
func (conv *conversationPane) onDisconnect() {
conv.entry.SetEditable(false)
conv.entry.SetSensitive(false)
}
func countVisibleLines(v gtki.TextView) uint {
lines := uint(1)
iter := getTextBufferFrom(v).GetStartIter()
for v.ForwardDisplayLine(iter) {
lines++
}
return lines
}
func (conv *conversationPane) calculateHeight(lines uint) uint {
return lines * 2 * getFontSizeFrom(conv.entry)
}
func (conv *conversationPane) doPotentialEntryResize() {
lines := countVisibleLines(conv.entry)
scroll := true
if lines > 3 {
scroll = false
lines = 3
}
conv.entryScroll.SetProperty("height-request", conv.calculateHeight(lines))
if scroll {
scrollToTop(conv.entryScroll)
}
}
func createConversationPane(account *account, uid string, ui *gtkUI, transientParent gtki.Window) *conversationPane {
builder := newBuilder("ConversationPane")
cp := &conversationPane{
to: uid,
account: account,
transientParent: transientParent,
shiftEnterSends: ui.settings.GetShiftEnterForSend(),
afterNewMessage: func() {},
currentPeer: func() (*rosters.Peer, bool) {
return ui.getPeer(account, uid)
},
}
builder.getItems(
"box", &cp.widget,
"history", &cp.history,
"historyScroll", &cp.scrollHistory,
"message", &cp.entry,
"notification-area", &cp.notificationArea,
"security-warning", &cp.securityWarning,
"menubar", &cp.menubar,
"messageScroll", &cp.entryScroll,
)
builder.ConnectSignals(map[string]interface{}{
"on_start_otr_signal": cp.onStartOtrSignal,
"on_end_otr_signal": cp.onEndOtrSignal,
"on_verify_fp_signal": cp.onVerifyFpSignal,
"on_connect": cp.onConnect,
"on_disconnect": cp.onDisconnect,
})
cp.entryScroll.SetProperty("height-request", cp.calculateHeight(1))
cp.history.SetBuffer(ui.getTags().createTextBuffer())
cp.history.Connect("size-allocate", func() {
scrollToBottom(cp.scrollHistory)
})
cp.entry.Connect("key-release-event", cp.doPotentialEntryResize)
ui.displaySettings.control(cp.history)
ui.displaySettings.control(cp.entry)
return cp
}
func (conv *conversationPane) connectEnterHandler(target gtki.Widget) {
if target == nil {
target = conv.entry
}
target.Connect("key-press-event", func(_ gtki.Widget, ev gdki.Event) bool {
evk := g.gdk.EventKeyFrom(ev)
ret := false
if conv.account.isInsertEnter(evk, conv.shiftEnterSends) {
insertEnter(conv.entry)
ret = true
} else if conv.account.isSend(evk, conv.shiftEnterSends) {
conv.onSendMessageSignal()
ret = true
}
return ret
})
}
func isShiftEnter(evk gdki.EventKey) bool {
return hasShift(evk) && hasEnter(evk)
}
func isNormalEnter(evk gdki.EventKey) bool {
return !hasControlingModifier(evk) && hasEnter(evk)
}
func (a *account) isInsertEnter(evk gdki.EventKey, shiftEnterSends bool) bool {
if shiftEnterSends {
return isNormalEnter(evk)
}
return isShiftEnter(evk)
}
func (a *account) isSend(evk gdki.EventKey, shiftEnterSends bool) bool {
if !shiftEnterSends {
return isNormalEnter(evk)
}
return isShiftEnter(evk)
}
func newConversationWindow(account *account, uid string, ui *gtkUI) *conversationWindow {
builder := newBuilder("Conversation")
win := builder.getObj("conversation").(gtki.Window)
peer, ok := ui.accountManager.contacts[account].Get(uid)
otherName := uid
if ok {
otherName = peer.NameForPresentation()
}
// TODO: Can we put the security rating here, maybe?
title := fmt.Sprintf("%s <-> %s", account.session.DisplayName(), otherName)
win.SetTitle(title)
winBox := builder.getObj("box").(gtki.Box)
cp := createConversationPane(account, uid, ui, win)
winBox.PackStart(cp.widget, true, true, 0)
conv := &conversationWindow{
conversationPane: cp,
win: win,
}
cp.connectEnterHandler(conv.win)
cp.afterNewMessage = conv.potentiallySetUrgent
// Unlike the GTK version, this is not supposed to be used as a callback but
// it attaches the callback to the widget
conv.win.HideOnDelete()
inEventHandler := false
conv.win.Connect("set-focus", func() {
if !inEventHandler {
inEventHandler = true
conv.entry.GrabFocus()
inEventHandler = false
}
})
conv.win.Connect("focus-in-event", func() {
conv.unsetUrgent()
})
conv.win.Connect("notify::is-active", func() {
if conv.win.IsActive() {
inEventHandler = true
conv.entry.GrabFocus()
inEventHandler = false
}
})
conv.win.Connect("hide", func() {
conv.onHide()
})
conv.win.Connect("show", func() {
conv.onShow()
})
ui.connectShortcutsChildWindow(conv.win)
ui.connectShortcutsConversationWindow(conv)
conv.parentWin = ui.window
return conv
}
func (conv *conversationPane) addNotification(notification gtki.InfoBar) {
conv.notificationArea.Add(notification)
}
func (conv *conversationWindow) Hide() {
conv.win.Hide()
}
func (conv *conversationWindow) tryEnsureCorrectWorkspace() {
if g.gdk.WorkspaceControlSupported() {
wi, _ := conv.parentWin.GetWindow()
parentPlace := wi.GetDesktop()
cwi, _ := conv.win.GetWindow()
cwi.MoveToDesktop(parentPlace)
}
}
func (conv *conversationPane) getConversation() (client.Conversation, bool) {
return conv.account.session.ConversationManager().GetConversationWith(conv.to, conv.currentResource())
}
func (conv *conversationPane) withCurrentPeer(f func(*rosters.Peer)) {
p, ok := conv.currentPeer()
if ok {
f(p)
}
}
func (conv *conversationPane) isVerified() bool {
conversation, exists := conv.getConversation()
if !exists {
log.Println("Conversation does not exist - this shouldn't happen")
return false
}
fingerprint := conversation.TheirFingerprint()
conf := conv.account.session.GetConfig()
p, hasPeer := conf.GetPeer(conv.to)
if hasPeer {
p.EnsureHasFingerprint(fingerprint)
}
return hasPeer && p.HasTrustedFingerprint(fingerprint)
}
func (conv *conversationPane) showIdentityVerificationWarning(u *gtkUI) {
conv.Lock()
defer conv.Unlock()
if conv.fingerprintWarning != nil {
log.Println("we are already showing a fingerprint warning, so not doing it again")
return
}
if conv.isVerified() {
log.Println("We have a peer and a trusted fingerprint already, so no reason to warn")
return
}
conv.fingerprintWarning = buildVerifyIdentityNotification(conv.account, conv.to, conv.currentResource(), conv.transientParent)
conv.addNotification(conv.fingerprintWarning)
}
func (conv *conversationPane) removeIdentityVerificationWarning() {
conv.Lock()
defer conv.Unlock()
if conv.fingerprintWarning != nil {
conv.fingerprintWarning.Hide()
conv.fingerprintWarning.Destroy()
conv.fingerprintWarning = nil
}
}
func (conv *conversationPane) updateSecurityWarning() {
conversation, ok := conv.getConversation()
if !ok {
return
}
conv.securityWarning.SetVisible(!conversation.IsEncrypted())
}
func (conv *conversationWindow) show(userInitiated bool) {
conv.updateSecurityWarning()
conv.win.Show()
conv.tryEnsureCorrectWorkspace()
}
func (conv *conversationPane) sendMessage(message string) error {
err := conv.account.session.EncryptAndSendTo(conv.to, conv.currentResource(), message)
if err != nil {
oerr, isoff := err.(*access.OfflineError)
if !isoff {
return err
}
conv.displayNotification(oerr.Error())
} else {
//TODO: review whether it should create a conversation
//TODO: this should be whether the message was encrypted or not, rather than
//whether the conversation is encrypted or not
conversation, _ := conv.account.session.ConversationManager().EnsureConversationWith(conv.to, conv.currentResource())
conv.appendMessage(conv.account.session.DisplayName(), time.Now(), conversation.IsEncrypted(), ui.StripSomeHTML([]byte(message)), true)
}
return nil
}
const timeDisplay = "15:04:05"
// Expects to be called from the GUI thread.
// Expects to be called when conv is already locked
func insertAtEnd(buff gtki.TextBuffer, text string) {
buff.Insert(buff.GetEndIter(), text)
}
// Expects to be called from the GUI thread.
// Expects to be called when conv is already locked
func insertWithTag(buff gtki.TextBuffer, tagName, text string) {
charCount := buff.GetCharCount()
insertAtEnd(buff, text)
oldEnd := buff.GetIterAtOffset(charCount)
newEnd := buff.GetEndIter()
buff.ApplyTagByName(tagName, oldEnd, newEnd)
}
func is(v bool, left, right string) string {
if v {
return left
}
return right
}
func showForDisplay(show string, gone bool) string {
switch show {
case "", "available", "online":
if gone {
return ""
}
return i18n.Local("Available")
case "xa":
return i18n.Local("Not Available")
case "away":
return i18n.Local("Away")
case "dnd":
return i18n.Local("Busy")
case "chat":
return i18n.Local("Free for Chat")
case "invisible":
return i18n.Local("Invisible")
}
return show
}
func onlineStatus(show, showStatus string) string {
sshow := showForDisplay(show, false)
if sshow != "" {
return sshow + showStatusForDisplay(showStatus)
}
return ""
}
func showStatusForDisplay(showStatus string) string {
if showStatus != "" {
return " (" + showStatus + ")"
}
return ""
}
func extraOfflineStatus(show, showStatus string) string {
sshow := showForDisplay(show, true)
if sshow == "" {
return showStatusForDisplay(showStatus)
}
if showStatus != "" {
return " (" + sshow + ": " + showStatus + ")"
}
return " (" + sshow + ")"
}
func createStatusMessage(from string, show, showStatus string, gone bool) string {
tail := ""
if gone {
tail = i18n.Local("Offline") + extraOfflineStatus(show, showStatus)
} else {
tail = onlineStatus(show, showStatus)
}
if tail != "" {
return from + i18n.Local(" is now ") + tail
}
return ""
}
func scrollToBottom(sw gtki.ScrolledWindow) {
adj := sw.GetVAdjustment()
adj.SetValue(adj.GetUpper() - adj.GetPageSize())
}
func scrollToTop(sw gtki.ScrolledWindow) {
adj := sw.GetVAdjustment()
adj.SetValue(adj.GetLower())
}
type taggableText struct {
tag string
text string
}
func (conv *conversationPane) appendToHistory(timestamp time.Time, attention bool, entries ...taggableText) {
conv.markNow()
doInUIThread(func() {
conv.Lock()
defer conv.Unlock()
buff, _ := conv.history.GetBuffer()
if buff.GetCharCount() != 0 {
insertAtEnd(buff, "\n")
}
insertAtEnd(buff, "[")
insertAtEnd(buff, timestamp.Format(timeDisplay))
insertAtEnd(buff, "] ")
for _, entry := range entries {
if entry.tag != "" {
insertWithTag(buff, entry.tag, entry.text)
} else {
insertAtEnd(buff, entry.text)
}
}
if attention {
conv.afterNewMessage()
}
})
}
func (conv *conversationPane) appendStatus(from string, timestamp time.Time, show, showStatus string, gone bool) {
conv.appendToHistory(timestamp, false, taggableText{"statusText", createStatusMessage(from, show, showStatus, gone)})
}
func (conv *conversationPane) appendMessage(from string, timestamp time.Time, encrypted bool, message []byte, outgoing bool) {
conv.appendToHistory(timestamp, true,
taggableText{
is(outgoing, "outgoingUser", "incomingUser"),
from,
},
taggableText{
text: ": ",
},
taggableText{
is(outgoing, "outgoingText", "incomingText"),
string(message),
})
}
func (conv *conversationPane) displayNotification(notification string) {
conv.appendToHistory(time.Now(), false, taggableText{"statusText", notification})
}
func (conv *conversationPane) displayNotificationVerifiedOrNot(notificationV, notificationNV string) {
if conv.isVerified() {
conv.displayNotification(notificationV)
} else {
conv.displayNotification(notificationNV)
}
}
func (conv *conversationWindow) setEnabled(enabled bool) {
if enabled {
conv.win.Emit("enable")
} else {
conv.win.Emit("disable")
}
}
type timedMark struct {
at time.Time
offset int
}
func (conv *conversationPane) markNow() {
conv.Lock()
defer conv.Unlock()
buf, _ := conv.history.GetBuffer()
offset := buf.GetCharCount()
conv.marks = append(conv.marks, &timedMark{
at: time.Now(),
offset: offset,
})
}
const reapInterval = time.Duration(1) * time.Hour
func (conv *conversationPane) reapOlderThan(t time.Time) {
conv.Lock()
defer conv.Unlock()
newMarks := []*timedMark{}
var lastMark *timedMark
isEnd := false
for ix, m := range conv.marks {
if t.Before(m.at) {
newMarks = conv.marks[ix:]
break
}
lastMark = m
isEnd = len(conv.marks)-1 == ix
}
if lastMark != nil {
off := lastMark.offset + 1
buf, _ := conv.history.GetBuffer()
sit := buf.GetStartIter()
eit := buf.GetIterAtOffset(off)
if isEnd {
eit = buf.GetEndIter()
newMarks = []*timedMark{}
}
buf.Delete(sit, eit)
for _, nm := range newMarks {
nm.offset = nm.offset - off
}
conv.marks = newMarks
}
}
func (conv *conversationPane) onHide() {
conv.reapOlderThan(time.Now().Add(-reapInterval))
conv.hidden = true
}
func (conv *conversationPane) onShow() {
if conv.hidden {
conv.reapOlderThan(time.Now().Add(-reapInterval))
conv.hidden = false
}
}
func (conv *conversationWindow) potentiallySetUrgent() {
if !conv.win.HasToplevelFocus() {
conv.win.SetUrgencyHint(true)
}
}
func (conv *conversationWindow) unsetUrgent() {
conv.win.SetUrgencyHint(false)
}