From fee8fc79c6863b348dd4e9ae2d4855f7c222059a Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 13 Mar 2020 12:06:02 +0100 Subject: [PATCH] Add support for upstream PASS command --- db.go | 27 ++++++++++++++++++++------- downstream.go | 1 + schema.sql | 1 + upstream.go | 11 +++++++++-- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/db.go b/db.go index 7a51a26..b7de816 100644 --- a/db.go +++ b/db.go @@ -18,6 +18,7 @@ type Network struct { Nick string Username string Realname string + Pass string } type Channel struct { @@ -89,7 +90,7 @@ func (db *DB) ListNetworks(username string) ([]Network, error) { db.lock.RLock() defer db.lock.RUnlock() - rows, err := db.db.Query("SELECT id, addr, nick, username, realname FROM Network WHERE user = ?", username) + rows, err := db.db.Query("SELECT id, addr, nick, username, realname, pass FROM Network WHERE user = ?", username) if err != nil { return nil, err } @@ -98,8 +99,8 @@ func (db *DB) ListNetworks(username string) ([]Network, error) { var networks []Network for rows.Next() { var net Network - var username, realname *string - if err := rows.Scan(&net.ID, &net.Addr, &net.Nick, &username, &realname); err != nil { + var username, realname, pass *string + if err := rows.Scan(&net.ID, &net.Addr, &net.Nick, &username, &realname, &pass); err != nil { return nil, err } if username != nil { @@ -108,6 +109,9 @@ func (db *DB) ListNetworks(username string) ([]Network, error) { if realname != nil { net.Realname = *realname } + if pass != nil { + net.Pass = *pass + } networks = append(networks, net) } if err := rows.Err(); err != nil { @@ -121,20 +125,29 @@ func (db *DB) StoreNetwork(username string, network *Network) error { db.lock.Lock() defer db.lock.Unlock() - var netUsername, netRealname *string + var netUsername, realname, pass *string if network.Username != "" { netUsername = &network.Username } if network.Realname != "" { - netRealname = &network.Realname + realname = &network.Realname + } + if network.Pass != "" { + pass = &network.Pass } 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) + _, err = db.db.Exec(`UPDATE Network + SET addr = ?, nick = ?, username = ?, realname = ?, pass = ? + WHERE id = ?`, + network.Addr, network.Nick, netUsername, realname, pass, 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) + res, err = db.db.Exec(`INSERT INTO Network(user, addr, nick, username, + realname, pass) + VALUES (?, ?, ?, ?, ?, ?)`, + username, network.Addr, network.Nick, netUsername, realname, pass) if err != nil { return err } diff --git a/downstream.go b/downstream.go index e4fd70a..84f8e0b 100644 --- a/downstream.go +++ b/downstream.go @@ -427,6 +427,7 @@ func (dc *downstreamConn) register() error { Command: irc.RPL_MYINFO, Params: []string{dc.nick, dc.srv.Hostname, "jounce", "aiwroO", "OovaimnqpsrtklbeI"}, }) + // TODO: RPL_ISUPPORT dc.SendMessage(&irc.Message{ Prefix: dc.srv.prefix(), Command: irc.ERR_NOMOTD, diff --git a/schema.sql b/schema.sql index bebe8d7..2a63ca8 100644 --- a/schema.sql +++ b/schema.sql @@ -10,6 +10,7 @@ CREATE TABLE Network ( nick VARCHAR(255) NOT NULL, username VARCHAR(255), realname VARCHAR(255), + pass VARCHAR(255), FOREIGN KEY(user) REFERENCES User(username), UNIQUE(user, addr, nick) ); diff --git a/upstream.go b/upstream.go index 14b21eb..a4d1e2f 100644 --- a/upstream.go +++ b/upstream.go @@ -172,8 +172,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error { if len(msg.Params) < 2 { return newNeedMoreParamsError(msg.Command) } - caps := strings.Fields(msg.Params[len(msg.Params) - 1]) - more := msg.Params[len(msg.Params) - 2] == "*" + caps := strings.Fields(msg.Params[len(msg.Params)-1]) + more := msg.Params[len(msg.Params)-2] == "*" for _, s := range caps { kv := strings.SplitN(s, "=", 2) @@ -460,6 +460,13 @@ func (uc *upstreamConn) register() { Params: []string{"LS", "302"}, }) + if uc.network.Pass != "" { + uc.SendMessage(&irc.Message{ + Command: "PASS", + Params: []string{uc.network.Pass}, + }) + } + uc.SendMessage(&irc.Message{ Command: "NICK", Params: []string{uc.nick},