Tidied up JOIN/PART etc. to use Client.reply()
This commit is contained in:
parent
0e77bd9b13
commit
defccb1101
43
rosella.go
43
rosella.go
@ -51,7 +51,10 @@ const (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
rplWelcome int = iota
|
rplWelcome int = iota
|
||||||
|
rplJoin
|
||||||
|
rplPart
|
||||||
rplTopic
|
rplTopic
|
||||||
|
rplNoTopic
|
||||||
rplNames
|
rplNames
|
||||||
rplNickChange
|
rplNickChange
|
||||||
errMoreArgs
|
errMoreArgs
|
||||||
@ -225,7 +228,7 @@ func (s *Server) joinChannel(client *Client, channelName string) {
|
|||||||
channel, exists := s.channelMap[channelName]
|
channel, exists := s.channelMap[channelName]
|
||||||
if exists == false {
|
if exists == false {
|
||||||
channel = &Channel{name: channelName,
|
channel = &Channel{name: channelName,
|
||||||
topic: "No Topic Set",
|
topic: "",
|
||||||
clientMap: make(map[string]*Client)}
|
clientMap: make(map[string]*Client)}
|
||||||
s.channelMap[channelName] = channel
|
s.channelMap[channelName] = channel
|
||||||
}
|
}
|
||||||
@ -234,20 +237,21 @@ func (s *Server) joinChannel(client *Client, channelName string) {
|
|||||||
client.channelMap[channelName] = channel
|
client.channelMap[channelName] = channel
|
||||||
|
|
||||||
for _, c := range channel.clientMap {
|
for _, c := range channel.clientMap {
|
||||||
c.outputChan <- fmt.Sprintf(":%s JOIN %s", client.nick, channelName)
|
c.reply(rplJoin, client.nick, channelName)
|
||||||
}
|
}
|
||||||
|
|
||||||
//RPL_TOPIC
|
if channel.topic != "" {
|
||||||
client.outputChan <- fmt.Sprintf(":rosella 332 %s :%s", channelName, channel.topic)
|
client.reply(rplTopic, channelName, channel.topic)
|
||||||
|
} else {
|
||||||
//RPL_NAMREPLY
|
client.reply(rplNoTopic, channelName)
|
||||||
var nicks string
|
|
||||||
for _, c := range channel.clientMap {
|
|
||||||
nicks = fmt.Sprintf("%s %s", c.nick, nicks)
|
|
||||||
}
|
}
|
||||||
client.outputChan <- fmt.Sprintf(":rosella 353 %s = %s :%s", client.nick, channelName, nicks)
|
|
||||||
//RPL_ENDOFNAMES
|
nicks := make([]string, 0, 100)
|
||||||
client.outputChan <- ":rosella 366"
|
for nick := range channel.clientMap {
|
||||||
|
nicks = append(nicks, nick)
|
||||||
|
}
|
||||||
|
|
||||||
|
client.reply(rplNames, channelName, strings.Join(nicks, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) partChannel(client *Client, channelName string) {
|
func (s *Server) partChannel(client *Client, channelName string) {
|
||||||
@ -258,7 +262,7 @@ func (s *Server) partChannel(client *Client, channelName string) {
|
|||||||
|
|
||||||
//Notify clients of the part
|
//Notify clients of the part
|
||||||
for _, c := range channel.clientMap {
|
for _, c := range channel.clientMap {
|
||||||
c.outputChan <- fmt.Sprintf(":%s PART %s", client.nick, channelName)
|
c.reply(rplPart, client.nick, channelName)
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(channel.clientMap, client.nick)
|
delete(channel.clientMap, client.nick)
|
||||||
@ -361,11 +365,18 @@ func (c *Client) reply(code int, args ...string) {
|
|||||||
switch code {
|
switch code {
|
||||||
case rplWelcome:
|
case rplWelcome:
|
||||||
c.outputChan <- fmt.Sprintf(":%s 001 %s :Welcome to %s", c.server.name, c.nick, c.server.name)
|
c.outputChan <- fmt.Sprintf(":%s 001 %s :Welcome to %s", c.server.name, c.nick, c.server.name)
|
||||||
|
case rplJoin:
|
||||||
|
c.outputChan <- fmt.Sprintf(":%s JOIN %s", args[0], args[1])
|
||||||
|
case rplPart:
|
||||||
|
c.outputChan <- fmt.Sprintf(":%s PART %s", args[0], args[1])
|
||||||
case rplTopic:
|
case rplTopic:
|
||||||
//Arg0 is channel, all following args are topic
|
c.outputChan <- fmt.Sprintf(":%s 332 %s :%s", c.server.name, args[0], args[1])
|
||||||
c.outputChan <- fmt.Sprintf(":%s 332 %s :%s", args[0], strings.Join(args[1:], " "))
|
case rplNoTopic:
|
||||||
|
c.outputChan <- fmt.Sprintf(":%s 331 %s :No topic is set", c.server.name, args[0])
|
||||||
case rplNames:
|
case rplNames:
|
||||||
//Send the names
|
//TODO: break long lists up into multiple messages
|
||||||
|
c.outputChan <- fmt.Sprintf(":%s 353 %s = %s :%s", c.server.name, c.nick, args[0], args[1])
|
||||||
|
c.outputChan <- fmt.Sprintf(":%s 366", c.server.name)
|
||||||
case rplNickChange:
|
case rplNickChange:
|
||||||
c.outputChan <- fmt.Sprintf(":%s NICK %s", args[0], args[1])
|
c.outputChan <- fmt.Sprintf(":%s NICK %s", args[0], args[1])
|
||||||
case errMoreArgs:
|
case errMoreArgs:
|
||||||
|
Loading…
Reference in New Issue
Block a user