Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
s2s: override stanza on received hooks (#261)
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Ángel Ortuño <[email protected]>
  • Loading branch information
ortuman authored Sep 29, 2022
1 parent 0b786ef commit 2a4dcb3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 43 deletions.
18 changes: 6 additions & 12 deletions pkg/c2s/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,8 @@ func (s *inC2S) processIQ(ctx context.Context, iq *stravaganza.IQ) error {
if err != nil {
return err
}
iq, ok := hi.Element.(*stravaganza.IQ)
if !ok {
return nil
}
iq = hi.Element.(*stravaganza.IQ)

targets, err := s.router.Route(ctx, iq)
switch err {
case router.ErrResourceNotFound:
Expand Down Expand Up @@ -649,10 +647,8 @@ func (s *inC2S) processPresence(ctx context.Context, presence *stravaganza.Prese
if err != nil {
return err
}
presence, ok := hi.Element.(*stravaganza.Presence)
if !ok {
return nil
}
presence = hi.Element.(*stravaganza.Presence)

targets, err := s.router.Route(ctx, presence)
switch err {
case nil, router.ErrUserNotAvailable:
Expand Down Expand Up @@ -704,10 +700,8 @@ sendMsg:
if err != nil {
return err
}
msg, ok := hi.Element.(*stravaganza.Message)
if !ok {
return nil
}
msg = hi.Element.(*stravaganza.Message)

targets, err := s.router.Route(ctx, msg)
switch err {
case router.ErrResourceNotFound:
Expand Down
63 changes: 32 additions & 31 deletions pkg/s2s/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,20 +324,20 @@ func (s *inS2S) handleConnected(ctx context.Context, elem stravaganza.Element) e
default:
if s.flags.isAuthenticated() || s.flags.isDialbackKeyAuthorized() {
// post element received event
hInf := &hook.S2SStreamInfo{
hi := &hook.S2SStreamInfo{
ID: s.ID().String(),
Sender: s.sender,
Target: s.target,
Element: elem,
}
halted, err := s.runHook(ctx, hook.S2SInStreamElementReceived, hInf)
halted, err := s.runHook(ctx, hook.S2SInStreamElementReceived, hi)
if err != nil {
return err
}
if halted {
return nil
}
switch stanza := hInf.Element.(type) {
switch stanza := hi.Element.(type) {
case stravaganza.Stanza:
return s.processStanza(ctx, stanza)

Expand Down Expand Up @@ -369,40 +369,41 @@ func (s *inS2S) processStanza(ctx context.Context, stanza stravaganza.Stanza) er

func (s *inS2S) processIQ(ctx context.Context, iq *stravaganza.IQ) error {
// run IQ received hook
_, err := s.runHook(ctx, hook.S2SInStreamIQReceived, &hook.S2SStreamInfo{
hi := &hook.S2SStreamInfo{
ID: s.ID().String(),
Sender: s.sender,
Target: s.target,
Element: iq,
})
}
_, err := s.runHook(ctx, hook.S2SInStreamIQReceived, hi)
if err != nil {
return err
}
iq = hi.Element.(*stravaganza.IQ)

if iq.IsResult() || iq.IsError() {
return nil // silently ignore
}
if s.mods.IsModuleIQ(iq) {
return s.mods.ProcessIQ(ctx, iq)
}
// run will route iq hook
hInf := &hook.S2SStreamInfo{
hi = &hook.S2SStreamInfo{
ID: s.ID().String(),
Sender: s.sender,
Target: s.target,
Element: iq,
}
halted, err := s.runHook(ctx, hook.S2SInStreamWillRouteElement, hInf)
halted, err := s.runHook(ctx, hook.S2SInStreamWillRouteElement, hi)
if halted {
return nil
}
if err != nil {
return err
}
outIQ, ok := hInf.Element.(*stravaganza.IQ)
if !ok {
return nil
}
targets, err := s.router.Route(ctx, outIQ)
iq = hi.Element.(*stravaganza.IQ)

targets, err := s.router.Route(ctx, iq)
switch err {
case router.ErrResourceNotFound:
return s.sendElement(ctx, stanzaerror.E(stanzaerror.ServiceUnavailable, iq).Element())
Expand All @@ -428,37 +429,36 @@ func (s *inS2S) processIQ(ctx context.Context, iq *stravaganza.IQ) error {

func (s *inS2S) processMessage(ctx context.Context, message *stravaganza.Message) error {
// post message received event
_, err := s.runHook(ctx, hook.S2SInStreamMessageReceived, &hook.S2SStreamInfo{
hi := &hook.S2SStreamInfo{
ID: s.ID().String(),
Sender: s.sender,
Target: s.target,
Element: message,
})
}
_, err := s.runHook(ctx, hook.S2SInStreamMessageReceived, hi)
if err != nil {
return err
}
msg := message
msg := hi.Element.(*stravaganza.Message)

sendMsg:
// run will route Message hook
hInf := &hook.S2SStreamInfo{
hi = &hook.S2SStreamInfo{
ID: s.ID().String(),
Sender: s.sender,
Target: s.target,
Element: msg,
}
halted, err := s.runHook(ctx, hook.S2SInStreamWillRouteElement, hInf)
halted, err := s.runHook(ctx, hook.S2SInStreamWillRouteElement, hi)
if halted {
return nil
}
if err != nil {
return err
}
outMsg, ok := hInf.Element.(*stravaganza.Message)
if !ok {
return nil
}
targets, err := s.router.Route(ctx, outMsg)
msg = hi.Element.(*stravaganza.Message)

targets, err := s.router.Route(ctx, msg)
switch err {
case router.ErrResourceNotFound:
// treat the stanza as if it were addressed to <node@domain>
Expand Down Expand Up @@ -500,35 +500,36 @@ sendMsg:

func (s *inS2S) processPresence(ctx context.Context, presence *stravaganza.Presence) error {
// run presence received hook
_, err := s.runHook(ctx, hook.S2SInStreamPresenceReceived, &hook.S2SStreamInfo{
hi := &hook.S2SStreamInfo{
ID: s.ID().String(),
Sender: s.sender,
Target: s.target,
Element: presence,
})
}
_, err := s.runHook(ctx, hook.S2SInStreamPresenceReceived, hi)
if err != nil {
return err
}
presence = hi.Element.(*stravaganza.Presence)

if presence.ToJID().IsFullWithUser() {
// run will route presence hook
hInf := &hook.S2SStreamInfo{
hi = &hook.S2SStreamInfo{
ID: s.ID().String(),
Sender: s.sender,
Target: s.target,
Element: presence,
}
halted, err := s.runHook(ctx, hook.S2SInStreamWillRouteElement, hInf)
halted, err := s.runHook(ctx, hook.S2SInStreamWillRouteElement, hi)
if halted {
return nil
}
if err != nil {
return err
}
outPr, ok := hInf.Element.(*stravaganza.Presence)
if !ok {
return nil
}
targets, err := s.router.Route(ctx, outPr)
presence = hi.Element.(*stravaganza.Presence)

targets, err := s.router.Route(ctx, presence)
switch err {
case nil, router.ErrUserNotAvailable:
_, err := s.runHook(ctx, hook.S2SInStreamPresenceRouted, &hook.S2SStreamInfo{
Expand Down

0 comments on commit 2a4dcb3

Please sign in to comment.