From 1542110f1b196a95484c406790ae07c8b3004542 Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Mon, 24 Jun 2019 11:31:08 +0200 Subject: [PATCH] If there is no match in router, properly send not-implemented reply for iq set & get --- router.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/router.go b/router.go index 82b4a22..d164cf8 100644 --- a/router.go +++ b/router.go @@ -1,6 +1,7 @@ package xmpp import ( + "encoding/xml" "strings" ) @@ -32,10 +33,31 @@ func NewRouter() *Router { // 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. func (r *Router) route(s Sender, p Packet) { + var match RouteMatch if r.Match(p, &match) { + // If we match, route the packet 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