1 var _
= require('lodash');
6 var ClientCommands = function (client
) {
9 module
.exports
= ClientCommands
;
11 ClientCommands
.prototype.run = function (command
, args
, irc_connection
, callback
) {
12 // Do we have a function to handle this command?
13 if (!listeners
[command
.toUpperCase()]) {
17 return listeners
[command
.toUpperCase()](args
, irc_connection
, callback
);
24 * Truncate a string into blocks of a set size
26 function truncateString(str
, block_size
) {
27 block_size
= block_size
|| 350;
32 for (current_pos
= 0; current_pos
< str
.length
; current_pos
= current_pos
+ block_size
) {
33 blocks
.push(str
.substr(current_pos
, block_size
));
43 PRIVMSG: function (args
, irc_connection
, callback
) {
44 // Maximum length of target + message we can send to the IRC server is 500 characters
45 // but we need to leave extra room for the sender prefix so the entire message can
46 // be sent from the IRCd to the target without being truncated.
48 var blocks
= truncateString(args
.msg
, 350);
49 blocks
.forEach(function (block
, idx
) {
50 // Apply the callback on the last message only
51 var cb
= (idx
=== blocks
.length
- 1) ?
55 irc_connection
.write('PRIVMSG ' + args
.target
+ ' :' + block
, cb
);
60 CTCP: function (args
, irc_connection
, callback
) {
61 if ((args
.target
) && (args
.type
)) {
63 irc_connection
.write('PRIVMSG ' + args
.target
+ ' :' + String
.fromCharCode(1) + args
.type
.toUpperCase() + ' ' + args
.params
+ String
.fromCharCode(1), callback
);
65 irc_connection
.write('NOTICE ' + args
.target
+ ' :' + String
.fromCharCode(1) + args
.type
.toUpperCase() + ' ' + args
.params
+ String
.fromCharCode(1), callback
);
71 RAW: function (args
, irc_connection
, callback
) {
72 irc_connection
.write(args
.data
, callback
);
76 JOIN: function (args
, irc_connection
, callback
) {
78 channels
= args
.channel
.split(",");
79 keys
= (args
.key
) ? args
.key
.split(",") : [];
80 _
.each(channels
, function (chan
, index
) {
81 irc_connection
.write('JOIN ' + chan
+ ' ' + (keys
[index
] || ''), callback
);
87 CHANNEL_INFO: function (args
, irc_connection
, callback
) {
89 irc_connection
.write('MODE ' + args
.channel
, callback
);
94 PART: function (args
, irc_connection
, callback
) {
96 _
.each(args
.channel
.split(","), function (chan
) {
97 irc_connection
.write('PART ' + chan
, callback
);
103 TOPIC: function (args
, irc_connection
, callback
) {
106 irc_connection
.write('TOPIC ' + args
.channel
+ ' :' + args
.topic
, callback
);
108 irc_connection
.write('TOPIC ' + args
.channel
, callback
);
114 KICK: function (args
, irc_connection
, callback
) {
115 if ((args
.channel
) && (args
.nick
)) {
116 irc_connection
.write('KICK ' + args
.channel
+ ' ' + args
.nick
+ ' :' + args
.reason
, callback
);
121 QUIT: function (args
, irc_connection
, callback
) {
122 websocket
.ircConnection
.end('QUIT :' + args
.message
+ '\r\n');
123 websocket
.sentQUIT
= true;
124 websocket
.ircConnection
.destroySoon();
125 websocket
.disconnect();
129 NOTICE: function (args
, irc_connection
, callback
) {
130 // Maximum length of target + message we can send to the IRC server is 500 characters
131 // but we need to leave extra room for the sender prefix so the entire message can
132 // be sent from the IRCd to the target without being truncated.
134 var blocks
= truncateString(args
.msg
, 350);
135 blocks
.forEach(function (block
, idx
) {
136 // Apply the callback on the last message only
137 var cb
= (idx
=== blocks
.length
- 1) ?
141 irc_connection
.write('NOTICE ' + args
.target
+ ' :' + block
, cb
);
146 MODE: function (args
, irc_connection
, callback
) {
147 if ((args
.target
) && (args
.mode
)) {
148 irc_connection
.write('MODE ' + args
.target
+ ' ' + args
.mode
+ ' ' + args
.params
, callback
);
153 NICK: function (args
, irc_connection
, callback
) {
155 irc_connection
.write('NICK ' + args
.nick
, callback
);
160 KIWI: function (args
, irc_connection
, callback
) {
161 if ((args
.target
) && (args
.data
)) {
162 irc_connection
.write('PRIVMSG ' + args
.target
+ ': ' + String
.fromCharCode(1) + 'KIWI ' + args
.data
+ String
.fromCharCode(1), callback
);
166 ENCODING: function (args
, irc_connection
, callback
) {
168 return callback(irc_connection
.setEncoding(args
.encoding
));