2019-06-18 14:28:30 +00:00
|
|
|
package xmpp
|
2016-01-06 15:51:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2019-06-22 16:36:16 +00:00
|
|
|
"reflect"
|
2016-01-06 15:51:12 +00:00
|
|
|
)
|
|
|
|
|
2018-01-13 17:50:17 +00:00
|
|
|
// ============================================================================
|
|
|
|
// Message Packet
|
|
|
|
|
2019-06-22 09:13:33 +00:00
|
|
|
// Message implements RFC 6120 - A.5 Client Namespace (a part)
|
2018-01-13 17:50:17 +00:00
|
|
|
type Message struct {
|
|
|
|
XMLName xml.Name `xml:"message"`
|
2019-06-22 09:13:33 +00:00
|
|
|
Attrs
|
|
|
|
|
2019-05-31 21:35:04 +00:00
|
|
|
Subject string `xml:"subject,omitempty"`
|
|
|
|
Body string `xml:"body,omitempty"`
|
|
|
|
Thread string `xml:"thread,omitempty"`
|
|
|
|
Error Err `xml:"error,omitempty"`
|
|
|
|
Extensions []MsgExtension `xml:",omitempty"`
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 17:50:17 +00:00
|
|
|
func (Message) Name() string {
|
|
|
|
return "message"
|
|
|
|
}
|
|
|
|
|
2019-06-22 09:13:33 +00:00
|
|
|
func NewMessage(a Attrs) Message {
|
2018-01-24 08:38:02 +00:00
|
|
|
return Message{
|
|
|
|
XMLName: xml.Name{Local: "message"},
|
2019-06-22 09:13:33 +00:00
|
|
|
Attrs: a,
|
2018-01-24 08:38:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 16:36:16 +00:00
|
|
|
// Get search and extracts a specific extension on a message.
|
|
|
|
// It receives a pointer to an MsgExtension. It will panic if the caller
|
|
|
|
// does not pass a pointer.
|
|
|
|
// It will return true if the passed extension is found and set the pointer
|
|
|
|
// to the extension passed as parameter to the found extension.
|
|
|
|
// It will return false if the extension is not found on the message.
|
|
|
|
//
|
|
|
|
// Example usage:
|
|
|
|
// var oob xmpp.OOB
|
|
|
|
// if ok := msg.Get(&oob); ok {
|
|
|
|
// // oob extension has been found
|
|
|
|
// }
|
|
|
|
func (msg *Message) Get(ext MsgExtension) bool {
|
|
|
|
target := reflect.ValueOf(ext)
|
|
|
|
if target.Kind() != reflect.Ptr {
|
|
|
|
panic("you must pass a pointer to the message Get method")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range msg.Extensions {
|
|
|
|
if reflect.TypeOf(e) == target.Type() {
|
|
|
|
source := reflect.ValueOf(e)
|
|
|
|
if source.Kind() != reflect.Ptr {
|
|
|
|
source = source.Elem()
|
|
|
|
}
|
|
|
|
target.Elem().Set(source.Elem())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-01-13 17:50:17 +00:00
|
|
|
type messageDecoder struct{}
|
|
|
|
|
|
|
|
var message messageDecoder
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2018-01-13 17:50:17 +00:00
|
|
|
func (messageDecoder) decode(p *xml.Decoder, se xml.StartElement) (Message, error) {
|
|
|
|
var packet Message
|
|
|
|
err := p.DecodeElement(&packet, &se)
|
|
|
|
return packet, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 17:10:21 +00:00
|
|
|
// XMPPFormat with all Extensions
|
2018-01-13 17:50:17 +00:00
|
|
|
func (msg *Message) XMPPFormat() string {
|
2019-06-04 17:10:21 +00:00
|
|
|
out, err := xml.MarshalIndent(msg, "", "")
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(out)
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
2019-05-31 21:35:04 +00:00
|
|
|
|
2019-06-23 10:15:47 +00:00
|
|
|
// UnmarshalXML implements custom parsing for messages
|
2019-05-31 21:35:04 +00:00
|
|
|
func (msg *Message) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
msg.XMLName = start.Name
|
|
|
|
|
|
|
|
// Extract packet attributes
|
|
|
|
for _, attr := range start.Attr {
|
|
|
|
if attr.Name.Local == "id" {
|
|
|
|
msg.Id = attr.Value
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "type" {
|
2019-06-22 09:13:33 +00:00
|
|
|
msg.Type = StanzaType(attr.Value)
|
2019-05-31 21:35:04 +00:00
|
|
|
}
|
|
|
|
if attr.Name.Local == "to" {
|
|
|
|
msg.To = attr.Value
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "from" {
|
|
|
|
msg.From = attr.Value
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "lang" {
|
|
|
|
msg.Lang = attr.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode inner elements
|
|
|
|
for {
|
|
|
|
t, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tt := t.(type) {
|
|
|
|
|
|
|
|
case xml.StartElement:
|
2019-06-05 08:23:18 +00:00
|
|
|
if msgExt := TypeRegistry.GetMsgExtension(tt.Name); msgExt != nil {
|
2019-06-03 10:40:42 +00:00
|
|
|
// Decode message extension
|
|
|
|
err = d.DecodeElement(msgExt, &tt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-31 21:35:04 +00:00
|
|
|
}
|
2019-06-03 10:40:42 +00:00
|
|
|
msg.Extensions = append(msg.Extensions, msgExt)
|
2019-05-31 21:35:04 +00:00
|
|
|
} else {
|
2019-06-03 10:40:42 +00:00
|
|
|
// Decode standard message sub-elements
|
2019-05-31 21:35:04 +00:00
|
|
|
var err error
|
|
|
|
switch tt.Name.Local {
|
|
|
|
case "body":
|
|
|
|
err = d.DecodeElement(&msg.Body, &tt)
|
|
|
|
case "thread":
|
|
|
|
err = d.DecodeElement(&msg.Thread, &tt)
|
|
|
|
case "subject":
|
|
|
|
err = d.DecodeElement(&msg.Subject, &tt)
|
|
|
|
case "error":
|
|
|
|
err = d.DecodeElement(&msg.Error, &tt)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case xml.EndElement:
|
|
|
|
if tt == start.End() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|