~samwhited/xmpp

xmpp/mux/option.go -rw-r--r-- 3.3 KiB
e9b0a2deSam Whited docs: do a quick editing pass over the docs a day 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
// Copyright 2020 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 mux

import (
	"encoding/xml"

	"mellium.im/xmpp"
	"mellium.im/xmpp/stanza"
)

// Option configures a ServeMux.
type Option func(m *ServeMux)

// IQ returns an option that matches IQ stanzas based on their type and the name
// of the payload.
func IQ(typ stanza.IQType, payload xml.Name, h IQHandler) Option {
	return func(m *ServeMux) {
		if h == nil {
			panic("mux: nil IQ handler")
		}
		pat := pattern{Stanza: iqStanza, Payload: payload, Type: string(typ)}
		if _, ok := m.iqPatterns[pat]; ok {
			panic("mux: multiple registrations for " + pat.String())
		}
		if m.iqPatterns == nil {
			m.iqPatterns = make(map[pattern]IQHandler)
		}
		m.iqPatterns[pat] = h
	}
}

// IQFunc returns an option that matches IQ stanzas.
// For more information see IQ.
func IQFunc(typ stanza.IQType, payload xml.Name, h IQHandlerFunc) Option {
	return IQ(typ, payload, h)
}

// Message returns an option that matches message stanzas by type.
func Message(typ stanza.MessageType, payload xml.Name, h MessageHandler) Option {
	return func(m *ServeMux) {
		if h == nil {
			panic("mux: nil message handler")
		}
		pat := pattern{Stanza: msgStanza, Payload: payload, Type: string(typ)}
		if _, ok := m.msgPatterns[pat]; ok {
			panic("mux: multiple registrations for " + pat.String())
		}
		if m.msgPatterns == nil {
			m.msgPatterns = make(map[pattern]MessageHandler)
		}
		m.msgPatterns[pat] = h
	}
}

// MessageFunc returns an option that matches message stanzas.
// For more information see Message.
func MessageFunc(typ stanza.MessageType, payload xml.Name, h MessageHandlerFunc) Option {
	return Message(typ, payload, h)
}

// Presence returns an option that matches presence stanzas by type.
func Presence(typ stanza.PresenceType, payload xml.Name, h PresenceHandler) Option {
	return func(m *ServeMux) {
		if h == nil {
			panic("mux: nil presence handler")
		}
		pat := pattern{Stanza: presStanza, Payload: payload, Type: string(typ)}
		if _, ok := m.presencePatterns[pat]; ok {
			panic("mux: multiple registrations for " + pat.String())
		}
		if m.presencePatterns == nil {
			m.presencePatterns = make(map[pattern]PresenceHandler)
		}
		m.presencePatterns[pat] = h
	}
}

// PresenceFunc returns an option that matches on presence stanzas.
// For more information see Presence.
func PresenceFunc(typ stanza.PresenceType, payload xml.Name, h PresenceHandlerFunc) Option {
	return Presence(typ, payload, h)
}

// Handle returns an option that matches on the provided XML name.
// If a handler already exists for n when the option is applied, the option
// panics.
func Handle(n xml.Name, h xmpp.Handler) Option {
	return func(m *ServeMux) {
		if h == nil {
			panic("mux: nil handler")
		}
		if stanza.Is(n, "") {
			panic("mux: tried to register stanza handler with Handle, use HandleIQ, HandleMessage, or HandlePresence instead")
		}
		if _, ok := m.patterns[n]; ok {
			panic("mux: multiple registrations for {" + n.Space + "}" + n.Local)
		}
		if m.patterns == nil {
			m.patterns = make(map[xml.Name]xmpp.Handler)
		}
		m.patterns[n] = h
	}
}

// HandleFunc returns an option that matches on the provided XML name.
func HandleFunc(n xml.Name, h xmpp.HandlerFunc) Option {
	return Handle(n, h)
}