@@ -27,7 +27,7 @@ import {
2727 TextualMessageEventContent
2828} from "matrix-bot-sdk" ;
2929
30- import BanList , { ALL_RULE_TYPES as ALL_BAN_LIST_RULE_TYPES , ListRuleChange , RULE_ROOM , RULE_SERVER , RULE_USER } from "./models/BanList " ;
30+ import { ALL_RULE_TYPES as ALL_BAN_LIST_RULE_TYPES , RULE_ROOM , RULE_SERVER , RULE_USER } from "./models/ListRule " ;
3131import { applyServerAcls } from "./actions/ApplyAcl" ;
3232import { RoomUpdateError } from "./models/RoomUpdateError" ;
3333import { COMMAND_PREFIX , handleCommand } from "./commands/CommandHandler" ;
@@ -50,6 +50,7 @@ import RuleServer from "./models/RuleServer";
5050import { RoomMemberManager } from "./RoomMembers" ;
5151import { ProtectedRoomActivityTracker } from "./queues/ProtectedRoomActivityTracker" ;
5252import { ThrottlingQueue } from "./queues/ThrottlingQueue" ;
53+ import PolicyList , { ListRuleChange } from "./models/PolicyList" ;
5354
5455const levelToFn = {
5556 [ LogLevel . DEBUG . toString ( ) ] : LogService . debug ,
@@ -153,7 +154,7 @@ export class Mjolnir {
153154 * @returns A new Mjolnir instance that can be started without further setup.
154155 */
155156 static async setupMjolnirFromConfig ( client : MatrixClient ) : Promise < Mjolnir > {
156- const banLists : BanList [ ] = [ ] ;
157+ const policyLists : PolicyList [ ] = [ ] ;
157158 const protectedRooms : { [ roomId : string ] : string } = { } ;
158159 const joinedRooms = await client . getJoinedRooms ( ) ;
159160 // Ensure we're also joined to the rooms we're protecting
@@ -178,7 +179,7 @@ export class Mjolnir {
178179 }
179180
180181 const ruleServer = config . web . ruleServer ? new RuleServer ( ) : null ;
181- const mjolnir = new Mjolnir ( client , managementRoomId , protectedRooms , banLists , ruleServer ) ;
182+ const mjolnir = new Mjolnir ( client , managementRoomId , protectedRooms , policyLists , ruleServer ) ;
182183 await mjolnir . logMessage ( LogLevel . INFO , "index" , "Mjolnir is starting up. Use !mjolnir to query status." ) ;
183184 Mjolnir . addJoinOnInviteListener ( mjolnir , client , config ) ;
184185 return mjolnir ;
@@ -192,9 +193,9 @@ export class Mjolnir {
192193 * If `config.protectAllJoinedRooms` is specified, then `protectedRooms` will be all joined rooms except watched banlists that we can't protect (because they aren't curated by us).
193194 */
194195 public readonly protectedRooms : { [ roomId : string ] : string } ,
195- private banLists : BanList [ ] ,
196+ private policyLists : PolicyList [ ] ,
196197 // Combines the rules from ban lists so they can be served to a homeserver module or another consumer.
197- public readonly ruleServer : RuleServer | null ,
198+ public readonly ruleServer : RuleServer | null ,
198199 ) {
199200 this . explicitlyProtectedRoomIds = Object . keys ( this . protectedRooms ) ;
200201
@@ -275,8 +276,8 @@ export class Mjolnir {
275276 this . taskQueue = new ThrottlingQueue ( this , config . backgroundDelayMS ) ;
276277 }
277278
278- public get lists ( ) : BanList [ ] {
279- return this . banLists ;
279+ public get lists ( ) : PolicyList [ ] {
280+ return this . policyLists ;
280281 }
281282
282283 public get state ( ) : string {
@@ -343,7 +344,7 @@ export class Mjolnir {
343344 } catch ( e ) {
344345 LogService . warn ( "Mjolnir" , extractRequestError ( e ) ) ;
345346 }
346- await this . buildWatchedBanLists ( ) ;
347+ await this . buildWatchedPolicyLists ( ) ;
347348 this . applyUnprotectedRooms ( ) ;
348349
349350 if ( config . verifyPermissionsOnStartup ) {
@@ -554,12 +555,12 @@ export class Mjolnir {
554555 const validatedSettings : { [ setting : string ] : any } = { }
555556 for ( let [ key , value ] of Object . entries ( savedSettings ) ) {
556557 if (
557- // is this a setting name with a known parser?
558- key in settingDefinitions
559- // is the datatype of this setting's value what we expect?
560- && typeof ( settingDefinitions [ key ] . value ) === typeof ( value )
561- // is this setting's value valid for the setting?
562- && settingDefinitions [ key ] . validate ( value )
558+ // is this a setting name with a known parser?
559+ key in settingDefinitions
560+ // is the datatype of this setting's value what we expect?
561+ && typeof ( settingDefinitions [ key ] . value ) === typeof ( value )
562+ // is this setting's value valid for the setting?
563+ && settingDefinitions [ key ] . validate ( value )
563564 ) {
564565 validatedSettings [ key ] = value ;
565566 } else {
@@ -593,8 +594,8 @@ export class Mjolnir {
593594 if ( ! ( key in protection . settings ) ) {
594595 throw new ProtectionSettingValidationError ( `Failed to find protection setting by name: ${ key } ` ) ;
595596 }
596- if ( typeof ( protection . settings [ key ] . value ) !== typeof ( value ) ) {
597- throw new ProtectionSettingValidationError ( `Invalid type for protection setting: ${ key } (${ typeof ( value ) } )` ) ;
597+ if ( typeof ( protection . settings [ key ] . value ) !== typeof ( value ) ) {
598+ throw new ProtectionSettingValidationError ( `Invalid type for protection setting: ${ key } (${ typeof ( value ) } )` ) ;
598599 }
599600 if ( ! protection . settings [ key ] . validate ( value ) ) {
600601 throw new ProtectionSettingValidationError ( `Invalid value for protection setting: ${ key } (${ value } )` ) ;
@@ -644,16 +645,16 @@ export class Mjolnir {
644645 }
645646
646647 /**
647- * Helper for constructing `BanList `s and making sure they have the right listeners set up.
648- * @param roomId The room id for the `BanList `.
649- * @param roomRef A reference (matrix.to URL) for the `BanList `.
648+ * Helper for constructing `PolicyList `s and making sure they have the right listeners set up.
649+ * @param roomId The room id for the `PolicyList `.
650+ * @param roomRef A reference (matrix.to URL) for the `PolicyList `.
650651 */
651- private async addBanList ( roomId : string , roomRef : string ) : Promise < BanList > {
652- const list = new BanList ( roomId , roomRef , this . client ) ;
652+ private async addPolicyList ( roomId : string , roomRef : string ) : Promise < PolicyList > {
653+ const list = new PolicyList ( roomId , roomRef , this . client ) ;
653654 this . ruleServer ?. watch ( list ) ;
654- list . on ( 'BanList .batch' , this . syncWithBanList . bind ( this ) ) ;
655+ list . on ( 'PolicyList .batch' , this . syncWithPolicyList . bind ( this ) ) ;
655656 await list . updateList ( ) ;
656- this . banLists . push ( list ) ;
657+ this . policyLists . push ( list ) ;
657658 return list ;
658659 }
659660
@@ -667,7 +668,7 @@ export class Mjolnir {
667668 return this . protections . get ( protectionName ) ?? null ;
668669 }
669670
670- public async watchList ( roomRef : string ) : Promise < BanList | null > {
671+ public async watchList ( roomRef : string ) : Promise < PolicyList | null > {
671672 const joinedRooms = await this . client . getJoinedRooms ( ) ;
672673 const permalink = Permalinks . parseUrl ( roomRef ) ;
673674 if ( ! permalink . roomIdOrAlias ) return null ;
@@ -677,37 +678,37 @@ export class Mjolnir {
677678 await this . client . joinRoom ( permalink . roomIdOrAlias , permalink . viaServers ) ;
678679 }
679680
680- if ( this . banLists . find ( b => b . roomId === roomId ) ) return null ;
681+ if ( this . policyLists . find ( b => b . roomId === roomId ) ) return null ;
681682
682- const list = await this . addBanList ( roomId , roomRef ) ;
683+ const list = await this . addPolicyList ( roomId , roomRef ) ;
683684
684685 await this . client . setAccountData ( WATCHED_LISTS_EVENT_TYPE , {
685- references : this . banLists . map ( b => b . roomRef ) ,
686+ references : this . policyLists . map ( b => b . roomRef ) ,
686687 } ) ;
687688
688- await this . warnAboutUnprotectedBanListRoom ( roomId ) ;
689+ await this . warnAboutUnprotectedPolicyListRoom ( roomId ) ;
689690
690691 return list ;
691692 }
692693
693- public async unwatchList ( roomRef : string ) : Promise < BanList | null > {
694+ public async unwatchList ( roomRef : string ) : Promise < PolicyList | null > {
694695 const permalink = Permalinks . parseUrl ( roomRef ) ;
695696 if ( ! permalink . roomIdOrAlias ) return null ;
696697
697698 const roomId = await this . client . resolveRoom ( permalink . roomIdOrAlias ) ;
698- const list = this . banLists . find ( b => b . roomId === roomId ) || null ;
699+ const list = this . policyLists . find ( b => b . roomId === roomId ) || null ;
699700 if ( list ) {
700- this . banLists . splice ( this . banLists . indexOf ( list ) , 1 ) ;
701+ this . policyLists . splice ( this . policyLists . indexOf ( list ) , 1 ) ;
701702 this . ruleServer ?. unwatch ( list ) ;
702703 }
703704
704705 await this . client . setAccountData ( WATCHED_LISTS_EVENT_TYPE , {
705- references : this . banLists . map ( b => b . roomRef ) ,
706+ references : this . policyLists . map ( b => b . roomRef ) ,
706707 } ) ;
707708 return list ;
708709 }
709710
710- public async warnAboutUnprotectedBanListRoom ( roomId : string ) {
711+ public async warnAboutUnprotectedPolicyListRoom ( roomId : string ) {
711712 if ( ! config . protectAllJoinedRooms ) return ; // doesn't matter
712713 if ( this . explicitlyProtectedRoomIds . includes ( roomId ) ) return ; // explicitly protected
713714
@@ -735,8 +736,8 @@ export class Mjolnir {
735736 }
736737 }
737738
738- private async buildWatchedBanLists ( ) {
739- this . banLists = [ ] ;
739+ private async buildWatchedPolicyLists ( ) {
740+ this . policyLists = [ ] ;
740741 const joinedRooms = await this . client . getJoinedRooms ( ) ;
741742
742743 let watchedListsEvent : { references ?: string [ ] } | null = null ;
@@ -755,8 +756,8 @@ export class Mjolnir {
755756 await this . client . joinRoom ( permalink . roomIdOrAlias , permalink . viaServers ) ;
756757 }
757758
758- await this . warnAboutUnprotectedBanListRoom ( roomId ) ;
759- await this . addBanList ( roomId , roomRef ) ;
759+ await this . warnAboutUnprotectedPolicyListRoom ( roomId ) ;
760+ await this . addPolicyList ( roomId , roomRef ) ;
760761 }
761762 }
762763
@@ -882,15 +883,15 @@ export class Mjolnir {
882883 * @param verbose Whether to report any errors to the management room.
883884 */
884885 public async syncLists ( verbose = true ) {
885- for ( const list of this . banLists ) {
886+ for ( const list of this . policyLists ) {
886887 const changes = await list . updateList ( ) ;
887888 await this . printBanlistChanges ( changes , list , true ) ;
888889 }
889890
890891 let hadErrors = false ;
891892 const [ aclErrors , banErrors ] = await Promise . all ( [
892- applyServerAcls ( this . banLists , this . protectedRoomsByActivity ( ) , this ) ,
893- applyUserBans ( this . banLists , this . protectedRoomsByActivity ( ) , this )
893+ applyServerAcls ( this . policyLists , this . protectedRoomsByActivity ( ) , this ) ,
894+ applyUserBans ( this . policyLists , this . protectedRoomsByActivity ( ) , this )
894895 ] ) ;
895896 const redactionErrors = await this . processRedactionQueue ( ) ;
896897 hadErrors = hadErrors || await this . printActionResult ( aclErrors , "Errors updating server ACLs:" ) ;
@@ -912,16 +913,16 @@ export class Mjolnir {
912913 /**
913914 * Pulls any changes to the rules that are in a policy room and updates all protected rooms
914915 * with those changes. Does not fail if there are errors updating the room, these are reported to the management room.
915- * @param banList The `BanList ` which we will check for changes and apply them to all protected rooms.
916+ * @param policyList The `PolicyList ` which we will check for changes and apply them to all protected rooms.
916917 * @returns When all of the protected rooms have been updated.
917918 */
918- private async syncWithBanList ( banList : BanList ) : Promise < void > {
919- const changes = await banList . updateList ( ) ;
919+ private async syncWithPolicyList ( policyList : PolicyList ) : Promise < void > {
920+ const changes = await policyList . updateList ( ) ;
920921
921922 let hadErrors = false ;
922923 const [ aclErrors , banErrors ] = await Promise . all ( [
923- applyServerAcls ( this . banLists , this . protectedRoomsByActivity ( ) , this ) ,
924- applyUserBans ( this . banLists , this . protectedRoomsByActivity ( ) , this )
924+ applyServerAcls ( this . policyLists , this . protectedRoomsByActivity ( ) , this ) ,
925+ applyUserBans ( this . policyLists , this . protectedRoomsByActivity ( ) , this )
925926 ] ) ;
926927 const redactionErrors = await this . processRedactionQueue ( ) ;
927928 hadErrors = hadErrors || await this . printActionResult ( aclErrors , "Errors updating server ACLs:" ) ;
@@ -939,7 +940,7 @@ export class Mjolnir {
939940 } ) ;
940941 }
941942 // This can fail if the change is very large and it is much less important than applying bans, so do it last.
942- await this . printBanlistChanges ( changes , banList , true ) ;
943+ await this . printBanlistChanges ( changes , policyList , true ) ;
943944 }
944945
945946 private async handleConsequence ( protection : Protection , roomId : string , eventId : string , sender : string , consequence : Consequence ) {
@@ -989,10 +990,10 @@ export class Mjolnir {
989990
990991 // Check for updated ban lists before checking protected rooms - the ban lists might be protected
991992 // themselves.
992- const banList = this . banLists . find ( list => list . roomId === roomId ) ;
993- if ( banList !== undefined ) {
993+ const policyList = this . policyLists . find ( list => list . roomId === roomId ) ;
994+ if ( policyList !== undefined ) {
994995 if ( ALL_BAN_LIST_RULE_TYPES . includes ( event [ 'type' ] ) || event [ 'type' ] === 'm.room.redaction' ) {
995- banList . updateForEvent ( event )
996+ policyList . updateForEvent ( event )
996997 }
997998 }
998999
@@ -1037,7 +1038,7 @@ export class Mjolnir {
10371038 // we cannot eagerly ban users (that is to ban them when they have never been a member)
10381039 // as they can be force joined to a room they might not have known existed.
10391040 // Only apply bans and then redactions in the room we are currently looking at.
1040- const banErrors = await applyUserBans ( this . banLists , [ roomId ] , this ) ;
1041+ const banErrors = await applyUserBans ( this . policyLists , [ roomId ] , this ) ;
10411042 const redactionErrors = await this . processRedactionQueue ( roomId ) ;
10421043 await this . printActionResult ( banErrors ) ;
10431044 await this . printActionResult ( redactionErrors ) ;
@@ -1051,7 +1052,7 @@ export class Mjolnir {
10511052 * @param ignoreSelf Whether to exclude changes that have been made by Mjolnir.
10521053 * @returns true if the message was sent, false if it wasn't (because there there were no changes to report).
10531054 */
1054- private async printBanlistChanges ( changes : ListRuleChange [ ] , list : BanList , ignoreSelf = false ) : Promise < boolean > {
1055+ private async printBanlistChanges ( changes : ListRuleChange [ ] , list : PolicyList , ignoreSelf = false ) : Promise < boolean > {
10551056 if ( ignoreSelf ) {
10561057 const sender = await this . client . getUserId ( ) ;
10571058 changes = changes . filter ( change => change . sender !== sender ) ;
0 commit comments