<commit_before>'use strict';

/**
 * @class config
 * @classdesc
 * Configuration object, that is passed to Hunt constructor
 *
 * @property {string} hostUrl - url to host of current application,
 * used in oAuth authentication strategies and for building redirects.
 * Populated from environment variable of HOST_URL.
 * For example - http://example.org/
 *
 * @property {string} env - application environment, if not set, populated
 * from enviromental value of NODE_ENV. Default is `development`, the values
 * of `staging` and `production` can be used
 *
 * @property {number} port - port for webserver process(es) to bind to.
 * Note: on *nix machines ports with number bellow 1000 are only bindable
 * by applicaiton ran as root user.
 *
 * @property {(string|null)} address - what address to bind to.
 * Default is '0.0.0.0' - all IPv4 addresses.
 * The address is populated from environment address of HUNTJS_ADDR.
 * HuntJS supports both IPv4 and IPv6 addresses as described
 * {@link http://code.danyork.com/2011/01/21/how-to-use-node-js-with-ipv6/ here}.
 *
 * @property {boolean} enableMongoose - enable mongo support via mongoose orm, default is true - enabled
 * @property {string} mongoUrl - connection string for mongoose ORM,
 * default is 'mongodb://localhost/hunt_dev'.
 * It can be automatically populated from enviromental values of `MONGO_URL`,
 * `MONGOSOUP_URL`,`MONGOHQ_URL`,`MONGOLAB_URL`.
 * Connection string has this {@link http://docs.mongodb.org/manual/reference/connection-string/ syntax }
 *
 * @property {boolean} enableMongooseUsers - enable mongodb based user object, build by pattern of Active Record.
 * Default is true - enabled
 *
 * @property {string} redisUrl - connection string for redis, default is
 * 'redis://localhost:6379', can be populated from enviromental variables
 * of 'REDISTOGO_URL', 'OPENREDIS_URL', 'REDISCLOUD_URL',
 *  'REDISGREEN_URL', 'REDIS_URL'
 *
 * @property {string} sequelizeUrl - URI for relational database connection. Is used in {@link https://www.npmjs.org/package/hunt-sequilize | Hunt-sequilize} plugin
 *
 * @property {string} public - directory for assets - css, images,
 * client side javascripts, that are served by {@link http://www.senchalabs.org/connect/static.html | connect static middleware}
 * for example, 'public': __dirname+'/public'
 *
 * @property {string} views - directory for {@link http://expressjs.com/api.html#app-settings | templates},
 * used by template engine for rendering static HTML pages on server side.
 * See this {@link https://huntjs.herokuapp.com/documentation/tutorial-templating.html | tutorial} for details
 *
 * @property {string} emailConfig - configuration string for {@link https://npmjs.org/package/nodemailer/ | email delivery system },
 * used for notify users.
 * When left blank, the direct transport is used - it is quite slow, and the emails
 * are usually marked as spam.
 * Also you can set up SMTP options directly like mentioned here {@link https://github.com/andris9/nodemailer#setting-up-smtp }
 * Examples:
 * 'nodemailer://<EMAIL>:somepassword@gmail',
 * 'nodemailer://<EMAIL>:7tu8z3zsaa421@Mailgun',
 *
 * @property {Boolean} usersApi - enable REST-api for editing user profiles
 *
 * @property {Boolean} dialog - enable dialog REST-api for private messages
 * @property {Boolean} huntKey - enable authorization by huntKey as GET query, or POST/PUT/DELETE body parameter. Default is false - disabled
 * @property {Boolean} huntKeyHeader - enable authorization by huntKey as custom header, default is false - disabled
 *
 * @property {Boolean} disableCsrf - disable csrf protection, default is false - CSRF protection is enabled. See {@link https://www.npmjs.org/package/csurf } for details. Also this works from the box with AngularJS $resourse and $http
 *
 * @property {String} adminEmail - email address of server administrator. If present, every error will be reported to this email with plenty of additional information and error stack.
 *
 * @property {number} maxWorkers - limit the number of webserver/background processes cluster spawns. Default - 1 per CPU core.
 *
 * @property {passport} passport - {@link http://passportjs.org | passport.js}
 * configuration object, used for setting the methods users authorize,
 *
 * @property {boolean} uploadFiles -  allow upload of files by HTTP-POST,
 * {@link http://expressjs.com/api.html#req.files},
 * default is false, disabled, and for a good cause {@link https://groups.google.com/forum/#!topic/nodejs/6KOlfk5cpcM}
 *
 * @property {object} io - configuration options for {@link http://socket.io/}. Default is `{'enabled':false}`, make `{'enabled':true} to enable. We can use this parameters for setup - {@link https://github.com/Automattic/engine.io#methods-1}. If we pass `true`, the sane default values will be set.
 * @property {string} favicon - path to favicon
 * @property {string} secret - string to preseed session hashes and do other security related things
 * @property {object} telnetServer - configuration options for RAI telnet server {@link https://github.com/andris9/rai#starting-a-server}
 */

require('colors');
var os = require('os'),
  crypto = require('crypto'),
  url = require('url');

function md5(str) {
  return crypto.createHash('md5').update(str).digest('hex').toString();
}

module.exports = exports = function (configToUse) {
  var
    numCPUs,
    hostname,
    parsedRedis,
    parsedMongo,
    parsedHostUrl,
    config = configToUse || {};
  config.env = config.env || process.env.NODE_ENV || 'development';
  config.port = config.port || parseInt(process.env.PORT, 10) || 3000;
  config.secret = config.secret || process.env.SECRET || null;
  config.adminEmail = config.adminEmail || process.env.ADMIN_EMAIL || null;
  config.address = config.address || process.env.HUNTJS_ADDR || '0.0.0.0';

  if (!config.adminEmail) {
    console.error(('ConfigManager: config.adminEmail is missing! Set the config parameter of adminEmail or environment value of ADMIN_EMAIL to be able to recieve email reports with errors!').yellow);
  }
  if (!config.secret) {
    console.error(('ConfigManager: config.secret is missing! Generating the secret on the fly...').yellow);
    config.secret = (md5(JSON.stringify(os)));
  }

  if (config.hostUrl) {
    console.log(('ConfigManager: hostUrl is set to ' + config.hostUrl).green);
  }

  if (!config.hostUrl) {
    if (process.env.HOST_URL) {
      config.hostUrl = process.env.HOST_URL;
    } else {
      hostname = os.hostname();
      if (config.env === 'development') {
        hostname = 'localhost';
      }
      config.hostUrl = 'http://' + hostname + ':' + config.port + '/';
    }
    console.log(('ConfigManager: assuming hostUrl is ' + config.hostUrl).yellow);
  }

//trying to setup mail dispatcher
  if (!config.emailConfig) {
    config.emailConfig = process.env.emailConfig;<commit_msg>more hard default secret<commit_after>'use strict';

/**
 * @class config
 * @classdesc
 * Configuration object, that is passed to Hunt constructor
 *
 * @property {string} hostUrl - url to host of current application,
 * used in oAuth authentication strategies and for building redirects.
 * Populated from environment variable of HOST_URL.
 * For example - http://example.org/
 *
 * @property {string} env - application environment, if not set, populated
 * from enviromental value of NODE_ENV. Default is `development`, the values
 * of `staging` and `production` can be used
 *
 * @property {number} port - port for webserver process(es) to bind to.
 * Note: on *nix machines ports with number bellow 1000 are only bindable
 * by applicaiton ran as root user.
 *
 * @property {(string|null)} address - what address to bind to.
 * Default is '0.0.0.0' - all IPv4 addresses.
 * The address is populated from environment address of HUNTJS_ADDR.
 * HuntJS supports both IPv4 and IPv6 addresses as described
 * {@link http://code.danyork.com/2011/01/21/how-to-use-node-js-with-ipv6/ here}.
 *
 * @property {boolean} enableMongoose - enable mongo support via mongoose orm, default is true - enabled
 * @property {string} mongoUrl - connection string for mongoose ORM,
 * default is 'mongodb://localhost/hunt_dev'.
 * It can be automatically populated from enviromental values of `MONGO_URL`,
 * `MONGOSOUP_URL`,`MONGOHQ_URL`,`MONGOLAB_URL`.
 * Connection string has this {@link http://docs.mongodb.org/manual/reference/connection-string/ syntax }
 *
 * @property {boolean} enableMongooseUsers - enable mongodb based user object, build by pattern of Active Record.
 * Default is true - enabled
 *
 * @property {string} redisUrl - connection string for redis, default is
 * 'redis://localhost:6379', can be populated from enviromental variables
 * of 'REDISTOGO_URL', 'OPENREDIS_URL', 'REDISCLOUD_URL',
 *  'REDISGREEN_URL', 'REDIS_URL'
 *
 * @property {string} sequelizeUrl - URI for relational database connection. Is used in {@link https://www.npmjs.org/package/hunt-sequilize | Hunt-sequilize} plugin
 *
 * @property {string} public - directory for assets - css, images,
 * client side javascripts, that are served by {@link http://www.senchalabs.org/connect/static.html | connect static middleware}
 * for example, 'public': __dirname+'/public'
 *
 * @property {string} views - directory for {@link http://expressjs.com/api.html#app-settings | templates},
 * used by template engine for rendering static HTML pages on server side.
 * See this {@link https://huntjs.herokuapp.com/documentation/tutorial-templating.html | tutorial} for details
 *
 * @property {string} emailConfig - configuration string for {@link https://npmjs.org/package/nodemailer/ | email delivery system },
 * used for notify users.
 * When left blank, the direct transport is used - it is quite slow, and the emails
 * are usually marked as spam.
 * Also you can set up SMTP options directly like mentioned here {@link https://github.com/andris9/nodemailer#setting-up-smtp }
 * Examples:
 * 'nodemailer://<EMAIL>:somepassword@gmail',
 * 'nodemailer://<EMAIL>:7tu8z3zsaa421@Mailgun',
 *
 * @property {Boolean} usersApi - enable REST-api for editing user profiles
 *
 * @property {Boolean} dialog - enable dialog REST-api for private messages
 * @property {Boolean} huntKey - enable authorization by huntKey as GET query, or POST/PUT/DELETE body parameter. Default is false - disabled
 * @property {Boolean} huntKeyHeader - enable authorization by huntKey as custom header, default is false - disabled
 *
 * @property {Boolean} disableCsrf - disable csrf protection, default is false - CSRF protection is enabled. See {@link https://www.npmjs.org/package/csurf } for details. Also this works from the box with AngularJS $resourse and $http
 *
 * @property {String} adminEmail - email address of server administrator. If present, every error will be reported to this email with plenty of additional information and error stack.
 *
 * @property {number} maxWorkers - limit the number of webserver/background processes cluster spawns. Default - 1 per CPU core.
 *
 * @property {passport} passport - {@link http://passportjs.org | passport.js}
 * configuration object, used for setting the methods users authorize,
 *
 * @property {boolean} uploadFiles -  allow upload of files by HTTP-POST,
 * {@link http://expressjs.com/api.html#req.files},
 * default is false, disabled, and for a good cause {@link https://groups.google.com/forum/#!topic/nodejs/6KOlfk5cpcM}
 *
 * @property {object} io - configuration options for {@link http://socket.io/}. Default is `{'enabled':false}`, make `{'enabled':true} to enable. We can use this parameters for setup - {@link https://github.com/Automattic/engine.io#methods-1}. If we pass `true`, the sane default values will be set.
 * @property {string} favicon - path to favicon
 * @property {string} secret - string to preseed session hashes and do other security related things
 * @property {object} telnetServer - configuration options for RAI telnet server {@link https://github.com/andris9/rai#starting-a-server}
 */

require('colors');
var
  os = require('os'),
  rack = require('./rack.js'),
  url = require('url');


module.exports = exports = function (configToUse) {
  var
    numCPUs,
    hostname,
    parsedRedis,
    parsedMongo,
    parsedHostUrl,
    config = configToUse || {};
  config.env = config.env || process.env.NODE_ENV || 'development';
  config.port = config.port || parseInt(process.env.PORT, 10) || 3000;
  config.secret = config.secret || process.env.SECRET || null;
  config.adminEmail = config.adminEmail || process.env.ADMIN_EMAIL || null;
  config.address = config.address || process.env.HUNTJS_ADDR || '0.0.0.0';

  if (!config.adminEmail) {
    console.error(('ConfigManager: config.adminEmail is missing! Set the config parameter of adminEmail or environment value of ADMIN_EMAIL to be able to recieve email reports with errors!').yellow);
  }
  if (!config.secret) {
    console.error(('ConfigManager: config.secret is missing! Generating the secret on the fly...').yellow);
    config.secret = rack();
  }

  if (config.hostUrl) {
    console.log(('ConfigManager: hostUrl is set to ' + config.hostUrl).green);
  }

  if (!config.hostUrl) {
    if (process.env.HOST_URL) {
      config.hostUrl = process.env.HOST_URL;
    } else {
      hostname = os.hostname();
      if (config.env === 'development') {
        hostname = 'localhost';
      }
      config.hostUrl = 'http://' + hostname + ':' + config.port + '/';
    }
    console.log(('ConfigManager: assuming hostUrl is ' + config.hostUrl).yellow);
  }

//trying to setup mail dispatcher
  if (!config.emailConfig) {
    config.emailConfig = process.env.emailConfig;
{'fraction_non_alphanumeric': 0.11900741502092357, 'fraction_numerical': 0.006974524631084355, 'mean_word_length': 4.0526706231454, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 16, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5351445', 'n_tokens_mistral': 4057, 'n_tokens_neox': 3918, 'n_words': 1352}