From 60b4b8d6ec4f31373e2bbf6c79cf23e77610de8a Mon Sep 17 00:00:00 2001 From: Timothy Willard Date: Sun, 27 Nov 2016 00:57:45 -0500 Subject: [PATCH 1/2] Update to now include a seperate config file --- lib/config.js | 4 ++++ lib/text.js | 46 +++++++++++++++++++++++----------------------- 2 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 lib/config.js diff --git a/lib/config.js b/lib/config.js new file mode 100644 index 0000000..b9a1610 --- /dev/null +++ b/lib/config.js @@ -0,0 +1,4 @@ +module.exports = { + fromAddress: 'foo@bar.com', + debugEnabled: false +}; \ No newline at end of file diff --git a/lib/text.js b/lib/text.js index 8af7eb1..70de701 100644 --- a/lib/text.js +++ b/lib/text.js @@ -1,12 +1,8 @@ var providers = require('./providers.js'), carriers = require('./carriers.js'), spawn = require('child_process').spawn, - StringDecoder = require('string_decoder').StringDecoder; - -var debugEnabled = false; - -// NOTE: Change this if you are self-hosting! -var fromAddress = 'foo@bar.com'; + StringDecoder = require('string_decoder').StringDecoder, + config = require('./config.js'); //---------------------------------------------------------------- /* @@ -14,25 +10,11 @@ var fromAddress = 'foo@bar.com'; value. */ function output() { - if (debugEnabled) { + if (config.debugEnabled) { return console.log.apply(this, arguments); } } -//---------------------------------------------------------------- -/* Enable verbosity for the text module. - - If enabled, logging functions will - print to stdout. - - Params: - enable - bool -*/ -function debug(enable) { - debugEnabled = enable; - return debugEnabled; -} - //---------------------------------------------------------------- /* Sends a text message @@ -61,7 +43,7 @@ function sendText(phone, message, carrier, region, cb) { var emails = providers_list.map(function(provider) { return provider.replace('%s', phone); }).join(','); - var args = ['-s', 'txt', '-e', 'set from=' + fromAddress, + var args = ['-s', 'txt', '-e', 'set from=' + config.fromAddress, '-e', 'set use_from=yes', '-e', 'set envelope_from=yes', '-b', emails]; var child = spawn('mutt', args); var decoder = new StringDecoder('utf8'); @@ -83,7 +65,25 @@ function sendText(phone, message, carrier, region, cb) { child.stdin.end(); } +//---------------------------------------------------------------- +/* Overrides default config + + Takes a new configuration object, which is + used to override the defaults + + Params: + obj - object of config properties to be overriden +*/ +function setConfig(obj) { + for (var prop in obj) { + if (obj.hasOwnProperty(prop)) { + config[prop] = obj[prop]; + } + } +} + module.exports = { send: sendText, // Send a text message - debug: debug // Enable or disable debug output + // debug: debug, No longer needed, see setConfig and config.js + config: setConfig // Override default config }; From 7f8c2feb51a11325302c268432a4427e87958c59 Mon Sep 17 00:00:00 2001 From: Timothy Willard Date: Fri, 2 Dec 2016 21:20:40 -0500 Subject: [PATCH 2/2] Update for suggestions made at typpo/textbelt pull request #94 --- lib/text.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/text.js b/lib/text.js index 70de701..a22334a 100644 --- a/lib/text.js +++ b/lib/text.js @@ -75,15 +75,10 @@ function sendText(phone, message, carrier, region, cb) { obj - object of config properties to be overriden */ function setConfig(obj) { - for (var prop in obj) { - if (obj.hasOwnProperty(prop)) { - config[prop] = obj[prop]; - } - } + config = Object.assign(config, obj); } module.exports = { send: sendText, // Send a text message - // debug: debug, No longer needed, see setConfig and config.js config: setConfig // Override default config };