~samwhited/xmpp

ref: 419d6d466133ee3a90f6deaa9e136dc39fad9c73 xmpp/stream/options.go -rw-r--r-- 1.2 KiB
419d6d46Sam Whited Remove old handler code 5 years 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
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package stream

import (
	"encoding/xml"
	"net"

	"golang.org/x/text/language"
)

// Option's can be used to configure the stream.
type Option func(*options)
type options struct {
	lang    language.Tag
	conn    net.Conn
	xmlns   string
	encoder *xml.Encoder
	decoder *xml.Decoder
}

func getOpts(o ...Option) (res options) {
	for _, f := range o {
		f(&res)
	}
	return
}

// The Language option specifies the default language for the stream. Clients
// that support multiple languages will assume that all messages, alerts,
// and other textual data in the stream is in the given language unless it is
// specifically overridden.
func Language(l language.Tag) Option {
	return func(o *options) {
		o.lang = l
	}
}

// Conn is the connection which the stream will use for sending and receiving
// data. To manually manage streams (not recommended), don't provide a
// connection and marshal and send the stream yourself.
func Conn(c net.Conn) Option {
	return func(o *options) {
		o.conn = c
		o.encoder = xml.NewEncoder(c)
		o.decoder = xml.NewDecoder(c)
	}
}