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 PRIVMSG: function (args
, irc_connection
, callback
) {
25 // Maximum length of target + message we can send to the IRC server is 500 characters
26 // but we need to leave extra room for the sender prefix so the entire message can
27 // be sent from the IRCd to the target without being truncated.
28 var wrap_length
= 350,
33 if (args
.target
&& (args
.msg
)) {
34 trunc_length
= wrap_length
- args
.target
.length
;
35 // If the message is longer than wrap_length, send the message in chunks
36 while (message
.length
> trunc_length
) {
37 trunc_msg
= message
.substr(0, trunc_length
);
38 message
= message
.substr(trunc_length
);
39 irc_connection
.write('PRIVMSG ' + args
.target
+ ' :' + trunc_msg
);
41 // Send the remaining text
42 irc_connection
.write('PRIVMSG ' + args
.target
+ ' :' + message
, callback
);
47 CTCP: function (args
, irc_connection
, callback
) {
48 if ((args
.target
) && (args
.type
)) {
50 irc_connection
.write('PRIVMSG ' + args
.target
+ ' :' + String
.fromCharCode(1) + args
.type
.toUpperCase() + ' ' + args
.params
+ String
.fromCharCode(1), callback
);
52 irc_connection
.write('NOTICE ' + args
.target
+ ' :' + String
.fromCharCode(1) + args
.type
.toUpperCase() + ' ' + args
.params
+ String
.fromCharCode(1), callback
);
58 RAW: function (args
, irc_connection
, callback
) {
59 irc_connection
.write(args
.data
, callback
);
63 JOIN: function (args
, irc_connection
, callback
) {
65 channels
= args
.channel
.split(",");
66 keys
= (args
.key
) ? args
.key
.split(",") : [];
67 _
.each(channels
, function (chan
, index
) {
68 irc_connection
.write('JOIN ' + chan
+ ' ' + (keys
[index
] || ''), callback
);
74 PART: function (args
, irc_connection
, callback
) {
76 _
.each(args
.channel
.split(","), function (chan
) {
77 irc_connection
.write('PART ' + chan
, callback
);
83 TOPIC: function (args
, irc_connection
, callback
) {
86 irc_connection
.write('TOPIC ' + args
.channel
+ ' :' + args
.topic
, callback
);
88 irc_connection
.write('TOPIC ' + args
.channel
, callback
);
94 KICK: function (args
, irc_connection
, callback
) {
95 if ((args
.channel
) && (args
.nick
)) {
96 irc_connection
.write('KICK ' + args
.channel
+ ' ' + args
.nick
+ ':' + args
.reason
, callback
);
101 QUIT: function (args
, irc_connection
, callback
) {
102 websocket
.ircConnection
.end('QUIT :' + args
.message
+ '\r\n');
103 websocket
.sentQUIT
= true;
104 websocket
.ircConnection
.destroySoon();
105 websocket
.disconnect();
109 NOTICE: function (args
, irc_connection
, callback
) {
110 // Maximum length of target + message we can send to the IRC server is 500 characters
111 // but we need to leave extra room for the sender prefix so the entire message can
112 // be sent from the IRCd to the target without being truncated.
113 var wrap_length
= 350,
118 if (args
.target
&& (args
.msg
)) {
119 trunc_length
= wrap_length
- args
.target
.length
;
120 // If the message is longer than wrap_length, send the message in chunks
121 while (message
.length
> trunc_length
) {
122 trunc_msg
= message
.substr(0, trunc_length
);
123 message
= message
.substr(trunc_length
);
124 irc_connection
.write('NOTICE ' + args
.target
+ ' :' + trunc_msg
);
126 // Send the remaining text
127 irc_connection
.write('NOTICE ' + args
.target
+ ' :' + message
, callback
);
132 MODE: function (args
, irc_connection
, callback
) {
133 if ((args
.target
) && (args
.mode
)) {
134 irc_connection
.write('MODE ' + args
.target
+ ' ' + args
.mode
+ ' ' + args
.params
, callback
);
139 NICK: function (args
, irc_connection
, callback
) {
141 irc_connection
.write('NICK ' + args
.nick
, callback
);
146 KIWI: function (args
, irc_connection
, callback
) {
147 if ((args
.target
) && (args
.data
)) {
148 irc_connection
.write('PRIVMSG ' + args
.target
+ ': ' + String
.fromCharCode(1) + 'KIWI ' + args
.data
+ String
.fromCharCode(1), callback
);