-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhipchat-exec.js
99 lines (84 loc) · 2.77 KB
/
hipchat-exec.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
// Generated by CoffeeScript 1.6.2
var HipchatExec, Hipchatter, exec, http, path;
http = require('http');
path = require('path');
Hipchatter = require('hipchatter');
exec = require('child_process').exec;
HipchatExec = (function() {
function HipchatExec(config) {
var cmd, script, _ref;
this.commands = {};
_ref = config.commands;
for (cmd in _ref) {
script = _ref[cmd];
this.commands[cmd] = script;
}
this.token = config.token;
this.notify_token = config.notify_token;
this.frequency = config.frequency;
this.room = config.room;
this.hipchatter = new Hipchatter(this.token);
this.last = new Date;
}
HipchatExec.prototype.run = function() {
var self;
self = this;
return setInterval((function() {
return self.pollHipchat();
}), this.frequency);
};
HipchatExec.prototype.pollHipchat = function() {
var self;
self = this;
return this.hipchatter.history(this.room, function(err, history) {
var cmd, date, item, message, script, _i, _len, _ref, _results;
if (err != null) {
return console.error('Hipchat error ' + err);
} else {
_ref = history.items;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
date = new Date(item.date);
if (date > self.last) {
self.last = date;
_results.push((function() {
var _ref1, _results1;
_ref1 = self.commands;
_results1 = [];
for (cmd in _ref1) {
script = _ref1[cmd];
if (item.message === cmd) {
console.log('Command: ' + cmd);
message = 'Received: <strong>' + cmd + '</strong><br />Running: <code>' + script + '</code>';
self.hipchatter.notify(self.room, message, self.notify_token, function(err, response) {
if (err != null) {
return console.error('Message error', err);
} else {
return console.log('Message successful: ' + message);
}
});
_results1.push(exec(script, function(err, stdout, stderr) {
if (err != null) {
console.error('Command error ' + err);
}
console.log(stdout);
return console.error(stderr);
}));
} else {
_results1.push(void 0);
}
}
return _results1;
})());
} else {
_results.push(void 0);
}
}
return _results;
}
});
};
return HipchatExec;
})();
module.exports = HipchatExec;