Skip to content

WIP: use xml Name #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func authPlain(socket io.ReadWriter, decoder *xml.Decoder, user string, password
// v.Any is type of sub-element in failure, which gives a description of what failed.
return errors.New("auth failure: " + v.Any.Local)
default:
return errors.New("expected SASL success or failure, got " + v.Name())
return errors.New("expected SASL success or failure, got " + v.Name().Local)
}
return err
}
Expand All @@ -60,8 +60,8 @@ type SASLSuccess struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl success"`
}

func (SASLSuccess) Name() string {
return "sasl:success"
func (s SASLSuccess) Name() xml.Name {
return s.XMLName
}

type saslSuccessDecoder struct{}
Expand All @@ -82,8 +82,8 @@ type SASLFailure struct {
Any xml.Name // error reason is a subelement
}

func (SASLFailure) Name() string {
return "sasl:failure"
func (x SASLFailure) Name() xml.Name {
return x.XMLName
}

type saslFailureDecoder struct{}
Expand Down Expand Up @@ -111,6 +111,10 @@ type BindBind struct {
Jid string `xml:"jid,omitempty"`
}

func (b BindBind) Name() xml.Name {
return b.XMLName
}

// Session is obsolete in RFC 6121.
// Added for compliance with RFC 3121.
// Remove when ejabberd purely conforms to RFC 6121.
Expand Down
2 changes: 1 addition & 1 deletion check_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *ServerCheck) Check() error {
case StreamError:
return errors.New("open stream error: " + p.Error.Local)
default:
return errors.New("expected packet received while expecting features, got " + p.Name())
return errors.New("expected packet received while expecting features, got " + p.Name().Local)
}

startTLSFeature := f.StartTLS.XMLName.Space + " " + f.StartTLS.XMLName.Local
Expand Down
6 changes: 3 additions & 3 deletions component.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *Component) Connect(connStr string) error {
case Handshake:
return nil
default:
return errors.New("unexpected packet, got " + v.Name())
return errors.New("unexpected packet, got " + v.Name().Local)
}
panic("unreachable")
}
Expand Down Expand Up @@ -128,8 +128,8 @@ type Handshake struct {
// Value string `xml:",innerxml"`
}

func (Handshake) Name() string {
return "component:handshake"
func (h Handshake) Name() xml.Name {
return h.XMLName
}

// Handshake decoding wrapper
Expand Down
6 changes: 5 additions & 1 deletion iot_control.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xmpp // import "gosrc.io/xmpp/iot"
package xmpp // import "gosrc.io/xmpp"

import (
"encoding/xml"
Expand All @@ -10,6 +10,10 @@ type ControlSet struct {
Fields []ControlField `xml:",any"`
}

func (c ControlSet) Name() xml.Name {
return c.XMLName
}

type ControlGetForm struct {
XMLName xml.Name `xml:"urn:xmpp:iot:control getForm"`
}
Expand Down
20 changes: 14 additions & 6 deletions iq.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func (iq IQ) MakeError(xerror Err) IQ {
return iq
}

func (IQ) Name() string {
return "iq"
func (iq IQ) Name() xml.Name {
return iq.XMLName
}

type iqDecoder struct{}
Expand Down Expand Up @@ -289,6 +289,10 @@ type DiscoInfo struct {
Features []Feature `xml:"feature"`
}

func (d DiscoInfo) Name() xml.Name {
return d.XMLName
}

type Identity struct {
XMLName xml.Name `xml:"identity,omitempty"`
Name string `xml:"name,attr,omitempty"`
Expand All @@ -310,6 +314,10 @@ type DiscoItems struct {
Items []DiscoItem `xml:"item"`
}

func (d DiscoItems) Name() xml.Name {
return d.XMLName
}

type DiscoItem struct {
XMLName xml.Name `xml:"item"`
Name string `xml:"name,attr,omitempty"`
Expand All @@ -318,8 +326,8 @@ type DiscoItem struct {
}

func init() {
typeRegistry.MapExtension(PKTIQ, xml.Name{"http://jabber.org/protocol/disco#info", "query"}, DiscoInfo{})
typeRegistry.MapExtension(PKTIQ, xml.Name{"http://jabber.org/protocol/disco#items", "query"}, DiscoItems{})
typeRegistry.MapExtension(PKTIQ, xml.Name{"urn:ietf:params:xml:ns:xmpp-bind", "bind"}, BindBind{})
typeRegistry.MapExtension(PKTIQ, xml.Name{"urn:xmpp:iot:control", "set"}, ControlSet{})
typeRegistry.MapExtension(PKTIQ, DiscoInfo{})
typeRegistry.MapExtension(PKTIQ, DiscoItems{})
typeRegistry.MapExtension(PKTIQ, BindBind{})
typeRegistry.MapExtension(PKTIQ, ControlSet{})
}
4 changes: 2 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type Message struct {
Extensions []MsgExtension `xml:",omitempty"`
}

func (Message) Name() string {
return "message"
func (msg Message) Name() xml.Name {
return msg.XMLName
}

func NewMessage(msgtype, from, to, id, lang string) Message {
Expand Down
12 changes: 10 additions & 2 deletions msg_chat_markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ type Markable struct {
XMLName xml.Name `xml:"urn:xmpp:chat-markers:0 markable"`
}

func (m Markable) Name() xml.Name {
return m.XMLName
}

type MarkReceived struct {
MsgExtension
XMLName xml.Name `xml:"urn:xmpp:chat-markers:0 received"`
ID string
}

func (m MarkReceived) Name() xml.Name {
return m.XMLName
}

func init() {
typeRegistry.MapExtension(PKTMessage, xml.Name{"urn:xmpp:chat-markers:0", "markable"}, Markable{})
typeRegistry.MapExtension(PKTMessage, xml.Name{"urn:xmpp:chat-markers:0", "received"}, MarkReceived{})
typeRegistry.MapExtension(PKTMessage, Markable{})
typeRegistry.MapExtension(PKTMessage, MarkReceived{})
}
6 changes: 5 additions & 1 deletion msg_oob.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type OOB struct {
Desc string `xml:"desc,omitempty"`
}

func (o OOB) Name() xml.Name {
return o.XMLName
}

func init() {
typeRegistry.MapExtension(PKTMessage, xml.Name{"jabber:x:oob", "x"}, OOB{})
typeRegistry.MapExtension(PKTMessage, OOB{})
}
12 changes: 10 additions & 2 deletions msg_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ type ReceiptRequest struct {
XMLName xml.Name `xml:"urn:xmpp:receipts request"`
}

func (r ReceiptRequest) Name() xml.Name {
return r.XMLName
}

type ReceiptReceived struct {
MsgExtension
XMLName xml.Name `xml:"urn:xmpp:receipts received"`
ID string
}

func (r ReceiptReceived) Name() xml.Name {
return r.XMLName
}

func init() {
typeRegistry.MapExtension(PKTMessage, xml.Name{"urn:xmpp:receipts", "request"}, ReceiptRequest{})
typeRegistry.MapExtension(PKTMessage, xml.Name{"urn:xmpp:receipts", "received"}, ReceiptReceived{})
typeRegistry.MapExtension(PKTMessage, ReceiptRequest{})
typeRegistry.MapExtension(PKTMessage, ReceiptReceived{})
}
4 changes: 3 additions & 1 deletion packet.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package xmpp // import "gosrc.io/xmpp"

import "encoding/xml"

type Packet interface {
Name() string
Name() xml.Name
}

// PacketAttrs represents the common structure for base XMPP packets.
Expand Down
30 changes: 15 additions & 15 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,54 +88,54 @@ func next(p *xml.Decoder) (Packet, error) {

func decodeStream(p *xml.Decoder, se xml.StartElement) (Packet, error) {
switch se.Name.Local {
case "error":
case StreamError{}.Name().Local:
return streamError.decode(p, se)
case "features":
case StreamFeatures{}.Name().Local:
return streamFeatures.decode(p, se)
default:
return nil, errors.New("unexpected XMPP packet " +
return nil, errors.New("unexpected stream XMPP packet " +
se.Name.Space + " <" + se.Name.Local + "/>")
}
}

func decodeSASL(p *xml.Decoder, se xml.StartElement) (Packet, error) {
switch se.Name.Local {
case "success":
case SASLSuccess{}.Name().Local:
return saslSuccess.decode(p, se)
case "failure":
case SASLFailure{}.Name().Local:
return saslFailure.decode(p, se)
default:
return nil, errors.New("unexpected XMPP packet " +
return nil, errors.New("unexpected sasl XMPP packet " +
se.Name.Space + " <" + se.Name.Local + "/>")
}
}

func decodeClient(p *xml.Decoder, se xml.StartElement) (Packet, error) {
switch se.Name.Local {
case "message":
case Message{}.Name().Local:
return message.decode(p, se)
case "presence":
case Presence{}.Name().Local:
return presence.decode(p, se)
case "iq":
case IQ{}.Name().Local:
return iq.decode(p, se)
default:
return nil, errors.New("unexpected XMPP packet " +
return nil, errors.New("unexpected client XMPP packet " +
se.Name.Space + " <" + se.Name.Local + "/>")
}
}

func decodeComponent(p *xml.Decoder, se xml.StartElement) (Packet, error) {
switch se.Name.Local {
case "handshake":
case Handshake{}.Name().Local:
return handshake.decode(p, se)
case "message":
case Message{}.Name().Local:
return message.decode(p, se)
case "presence":
case Presence{}.Name().Local:
return presence.decode(p, se)
case "iq":
case IQ{}.Name().Local:
return iq.decode(p, se)
default:
return nil, errors.New("unexpected XMPP packet " +
return nil, errors.New("unexpected component XMPP packet " +
se.Name.Space + " <" + se.Name.Local + "/>")
}
}
2 changes: 1 addition & 1 deletion pep.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xmpp // import "gosrc.io/xmpp/pep"
package xmpp // import "gosrc.io/xmpp"

import (
"encoding/xml"
Expand Down
4 changes: 2 additions & 2 deletions presence.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type Presence struct {
Error Err `xml:"error,omitempty"`
}

func (Presence) Name() string {
return "presence"
func (p Presence) Name() xml.Name {
return p.XMLName
}

func NewPresence(from, to, id, lang string) Presence {
Expand Down
7 changes: 5 additions & 2 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"sync"
)

type MsgExtension interface{}
type MsgExtension interface {
Name() xml.Name
}

// The Registry for msg and IQ types is a global variable.
// TODO: Move to the client init process to remove the dependency on a global variable.
Expand Down Expand Up @@ -49,7 +51,8 @@ func newRegistry() *registry {
// The match is done per packetType (iq, message, or presence) and XML tag name.
// You can use the alias "*" as local XML name to be able to match all unknown tag name for that
// packet type and namespace.
func (r *registry) MapExtension(pktType packetType, name xml.Name, extension MsgExtension) {
func (r *registry) MapExtension(pktType packetType, extension MsgExtension) {
name := extension.Name()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not work alike expected yet.
We need to read the golang xml tag annotation -> a new created xml.Name does not contain a value in Local and Space

key := registryKey{pktType, name.Space}
r.msgTypesLock.RLock()
store := r.msgTypes[key]
Expand Down
16 changes: 9 additions & 7 deletions registry_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package xmpp // import "gosrc.io/xmpp"

import (
"encoding/xml"
"reflect"
"testing"
)
Expand All @@ -11,11 +10,14 @@ func TestRegistry_RegisterMsgExt(t *testing.T) {
typeRegistry := newRegistry()

// Register an element
name := xml.Name{Space: "urn:xmpp:receipts", Local: "request"}
typeRegistry.MapExtension(PKTMessage, name, ReceiptRequest{})
req := ReceiptRequest{}
res := ReceiptReceived{}

typeRegistry.MapExtension(PKTMessage, req)
typeRegistry.MapExtension(PKTMessage, res)

// Match that element
receipt := typeRegistry.GetMsgExtension(name)
receipt := typeRegistry.GetMsgExtension(req.Name())
if receipt == nil {
t.Error("cannot read element type from registry")
return
Expand All @@ -33,12 +35,12 @@ func BenchmarkRegistryGet(b *testing.B) {
typeRegistry := newRegistry()

// Register an element
name := xml.Name{Space: "urn:xmpp:receipts", Local: "request"}
typeRegistry.MapExtension(PKTMessage, name, ReceiptRequest{})
req := ReceiptRequest{}
typeRegistry.MapExtension(PKTMessage, req)

for i := 0; i < b.N; i++ {
// Match that element
receipt := typeRegistry.GetExtensionType(PKTMessage, name)
receipt := typeRegistry.GetExtensionType(PKTMessage, req.Name())
if receipt == nil {
b.Error("cannot read element type from registry")
return
Expand Down
8 changes: 4 additions & 4 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type StreamFeatures struct {
Any []xml.Name `xml:",any"`
}

func (StreamFeatures) Name() string {
return "stream:features"
func (s StreamFeatures) Name() xml.Name {
return s.XMLName
}

type streamFeatureDecoder struct{}
Expand All @@ -39,8 +39,8 @@ type StreamError struct {
Error xml.Name `xml:",any"`
}

func (StreamError) Name() string {
return "stream:error"
func (s StreamError) Name() xml.Name {
return s.XMLName
}

type streamErrorDecoder struct{}
Expand Down