-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
111 lines (98 loc) · 2.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var readline = require('readline');
var parseBool = require('parseboolean');
var SlackBot = require('slackbots');
var argv = require('yargs').argv;
var channel = 'general';
var botname = 'Bot';
var icon = ':computer:';
var silent = false;
var muted = false;
var outputBuffer = [];
// ARGV Channel (-f)
if (argv.f && argv.f != '') {
channel = argv.f
} else {
channel = process.env.SLACKCAT_CHANNEL || channel;
}
channel = channel.replace('#','');
// ARGV Bot Name (-n)
if (argv.n && argv.n != '') {
botname = argv.n;
} else {
botname = process.env.SLACKCAT_USERNAME || botname;
}
// ARGV Bot Emoticon Icon (-i)
if (argv.i && argv.i != '') {
icon = argv.i;
} else {
icon = process.env.SLACKCAT_ICON || icon;
}
// ARGV Be Silent *Don't console.log input to the output buffer* (-s)
if (argv.s && argv.s != '') {
silent = parseBool(argv.s);
} else {
silent = parseBool(process.env.SLACKCAT_SILENT) || silent;
}
// ARGV Be Muted *Don't actually output slack* (-m)
if (argv.m && argv.m != '') {
muted = parseBool(argv.m);
} else {
muted = parseBool(process.env.SLACKCAT_MUTED) || muted;
}
// Slack Params
var params = {
icon_emoji: icon
};
// Missing Token?
if (!process.env.SLACKCAT_API_TOKEN || process.env.SLACKCAT_API_TOKEN == '') {
console.log('Error: Please set your SLACKCAT_API_TOKEN environment variable. See: https://my.slack.com/services/new/bot');
process.exit(1);
}
// Create A Bot
var bot = new SlackBot({
token: process.env.SLACKCAT_API_TOKEN, // Token from https://my.slack.com/services/new/bot
name: botname
});
bot.on('start', function() {
// Object Push Prototype
Object.defineProperty(outputBuffer, "push", {
enumerable: false, // hide from for...in
configurable: false, // prevent further meddling...
writable: false, // see above ^
value: function () {
for (var i = 0, n = this.length, l = arguments.length; i < l; i++, n++) {
if (!silent) {
console.log(arguments[i]);
}
if (!muted) {
bot.postMessageToChannel(channel, arguments[i], params);
}
outputBuffer.pop();
}
return n;
}
});
try {
// Hook STDIN
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
// Line Out
rl.on('line', function(line) {
outputBuffer.push(line);
});
// Watch For STDIO close and wait 10 seconds before closing
rl.on('close', function() {
setTimeout(function() {
process.exit(1);
}, 10000);
});
} catch (err) {
console.log('SlackCat Readline Error:', err);
setTimeout(function() {
process.exit(1);
}, 10000);
}
});