-
Notifications
You must be signed in to change notification settings - Fork 0
/
lousybot.js
232 lines (186 loc) · 5.72 KB
/
lousybot.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var serverHost = '127.0.0.1',
serverPort = 6668,
serverPass = '*',
botNick = 'lousybot',
joinChannels = [ '#test' ],
pluginDir = './plugins',
commandPrefix = '!',
enableHttp = true,
httpPort = 31337,
httpHost = 'localhost',
IRC_MESSAGE_END = '\r\n',
IRC_MAX_MESSAGE = 417,
fs = require('fs'),
http = require('http'),
net = require('net'),
queryString = require('querystring'),
spawn = require('child_process').spawn,
url = require('url');
function formatIrcMessage(command, text) {
return command + ' ' + text + IRC_MESSAGE_END;
}
function parseIrcMessage(s) {
console.log('parse ' + s);
var match = s.match(/(?::(.+?) )?(.+?) (.*)/),
parsed = {
prefix : match[1],
command : match[2],
params : match[3].split(' ')
},
hostmaskMatch;
if (parsed.prefix !== undefined) {
hostmaskMatch = parsed.prefix.match(/(.+)!(.+)@(.+)/);
if (hostmaskMatch) {
parsed.from = {
nick : hostmaskMatch[1],
user : hostmaskMatch[2],
host : hostmaskMatch[3]
};
}
}
console.log(parsed);
return parsed;
}
function loadPlugins(path) {
var exp,
plugin,
pluginPath;
fs.readdirSync(path).forEach(function (file) {
if (file.match(/\.js$/)) {
pluginPath = path + '/' + file;
console.log('require ' + pluginPath);
plugin = require(pluginPath);
for (exp in plugin) {
conn.addListener(exp, plugin[exp]);
}
}
});
}
function parseBotCommand(s) {
var argSep,
command,
result = {
botCommand : '',
botCommandArgs : ''
};
if (s.substr(0, commandPrefix.length) === commandPrefix) {
command = s.substr(commandPrefix.length);
argSep = command.indexOf(' ');
if (argSep !== -1) {
result.botCommand = command.substr(0, argSep);
result.botCommandArgs = command.substr(argSep + 1, command.length);
} else {
result.botCommand = command;
}
}
return result;
}
// divide a string into parts, each a maximum of partSize length
function partString(s, partSize) {
var numParts = Math.floor(s.length / partSize),
i,
start,
result = [];
if (s.length % partSize !== 0) {
numParts += 1;
}
for (i = 0; i < numParts; i++) {
start = i * partSize;
result.push(s.slice(start, start + partSize));
}
return result;
}
var conn = net.createConnection(serverPort, host=serverHost);
conn.botNick = botNick;
conn.messageBuffer = '';
conn.sendMessage = function (command, text) {
var formatted = formatIrcMessage(command, text);
console.log('send ' + formatted);
this.write(formatted);
};
conn.privmsg = function (to, message) {
var prefixLen = ('PRIVMSG ' + to + ' :').length,
partSize = IRC_MAX_MESSAGE - prefixLen,
messageParts,
i,
messagePart;
messageParts = partString(message, partSize);
for (i = 0; i < messageParts.length; i++) {
this.sendMessage('PRIVMSG', to + ' :' + messageParts[i]);
}
};
conn.addListener('connect', function () {
this.sendMessage('PASS', serverPass);
this.sendMessage('NICK', botNick);
this.sendMessage('USER', botNick + ' * * ' + botNick);
});
conn.addListener('data', function (buffer) {
var connection;
console.log('receive ' + buffer.toString());
this.messageBuffer += buffer.toString();
if (this.messageBuffer.match(new RegExp(IRC_MESSAGE_END + '$'))) {
connection = this;
this.messageBuffer.split(IRC_MESSAGE_END).forEach(function (message) {
var parsedMessage;
if (message !== '') {
parsedMessage = parseIrcMessage(message);
connection.emit(parsedMessage.command, parsedMessage);
}
});
this.messageBuffer = '';
}
});
// spawn a child process and call lineCallback for each line of its stdout
conn.spawn = function (command, args, lineCallback) {
var child = spawn(command, args),
buffer = '',
lines;
child.stdout.on('data', function (data) {
lines = (buffer + data).split('\n');
buffer = lines.pop();
lines.forEach(lineCallback);
});
return child;
};
// IRC command listeners
// welcome
conn.addListener('001', function () {
var connection = this;
joinChannels.forEach(function (channel) {
connection.sendMessage('JOIN', channel);
});
});
conn.addListener('PING', function (m) {
this.sendMessage('PONG', m.params[0]);
});
conn.addListener('PRIVMSG', function (m) {
var botCommand,
x;
m.to = m.params[0];
m.text = m.params.slice(1).join(' ').substr(1);
botCommand = parseBotCommand(m.text);
for (x in botCommand) {
m[x] = botCommand[x];
}
if (m.to.match(/^#/)) {
m.channel = m.to;
this.emit('channelMessage', m);
} else {
this.emit('privateMessage', m);
}
});
loadPlugins(pluginDir);
if (enableHttp) {
http.createServer(function (req, resp) {
var query,
urlParsed;
urlParsed = url.parse(req.url);
console.log(req.method + ' ' + urlParsed.href);
query = queryString.parse(urlParsed.query);
if (query.to && query.message) {
conn.privmsg(query.to, query.message);
}
resp.writeHead(200, { 'Content-Type' : 'text/html' });
resp.end(conn.botNick + ' <form method="get" action=""><input type="text" name="to" /><input type="text" name="message" /><input type="submit" /></form>');
}).listen(httpPort, httpHost);
}