From fde524ef98abc826d85524ccd28eb9ab669301e1 Mon Sep 17 00:00:00 2001 From: genofire Date: Wed, 26 Jun 2019 09:04:03 +0200 Subject: [PATCH] fix connection to ipv6 address + use fallback to jid domain --- client.go | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/client.go b/client.go index 76de3de..d24dffe 100644 --- a/client.go +++ b/client.go @@ -85,21 +85,25 @@ Setting up the client / Checking the parameters // Default the port to 5222. // TODO: better config checks func NewClient(config Config, r *Router) (c *Client, err error) { - // TODO: If option address is nil, use the Jid domain to compose the address - if config.Address, err = checkAddress(config.Address); err != nil { - return nil, NewConnError(err, true) - } - - if config.Password == "" { - err = errors.New("missing password") - return nil, NewConnError(err, true) - } - // Parse JID if config.parsedJid, err = NewJid(config.Jid); err != nil { err = errors.New("missing jid") return nil, NewConnError(err, true) } + + if config.Password == "" { + err = errors.New("missing password") + return nil, NewConnError(err, true) + } + + // fallback to jid domain + if config.Address == "" { + config.Address = config.parsedJid.Domain + } + // if address has no port (behind his ipv6 address) - add default port + if strings.LastIndex(config.Address, ":") <= strings.LastIndex(config.Address, "]") { + config.Address += ":5222" + } c = new(Client) c.config = config @@ -112,24 +116,6 @@ func NewClient(config Config, r *Router) (c *Client, err error) { return } -// TODO Pass JID to be able to add default address based on JID, if addr is empty -func checkAddress(addr string) (string, error) { - var err error - hostport := strings.Split(addr, ":") - if len(hostport) > 2 { - err = errors.New("too many colons in xmpp server address") - return addr, err - } - - // Address is composed of two parts, we are good - if len(hostport) == 2 && hostport[1] != "" { - return addr, err - } - - // Port was not passed, we append XMPP default port: - return strings.Join([]string{hostport[0], "5222"}, ":"), err -} - // Connect triggers actual TCP connection, based on previously defined parameters. func (c *Client) Connect() error { var err error