Moved joinChannel/partChannel methods to Client

This commit is contained in:
Harry Jeffery 2013-08-24 07:37:53 +01:00
parent 3ffbc1eb62
commit e13297a24b

View File

@ -180,7 +180,7 @@ func (s *Server) handleEvent(e Event) {
if args[0] == "0" { if args[0] == "0" {
//Quit all channels //Quit all channels
for channel := range e.client.channelMap { for channel := range e.client.channelMap {
s.partChannel(e.client, channel) e.client.partChannel(channel)
} }
return return
} }
@ -189,7 +189,7 @@ func (s *Server) handleEvent(e Event) {
for _, channel := range channels { for _, channel := range channels {
//Join the channel if it's valid //Join the channel if it's valid
if channelRegexp.Match([]byte(channel)) { if channelRegexp.Match([]byte(channel)) {
s.joinChannel(e.client, channel) e.client.joinChannel(channel)
} }
} }
@ -208,7 +208,7 @@ func (s *Server) handleEvent(e Event) {
for _, channel := range channels { for _, channel := range channels {
//Part the channel if it's valid //Part the channel if it's valid
if channelRegexp.Match([]byte(channel)) { if channelRegexp.Match([]byte(channel)) {
s.partChannel(e.client, channel) e.client.partChannel(channel)
} }
} }
@ -372,26 +372,26 @@ func (s *Server) handleEvent(e Event) {
} }
} }
func (s *Server) joinChannel(client *Client, channelName string) { func (c *Client) joinChannel(channelName string) {
channel, exists := s.channelMap[channelName] channel, exists := c.server.channelMap[channelName]
if exists == false { if exists == false {
channel = &Channel{name: channelName, channel = &Channel{name: channelName,
topic: "", topic: "",
clientMap: make(map[string]*Client)} clientMap: make(map[string]*Client)}
s.channelMap[channelName] = channel c.server.channelMap[channelName] = channel
} }
channel.clientMap[client.nick] = client channel.clientMap[c.nick] = c
client.channelMap[channelName] = channel c.channelMap[channelName] = channel
for _, c := range channel.clientMap { for _, client := range channel.clientMap {
c.reply(rplJoin, client.nick, channelName) client.reply(rplJoin, c.nick, channelName)
} }
if channel.topic != "" { if channel.topic != "" {
client.reply(rplTopic, channelName, channel.topic) c.reply(rplTopic, channelName, channel.topic)
} else { } else {
client.reply(rplNoTopic, channelName) c.reply(rplNoTopic, channelName)
} }
nicks := make([]string, 0, 100) nicks := make([]string, 0, 100)
@ -399,22 +399,22 @@ func (s *Server) joinChannel(client *Client, channelName string) {
nicks = append(nicks, nick) nicks = append(nicks, nick)
} }
client.reply(rplNames, channelName, strings.Join(nicks, " ")) c.reply(rplNames, channelName, strings.Join(nicks, " "))
} }
func (s *Server) partChannel(client *Client, channelName string) { func (c *Client) partChannel(channelName string) {
channel, exists := s.channelMap[channelName] channel, exists := c.server.channelMap[channelName]
if exists == false { if exists == false {
return return
} }
//Notify clients of the part //Notify clients of the part
for _, c := range channel.clientMap { for _, client := range channel.clientMap {
c.reply(rplPart, client.nick, channelName) client.reply(rplPart, c.nick, channelName)
} }
delete(channel.clientMap, client.nick) delete(channel.clientMap, c.nick)
delete(client.channelMap, channelName) delete(c.channelMap, channelName)
} }
func (c *Client) clientThread() { func (c *Client) clientThread() {
@ -430,7 +430,7 @@ func (c *Client) clientThread() {
defer func() { defer func() {
//Part from all channels //Part from all channels
for channelName := range c.channelMap { for channelName := range c.channelMap {
c.server.partChannel(c, channelName) c.partChannel(channelName)
} }
delete(c.server.clientMap, c.nick) delete(c.server.clientMap, c.nick)