structs.go
changeset 72 53f15893a1a7
parent 61 16513974d273
child 74 e619e18dcec3
child 88 d2ec96c80efe
equal deleted inserted replaced
71:578c2a83dc18 72:53f15893a1a7
    19 
    19 
    20 // JID represents an entity that can communicate with other
    20 // JID represents an entity that can communicate with other
    21 // entities. It looks like node@domain/resource. Node and resource are
    21 // entities. It looks like node@domain/resource. Node and resource are
    22 // sometimes optional.
    22 // sometimes optional.
    23 type JID struct {
    23 type JID struct {
    24 	Node string
    24 	Node     string
    25 	Domain string
    25 	Domain   string
    26 	Resource string
    26 	Resource string
    27 }
    27 }
       
    28 
    28 var _ fmt.Stringer = &JID{}
    29 var _ fmt.Stringer = &JID{}
    29 var _ flag.Value = &JID{}
    30 var _ flag.Value = &JID{}
    30 
    31 
    31 // XMPP's <stream:stream> XML element
    32 // XMPP's <stream:stream> XML element
    32 type stream struct {
    33 type stream struct {
    33 	To string `xml:"attr"`
    34 	To      string `xml:"attr"`
    34 	From string `xml:"attr"`
    35 	From    string `xml:"attr"`
    35 	Id string `xml:"attr"`
    36 	Id      string `xml:"attr"`
    36 	Lang string `xml:"attr"`
    37 	Lang    string `xml:"attr"`
    37 	Version string `xml:"attr"`
    38 	Version string `xml:"attr"`
    38 }
    39 }
       
    40 
    39 var _ xml.Marshaler = &stream{}
    41 var _ xml.Marshaler = &stream{}
    40 var _ fmt.Stringer = &stream{}
    42 var _ fmt.Stringer = &stream{}
    41 
    43 
    42 // <stream:error>
    44 // <stream:error>
    43 type streamError struct {
    45 type streamError struct {
    44 	Any Generic
    46 	Any  Generic
    45 	Text *errText
    47 	Text *errText
    46 }
    48 }
       
    49 
    47 var _ xml.Marshaler = &streamError{}
    50 var _ xml.Marshaler = &streamError{}
    48 
    51 
    49 type errText struct {
    52 type errText struct {
    50 	Lang string `xml:"attr"`
    53 	Lang string `xml:"attr"`
    51 	Text string `xml:"chardata"`
    54 	Text string `xml:"chardata"`
    52 }
    55 }
       
    56 
    53 var _ xml.Marshaler = &errText{}
    57 var _ xml.Marshaler = &errText{}
    54 
    58 
    55 type Features struct {
    59 type Features struct {
    56 	Starttls *starttls
    60 	Starttls   *starttls
    57 	Mechanisms mechs
    61 	Mechanisms mechs
    58 	Bind *bindIq
    62 	Bind       *bindIq
    59 	Session *Generic
    63 	Session    *Generic
    60 	Any *Generic
    64 	Any        *Generic
    61 }
    65 }
    62 
    66 
    63 type starttls struct {
    67 type starttls struct {
    64 	XMLName xml.Name
    68 	XMLName  xml.Name
    65 	Required *string
    69 	Required *string
    66 }
    70 }
    67 
    71 
    68 type mechs struct {
    72 type mechs struct {
    69 	Mechanism []string
    73 	Mechanism []string
    70 }
    74 }
    71 
    75 
    72 type auth struct {
    76 type auth struct {
    73 	XMLName xml.Name
    77 	XMLName   xml.Name
    74 	Chardata string `xml:"chardata"`
    78 	Chardata  string `xml:"chardata"`
    75 	Mechanism string `xml:"attr"`
    79 	Mechanism string `xml:"attr"`
    76 	Any *Generic
    80 	Any       *Generic
    77 }
    81 }
    78 
    82 
    79 // One of the three core XMPP stanza types: iq, message, presence. See
    83 // One of the three core XMPP stanza types: iq, message, presence. See
    80 // RFC3920, section 9.
    84 // RFC3920, section 9.
    81 type Stanza interface {
    85 type Stanza interface {
   100 	innerxml() string
   104 	innerxml() string
   101 }
   105 }
   102 
   106 
   103 // message stanza
   107 // message stanza
   104 type Message struct {
   108 type Message struct {
   105 	To string `xml:"attr"`
   109 	To       string `xml:"attr"`
   106 	From string `xml:"attr"`
   110 	From     string `xml:"attr"`
   107 	Id string `xml:"attr"`
   111 	Id       string `xml:"attr"`
   108 	Type string `xml:"attr"`
   112 	Type     string `xml:"attr"`
   109 	Lang string `xml:"attr"`
   113 	Lang     string `xml:"attr"`
   110 	Innerxml string `xml:"innerxml"`
   114 	Innerxml string `xml:"innerxml"`
   111 	Error *Error
   115 	Error    *Error
   112 	Subject *Generic
   116 	Subject  *Generic
   113 	Body *Generic
   117 	Body     *Generic
   114 	Thread *Generic
   118 	Thread   *Generic
   115 	Nested []interface{}
   119 	Nested   []interface{}
   116 }
   120 }
       
   121 
   117 var _ xml.Marshaler = &Message{}
   122 var _ xml.Marshaler = &Message{}
   118 var _ Stanza = &Message{}
   123 var _ Stanza = &Message{}
   119 
   124 
   120 // presence stanza
   125 // presence stanza
   121 type Presence struct {
   126 type Presence struct {
   122 	To string `xml:"attr"`
   127 	To       string `xml:"attr"`
   123 	From string `xml:"attr"`
   128 	From     string `xml:"attr"`
   124 	Id string `xml:"attr"`
   129 	Id       string `xml:"attr"`
   125 	Type string `xml:"attr"`
   130 	Type     string `xml:"attr"`
   126 	Lang string `xml:"attr"`
   131 	Lang     string `xml:"attr"`
   127 	Innerxml string `xml:"innerxml"`
   132 	Innerxml string `xml:"innerxml"`
   128 	Error *Error
   133 	Error    *Error
   129 	Show *Generic
   134 	Show     *Generic
   130 	Status *Generic
   135 	Status   *Generic
   131 	Priority *Generic
   136 	Priority *Generic
   132 	Nested []interface{}
   137 	Nested   []interface{}
   133 }
   138 }
       
   139 
   134 var _ xml.Marshaler = &Presence{}
   140 var _ xml.Marshaler = &Presence{}
   135 var _ Stanza = &Presence{}
   141 var _ Stanza = &Presence{}
   136 
   142 
   137 // iq stanza
   143 // iq stanza
   138 type Iq struct {
   144 type Iq struct {
   139 	To string `xml:"attr"`
   145 	To       string `xml:"attr"`
   140 	From string `xml:"attr"`
   146 	From     string `xml:"attr"`
   141 	Id string `xml:"attr"`
   147 	Id       string `xml:"attr"`
   142 	Type string `xml:"attr"`
   148 	Type     string `xml:"attr"`
   143 	Lang string `xml:"attr"`
   149 	Lang     string `xml:"attr"`
   144 	Innerxml string `xml:"innerxml"`
   150 	Innerxml string `xml:"innerxml"`
   145 	Error *Error
   151 	Error    *Error
   146 	Nested []interface{}
   152 	Nested   []interface{}
   147 }
   153 }
       
   154 
   148 var _ xml.Marshaler = &Iq{}
   155 var _ xml.Marshaler = &Iq{}
   149 var _ Stanza = &Iq{}
   156 var _ Stanza = &Iq{}
   150 
   157 
   151 // Describes an XMPP stanza error. See RFC 3920, Section 9.3.
   158 // Describes an XMPP stanza error. See RFC 3920, Section 9.3.
   152 type Error struct {
   159 type Error struct {
   154 	// The error type attribute.
   161 	// The error type attribute.
   155 	Type string `xml:"attr"`
   162 	Type string `xml:"attr"`
   156 	// Any nested element, if present.
   163 	// Any nested element, if present.
   157 	Any *Generic
   164 	Any *Generic
   158 }
   165 }
       
   166 
   159 var _ os.Error = &Error{}
   167 var _ os.Error = &Error{}
   160 
   168 
   161 // Used for resource binding as a nested element inside <iq/>.
   169 // Used for resource binding as a nested element inside <iq/>.
   162 type bindIq struct {
   170 type bindIq struct {
   163 	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
   171 	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
   164 	Resource *string `xml:"resource"`
   172 	Resource *string  `xml:"resource"`
   165 	Jid *string `xml:"jid"`
   173 	Jid      *string  `xml:"jid"`
   166 }
   174 }
   167 
   175 
   168 // Holds an XML element not described by the more specific types.
   176 // Holds an XML element not described by the more specific types.
   169 type Generic struct {
   177 type Generic struct {
   170 	XMLName xml.Name
   178 	XMLName  xml.Name
   171 	Any *Generic
   179 	Any      *Generic
   172 	Chardata string `xml:"chardata"`
   180 	Chardata string `xml:"chardata"`
   173 }
   181 }
       
   182 
   174 var _ fmt.Stringer = &Generic{}
   183 var _ fmt.Stringer = &Generic{}
   175 
   184 
   176 func (jid *JID) String() string {
   185 func (jid *JID) String() string {
   177 	result := jid.Domain
   186 	result := jid.Domain
   178 	if jid.Node != "" {
   187 	if jid.Node != "" {
   301 	if st.GetLang() != "" {
   310 	if st.GetLang() != "" {
   302 		writeField(buf, "xml:lang", st.GetLang())
   311 		writeField(buf, "xml:lang", st.GetLang())
   303 	}
   312 	}
   304 	buf.WriteString(">")
   313 	buf.WriteString(">")
   305 
   314 
   306 	if m, ok := st.(*Message) ; ok {
   315 	if m, ok := st.(*Message); ok {
   307 		err := xml.Marshal(buf, m.Subject)
   316 		err := xml.Marshal(buf, m.Subject)
   308 		if err != nil {
   317 		if err != nil {
   309 			return nil, err
   318 			return nil, err
   310 		}
   319 		}
   311 		err = xml.Marshal(buf, m.Body)
   320 		err = xml.Marshal(buf, m.Body)
   315 		err = xml.Marshal(buf, m.Thread)
   324 		err = xml.Marshal(buf, m.Thread)
   316 		if err != nil {
   325 		if err != nil {
   317 			return nil, err
   326 			return nil, err
   318 		}
   327 		}
   319 	}
   328 	}
   320 	if p, ok := st.(*Presence) ; ok {
   329 	if p, ok := st.(*Presence); ok {
   321 		err := xml.Marshal(buf, p.Show)
   330 		err := xml.Marshal(buf, p.Show)
   322 		if err != nil {
   331 		if err != nil {
   323 			return nil, err
   332 			return nil, err
   324 		}
   333 		}
   325 		err = xml.Marshal(buf, p.Status)
   334 		err = xml.Marshal(buf, p.Status)
   329 		err = xml.Marshal(buf, p.Priority)
   338 		err = xml.Marshal(buf, p.Priority)
   330 		if err != nil {
   339 		if err != nil {
   331 			return nil, err
   340 			return nil, err
   332 		}
   341 		}
   333 	}
   342 	}
   334 	if nested := st.GetNested() ; nested != nil {
   343 	if nested := st.GetNested(); nested != nil {
   335 		for _, n := range(nested) {
   344 		for _, n := range nested {
   336 			xml.Marshal(buf, n)
   345 			xml.Marshal(buf, n)
   337 		}
   346 		}
   338 	} else if st.innerxml() != "" {
   347 	} else if st.innerxml() != "" {
   339 		buf.WriteString(st.innerxml())
   348 		buf.WriteString(st.innerxml())
   340 	}
   349 	}
   367 	return m.Id
   376 	return m.Id
   368 }
   377 }
   369 
   378 
   370 func (m *Message) GetType() string {
   379 func (m *Message) GetType() string {
   371 	return m.Type
   380 	return m.Type
   372 	}
   381 }
   373 
   382 
   374 func (m *Message) GetLang() string {
   383 func (m *Message) GetLang() string {
   375 	return m.Lang
   384 	return m.Lang
   376 }
   385 }
   377 
   386 
   411 	return p.Id
   420 	return p.Id
   412 }
   421 }
   413 
   422 
   414 func (p *Presence) GetType() string {
   423 func (p *Presence) GetType() string {
   415 	return p.Type
   424 	return p.Type
   416 	}
   425 }
   417 
   426 
   418 func (p *Presence) GetLang() string {
   427 func (p *Presence) GetLang() string {
   419 	return p.Lang
   428 	return p.Lang
   420 }
   429 }
   421 
   430 
   455 	return iq.Id
   464 	return iq.Id
   456 }
   465 }
   457 
   466 
   458 func (iq *Iq) GetType() string {
   467 func (iq *Iq) GetType() string {
   459 	return iq.Type
   468 	return iq.Type
   460 	}
   469 }
   461 
   470 
   462 func (iq *Iq) GetLang() string {
   471 func (iq *Iq) GetLang() string {
   463 	return iq.Lang
   472 	return iq.Lang
   464 }
   473 }
   465 
   474 
   512 		return nil, err
   521 		return nil, err
   513 	}
   522 	}
   514 	return stan, nil
   523 	return stan, nil
   515 }
   524 }
   516 
   525 
   517 var bindExt Extension = Extension{StanzaHandlers:
   526 var bindExt Extension = Extension{StanzaHandlers: map[string]func(*xml.Name) interface{}{NsBind: newBind},
   518 	map[string] func(*xml.Name) interface{}{NsBind: newBind},
   527 	Start: func(cl *Client) {}}
   519 		Start: func(cl *Client) {}}
       
   520 
   528 
   521 func newBind(name *xml.Name) interface{} {
   529 func newBind(name *xml.Name) interface{} {
   522 	return &bindIq{}
   530 	return &bindIq{}
   523 }
   531 }