@@ -29,20 +29,24 @@ const execAsync = promisify(exec);
2929
3030// Determines local user (based on Discord UID)
3131async function localUser ( user : string ) : Promise < string > {
32- // Handle root user (for admos root <command>)
33- if ( user === ROOT_UID ) {
34- return 'root' ;
35- }
32+ try {
33+ // Handle root user (for admos root <command>)
34+ if ( user === ROOT_UID ) {
35+ return 'root' ;
36+ }
3637
37- const dbContent : string = await readFile ( Config . databasePath , 'utf-8' ) ;
38- const dbParsed = JSON . parse ( dbContent ) as {
39- users : Record < string , string > ;
40- } ;
38+ const dbContent : string = await readFile ( Config . databasePath , 'utf-8' ) ;
39+ const dbParsed = JSON . parse ( dbContent ) as {
40+ users : Record < string , string > ;
41+ } ;
4142
42- // Cannot be null, since only an allowedUser can send commands
43- const serverUser : string = dbParsed . users [ user ] ;
43+ // Cannot be null, since only an allowedUser can send commands
44+ const serverUser : string = dbParsed . users [ user ] ;
4445
45- return shellEscape ( [ serverUser ] ) ;
46+ return shellEscape ( [ serverUser ] ) ;
47+ } catch {
48+ return '.' ; // Silently fail (cmd exec will return an error), but encountering such an error is basically impossible
49+ }
4650}
4751
4852// Axios response creation
@@ -98,21 +102,21 @@ async function fileWrite(url: string, path: string, payload: ICommandQueueItem):
98102 const isDirCmd = shellEscape ( [ `[ -d ${ escapedPath } ] && echo 1 || echo 0` ] ) ;
99103 let { stdout } = await execAsync ( `sudo /usr/local/bin/cmdex ${ serverUser } ${ isDirCmd } ` ) ;
100104 if ( stdout . trim ( ) === '1' ) {
101- return COMMON . DIR_ERR ;
105+ return COMMON . EB_DIR_ERR ;
102106 }
103107
104108 // Test if the user has permission to write to the file (while creating the neccessary directories)
105109 const testPermissionCmd = shellEscape ( [ `mkdir -p "$(dirname ${ escapedPath } )" && echo > ${ escapedPath } && rm -f ${ escapedPath } ` ] ) ;
106110 ( { stdout } = await execAsync ( `sudo /usr/local/bin/cmdex ${ serverUser } ${ testPermissionCmd } ` ) ) ;
107111 if ( stdout . trim ( ) !== '' ) {
108- return COMMON . PERM_ERR ;
112+ return COMMON . EB_PERM_ERR ;
109113 }
110114
111115 // Download the file from Discord CDN
112116 const curlCmd = shellEscape ( [ `curl -fsSL ${ safeUrl } -o ${ escapedPath } ` ] ) ;
113117 ( { stdout } = await execAsync ( `sudo /usr/local/bin/cmdex ${ serverUser } ${ curlCmd } ` ) ) ;
114118 if ( stdout . trim ( ) !== '' ) {
115- return COMMON . DOWNLOAD_ERR ;
119+ return COMMON . EB_DOWNLOAD_ERR ;
116120 }
117121
118122 // Set the file permissions to 660
@@ -123,7 +127,7 @@ async function fileWrite(url: string, path: string, payload: ICommandQueueItem):
123127 const permCmd = shellEscape ( [ `cd ${ userHomeDir } && chown ${ serverUser } :${ serverUser } ${ escapedPath } && chmod 644 ${ escapedPath } ` ] ) ;
124128 ( { stdout } = await execAsync ( `sudo /usr/local/bin/cmdex root ${ permCmd } ` ) ) ;
125129 if ( stdout . trim ( ) !== '' ) {
126- return COMMON . SET_PERM_ERR ;
130+ return COMMON . EB_SET_PERM_ERR ;
127131 }
128132
129133 return WRITE_OP_SUCCESS ;
0 commit comments