@@ -35,7 +35,7 @@ type Host struct {
35
35
mu sync.Mutex
36
36
motd string
37
37
count int
38
- clients map [chat.Member ][]Client
38
+ clients map [chat.Member ][]client
39
39
}
40
40
41
41
// NewHost creates a Host on top of an existing listener.
@@ -46,7 +46,7 @@ func NewHost(listener *sshd.SSHListener, auth *Auth) *Host {
46
46
listener : listener ,
47
47
commands : chat.Commands {},
48
48
auth : auth ,
49
- clients : map [chat.Member ][]Client {},
49
+ clients : map [chat.Member ][]client {},
50
50
}
51
51
52
52
// Make our own commands registry instance.
@@ -75,10 +75,11 @@ func (h *Host) SetMotd(motd string) {
75
75
// Connect a specific Terminal to this host and its room.
76
76
func (h * Host ) Connect (term * sshd.Terminal ) {
77
77
requestedName := term .Conn .Name ()
78
- user := message .BufferedScreen (requestedName , term )
79
-
80
- client := h .addClient (user , term .Conn )
81
- defer h .removeClient (user , client )
78
+ screen := message .BufferedScreen (requestedName , term )
79
+ user := & client {
80
+ Member : screen ,
81
+ conns : []sshd.Connection {term .Conn },
82
+ }
82
83
83
84
h .mu .Lock ()
84
85
motd := h .motd
@@ -91,10 +92,10 @@ func (h *Host) Connect(term *sshd.Terminal) {
91
92
user .SetConfig (cfg )
92
93
93
94
// Close term once user is closed.
94
- defer user .Close ()
95
+ defer screen .Close ()
95
96
defer term .Close ()
96
97
97
- go user .Consume ()
98
+ go screen .Consume ()
98
99
99
100
// Send MOTD
100
101
if motd != "" {
@@ -180,44 +181,6 @@ func (h *Host) Connect(term *sshd.Terminal) {
180
181
logger .Debugf ("[%s] Leaving: %s" , term .Conn .RemoteAddr (), user .Name ())
181
182
}
182
183
183
- func (h * Host ) addClient (user chat.Member , conn sshd.Connection ) * Client {
184
- client := Client {
185
- user : user ,
186
- conn : conn ,
187
- timestamp : time .Now (),
188
- }
189
- h .mu .Lock ()
190
- if _ , ok := h .clients [user ]; ok {
191
- logger .Warningf ("user collision: %q" , user )
192
- }
193
- h .clients [user ] = append (h .clients [user ], client )
194
- h .mu .Unlock ()
195
- return & client
196
- }
197
-
198
- func (h * Host ) removeClient (user chat.Member , client * Client ) {
199
- h .mu .Lock ()
200
- defer h .mu .Unlock ()
201
-
202
- clients := h .clients [user ]
203
- for i , c := range clients {
204
- // Find the user
205
- if & c != client {
206
- continue
207
- }
208
- // Delete corresponding client
209
- clients [i ] = clients [len (clients )- 1 ]
210
- clients = clients [:len (clients )- 1 ]
211
- break
212
- }
213
- }
214
-
215
- func (h * Host ) findClients (user chat.Member ) []Client {
216
- h .mu .Lock ()
217
- defer h .mu .Unlock ()
218
- return h .clients [user ]
219
- }
220
-
221
184
// Serve our chat room onto the listener
222
185
func (h * Host ) Serve () {
223
186
h .listener .HandlerFunc = h .Connect
@@ -244,7 +207,7 @@ func (h *Host) completeCommand(partial string) string {
244
207
}
245
208
246
209
// AutoCompleteFunction returns a callback for terminal autocompletion
247
- func (h * Host ) AutoCompleteFunction (u Replier ) func (line string , pos int , key rune ) (newLine string , newPos int , ok bool ) {
210
+ func (h * Host ) AutoCompleteFunction (u User ) func (line string , pos int , key rune ) (newLine string , newPos int , ok bool ) {
248
211
return func (line string , pos int , key rune ) (newLine string , newPos int , ok bool ) {
249
212
if key != 9 {
250
213
return
@@ -295,12 +258,13 @@ func (h *Host) AutoCompleteFunction(u Replier) func(line string, pos int, key ru
295
258
}
296
259
297
260
// GetUser returns a message.User based on a name.
298
- func (h * Host ) GetUser (name string ) (chat. Member , bool ) {
261
+ func (h * Host ) GetUser (name string ) (User , bool ) {
299
262
m , ok := h .MemberByID (name )
300
263
if ! ok {
301
264
return nil , false
302
265
}
303
- return m .Member , true
266
+ u , ok := m .Member .(User )
267
+ return u , ok
304
268
}
305
269
306
270
// InitCommands adds host-specific commands to a Commands container. These will
@@ -330,7 +294,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
330
294
txt := fmt .Sprintf ("[Sent PM to %s]" , target .Name ())
331
295
ms := message .NewSystemMsg (txt , msg .From ())
332
296
room .Send (ms )
333
- target .( Replier ). SetReplyTo (msg .From ())
297
+ target .SetReplyTo (msg .From ())
334
298
return nil
335
299
},
336
300
})
@@ -346,7 +310,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
346
310
return errors .New ("must specify message" )
347
311
}
348
312
349
- target := msg .From ().(Replier ).ReplyTo ()
313
+ target := msg .From ().(chat. Member ).ReplyTo ()
350
314
if target == nil {
351
315
return errors .New ("no message to reply to" )
352
316
}
@@ -376,14 +340,12 @@ func (h *Host) InitCommands(c *chat.Commands) {
376
340
return errors .New ("user not found" )
377
341
}
378
342
379
- // FIXME: Handle many clients
380
- clients := h .findClients (target )
381
343
var whois string
382
344
switch room .IsOp (msg .From ().(chat.Member )) {
383
345
case true :
384
- whois = whoisAdmin (clients )
346
+ whois = whoisAdmin (target )
385
347
case false :
386
- whois = whoisPublic (clients )
348
+ whois = whoisPublic (target )
387
349
}
388
350
room .Send (message .NewSystemMsg (whois , msg .From ()))
389
351
@@ -439,9 +401,12 @@ func (h *Host) InitCommands(c *chat.Commands) {
439
401
room .Ops .Add (set .Keyize (user .ID ()))
440
402
}
441
403
442
- for _ , client := range h .findClients (user ) {
443
- h .auth .Op (client .conn .PublicKey (), until )
444
- }
404
+ // TODO: Add pubkeys to op
405
+ /*
406
+ for _, conn := range user.Connections() {
407
+ h.auth.Op(conn.PublicKey(), until)
408
+ }
409
+ */
445
410
446
411
body := fmt .Sprintf ("Made op by %s." , msg .From ().Name ())
447
412
room .Send (message .NewSystemMsg (body , user ))
@@ -477,17 +442,16 @@ func (h *Host) InitCommands(c *chat.Commands) {
477
442
until , _ = time .ParseDuration (args [1 ])
478
443
}
479
444
480
- clients := h .findClients (target )
481
- for _ , client := range clients {
482
- h .auth .Ban (client .conn .PublicKey (), until )
483
- h .auth .BanAddr (client .conn .RemoteAddr (), until )
445
+ for _ , conn := range target .Connections () {
446
+ h .auth .Ban (conn .PublicKey (), until )
447
+ h .auth .BanAddr (conn .RemoteAddr (), until )
484
448
}
485
449
486
450
body := fmt .Sprintf ("%s was banned by %s." , target .Name (), msg .From ().Name ())
487
451
room .Send (message .NewAnnounceMsg (body ))
488
- target .(io. Closer ). Close ()
452
+ target .Close ()
489
453
490
- logger .Debugf ("Banned: \n -> %s" , whoisAdmin (clients ))
454
+ logger .Debugf ("Banned: \n -> %s" , whoisAdmin (target ))
491
455
492
456
return nil
493
457
},
0 commit comments