Allow changing nickname

This commit is contained in:
Simon Ser 2020-02-07 12:19:42 +01:00
parent 50fc19c92f
commit 6d03af8243
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 33 additions and 8 deletions

View File

@ -234,16 +234,18 @@ func (c *downstreamConn) register() error {
func (c *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
switch msg.Command {
case "NICK", "USER":
case "USER":
return ircError{&irc.Message{
Command: irc.ERR_ALREADYREGISTERED,
Params: []string{
c.nick,
"You may not reregister",
},
Params: []string{c.nick, "You may not reregister"},
}}
case "NICK":
c.user.forEachUpstream(func(uc *upstreamConn) {
uc.messages <- msg
})
default:
c.logger.Printf("unhandled message: %v", msg)
return newUnknownCommandError(msg.Command)
}
return nil
}

View File

@ -38,6 +38,7 @@ type upstreamConn struct {
channelModesWithParam string
registered bool
nick string
closed bool
modes modeSet
channels map[string]*upstreamChannel
@ -112,7 +113,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
modeStr := msg.Params[1]
if name == msg.Prefix.Name { // user mode change
if name != c.upstream.Nick {
if name != c.nick {
return fmt.Errorf("received MODE message for unknow nick %q", name)
}
return c.modes.Apply(modeStr)
@ -151,13 +152,34 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
if len(msg.Params) > 5 {
c.channelModesWithParam = msg.Params[5]
}
case "NICK":
if len(msg.Params) < 1 {
return newNeedMoreParamsError(msg.Command)
}
newNick := msg.Params[0]
if msg.Prefix.Name == c.nick {
c.logger.Printf("changed nick from %q to %q", c.nick, newNick)
c.nick = newNick
}
for _, ch := range c.channels {
if membership, ok := ch.Members[msg.Prefix.Name]; ok {
delete(ch.Members, msg.Prefix.Name)
ch.Members[newNick] = membership
}
}
c.user.forEachDownstream(func(dc *downstreamConn) {
dc.messages <- msg
})
case "JOIN":
if len(msg.Params) < 1 {
return newNeedMoreParamsError(msg.Command)
}
for _, ch := range strings.Split(msg.Params[0], ",") {
if msg.Prefix.Name == c.upstream.Nick {
if msg.Prefix.Name == c.nick {
c.logger.Printf("joined channel %q", ch)
c.channels[ch] = &upstreamChannel{
Name: ch,
@ -181,7 +203,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
}
for _, ch := range strings.Split(msg.Params[0], ",") {
if msg.Prefix.Name == c.upstream.Nick {
if msg.Prefix.Name == c.nick {
c.logger.Printf("parted channel %q", ch)
delete(c.channels, ch)
} else {
@ -295,6 +317,7 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
func (c *upstreamConn) readMessages() error {
defer c.Close()
c.nick = c.upstream.Nick
c.messages <- &irc.Message{
Command: "NICK",
Params: []string{c.upstream.Nick},