If there is no match in router, properly send not-implemented reply for iq set & get

This commit is contained in:
Mickael Remond 2019-06-24 11:31:08 +02:00 committed by Mickaël Rémond
parent d6d371df4d
commit 1542110f1b

View File

@ -1,6 +1,7 @@
package xmpp package xmpp
import ( import (
"encoding/xml"
"strings" "strings"
) )
@ -32,10 +33,31 @@ func NewRouter() *Router {
// route is called by the XMPP client to dispatch stanza received using the set up routes. // 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. // It is also used by test, but is not supposed to be used directly by users of the library.
func (r *Router) route(s Sender, p Packet) { func (r *Router) route(s Sender, p Packet) {
var match RouteMatch var match RouteMatch
if r.Match(p, &match) { if r.Match(p, &match) {
// If we match, route the packet
match.Handler.HandlePacket(s, p) match.Handler.HandlePacket(s, p)
return
} }
// If there is no match and we receive an iq set or get, we need to send a reply
if iq, ok := p.(IQ); ok {
if iq.Type == IQTypeGet || iq.Type == IQTypeSet {
iqNotImplemented(s, iq)
}
}
}
func iqNotImplemented(s Sender, iq IQ) {
err := Err{
XMLName: xml.Name{Local: "error"},
Code: 500,
Type: "cancel",
Reason: "feature-not-implemented",
}
reply := iq.MakeError(err)
_ = s.Send(reply)
} }
// NewRoute registers an empty routes // NewRoute registers an empty routes