forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_events.go
199 lines (176 loc) · 4.91 KB
/
account_events.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
package gui
import (
"log"
"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/twstrike/gotk3adapter/gtki"
"github.com/twstrike/coyim/i18n"
"github.com/twstrike/coyim/session/events"
"github.com/twstrike/coyim/xmpp/utils"
)
func (u *gtkUI) handleOneAccountEvent(ev interface{}) {
switch t := ev.(type) {
case events.Event:
doInUIThread(func() {
u.handleSessionEvent(t)
})
case events.Peer:
doInUIThread(func() {
u.handlePeerEvent(t)
})
case events.Notification:
doInUIThread(func() {
u.handleNotificationEvent(t)
})
case events.Presence:
doInUIThread(func() {
u.handlePresenceEvent(t)
})
case events.Message:
doInUIThread(func() {
u.handleMessageEvent(t)
})
case events.Log:
doInUIThread(func() {
u.handleLogEvent(t)
})
default:
log.Printf("unsupported event %#v\n", t)
}
}
func (u *gtkUI) observeAccountEvents() {
for ev := range u.events {
u.handleOneAccountEvent(ev)
}
}
func (u *gtkUI) handleLogEvent(ev events.Log) {
m := ev.Message
switch ev.Level {
case events.Info:
log.Println(">>> INFO", m)
case events.Warn:
log.Println(">>> WARN", m)
case events.Alert:
log.Println(">>> ALERT", m)
}
}
func (u *gtkUI) handleMessageEvent(ev events.Message) {
account := u.findAccountForSession(ev.Session)
if account == nil {
//TODO error
return
}
u.roster.messageReceived(
account,
ev.From,
ev.Resource,
ev.When,
ev.Encrypted,
ev.Body,
)
}
func (u *gtkUI) handleSessionEvent(ev events.Event) {
account := u.findAccountForSession(ev.Session)
if account != nil {
switch ev.Type {
case events.Connected:
account.enableExistingConversationWindows(true)
case events.Disconnected:
account.enableExistingConversationWindows(false)
case events.ConnectionLost:
u.notifyConnectionFailure(account, u.connectionFailureMoreInfoConnectionLost)
go u.connectWithRandomDelay(account)
case events.RosterReceived:
u.roster.update(account, ev.Session.R())
}
}
u.rosterUpdated()
}
func (u *gtkUI) handlePresenceEvent(ev events.Presence) {
if ev.Session.GetConfig().HideStatusUpdates {
return
}
log.Printf("[%s] Presence from %v: show: %v status: %v gone: %v\n", ev.To, ev.From, ev.Show, ev.Status, ev.Gone)
u.rosterUpdated()
account := u.findAccountForSession(ev.Session)
if account == nil {
return
}
u.roster.presenceUpdated(
account,
utils.RemoveResourceFromJid(ev.From),
ev.Show,
ev.Status,
ev.Gone,
)
}
func convWindowNowOrLater(account *account, peer string, f func(conversationView)) {
convWin, ok := account.getConversationWith(peer)
if !ok {
account.afterConversationWindowCreated(peer, f)
} else {
f(convWin)
}
}
func (u *gtkUI) handlePeerEvent(ev events.Peer) {
identityWarning := func(cv conversationView) {
cv.updateSecurityWarning()
cv.showIdentityVerificationWarning(u)
}
switch ev.Type {
case events.IQReceived:
//TODO
log.Printf("received iq: %v\n", ev.From)
case events.OTREnded:
peer := ev.From
account := u.findAccountForSession(ev.Session)
convWindowNowOrLater(account, peer, func(cv conversationView) {
cv.displayNotification(i18n.Local("Private conversation has ended."))
cv.updateSecurityWarning()
})
case events.OTRNewKeys:
peer := ev.From
account := u.findAccountForSession(ev.Session)
convWindowNowOrLater(account, peer, func(cv conversationView) {
cv.displayNotificationVerifiedOrNot(i18n.Local("Private conversation started."), i18n.Local("Unverified conversation started."))
identityWarning(cv)
})
case events.OTRRenewedKeys:
peer := ev.From
account := u.findAccountForSession(ev.Session)
convWindowNowOrLater(account, peer, func(cv conversationView) {
cv.displayNotificationVerifiedOrNot(i18n.Local("Successfully refreshed the private conversation."), i18n.Local("Successfully refreshed the unverified private conversation."))
identityWarning(cv)
})
case events.SubscriptionRequest:
confirmDialog := authorizePresenceSubscriptionDialog(u.window, ev.From)
doInUIThread(func() {
responseType := gtki.ResponseType(confirmDialog.Run())
switch responseType {
case gtki.RESPONSE_YES:
ev.Session.HandleConfirmOrDeny(ev.From, true)
case gtki.RESPONSE_NO:
ev.Session.HandleConfirmOrDeny(ev.From, false)
default:
// We got a different response, such as a close of the window. In this case we want
// to keep the subscription request open
}
confirmDialog.Destroy()
})
case events.Subscribed:
jid := ev.Session.GetConfig().Account
log.Printf("[%s] Subscribed to %s\n", jid, ev.From)
u.rosterUpdated()
case events.Unsubscribe:
jid := ev.Session.GetConfig().Account
log.Printf("[%s] Unsubscribed from %s\n", jid, ev.From)
u.rosterUpdated()
}
}
func (u *gtkUI) handleNotificationEvent(ev events.Notification) {
peer := ev.Peer
account := u.findAccountForSession(ev.Session)
convWin, err := u.roster.openConversationView(account, peer, false)
if err != nil {
return
}
convWin.displayNotification(i18n.Local(ev.Notification))
}