Nicks and channels should be now case insensitive

This commit is contained in:
Andreas Neue 2016-07-21 14:10:11 +02:00
parent eb8f0ac3d8
commit 76308cbd56
1 changed files with 39 additions and 30 deletions

View File

@ -120,31 +120,32 @@ func (sv *Server) dispatch() {
sv.recvMsg(msg)
sv.packetsTransferred++
case cl := <-sv.AddClient:
nick := cl.Name()
if _, exists := sv.clients[nick]; exists {
lnick := strings.ToLower(cl.Name())
if _, exists := sv.clients[lnick]; exists {
cl.Register(false)
xlog.Info("Client registration failed: '%s'", nick)
xlog.Info("Client registration failed: '%s'", lnick)
} else {
sv.clients[nick] = cl
sv.sendLogon(cl.Name())
sv.clients[lnick] = cl
sv.sendLogon(lnick)
sv.connectionsCurrent = float64(len(sv.clients))
cl.Register(true)
xlog.Info("Client registered: '%s'", nick)
xlog.Info("Client registered: '%s'", lnick)
xlog.Info("Server has %d client(s)", len(sv.clients))
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
}
case cl := <-sv.DelClient:
nick := cl.Name()
lnick := strings.ToLower(nick)
cl.Destroy()
for chname, ch := range sv.chUsers {
if _, exists := ch[nick]; exists {
delete(ch, nick)
if _, exists := ch[lnick]; exists {
delete(ch, lnick)
sv.sendMsg(irc.M(nick, "PART", chname, "quit"))
}
}
delete(sv.clients, nick)
delete(sv.clients, lnick)
sv.connectionsCurrent = float64(len(sv.clients))
xlog.Info("Client deleted: '%s'", nick)
xlog.Info("Client deleted: '%s'", lnick)
xlog.Info("Server has %d client(s)", len(sv.clients))
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
default:
@ -182,34 +183,36 @@ func (sv *Server) recvMsg(msg *irc.Message) {
func (sv *Server) sendMsg(msg *irc.Message) {
if strings.HasPrefix(msg.Args[0], "#") {
ch := msg.Args[0]
if _, exists := sv.chUsers[ch]; !exists {
lch := strings.ToLower(msg.Args[0])
if _, exists := sv.chUsers[lch]; !exists {
sv.sendReply(msg.Pre, ERR_NOSUCHNICK, msg.Args[0], "No such nick/channel")
return
}
for nick, _ := range sv.chUsers[ch] {
if msg.Pre == nick && msg.Cmd == "PRIVMSG" {
for lnick, _ := range sv.chUsers[lch] {
if strings.ToLower(msg.Pre) == lnick && msg.Cmd == "PRIVMSG" {
continue
}
if cl, exists := sv.clients[nick]; exists {
if cl, exists := sv.clients[lnick]; exists {
cl.Receive(msg)
}
}
} else {
if _, exists := sv.clients[msg.Args[0]]; !exists {
lnick := strings.ToLower(msg.Args[0])
if _, exists := sv.clients[lnick]; !exists {
sv.sendReply(msg.Pre, ERR_NOSUCHNICK, msg.Args[0], "No such nick/channel")
return
}
cl := sv.clients[msg.Args[0]]
cl := sv.clients[lnick]
cl.Receive(msg)
}
}
func (sv *Server) sendReply(tar, cmd, args, trail string) {
if _, exists := sv.clients[tar]; !exists {
lnick := strings.ToLower(tar)
if _, exists := sv.clients[lnick]; !exists {
return
}
cl := sv.clients[tar]
cl := sv.clients[lnick]
if args != "" {
args = tar + " " + args
} else {
@ -238,40 +241,46 @@ func (sv *Server) sendLogon(nick string) {
}
func (sv *Server) channelJoin(nick, ch string) {
if _, exists := sv.chUsers[ch]; !exists {
sv.chUsers[ch] = make(map[string]string)
sv.chTopics[ch] = ""
lnick := strings.ToLower(nick)
lch := strings.ToLower(ch)
if _, exists := sv.chUsers[lch]; !exists {
sv.chUsers[lch] = make(map[string]string)
sv.chTopics[lch] = ""
}
if _, exists := sv.chUsers[ch][nick]; exists {
if _, exists := sv.chUsers[lch][lnick]; exists {
return
}
sv.chUsers[ch][nick] = ""
sv.chUsers[lch][lnick] = ""
sv.sendMsg(irc.M(nick, "JOIN", ch, ""))
sv.sendReply(nick, RPL_TOPIC, ch, sv.chTopics[ch])
sv.channelNames(nick, ch)
}
func (sv *Server) channelPart(nick, ch, reason string) {
if _, exists := sv.chUsers[ch]; !exists {
lnick := strings.ToLower(nick)
lch := strings.ToLower(nick)
if _, exists := sv.chUsers[lch]; !exists {
return
}
if _, exists := sv.chUsers[ch][nick]; !exists {
if _, exists := sv.chUsers[lch][lnick]; !exists {
return
}
sv.sendMsg(irc.M(nick, "PART", ch, reason))
delete(sv.chUsers[ch], nick)
delete(sv.chUsers[lch], lnick)
}
func (sv *Server) channelNames(nick, ch string) {
if _, exists := sv.chUsers[ch]; !exists {
lch := strings.ToLower(ch)
if _, exists := sv.chUsers[lch]; !exists {
return
}
names := ""
for name, mode := range sv.chUsers[ch] {
for lnick, mode := range sv.chUsers[lch] {
nick := sv.clients[lnick].Name()
if names != "" {
names += " "
}
names = names + mode + name
names = names + mode + nick
}
sv.sendReply(nick, RPL_NAMEREPLY, "= "+ch, names)
sv.sendReply(nick, RPL_ENDOFNAMES, ch, "End of /NAMES list")