Add support for upstream PASS command

This commit is contained in:
Simon Ser 2020-03-13 12:06:02 +01:00
parent e39948cdbd
commit fee8fc79c6
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 31 additions and 9 deletions

27
db.go
View File

@ -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
}

View File

@ -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,

View File

@ -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)
);

View File

@ -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},