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"
"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 (

View File

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