-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
112 lines (92 loc) · 2.69 KB
/
message.go
File metadata and controls
112 lines (92 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package xmpp
import (
"encoding/xml"
"fmt"
)
// A Message represents a message stanza
type Message struct {
XMLName xml.Name `xml:"message"`
ID string `xml:"id,attr,omitempty"`
To JID `xml:"to,attr,omitempty"`
From JID `xml:"from,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
Lang string `xml:"lang,attr,omitempty"`
Subject string `xml:"subject,omitempty"`
Body string `xml:"body,omitempty"`
Thread string `xml:"thread,omitempty"`
Container
}
// Reply builds a reply struct to the message
func (m *Message) Reply(format string, a ...interface{}) *Message {
return &Message{
To: m.From,
Type: m.Type,
Body: fmt.Sprintf(format, a...),
}
}
// Normal returns true if the message is of type normal
func (m *Message) Normal() bool {
return (m.Type == "") || (m.Type == "normal")
}
// Chat returns true if the message is of type chat
func (m *Message) Chat() bool {
return m.Type == "chat"
}
// Chat returns true if the message is of type error
func (m *Message) Error() bool {
return m.Type == "error"
}
// Chat returns true if the message is of type headline
func (m *Message) Headline() bool {
return m.Type == "headline"
}
// GetID returns the id field
func (m *Message) GetID() string { return m.ID }
// GetFrom returns the from field
func (m *Message) GetFrom() JID { return m.From }
// GetTo returns the to field
func (m *Message) GetTo() JID { return m.To }
// GetType returns the type field
func (m *Message) GetType() string { return m.Type }
// GetLang returns the lang field
func (m *Message) GetLang() string { return m.Lang }
// SetID sets the id field
func (m *Message) SetID(s string) { m.ID = s }
// SetFrom sets the from field
func (m *Message) SetFrom(s JID) { m.From = s }
// SetTo sets the to field
func (m *Message) SetTo(s JID) { m.To = s }
// SetType sets the type field
func (m *Message) SetType(s string) { m.Type = s }
// SetLang sets the lang field
func (m *Message) SetLang(s string) { m.Lang = s }
func (m *Message) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
type Raw Message
type comboType struct {
Raw
Proxies []proxy `xml:",any"`
}
combo := &comboType{}
if err := dec.DecodeElement(combo, &start); err != nil {
panic(err)
}
*m = Message(combo.Raw)
m.XMLName = start.Name
m.Children = proxyToInterface(combo.Proxies)
return nil
}
func (m *Message) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
type Raw Message
type combo struct {
Raw
Children []interface{} `xml:",any"`
}
raw := &combo{}
raw.Raw = Raw(*m)
start.Name = xml.Name{Local: "message"}
raw.Children = m.Container.Children
return enc.EncodeElement(raw, start)
}
func initMessage() {
AddElement(&Message{})
}