forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for XEP-0202: Entity Time (ortuman#150)
- Loading branch information
Showing
7 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2021 The jackal Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package xep0202 | ||
|
||
import "github.com/ortuman/jackal/router" | ||
|
||
//go:generate moq -out router.mock_test.go . globalRouter:routerMock | ||
type globalRouter interface { | ||
router.Router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2021 The jackal Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package xep0202 | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/jackal-xmpp/stravaganza/v2" | ||
stanzaerror "github.com/jackal-xmpp/stravaganza/v2/errors/stanza" | ||
"github.com/ortuman/jackal/log" | ||
"github.com/ortuman/jackal/router" | ||
xmpputil "github.com/ortuman/jackal/util/xmpp" | ||
) | ||
|
||
const timeNamespace = "urn:xmpp:time" | ||
|
||
const ( | ||
// ModuleName represents time module name. | ||
ModuleName = "time" | ||
|
||
// XEPNumber represents time XEP number. | ||
XEPNumber = "0202" | ||
) | ||
|
||
// Time represents a last activity (XEP-0202) module type. | ||
type Time struct { | ||
router router.Router | ||
tmFn func() time.Time | ||
} | ||
|
||
// New returns a new initialized Time instance. | ||
func New( | ||
router router.Router, | ||
) *Time { | ||
return &Time{ | ||
router: router, | ||
tmFn: time.Now, | ||
} | ||
} | ||
|
||
// Name returns time module name. | ||
func (m *Time) Name() string { return ModuleName } | ||
|
||
// StreamFeature returns time module stream feature. | ||
func (m *Time) StreamFeature(_ context.Context, _ string) (stravaganza.Element, error) { | ||
return nil, nil | ||
} | ||
|
||
// ServerFeatures returns time server disco features. | ||
func (m *Time) ServerFeatures(_ context.Context) ([]string, error) { | ||
return []string{timeNamespace}, nil | ||
} | ||
|
||
// AccountFeatures returns time account disco features. | ||
func (m *Time) AccountFeatures(_ context.Context) ([]string, error) { | ||
return nil, nil | ||
} | ||
|
||
// Start starts time module. | ||
func (m *Time) Start(_ context.Context) error { | ||
log.Infow("Started time module", "xep", XEPNumber) | ||
return nil | ||
} | ||
|
||
// Stop stops time module. | ||
func (m *Time) Stop(_ context.Context) error { | ||
log.Infow("Stopped time module", "xep", XEPNumber) | ||
return nil | ||
} | ||
|
||
// MatchesNamespace tells whether namespace matches time module. | ||
func (m *Time) MatchesNamespace(namespace string, serverTarget bool) bool { | ||
if !serverTarget { | ||
return false | ||
} | ||
return namespace == timeNamespace | ||
} | ||
|
||
// ProcessIQ process a time iq. | ||
func (m *Time) ProcessIQ(ctx context.Context, iq *stravaganza.IQ) error { | ||
switch { | ||
case iq.IsGet() && iq.ChildNamespace("time", timeNamespace) != nil: | ||
m.reportServerTime(ctx, iq) | ||
return nil | ||
default: | ||
_, _ = m.router.Route(ctx, xmpputil.MakeErrorStanza(iq, stanzaerror.BadRequest)) | ||
return nil | ||
} | ||
} | ||
|
||
func (m *Time) reportServerTime(ctx context.Context, iq *stravaganza.IQ) { | ||
tm := m.tmFn() | ||
|
||
resIQ := xmpputil.MakeResultIQ(iq, stravaganza.NewBuilder("time"). | ||
WithAttribute(stravaganza.Namespace, timeNamespace). | ||
WithChild(stravaganza.NewBuilder("tzo").WithText(tm.Format("-07:00")).Build()). | ||
WithChild(stravaganza.NewBuilder("utc").WithText(tm.UTC().Format("2006-01-02T15:04:05Z")).Build()). | ||
Build(), | ||
) | ||
_, _ = m.router.Route(ctx, resIQ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2021 The jackal Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package xep0202 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/jackal-xmpp/stravaganza/v2" | ||
"github.com/jackal-xmpp/stravaganza/v2/jid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTime_GetTime(t *testing.T) { | ||
// given | ||
routerMock := &routerMock{} | ||
|
||
var respStanzas []stravaganza.Stanza | ||
routerMock.RouteFunc = func(ctx context.Context, stanza stravaganza.Stanza) ([]jid.JID, error) { | ||
respStanzas = append(respStanzas, stanza) | ||
return nil, nil | ||
} | ||
m := &Time{ | ||
router: routerMock, | ||
tmFn: func() time.Time { | ||
return time.Date(1984, 01, 03, 00, 00, 00, 00, time.UTC) | ||
}, | ||
} | ||
|
||
// when | ||
iq, _ := stravaganza.NewIQBuilder(). | ||
WithAttribute(stravaganza.ID, uuid.New().String()). | ||
WithAttribute(stravaganza.Type, stravaganza.GetType). | ||
WithAttribute(stravaganza.From, "ortuman@jackal.im/chamber"). | ||
WithAttribute(stravaganza.To, "jackal.im"). | ||
WithChild( | ||
stravaganza.NewBuilder("time"). | ||
WithAttribute(stravaganza.Namespace, timeNamespace). | ||
Build(), | ||
). | ||
BuildIQ() | ||
_ = m.ProcessIQ(context.Background(), iq) | ||
|
||
// then | ||
require.Len(t, respStanzas, 1) | ||
|
||
require.Equal(t, "iq", respStanzas[0].Name()) | ||
require.Equal(t, stravaganza.ResultType, respStanzas[0].Attribute(stravaganza.Type)) | ||
|
||
tm := respStanzas[0].ChildNamespace("time", timeNamespace) | ||
require.NotNil(t, tm) | ||
|
||
tzo := tm.Child("tzo") | ||
utc := tm.Child("utc") | ||
|
||
require.Equal(t, "+00:00", tzo.Text()) | ||
require.Equal(t, "1984-01-03T00:00:00Z", utc.Text()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters