Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper long line wrapping. #268

Merged
3 commits merged into from Jan 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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