Skip to content

Commit

Permalink
Add check-login
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroCB committed Jul 20, 2020
1 parent 1c7d241 commit 3fcab33
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
32 changes: 32 additions & 0 deletions src/check-login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const login = require("facebook-chat-api");
const chalk = require("chalk");

const tools = require("./tools");

module.exports = (fail, succeed) => {
try {
const config = tools.loadConfig();
login({ "appState": config.appState }, tools.silentOpt, (err, api) => {
if (err) {
if (config.email && config.password) {
login({ "email": config.email, "password": config.password },
tools.silentOpt, (err, api) => {
if (err) {
fail()
} else {
// Re-login succeeded; need to update stored session
tools.updateLogin(config, api);
succeed();
}
});
} else {
fail();
}
} else {
succeed();
}
});
} catch {
fail()
}
}
42 changes: 14 additions & 28 deletions src/mnotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Locals
const init = require("./init");
const tools = require("./tools")
const checkLogin = require("./check-login");

// Stdlib
const fs = require("fs");
Expand All @@ -17,7 +18,8 @@ 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": "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." }
];

const helpSections = [
Expand All @@ -33,31 +35,22 @@ const helpSections = [
]

function start() {
getStdin(input => {
tools.getStdin(input => {
try {
loginWithConfig(load_config(), input);
loginWithConfig(tools.loadConfig(), input);
} catch (e) {
console.log(`${chalk.black.bgYellowBright.bold("WARN")} mnotify must \
be configured before you can use it. Running ${chalk.blue("mnotify --init")}...\n`);

init.init(success => {
if (!success) { return process.exit(1); }

loginWithConfig(load_config(), input);
loginWithConfig(tools.loadConfig(), input);
});
}
});
}

function load_config() {
if (tools.configExists()) {
const config = fs.readFileSync(tools.getConfigPath());
return JSON.parse(config);
} else {
throw Error("Config not found");
}
}

function loginWithConfig(config, msg) {
login({ "appState": config.appState }, tools.silentOpt, (err, api) => {
if (err) {
Expand All @@ -67,7 +60,8 @@ function loginWithConfig(config, msg) {
if (err) {
failedLogin(config, msg, true)
} else {
updateLogin(config, api)
// Re-login succeeded; need to update stored session
tools.updateLogin(config, api)
notify(config, api, msg);
}
});
Expand Down Expand Up @@ -109,20 +103,6 @@ Try again by running")} ${chalk.blue("mnotify --init")}${chalk.red(".")}`);
});
}

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

function getStdin(cb) {
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
cb(chunk.toString());
}
});
}

function notify(config, api, msg) {
api.sendMessage(`New message from mnotify: ${msg}`, config.recipient);
}
Expand All @@ -140,6 +120,12 @@ if (require.main === module) {
init.init(() => { });
} 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(".")}`);
}, () => {
console.log(chalk.green("Your current login is still valid."));
});
} else {
start();
}
Expand Down
23 changes: 23 additions & 0 deletions src/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,35 @@ exports.configExists = () => {
return fs.existsSync(exports.getConfigDir());
}

exports.loadConfig = () => {
if (exports.configExists()) {
const config = fs.readFileSync(exports.getConfigPath());
return JSON.parse(config);
} else {
throw Error("Config not found");
}
}

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

exports.printHeader = () => {
console.log(chalk.blue("======="));
console.log(chalk.bgBlue("mnotify"));
console.log(chalk.blue("======="));
}

exports.getStdin = cb => {
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
cb(chunk.toString());
}
});
}

// Stored options for common invocations

exports.silentOpt = {
Expand Down

0 comments on commit 3fcab33

Please sign in to comment.