@@ -31,7 +31,6 @@ import {
3131 isError ,
3232} from "matrix-protection-suite" ;
3333import { UnlistedUserRedactionQueue } from "./queues/UnlistedUserRedactionQueue" ;
34- import { findCommandTable } from "./commands/interface-manager/InterfaceCommand" ;
3534import { ThrottlingQueue } from "./queues/ThrottlingQueue" ;
3635import ManagementRoomOutput from "./ManagementRoomOutput" ;
3736import { ReportPoller } from "./report/ReportPoller" ;
@@ -42,12 +41,6 @@ import {
4241 SynapseAdminClient ,
4342} from "matrix-protection-suite-for-matrix-bot-sdk" ;
4443import { IConfig } from "./config" ;
45- import {
46- COMMAND_PREFIX ,
47- DraupnirContext ,
48- extractCommandFromMessageBody ,
49- handleCommand ,
50- } from "./commands/CommandHandler" ;
5144import { LogLevel } from "matrix-bot-sdk" ;
5245import {
5346 ARGUMENT_PROMPT_LISTENER ,
@@ -59,7 +52,6 @@ import { RendererMessageCollector } from "./capabilities/RendererMessageCollecto
5952import { DraupnirRendererMessageCollector } from "./capabilities/DraupnirRendererMessageCollector" ;
6053import { renderProtectionFailedToStart } from "./protections/ProtectedRoomsSetRenderers" ;
6154import { draupnirStatusInfo , renderStatusInfo } from "./commands/StatusCommand" ;
62- import { renderMatrixAndSend } from "./commands/interface-manager/DeadDocumentMatrix" ;
6355import { isInvitationForUser } from "./protections/invitation/inviteCore" ;
6456import {
6557 StringRoomID ,
@@ -68,9 +60,13 @@ import {
6860 isStringRoomID ,
6961 isStringRoomAlias ,
7062 MatrixRoomReference ,
71- userLocalpart ,
7263 userServerName ,
7364} from "@the-draupnir-project/matrix-basic-types" ;
65+ import {
66+ MatrixAdaptorContext ,
67+ sendMatrixEventsFromDeadDocument ,
68+ } from "./commands/interface-manager/MPSMatrixInterfaceAdaptor" ;
69+ import { makeDraupnirCommandDispatcher } from "./commands/DraupnirCommandDispatcher" ;
7470
7571const log = new Logger ( "Draupnir" ) ;
7672
@@ -80,14 +76,14 @@ const log = new Logger("Draupnir");
8076// to Mjolnir because it needs to be started after Mjolnir started and not before.
8177// And giving it to the class was a dumb easy way of doing that.
8278
83- export class Draupnir implements Client {
79+ export class Draupnir implements Client , MatrixAdaptorContext {
8480 /**
8581 * This is for users who are not listed on a watchlist,
8682 * but have been flagged by the automatic spam detection as suispicous
8783 */
8884 public unlistedUserRedactionQueue = new UnlistedUserRedactionQueue ( ) ;
8985
90- private readonly commandTable = findCommandTable ( "draupnir" ) ;
86+ private readonly commandDispatcher = makeDraupnirCommandDispatcher ( this ) ;
9187 public taskQueue : ThrottlingQueue ;
9288 /**
9389 * Reporting back to the management room.
@@ -106,8 +102,6 @@ export class Draupnir implements Client {
106102
107103 public readonly reactionHandler : MatrixReactionHandler ;
108104
109- public readonly commandContext : Omit < DraupnirContext , "event" > ;
110-
111105 private readonly timelineEventListener = this . handleTimelineEvent . bind ( this ) ;
112106
113107 public readonly capabilityMessageRenderer : RendererMessageCollector ;
@@ -146,38 +140,16 @@ export class Draupnir implements Client {
146140 if ( config . pollReports ) {
147141 this . reportPoller = new ReportPoller ( this , this . reportManager ) ;
148142 }
149-
150- this . commandContext = {
151- draupnir : this ,
152- roomID : this . managementRoomID ,
153- client : this . client ,
154- reactionHandler : this . reactionHandler ,
155- clientPlatform : this . clientPlatform ,
156- } ;
157143 this . reactionHandler . on (
158144 ARGUMENT_PROMPT_LISTENER ,
159- makeListenerForArgumentPrompt (
160- this . client ,
161- this . clientPlatform ,
162- this . managementRoomID ,
163- this . reactionHandler ,
164- this . commandTable ,
165- this . commandContext
166- )
145+ makeListenerForArgumentPrompt ( this . commandDispatcher )
167146 ) ;
168147 this . reactionHandler . on (
169148 DEFAUILT_ARGUMENT_PROMPT_LISTENER ,
170- makeListenerForPromptDefault (
171- this . client ,
172- this . clientPlatform ,
173- this . managementRoomID ,
174- this . reactionHandler ,
175- this . commandTable ,
176- this . commandContext
177- )
149+ makeListenerForPromptDefault ( this . commandDispatcher )
178150 ) ;
179151 this . capabilityMessageRenderer = new DraupnirRendererMessageCollector (
180- this . client ,
152+ this . clientPlatform . toRoomMessageSender ( ) ,
181153 this . managementRoomID
182154 ) ;
183155 }
@@ -256,7 +228,7 @@ export class Draupnir implements Client {
256228 draupnir ,
257229 ( error , protectionName , description ) =>
258230 renderProtectionFailedToStart (
259- client ,
231+ clientPlatform . toRoomMessageSender ( ) ,
260232 managementRoom . toRoomIDOrAlias ( ) ,
261233 error ,
262234 protectionName ,
@@ -291,11 +263,11 @@ export class Draupnir implements Client {
291263 "Mjolnir@startup" ,
292264 "Startup complete. Now monitoring rooms."
293265 ) ;
294- await renderMatrixAndSend (
295- renderStatusInfo ( statusInfo ) ,
266+ await sendMatrixEventsFromDeadDocument (
267+ this . clientPlatform . toRoomMessageSender ( ) ,
296268 this . managementRoomID ,
297- undefined ,
298- this . client
269+ renderStatusInfo ( statusInfo ) ,
270+ { }
299271 ) ;
300272 } catch ( ex ) {
301273 log . error ( `Caught an error when trying to show status at startup` , ex ) ;
@@ -355,33 +327,12 @@ export class Draupnir implements Client {
355327 ) ;
356328 return ;
357329 }
358- const commandBeingRun = extractCommandFromMessageBody (
359- event . content . body ,
330+ this . commandDispatcher . handleCommandMessageEvent (
360331 {
361- prefix : COMMAND_PREFIX ,
362- localpart : userLocalpart ( this . clientUserID ) ,
363- userId : this . clientUserID ,
364- additionalPrefixes : this . config . commands . additionalPrefixes ,
365- allowNoPrefix : this . config . commands . allowNoPrefix ,
366- }
367- ) ;
368- if ( commandBeingRun === undefined ) {
369- return ;
370- }
371- log . info ( `Command being run by ${ event . sender } : ${ commandBeingRun } ` ) ;
372- void Task (
373- this . client
374- . sendReadReceipt ( roomID , event . event_id )
375- . then ( ( _ ) => Ok ( undefined ) )
376- ) ;
377- void Task (
378- handleCommand (
379- roomID ,
380332 event,
381- commandBeingRun ,
382- this ,
383- this . commandTable
384- ) . then ( ( _ ) => Ok ( undefined ) )
333+ roomID,
334+ } ,
335+ event . content . body
385336 ) ;
386337 }
387338 this . reportManager . handleTimelineEvent ( roomID , event ) ;
@@ -414,4 +365,11 @@ export class Draupnir implements Client {
414365 public handleEventReport ( report : EventReport ) : void {
415366 this . protectedRoomsSet . handleEventReport ( report ) ;
416367 }
368+
369+ /**
370+ * This is needed to implement the MatrixInterfaceAdaptor interface.
371+ */
372+ public get commandRoomID ( ) {
373+ return this . managementRoomID ;
374+ }
417375}
0 commit comments