2018-12-26 17:50:01 +00:00
|
|
|
package xmpp // import "gosrc.io/xmpp"
|
2016-01-06 15:51:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2018-01-13 17:50:17 +00:00
|
|
|
// ============================================================================
|
|
|
|
// Message Packet
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
XMLName xml.Name `xml:"message"`
|
2018-01-13 16:54:07 +00:00
|
|
|
PacketAttrs
|
2016-01-06 15:51:12 +00:00
|
|
|
Subject string `xml:"subject,omitempty"`
|
|
|
|
Body string `xml:"body,omitempty"`
|
|
|
|
Thread string `xml:"thread,omitempty"`
|
2018-01-23 08:10:10 +00:00
|
|
|
Error Err `xml:"error,omitempty"`
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 17:50:17 +00:00
|
|
|
func (Message) Name() string {
|
|
|
|
return "message"
|
|
|
|
}
|
|
|
|
|
2018-01-24 08:38:02 +00:00
|
|
|
func NewMessage(msgtype, from, to, id, lang string) Message {
|
|
|
|
return Message{
|
|
|
|
XMLName: xml.Name{Local: "message"},
|
|
|
|
PacketAttrs: PacketAttrs{
|
|
|
|
Id: id,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Type: msgtype,
|
|
|
|
Lang: lang,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Message) XMPPFormat() string {
|
2016-01-06 15:51:12 +00:00
|
|
|
return fmt.Sprintf("<message to='%s' type='chat' xml:lang='en'>"+
|
|
|
|
"<body>%s</body></message>",
|
2018-01-13 17:50:17 +00:00
|
|
|
msg.To,
|
|
|
|
xmlEscape(msg.Body))
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|