3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-26 05:49:25 +01:00

save reply source id at init time for nick changes

This commit is contained in:
Jeremy Latt 2014-02-13 10:57:00 -08:00
parent 261aaa128f
commit c805006ab8
3 changed files with 36 additions and 12 deletions

View File

@ -15,8 +15,14 @@ func joinedLen(names []string) int {
} }
type BaseReply struct { type BaseReply struct {
source Identifier id string
message string message string
source Identifier
}
func (reply *BaseReply) SetSource(source Identifier) {
reply.id = source.Id()
reply.source = source
} }
func (reply *BaseReply) Source() Identifier { func (reply *BaseReply) Source() Identifier {
@ -33,22 +39,22 @@ func NewStringReply(source Identifier, code string,
reply := &StringReply{ reply := &StringReply{
code: code, code: code,
} }
reply.SetSource(source)
reply.message = fmt.Sprintf(format, args...) reply.message = fmt.Sprintf(format, args...)
reply.source = source
return reply return reply
} }
func (reply *StringReply) Format(client *Client) []string { func (reply *StringReply) Format(client *Client) []string {
message := fmt.Sprintf("%s %s", reply.code, reply.message) message := fmt.Sprintf("%s %s", reply.code, reply.message)
if Identifier(client.server) != reply.source { if Identifier(client.server) != reply.source {
message = fmt.Sprintf(":%s %s", reply.source.Id(), message) message = fmt.Sprintf(":%s %s", reply.id, message)
} }
return []string{message} return []string{message}
} }
func (reply *StringReply) String() string { func (reply *StringReply) String() string {
return fmt.Sprintf("Reply(source=%s, code=%s, message=%s)", return fmt.Sprintf("Reply(source=%s, code=%s, message=%s)",
reply.source, reply.code, reply.message) reply.id, reply.code, reply.message)
} }
type NumericReply struct { type NumericReply struct {
@ -61,8 +67,8 @@ func NewNumericReply(source Identifier, code int, format string,
reply := &NumericReply{ reply := &NumericReply{
code: code, code: code,
} }
reply.SetSource(source)
reply.message = fmt.Sprintf(format, args...) reply.message = fmt.Sprintf(format, args...)
reply.source = source
return reply return reply
} }
@ -70,14 +76,14 @@ func (reply *NumericReply) Format(client *Client) []string {
message := fmt.Sprintf("%03d %s %s", message := fmt.Sprintf("%03d %s %s",
reply.code, client.Nick(), reply.message) reply.code, client.Nick(), reply.message)
if Identifier(client.server) != reply.source { if Identifier(client.server) != reply.source {
message = fmt.Sprintf(":%s %s", reply.source.Id(), message) message = fmt.Sprintf(":%s %s", reply.id, message)
} }
return []string{message} return []string{message}
} }
func (reply *NumericReply) String() string { func (reply *NumericReply) String() string {
return fmt.Sprintf("Reply(source=%s, code=%d, message=%s)", return fmt.Sprintf("Reply(source=%s, code=%d, message=%s)",
reply.source, reply.code, reply.message) reply.id, reply.code, reply.message)
} }
// names reply // names reply
@ -88,12 +94,11 @@ type NamesReply struct {
} }
func NewNamesReply(channel *Channel) Reply { func NewNamesReply(channel *Channel) Reply {
return &NamesReply{ reply := &NamesReply{
BaseReply: &BaseReply{
source: channel,
},
channel: channel, channel: channel,
} }
reply.SetSource(channel)
return reply
} }
func (reply *NamesReply) Format(client *Client) []string { func (reply *NamesReply) Format(client *Client) []string {
@ -401,3 +406,7 @@ func ErrChanOPrivIsNeeded(channel *Channel) Reply {
func ErrNoMOTD(server *Server) Reply { func ErrNoMOTD(server *Server) Reply {
return NewNumericReply(server, ERR_NOMOTD, ":MOTD File is missing") return NewNumericReply(server, ERR_NOMOTD, ":MOTD File is missing")
} }
func ErrNoNicknameGiven(server *Server) Reply {
return NewNumericReply(server, ERR_NONICKNAMEGIVEN, ":No nickname given")
}

View File

@ -233,11 +233,17 @@ func (m *PassCommand) HandleServer(s *Server) {
func (m *NickCommand) HandleServer(s *Server) { func (m *NickCommand) HandleServer(s *Server) {
c := m.Client() c := m.Client()
if m.nickname == "" {
c.Reply(ErrNoNicknameGiven(s))
return
}
if s.clients[m.nickname] != nil { if s.clients[m.nickname] != nil {
c.Reply(ErrNickNameInUse(s, m.nickname)) c.Reply(ErrNickNameInUse(s, m.nickname))
return return
} }
// Make reply before changing nick.
reply := RplNick(c, m.nickname) reply := RplNick(c, m.nickname)
s.clients.Remove(c) s.clients.Remove(c)

View File

@ -1,6 +1,7 @@
package irc package irc
import ( import (
"errors"
"fmt" "fmt"
) )
@ -51,9 +52,17 @@ func (channels ChannelNameMap) Remove(channel *Channel) error {
type ClientNameMap map[string]*Client type ClientNameMap map[string]*Client
var (
ErrNickMissing = errors.New("nick missing")
ErrNicknameInUse = errors.New("nickname in use")
)
func (clients ClientNameMap) Add(client *Client) error { func (clients ClientNameMap) Add(client *Client) error {
if !client.HasNick() {
return ErrNickMissing
}
if clients[client.nick] != nil { if clients[client.nick] != nil {
return fmt.Errorf("%s: already set", client.nick) return ErrNicknameInUse
} }
clients[client.nick] = client clients[client.nick] = client
return nil return nil