diff --git a/go.mod b/go.mod index 3ea65bd2..c45bb4f8 100644 --- a/go.mod +++ b/go.mod @@ -38,6 +38,7 @@ require ( github.com/tidwall/sjson v1.2.5 // indirect github.com/yuin/goldmark v1.7.4 // indirect go.mau.fi/zeroconfig v0.1.3 // indirect + golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.24.0 // indirect golang.org/x/text v0.17.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect @@ -45,3 +46,5 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect maunium.net/go/mauflag v1.0.0 // indirect ) + +replace maunium.net/go/mautrix => github.com/maltee1/mautrix-go v0.0.0-20240820201051-5fa9ef2ab1bf diff --git a/go.sum b/go.sum index 3cabb480..5920d028 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/maltee1/mautrix-go v0.0.0-20240820201051-5fa9ef2ab1bf h1:n18kxEPB1c7mlMP0ysiel3orOa5uoi1O60yY21UQbfM= +github.com/maltee1/mautrix-go v0.0.0-20240820201051-5fa9ef2ab1bf/go.mod h1:NhWZ4jpQ2CW+t6TmGrnydAIL0htdoXmGiNTdHb2PzL4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -72,6 +74,8 @@ golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDT golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ= golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -90,7 +94,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M= maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA= -maunium.net/go/mautrix v0.20.1-0.20240817111253-2355d70426f4 h1:S4+mHwQC2CiCTl0cn21zd4V8JiqGYMYlsde2v7jC8bE= -maunium.net/go/mautrix v0.20.1-0.20240817111253-2355d70426f4/go.mod h1:V725r8w7oddsS7CxnmTAp634A4nwJCFY7J3jiTMUz2c= nhooyr.io/websocket v1.8.17 h1:KEVeLJkUywCKVsnLIDlD/5gtayKp8VoCkksHCGGfT9Y= nhooyr.io/websocket v1.8.17/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 56ab59ac..977cd1c0 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -69,6 +69,7 @@ var ( _ bridgev2.RoomNameHandlingNetworkAPI = (*SignalClient)(nil) _ bridgev2.RoomAvatarHandlingNetworkAPI = (*SignalClient)(nil) _ bridgev2.RoomTopicHandlingNetworkAPI = (*SignalClient)(nil) + _ bridgev2.JoinRulesHandlingNetworkAPI = (*SignalClient)(nil) ) var pushCfg = &bridgev2.PushConfig{ diff --git a/pkg/connector/handlematrix.go b/pkg/connector/handlematrix.go index 87cebe2a..abd0f936 100644 --- a/pkg/connector/handlematrix.go +++ b/pkg/connector/handlematrix.go @@ -472,3 +472,50 @@ func (s *SignalClient) HandleMatrixMembership(ctx context.Context, msg *bridgev2 msg.Portal.Metadata.(*signalid.PortalMetadata).Revision = revision return true, nil } + +func joinRuleToAccessControl(ctx context.Context, joinRule event.JoinRule) signalmeow.AccessControl { + log := zerolog.Ctx(ctx).With(). + Str("JoinRule", string(joinRule)). + Logger() + var addFromInviteLinkAccess signalmeow.AccessControl + switch joinRule { + case event.JoinRuleInvite: + addFromInviteLinkAccess = signalmeow.AccessControl_UNSATISFIABLE + case event.JoinRuleKnock, event.JoinRuleKnockRestricted: + addFromInviteLinkAccess = signalmeow.AccessControl_ADMINISTRATOR + case event.JoinRulePublic, event.JoinRuleRestricted: + addFromInviteLinkAccess = signalmeow.AccessControl_ANY + default: + log.Debug().Msg("unknown join rule") + addFromInviteLinkAccess = signalmeow.AccessControl_UNSATISFIABLE + } + return addFromInviteLinkAccess +} + +func (s *SignalClient) HandleMatrixJoinRules(ctx context.Context, msg *bridgev2.MatrixJoinRule) (bool, error) { + log := zerolog.Ctx(ctx).With(). + Str("From JoinRule", string(msg.PrevContent.JoinRule)). + Str("To JoinRule", string(msg.Content.JoinRule)). + Logger() + if msg.Portal.RoomType == database.RoomTypeDM { + log.Info().Msg("Ignoring Join Rule Change for DM") + return false, nil + } + newAddFromInviteLinkAccess := joinRuleToAccessControl(ctx, msg.Content.JoinRule) + oldAddFromInviteLinkAccess := joinRuleToAccessControl(ctx, msg.PrevContent.JoinRule) + if newAddFromInviteLinkAccess == oldAddFromInviteLinkAccess { + log.Debug().Msg("join rule change does not hange access control") + return false, nil + } + gc := &signalmeow.GroupChange{ModifyAddFromInviteLinkAccess: &newAddFromInviteLinkAccess} + _, groupID, err := signalid.ParsePortalID(msg.Portal.ID) + if err != nil || groupID == "" { + return false, err + } + revision, err := s.Client.UpdateGroup(ctx, gc, groupID) + if err != nil { + return false, err + } + msg.Portal.Metadata.(*signalid.PortalMetadata).Revision = revision + return true, nil +}