import { Cli , Bridge , AppServiceRegistration , ClientEncryptionSession , ClientEncryptionStore , Logging } from '../../..' ; Logging . configure ( { console : "debug" , } ) ; const log = Logging . get ( "index" ) ; const encMap = new Map < string , ClientEncryptionSession > ( ) ; const encryptionStore = { async getStoredSession ( userId ) { return encMap . get ( userId ) || null ; } , async setStoredSession ( session ) { log . info ( "Set session" , session . userId , session . deviceId ) ; encMap . set ( session . userId , session ) ; } , async updateSyncToken ( ) { } , } ; new Cli ( { registrationPath : "enc-registration.yaml" , generateRegistration : function ( reg , callback ) { reg . setId ( AppServiceRegistration . generateToken ( ) ) ; reg . setHomeserverToken ( AppServiceRegistration . generateToken ( ) ) ; reg . setAppServiceToken ( AppServiceRegistration . generateToken ( ) ) ; reg . setSenderLocalpart ( "encbot" ) ; reg . addRegexPattern ( "users" , "@enc_.*" , true ) ; callback ( reg ) ; } , run : function ( port , _config , registration ) { let bridge ; bridge = new Bridge ( { homeserverUrl : "http://localhost:8008" , domain : process . env . MATRIX_DOMAIN , registration : "enc-registration.yaml" , bridgeEncryption : { homeserverUrl : "http://localhost:8004" , store : encryptionStore , } , controller : { onUserQuery : function ( ) { return { } ; } , onEvent : async function ( request , context ) { const event = request . getData ( ) ; const bot = bridge . getBot ( ) ; const intent = bridge . getIntentFromLocalpart ( `enc_${ context . senders . matrix . localpart }` ) ; console . log ( request , bot . getUserId ( ) ) ; if ( event . type === "m.room.member" && event . content . membership === "invite" && event . state_key === bot . getUserId ( ) ) { console . log ( "Joining the room!" ) ; try { await intent . join ( event . room_id ) ; console . log ( "Joined the room!" ) ; } catch ( ex ) { console . log ( "Err joining room:" , ex ) ; } return ; } if ( event . type === "m.room.encrypted" ) { await intent . sendText ( event . room_id , "Not encrypted!" ) ; return ; } if ( event . type !== "m.room.message" || ! event . content ) { return ; } await intent . sendText ( event . room_id , event . content . body as string ) ; } } } ) ; const splitUrl = registration . getAppServiceUrl ( ) . split ( ":" ) ; const urlPort = parseInt ( splitUrl [ splitUrl . length - 1 ] ) ; port = port || urlPort || 8000 ; bridge . run ( port ) ; log . info ( `Matrix-side listening on port ${ port }` ) ; } } ) . run ( ) ;