-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmessage.go
More file actions
90 lines (70 loc) · 2.18 KB
/
message.go
File metadata and controls
90 lines (70 loc) · 2.18 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
package xco
import (
"encoding/xml"
"github.com/pkg/errors"
)
// MessageType defines the constants for the types of messages within XEP-0114
type MessageType string
const (
// CHAT defines the chat message type
CHAT = MessageType("chat")
// ERROR defines the error message type
ERROR = MessageType("error")
// GROUPCHAT defines the group chat message type
GROUPCHAT = MessageType("groupchat")
// HEADLINE defines the headline message type
HEADLINE = MessageType("headline")
// NORMAL defines the normal message type
NORMAL = MessageType("normal")
)
// A Message is an incoming or outgoing Component message
type Message struct {
Header
Type MessageType `xml:"type,attr,omitempty"`
Subject string `xml:"subject,omitempty"`
Body string `xml:"body,omitempty"`
Error *Error `xml:"error"`
Thread string `xml:"thread,omitempty"`
Content string `xml:",innerxml"` // allow arbitrary content
// XEP-0184 message delivery receipts
ReceiptRequest *xml.Name `xml:"urn:xmpp:receipts request,omitempty"`
ReceiptAck *ReceiptAck `xml:"urn:xmpp:receipts received,omitempty"`
// XEP-0172 User nicknames
Nick string `xml:"http://jabber.org/protocol/nick nick,omitempty"`
XMLName xml.Name `xml:"message"`
}
// A MessageHandler handles an incoming message
type MessageHandler func(*Component, *Message) error
func noOpMessageHandler(c *Component, m *Message) error {
return nil
}
// BodyResponseHandler builds a simple request-response style function which returns the body
// of the response message
func BodyResponseHandler(fn func(*Message) (string, error)) MessageHandler {
return func(c *Component, m *Message) error {
body, err := fn(m)
if err != nil {
return err
}
resp := m.Response()
resp.Body = body
return errors.Wrap(c.Send(resp), "Error sending message response")
}
}
// Response returns a new message representing a response to this
// message. The To and From attributes of the header are reversed to
// indicate the new origin.
func (m *Message) Response() *Message {
resp := &Message{
Header: Header{
From: m.To,
To: m.From,
ID: m.ID,
},
Subject: m.Subject,
Thread: m.Thread,
Type: m.Type,
XMLName: m.XMLName,
}
return resp
}