/
topic.go
107 lines (88 loc) · 1.84 KB
/
topic.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
/*
* 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/message"
"github.com/flatpeach/starfruit/server"
"github.com/flatpeach/starfruit/user"
)
type Topic struct{}
func (module *Topic) Handle(s *server.Server, u *user.User, m *message.Message) error {
// TOPIC <channel> [ <topic> ]
if len(m.Params) == 0 {
u.SendErrorNeedMoreParams("TOPIC")
return nil
}
channelName := m.Params[0]
cnl := s.FindChannelByName(channelName)
if cnl == nil {
u.SendMessage(message.New(
s.Config.Server.Name,
message.ERR_NOSUCHCHANNEL,
[]string{u.NickName, channelName},
"No such channel",
))
return nil
}
if !s.IsUserJoinedChannel(u.Id, cnl.Id) {
u.SendMessage(message.New(
s.Config.Server.Name,
message.ERR_NOTONCHANNEL,
[]string{u.NickName, channelName},
"You're not on that channel",
))
return nil
}
if len(m.Params) > 1 {
var newTopic = m.Params[1]
cnl.SetTopic(newTopic, u.Full())
s.BroadcastMessage(cnl.Id, message.New(
u.Full(),
"TOPIC",
[]string{
channelName,
},
newTopic,
), nil)
return nil
}
if cnl.Topic() == "" {
u.SendMessage(message.New(
s.Config.Server.Name,
message.RPL_NOTOPIC,
[]string{
u.NickName,
channelName,
},
"No topic is set.",
))
return nil
} else {
u.SendMessage(message.New(
s.Config.Server.Name,
message.RPL_TOPIC,
[]string{
u.NickName,
channelName,
},
cnl.Topic(),
))
u.SendMessage(message.New(
s.Config.Server.Name,
message.RPL_TOPICWHOTIME,
[]string{
u.NickName,
channelName,
cnl.TopicSetBy(),
fmt.Sprintf("%d", cnl.TopicSetTime()),
},
nil,
))
// Sendout message RPL_TOPICWHOTIME
}
return nil
}