Skip to content

Commit

Permalink
Add option to log in with BotCore
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroCB committed Jul 25, 2020
1 parent 9285952 commit f97a44b
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 7 deletions.
83 changes: 80 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test": "echo test | mnotify",
"init": "mnotify --init",
"help": "mnotify --help",
"botcore": "mnotify --using-botcore",
"postinstall": "src/postinstall.js"
},
"repository": {
Expand All @@ -29,10 +30,11 @@
},
"homepage": "https://github.com/AstroCB/mnotify#readme",
"dependencies": {
"chalk": "^4.0.0",
"chalk": "^4.1.0",
"command-line-args": "^5.1.1",
"command-line-usage": "^6.1.0",
"facebook-chat-api": "^1.8.0",
"messenger-botcore": "^2.2.0",
"readline-sync": "^1.4.10"
}
}
11 changes: 10 additions & 1 deletion src/mnotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const argDefs = [
{ "name": "init", "type": Boolean, "description": "Initialize mnotify so that it can be used to send notifications." },
{ "name": "help", "alias": "h", "type": Boolean, "description": "Display this help message." },
{ "name": "check-login", "alias": "c", "type": Boolean, "description": "Check whether your currently-stored login information is valid." },
{ "name": "update-password", "alias": "p", "type": String, "description": "Update your sending account's password without re-running init." }
{ "name": "update-password", "alias": "p", "type": String, "description": "Update your sending account's password without re-running init." },
{ "name": "using-botcore", "alias": "b", "type": Boolean, "description": `Process sender account logins using BotCore ({underline https://github.com/AstroCB/BotCore}) instead of a raw username/password. You must still run ${chalk.blue("mnotify --init")} before using this command in order to configure the receiver's account.`}
];

const helpSections = [
Expand Down Expand Up @@ -146,6 +147,14 @@ if (require.main === module) {
});
} else if (options["update-password"]) {
tools.updatePassword(options["update-password"]);
} else if (options["using-botcore"]) {
tools.botcoreLogin(err => {
if (err) {
console.log(chalk.red(err));
} else {
start();
}
});
} else {
start();
}
Expand Down
42 changes: 40 additions & 2 deletions src/tools.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require("fs");
const chalk = require("chalk");
const login = require("facebook-chat-api");
const botcore = require("messenger-botcore");

// Location of config directory (respects user settings)
exports.getConfigDir = () => {
Expand All @@ -25,8 +26,8 @@ exports.loadConfig = () => {
}
}

exports.saveConfig = config => {
fs.writeFile(exports.getConfigPath(), JSON.stringify(config), () => { });
exports.saveConfig = (config, callback = () => { }) => {
fs.writeFile(exports.getConfigPath(), JSON.stringify(config), callback);
}

exports.printNoConfigError = () => {
Expand Down Expand Up @@ -91,6 +92,43 @@ exports.checkLogin = (fail, succeed) => {
}
}

exports.botcoreLogin = callback => {
const requiredVars = ["MEMCACHIER_USERNAME", "MEMCACHIER_SERVERS", "MEMCACHIER_PASSWORD"];
requiredVars.forEach(v => {
if (!process.env[v]) {
return callback(`Failed to access BotCore; make sure your required credentials [${requiredVars.join(", ")}]\
are stored as environment variables: https://github.com/AstroCB/BotCore/blob/master/DOCS.md#credentialsobj`);
}
});

botcore.login.login(process.env, (err, api) => {
if (err) {
callback(`Error with login: ${err}`);
} else {
// Close out BotCore memcache connection to avoid hanging
const mem = botcore.login.getMemCache();
if (mem) {
mem.close();
}

let config;
try {
config = this.loadConfig();
} catch {
console.log(chalk.yellow(`Error loading config; even when logging in with BotCore,\
you must still configure mnotify to set up the receiver's account.`));

// Call with no error anyway to passthrough to the default init that happens in start()
return callback(null);
}

// Update the login in the config
config.appState = api.getAppState();
this.saveConfig(config, callback);
}
});
}

// Stored options for common invocations

exports.silentOpt = {
Expand Down

0 comments on commit f97a44b

Please sign in to comment.