Skip to content

Commit 0b78ffe

Browse files
committed
IRC message builder
1 parent 5c486f5 commit 0b78ffe

File tree

4 files changed

+78
-24
lines changed

4 files changed

+78
-24
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ module.exports.Client = require('./src/client');
99
module.exports.Client.setDefaultTransport(require('./src/transports/net'));
1010

1111
module.exports.ircLineParser = require('./src/irclineparser');
12+
module.exports.Message = require('./src/ircmessage');

src/client.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var EventEmitter = require('eventemitter3');
44
var _ = require('lodash');
55
var MiddlewareHandler = require('middleware-handler');
66
var IrcCommandHandler = require('./commands/').CommandHandler;
7+
var IrcMessage = require('./ircmessage');
78
var Connection = require('./connection');
89
var NetworkInfo = require('./networkinfo');
910
var User = require('./user');
@@ -308,7 +309,11 @@ module.exports = class IrcClient extends EventEmitter {
308309
* Client API
309310
*/
310311
raw(input) {
311-
this.connection.write(this.rawString.apply(this, arguments));
312+
if (input instanceof IrcMessage) {
313+
this.connection.write(input.to1459());
314+
} else {
315+
this.connection.write(this.rawString.apply(this, arguments));
316+
}
312317
}
313318

314319

src/irclineparser.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var _ = require('lodash');
44
var MessageTags = require('./messagetags');
5+
var IrcMessage = require('./ircmessage');
56

67
module.exports = parseIrcLine;
78

@@ -13,38 +14,31 @@ module.exports = parseIrcLine;
1314
var parse_regex = /^(?:@([^ ]+) )?(?::((?:(?:([^\s!@]+)(?:!([^\s@]+))?)@)?(\S+)) )?((?:[a-zA-Z]+)|(?:[0-9]{3}))(?: ([^:].*?))?(?: :(.*))?$/i;
1415

1516
function parseIrcLine(line) {
16-
var msg;
17-
var tags = Object.create(null);
18-
var msg_obj;
19-
2017
// Parse the complete line, removing any carriage returns
21-
msg = parse_regex.exec(line.replace(/^\r+|\r+$/, ''));
22-
23-
if (!msg) {
18+
let matches = parse_regex.exec(line.replace(/^\r+|\r+$/, ''));
19+
if (!matches) {
2420
// The line was not parsed correctly, must be malformed
2521
return;
2622
}
2723

28-
// Extract any tags (msg[1])
29-
if (msg[1]) {
30-
tags = MessageTags.decode(msg[1]);
24+
let msg = new IrcMessage();
25+
26+
if (matches[1]) {
27+
msg.tags = MessageTags.decode(matches[1]);
3128
}
3229

33-
// Nick value will be in the prefix slot if a full user mask is not used
34-
msg_obj = {
35-
tags: tags,
36-
prefix: msg[2],
37-
nick: msg[3] || msg[2],
38-
ident: msg[4] || '',
39-
hostname: msg[5] || '',
40-
command: msg[6],
41-
params: msg[7] ? msg[7].split(/ +/) : []
42-
};
30+
msg.prefix = matches[2];
31+
// Nick will be in the prefix slot if a full user mask is not used
32+
msg.nick = matches[3] || matches[2];
33+
msg.ident = matches[4] || '';
34+
msg.hostname = matches[5] || '';
35+
msg.command = matches[6];
36+
msg.params = matches[7] ? matches[7].split(/ +/) : [];
4337

4438
// Add the trailing param to the params list
45-
if (typeof msg[8] !== 'undefined') {
46-
msg_obj.params.push(_.trimEnd(msg[8]));
39+
if (typeof matches[8] !== 'undefined') {
40+
msg.params.push(_.trimEnd(matches[8]));
4741
}
4842

49-
return msg_obj;
43+
return msg;
5044
}

src/ircmessage.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
let MessageTags = require('./messagetags');
2+
3+
module.exports = class IrcMessage {
4+
static fromRaw(rawMessage) {
5+
6+
}
7+
8+
constructor(command, ...args) {
9+
this.tags = Object.create(null);
10+
this.prefix = '';
11+
this.nick = '';
12+
this.ident = '';
13+
this.hostname = '';
14+
this.command = command || '';
15+
this.params = args || [];
16+
}
17+
18+
to1459() {
19+
let parts = [];
20+
21+
let tags = MessageTags.encode(this.tags);
22+
if (tags) {
23+
parts.push('@' + tags);
24+
}
25+
26+
if (this.prefix) {
27+
// TODO: If prefix is empty, build it from the nick!ident@hostname
28+
parts.push(':' + this.prefix);
29+
}
30+
31+
parts.push(this.command);
32+
33+
if (this.params.length > 0) {
34+
this.params.forEach((param, idx) => {
35+
if (idx === this.params.length - 1 && param.indexOf(' ') > -1) {
36+
parts.push(':' + param);
37+
} else {
38+
parts.push(param);
39+
}
40+
});
41+
}
42+
43+
return parts.join(' ');
44+
}
45+
46+
toJson() {
47+
return {
48+
tags: Object.assign({}, this.tags),
49+
source: this.prefix,
50+
command: this.command,
51+
params: this.params,
52+
};
53+
}
54+
};

0 commit comments

Comments
 (0)