~samwhited/xmpp

ref: a3e4231bda43a7c8b0b9bfbe9a55b7fc516b4724 xmpp/muc/muc.go -rw-r--r-- 7.0 KiB
a3e4231bSam Whited muc: add ability to set affiliation and handle changes 3 months 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// 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 implements Multi-User Chat.
package muc // import "mellium.im/xmpp/muc"

import (
	"context"
	"encoding/xml"
	"sync"

	"mellium.im/xmlstream"
	"mellium.im/xmpp"
	"mellium.im/xmpp/form"
	"mellium.im/xmpp/internal/attr"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/stanza"
)

// Various namespaces used by this package, provided as a convenience.
const (
	NS      = `http://jabber.org/protocol/muc`
	NSUser  = `http://jabber.org/protocol/muc#user`
	NSOwner = `http://jabber.org/protocol/muc#owner`
	NSAdmin = `http://jabber.org/protocol/muc#admin`

	// NSConf is the legacy conference namespace, now only used for direct MUC
	// invitations and backwards compatibility.
	NSConf = `jabber:x:conference`
)

// GetConfig requests a room config form.
func GetConfig(ctx context.Context, room jid.JID, s *xmpp.Session) (*form.Data, error) {
	return GetConfigIQ(ctx, stanza.IQ{
		To: room,
	}, s)
}

// GetConfigIQ is like GetConfig except that it lets you customize the IQ.
// Changing the type of the IQ has no effect.
func GetConfigIQ(ctx context.Context, iq stanza.IQ, s *xmpp.Session) (*form.Data, error) {
	if iq.Type != stanza.GetIQ {
		iq.Type = stanza.GetIQ
	}
	formResp := struct {
		XMLName  xml.Name  `xml:"http://jabber.org/protocol/muc#owner query"`
		DataForm form.Data `xml:"jabber:x:data x"`
	}{
		DataForm: form.Data{},
	}
	err := s.UnmarshalIQElement(ctx, xmlstream.Wrap(
		nil,
		xml.StartElement{Name: xml.Name{Space: NSOwner, Local: "query"}},
	), iq, &formResp)
	return &formResp.DataForm, err
}

// SetConfig sets the room config.
// The form should be the one provided by a call to GetConfig with various
// values set.
func SetConfig(ctx context.Context, room jid.JID, form *form.Data, s *xmpp.Session) error {
	return SetConfigIQ(ctx, stanza.IQ{
		To: room,
	}, form, s)
}

// SetConfigIQ is like SetConfig except that it lets you customize the IQ.
// Changing the type of the IQ has no effect.
func SetConfigIQ(ctx context.Context, iq stanza.IQ, form *form.Data, s *xmpp.Session) error {
	if iq.Type != stanza.SetIQ {
		iq.Type = stanza.SetIQ
	}
	submission, _ := form.Submit()
	r, err := s.SendIQElement(ctx, xmlstream.Wrap(
		submission,
		xml.StartElement{Name: xml.Name{Space: NSOwner, Local: "query"}},
	), iq)
	if err != nil {
		return err
	}
	return r.Close()
}

// HandleClient returns an option that registers the handler for use with a
// multiplexer.
func HandleClient(h *Client) mux.Option {
	return func(m *mux.ServeMux) {
		userPresence := xml.Name{Space: NSUser, Local: "x"}

		mux.Presence(stanza.AvailablePresence, userPresence, h)(m)
		mux.Presence(stanza.UnavailablePresence, userPresence, h)(m)
		mux.Message(stanza.NormalMessage, userPresence, h)(m)
	}
}

// Client is an xmpp.Handler that handles MUC payloads from a client
// perspective.
type Client struct {
	managed  map[string]*Channel
	managedM sync.Mutex

	// HandleInvite will be called if we receive a mediated MUC invitation.
	HandleInvite       func(Invitation)
	HandleUserPresence func(stanza.Presence, Item)
}

// HandleMessage satisfies mux.MessageHandler.
// it is used by the multiplexer and normally does not need to be called by the
// user.
func (c *Client) HandleMessage(p stanza.Message, r xmlstream.TokenReadEncoder) error {
	d := xml.NewTokenDecoder(r)
	msg := struct {
		stanza.Message
		X Invitation `xml:"http://jabber.org/protocol/muc#user x"`
	}{}
	err := d.Decode(&msg)
	if err != nil {
		return err
	}

	if msg.X.XMLName.Local != "" && c.HandleInvite != nil {
		c.HandleInvite(msg.X)
		return nil
	}
	return nil
}

type mucPresence struct {
	stanza.Presence
	X struct {
		XMLName xml.Name
		Item    Item `xml:"item"`
		Status  []struct {
			Code int `xml:"code,attr"`
		} `xml:"status,omitempty"`
	} `xml:"x"`
}

func (p *mucPresence) HasStatus(code int) bool {
	for _, status := range p.X.Status {
		if status.Code == code {
			return true
		}
	}
	return false
}

// HandlePresence satisfies mux.PresenceHandler.
// it is used by the multiplexer and normally does not need to be called by the
// user.
func (c *Client) HandlePresence(p stanza.Presence, r xmlstream.TokenReadEncoder) error {
	// If this is a self-presence, check if we're joining or departing and send on
	// the channel.
	c.managedM.Lock()
	defer c.managedM.Unlock()
	channel, ok := c.managed[p.From.String()]
	// TODO: what do we do with presences that aren't managed?
	if !ok {
		return nil
	}
	d := xml.NewTokenDecoder(r)
	var decodedPresence mucPresence
	err := d.Decode(&decodedPresence)
	if err != nil {
		return err
	}

	switch p.Type {
	case stanza.AvailablePresence:
		// TODO: make consts for the statuses when possible. Wait until we can
		// determine if they can be generated or have to be hand rolled first.
		// See: https://github.com/xsf/registrar/pull/38
		if decodedPresence.HasStatus(110) && channel.join != nil {
			channel.join <- p.From
			channel.join = nil
			return nil
		}
		if decodedPresence.X.XMLName.Space == NSUser && c.HandleUserPresence != nil {
			c.HandleUserPresence(decodedPresence.Presence, decodedPresence.X.Item)
		}
	case stanza.UnavailablePresence:
		channel.depart <- struct{}{}
		delete(c.managed, channel.addr.String())
	}
	return nil
}

// Join a MUC on the provided session.
// Room should be a full JID in which the desired nickname is the resourcepart.
//
// Join blocks until the full room roster has been received.
func (c *Client) Join(ctx context.Context, room jid.JID, s *xmpp.Session, opt ...Option) (*Channel, error) {
	return c.JoinPresence(ctx, stanza.Presence{
		To: room,
	}, s, opt...)
}

// JoinPresence is like Join except that it gives you more control over the
// presence.
// Changing the presence type has no effect.
func (c *Client) JoinPresence(ctx context.Context, p stanza.Presence, s *xmpp.Session, opt ...Option) (*Channel, error) {
	if p.Type != "" {
		p.Type = ""
	}
	if p.ID == "" {
		p.ID = attr.RandomID()
	}

	c.managedM.Lock()

	channel := &Channel{
		addr:    p.To,
		client:  c,
		session: s,

		join:   make(chan jid.JID, 1),
		depart: make(chan struct{}),
	}
	if c.managed == nil {
		c.managed = make(map[string]*Channel)
	}
	c.managed[p.To.String()] = channel
	c.managedM.Unlock()

	conf := config{}
	for _, o := range opt {
		o(&conf)
	}
	channel.pass = conf.password

	errChan := make(chan error)
	go func(errChan chan<- error) {
		resp, err := s.SendPresenceElement(ctx, conf.TokenReader(), p)
		//err := s.Send(ctx, p.Wrap(conf.TokenReader()))
		if err != nil {
			errChan <- err
			return
		}
		/* #nosec */
		defer resp.Close()
		// Pop the start presence token.
		_, err = resp.Token()
		if err != nil {
			errChan <- err
			return
		}

		stanzaError, err := stanza.UnmarshalError(resp)
		if err != nil {
			errChan <- err
			return
		}
		errChan <- stanzaError
	}(errChan)

	select {
	case err := <-errChan:
		return nil, err
	case roomAddr := <-channel.join:
		channel.addr = roomAddr
	case <-ctx.Done():
		return nil, ctx.Err()
	}

	return channel, nil
}