From 45bb0d05e72386ae24c8876d9266c168c713697a Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Tue, 25 Feb 2014 20:17:26 -0800 Subject: [PATCH] normalize all unicode input from the client --- irc/channel.go | 5 +++-- irc/server.go | 37 +++++++++++++++++++------------------ irc/socket.go | 3 ++- irc/types.go | 6 +++++- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/irc/channel.go b/irc/channel.go index d9b73d08..5f469bd3 100644 --- a/irc/channel.go +++ b/irc/channel.go @@ -2,6 +2,7 @@ package irc import ( "strconv" + "strings" ) type Channel struct { @@ -30,11 +31,11 @@ func NewChannel(s *Server, name string) *Channel { InviteMask: []UserMask{}, }, members: make(MemberSet), - name: name, + name: strings.ToLower(name), server: s, } - s.channels[name] = channel + s.channels.Add(channel) return channel } diff --git a/irc/server.go b/irc/server.go index 7ac9367d..135c20d2 100644 --- a/irc/server.go +++ b/irc/server.go @@ -226,7 +226,7 @@ func (s *Server) GenerateGuestNick() string { continue // TODO handle error } nick := fmt.Sprintf("guest%d", randInt) - if s.clients[nick] == nil { + if s.clients.Get(nick) == nil { return nick } } @@ -407,7 +407,7 @@ func (msg *NickCommand) HandleServer(server *Server) { return } - if server.clients.Get(msg.nickname) != nil { + if server.clients.Get(msg.nickname) != client { client.ErrNickNameInUse(msg.nickname) return } @@ -439,7 +439,7 @@ func (m *JoinCommand) HandleServer(s *Server) { continue } - channel := s.channels[name] + channel := s.channels.Get(name) if channel == nil { channel = NewChannel(s, name) } @@ -450,7 +450,7 @@ func (m *JoinCommand) HandleServer(s *Server) { func (m *PartCommand) HandleServer(server *Server) { client := m.Client() for _, chname := range m.channels { - channel := server.channels[chname] + channel := server.channels.Get(chname) if channel == nil { m.Client().ErrNoSuchChannel(chname) @@ -463,7 +463,7 @@ func (m *PartCommand) HandleServer(server *Server) { func (msg *TopicCommand) HandleServer(server *Server) { client := msg.Client() - channel := server.channels[msg.channel] + channel := server.channels.Get(msg.channel) if channel == nil { client.ErrNoSuchChannel(msg.channel) return @@ -479,7 +479,7 @@ func (msg *TopicCommand) HandleServer(server *Server) { func (msg *PrivMsgCommand) HandleServer(server *Server) { client := msg.Client() if IsChannel(msg.target) { - channel := server.channels[msg.target] + channel := server.channels.Get(msg.target) if channel == nil { client.ErrNoSuchChannel(msg.target) return @@ -489,7 +489,7 @@ func (msg *PrivMsgCommand) HandleServer(server *Server) { return } - target := server.clients[msg.target] + target := server.clients.Get(msg.target) if target == nil { client.ErrNoSuchNick(msg.target) return @@ -586,7 +586,7 @@ func (m *WhoisCommand) HandleServer(server *Server) { func (msg *ChannelModeCommand) HandleServer(server *Server) { client := msg.Client() - channel := server.channels[msg.channel] + channel := server.channels.Get(msg.channel) if channel == nil { client.ErrNoSuchChannel(msg.channel) return @@ -617,14 +617,14 @@ func (msg *WhoCommand) HandleServer(server *Server) { } } } else if IsChannel(mask) { - channel := server.channels[mask] + channel := server.channels.Get(mask) if channel != nil { for member := range channel.members { client.RplWhoReply(channel, member) } } } else { - mclient := server.clients[mask] + mclient := server.clients.Get(mask) if mclient != nil { client.RplWhoReply(nil, mclient) } @@ -682,7 +682,7 @@ func (msg *MOTDCommand) HandleServer(server *Server) { func (msg *NoticeCommand) HandleServer(server *Server) { client := msg.Client() if IsChannel(msg.target) { - channel := server.channels[msg.target] + channel := server.channels.Get(msg.target) if channel == nil { client.ErrNoSuchChannel(msg.target) return @@ -703,13 +703,13 @@ func (msg *NoticeCommand) HandleServer(server *Server) { func (msg *KickCommand) HandleServer(server *Server) { client := msg.Client() for chname, nickname := range msg.kicks { - channel := server.channels[chname] + channel := server.channels.Get(chname) if channel == nil { client.ErrNoSuchChannel(chname) continue } - target := server.clients[nickname] + target := server.clients.Get(nickname) if target == nil { client.ErrNoSuchNick(nickname) continue @@ -737,7 +737,7 @@ func (msg *ListCommand) HandleServer(server *Server) { } } else { for _, chname := range msg.channels { - channel := server.channels[chname] + channel := server.channels.Get(chname) if channel == nil || (!client.flags[Operator] && channel.flags[Private]) { client.ErrNoSuchChannel(chname) continue @@ -758,7 +758,7 @@ func (msg *NamesCommand) HandleServer(server *Server) { } for _, chname := range msg.channels { - channel := server.channels[chname] + channel := server.channels.Get(chname) if channel == nil { client.ErrNoSuchChannel(chname) continue @@ -830,10 +830,11 @@ func (msg *InviteCommand) HandleServer(server *Server) { return } - channel := server.channels[msg.channel] + channel := server.channels.Get(msg.channel) if channel == nil { - client.RplInviting(target, msg.channel) - target.Reply(RplInviteMsg(client, msg.channel)) + name := strings.ToLower(msg.channel) + client.RplInviting(target, name) + target.Reply(RplInviteMsg(client, name)) return } diff --git a/irc/socket.go b/irc/socket.go index a93e7a5b..7aa8d93e 100644 --- a/irc/socket.go +++ b/irc/socket.go @@ -2,6 +2,7 @@ package irc import ( "bufio" + "code.google.com/p/go.text/unicode/norm" "io" "log" "net" @@ -23,7 +24,7 @@ type Socket struct { func NewSocket(conn net.Conn, commands chan<- editableCommand) *Socket { socket := &Socket{ conn: conn, - reader: bufio.NewReader(conn), + reader: bufio.NewReader(norm.NFKC.Reader(conn)), writer: bufio.NewWriter(conn), } diff --git a/irc/types.go b/irc/types.go index d8d9f9f8..6cb49a60 100644 --- a/irc/types.go +++ b/irc/types.go @@ -54,6 +54,10 @@ func (mode ChannelMode) String() string { type ChannelNameMap map[string]*Channel +func (channels ChannelNameMap) Get(name string) *Channel { + return channels[strings.ToLower(name)] +} + func (channels ChannelNameMap) Add(channel *Channel) error { if channels[channel.name] != nil { return fmt.Errorf("%s: already set", channel.name) @@ -100,7 +104,7 @@ func (clients ClientNameMap) Remove(client *Client) error { if clients.Get(client.nick) != client { return ErrNicknameMismatch } - delete(clients, client.nick) + delete(clients, strings.ToLower(client.nick)) return nil }