X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=server%2Fclientcommands.js;h=33edeffb79cabf93054688fd7871bf0e5c5cbfb3;hb=39105e8e96606e95e6aa18806aad00d470098c17;hp=f5c870b02f27d35bc4852ff23912b7fcc14aa448;hpb=991091b7cbde982ee6cee82a64e615d4207bfbb7;p=KiwiIRC.git diff --git a/server/clientcommands.js b/server/clientcommands.js index f5c870b..33edeff 100644 --- a/server/clientcommands.js +++ b/server/clientcommands.js @@ -20,11 +20,40 @@ ClientCommands.prototype.run = function (command, args, irc_connection, callback +/** + * Truncate a string into blocks of a set size + */ +function truncateString(str, block_size) { + block_size = block_size || 350; + + var blocks = [], + current_pos; + + for (current_pos = 0; current_pos < str.length; current_pos = current_pos + block_size) { + blocks.push(str.substr(current_pos, block_size)); + } + + return blocks; +} + + + + var listeners = { PRIVMSG: function (args, irc_connection, callback) { - if (args.target && (args.msg)) { - irc_connection.write('PRIVMSG ' + args.target + ' :' + args.msg, callback); - } + // Maximum length of target + message we can send to the IRC server is 500 characters + // but we need to leave extra room for the sender prefix so the entire message can + // be sent from the IRCd to the target without being truncated. + + var blocks = truncateString(args.msg, 350); + blocks.forEach(function (block, idx) { + // Apply the callback on the last message only + var cb = (idx === blocks.length - 1) ? + callback : + undefined; + + irc_connection.write('PRIVMSG ' + args.target + ' :' + block, cb); + }); }, @@ -77,7 +106,7 @@ var listeners = { KICK: function (args, irc_connection, callback) { if ((args.channel) && (args.nick)) { - irc_connection.write('KICK ' + args.channel + ' ' + args.nick + ':' + args.reason, callback); + irc_connection.write('KICK ' + args.channel + ' ' + args.nick + ' :' + args.reason, callback); } }, @@ -91,9 +120,19 @@ var listeners = { NOTICE: function (args, irc_connection, callback) { - if ((args.target) && (args.msg)) { - irc_connection.write('NOTICE ' + args.target + ' :' + args.msg, callback); - } + // Maximum length of target + message we can send to the IRC server is 500 characters + // but we need to leave extra room for the sender prefix so the entire message can + // be sent from the IRCd to the target without being truncated. + + var blocks = truncateString(args.msg, 350); + blocks.forEach(function (block, idx) { + // Apply the callback on the last message only + var cb = (idx === blocks.length - 1) ? + callback : + undefined; + + irc_connection.write('NOTICE ' + args.target + ' :' + block, cb); + }); }, @@ -115,5 +154,11 @@ var listeners = { if ((args.target) && (args.data)) { irc_connection.write('PRIVMSG ' + args.target + ': ' + String.fromCharCode(1) + 'KIWI ' + args.data + String.fromCharCode(1), callback); } + }, + + ENCODING: function (args, irc_connection, callback) { + if (args.encoding) { + return callback(irc_connection.setEncoding(args.encoding)); + } } };