structs.go
changeset 111 36287f2cf06e
parent 110 7696e6a01709
child 112 bd56fb741f69
equal deleted inserted replaced
110:7696e6a01709 111:36287f2cf06e
    75 	Chardata  string `xml:",chardata"`
    75 	Chardata  string `xml:",chardata"`
    76 	Mechanism string `xml:"mechanism,attr,omitempty"`
    76 	Mechanism string `xml:"mechanism,attr,omitempty"`
    77 	Any       *Generic
    77 	Any       *Generic
    78 }
    78 }
    79 
    79 
       
    80 type Stanzer interface {
       
    81 	GetHeader() *Header
       
    82 }
       
    83 
    80 // One of the three core XMPP stanza types: iq, message, presence. See
    84 // One of the three core XMPP stanza types: iq, message, presence. See
    81 // RFC3920, section 9.
    85 // RFC3920, section 9.
    82 type Stanza struct {
    86 type Header struct {
    83 	To       string   `xml:"to,attr,omitempty"`
    87 	To       string   `xml:"to,attr,omitempty"`
    84 	From     string   `xml:"from,attr,omitempty"`
    88 	From     string   `xml:"from,attr,omitempty"`
    85 	Id       string   `xml:"id,attr,omitempty"`
    89 	Id       string   `xml:"id,attr,omitempty"`
    86 	Type     string   `xml:"type,attr,omitempty"`
    90 	Type     string   `xml:"type,attr,omitempty"`
    87 	Lang     string   `xml:"http://www.w3.org/XML/1998/namespace lang,attr,omitempty"`
    91 	Lang     string   `xml:"http://www.w3.org/XML/1998/namespace lang,attr,omitempty"`
    91 }
    95 }
    92 
    96 
    93 // message stanza
    97 // message stanza
    94 type Message struct {
    98 type Message struct {
    95 	XMLName  xml.Name `xml:"message"`
    99 	XMLName  xml.Name `xml:"message"`
    96 	Stanza
   100 	Header
    97 	Subject  *Generic `xml:"subject"`
   101 	Subject  *Generic `xml:"subject"`
    98 	Body     *Generic `xml:"body"`
   102 	Body     *Generic `xml:"body"`
    99 	Thread   *Generic `xml:"thread"`
   103 	Thread   *Generic `xml:"thread"`
   100 }
   104 }
       
   105 var _ Stanzer = &Message{}
   101 
   106 
   102 // presence stanza
   107 // presence stanza
   103 type Presence struct {
   108 type Presence struct {
   104 	XMLName  xml.Name `xml:"presence"`
   109 	XMLName  xml.Name `xml:"presence"`
   105 	Stanza
   110 	Header
   106 	Show     *Generic `xml:"show"`
   111 	Show     *Generic `xml:"show"`
   107 	Status   *Generic `xml:"status"`
   112 	Status   *Generic `xml:"status"`
   108 	Priority *Generic `xml:"priority"`
   113 	Priority *Generic `xml:"priority"`
   109 }
   114 }
       
   115 var _ Stanzer = &Presence{}
   110 
   116 
   111 // iq stanza
   117 // iq stanza
   112 type Iq struct {
   118 type Iq struct {
   113 	XMLName  xml.Name `xml:"iq"`
   119 	XMLName  xml.Name `xml:"iq"`
   114 	Stanza
   120 	Header
   115 }
   121 }
       
   122 var _ Stanzer = &Iq{}
   116 
   123 
   117 // Describes an XMPP stanza error. See RFC 3920, Section 9.3.
   124 // Describes an XMPP stanza error. See RFC 3920, Section 9.3.
   118 type Error struct {
   125 type Error struct {
   119 	XMLName xml.Name `xml:"error"`
   126 	XMLName xml.Name `xml:"error"`
   120 	// The error type attribute.
   127 	// The error type attribute.
   220 		}
   227 		}
   221 	}
   228 	}
   222 	return s, nil
   229 	return s, nil
   223 }
   230 }
   224 
   231 
       
   232 func (iq *Iq) GetHeader() *Header {
       
   233 	return &iq.Header
       
   234 }
       
   235 
       
   236 func (m *Message) GetHeader() *Header {
       
   237 	return &m.Header
       
   238 }
       
   239 
       
   240 func (p *Presence) GetHeader() *Header {
       
   241 	return &p.Header
       
   242 }
       
   243 
   225 func (u *Generic) String() string {
   244 func (u *Generic) String() string {
   226 	if u == nil {
   245 	if u == nil {
   227 		return "nil"
   246 		return "nil"
   228 	}
   247 	}
   229 	var sub string
   248 	var sub string
   248 	Start: func(cl *Client) {}}
   267 	Start: func(cl *Client) {}}
   249 
   268 
   250 func newBind(name *xml.Name) interface{} {
   269 func newBind(name *xml.Name) interface{} {
   251 	return &bindIq{}
   270 	return &bindIq{}
   252 }
   271 }
   253 
       
   254 func getStanza(v interface{}) *Stanza {
       
   255 	switch s := v.(type) {
       
   256 	case *Iq:
       
   257 		return &s.Stanza
       
   258 	case *Message:
       
   259 		return &s.Stanza
       
   260 	case *Presence:
       
   261 		return &s.Stanza
       
   262 	}
       
   263 	return nil
       
   264 }