11'use strict' ;
22
3+ const fs = require ( 'fs' ) ;
34const path = require ( 'path' ) ;
45const { getConfig } = require ( './config.js' ) ;
56const chatStore = require ( './chatStore.js' ) ;
@@ -20,6 +21,25 @@ function normalizeHomeserverUrl(url) {
2021 return u ;
2122}
2223
24+ function getPersistentMatrixDeviceId ( ) {
25+ const dataDir = path . join ( __dirname , '..' , 'data' ) ;
26+ const file = path . join ( dataDir , 'matrix-device-id.txt' ) ;
27+ try {
28+ if ( ! fs . existsSync ( dataDir ) ) fs . mkdirSync ( dataDir , { recursive : true } ) ;
29+ if ( fs . existsSync ( file ) ) {
30+ const existing = fs . readFileSync ( file , 'utf8' ) . trim ( ) ;
31+ if ( / ^ [ A - Z 0 - 9 ] { 6 , 16 } $ / . test ( existing ) ) return existing ;
32+ }
33+ const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ;
34+ let id = '' ;
35+ for ( let i = 0 ; i < 10 ; i += 1 ) id += alphabet [ Math . floor ( Math . random ( ) * alphabet . length ) ] ;
36+ fs . writeFileSync ( file , id , 'utf8' ) ;
37+ return id ;
38+ } catch ( _ ) {
39+ return '' ;
40+ }
41+ }
42+
2343function matrixUsernameForSender ( sender ) {
2444 if ( ! sender || typeof sender !== 'string' ) return 'matrix_unknown' ;
2545 const local = sender . replace ( / ^ @ / , '' ) . replace ( / : / g, '_' ) . replace ( / [ ^ a - z A - Z 0 - 9 _ - ] / g, '_' ) ;
@@ -41,6 +61,7 @@ async function matrixLoginWithPassword(homeserverUrl, userId, password) {
4161 type : 'm.id.user' ,
4262 user : userId . trim ( )
4363 } ,
64+ device_id : getPersistentMatrixDeviceId ( ) ,
4465 password : String ( password ) ,
4566 initial_device_display_name : 'ShadowAI'
4667 } ;
0 commit comments