mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2024-11-25 03:52:02 -08:00
Refactor namespace handling
This commit is contained in:
parent
24ac2c0526
commit
d2765aec15
@ -13,6 +13,7 @@ func main() {
|
|||||||
for {
|
for {
|
||||||
_, packet, err := component.ReadPacket()
|
_, packet, err := component.ReadPacket()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("read error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("Packet received: ", packet)
|
fmt.Println("Packet received: ", packet)
|
||||||
|
91
parser.go
91
parser.go
@ -60,9 +60,8 @@ func nextStart(p *xml.Decoder) (xml.StartElement, error) {
|
|||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan XML token stream for next element and save into val.
|
// next scans XML token stream for next element and then assign a structure to decode
|
||||||
// If val == nil, allocate new element based on proto map.
|
// that elements.
|
||||||
// Either way, return val.
|
|
||||||
// TODO Use an interface to return packets interface xmppDecoder
|
// TODO Use an interface to return packets interface xmppDecoder
|
||||||
func next(p *xml.Decoder) (xml.Name, interface{}, error) {
|
func next(p *xml.Decoder) (xml.Name, interface{}, error) {
|
||||||
// Read start element to find out what type we want.
|
// Read start element to find out what type we want.
|
||||||
@ -71,26 +70,28 @@ func next(p *xml.Decoder) (xml.Name, interface{}, error) {
|
|||||||
return xml.Name{}, nil, err
|
return xml.Name{}, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put it in an interface and allocate one.
|
// Put it in an interface and allocate the right structure
|
||||||
var nv interface{}
|
var nv interface{}
|
||||||
switch se.Name.Space + " " + se.Name.Local {
|
// TODO: general case = Parse IQ / presence / message => split SASL Stream and component cases
|
||||||
case NSStream + " error":
|
switch se.Name.Space {
|
||||||
nv = &StreamError{}
|
case NSStream:
|
||||||
// TODO: general case = Parse IQ / presence / message => split SASL case
|
if nv, err = decodeStream(se); err != nil {
|
||||||
case nsSASL + " success":
|
return xml.Name{}, nil, err
|
||||||
nv = &saslSuccess{}
|
}
|
||||||
case nsSASL + " failure":
|
case nsSASL:
|
||||||
nv = &saslFailure{}
|
if nv, err = decodeSASL(se); err != nil {
|
||||||
case NSClient + " message":
|
return xml.Name{}, nil, err
|
||||||
nv = &ClientMessage{}
|
}
|
||||||
case NSClient + " presence":
|
case NSClient:
|
||||||
nv = &ClientPresence{}
|
if nv, err = decodeClient(se); err != nil {
|
||||||
case NSClient + " iq":
|
return xml.Name{}, nil, err
|
||||||
nv = &ClientIQ{}
|
}
|
||||||
case NSComponent + " handshake":
|
case NSComponent:
|
||||||
nv = &Handshake{}
|
if nv, err = decodeComponent(se); err != nil {
|
||||||
|
return xml.Name{}, nil, err
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return xml.Name{}, nil, errors.New("unexpected XMPP message " +
|
return xml.Name{}, nil, errors.New("unknown namespace " +
|
||||||
se.Name.Space + " <" + se.Name.Local + "/>")
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,3 +101,51 @@ func next(p *xml.Decoder) (xml.Name, interface{}, error) {
|
|||||||
}
|
}
|
||||||
return se.Name, nv, err
|
return se.Name, nv, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func decodeStream(se xml.StartElement) (interface{}, error) {
|
||||||
|
switch se.Name.Local {
|
||||||
|
case "error":
|
||||||
|
return &StreamError{}, nil
|
||||||
|
default:
|
||||||
|
return nil, errors.New("unexpected XMPP packet " +
|
||||||
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeSASL(se xml.StartElement) (interface{}, error) {
|
||||||
|
switch se.Name.Local {
|
||||||
|
case "success":
|
||||||
|
return &saslSuccess{}, nil
|
||||||
|
case "failure":
|
||||||
|
return &saslFailure{}, nil
|
||||||
|
default:
|
||||||
|
return nil, errors.New("unexpected XMPP packet " +
|
||||||
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeClient(se xml.StartElement) (interface{}, error) {
|
||||||
|
switch se.Name.Local {
|
||||||
|
case "message":
|
||||||
|
return &ClientMessage{}, nil
|
||||||
|
case "presence":
|
||||||
|
return &ClientPresence{}, nil
|
||||||
|
case "iq":
|
||||||
|
return &ClientIQ{}, nil
|
||||||
|
default:
|
||||||
|
return nil, errors.New("unexpected XMPP packet " +
|
||||||
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeComponent(se xml.StartElement) (interface{}, error) {
|
||||||
|
switch se.Name.Local {
|
||||||
|
case "handshake":
|
||||||
|
return &Handshake{}, nil
|
||||||
|
case "iq":
|
||||||
|
return &ClientIQ{}, nil
|
||||||
|
default:
|
||||||
|
return nil, errors.New("unexpected XMPP packet " +
|
||||||
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user