mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2024-11-21 10:02:00 -08:00
Expose JID fields and rename to match XEP-0029 wording
See: XEP-0029 - Definition of Jabber Identifiers (JIDs) https://xmpp.org/extensions/xep-0029.html
This commit is contained in:
parent
269f78b30d
commit
1be04b0fba
24
jid.go
24
jid.go
@ -7,9 +7,9 @@ import (
|
||||
)
|
||||
|
||||
type Jid struct {
|
||||
username string
|
||||
domain string
|
||||
resource string
|
||||
Node string
|
||||
Domain string
|
||||
Resource string
|
||||
}
|
||||
|
||||
func NewJid(sjid string) (*Jid, error) {
|
||||
@ -21,29 +21,29 @@ func NewJid(sjid string) (*Jid, error) {
|
||||
|
||||
s1 := strings.SplitN(sjid, "@", 2)
|
||||
if len(s1) == 1 { // This is a server or component JID
|
||||
jid.domain = s1[0]
|
||||
jid.Domain = s1[0]
|
||||
} else { // JID has a local username part
|
||||
if s1[0] == "" {
|
||||
return jid, fmt.Errorf("invalid jid '%s", sjid)
|
||||
}
|
||||
jid.username = s1[0]
|
||||
jid.Node = s1[0]
|
||||
if s1[1] == "" {
|
||||
return jid, fmt.Errorf("domain cannot be empty")
|
||||
}
|
||||
jid.domain = s1[1]
|
||||
jid.Domain = s1[1]
|
||||
}
|
||||
|
||||
// Extract resource from domain field
|
||||
s2 := strings.SplitN(jid.domain, "/", 2)
|
||||
s2 := strings.SplitN(jid.Domain, "/", 2)
|
||||
if len(s2) == 2 { // If len = 1, domain is already correct, and resource is already empty
|
||||
jid.domain = s2[0]
|
||||
jid.resource = s2[1]
|
||||
jid.Domain = s2[0]
|
||||
jid.Resource = s2[1]
|
||||
}
|
||||
|
||||
if !isUsernameValid(jid.username) {
|
||||
return jid, fmt.Errorf("invalid username in JID '%s'", sjid)
|
||||
if !isUsernameValid(jid.Node) {
|
||||
return jid, fmt.Errorf("invalid Node in JID '%s'", sjid)
|
||||
}
|
||||
if !isDomainValid(jid.domain) {
|
||||
if !isDomainValid(jid.Domain) {
|
||||
return jid, fmt.Errorf("invalid domain in JID '%s'", sjid)
|
||||
}
|
||||
|
||||
|
12
jid_test.go
12
jid_test.go
@ -28,16 +28,16 @@ func TestValidJids(t *testing.T) {
|
||||
t.Error("jid should not be nil")
|
||||
}
|
||||
|
||||
if jid.username != tt.expected.username {
|
||||
t.Errorf("incorrect jid username (%s): %s", tt.expected.username, jid.username)
|
||||
if jid.Node != tt.expected.Node {
|
||||
t.Errorf("incorrect jid Node (%s): %s", tt.expected.Node, jid.Node)
|
||||
}
|
||||
|
||||
if jid.username != tt.expected.username {
|
||||
t.Errorf("incorrect jid domain (%s): %s", tt.expected.domain, jid.domain)
|
||||
if jid.Node != tt.expected.Node {
|
||||
t.Errorf("incorrect jid domain (%s): %s", tt.expected.Domain, jid.Domain)
|
||||
}
|
||||
|
||||
if jid.resource != tt.expected.resource {
|
||||
t.Errorf("incorrect jid resource (%s): %s", tt.expected.resource, jid.resource)
|
||||
if jid.Resource != tt.expected.Resource {
|
||||
t.Errorf("incorrect jid resource (%s): %s", tt.expected.Resource, jid.Resource)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
10
session.go
10
session.go
@ -37,7 +37,7 @@ func NewSession(conn net.Conn, o Config) (net.Conn, *Session, error) {
|
||||
|
||||
// starttls
|
||||
var tlsConn net.Conn
|
||||
tlsConn = s.startTlsIfSupported(conn, o.parsedJid.domain)
|
||||
tlsConn = s.startTlsIfSupported(conn, o.parsedJid.Domain)
|
||||
if s.TlsEnabled {
|
||||
s.reset(conn, tlsConn, o)
|
||||
}
|
||||
@ -64,7 +64,7 @@ func (s *Session) PacketId() string {
|
||||
|
||||
func (s *Session) init(conn net.Conn, o Config) {
|
||||
s.setProxy(nil, conn, o)
|
||||
s.Features = s.open(o.parsedJid.domain)
|
||||
s.Features = s.open(o.parsedJid.Domain)
|
||||
}
|
||||
|
||||
func (s *Session) reset(conn net.Conn, newConn net.Conn, o Config) {
|
||||
@ -73,7 +73,7 @@ func (s *Session) reset(conn net.Conn, newConn net.Conn, o Config) {
|
||||
}
|
||||
|
||||
s.setProxy(conn, newConn, o)
|
||||
s.Features = s.open(o.parsedJid.domain)
|
||||
s.Features = s.open(o.parsedJid.Domain)
|
||||
}
|
||||
|
||||
// TODO: setProxyLogger ? better name ? This is not a TCP / HTTP proxy
|
||||
@ -141,7 +141,7 @@ func (s *Session) auth(o Config) {
|
||||
return
|
||||
}
|
||||
|
||||
s.err = authSASL(s.socketProxy, s.decoder, s.Features, o.parsedJid.username, o.Password)
|
||||
s.err = authSASL(s.socketProxy, s.decoder, s.Features, o.parsedJid.Node, o.Password)
|
||||
}
|
||||
|
||||
func (s *Session) bind(o Config) {
|
||||
@ -150,7 +150,7 @@ func (s *Session) bind(o Config) {
|
||||
}
|
||||
|
||||
// Send IQ message asking to bind to the local user name.
|
||||
var resource = o.parsedJid.resource
|
||||
var resource = o.parsedJid.Resource
|
||||
if resource != "" {
|
||||
fmt.Fprintf(s.socketProxy, "<iq type='set' id='%s'><bind xmlns='%s'><resource>%s</resource></bind></iq>",
|
||||
s.PacketId(), nsBind, resource)
|
||||
|
Loading…
Reference in New Issue
Block a user