-
Notifications
You must be signed in to change notification settings - Fork 8
/
session.go
287 lines (250 loc) · 7.24 KB
/
session.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
package state
import (
"sync"
"time"
"github.com/mk6i/retro-aim-server/wire"
)
// SessSendStatus is the result of sending a message to a user.
type SessSendStatus int
const (
// SessSendOK indicates message was sent to recipient
SessSendOK SessSendStatus = iota
// SessSendClosed indicates send did not complete because session is closed
SessSendClosed
// SessQueueFull indicates send failed due to full queue -- client is likely
// dead
SessQueueFull
)
// Session represents a user's current session. Unless stated otherwise, all
// methods may be safely accessed by multiple goroutines.
type Session struct {
awayMessage string
chatRoomCookie string
closed bool
id string
idle bool
idleTime time.Time
invisible bool
msgCh chan wire.SNACMessage
mutex sync.RWMutex
nowFn func() time.Time
signonComplete bool
screenName string
signonTime time.Time
stopCh chan struct{}
warning uint16
caps [][16]byte
}
// NewSession returns a new instance of Session. By default, the user may have
// up to 1000 pending messages before blocking.
func NewSession() *Session {
return &Session{
msgCh: make(chan wire.SNACMessage, 1000),
nowFn: time.Now,
stopCh: make(chan struct{}),
signonTime: time.Now(),
caps: make([][16]byte, 0),
}
}
// IncrementWarning increments the user's warning level. To decrease, pass a
// negative increment value.
func (s *Session) IncrementWarning(incr uint16) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.warning += incr
}
// SetInvisible toggles the user's invisibility status.
func (s *Session) SetInvisible(invisible bool) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.invisible = invisible
}
// Invisible returns true if the user is idle.
func (s *Session) Invisible() bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.invisible
}
// SetScreenName sets the user's screen name.
func (s *Session) SetScreenName(screenName string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.screenName = screenName
}
// ScreenName returns the user's screen name.
func (s *Session) ScreenName() string {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.screenName
}
// SetID sets the user's session ID.
func (s *Session) SetID(ID string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.id = ID
}
// ID returns the user's session ID.
func (s *Session) ID() string {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.id
}
// SetSignonTime sets the user's sign-ontime.
func (s *Session) SetSignonTime(t time.Time) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.signonTime = t
}
// SetIdle sets the user's idle state.
func (s *Session) SetIdle(dur time.Duration) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.idle = true
// set the time the user became idle
s.idleTime = s.nowFn().Add(-dur)
}
// UnsetIdle removes the user's idle state.
func (s *Session) UnsetIdle() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.idle = false
}
// SetAwayMessage sets the user's away message.
func (s *Session) SetAwayMessage(awayMessage string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.awayMessage = awayMessage
}
// AwayMessage returns the user's away message.
func (s *Session) AwayMessage() string {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.awayMessage
}
// SetChatRoomCookie sets the chatRoomCookie for the chat room the user is currently in.
func (s *Session) SetChatRoomCookie(cookie string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.chatRoomCookie = cookie
}
// ChatRoomCookie gets the chatRoomCookie for the chat room the user is currently in.
func (s *Session) ChatRoomCookie() string {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.chatRoomCookie
}
// SignonComplete indicates whether the client has completed the sign-on sequence.
func (s *Session) SignonComplete() bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.signonComplete
}
// SetSignonComplete indicates that the client has completed the sign-on sequence.
func (s *Session) SetSignonComplete() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.signonComplete = true
}
// TLVUserInfo returns a TLV list containing session information required by
// multiple SNAC message types that convey user information.
func (s *Session) TLVUserInfo() wire.TLVUserInfo {
s.mutex.RLock()
defer s.mutex.RUnlock()
return wire.TLVUserInfo{
ScreenName: s.screenName,
WarningLevel: s.warning,
TLVBlock: wire.TLVBlock{
TLVList: s.userInfo(),
},
}
}
func (s *Session) userInfo() wire.TLVList {
tlvs := wire.TLVList{}
// sign-in timestamp
tlvs.Append(wire.NewTLV(wire.OServiceUserInfoSignonTOD, uint32(s.signonTime.Unix())))
// away message status
if s.awayMessage != "" {
tlvs.Append(wire.NewTLV(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagOSCARFree|wire.OServiceUserFlagUnavailable))
} else {
tlvs.Append(wire.NewTLV(wire.OServiceUserInfoUserFlags, wire.OServiceUserFlagOSCARFree))
}
// invisibility status
if s.invisible {
tlvs.Append(wire.NewTLV(wire.OServiceUserInfoStatus, wire.OServiceUserFlagInvisible))
} else {
tlvs.Append(wire.NewTLV(wire.OServiceUserInfoStatus, uint16(0)))
}
// idle status
if s.idle {
tlvs.Append(wire.NewTLV(wire.OServiceUserInfoIdleTime, uint16(s.nowFn().Sub(s.idleTime).Seconds())))
} else {
tlvs.Append(wire.NewTLV(wire.OServiceUserInfoIdleTime, uint16(0)))
}
// capabilities (buddy icon, chat, etc...)
if len(s.caps) > 0 {
tlvs.Append(wire.NewTLV(wire.OServiceUserInfoOscarCaps, s.caps))
}
return tlvs
}
// SetCaps sets capability UUIDs that represent the features the client
// supports. If set, capability metadata appears in the user info TLV list.
func (s *Session) SetCaps(caps [][16]byte) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.caps = caps
}
// Caps retrieves user capabilities.
func (s *Session) Caps() [][16]byte {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.caps
}
func (s *Session) Warning() uint16 {
s.mutex.RLock()
defer s.mutex.RUnlock()
var w uint16
w = s.warning
return w
}
// ReceiveMessage returns a channel of messages relayed via this session. It
// may only be read by one consumer. The channel never closes; call this method
// in a select block along with Closed in order to detect session closure.
func (s *Session) ReceiveMessage() chan wire.SNACMessage {
return s.msgCh
}
// RelayMessage receives a SNAC message from a user and passes it on
// asynchronously to the consumer of this session's messages. It returns
// SessSendStatus to indicate whether the message was successfully sent or
// not. This method is non-blocking.
func (s *Session) RelayMessage(msg wire.SNACMessage) SessSendStatus {
s.mutex.RLock()
defer s.mutex.RUnlock()
if s.closed {
return SessSendClosed
}
select {
case s.msgCh <- msg:
return SessSendOK
case <-s.stopCh:
return SessSendClosed
default:
return SessQueueFull
}
}
// Close shuts down the session's ability to relay messages. Once invoked,
// RelayMessage returns SessQueueFull and Closed returns a closed channel.
// It is not possible to re-open message relaying once closed. It is safe to
// call from multiple go routines.
func (s *Session) Close() {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.closed {
return
}
close(s.stopCh)
s.closed = true
}
// Closed blocks until the session is closed.
func (s *Session) Closed() <-chan struct{} {
return s.stopCh
}