@@ -17,6 +17,14 @@ type EventIDFormat int
1717// RedactionAlgorithm refers to the redaction algorithm used in a room version.
1818type RedactionAlgorithm int
1919
20+ // JoinRulesPermittingKnockInEventAuth specifies which kinds of join_rule allow
21+ // a room to be knocked upon.
22+ type JoinRulesPermittingKnockInEventAuth int
23+
24+ // JoinRulesPermittingRestrictedJoinInEventAuth specifies which kinds of join_rule allow
25+ // a room to be joined via a space.
26+ type JoinRulesPermittingRestrictedJoinInEventAuth int
27+
2028// Room version constants. These are strings because the version grammar
2129// allows for future expansion.
2230// https://matrix.org/docs/spec/#room-version-grammar
@@ -59,6 +67,20 @@ const (
5967 RedactionAlgorithmV4 // protects membership 'join_authorised_via_users_server' key
6068)
6169
70+ // Which join_rules permit knocking?
71+ const (
72+ KnocksForbidden JoinRulesPermittingKnockInEventAuth = iota + 1 // no rooms can be knocked upon
73+ KnockOnly // rooms with join_rule "knock" can be knocked upon
74+ KnockOrKnockRestricted // rooms with join_rule "knock" or "knock_restricted" can be knocked upon
75+ )
76+
77+ // Which join_rules permit restricted joins?
78+ const (
79+ NoRestrictedJoins JoinRulesPermittingRestrictedJoinInEventAuth = iota + 1 // no rooms can be joined via a space
80+ RestrictedOnly // rooms with join_rule "restricted" can be joined via a space
81+ RestrictedOrKnockRestricted // rooms with join_rule "restricted" or "knock_restricted" can be joined via a space
82+ )
83+
6284var roomVersionMeta = map [RoomVersion ]RoomVersionDescription {
6385 RoomVersionV1 : {
6486 Supported : true ,
@@ -70,8 +92,8 @@ var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
7092 enforceSignatureChecks : false ,
7193 enforceCanonicalJSON : false ,
7294 powerLevelsIncludeNotifications : false ,
73- allowKnockingInEventAuth : false ,
74- allowRestrictedJoinsInEventAuth : false ,
95+ allowKnockingInEventAuth : KnocksForbidden ,
96+ allowRestrictedJoinsInEventAuth : NoRestrictedJoins ,
7597 requireIntegerPowerLevels : false ,
7698 },
7799 RoomVersionV2 : {
@@ -84,8 +106,8 @@ var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
84106 enforceSignatureChecks : false ,
85107 enforceCanonicalJSON : false ,
86108 powerLevelsIncludeNotifications : false ,
87- allowKnockingInEventAuth : false ,
88- allowRestrictedJoinsInEventAuth : false ,
109+ allowKnockingInEventAuth : KnocksForbidden ,
110+ allowRestrictedJoinsInEventAuth : NoRestrictedJoins ,
89111 requireIntegerPowerLevels : false ,
90112 },
91113 RoomVersionV3 : {
@@ -98,8 +120,8 @@ var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
98120 enforceSignatureChecks : false ,
99121 enforceCanonicalJSON : false ,
100122 powerLevelsIncludeNotifications : false ,
101- allowKnockingInEventAuth : false ,
102- allowRestrictedJoinsInEventAuth : false ,
123+ allowKnockingInEventAuth : KnocksForbidden ,
124+ allowRestrictedJoinsInEventAuth : NoRestrictedJoins ,
103125 requireIntegerPowerLevels : false ,
104126 },
105127 RoomVersionV4 : {
@@ -112,8 +134,8 @@ var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
112134 enforceSignatureChecks : false ,
113135 enforceCanonicalJSON : false ,
114136 powerLevelsIncludeNotifications : false ,
115- allowKnockingInEventAuth : false ,
116- allowRestrictedJoinsInEventAuth : false ,
137+ allowKnockingInEventAuth : KnocksForbidden ,
138+ allowRestrictedJoinsInEventAuth : NoRestrictedJoins ,
117139 requireIntegerPowerLevels : false ,
118140 },
119141 RoomVersionV5 : {
@@ -126,8 +148,8 @@ var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
126148 enforceSignatureChecks : true ,
127149 enforceCanonicalJSON : false ,
128150 powerLevelsIncludeNotifications : false ,
129- allowKnockingInEventAuth : false ,
130- allowRestrictedJoinsInEventAuth : false ,
151+ allowKnockingInEventAuth : KnocksForbidden ,
152+ allowRestrictedJoinsInEventAuth : NoRestrictedJoins ,
131153 requireIntegerPowerLevels : false ,
132154 },
133155 RoomVersionV6 : {
@@ -140,8 +162,8 @@ var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
140162 enforceSignatureChecks : true ,
141163 enforceCanonicalJSON : true ,
142164 powerLevelsIncludeNotifications : true ,
143- allowKnockingInEventAuth : false ,
144- allowRestrictedJoinsInEventAuth : false ,
165+ allowKnockingInEventAuth : KnocksForbidden ,
166+ allowRestrictedJoinsInEventAuth : NoRestrictedJoins ,
145167 requireIntegerPowerLevels : false ,
146168 },
147169 RoomVersionV7 : {
@@ -154,8 +176,8 @@ var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
154176 enforceSignatureChecks : true ,
155177 enforceCanonicalJSON : true ,
156178 powerLevelsIncludeNotifications : true ,
157- allowKnockingInEventAuth : true ,
158- allowRestrictedJoinsInEventAuth : false ,
179+ allowKnockingInEventAuth : KnockOnly ,
180+ allowRestrictedJoinsInEventAuth : NoRestrictedJoins ,
159181 requireIntegerPowerLevels : false ,
160182 },
161183 RoomVersionV8 : {
@@ -168,8 +190,8 @@ var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
168190 enforceSignatureChecks : true ,
169191 enforceCanonicalJSON : true ,
170192 powerLevelsIncludeNotifications : true ,
171- allowKnockingInEventAuth : true ,
172- allowRestrictedJoinsInEventAuth : true ,
193+ allowKnockingInEventAuth : KnocksForbidden ,
194+ allowRestrictedJoinsInEventAuth : RestrictedOnly ,
173195 requireIntegerPowerLevels : false ,
174196 },
175197 RoomVersionV9 : {
@@ -182,8 +204,8 @@ var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
182204 enforceSignatureChecks : true ,
183205 enforceCanonicalJSON : true ,
184206 powerLevelsIncludeNotifications : true ,
185- allowKnockingInEventAuth : true ,
186- allowRestrictedJoinsInEventAuth : true ,
207+ allowKnockingInEventAuth : KnocksForbidden ,
208+ allowRestrictedJoinsInEventAuth : RestrictedOnly ,
187209 requireIntegerPowerLevels : false ,
188210 },
189211 "org.matrix.msc3667" : { // based on room version 7
@@ -196,10 +218,24 @@ var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
196218 enforceSignatureChecks : true ,
197219 enforceCanonicalJSON : true ,
198220 powerLevelsIncludeNotifications : true ,
199- allowKnockingInEventAuth : true ,
200- allowRestrictedJoinsInEventAuth : false ,
221+ allowKnockingInEventAuth : KnockOnly ,
222+ allowRestrictedJoinsInEventAuth : NoRestrictedJoins ,
201223 requireIntegerPowerLevels : true ,
202224 },
225+ "org.matrix.msc3787" : { // roughly, the union of v7 and v9
226+ Supported : true ,
227+ Stable : false ,
228+ stateResAlgorithm : StateResV2 ,
229+ eventFormat : EventFormatV2 ,
230+ eventIDFormat : EventIDFormatV3 ,
231+ redactionAlgorithm : RedactionAlgorithmV4 ,
232+ enforceSignatureChecks : true ,
233+ enforceCanonicalJSON : true ,
234+ powerLevelsIncludeNotifications : true ,
235+ allowKnockingInEventAuth : KnockOrKnockRestricted ,
236+ allowRestrictedJoinsInEventAuth : RestrictedOrKnockRestricted ,
237+ requireIntegerPowerLevels : false ,
238+ },
203239}
204240
205241// RoomVersions returns information about room versions currently
@@ -249,11 +285,11 @@ type RoomVersionDescription struct {
249285 eventFormat EventFormat
250286 eventIDFormat EventIDFormat
251287 redactionAlgorithm RedactionAlgorithm
288+ allowKnockingInEventAuth JoinRulesPermittingKnockInEventAuth
289+ allowRestrictedJoinsInEventAuth JoinRulesPermittingRestrictedJoinInEventAuth
252290 enforceSignatureChecks bool
253291 enforceCanonicalJSON bool
254292 powerLevelsIncludeNotifications bool
255- allowKnockingInEventAuth bool
256- allowRestrictedJoinsInEventAuth bool
257293 requireIntegerPowerLevels bool
258294 Supported bool
259295 Stable bool
@@ -309,20 +345,50 @@ func (v RoomVersion) PowerLevelsIncludeNotifications() (bool, error) {
309345 return false , UnsupportedRoomVersionError {v }
310346}
311347
312- // AllowKnockingInEventAuth returns true if the given room version allows for
313- // the `knock` membership state or false otherwise.
314- func (v RoomVersion ) AllowKnockingInEventAuth () (bool , error ) {
348+ // AllowKnockingInEventAuth returns true if the given room version and given
349+ // join rule allows for the `knock` membership state or false otherwise.
350+ func (v RoomVersion ) AllowKnockingInEventAuth (joinRule string ) (bool , error ) {
315351 if r , ok := roomVersionMeta [v ]; ok {
316- return r .allowKnockingInEventAuth , nil
352+ switch r .allowKnockingInEventAuth {
353+ case KnockOnly :
354+ return joinRule == Knock , nil
355+ case KnockOrKnockRestricted :
356+ return (joinRule == Knock || joinRule == KnockRestricted ), nil
357+ case KnocksForbidden :
358+ return false , nil
359+ }
317360 }
318361 return false , UnsupportedRoomVersionError {v }
319362}
320363
321- // AllowRestrictedJoinsInEventAuth returns true if the given room version allows
322- // for memberships signed by servers in the restricted join rules.
323- func (v RoomVersion ) AllowRestrictedJoinsInEventAuth () (bool , error ) {
364+ // AllowRestrictedJoinsInEventAuth returns true if the given room version and
365+ // join rule allows for memberships signed by servers in the restricted join rules.
366+ func (v RoomVersion ) AllowRestrictedJoinsInEventAuth (joinRule string ) (bool , error ) {
324367 if r , ok := roomVersionMeta [v ]; ok {
325- return r .allowRestrictedJoinsInEventAuth , nil
368+ switch r .allowRestrictedJoinsInEventAuth {
369+ case NoRestrictedJoins :
370+ return false , nil
371+ case RestrictedOnly :
372+ return joinRule == Restricted , nil
373+ case RestrictedOrKnockRestricted :
374+ return (joinRule == Restricted || joinRule == KnockRestricted ), nil
375+ }
376+ }
377+ return false , UnsupportedRoomVersionError {v }
378+ }
379+
380+ // MayAllowRestrictedJoinsInEventAuth returns true if the given room version
381+ // might allow for memberships signed by servers in the restricted join rules.
382+ // (For an authoritative answer, the room's join rules must be known. If they
383+ // are, use AllowRestrictedJoinsInEventAuth.)
384+ func (v RoomVersion ) MayAllowRestrictedJoinsInEventAuth () (bool , error ) {
385+ if r , ok := roomVersionMeta [v ]; ok {
386+ switch r .allowRestrictedJoinsInEventAuth {
387+ case NoRestrictedJoins :
388+ return false , nil
389+ case RestrictedOnly , RestrictedOrKnockRestricted :
390+ return true , nil
391+ }
326392 }
327393 return false , UnsupportedRoomVersionError {v }
328394}
0 commit comments