Skip to content

Commit

Permalink
remove router param from IQHandler
Browse files Browse the repository at this point in the history
ortuman committed Feb 4, 2019
1 parent e11d75b commit deeff0f
Showing 20 changed files with 238 additions and 227 deletions.
4 changes: 2 additions & 2 deletions c2s/in.go
Original file line number Diff line number Diff line change
@@ -389,7 +389,7 @@ func (s *inStream) handleConnected(elem xmpp.XElement) {
iq := elem.(*xmpp.IQ)
if reg := s.mods.Register; reg != nil && reg.MatchesIQ(iq) {
if s.IsSecured() {
reg.ProcessIQ(iq, s.router)
reg.ProcessIQ(iq)
} else {
// Channel isn't safe enough to enable a password change
s.writeElement(iq.NotAuthorizedError())
@@ -459,7 +459,7 @@ func (s *inStream) handleSessionStarted(elem xmpp.XElement) {
switch stanza := stanza.(type) {
case *xmpp.IQ:
if di := s.mods.DiscoInfo; di != nil && di.MatchesIQ(stanza) {
di.ProcessIQ(stanza, s.router)
di.ProcessIQ(stanza)
return
}
break
14 changes: 7 additions & 7 deletions module/module.go
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ type IQHandler interface {

// ProcessIQ processes a module IQ taking according actions
// over the associated stream.
ProcessIQ(iq *xmpp.IQ, router *router.Router)
ProcessIQ(iq *xmpp.IQ)
}

// Modules structure keeps reference to a set of preconfigured modules.
@@ -85,28 +85,28 @@ func New(config *Config, router *router.Router) *Modules {

// XEP-0049: Private XML Storage (https://xmpp.org/extensions/xep-0049.html)
if _, ok := config.Enabled["private"]; ok {
m.Private = xep0049.New()
m.Private = xep0049.New(router)
m.iqHandlers = append(m.iqHandlers, m.Private)
m.all = append(m.all, m.Private)
}

// XEP-0054: vcard-temp (https://xmpp.org/extensions/xep-0054.html)
if _, ok := config.Enabled["vcard"]; ok {
m.VCard = xep0054.New(m.DiscoInfo)
m.VCard = xep0054.New(m.DiscoInfo, router)
m.iqHandlers = append(m.iqHandlers, m.VCard)
m.all = append(m.all, m.VCard)
}

// XEP-0077: In-band registration (https://xmpp.org/extensions/xep-0077.html)
if _, ok := config.Enabled["registration"]; ok {
m.Register = xep0077.New(&config.Registration, m.DiscoInfo)
m.Register = xep0077.New(&config.Registration, m.DiscoInfo, router)
m.iqHandlers = append(m.iqHandlers, m.Register)
m.all = append(m.all, m.Register)
}

// XEP-0092: Software Version (https://xmpp.org/extensions/xep-0092.html)
if _, ok := config.Enabled["version"]; ok {
m.Version = xep0092.New(&config.Version, m.DiscoInfo)
m.Version = xep0092.New(&config.Version, m.DiscoInfo, router)
m.iqHandlers = append(m.iqHandlers, m.Version)
m.all = append(m.all, m.Version)
}
@@ -126,7 +126,7 @@ func New(config *Config, router *router.Router) *Modules {

// XEP-0199: XMPP Ping (https://xmpp.org/extensions/xep-0199.html)
if _, ok := config.Enabled["ping"]; ok {
m.Ping = xep0199.New(&config.Ping, m.DiscoInfo)
m.Ping = xep0199.New(&config.Ping, m.DiscoInfo, router)
m.iqHandlers = append(m.iqHandlers, m.Ping)
m.all = append(m.all, m.Ping)
}
@@ -140,7 +140,7 @@ func (m *Modules) ProcessIQ(iq *xmpp.IQ) {
if !handler.MatchesIQ(iq) {
continue
}
handler.ProcessIQ(iq, m.router)
handler.ProcessIQ(iq)
return
}

4 changes: 2 additions & 2 deletions module/roster/roster.go
Original file line number Diff line number Diff line change
@@ -60,9 +60,9 @@ func (x *Roster) MatchesIQ(iq *xmpp.IQ) bool {

// ProcessIQ processes a roster IQ taking according actions
// over the associated stream.
func (x *Roster) ProcessIQ(iq *xmpp.IQ, r *router.Router) {
func (x *Roster) ProcessIQ(iq *xmpp.IQ) {
x.actorCh <- func() {
stm := r.UserStream(iq.FromJID())
stm := x.router.UserStream(iq.FromJID())
if stm == nil {
return
}
20 changes: 10 additions & 10 deletions module/roster/roster_test.go
Original file line number Diff line number Diff line change
@@ -54,17 +54,17 @@ func TestRoster_FetchRoster(t *testing.T) {
q.AppendElement(xmpp.NewElementName("q2"))
iq.AppendElement(q)

r.ProcessIQ(iq, rtr)
r.ProcessIQ(iq)
elem := stm.ReceiveElement()
require.Equal(t, xmpp.ErrBadRequest.Error(), elem.Error().Elements().All()[0].Name())

iq.SetType(xmpp.GetType)
r.ProcessIQ(iq, rtr)
r.ProcessIQ(iq)
elem = stm.ReceiveElement()
require.Equal(t, xmpp.ErrBadRequest.Error(), elem.Error().Elements().All()[0].Name())
q.ClearElements()

r.ProcessIQ(iq, rtr)
r.ProcessIQ(iq)
elem = stm.ReceiveElement()
require.Equal(t, "iq", elem.Name())
require.Equal(t, xmpp.ResultType, elem.Type())
@@ -95,7 +95,7 @@ func TestRoster_FetchRoster(t *testing.T) {
r = New(&Config{Versioning: true}, rtr)
defer r.Shutdown()

r.ProcessIQ(iq, rtr)
r.ProcessIQ(iq)
elem = stm.ReceiveElement()
require.Equal(t, "iq", elem.Name())
require.Equal(t, xmpp.ResultType, elem.Type())
@@ -112,7 +112,7 @@ func TestRoster_FetchRoster(t *testing.T) {
q.SetAttribute("ver", "v1")
iq.AppendElement(q)

r.ProcessIQ(iq, rtr)
r.ProcessIQ(iq)
elem = stm.ReceiveElement()
require.Equal(t, "iq", elem.Name())
require.Equal(t, xmpp.ResultType, elem.Type())
@@ -130,7 +130,7 @@ func TestRoster_FetchRoster(t *testing.T) {
r = New(&Config{}, rtr)
defer r.Shutdown()

r.ProcessIQ(iq, rtr)
r.ProcessIQ(iq)
elem = stm.ReceiveElement()
require.Equal(t, xmpp.ErrInternalServerError.Error(), elem.Error().Elements().All()[0].Name())
s.DisableMockedError()
@@ -168,14 +168,14 @@ func TestRoster_Update(t *testing.T) {
q.AppendElement(item)
iq.AppendElement(q)

r.ProcessIQ(iq, rtr)
r.ProcessIQ(iq)
elem := stm1.ReceiveElement()
require.Equal(t, xmpp.ErrBadRequest.Error(), elem.Error().Elements().All()[0].Name())

q.ClearElements()
q.AppendElement(item)

r.ProcessIQ(iq, rtr)
r.ProcessIQ(iq)
elem = stm1.ReceiveElement()
require.Equal(t, "iq", elem.Name())
require.Equal(t, xmpp.ResultType, elem.Type())
@@ -190,7 +190,7 @@ func TestRoster_Update(t *testing.T) {
q.ClearElements()
q.AppendElement(item)

r.ProcessIQ(iq, rtr)
r.ProcessIQ(iq)
elem = stm1.ReceiveElement()
require.Equal(t, "iq", elem.Name())
require.Equal(t, xmpp.ResultType, elem.Type())
@@ -242,7 +242,7 @@ func TestRoster_RemoveItem(t *testing.T) {
q.AppendElement(item)
iq.AppendElement(q)

r.ProcessIQ(iq, rtr)
r.ProcessIQ(iq)
elem := stm.ReceiveElement()
require.Equal(t, iqID, elem.ID())

34 changes: 17 additions & 17 deletions module/xep0012/last_activity.go
Original file line number Diff line number Diff line change
@@ -52,9 +52,9 @@ func (x *LastActivity) MatchesIQ(iq *xmpp.IQ) bool {
}

// ProcessIQ processes a last activity IQ taking according actions over the associated stream.
func (x *LastActivity) ProcessIQ(iq *xmpp.IQ, r *router.Router) {
func (x *LastActivity) ProcessIQ(iq *xmpp.IQ) {
x.actorCh <- func() {
x.processIQ(iq, r)
x.processIQ(iq)
}
}

@@ -78,46 +78,46 @@ func (x *LastActivity) loop() {
}
}

func (x *LastActivity) processIQ(iq *xmpp.IQ, r *router.Router) {
func (x *LastActivity) processIQ(iq *xmpp.IQ) {
fromJID := iq.FromJID()
toJID := iq.ToJID()
if toJID.IsServer() {
x.sendServerUptime(iq, r)
x.sendServerUptime(iq)
} else if toJID.IsBare() {
ok, err := x.isSubscribedTo(toJID, fromJID)
if err != nil {
log.Error(err)
_ = r.Route(iq.InternalServerError())
_ = x.router.Route(iq.InternalServerError())
return
}
if ok {
x.sendUserLastActivity(iq, toJID, r)
x.sendUserLastActivity(iq, toJID)
} else {
_ = r.Route(iq.ForbiddenError())
_ = x.router.Route(iq.ForbiddenError())
}
} else {
_ = r.Route(iq.BadRequestError())
_ = x.router.Route(iq.BadRequestError())
}
}

func (x *LastActivity) sendServerUptime(iq *xmpp.IQ, r *router.Router) {
func (x *LastActivity) sendServerUptime(iq *xmpp.IQ) {
secs := int(time.Duration(time.Now().UnixNano()-x.startTime.UnixNano()) / time.Second)
x.sendReply(iq, secs, "", r)
x.sendReply(iq, secs, "")
}

func (x *LastActivity) sendUserLastActivity(iq *xmpp.IQ, to *jid.JID, r *router.Router) {
func (x *LastActivity) sendUserLastActivity(iq *xmpp.IQ, to *jid.JID) {
if len(x.router.UserStreams(to.Node())) > 0 { // user is online
x.sendReply(iq, 0, "", r)
x.sendReply(iq, 0, "")
return
}
usr, err := storage.FetchUser(to.Node())
if err != nil {
log.Error(err)
_ = r.Route(iq.InternalServerError())
_ = x.router.Route(iq.InternalServerError())
return
}
if usr == nil {
_ = r.Route(iq.ItemNotFoundError())
_ = x.router.Route(iq.ItemNotFoundError())
return
}
var secs int
@@ -128,16 +128,16 @@ func (x *LastActivity) sendUserLastActivity(iq *xmpp.IQ, to *jid.JID, r *router.
status = st.Text()
}
}
x.sendReply(iq, secs, status, r)
x.sendReply(iq, secs, status)
}

func (x *LastActivity) sendReply(iq *xmpp.IQ, secs int, status string, r *router.Router) {
func (x *LastActivity) sendReply(iq *xmpp.IQ, secs int, status string) {
q := xmpp.NewElementNamespace("query", lastActivityNamespace)
q.SetText(status)
q.SetAttribute("seconds", strconv.Itoa(secs))
res := iq.ResultIQ()
res.AppendElement(q)
_ = r.Route(res)
_ = x.router.Route(res)
}

func (x *LastActivity) isSubscribedTo(contact *jid.JID, userJID *jid.JID) (bool, error) {
10 changes: 5 additions & 5 deletions module/xep0012/last_activity_test.go
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ func TestXEP0012_GetServerLastActivity(t *testing.T) {
iq.SetToJID(j1)
iq.AppendElement(xmpp.NewElementNamespace("query", lastActivityNamespace))

x.ProcessIQ(iq, r)
x.ProcessIQ(iq)
elem := stm.ReceiveElement()
q := elem.Elements().Child("query")
require.NotNil(t, q)
@@ -99,7 +99,7 @@ func TestXEP0012_GetOnlineUserLastActivity(t *testing.T) {
iq.SetToJID(j2.ToBareJID())
iq.AppendElement(xmpp.NewElementNamespace("query", lastActivityNamespace))

x.ProcessIQ(iq, r)
x.ProcessIQ(iq)
elem := stm1.ReceiveElement()
require.Equal(t, xmpp.ErrForbidden.Error(), elem.Error().Elements().All()[0].Name())

@@ -117,7 +117,7 @@ func TestXEP0012_GetOnlineUserLastActivity(t *testing.T) {
JID: "noelia@jackal.im",
Subscription: "both",
})
x.ProcessIQ(iq, r)
x.ProcessIQ(iq)
elem = stm1.ReceiveElement()
q := elem.Elements().ChildNamespace("query", lastActivityNamespace)
secs := q.Attributes().Get("seconds")
@@ -126,14 +126,14 @@ func TestXEP0012_GetOnlineUserLastActivity(t *testing.T) {
// set as online
r.Bind(stm2)

x.ProcessIQ(iq, r)
x.ProcessIQ(iq)
elem = stm1.ReceiveElement()
q = elem.Elements().ChildNamespace("query", lastActivityNamespace)
secs = q.Attributes().Get("seconds")
require.Equal(t, "0", secs)

s.EnableMockedError()
x.ProcessIQ(iq, r)
x.ProcessIQ(iq)
elem = stm1.ReceiveElement()
require.Equal(t, xmpp.ErrInternalServerError.Error(), elem.Error().Elements().All()[0].Name())
s.DisableMockedError()
Loading
Oops, something went wrong.

0 comments on commit deeff0f

Please sign in to comment.