Skip to content

Commit

Permalink
Add --update-password and refactor tools
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroCB committed Jul 20, 2020
1 parent 3fcab33 commit a0b3eeb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 38 deletions.
32 changes: 0 additions & 32 deletions src/check-login.js

This file was deleted.

13 changes: 8 additions & 5 deletions src/mnotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Locals
const init = require("./init");
const tools = require("./tools")
const checkLogin = require("./check-login");

// Stdlib
const fs = require("fs");
Expand All @@ -17,9 +16,10 @@ const usage = require("command-line-usage");

// Command-line args
const argDefs = [
{ "name": "help", "alias": "h", "type": Boolean, "description": "Display this help message." },
{ "name": "init", "type": Boolean, "description": "Initialize mnotify so that it can be used to send notifications." },
{ "name": "check-login", "alias": "c", "type": Boolean, "description": "Check whether your currently-stored login information is valid." }
{ "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." }
];

const helpSections = [
Expand Down Expand Up @@ -113,6 +113,7 @@ function printHelp() {
console.log(guide);
}

// Main entry point
if (require.main === module) {
const options = args(argDefs);

Expand All @@ -121,11 +122,13 @@ if (require.main === module) {
} else if (options.help) {
printHelp();
} else if (options["check-login"]) {
checkLogin(() => {
console.log(`${chalk.red("Unable to load config. Try running ")}${chalk.blue("mnotify --init")}${chalk.red(".")}`);
tools.checkLogin(() => {
tools.printNoConfigError();
}, () => {
console.log(chalk.green("Your current login is still valid."));
});
} else if (options["update-password"]) {
tools.updatePassword(options["update-password"]);
} else {
start();
}
Expand Down
49 changes: 48 additions & 1 deletion src/tools.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require("fs");
const chalk = require("chalk");
const login = require("facebook-chat-api");

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

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

exports.printNoConfigError = () => {
console.log(`${chalk.red("Unable to load config. Try running ")}${chalk.blue("mnotify --init")}${chalk.red(".")}`);
}

exports.updateLogin = (config, api) => {
config.appState = api.getAppState();
fs.writeFile(exports.getConfigPath(), JSON.stringify(config), () => { });
exports.saveConfig(config);
}

exports.updatePassword = pass => {
try {
const config = exports.loadConfig();
config.password = pass;
exports.saveConfig(config);
} catch {
exports.printNoConfigError();
}
}

exports.printHeader = () => {
Expand All @@ -44,6 +63,34 @@ exports.getStdin = cb => {
});
}

exports.checkLogin = (fail, succeed) => {
try {
const config = exports.loadConfig();
login({ "appState": config.appState }, exports.silentOpt, (err, _) => {
if (err) {
if (config.email && config.password) {
login({ "email": config.email, "password": config.password },
exports.silentOpt, (err, api) => {
if (err) {
fail()
} else {
// Re-login succeeded; need to update stored session
exports.updateLogin(config, api);
succeed();
}
});
} else {
fail();
}
} else {
succeed();
}
});
} catch {
fail()
}
}

// Stored options for common invocations

exports.silentOpt = {
Expand Down

0 comments on commit a0b3eeb

Please sign in to comment.