Changeset 743 in code


Ignore:
Timestamp:
Dec 4, 2021, 7:07:23 PM (4 years ago)
Author:
contact
Message:

Fallback to alt nick

If the nickname we want is taken, fallback to another one by
appending underscores. Use MONITOR to figure out when we can request
our desired nick again.

Closes: https://todo.sr.ht/~emersion/soju/35

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/upstream.go

    r742 r743  
    775775                }
    776776
     777                uc.updateMonitor()
     778
    777779                uc.forEachDownstream(func(dc *downstreamConn) {
    778780                        if dc.network == nil {
     
    877879                                dc.updateNick()
    878880                        })
     881                        uc.updateMonitor()
    879882                }
    880883        case "SETNAME":
     
    15211524                        prefix := irc.ParsePrefix(target)
    15221525                        uc.monitored.SetValue(prefix.Name, online)
     1526                }
     1527
     1528                // Check if the nick we want is now free
     1529                wantNick := GetNick(&uc.user.User, &uc.network.Network)
     1530                wantNickCM := uc.network.casemap(wantNick)
     1531                if !online && uc.nickCM != wantNickCM {
     1532                        found := false
     1533                        for _, target := range targets {
     1534                                prefix := irc.ParsePrefix(target)
     1535                                if uc.network.casemap(prefix.Name) == wantNickCM {
     1536                                        found = true
     1537                                        break
     1538                                }
     1539                        }
     1540                        if found {
     1541                                uc.logger.Printf("desired nick %q is now available", wantNick)
     1542                                uc.SendMessage(&irc.Message{
     1543                                        Command: "NICK",
     1544                                        Params:  []string{wantNick},
     1545                                })
     1546                        }
    15231547                }
    15241548
     
    16881712                }
    16891713                return fmt.Errorf("fatal server error: %v", text)
    1690         case irc.ERR_PASSWDMISMATCH, irc.ERR_ERRONEUSNICKNAME, irc.ERR_NICKNAMEINUSE, irc.ERR_NICKCOLLISION, irc.ERR_UNAVAILRESOURCE, irc.ERR_NOPERMFORHOST, irc.ERR_YOUREBANNEDCREEP:
     1714        case irc.ERR_NICKNAMEINUSE:
     1715                // At this point, we haven't received ISUPPORT so we don't know the
     1716                // maximum nickname length or whether the server supports MONITOR. Many
     1717                // servers have NICKLEN=30 so let's just use that.
     1718                if !uc.registered && len(uc.nick)+1 < 30 {
     1719                        uc.nick = uc.nick + "_"
     1720                        uc.nickCM = uc.network.casemap(uc.nick)
     1721                        uc.logger.Printf("desired nick is not available, falling back to %q", uc.nick)
     1722                        uc.SendMessage(&irc.Message{
     1723                                Command: "NICK",
     1724                                Params:  []string{uc.nick},
     1725                        })
     1726                        return nil
     1727                }
     1728                fallthrough
     1729        case irc.ERR_PASSWDMISMATCH, irc.ERR_ERRONEUSNICKNAME, irc.ERR_NICKCOLLISION, irc.ERR_UNAVAILRESOURCE, irc.ERR_NOPERMFORHOST, irc.ERR_YOUREBANNEDCREEP:
    16911730                if !uc.registered {
    16921731                        return registrationError{msg}
     
    20882127                                if _, ok := add[targetCM]; !ok {
    20892128                                        addList = append(addList, targetCM)
     2129                                        add[targetCM] = struct{}{}
    20902130                                }
    2091                                 add[targetCM] = struct{}{}
    20922131                        } else {
    20932132                                seen[targetCM] = struct{}{}
     
    20952134                }
    20962135        })
     2136
     2137        wantNick := GetNick(&uc.user.User, &uc.network.Network)
     2138        wantNickCM := uc.network.casemap(wantNick)
     2139        if _, ok := add[wantNickCM]; !ok && !uc.monitored.Has(wantNick) && !uc.isOurNick(wantNick) {
     2140                addList = append(addList, wantNickCM)
     2141                add[wantNickCM] = struct{}{}
     2142        }
    20972143
    20982144        removeAll := true
Note: See TracChangeset for help on using the changeset viewer.