Merge pull request #108 from kjx98/master

implement DNS SRV lookup for NewClient method
This commit is contained in:
mattn 2020-01-29 00:56:32 +09:00 committed by GitHub
commit ac4c216a42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

40
xmpp.go
View File

@ -199,13 +199,35 @@ type Options struct {
// NewClient establishes a new Client connection based on a set of Options. // NewClient establishes a new Client connection based on a set of Options.
func (o Options) NewClient() (*Client, error) { func (o Options) NewClient() (*Client, error) {
host := o.Host host := o.Host
if strings.TrimSpace(host) == "" {
a := strings.SplitN(o.User, "@", 2)
if len(a) == 2 {
if _, addrs, err := net.LookupSRV("xmpp-client", "tcp", a[1]); err == nil {
if len(addrs) > 0 {
// default to first record
host = fmt.Sprintf("%s:%d", addrs[0].Target, addrs[0].Port)
defP := addrs[0].Priority
for _, adr := range addrs {
if adr.Priority < defP {
host = fmt.Sprintf("%s:%d", adr.Target, adr.Port)
defP = adr.Priority
}
}
} else {
host = a[1]
}
} else {
host = a[1]
}
}
}
c, err := connect(host, o.User, o.Password) c, err := connect(host, o.User, o.Password)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if strings.LastIndex(o.Host, ":") > 0 { if strings.LastIndex(host, ":") > 0 {
host = host[:strings.LastIndex(o.Host, ":")] host = host[:strings.LastIndex(host, ":")]
} }
client := new(Client) client := new(Client)
@ -641,13 +663,21 @@ func (c *Client) Recv() (stanza interface{}, err error) {
return Presence{v.From, v.To, v.Type, v.Show, v.Status}, nil return Presence{v.From, v.To, v.Type, v.Show, v.Status}, nil
case *clientIQ: case *clientIQ:
// TODO check more strictly // TODO check more strictly
if bytes.Equal(bytes.TrimSpace(v.Query), []byte(`<ping xmlns='urn:xmpp:ping'/>`)) || bytes.Equal(bytes.TrimSpace(v.Query), []byte(`<ping xmlns="urn:xmpp:ping"/>`)) { if v.Query.XMLName.Space == "urn:xmpp:ping" {
err := c.SendResultPing(v.ID, v.From) err := c.SendResultPing(v.ID, v.From)
if err != nil { if err != nil {
return Chat{}, err return Chat{}, err
} }
} }
return IQ{ID: v.ID, From: v.From, To: v.To, Type: v.Type, Query: v.Query}, nil if v.Query.XMLName.Local == "" {
return IQ{ID: v.ID, From: v.From, To: v.To, Type: v.Type}, nil
} else if res, err := xml.Marshal(v.Query); err != nil {
// should never occur
return Chat{}, err
} else {
return IQ{ID: v.ID, From: v.From, To: v.To, Type: v.Type,
Query: res}, nil
}
} }
} }
} }
@ -848,7 +878,7 @@ type clientIQ struct {
ID string `xml:"id,attr"` ID string `xml:"id,attr"`
To string `xml:"to,attr"` To string `xml:"to,attr"`
Type string `xml:"type,attr"` // error, get, result, set Type string `xml:"type,attr"` // error, get, result, set
Query []byte `xml:",innerxml"` Query XMLElement `xml:",any"`
Error clientError Error clientError
Bind bindBind Bind bindBind
} }