Skip to content

Commit

Permalink
Merge pull request #268 from apeiron/0.3.x
Browse files Browse the repository at this point in the history
Add proper long line wrapping.
  • Loading branch information
Chris Nehren committed Jan 7, 2015
2 parents 8aa8b03 + 0cb9372 commit 377ce46
Showing 1 changed file with 62 additions and 13 deletions.
75 changes: 62 additions & 13 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ function Client(server, nick, opt) {
// (normally this is because you chose something too long and
// the server has shortened it
self.nick = message.args[0];
// Note our hostmask to use it in splitting long messages.
// We don't send our hostmask when issuing PRIVMSGs or NOTICEs,
// of course, but rather the servers on the other side will
// include it in messages and will truncate what we send if
// the string is too long. Therefore, we need to be considerate
// neighbors and truncate our messages accordingly.
self.hostMask = message.args[message.args.length - 1];
self._updateMaxLineLength();
self.emit('registered', message);
break;
case 'rpl_myinfo':
Expand Down Expand Up @@ -202,6 +210,7 @@ function Client(server, nick, opt) {
self.opt.nickMod++;
self.send('NICK', self.opt.nick + self.opt.nickMod);
self.nick = self.opt.nick + self.opt.nickMod;
self._updateMaxLineLength();
break;
case 'PING':
self.send('PONG', message.args[0]);
Expand Down Expand Up @@ -281,9 +290,11 @@ function Client(server, nick, opt) {
});
break;
case 'NICK':
if (message.nick == self.nick)
if (message.nick == self.nick) {
// the user just changed their own nick
self.nick = message.args[0];
self._updateMaxLineLength();
}

if (self.opt.debug)
util.log('NICK: ' + message.nick + ' changes nick to ' + message.args[0]);
Expand Down Expand Up @@ -831,34 +842,66 @@ Client.prototype.part = function(channel, message, callback) { // {{{
this.send('PART', channel);
}
}; // }}}
Client.prototype.say = function(target, text) { // {{{
Client.prototype.action = function(channel, text) { // {{{
var self = this;
if (typeof text !== 'undefined') {
text.toString().split(/\r?\n/).filter(function(line) {
return line.length > 0;
}).forEach(function(line) {
var r = new RegExp('.{1,' + self.opt.messageSplit + '}', 'g'),
messagePart;
while ((messagePart = r.exec(line)) !== null) {
self.send('PRIVMSG', target, messagePart[0]);
self.emit('selfMessage', target, messagePart[0]);
}
self.say(channel, '\u0001ACTION ' + line + '\u0001');
});
}
}; // }}}
Client.prototype.action = function(channel, text) { // {{{
Client.prototype._splitLongLines = function(words, maxLength, destination) { // {{{
if (words.length < maxLength) {
destination.push(words);
return destination;
}
var c = words[maxLength - 1];
var cutPos;
if (c.match(/\s/)) {
cutPos = maxLength - 1;
} else {
var offset = 1;
while ((maxLength - offset) > 0) {
var c = words[maxLength - offset];
if (c.match(/\s/)) {
cutPos = maxLength - offset;
break;
}
offset++;
}
if (maxLength - offset <= 0) {
cutPos = maxLength;
}
}
var part = words.substring(0, cutPos);
destination.push(part);
return this._splitLongLines(words.substring(cutPos + 1, words.length), maxLength, destination);
}; // }}}
Client.prototype.say = function(target, text) { // {{{
this._speak('PRIVMSG', target, text);
}; // }}}
Client.prototype.notice = function(target, text) { // {{{
this._speak('NOTICE', target, text);
}; // }}}
Client.prototype._speak = function(kind, target, text) { // {{{
var self = this;
var maxLength = this.maxLineLength - target.length;
if (typeof text !== 'undefined') {
text.toString().split(/\r?\n/).filter(function(line) {
return line.length > 0;
}).forEach(function(line) {
self.say(channel, '\u0001ACTION ' + line + '\u0001');
var linesToSend = self._splitLongLines(line, maxLength, []);
linesToSend.forEach(function(toSend) {
self.send(kind, target, toSend);
if (kind == 'PRIVMSG') {
self.emit('selfMessage', target, toSend);
}
});
});
}
}; // }}}
Client.prototype.notice = function(target, text) { // {{{
this.send('NOTICE', target, text);
}; // }}}
Client.prototype.whois = function(nick, callback) { // {{{
if (typeof callback === 'function') {
var callbackWrapper = function(info) {
Expand Down Expand Up @@ -905,6 +948,12 @@ Client.prototype.ctcp = function(to, type, text) {
return this[type === 'privmsg' ? 'say' : 'notice'](to, '\1' + text + '\1');
};

// blatantly stolen from irssi's splitlong.pl. Thanks, Bjoern Krombholz!
Client.prototype._updateMaxLineLength = function() {
// 497 = 510 - (":" + "!" + " PRIVMSG " + " :").length;
// target is determined in _speak() and subtracted there
this.maxLineLength = 497 - this.nick.length - this.hostMask.length;
};
/*
* parseMessage(line, stripColors)
*
Expand Down

0 comments on commit 377ce46

Please sign in to comment.