From d54c8c1122a589df1bdd5407fc9a41fbf15f9072 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 9 Mar 2021 18:54:38 +0100 Subject: [PATCH] Add Network.{URL,GetUsername,GetRealname} Just a bunch of helpers that can be re-used. --- db.go | 30 ++++++++++++++++++++++++++++++ upstream.go | 21 ++++----------------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/db.go b/db.go index 85f02c4..85fdec9 100644 --- a/db.go +++ b/db.go @@ -4,6 +4,7 @@ import ( "database/sql" "fmt" "math" + "net/url" "strings" "sync" "time" @@ -54,6 +55,35 @@ func (net *Network) GetName() string { 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 const ( diff --git a/upstream.go b/upstream.go index 46ce203..c534425 100644 --- a/upstream.go +++ b/upstream.go @@ -10,7 +10,6 @@ import ( "fmt" "io" "net" - "net/url" "strconv" "strings" "time" @@ -109,15 +108,9 @@ func connectToUpstream(network *network) (*upstreamConn, error) { dialer := net.Dialer{Timeout: connectTimeout} - s := network.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) + u, err := network.URL() if err != nil { - return nil, fmt.Errorf("failed to parse upstream server URL: %v", err) + return nil, err } var netConn net.Conn @@ -1566,14 +1559,8 @@ func splitSpace(s string) []string { func (uc *upstreamConn) register() { uc.nick = uc.network.Nick - uc.username = uc.network.Username - if uc.username == "" { - uc.username = uc.nick - } - uc.realname = uc.network.Realname - if uc.realname == "" { - uc.realname = uc.nick - } + uc.username = uc.network.GetUsername() + uc.realname = uc.network.GetRealname() uc.SendMessage(&irc.Message{ Command: "CAP",