-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.js
executable file
·202 lines (182 loc) · 6.4 KB
/
bot.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env node
const console = require('console');
var set = require(__dirname + '/settings.js');
var Client = require('irc').Client;
var client = new Client('irc.libera.chat', 'sudobot', {
channels: [ '#sudoroom' ],
nick: 'sudobot',
userName: 'sudobot',
realName: 'sudobot',
password: set.password,
port: 7000,
debug: true,
autoConnect: false,
secure: true,
selfSigned: true,
certExpired: true,
sasl: true,
stripColors: true,
});
var minimist = require('minimist');
var split = require('split2');
var through = require('through2');
var spawn = require('child_process').spawn;
var failing = {};
var timeout = null;
var last = {};
function say (msg) {
client.say('#sudoroom', msg);
}
client.addListener('message#sudoroom', function (from, message) {
//if (/^....ATTN:.This.channel.has.moved\s+/.test(message)) {
//if (/^.fter.the.acquisition.by.Private\s+/.test(message)) {
if (/^.hristel.just.posted.this..denial..on.the.freenode\s+/.test(message)) {
// this does not work :) say('/kick ' + from);
client.send('KICK', '#sudoroom', from);
}
if (/^!say\s+/.test(message)) {
var argv = minimist(message.split(/\s+/).slice(1));
var args = [];
// amplitude
if (argv.a) {
var amp = parseInt(argv.a);
if(amp) args.push('-a', amp);
}
// Indicate capital letters with: 1=sound, 2=the word "capitals", higher values = a pitch increase (try -k20).
var k = 20;
if (argv.k) {
k = parseInt(argv.k) || k;
}
args.push('-k', k);
var voice = "en+f3";
if (argv.v) {
var m = args.v.match(/[\w\+_-]+/);
if(m)
voice = m.pop();
}
var s = 87;
if (argv.s) {
s = parseInt(args.s) || s;
}
args.push('-s', s);
if (argv.p) {
var p = parseInt(args.p);
if(p)
args.push('-p', p);
}
args.unshift('[email protected]', 'bin/mainscreenturnon; espeak ');
args.push('-w /tmp/out.wav --stdin && aplay /tmp/out.wav');
var ps = spawn('ssh', args);
ps.stdin.end(argv._.join(' '));
ps.stderr.pipe(process.stdout); ps.stdout.pipe(process.stdout)
} else if(/^!ssay\s+/.test(message)) {
var argv = minimist(message.split(/\s+/).slice(1));
var ps = spawn('aoss', ['flite', '-voice', '/opt/voices/cmu_us_awb.flitevox']);
ps.stdin.end(argv._.join(' '));
} else if(/^!fsay\s+/.test(message)) {
var argv = minimist(message.split(/\s+/).slice(1));
var ps = spawn('aoss', ['flite', '-voice', '/opt/voices/cmu_us_slt.flitevox']);
ps.stdin.end(argv._.join(' '));
}
if (/sudoroom_BigTV/.test(message)) {
spawn('ssh', ['[email protected]', 'bin/mainscreenturnon']);
}
});
function currentHour() {
var timeNow = new Date();
var hour = timeNow.getHours();
return hour;
}
var prev = { ssh: null, health: null };
client.connect(5, function () { ssh(); })
function ssh () {
var ps = spawn('ssh', [ '[email protected]', 'psy log doorjam' ]);
ps.on('exit', function () {
clearTimeout(timeout);
timeout = null;
setTimeout(ssh, 300000);
failing.ssh = true;
});
ps.stderr.pipe(process.stderr);
ps.stdout.pipe(process.stdout);
ps.stdout.pipe(split()).pipe(through(write));
ps.stderr.pipe(split()).pipe(through(write));
function write (buf, enc, next) {
if (failing.ssh && !timeout) {
timeout = setTimeout(function () {
say('DOOR EVENT: omnidoor ssh connection established');
failing.ssh = false;
timeout = null;
}, 5000)
}
var line = buf.toString();
if (line.indexOf('Could not resolve') > -1) {
say('DOOR EVENT: omnidoor ssh connection FAILED!!!!');
}
var m;
if (/^#ANNOUNCE/.test(line) && (m = /"([^"]+)"/.exec(line))) {
failing.logs = false;
if (!last.swipe || Date.now() - last.swipe > 1000*15) {
say('DOOR EVENT: ' + m[1] + ' swiped into the building');
last.swipe = Date.now();
}
}
else if (/^Access granted/i.test(line) && !/^#ANNOUNCE/.test(prev)) {
failing.logs = false;
if (!last.swipe || Date.now() - last.swipe > 1000*15) {
if (currentHour() < 11) {
say('!say DOOR EVENT: somebody swiped into the building');
}
else {
say('DOOR EVENT: somebody swiped into the building');
}
last.swipe = Date.now();
}
}
else if (/^Everything initialized and ready/i.test(line)) {
failing.logs = false;
failing.serial = false;
failing.magstripe = false;
say('DOOR EVENT: READY');
}
else if (/^(Error:|SERIAL ERROR)/i.test(line)) {
failing.logs = false;
if (!failing.serial) {
say('DOOR EVENT: ' + line);
}
failing.serial = true;
}
else if (/^(Error:|MAGSTRIPE ERROR)/i.test(line)) {
failing.logs = false;
if (!failing.magstripe) {
say('DOOR EVENT: ' + line);
}
failing.magstripe = true;
}
else if (/^Can not find log files,/.test(line)) {
if (!failing.logs) {
say('DOOR EVENT: log read failure');
}
failing.logs = true;
}
else if (/^health/.test(line)) {
try { var health = JSON.parse(line.replace(/^health\s+/, '')) }
catch (err) { return next() }
if ((isNaN(health.voltage) || health.voltage < 13.0)
&& health.sinceMotor > 1000*10
&& health.voltage >= 0
&& (!last.criticalVoltage
|| Date.now() - last.criticalVoltage >= 1000*60*10)) {
say('CRITICAL ARDUINO VOLTAGE: ' + health.voltage);
last.criticalVoltage = Date.now();
}
if (health.sinceVoltage > 1000 * 60 * 2
&& Date.now() - last.sinceVoltage >= 1000*60*10) {
var mins = Math.floor(sinceVoltage / 1000 / 60);
say('NO ARDUINO VOLTAGE REPORTED IN ' + mins + ' MINUTES');
last.sinceVoltage = Date.now();
}
}
next();
}
}