diff --git a/db.go b/db.go index d30352d..3c7b6a5 100644 --- a/db.go +++ b/db.go @@ -112,6 +112,13 @@ func (net *Network) GetUsername() string { return net.Nick } +func GetNick(user *User, net *Network) string { + if net.Nick != "" { + return net.Nick + } + return user.Username +} + func GetRealname(user *User, net *Network) string { if net.Realname != "" { return net.Realname @@ -119,7 +126,7 @@ func GetRealname(user *User, net *Network) string { if user.Realname != "" { return user.Realname } - return net.Nick + return GetNick(user, net) } type MessageFilter int diff --git a/db_postgres.go b/db_postgres.go index d4d4b11..7db1edc 100644 --- a/db_postgres.go +++ b/db_postgres.go @@ -36,7 +36,7 @@ CREATE TABLE "Network" ( name VARCHAR(255), "user" INTEGER NOT NULL REFERENCES "User"(id) ON DELETE CASCADE, addr VARCHAR(255) NOT NULL, - nick VARCHAR(255) NOT NULL, + nick VARCHAR(255), username VARCHAR(255), realname VARCHAR(255), pass VARCHAR(255), @@ -77,6 +77,7 @@ CREATE TABLE "DeliveryReceipt" ( var postgresMigrations = []string{ "", // migration #0 is reserved for schema initialization + `ALTER TABLE "Network" ALTER COLUMN nick DROP NOT NULL`, } type PostgresDB struct { @@ -259,15 +260,16 @@ func (db *PostgresDB) ListNetworks(ctx context.Context, userID int64) ([]Network var networks []Network for rows.Next() { var net Network - var name, username, realname, pass, connectCommands sql.NullString + var name, nick, username, realname, pass, connectCommands sql.NullString var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString - err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname, + err := rows.Scan(&net.ID, &name, &net.Addr, &nick, &username, &realname, &pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword, &net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob, &net.Enabled) if err != nil { return nil, err } net.Name = name.String + net.Nick = nick.String net.Username = username.String net.Realname = realname.String net.Pass = pass.String @@ -291,6 +293,7 @@ func (db *PostgresDB) StoreNetwork(ctx context.Context, userID int64, network *N defer cancel() netName := toNullString(network.Name) + nick := toNullString(network.Nick) netUsername := toNullString(network.Username) realname := toNullString(network.Realname) pass := toNullString(network.Pass) @@ -320,7 +323,7 @@ func (db *PostgresDB) StoreNetwork(ctx context.Context, userID int64, network *N sasl_external_key, enabled) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING id`, - userID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands, + userID, netName, network.Addr, nick, netUsername, realname, pass, connectCommands, saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob, network.SASL.External.PrivKeyBlob, network.Enabled).Scan(&network.ID) } else { @@ -331,7 +334,7 @@ func (db *PostgresDB) StoreNetwork(ctx context.Context, userID int64, network *N sasl_plain_password = $11, sasl_external_cert = $12, sasl_external_key = $13, enabled = $14 WHERE id = $1`, - network.ID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands, + network.ID, netName, network.Addr, nick, netUsername, realname, pass, connectCommands, saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob, network.SASL.External.PrivKeyBlob, network.Enabled) } diff --git a/db_sqlite.go b/db_sqlite.go index 83b914a..1616c1e 100644 --- a/db_sqlite.go +++ b/db_sqlite.go @@ -28,7 +28,7 @@ CREATE TABLE Network ( name TEXT, user INTEGER NOT NULL, addr TEXT NOT NULL, - nick TEXT NOT NULL, + nick TEXT, username TEXT, realname TEXT, pass TEXT, @@ -138,6 +138,36 @@ var sqliteMigrations = []string{ "ALTER TABLE Channel ADD COLUMN detached_internal_msgid VARCHAR(255)", "ALTER TABLE Network ADD COLUMN enabled INTEGER NOT NULL DEFAULT 1", "ALTER TABLE User ADD COLUMN realname VARCHAR(255)", + ` + CREATE TABLE NetworkNew ( + id INTEGER PRIMARY KEY, + name TEXT, + user INTEGER NOT NULL, + addr TEXT NOT NULL, + nick TEXT, + username TEXT, + realname TEXT, + pass TEXT, + connect_commands TEXT, + sasl_mechanism TEXT, + sasl_plain_username TEXT, + sasl_plain_password TEXT, + sasl_external_cert BLOB, + sasl_external_key BLOB, + enabled INTEGER NOT NULL DEFAULT 1, + FOREIGN KEY(user) REFERENCES User(id), + UNIQUE(user, addr, nick), + UNIQUE(user, name) + ); + INSERT INTO NetworkNew + SELECT id, name, user, addr, nick, username, realname, pass, + connect_commands, sasl_mechanism, sasl_plain_username, + sasl_plain_password, sasl_external_cert, sasl_external_key, + enabled + FROM Network; + DROP TABLE Network; + ALTER TABLE NetworkNew RENAME TO Network; + `, } type SqliteDB struct { @@ -393,15 +423,16 @@ func (db *SqliteDB) ListNetworks(ctx context.Context, userID int64) ([]Network, var networks []Network for rows.Next() { var net Network - var name, username, realname, pass, connectCommands sql.NullString + var name, nick, username, realname, pass, connectCommands sql.NullString var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString - err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname, + err := rows.Scan(&net.ID, &name, &net.Addr, &nick, &username, &realname, &pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword, &net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob, &net.Enabled) if err != nil { return nil, err } net.Name = name.String + net.Nick = nick.String net.Username = username.String net.Realname = realname.String net.Pass = pass.String @@ -446,7 +477,7 @@ func (db *SqliteDB) StoreNetwork(ctx context.Context, userID int64, network *Net args := []interface{}{ sql.Named("name", toNullString(network.Name)), sql.Named("addr", network.Addr), - sql.Named("nick", network.Nick), + sql.Named("nick", toNullString(network.Nick)), sql.Named("username", toNullString(network.Username)), sql.Named("realname", toNullString(network.Realname)), sql.Named("pass", toNullString(network.Pass)), diff --git a/downstream.go b/downstream.go index 19da13d..775dadb 100644 --- a/downstream.go +++ b/downstream.go @@ -102,7 +102,7 @@ func getNetworkAttrs(network *network) irc.Tags { attrs := irc.Tags{ "name": irc.TagValue(network.GetName()), "state": irc.TagValue(state), - "nickname": irc.TagValue(network.Nick), + "nickname": irc.TagValue(GetNick(&network.user.User, &network.Network)), } if network.Username != "" { @@ -335,7 +335,7 @@ func isOurNick(net *network, nick string) bool { // know whether this name is our nickname. Best-effort: use the network's // configured nickname and hope it was the one being used when we were // connected. - return net.casemap(nick) == net.casemap(net.Nick) + return net.casemap(nick) == net.casemap(GetNick(&net.user.User, &net.Network)) } // marshalEntity converts an upstream entity name (ie. channel or nick) into a @@ -2414,6 +2414,9 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error { return err } + if record.Nick == dc.user.Username { + record.Nick = "" + } if record.Realname == dc.user.Realname { record.Realname = "" } @@ -2455,6 +2458,9 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error { return err } + if record.Nick == dc.user.Username { + record.Nick = "" + } if record.Realname == dc.user.Realname { record.Realname = "" } diff --git a/service.go b/service.go index 2748c81..a596c14 100644 --- a/service.go +++ b/service.go @@ -484,7 +484,6 @@ func handleServiceNetworkCreate(dc *downstreamConn, params []string) error { record := &Network{ Addr: *fs.Addr, - Nick: dc.nick, Enabled: true, } if err := fs.update(record); err != nil { diff --git a/upstream.go b/upstream.go index fc9b2fd..bc7df32 100644 --- a/upstream.go +++ b/upstream.go @@ -1671,7 +1671,7 @@ func splitSpace(s string) []string { } func (uc *upstreamConn) register() { - uc.nick = uc.network.Nick + uc.nick = GetNick(&uc.user.User, &uc.network.Network) uc.nickCM = uc.network.casemap(uc.nick) uc.username = uc.network.GetUsername() uc.realname = GetRealname(&uc.user.User, &uc.network.Network)