Skip to content

Commit

Permalink
Merge pull request #591 from prey/Fix-files-changes-code-style
Browse files Browse the repository at this point in the history
changes
  • Loading branch information
SoraKenji authored Apr 22, 2022
2 parents db29314 + 412f615 commit c06cfa4
Show file tree
Hide file tree
Showing 4 changed files with 455 additions and 447 deletions.
110 changes: 56 additions & 54 deletions lib/agent/actions/alert/index.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,90 @@
/* eslint-disable linebreak-style */
// Prey Alert
// Written by Tomas Pollak

const { join } = require('path');
const common = require('../../common');

const { system } = common;
const flash = join(__dirname, process.platform, 'flash');
const preyApp = join(__dirname, '..', '..', 'utils', 'Prey.app');
const actionsApp = join(__dirname, '..', '..', 'utils', 'prey-actions.app');
// eslint-disable-next-line import/order
const Emitter = require('events').EventEmitter;

const isWin = process.platform === 'win32';
const isMac = process.platform === 'darwin';

let child;
let emitter;
let app;
let binary;
const userInputStr = 'User input: ';

// eslint-disable-next-line consistent-return
exports.start = (id, opts, cb) => {
let message = opts.message || opts.alert_message;
const { title } = opts;
const level = opts.level || 'info';
let reply = opts.reply || opts.entry || opts.response;

if (!message || message.toString().trim() === '') return cb(new Error('Message required'));
var join = require('path').join,
common = require('./../../common'),
system = common.system,
flash = join(__dirname, process.platform, 'flash'),
prey_app = join(__dirname, '..', '..', 'utils', 'Prey.app'),
actions_app = join(__dirname, '..', '..', 'utils', 'prey-actions.app'),
Emitter = require('events').EventEmitter,
is_win = process.platform == 'win32',
is_mac = process.platform == 'darwin';

var child,
emitter,
app,
binary,
user_input_str = 'User input: ';

exports.start = function(id, opts, cb) {
var opts = opts || {},
message = opts.message || opts.alert_message,
title = opts.title,
level = opts.level || 'info',
reply = opts.reply || opts.entry || opts.response;

if (!message || message.toString().trim() == '')
return cb(new Error('Message required'));

// remove newlines so the message can be completely displayed
message = message.toString().replace(/(\r\n|\n|\r)/gm, ' ');
message = message.toString().replace(/(\r\n|\n|\r)/gm," ");

let returned = 0;
let bin = flash;
let args = ['-l', level];
var reply,
returned = 0,
bin = flash,
args = ['-l', level];

if (reply) args = args.concat(['-e', 'Type here:']);
if (reply)
args = args.concat(['-e', 'Type here:']);

if (title && title.toString().trim() !== '') args = args.concat(['-t', title]);
if (title && title.toString().trim() != '')
args = args.concat(['-t', title]);

if (isWin) {
if (is_win) {
args.push('-m', message); // in windows, the bin expects a -m message argument
bin += '.exe';
} else if (isMac) {
} else if (is_mac) {
args.push(message);
bin += '.py';
} else {
args.push(message);
bin += ((system.python_version && system.python_version >= '3.0.0') ? '3.py' : '.py');
bin += ((system.python_version && system.python_version >= "3.0.0") ? '3.py' : '.py');
}

if (isMac && common.os_release >= '10.14') {
app = preyApp;
if (is_mac && common.os_release >= '10.14') {
app = prey_app;
binary = 'Prey';
if (common.os_release >= '11.0') {
app = actionsApp;
app = actions_app;
binary = 'prey-actions';
}

bin = join(app, 'Contents', 'MacOS', binary);
bin = join(app, 'Contents', 'MacOS', binary);
args = ['-alert', message];
}

const done = (err) => {
// eslint-disable-next-line no-plusplus
function done(err) {
if (returned++) return;

if (emitter) emitter.emit('end', id, err, reply);
if (emitter)
emitter.emit('end', id, err, reply);

emitter = null;
};
}

// eslint-disable-next-line consistent-return
system.spawn_as_logged_user(bin, args, (err, alert) => {
system.spawn_as_logged_user(bin, args, function(err, alert) {
if (err) return done(err);

alert.stdout.on('data', (chunk) => {
if (chunk.toString().match(userInputStr)) reply = chunk.toString().replace(userInputStr, '').trim();
alert.stdout.on('data', function(chunk) {
if (chunk.toString().match(user_input_str)) {
reply = chunk.toString().replace(user_input_str, '').trim();
}
});

alert.on('error', done);

alert.once('exit', () => {
alert.once('exit', function() {
child = null;
done();
});
Expand All @@ -92,13 +93,14 @@ exports.start = (id, opts, cb) => {
emitter = new Emitter();
cb(null, emitter);
});
};

exports.stop = () => {
}

exports.stop = function() {
// if child is killed, the 'exit' event is triggered
// and it will fire the emitter's end' event, marking
// the action as stopped.
if (child && !child.exitCode) {
child.kill();
}
};
}
Loading

0 comments on commit c06cfa4

Please sign in to comment.