mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2024-11-24 19:42:00 -08:00
5eae7f4ef7
The URL will be more permanent as this is a place we dedicate as short URL for our go projects.
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package xmpp // import "gosrc.io/xmpp"
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
)
|
|
|
|
// ============================================================================
|
|
// Message Packet
|
|
|
|
type Message struct {
|
|
XMLName xml.Name `xml:"message"`
|
|
PacketAttrs
|
|
Subject string `xml:"subject,omitempty"`
|
|
Body string `xml:"body,omitempty"`
|
|
Thread string `xml:"thread,omitempty"`
|
|
Error Err `xml:"error,omitempty"`
|
|
}
|
|
|
|
func (Message) Name() string {
|
|
return "message"
|
|
}
|
|
|
|
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,
|
|
},
|
|
}
|
|
}
|
|
|
|
type messageDecoder struct{}
|
|
|
|
var message messageDecoder
|
|
|
|
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 {
|
|
return fmt.Sprintf("<message to='%s' type='chat' xml:lang='en'>"+
|
|
"<body>%s</body></message>",
|
|
msg.To,
|
|
xmlEscape(msg.Body))
|
|
}
|