~samwhited/xmpp

xmpp/muc/invites.go -rw-r--r-- 5.9 KiB
c9743d9fSam Whited docs: update changelog to include deprecations 4 days ago
                                                                                
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2021 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package muc

import (
	"context"
	"encoding/xml"

	"mellium.im/xmlstream"
	"mellium.im/xmpp"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/stanza"
)

var directName = xml.Name{Space: NSConf, Local: "x"}

type inviteHandler struct {
	F func(Invitation)
}

func (h inviteHandler) HandleMessage(msg stanza.Message, t xmlstream.TokenReadEncoder) error {
	d := xml.NewTokenDecoder(t)
	// Pop the <message> token.
	_, err := d.Token()
	if err != nil {
		return err
	}
	var x Invitation
	err = d.Decode(&x)
	if err != nil {
		return err
	}

	if h.F != nil {
		h.F(x)
		return nil
	}
	return nil
}

// HandleInvite returns an option that registers a handler for direct MUC
// invitations.
// To handle mediated invitations register a client handler using HandleClient.
func HandleInvite(f func(Invitation)) mux.Option {
	return func(m *mux.ServeMux) {
		msgName := xml.Name{Space: NSConf, Local: "x"}

		mux.Message(stanza.NormalMessage, msgName, inviteHandler{
			F: f,
		})(m)
	}
}

// Invitation is a mediated or direct MUC invitation.
// When the XML is marshaled or unmarshaled the namespace determines whether the
// invitation was direct or mediated.
// The default is mediated.
type Invitation struct {
	XMLName  xml.Name
	Continue bool
	JID      jid.JID
	Password string
	Reason   string
	Thread   string
}

// MarshalDirect returns the invitation as a direct MUC invitation (sent
// directly to the invitee).
func (i Invitation) MarshalDirect() xml.TokenReader {
	attr := []xml.Attr{{
		Name:  xml.Name{Local: "jid"},
		Value: i.JID.String(),
	}}
	if i.Continue {
		attr = append(attr, xml.Attr{
			Name:  xml.Name{Local: "continue"},
			Value: "true",
		})
		if i.Thread != "" {
			attr = append(attr, xml.Attr{
				Name:  xml.Name{Local: "thread"},
				Value: i.Thread,
			})
		}
	}
	if i.Password != "" {
		attr = append(attr, xml.Attr{
			Name:  xml.Name{Local: "password"},
			Value: i.Password,
		})
	}
	if i.Reason != "" {
		attr = append(attr, xml.Attr{
			Name:  xml.Name{Local: "reason"},
			Value: i.Reason,
		})
	}
	return xmlstream.Wrap(
		nil,
		xml.StartElement{Name: i.XMLName, Attr: attr},
	)
}

// MarshalMediated returns the invitation as a mediated MUC invitation (sent
// to the room and then forwarded to the invitee).
func (i Invitation) MarshalMediated() xml.TokenReader {
	var reasonEl, passEl, continueEl xml.TokenReader
	if i.Reason != "" {
		reasonEl = xmlstream.Wrap(
			xmlstream.Token(xml.CharData(i.Reason)),
			xml.StartElement{Name: xml.Name{Local: "reason"}},
		)
	}
	if i.Password != "" {
		passEl = xmlstream.Wrap(
			xmlstream.Token(xml.CharData(i.Password)),
			xml.StartElement{Name: xml.Name{Local: "password"}},
		)
	}
	if i.Continue {
		var attr []xml.Attr
		if i.Thread != "" {
			attr = []xml.Attr{{
				Name:  xml.Name{Local: "thread"},
				Value: i.Thread,
			}}
		}
		continueEl = xmlstream.Wrap(
			nil,
			xml.StartElement{Name: xml.Name{Local: "continue"}, Attr: attr},
		)
	}
	return xmlstream.Wrap(
		xmlstream.MultiReader(
			xmlstream.Wrap(
				xmlstream.MultiReader(
					reasonEl,
					continueEl,
				),
				xml.StartElement{
					Name: xml.Name{Local: "invite"},
					Attr: []xml.Attr{{Name: xml.Name{Local: "to"}, Value: i.JID.String()}},
				},
			),
			passEl,
		),
		xml.StartElement{Name: xml.Name{Space: NSUser, Local: "x"}},
	)
}

// TokenReader satisfies the xmlstream.Marshaler interface.
//
// It calls either MarshalDirect or MarshalMediated depending on the invitations
// XMLName field.
func (i Invitation) TokenReader() xml.TokenReader {
	// Direct invite
	if i.XMLName == directName {
		return i.MarshalDirect()
	}

	// Mediated invite
	return i.MarshalMediated()
}

// WriteXML satisfies the xmlstream.WriterTo interface.
// It is like MarshalXML except it writes tokens to w.
func (i Invitation) WriteXML(w xmlstream.TokenWriter) (n int, err error) {
	return xmlstream.Copy(w, i.TokenReader())
}

// MarshalXML implements xml.Marshaler.
func (i Invitation) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
	_, err := i.WriteXML(e)
	return err
}

// UnmarshalXML implements xml.Unmarshaler.
func (i *Invitation) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	if start.Name == directName {
		s := struct {
			XMLName  xml.Name `xml:"jabber:x:conference x"`
			Continue bool     `xml:"continue,attr"`
			JID      jid.JID  `xml:"jid,attr"`
			Pass     string   `xml:"password,attr"`
			Reason   string   `xml:"reason,attr"`
			Thread   string   `xml:"thread,attr"`
		}{}
		err := d.DecodeElement(&s, &start)
		if err != nil {
			return err
		}
		i.XMLName = s.XMLName
		i.Continue = s.Continue
		i.JID = s.JID
		i.Password = s.Pass
		i.Reason = s.Reason
		i.Thread = s.Thread
		return nil
	}

	s := struct {
		XMLName xml.Name `xml:"http://jabber.org/protocol/muc#user x"`
		Invite  struct {
			To       jid.JID `xml:"to,attr"`
			Reason   string  `xml:"reason"`
			Continue struct {
				XMLName xml.Name
				Thread  string `xml:"thread,attr"`
			} `xml:"continue"`
		} `xml:"invite"`
		Pass string `xml:"password"`
	}{}
	err := d.DecodeElement(&s, &start)
	if err != nil {
		return err
	}
	i.XMLName = s.XMLName
	i.Continue = s.Invite.Continue.XMLName.Local != ""
	i.JID = s.Invite.To
	i.Password = s.Pass
	i.Reason = s.Invite.Reason
	i.Thread = s.Invite.Continue.Thread
	return nil
}

// Invite sends a direct MUC invitation using the provided session.
// This is useful when a mediated invitation (one sent through the channel using
// the Invite method) is being blocked by a user that does not allow contact
// from unrecognized JIDs.
// Changing the XMLName field of the invite has no effect.
func Invite(ctx context.Context, to jid.JID, invite Invitation, s *xmpp.Session) error {
	invite.XMLName = directName
	return s.Send(ctx, stanza.Message{
		To:   to,
		Type: stanza.NormalMessage,
	}.Wrap(invite.MarshalDirect()))
}