Handle nick collisions by right-padding with _

This commit is contained in:
Ken-Håvard Lieng 2017-04-06 23:26:58 +02:00
parent 50dc0ef64f
commit 961c0415a0
8 changed files with 51 additions and 27 deletions

File diff suppressed because one or more lines are too long

View File

@ -77,4 +77,6 @@ export default function handleSocket(socket, { dispatch, getState }) {
`Channels: ${data.channels}` `Channels: ${data.channels}`
], tab.server, tab.channel)); ], tab.server, tab.channel));
}); });
socket.on('print', ({ server, message }) => dispatch(inform(message, server)));
} }

View File

@ -21,6 +21,7 @@ type Client struct {
Realname string Realname string
Messages chan *Message Messages chan *Message
ConnectionChanged chan bool ConnectionChanged chan bool
HandleNickInUse func(string) string
nick string nick string
channels []string channels []string

View File

@ -188,6 +188,12 @@ func (c *Client) recv() {
case ReplyWelcome: case ReplyWelcome:
c.once.Do(c.ready.Done) c.once.Do(c.ready.Done)
case "433":
if c.HandleNickInUse != nil {
c.nick = c.HandleNickInUse(c.nick)
c.writeNick(c.nick)
}
} }
} }
} }

View File

@ -10,6 +10,17 @@ import (
"github.com/khlieng/dispatch/storage" "github.com/khlieng/dispatch/storage"
) )
func createNickInUseHandler(i *irc.Client, session *Session) func(string) string {
return func(nick string) string {
newNick := nick + "_"
session.print(i.Host, "Nickname", nick, "is already in use, using", newNick, "instead")
go session.user.SetNick(newNick, i.Host)
return newNick
}
}
func reconnectIRC() { func reconnectIRC() {
for _, user := range storage.LoadUsers() { for _, user := range storage.LoadUsers() {
session := NewSession(user) session := NewSession(user)
@ -23,6 +34,7 @@ func reconnectIRC() {
i.TLS = server.TLS i.TLS = server.TLS
i.Password = server.Password i.Password = server.Password
i.Realname = server.Realname i.Realname = server.Realname
i.HandleNickInUse = createNickInUseHandler(i, session)
if cert := user.GetCertificate(); cert != nil { if cert := user.GetCertificate(); cert != nil {
i.TLSConfig = &tls.Config{ i.TLSConfig = &tls.Config{

View File

@ -64,7 +64,7 @@ type Quit struct {
type Chat struct { type Chat struct {
Server string `json:"server"` Server string `json:"server"`
From string `json:"from"` From string `json:"from,omitempty"`
To string `json:"to,omitempty"` To string `json:"to,omitempty"`
Message string `json:"message"` Message string `json:"message"`
} }

View File

@ -4,6 +4,8 @@ import (
"sync" "sync"
"time" "time"
"fmt"
"github.com/khlieng/dispatch/irc" "github.com/khlieng/dispatch/irc"
"github.com/khlieng/dispatch/storage" "github.com/khlieng/dispatch/storage"
) )
@ -125,6 +127,13 @@ func (s *Session) sendError(err error, server string) {
}) })
} }
func (s *Session) print(server string, a ...interface{}) {
s.sendJSON("print", Chat{
Server: server,
Message: fmt.Sprintln(a...),
})
}
func (s *Session) resetExpirationIfEmpty() { func (s *Session) resetExpirationIfEmpty() {
if s.numIRC() == 0 && s.numWS() == 0 { if s.numIRC() == 0 && s.numWS() == 0 {
s.reset <- AnonymousSessionExpiration s.reset <- AnonymousSessionExpiration

View File

@ -63,13 +63,6 @@ func (h *wsHandler) init() {
h.session.numWS(), "WebSocket connections") h.session.numWS(), "WebSocket connections")
channels := h.session.user.GetChannels() channels := h.session.user.GetChannels()
/*for i, channel := range channels {
channels[i].Topic = channelStore.GetTopic(channel.Server, channel.Name)
}
h.session.sendJSON("channels", channels)
h.session.sendJSON("servers", h.session.user.GetServers())
h.session.sendJSON("connection_update", h.session.getConnectionStates())*/
for _, channel := range channels { for _, channel := range channels {
h.session.sendJSON("users", Userlist{ h.session.sendJSON("users", Userlist{
@ -95,6 +88,7 @@ func (h *wsHandler) connect(b []byte) {
i := irc.NewClient(data.Nick, data.Username) i := irc.NewClient(data.Nick, data.Username)
i.TLS = data.TLS i.TLS = data.TLS
i.Realname = data.Realname i.Realname = data.Realname
i.HandleNickInUse = createNickInUseHandler(i, h.session)
if data.Password == "" && if data.Password == "" &&
viper.GetString("defaults.password") != "" && viper.GetString("defaults.password") != "" &&