Store NICK changes in the DB

This commit is contained in:
Simon Ser 2020-03-12 19:17:06 +01:00
parent 0c4e9b539c
commit 0ef08dfbb5
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 59 additions and 0 deletions

26
db.go
View File

@ -117,6 +117,32 @@ func (db *DB) ListNetworks(username string) ([]Network, error) {
return networks, nil
}
func (db *DB) StoreNetwork(username string, network *Network) error {
db.lock.Lock()
defer db.lock.Unlock()
var netUsername, netRealname *string
if network.Username != "" {
netUsername = &network.Username
}
if network.Realname != "" {
netRealname = &network.Realname
}
var err error
if network.ID != 0 {
_, err = db.db.Exec("UPDATE Network SET addr = ?, nick = ?, username = ?, realname = ? WHERE id = ?", network.Addr, network.Nick, netUsername, netRealname, network.ID)
} else {
var res sql.Result
res, err = db.db.Exec("INSERT INTO Network(user, addr, nick, username, realname) VALUES (?, ?, ?, ?, ?)", username, network.Addr, network.Nick, netUsername, netRealname)
if err != nil {
return err
}
network.ID, err = res.LastInsertId()
}
return err
}
func (db *DB) ListChannels(networkID int64) ([]Channel, error) {
db.lock.RLock()
defer db.lock.RUnlock()

View File

@ -105,6 +105,14 @@ func (dc *downstreamConn) marshalChannel(uc *upstreamConn, name string) string {
return name
}
func (dc *downstreamConn) forEachNetwork(f func(*network)) {
if dc.network != nil {
f(dc.network)
} else {
dc.user.forEachNetwork(f)
}
}
func (dc *downstreamConn) forEachUpstream(f func(*upstreamConn)) {
dc.user.forEachUpstream(func(uc *upstreamConn) {
if dc.network != nil && uc.network != dc.network {
@ -458,6 +466,23 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
Params: []string{dc.nick, "You may not reregister"},
}}
case "NICK":
var nick string
if err := parseMessageParams(msg, &nick); err != nil {
return err
}
var err error
dc.forEachNetwork(func(n *network) {
if err != nil {
return
}
n.Nick = nick
err = dc.srv.db.StoreNetwork(dc.user.Username, &n.Network)
})
if err != nil {
return err
}
dc.forEachUpstream(func(uc *upstreamConn) {
uc.SendMessage(msg)
})

View File

@ -109,6 +109,14 @@ func newUser(srv *Server, record *User) *user {
}
}
func (u *user) forEachNetwork(f func(*network)) {
u.lock.Lock()
for _, network := range u.networks {
f(network)
}
u.lock.Unlock()
}
func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
u.lock.Lock()
for _, network := range u.networks {