Add Network.{URL,GetUsername,GetRealname}

Just a bunch of helpers that can be re-used.
This commit is contained in:
Simon Ser 2021-03-09 18:54:38 +01:00
parent 1645371276
commit d54c8c1122
2 changed files with 34 additions and 17 deletions

30
db.go
View File

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"math" "math"
"net/url"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -54,6 +55,35 @@ func (net *Network) GetName() string {
return net.Addr return net.Addr
} }
func (net *Network) URL() (*url.URL, error) {
s := net.Addr
if !strings.Contains(s, "://") {
// This is a raw domain name, make it an URL with the default scheme
s = "ircs://" + s
}
u, err := url.Parse(s)
if err != nil {
return nil, fmt.Errorf("failed to parse upstream server URL: %v", err)
}
return u, nil
}
func (net *Network) GetUsername() string {
if net.Username != "" {
return net.Username
}
return net.Nick
}
func (net *Network) GetRealname() string {
if net.Realname != "" {
return net.Realname
}
return net.Nick
}
type MessageFilter int type MessageFilter int
const ( const (

View File

@ -10,7 +10,6 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"net/url"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -109,15 +108,9 @@ func connectToUpstream(network *network) (*upstreamConn, error) {
dialer := net.Dialer{Timeout: connectTimeout} dialer := net.Dialer{Timeout: connectTimeout}
s := network.Addr u, err := network.URL()
if !strings.Contains(s, "://") {
// This is a raw domain name, make it an URL with the default scheme
s = "ircs://" + s
}
u, err := url.Parse(s)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse upstream server URL: %v", err) return nil, err
} }
var netConn net.Conn var netConn net.Conn
@ -1566,14 +1559,8 @@ func splitSpace(s string) []string {
func (uc *upstreamConn) register() { func (uc *upstreamConn) register() {
uc.nick = uc.network.Nick uc.nick = uc.network.Nick
uc.username = uc.network.Username uc.username = uc.network.GetUsername()
if uc.username == "" { uc.realname = uc.network.GetRealname()
uc.username = uc.nick
}
uc.realname = uc.network.Realname
if uc.realname == "" {
uc.realname = uc.nick
}
uc.SendMessage(&irc.Message{ uc.SendMessage(&irc.Message{
Command: "CAP", Command: "CAP",