2016-10-09 19:14:02 +00:00
"use strict" ;
2016-09-05 12:37:27 +00:00
const pkg = require ( "../package.json" ) ;
2016-06-08 09:26:24 +00:00
var _ = require ( "lodash" ) ;
2014-10-04 23:22:23 +00:00
var path = require ( "path" ) ;
2016-04-27 08:13:25 +00:00
var os = require ( "os" ) ;
2016-07-04 20:15:30 +00:00
var fs = require ( "fs" ) ;
2016-11-19 18:32:47 +00:00
var net = require ( "net" ) ;
2017-03-23 07:47:51 +00:00
var bcrypt = require ( "bcryptjs" ) ;
2017-06-08 18:40:17 +00:00
const colors = require ( "colors/safe" ) ;
2014-10-04 23:22:23 +00:00
2017-12-07 03:54:54 +00:00
let homePath ;
let configPath ;
let usersPath ;
let storagePath ;
let packagesPath ;
const Helper = {
2016-06-08 09:26:24 +00:00
config : null ,
2017-12-07 03:54:54 +00:00
expandHome ,
getHomePath ,
getPackagesPath ,
getPackageModulePath ,
getStoragePath ,
getConfigPath ,
getUsersPath ,
getUserConfigPath ,
getUserLogsPath ,
setHome ,
getVersion ,
getGitCommit ,
ip2hex ,
2016-10-21 19:00:43 +00:00
password : {
hash : passwordHash ,
compare : passwordCompare ,
requiresUpdate : passwordRequiresUpdate ,
} ,
2014-09-13 12:23:17 +00:00
} ;
2016-05-09 16:19:16 +00:00
module . exports = Helper ;
2016-06-08 09:26:24 +00:00
Helper . config = require ( path . resolve ( path . join (
_ _dirname ,
".." ,
"defaults" ,
"config.js"
) ) ) ;
2016-09-05 12:37:27 +00:00
function getVersion ( ) {
const gitCommit = getGitCommit ( ) ;
return gitCommit ? ` source ( ${ gitCommit } ) ` : ` v ${ pkg . version } ` ;
}
let _gitCommit ;
function getGitCommit ( ) {
if ( _gitCommit !== undefined ) {
return _gitCommit ;
}
try {
_gitCommit = require ( "child_process" )
. execSync ( "git rev-parse --short HEAD 2> /dev/null" ) // Returns hash of current commit
. toString ( )
. trim ( ) ;
return _gitCommit ;
} catch ( e ) {
// Not a git repository or git is not installed
_gitCommit = null ;
return null ;
}
}
2017-12-07 03:54:54 +00:00
function setHome ( newPath ) {
homePath = expandHome ( newPath ) ;
configPath = path . join ( homePath , "config.js" ) ;
usersPath = path . join ( homePath , "users" ) ;
storagePath = path . join ( homePath , "storage" ) ;
packagesPath = path . join ( homePath , "packages" , "node_modules" ) ;
2016-06-08 09:26:24 +00:00
// Reload config from new home location
2017-12-07 03:54:54 +00:00
if ( fs . existsSync ( configPath ) ) {
var userConfig = require ( configPath ) ;
2017-07-06 12:00:43 +00:00
this . config = _ . merge ( this . config , userConfig ) ;
2016-07-04 20:15:30 +00:00
}
2016-12-10 08:53:06 +00:00
2017-06-08 18:40:17 +00:00
if ( ! this . config . displayNetwork && ! this . config . lockNetwork ) {
this . config . lockNetwork = true ;
log . warn ( ` ${ colors . bold ( "displayNetwork" ) } and ${ colors . bold ( "lockNetwork" ) } are false, setting ${ colors . bold ( "lockNetwork" ) } to true. ` ) ;
}
2017-11-12 18:24:21 +00:00
// Load theme color from manifest.json
const manifest = require ( "../public/manifest.json" ) ;
this . config . themeColor = manifest . theme _color ;
2016-12-10 08:53:06 +00:00
// TODO: Remove in future release
if ( this . config . debug === true ) {
log . warn ( "debug option is now an object, see defaults file for more information." ) ;
this . config . debug = { ircFramework : true } ;
}
2017-09-19 09:08:08 +00:00
// TODO: Remove in future release
// Backwards compatibility for old way of specifying themes in settings
if ( this . config . theme . includes ( ".css" ) ) {
2017-12-07 03:54:54 +00:00
log . warn ( ` Referring to CSS files in the ${ colors . green ( "theme" ) } setting of ${ colors . green ( configPath ) } is ${ colors . bold ( "deprecated" ) } and will be removed in a future version. ` ) ;
2017-09-19 09:08:08 +00:00
} else {
this . config . theme = ` themes/ ${ this . config . theme } .css ` ;
}
2016-05-09 16:19:16 +00:00
}
2017-12-07 03:54:54 +00:00
function getHomePath ( ) {
return homePath ;
}
function getConfigPath ( ) {
return configPath ;
}
function getUsersPath ( ) {
return usersPath ;
}
2016-05-08 06:21:31 +00:00
function getUserConfigPath ( name ) {
2017-12-07 03:54:54 +00:00
return path . join ( usersPath , name + ".json" ) ;
2016-05-08 06:21:31 +00:00
}
2016-05-15 21:13:51 +00:00
function getUserLogsPath ( name , network ) {
2017-12-07 03:54:54 +00:00
return path . join ( homePath , "logs" , name , network ) ;
2016-05-15 21:13:51 +00:00
}
2017-07-06 15:33:09 +00:00
function getStoragePath ( ) {
2017-12-07 03:54:54 +00:00
return storagePath ;
2017-07-06 15:33:09 +00:00
}
2017-06-22 21:09:55 +00:00
function getPackagesPath ( ) {
2017-12-07 03:54:54 +00:00
return packagesPath ;
2017-06-22 21:09:55 +00:00
}
function getPackageModulePath ( packageName ) {
return path . join ( Helper . getPackagesPath ( ) , packageName ) ;
}
2016-11-19 18:32:47 +00:00
function ip2hex ( address ) {
// no ipv6 support
if ( ! net . isIPv4 ( address ) ) {
return "00000000" ;
}
return address . split ( "." ) . map ( function ( octet ) {
var hex = parseInt ( octet , 10 ) . toString ( 16 ) ;
if ( hex . length === 1 ) {
hex = "0" + hex ;
}
return hex ;
} ) . join ( "" ) ;
}
2016-05-08 06:21:31 +00:00
function expandHome ( shortenedPath ) {
2017-04-17 19:48:28 +00:00
if ( ! shortenedPath ) {
return "" ;
}
2016-04-27 08:13:25 +00:00
2017-08-16 07:06:20 +00:00
const home = os . homedir ( ) . replace ( "$" , "$$$$" ) ;
2016-05-08 06:21:31 +00:00
return path . resolve ( shortenedPath . replace ( /^~($|\/|\\)/ , home + "$1" ) ) ;
2016-04-27 08:13:25 +00:00
}
2016-10-21 19:00:43 +00:00
function passwordRequiresUpdate ( password ) {
return bcrypt . getRounds ( password ) !== 11 ;
}
function passwordHash ( password ) {
return bcrypt . hashSync ( password , bcrypt . genSaltSync ( 11 ) ) ;
}
function passwordCompare ( password , expected ) {
2017-03-23 07:47:51 +00:00
return bcrypt . compare ( password , expected ) ;
2016-10-21 19:00:43 +00:00
}