/
whois.go
95 lines (83 loc) · 1.79 KB
/
whois.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
/*
* Copyright 2014 The starfruit Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package module
import (
"github.com/flatpeach/starfruit/message"
"github.com/flatpeach/starfruit/server"
"github.com/flatpeach/starfruit/user"
"strings"
)
type Whois struct{}
func (module *Whois) Handle(s *server.Server, u *user.User, m *message.Message) error {
// WHOIS <mask> *( "," <mask> )
if len(m.Params) != 1 {
u.SendMessage(message.New(
s.Config.Server.Name,
message.ERR_NEEDMOREPARAMS,
nil,
"Need more params",
))
return nil
}
nicks := strings.Split(m.Params[0], ",")
for _, nick := range nicks {
target := s.GetUserByNickName(nick)
if u == nil {
// @Todo: fulfill the errors here
continue
}
u.SendMessage(message.New(
s.Config.Server.Name,
message.RPL_WHOISUSER,
[]string{
u.NickName,
target.NickName,
target.UserName,
target.HostName,
"*",
},
u.RealName,
))
u.SendMessage(message.New(
s.Config.Server.Name,
message.RPL_WHOISSERVER,
[]string{
u.NickName,
target.NickName,
s.Config.Server.Name,
},
s.Config.Server.Name,
))
joinedChannels := s.GetJoinedChannels(u.Id)
if len(joinedChannels) > 0 {
u.SendMessage(message.New(
s.Config.Server.Name,
message.RPL_WHOISCHANNELS,
[]string{
u.NickName,
target.NickName,
},
strings.Join((func() []string {
var names []string
for _, cnl := range joinedChannels {
names = append(names, cnl.String())
}
return names
})(), " "),
))
}
u.SendMessage(message.New(
s.Config.Server.Name,
message.RPL_ENDOFWHOIS,
[]string{
u.NickName,
target.NickName,
},
"End of /WHOIS list.",
))
}
return nil
}