2019-06-18 14:28:30 +00:00
|
|
|
package xmpp
|
2019-06-13 15:22:39 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-28 20:48:01 +00:00
|
|
|
"context"
|
2019-06-24 09:31:08 +00:00
|
|
|
"encoding/xml"
|
2019-06-13 15:22:39 +00:00
|
|
|
"strings"
|
2019-10-29 09:34:23 +00:00
|
|
|
"sync"
|
2019-06-26 15:14:52 +00:00
|
|
|
|
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-06-13 15:22:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2019-06-14 07:37:38 +00:00
|
|
|
The XMPP router helps client and component developers select which XMPP they would like to process,
|
2019-06-13 15:22:39 +00:00
|
|
|
and associate processing code depending on the router configuration.
|
|
|
|
|
2019-06-17 09:59:39 +00:00
|
|
|
Here are important rules to keep in mind while setting your routes and matchers:
|
|
|
|
- Routes are evaluated in the order they are set.
|
|
|
|
- When a route matches, it is executed and all others routes are ignored. For each packet, only a single
|
|
|
|
route is executed.
|
2019-06-21 14:48:13 +00:00
|
|
|
- An empty route will match everything. Adding an empty route as the last route in your router will
|
|
|
|
allow you to get all stanzas that did not match any previous route. You can for example use this to
|
|
|
|
log all unexpected stanza received by your client or component.
|
2019-06-17 09:59:39 +00:00
|
|
|
|
2019-06-13 15:22:39 +00:00
|
|
|
TODO: Automatically reply to IQ that do not match any route, to comply to XMPP standard.
|
|
|
|
*/
|
|
|
|
|
|
|
|
type Router struct {
|
|
|
|
// Routes to be matched, in order.
|
|
|
|
routes []*Route
|
2019-10-28 20:48:01 +00:00
|
|
|
|
2019-10-29 10:02:41 +00:00
|
|
|
IQResultRoutes map[string]*IQResultRoute
|
|
|
|
IQResultRouteLock sync.RWMutex
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRouter returns a new router instance.
|
|
|
|
func NewRouter() *Router {
|
2019-10-28 20:48:01 +00:00
|
|
|
return &Router{
|
2019-10-29 10:02:41 +00:00
|
|
|
IQResultRoutes: make(map[string]*IQResultRoute),
|
2019-10-28 20:48:01 +00:00
|
|
|
}
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 09:13:25 +00:00
|
|
|
// route is called by the XMPP client to dispatch stanza received using the set up routes.
|
|
|
|
// It is also used by test, but is not supposed to be used directly by users of the library.
|
2019-06-26 15:14:52 +00:00
|
|
|
func (r *Router) route(s Sender, p stanza.Packet) {
|
2019-10-28 20:48:01 +00:00
|
|
|
iq, isIq := p.(stanza.IQ)
|
|
|
|
if isIq {
|
2019-10-29 10:02:41 +00:00
|
|
|
r.IQResultRouteLock.RLock()
|
|
|
|
route, ok := r.IQResultRoutes[iq.Id]
|
|
|
|
r.IQResultRouteLock.RUnlock()
|
2019-10-29 09:34:23 +00:00
|
|
|
if ok {
|
2019-10-29 10:02:41 +00:00
|
|
|
r.IQResultRouteLock.Lock()
|
|
|
|
delete(r.IQResultRoutes, iq.Id)
|
|
|
|
r.IQResultRouteLock.Unlock()
|
2019-10-29 09:34:23 +00:00
|
|
|
close(route.matched)
|
2019-10-29 10:02:41 +00:00
|
|
|
route.handler.HandleIQ(route.context, s, iq)
|
2019-10-29 09:34:23 +00:00
|
|
|
return
|
2019-10-28 20:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 09:31:08 +00:00
|
|
|
|
2019-06-13 15:22:39 +00:00
|
|
|
var match RouteMatch
|
|
|
|
if r.Match(p, &match) {
|
2019-06-24 09:31:08 +00:00
|
|
|
// If we match, route the packet
|
2019-06-14 07:37:38 +00:00
|
|
|
match.Handler.HandlePacket(s, p)
|
2019-06-24 09:31:08 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-28 20:48:01 +00:00
|
|
|
|
2019-06-24 09:31:08 +00:00
|
|
|
// If there is no match and we receive an iq set or get, we need to send a reply
|
2019-10-28 20:48:01 +00:00
|
|
|
if isIq && (iq.Type == stanza.IQTypeGet || iq.Type == stanza.IQTypeSet) {
|
|
|
|
iqNotImplemented(s, iq)
|
2019-06-24 09:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func iqNotImplemented(s Sender, iq stanza.IQ) {
|
|
|
|
err := stanza.Err{
|
2019-06-24 09:31:08 +00:00
|
|
|
XMLName: xml.Name{Local: "error"},
|
2019-06-24 10:16:19 +00:00
|
|
|
Code: 501,
|
2019-06-24 09:31:08 +00:00
|
|
|
Type: "cancel",
|
|
|
|
Reason: "feature-not-implemented",
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
2019-06-24 09:31:08 +00:00
|
|
|
reply := iq.MakeError(err)
|
|
|
|
_ = s.Send(reply)
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRoute registers an empty routes
|
|
|
|
func (r *Router) NewRoute() *Route {
|
|
|
|
route := &Route{}
|
|
|
|
r.routes = append(r.routes, route)
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:02:41 +00:00
|
|
|
// NewIQResultRoute register a route that will catch an IQ result stanza with
|
2019-10-28 20:48:01 +00:00
|
|
|
// the given Id. The route will only match ones, after which it will automatically
|
|
|
|
// be unregistered
|
2019-10-29 10:02:41 +00:00
|
|
|
func (r *Router) NewIQResultRoute(ctx context.Context, id string) *IQResultRoute {
|
|
|
|
route := &IQResultRoute{
|
2019-10-28 20:48:01 +00:00
|
|
|
context: ctx,
|
|
|
|
matched: make(chan struct{}),
|
|
|
|
}
|
2019-10-29 10:02:41 +00:00
|
|
|
r.IQResultRouteLock.Lock()
|
|
|
|
r.IQResultRoutes[id] = route
|
|
|
|
r.IQResultRouteLock.Unlock()
|
|
|
|
|
2019-10-28 20:48:01 +00:00
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-route.context.Done():
|
2019-10-29 10:02:41 +00:00
|
|
|
r.IQResultRouteLock.Lock()
|
|
|
|
delete(r.IQResultRoutes, id)
|
|
|
|
r.IQResultRouteLock.Unlock()
|
2019-10-28 20:48:01 +00:00
|
|
|
if route.timeoutHandler != nil {
|
|
|
|
route.timeoutHandler(route.context.Err())
|
|
|
|
}
|
|
|
|
case <-route.matched:
|
|
|
|
}
|
|
|
|
}()
|
2019-10-29 10:02:41 +00:00
|
|
|
|
2019-10-28 20:48:01 +00:00
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func (r *Router) Match(p stanza.Packet, match *RouteMatch) bool {
|
2019-06-13 15:22:39 +00:00
|
|
|
for _, route := range r.routes {
|
|
|
|
if route.Match(p, match) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle registers a new route with a matcher for a given packet name (iq, message, presence)
|
|
|
|
// See Route.Packet() and Route.Handler().
|
|
|
|
func (r *Router) Handle(name string, handler Handler) *Route {
|
|
|
|
return r.NewRoute().Packet(name).Handler(handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleFunc registers a new route with a matcher for for a given packet name (iq, message, presence)
|
|
|
|
// See Route.Path() and Route.HandlerFunc().
|
2019-06-26 15:14:52 +00:00
|
|
|
func (r *Router) HandleFunc(name string, f func(s Sender, p stanza.Packet)) *Route {
|
2019-06-13 15:22:39 +00:00
|
|
|
return r.NewRoute().Packet(name).HandlerFunc(f)
|
|
|
|
}
|
|
|
|
|
2019-10-28 20:48:01 +00:00
|
|
|
// ============================================================================
|
2019-10-29 10:02:41 +00:00
|
|
|
|
|
|
|
// TimeoutHandlerFunc is a function type for handling IQ result timeouts.
|
2019-10-28 20:48:01 +00:00
|
|
|
type TimeoutHandlerFunc func(err error)
|
|
|
|
|
2019-10-29 10:02:41 +00:00
|
|
|
// IQResultRoute is a temporary route to match IQ result stanzas
|
|
|
|
type IQResultRoute struct {
|
2019-10-28 20:48:01 +00:00
|
|
|
context context.Context
|
|
|
|
matched chan struct{}
|
2019-10-29 10:02:41 +00:00
|
|
|
handler IQResultHandler
|
2019-10-28 20:48:01 +00:00
|
|
|
timeoutHandler TimeoutHandlerFunc
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:02:41 +00:00
|
|
|
// Handler adds an IQ handler to the route.
|
|
|
|
func (r *IQResultRoute) Handler(handler IQResultHandler) *IQResultRoute {
|
2019-10-28 20:48:01 +00:00
|
|
|
r.handler = handler
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:02:41 +00:00
|
|
|
// HandlerFunc updates the route to call a handler function when an IQ result is received.
|
|
|
|
func (r *IQResultRoute) HandlerFunc(f IQResultHandlerFunc) *IQResultRoute {
|
2019-10-28 20:48:01 +00:00
|
|
|
return r.Handler(f)
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:02:41 +00:00
|
|
|
// TimeoutHandlerFunc registers a function that will be called automatically when
|
|
|
|
// the IQ result route is cancelled (most likely due to a timeout on the context).
|
|
|
|
func (r *IQResultRoute) TimeoutHandlerFunc(f TimeoutHandlerFunc) *IQResultRoute {
|
2019-10-28 20:48:01 +00:00
|
|
|
r.timeoutHandler = f
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:02:41 +00:00
|
|
|
// ============================================================================
|
|
|
|
// IQ result handler
|
|
|
|
|
|
|
|
// IQResultHandler is a utility interface for IQ result handlers
|
|
|
|
type IQResultHandler interface {
|
|
|
|
HandleIQ(ctx context.Context, s Sender, iq stanza.IQ)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IQResultHandlerFunc is an adapter to allow using functions as IQ result handlers.
|
|
|
|
type IQResultHandlerFunc func(ctx context.Context, s Sender, iq stanza.IQ)
|
|
|
|
|
|
|
|
// HandleIQ is a proxy function to implement IQResultHandler using a function.
|
|
|
|
func (f IQResultHandlerFunc) HandleIQ(ctx context.Context, s Sender, iq stanza.IQ) {
|
|
|
|
f(ctx, s, iq)
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:22:39 +00:00
|
|
|
// ============================================================================
|
|
|
|
// Route
|
2019-10-29 10:02:41 +00:00
|
|
|
|
2019-06-13 15:22:39 +00:00
|
|
|
type Handler interface {
|
2019-06-26 15:14:52 +00:00
|
|
|
HandlePacket(s Sender, p stanza.Packet)
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Route struct {
|
|
|
|
handler Handler
|
|
|
|
// Matchers are used to "specialize" routes and focus on specific packet features
|
2019-09-03 10:45:56 +00:00
|
|
|
matchers []Matcher
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) Handler(handler Handler) *Route {
|
|
|
|
r.handler = handler
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// The HandlerFunc type is an adapter to allow the use of
|
|
|
|
// ordinary functions as XMPP handlers. If f is a function
|
|
|
|
// with the appropriate signature, HandlerFunc(f) is a
|
|
|
|
// Handler that calls f.
|
2019-06-26 15:14:52 +00:00
|
|
|
type HandlerFunc func(s Sender, p stanza.Packet)
|
2019-06-13 15:22:39 +00:00
|
|
|
|
2019-06-14 07:37:38 +00:00
|
|
|
// HandlePacket calls f(s, p)
|
2019-06-26 15:14:52 +00:00
|
|
|
func (f HandlerFunc) HandlePacket(s Sender, p stanza.Packet) {
|
2019-06-14 07:37:38 +00:00
|
|
|
f(s, p)
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandlerFunc sets a handler function for the route
|
|
|
|
func (r *Route) HandlerFunc(f HandlerFunc) *Route {
|
|
|
|
return r.Handler(f)
|
|
|
|
}
|
|
|
|
|
2019-09-03 10:45:56 +00:00
|
|
|
// AddMatcher adds a matcher to the route
|
|
|
|
func (r *Route) AddMatcher(m Matcher) *Route {
|
2019-06-13 15:22:39 +00:00
|
|
|
r.matchers = append(r.matchers, m)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func (r *Route) Match(p stanza.Packet, match *RouteMatch) bool {
|
2019-06-13 15:22:39 +00:00
|
|
|
for _, m := range r.matchers {
|
|
|
|
if matched := m.Match(p, match); !matched {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a match, let's pass info route match info
|
|
|
|
match.Route = r
|
|
|
|
match.Handler = r.handler
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------
|
|
|
|
// Match on packet name
|
|
|
|
|
|
|
|
type nameMatcher string
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func (n nameMatcher) Match(p stanza.Packet, match *RouteMatch) bool {
|
2019-06-13 15:22:39 +00:00
|
|
|
var name string
|
|
|
|
// TODO: To avoid type switch everywhere in matching, I think we will need to have
|
|
|
|
// to move to a concrete type for packets, to make matching and comparison more natural.
|
|
|
|
// Current code structure is probably too rigid.
|
|
|
|
// Maybe packet types should even be from an enum.
|
|
|
|
switch p.(type) {
|
2019-06-26 15:14:52 +00:00
|
|
|
case stanza.Message:
|
2019-06-13 15:22:39 +00:00
|
|
|
name = "message"
|
2019-06-26 15:14:52 +00:00
|
|
|
case stanza.IQ:
|
2019-06-13 15:22:39 +00:00
|
|
|
name = "iq"
|
2019-06-26 15:14:52 +00:00
|
|
|
case stanza.Presence:
|
2019-06-13 15:22:39 +00:00
|
|
|
name = "presence"
|
|
|
|
}
|
|
|
|
if name == string(n) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Packet matches on a packet name (iq, message, presence, ...)
|
|
|
|
// It matches on the Local part of the xml.Name
|
|
|
|
func (r *Route) Packet(name string) *Route {
|
|
|
|
name = strings.ToLower(name)
|
2019-09-03 10:45:56 +00:00
|
|
|
return r.AddMatcher(nameMatcher(name))
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 14:48:13 +00:00
|
|
|
// -------------------------
|
|
|
|
// Match on stanza type
|
|
|
|
|
|
|
|
// nsTypeMather matches on a list of IQ payload namespaces
|
|
|
|
type nsTypeMatcher []string
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func (m nsTypeMatcher) Match(p stanza.Packet, match *RouteMatch) bool {
|
|
|
|
var stanzaType stanza.StanzaType
|
2019-06-21 14:48:13 +00:00
|
|
|
switch packet := p.(type) {
|
2019-06-26 15:14:52 +00:00
|
|
|
case stanza.IQ:
|
2019-06-21 14:48:13 +00:00
|
|
|
stanzaType = packet.Type
|
2019-06-26 15:14:52 +00:00
|
|
|
case stanza.Presence:
|
2019-06-21 14:48:13 +00:00
|
|
|
stanzaType = packet.Type
|
2019-06-26 15:14:52 +00:00
|
|
|
case stanza.Message:
|
2019-06-21 14:48:13 +00:00
|
|
|
if packet.Type == "" {
|
|
|
|
// optional on message, normal is the default type
|
|
|
|
stanzaType = "normal"
|
|
|
|
} else {
|
|
|
|
stanzaType = packet.Type
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2019-06-22 09:24:14 +00:00
|
|
|
return matchInArray(m, string(stanzaType))
|
2019-06-21 14:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IQNamespaces adds an IQ matcher, expecting both an IQ and a
|
|
|
|
func (r *Route) StanzaType(types ...string) *Route {
|
|
|
|
for k, v := range types {
|
|
|
|
types[k] = strings.ToLower(v)
|
|
|
|
}
|
2019-09-03 10:45:56 +00:00
|
|
|
return r.AddMatcher(nsTypeMatcher(types))
|
2019-06-21 14:48:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:22:39 +00:00
|
|
|
// -------------------------
|
|
|
|
// Match on IQ and namespace
|
|
|
|
|
|
|
|
// nsIqMather matches on a list of IQ payload namespaces
|
|
|
|
type nsIQMatcher []string
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
func (m nsIQMatcher) Match(p stanza.Packet, match *RouteMatch) bool {
|
|
|
|
iq, ok := p.(stanza.IQ)
|
2019-06-13 15:22:39 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2019-06-19 08:21:57 +00:00
|
|
|
if iq.Payload == nil {
|
2019-06-13 15:22:39 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-06-19 08:21:57 +00:00
|
|
|
return matchInArray(m, iq.Payload.Namespace())
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IQNamespaces adds an IQ matcher, expecting both an IQ and a
|
|
|
|
func (r *Route) IQNamespaces(namespaces ...string) *Route {
|
|
|
|
for k, v := range namespaces {
|
|
|
|
namespaces[k] = strings.ToLower(v)
|
|
|
|
}
|
2019-09-03 10:45:56 +00:00
|
|
|
return r.AddMatcher(nsIQMatcher(namespaces))
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// Matchers
|
|
|
|
|
2019-09-03 10:45:56 +00:00
|
|
|
// Matchers are used to "specialize" routes and focus on specific packet features.
|
|
|
|
// You can register attach them to a route via the AddMatcher method.
|
|
|
|
type Matcher interface {
|
2019-06-26 15:14:52 +00:00
|
|
|
Match(stanza.Packet, *RouteMatch) bool
|
2019-06-13 15:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RouteMatch extracts and gather match information
|
|
|
|
type RouteMatch struct {
|
|
|
|
Route *Route
|
|
|
|
Handler Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchInArray is a generic matching function to check if a string is a list
|
|
|
|
// of specific function
|
|
|
|
func matchInArray(arr []string, value string) bool {
|
|
|
|
for _, str := range arr {
|
|
|
|
if str == value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|