/
list.go
62 lines (53 loc) · 1.27 KB
/
list.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
/*
* 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 (
"fmt"
"github.com/flatpeach/starfruit/channel"
"github.com/flatpeach/starfruit/message"
"github.com/flatpeach/starfruit/server"
"github.com/flatpeach/starfruit/user"
"strings"
)
type List struct{}
func (module *List) Handle(s *server.Server, u *user.User, m *message.Message) error {
// LIST [ <channel> * ( "," <channel> ) ]
var (
channelsToList []*channel.Channel
)
if len(m.Params) == 0 {
// List all channels
channelsToList = s.GetAllChannels()
} else {
// List only specific channel
channelNames := strings.Split(m.Params[0], ",")
for _, channelName := range channelNames {
cnl := s.FindChannelByName(channelName)
if cnl != nil {
channelsToList = append(channelsToList, cnl)
}
}
}
for _, cnl := range channelsToList {
u.SendMessage(message.New(
s.Config.Server.Name,
message.RPL_LIST,
[]string{
u.NickName,
cnl.Name,
fmt.Sprintf("%d", s.ChannelUserCount(cnl.Id)),
},
cnl.Topic(),
))
}
u.SendMessage(message.New(
s.Config.Server.Name,
message.RPL_LISTEND,
[]string{u.NickName},
"End of /LIST.",
))
return nil
}