forked from misterhat/captmoose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirc.js
More file actions
154 lines (126 loc) · 3.89 KB
/
irc.js
File metadata and controls
154 lines (126 loc) · 3.89 KB
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
var http = require('http'),
irc = require('irc'),
c = require('irc-colors');
var config = require('./config'),
client = new irc.Client(config.server, config.nick || 'CaptMoose', {
channels: config.channels
}),
// when the last moose display occured
lastMessage = 0,
url = 'http://' + config.host + (config.port ? ':' + config.port : '');
// find a moose on the moose server and try to parse it
function findMoose(name, done) {
http.request({
host: config.host,
port: config.port,
path: '/moose/' + encodeURIComponent(name)
}, function (res) {
var moose = '';
if (!/^2/.test(res.statusCode)) {
return done();
}
res.on('data', function (chunk) {
moose += chunk;
});
res.on('end', function () {
try {
moose = JSON.parse(moose);
done(null, moose);
} catch (e) {
done(e);
}
});
res.on('error', function (err) {
done(err);
});
}).end();
}
// remove the transparent padding around the moose
function shrinkMoose(moose) {
var minX = moose[0].length,
minY = moose.length, maxY = 0,
i, j;
for (i = 0; i < moose.length; i += 1) { // height
for (j = 0; j < moose[0].length; j += 1) { // width
if (moose[i][j] !== 'transparent') {
if (i < minY) {
minY = i;
} else if (i > maxY) {
maxY = i;
}
if (j < minX) {
minX = j;
}
}
}
}
moose = moose.slice(minY, maxY + 1).map(function (row) {
var lastColour = row.length,
i;
for (i = 0; i < row.length; i += 1) {
if (row[i] !== 'transparent') {
lastColour = i;
}
}
return row.slice(minX, lastColour + 1);
});
return moose;
}
// turn the moost from a 2D list of colours to a 2D list of IRC colour codes
function formatMoose(moose) {
return moose.map(function (row) {
return row.map(function (colour) {
if (colour === 'transparent') {
return c.stripColors(' ');
}
return c[colour]['bg' + colour]('@');
});
});
}
// we don't want the moose to get kicked for spamming, so implement a short
// delay between two lines
function sayMoose(say, moose) {
if (moose.length) {
say(moose[0].join(''));
if (moose[1]) {
say(moose[1].join(''));
}
setTimeout(function () {
sayMoose(say, moose.slice(2, moose.length));
}, 800);
} else {
return;
}
}
client.addListener('message', function (from, to, message) {
// make sure only valid characters exist in the mooseme command
var mooseMe = message.match(/^\.?moose(?:me)? ([A-z0-9 -_]+)/),
bots = /^\.bots/.test(message),
remaining;
// message not from channel
if (!/#/.test(to) || !(mooseMe || bots)) {
return;
}
remaining = Math.round((Date.now() - lastMessage) / 1000);
// moose was called to recently
if (remaining < 25) {
return client.say(from, 'please wait another ' + (25 - remaining) +
' seconds');
}
if (bots) {
client.say(to, 'CaptDeer [NodeJS], create moose pictures at ' + url);
return;
}
findMoose(mooseMe[1].trim(), function (err, moose) {
if (err) {
client.say(to, c.bold.red('moose parsing error'));
return console.error(err.stack);
}
if (!moose) {
return client.say(to, c.bold.red('moose not found'));
}
moose = formatMoose(shrinkMoose(moose.moose));
lastMessage = Date.now();
sayMoose(client.say.bind(client, to), moose);
});
});