/
mux.go
170 lines (150 loc) · 4.05 KB
/
mux.go
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
// Copyright 2017 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 implements an XMPP multiplexer.
//
// Be advised: This package is experimental and likely to change or be removed
// entirely.
package mux
import (
"encoding/xml"
"mellium.im/xmlstream"
"mellium.im/xmpp"
"mellium.im/xmpp/internal/ns"
"mellium.im/xmpp/stanza"
)
// ServeMux is an XMPP stream multiplexer.
// It matches the start element token of each top level stream element against a
// list of registered patterns and calls the handler for the pattern that most
// closely matches the token.
//
// Patterns are XML names.
// If either the namespace or the localname is left off, any namespace or
// localname will be matched.
// Full XML names take precedence, followed by wildcard namespaces, followed by
// wildcard localnames.
type ServeMux struct {
fallback xmpp.Handler
patterns map[xml.Name]xmpp.Handler
}
func fallback(t xmlstream.TokenReadWriter, start *xml.StartElement) error {
if start.Name.Local != "iq" {
return nil
}
typeIdx := -1
toIdx := -1
fromIdx := -1
for i, a := range start.Attr {
switch a.Name.Local {
case "type":
if a.Value == "error" {
return nil
}
typeIdx = i
case "from":
fromIdx = i
case "to":
toIdx = i
}
if typeIdx > -1 && fromIdx > -1 && toIdx > -1 {
break
}
}
switch {
case toIdx < 0 && fromIdx < 0:
// nothing to do here
case toIdx < 0:
start.Attr[fromIdx].Name.Local = "to"
case fromIdx < 0:
start.Attr[toIdx].Name.Local = "from"
default:
// swap values
start.Attr[toIdx].Value, start.Attr[fromIdx].Value = start.Attr[fromIdx].Value, start.Attr[toIdx].Value
}
// TODO: double check with RFC 6120 that if there is no type the default
// is "get" (and thus an error response should be generated).
if typeIdx >= 0 {
start.Attr[typeIdx].Value = "error"
} else {
start.Attr = append(start.Attr, xml.Attr{
Name: xml.Name{Local: "type"},
Value: "error",
})
}
e := stanza.Error{
Type: stanza.Cancel,
Condition: stanza.FeatureNotImplemented,
}
_, err := xmlstream.Copy(t, xmlstream.Wrap(e.TokenReader(), *start))
return err
}
// New allocates and returns a new ServeMux.
func New(opt ...Option) *ServeMux {
m := &ServeMux{
fallback: xmpp.HandlerFunc(fallback),
patterns: make(map[xml.Name]xmpp.Handler),
}
for _, o := range opt {
o(m)
}
return m
}
// Handler returns the handler to use for a top level element with the provided
// XML name.
// If no handler exists, a default handler is returned (h is always non-nil).
func (m *ServeMux) Handler(name xml.Name) (h xmpp.Handler, ok bool) {
h = m.patterns[name]
if h == nil {
return m.fallback, false
}
return h, true
}
// HandleXMPP dispatches the request to the handler whose pattern most closely
// matches start.Name.
func (m *ServeMux) HandleXMPP(t xmlstream.TokenReadWriter, start *xml.StartElement) error {
h, ok := m.Handler(start.Name)
if ok {
return h.HandleXMPP(t, start)
}
n := start.Name
n.Space = ""
h, ok = m.Handler(n)
if ok {
return h.HandleXMPP(t, start)
}
n = start.Name
n.Local = ""
h, _ = m.Handler(n)
return h.HandleXMPP(t, start)
}
// Option configures a ServeMux.
type Option func(m *ServeMux)
func registerStanza(local string, h xmpp.Handler) Option {
return func(m *ServeMux) {
if h == nil {
return
}
n := xml.Name{Local: local, Space: ns.Client}
m.patterns[n] = h
n = xml.Name{Local: local, Space: ns.Server}
m.patterns[n] = h
}
}
// IQ returns an option that matches on all IQ stanzas.
func IQ(h xmpp.Handler) Option {
return registerStanza("iq", h)
}
// Message returns an option that matches on all message stanzas.
func Message(h xmpp.Handler) Option {
return registerStanza("message", h)
}
// Presence returns an option that matches on all presence stanzas.
func Presence(h xmpp.Handler) Option {
return registerStanza("presence", h)
}
// Handle returns an option that matches on the provided XML name.
func Handle(n xml.Name, h xmpp.Handler) Option {
return func(m *ServeMux) {
m.patterns[n] = h
}
}