~samwhited/xmpp

ref: 30367126f2dd56ba6d5b5d60dd6bcd640cf7aede xmpp/config.go -rw-r--r-- 1.8 KiB
30367126Sam Whited Remove return that breaks formatting in gddo 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 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 xmpp

import (
	"crypto/tls"
	"encoding/xml"

	"golang.org/x/text/language"
	"mellium.im/xmpp/internal"
	"mellium.im/xmpp/jid"
)

// Config represents the configuration of an XMPP session.
type Config struct {
	// An XMPP server address.
	Location *jid.JID

	// An XMPP connection origin (local address). If the origin has a resource
	// part and this is a client config, the given resource will be requested (but
	// not necessarily assigned) during the initial connection handshake.
	// Generally it is recommended to leave this a bare JID and let the server
	// assign a resource part.
	Origin *jid.JID

	// The supported stream features.
	Features map[xml.Name]StreamFeature

	// The default language for any streams constructed using this config.
	Lang language.Tag

	// True if this is a server-to-server session.
	S2S bool

	// TLS config for STARTTLS.
	TLSConfig *tls.Config

	// XMPP protocol version
	Version internal.Version
}

// NewClientConfig constructs a new client-to-server session configuration with
// sane defaults.
func NewClientConfig(origin *jid.JID, features ...StreamFeature) (c *Config) {
	c = NewServerConfig(origin.Domain(), origin, features...)
	c.S2S = false
	return c
}

// NewServerConfig constructs a new server-to-server session configuration with
// sane defaults.
func NewServerConfig(location, origin *jid.JID, features ...StreamFeature) (c *Config) {
	c = &Config{
		Features: make(map[xml.Name]StreamFeature),
		Location: location,
		Origin:   origin,
		S2S:      true,
		Version:  internal.DefaultVersion,
	}
	for _, f := range features {
		c.Features[f.Name] = f
	}
	return c
}

func (config *Config) connType() string {
	if config.S2S {
		return "xmpp-server"
	}
	return "xmpp-client"
}