Commit | Line | Data |
---|---|---|
a8bf3ea4 JA |
1 | var _ = require('underscore'); |
2 | ||
3 | var irc_numerics = { | |
4 | RPL_WELCOME: '001', | |
5 | RPL_MYINFO: '004', | |
6 | RPL_ISUPPORT: '005', | |
f3766a0b | 7 | RPL_WHOISREGNICK: '307', |
a8bf3ea4 JA |
8 | RPL_WHOISUSER: '311', |
9 | RPL_WHOISSERVER: '312', | |
10 | RPL_WHOISOPERATOR: '313', | |
11 | RPL_WHOISIDLE: '317', | |
12 | RPL_ENDOFWHOIS: '318', | |
13 | RPL_WHOISCHANNELS: '319', | |
14 | RPL_LISTSTART: '321', | |
15 | RPL_LIST: '322', | |
16 | RPL_LISTEND: '323', | |
17 | RPL_NOTOPIC: '331', | |
18 | RPL_TOPIC: '332', | |
19 | RPL_TOPICWHOTIME: '333', | |
20 | RPL_NAMEREPLY: '353', | |
21 | RPL_ENDOFNAMES: '366', | |
22 | RPL_BANLIST: '367', | |
23 | RPL_ENDOFBANLIST: '368', | |
24 | RPL_MOTD: '372', | |
25 | RPL_MOTDSTART: '375', | |
26 | RPL_ENDOFMOTD: '376', | |
27 | RPL_WHOISMODES: '379', | |
28 | ERR_NOSUCHNICK: '401', | |
29 | ERR_CANNOTSENDTOCHAN: '404', | |
30 | ERR_TOOMANYCHANNELS: '405', | |
31 | ERR_NICKNAMEINUSE: '433', | |
32 | ERR_USERNOTINCHANNEL: '441', | |
33 | ERR_NOTONCHANNEL: '442', | |
34 | ERR_NOTREGISTERED: '451', | |
35 | ERR_LINKCHANNEL: '470', | |
36 | ERR_CHANNELISFULL: '471', | |
37 | ERR_INVITEONLYCHAN: '473', | |
38 | ERR_BANNEDFROMCHAN: '474', | |
39 | ERR_BADCHANNELKEY: '475', | |
40 | ERR_CHANOPRIVSNEEDED: '482', | |
41 | RPL_STARTTLS: '670' | |
42 | }; | |
43 | ||
44 | ||
2a8e95d1 | 45 | var IrcCommands = function (irc_connection, con_num, client) { |
a8bf3ea4 JA |
46 | this.irc_connection = irc_connection; |
47 | this.con_num = con_num; | |
48 | this.client = client; | |
49 | }; | |
2a8e95d1 | 50 | module.exports = IrcCommands; |
a8bf3ea4 | 51 | |
2a8e95d1 | 52 | IrcCommands.prototype.bindEvents = function () { |
a8bf3ea4 | 53 | var that = this; |
2a8e95d1 | 54 | |
a8bf3ea4 JA |
55 | _.each(listeners, function (listener, command) { |
56 | var s = command.substr(0, 4); | |
57 | if ((s === 'RPL_') || (s === 'ERR_')) { | |
58 | command = irc_numerics[command]; | |
59 | } | |
60 | that.irc_connection.on('irc_' + command, function () { | |
61 | listener.apply(that, arguments); | |
62 | }); | |
63 | }); | |
64 | }; | |
65 | ||
c08717da D |
66 | IrcCommands.prototype.dispose = function () { |
67 | this.removeAllListeners(); | |
68 | }; | |
69 | ||
2a8e95d1 D |
70 | |
71 | ||
a8bf3ea4 JA |
72 | var listeners = { |
73 | 'RPL_WELCOME': function (command) { | |
74 | var nick = command.params[0]; | |
75 | this.irc_connection.registered = true; | |
eec8c71e JA |
76 | this.cap_negotation = false; |
77 | console.log(this.irc_connection.cap.enabled); | |
2f1e8a71 | 78 | this.client.sendIrcCommand('connect', {server: this.con_num, nick: nick}); |
a8bf3ea4 JA |
79 | }, |
80 | 'RPL_ISUPPORT': function (command) { | |
81 | var options, i, option, matches, j; | |
82 | options = command.params; | |
83 | for (i = 1; i < options.length; i++) { | |
84 | option = options[i].split("=", 2); | |
85 | option[0] = option[0].toUpperCase(); | |
86 | this.irc_connection.options[option[0]] = (typeof option[1] !== 'undefined') ? option[1] : true; | |
87 | if (_.include(['NETWORK', 'PREFIX', 'CHANTYPES', 'CHANMODES', 'NAMESX'], option[0])) { | |
88 | if (option[0] === 'PREFIX') { | |
89 | matches = /\(([^)]*)\)(.*)/.exec(option[1]); | |
90 | if ((matches) && (matches.length === 3)) { | |
91 | this.irc_connection.options.PREFIX = []; | |
92 | for (j = 0; j < matches[2].length; j++) { | |
93 | this.irc_connection.options.PREFIX.push({symbol: matches[2].charAt(j), mode: matches[1].charAt(j)}); | |
94 | } | |
95 | } | |
96 | } else if (option[0] === 'CHANTYPES') { | |
97 | this.irc_connection.options.CHANTYPES = this.irc_connection.options.CHANTYPES.split(''); | |
98 | } else if (option[0] === 'CHANMODES') { | |
99 | this.irc_connection.options.CHANMODES = option[1].split(','); | |
eec8c71e | 100 | } else if ((option[0] === 'NAMESX') && (!_.contains(this.irc_connection.cap.enabled, 'multi-prefix'))) { |
b8e4d9f7 | 101 | this.irc_connection.write('PROTOCTL NAMESX'); |
a8bf3ea4 JA |
102 | } |
103 | } | |
104 | } | |
2f1e8a71 | 105 | //this.client.sendIrcCommand({server: this.con_num, command: 'RPL_ISUPPORT', options: this.irc_connection.options}); |
a8bf3ea4 | 106 | //websocket.sendClientEvent('options', {server: '', "options": irc_connection.IRC.options}); |
2f1e8a71 | 107 | this.client.sendIrcCommand('options', {server: this.con_num, options: this.irc_connection.options}); |
a8bf3ea4 JA |
108 | }, |
109 | 'RPL_ENDOFWHOIS': function (command) { | |
110 | /*command.server = this.con_num; | |
111 | command.command = 'RPL_ENDOFWHOIS'; | |
2f1e8a71 | 112 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 113 | //websocket.sendClientEvent('whois', {server: '', nick: msg.params.split(" ", 3)[1], "msg": msg.trailing, end: true}); |
2f1e8a71 | 114 | this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], msg: command.trailing, end: true}); |
a8bf3ea4 JA |
115 | }, |
116 | 'RPL_WHOISUSER': function (command) { | |
117 | /*command.server = this.con_num; | |
118 | command.command = 'RPL_WHOISUSER'; | |
2f1e8a71 | 119 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 120 | //websocket.sendClientEvent('whois', {server: '', nick: msg.params.split(" ", 3)[1], "msg": msg.trailing, end: false}); |
2f1e8a71 | 121 | this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], ident: command.params[2], host: command.params[3], msg: command.trailing, end: false}); |
a8bf3ea4 JA |
122 | }, |
123 | 'RPL_WHOISSERVER': function (command) { | |
124 | /*command.server = this.con_num; | |
125 | command.command = 'RPL_WHOISSERVER'; | |
2f1e8a71 | 126 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 127 | //websocket.sendClientEvent('whois', {server: '', nick: msg.params.split(" ", 3)[1], "msg": msg.trailing, end: false}); |
0cfbecfc | 128 | this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], irc_server: command.params[2], end: false}); |
a8bf3ea4 JA |
129 | }, |
130 | 'RPL_WHOISOPERATOR': function (command) { | |
131 | /*command.server = this.con_num; | |
132 | command.command = 'RPL_WHOISOPERATOR'; | |
2f1e8a71 | 133 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 134 | //websocket.sendClientEvent('whois', {server: '', nick: msg.params.split(" ", 3)[1], "msg": msg.trailing, end: false}); |
2f1e8a71 | 135 | this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], msg: command.trailing, end: false}); |
a8bf3ea4 JA |
136 | }, |
137 | 'RPL_WHOISCHANNELS': function (command) { | |
138 | /*command.server = this.con_num; | |
139 | command.command = 'RPL_WHOISCHANNELS'; | |
2f1e8a71 | 140 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 141 | //websocket.sendClientEvent('whois', {server: '', nick: msg.params.split(" ", 3)[1], "msg": msg.trailing, end: false}); |
2f1e8a71 | 142 | this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], chans: command.trailing, end: false}); |
a8bf3ea4 JA |
143 | }, |
144 | 'RPL_WHOISMODES': function (command) { | |
145 | /*command.server = this.con_num; | |
146 | command.command = 'RPL_WHOISMODES'; | |
2f1e8a71 | 147 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 148 | //websocket.sendClientEvent('whois', {server: '', nick: msg.params.split(" ", 3)[1], "msg": msg.trailing, end: false}); |
2f1e8a71 | 149 | this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], msg: command.trailing, end: false}); |
a8bf3ea4 JA |
150 | }, |
151 | 'RPL_WHOISIDLE': function (command) { | |
152 | /*command.server = this.con_num; | |
153 | command.command = 'RPL_WHOISIDLE'; | |
2f1e8a71 | 154 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 155 | //websocket.sendClientEvent('whois', {server: '', nick: msg.params.split(" ", 3)[1], "msg": msg.trailing, end: false}); |
7f96413b | 156 | if (command.params[3]) { |
2f1e8a71 | 157 | this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], idle: command.params[2], logon: command.params[3], end: false}); |
7f96413b | 158 | } else { |
2f1e8a71 | 159 | this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], idle: command.params[2], end: false}); |
7f96413b | 160 | } |
a8bf3ea4 | 161 | }, |
f3766a0b | 162 | 'RPL_WHOISREGNICK': function (command) { |
2f1e8a71 | 163 | this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], msg: command.trailing, end: false}); |
f3766a0b | 164 | }, |
a8bf3ea4 JA |
165 | 'RPL_LISTSTART': function (command) { |
166 | /*command.server = this.con_num; | |
167 | command.command = 'RPL_LISTSTART'; | |
2f1e8a71 D |
168 | this.client.sendIrcCommand(command);*/ |
169 | this.client.sendIrcCommand('list_start', {server: this.con_num}); | |
a8bf3ea4 JA |
170 | this.client.buffer.list = []; |
171 | }, | |
172 | 'RPL_LISTEND': function (command) { | |
173 | /*command.server = this.con_num; | |
174 | command.command = 'RPL_LISTEND'; | |
2f1e8a71 | 175 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 JA |
176 | if (this.client.buffer.list.length > 0) { |
177 | this.client.buffer.list = _.sortBy(this.client.buffer.list, function (channel) { | |
178 | return channel.num_users; | |
179 | }); | |
2f1e8a71 | 180 | this.client.sendIrcCommand('list_channel', {server: this.con_num, chans: this.client.buffer.list}); |
a8bf3ea4 JA |
181 | this.client.buffer.list = []; |
182 | } | |
2f1e8a71 | 183 | this.client.sendIrcCommand('list_end', {server: this.con_num}); |
a8bf3ea4 JA |
184 | }, |
185 | 'RPL_LIST': function (command) { | |
186 | /*command.server = this.con_num; | |
187 | command.command = 'RPL_LIST'; | |
2f1e8a71 | 188 | this.client.sendIrcCommand(command);*/ |
6d3dc969 | 189 | this.client.buffer.list.push({server: this.con_num, channel: command.params[1], num_users: parseInt(command.params[2], 10), topic: command.trailing}); |
a8bf3ea4 JA |
190 | if (this.client.buffer.list.length > 200){ |
191 | this.client.buffer.list = _.sortBy(this.client.buffer.list, function (channel) { | |
192 | return channel.num_users; | |
193 | }); | |
2f1e8a71 | 194 | this.client.sendIrcCommand('list_channel', {server: this.con_num, chans: this.client.buffer.list}); |
a8bf3ea4 JA |
195 | this.client.buffer.list = []; |
196 | } | |
197 | }, | |
198 | 'RPL_MOTD': function (command) { | |
199 | /*command.server = this.con_num; | |
200 | command.command = 'RPL_MOTD'; | |
2f1e8a71 | 201 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 JA |
202 | this.client.buffer.motd += command.trailing + '\n'; |
203 | }, | |
204 | 'RPL_MOTDSTART': function (command) { | |
205 | /*command.server = this.con_num; | |
206 | command.command = 'RPL_MOTDSTART'; | |
2f1e8a71 | 207 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 JA |
208 | this.client.buffer.motd = ''; |
209 | }, | |
210 | 'RPL_ENDOFMOTD': function (command) { | |
211 | /*command.server = this.con_num; | |
212 | command.command = 'RPL_ENDOFMOTD'; | |
2f1e8a71 | 213 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 214 | //websocket.sendClientEvent('motd', {server: '', 'msg': websocket.kiwi.buffer.motd}); |
2f1e8a71 | 215 | this.client.sendIrcCommand('motd', {server: this.con_num, msg: this.client.buffer.motd}); |
a8bf3ea4 JA |
216 | }, |
217 | 'RPL_NAMEREPLY': function (command) { | |
218 | /*command.server = this.con_num; | |
219 | command.command = 'RPL_NAMEREPLY'; | |
2f1e8a71 | 220 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 JA |
221 | var members = command.trailing.split(' '); |
222 | var member_list = []; | |
223 | var that = this; | |
224 | var i = 0; | |
225 | _.each(members, function (member) { | |
226 | var j, k, modes = []; | |
227 | for (j = 0; j < member.length; j++) { | |
228 | for (k = 0; k < that.irc_connection.options.PREFIX.length; k++) { | |
229 | if (member.charAt(j) === that.irc_connection.options.PREFIX[k].symbol) { | |
230 | modes.push(that.irc_connection.options.PREFIX[k].mode); | |
231 | i++; | |
232 | } | |
233 | } | |
234 | } | |
235 | member_list.push({nick: member, modes: modes}); | |
236 | if (i++ >= 50) { | |
2f1e8a71 | 237 | that.client.sendIrcCommand('userlist', {server: that.con_num, users: member_list, channel: command.params[2]}); |
a8bf3ea4 JA |
238 | member_list = []; |
239 | i = 0; | |
240 | } | |
241 | }); | |
242 | if (i > 0) { | |
2f1e8a71 | 243 | this.client.sendIrcCommand('userlist', {server: this.con_num, users: member_list, channel: command.params[2]}); |
a8bf3ea4 JA |
244 | } |
245 | }, | |
246 | 'RPL_ENDOFNAMES': function (command) { | |
247 | /*command.server = this.con_num; | |
248 | command.command = 'RPL_ENDOFNAMES'; | |
2f1e8a71 | 249 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 250 | //websocket.sendClientEvent('userlist_end', {server: '', channel: msg.params.split(" ")[1]}); |
2f1e8a71 | 251 | this.client.sendIrcCommand('userlist_end', {server: this.con_num, channel: command.params[1]}); |
a8bf3ea4 JA |
252 | }, |
253 | 'RPL_BANLIST': function (command) { | |
254 | /*command.server = this.con_num; | |
255 | command.command = 'RPL_BANLIST'; | |
2f1e8a71 | 256 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 257 | //websocket.sendClientEvent('banlist', {server: '', channel: params[1], banned: params[2], banned_by: params[3], banned_at: params[4]}); |
2f1e8a71 | 258 | this.client.sendIrcCommand('banlist', {server: this.con_num, channel: command.params[1], banned: command.params[2], banned_by: command.params[3], banned_at: command.params[4]}); |
a8bf3ea4 JA |
259 | }, |
260 | 'RPL_ENDOFBANLIST': function (command) { | |
261 | /*command.server = this.con_num; | |
262 | command.command = 'RPL_ENDOFBANLIST'; | |
2f1e8a71 | 263 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 264 | //websocket.sendClientEvent('banlist_end', {server: '', channel: msg.params.split(" ")[1]}); |
2f1e8a71 | 265 | this.client.sendIrcCommand('banlist_end', {server: this.con_num, channel: command.params[1]}); |
a8bf3ea4 JA |
266 | }, |
267 | 'RPL_TOPIC': function (command) { | |
268 | /*command.server = this.con_num; | |
269 | command.command = 'RPL_TOPIC'; | |
2f1e8a71 | 270 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 271 | //{nick: '', channel: msg.params.split(" ")[1], topic: msg.trailing}; |
2f1e8a71 | 272 | this.client.sendIrcCommand('topic', {server: this.con_num, nick: '', channel: command.params[1], topic: command.trailing}); |
a8bf3ea4 JA |
273 | }, |
274 | 'RPL_NOTOPIC': function (command) { | |
275 | /*command.server = this.con_num; | |
276 | command.command = 'RPL_NOTOPIC'; | |
2f1e8a71 D |
277 | this.client.sendIrcCommand(command);*/ |
278 | this.client.sendIrcCommand('topic', {server: this.con_num, nick: '', channel: command.params[1], topic: ''}); | |
a8bf3ea4 JA |
279 | }, |
280 | 'RPL_TOPICWHOTIME': function (command) { | |
281 | /*command.server = this.con_num; | |
282 | command.command = 'RPL_TOPICWHOTIME'; | |
2f1e8a71 | 283 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 284 | //{nick: nick, channel: channel, when: when}; |
2f1e8a71 | 285 | this.client.sendIrcCommand('topicsetby', {server: this.con_num, nick: command.params[2], channel: command.params[1], when: command.params[3]}); |
a8bf3ea4 JA |
286 | }, |
287 | 'PING': function (command) { | |
b8e4d9f7 | 288 | this.irc_connection.write('PONG ' + command.trailing); |
a8bf3ea4 JA |
289 | }, |
290 | 'JOIN': function (command) { | |
291 | var channel; | |
292 | if (typeof command.trailing === 'string' && command.trailing !== '') { | |
293 | channel = command.trailing; | |
294 | } else if (typeof command.params[0] === 'string' && command.params[0] !== '') { | |
295 | channel = command.params[0]; | |
296 | } | |
297 | /*command.server = this.con_num; | |
298 | command.command = 'JOIN'; | |
299 | command.params = [channel]; | |
2f1e8a71 | 300 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 301 | //websocket.sendClientEvent('join', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: channel}); |
2f1e8a71 | 302 | this.client.sendIrcCommand('join', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, channel: channel}); |
a8bf3ea4 JA |
303 | |
304 | if (command.nick === this.nick) { | |
b8e4d9f7 | 305 | this.irc_connection.write('NAMES ' + channel); |
a8bf3ea4 JA |
306 | } |
307 | }, | |
308 | 'PART': function (command) { | |
309 | /*command.server = this.con_num; | |
310 | command.command = 'PART'; | |
2f1e8a71 | 311 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 312 | //websocket.sendClientEvent('part', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), message: msg.trailing}); |
2f1e8a71 | 313 | this.client.sendIrcCommand('part', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], message: command.trailing}); |
a8bf3ea4 JA |
314 | }, |
315 | 'KICK': function (command) { | |
316 | /*command.server = this.con_num; | |
317 | command.command = 'KICK'; | |
2f1e8a71 | 318 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 319 | //websocket.sendClientEvent('kick', {kicked: params[1], nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: params[0].trim(), message: msg.trailing}); |
2f1e8a71 | 320 | this.client.sendIrcCommand('kick', {server: this.con_num, kicked: command.params[1], nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], message: command.trailing}); |
a8bf3ea4 JA |
321 | }, |
322 | 'QUIT': function (command) { | |
323 | /*command.server = this.con_num; | |
324 | command.command = 'QUIT'; | |
2f1e8a71 | 325 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 326 | //websocket.sendClientEvent('quit', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, message: msg.trailing}); |
2f1e8a71 | 327 | this.client.sendIrcCommand('quit', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, message: command.trailing}); |
a8bf3ea4 JA |
328 | }, |
329 | 'NOTICE': function (command) { | |
330 | /*command.server = this.con_num; | |
331 | command.command = 'NOTICE'; | |
2f1e8a71 | 332 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 JA |
333 | if ((command.trailing.charAt(0) === String.fromCharCode(1)) && (command.trailing.charAt(command.trailing.length - 1) === String.fromCharCode(1))) { |
334 | // It's a CTCP response | |
335 | //websocket.sendClientEvent('ctcp_response', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), msg: msg.trailing.substr(1, msg.trailing.length - 2)}); | |
2f1e8a71 | 336 | this.client.sendIrcCommand('ctcp_response', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], msg: command.trailing.substr(1, command.trailing.length - 2)}); |
a8bf3ea4 JA |
337 | } else { |
338 | //websocket.sendClientEvent('notice', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, target: msg.params.trim(), msg: msg.trailing}); | |
2f1e8a71 | 339 | this.client.sendIrcCommand('notice', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, target: command.params[0], msg: command.trailing}); |
a8bf3ea4 JA |
340 | } |
341 | }, | |
342 | 'NICK': function (command) { | |
343 | /*command.server = this.con_num; | |
344 | command.command = 'NICK'; | |
2f1e8a71 | 345 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 346 | //websocket.sendClientEvent('nick', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, newnick: msg.trailing}); |
2f1e8a71 | 347 | this.client.sendIrcCommand('nick', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, newnick: command.trailing}); |
a8bf3ea4 JA |
348 | }, |
349 | 'TOPIC': function (command) { | |
350 | /*command.server = this.con_num; | |
351 | command.command = 'TOPIC'; | |
2f1e8a71 | 352 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 353 | //{nick: msg.nick, channel: msg.params, topic: msg.trailing}; |
2f1e8a71 | 354 | this.client.sendIrcCommand('topic', {server: this.con_num, nick: command.nick, channel: command.params[0], topic: command.trailing}); |
a8bf3ea4 | 355 | }, |
703d2778 JA |
356 | 'MODE': function (command) { |
357 | var chanmodes = this.irc_connection.options.CHANMODES, | |
358 | prefixes = this.irc_connection.options.PREFIX, | |
359 | always_param = chanmodes[0].concat(chanmodes[1]), | |
360 | modes = [], | |
361 | has_param, i, j, add; | |
362 | ||
363 | prefixes = _.reduce(prefixes, function (list, prefix) { | |
364 | list.push(prefix.mode); | |
365 | return list; | |
366 | }, []); | |
367 | always_param = always_param.split('').concat(prefixes); | |
368 | ||
369 | has_param = function (mode, add) { | |
370 | if (_.find(always_param, function (m) { | |
371 | return m === mode; | |
372 | })) { | |
373 | return true; | |
374 | } else if (add && _.find(chanmodes[2].split(''), function (m) { | |
375 | return m === mode; | |
376 | })) { | |
377 | return true; | |
378 | } else { | |
379 | return false; | |
380 | } | |
381 | }; | |
382 | ||
383 | if (!command.params[1]) { | |
384 | command.params[1] = command.trailing; | |
a8bf3ea4 | 385 | } |
703d2778 JA |
386 | j = 0; |
387 | for (i = 0; i < command.params[1].length; i++) { | |
388 | switch (command.params[1][i]) { | |
389 | case '+': | |
390 | add = true; | |
391 | break; | |
392 | case '-': | |
393 | add = false; | |
394 | break; | |
395 | default: | |
396 | if (has_param(command.params[1][i], add)) { | |
397 | modes.push({mode: (add ? '+' : '-') + command.params[1][i], param: command.params[2 + j]}); | |
398 | j++; | |
399 | } else { | |
400 | modes.push({mode: (add ? '+' : '-') + command.params[1][i], param: null}); | |
401 | } | |
402 | } | |
403 | } | |
404 | ||
2f1e8a71 | 405 | this.client.sendIrcCommand('mode', { |
4e85992d D |
406 | server: this.con_num, |
407 | target: command.params[0], | |
408 | nick: command.nick || command.prefix || '', | |
409 | modes: modes | |
410 | }); | |
a8bf3ea4 JA |
411 | }, |
412 | 'PRIVMSG': function (command) { | |
413 | /*command.server = this.con_num; | |
414 | command.command = 'PRIVMSG'; | |
2f1e8a71 | 415 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 JA |
416 | var tmp, namespace; |
417 | if ((command.trailing.charAt(0) === String.fromCharCode(1)) && (command.trailing.charAt(command.trailing.length - 1) === String.fromCharCode(1))) { | |
418 | //CTCP request | |
419 | if (command.trailing.substr(1, 6) === 'ACTION') { | |
2f1e8a71 | 420 | this.client.sendIrcCommand('action', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], msg: command.trailing.substr(7, command.trailing.length - 2)}); |
a8bf3ea4 | 421 | } else if (command.trailing.substr(1, 4) === 'KIWI') { |
6d3dc969 | 422 | tmp = command.trailing.substr(6, command.trailing.length - 2); |
a8bf3ea4 | 423 | namespace = tmp.split(' ', 1)[0]; |
2f1e8a71 | 424 | this.client.sendIrcCommand('kiwi', {server: this.con_num, namespace: namespace, data: tmp.substr(namespace.length + 1)}); |
6d3dc969 | 425 | } else if (command.trailing.substr(1, 7) === 'VERSION') { |
b8e4d9f7 | 426 | this.irc_connection.write('NOTICE ' + command.nick + ' :' + String.fromCharCode(1) + 'VERSION KiwiIRC' + String.fromCharCode(1)); |
a8bf3ea4 | 427 | } else { |
2f1e8a71 | 428 | this.client.sendIrcCommand('ctcp_request', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], msg: command.trailing.substr(1, command.trailing.length - 2)}); |
a8bf3ea4 JA |
429 | } |
430 | } else { | |
431 | //{nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), msg: msg.trailing} | |
2f1e8a71 | 432 | this.client.sendIrcCommand('msg', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], msg: command.trailing}); |
a8bf3ea4 JA |
433 | } |
434 | }, | |
eec8c71e JA |
435 | 'CAP': function (command) { |
436 | // TODO: capability modifiers | |
437 | // i.e. - for disable, ~ for requires ACK, = for sticky | |
438 | var capabilities = command.trailing.replace(/[\-~=]/, '').split(' '); | |
439 | var want = ['multi-prefix']; | |
440 | var request; | |
441 | ||
442 | switch (command.params[1]) { | |
443 | case 'LS': | |
444 | request = _.intersection(capabilities, want); | |
445 | if (request.length > 0) { | |
446 | this.irc_connection.cap.requested = request; | |
447 | this.irc_connection.write('CAP REQ :' + request.join(' ')); | |
448 | } else { | |
449 | this.irc_connection.write('CAP END'); | |
450 | this.irc_connection.cap_negotation = false; | |
451 | } | |
452 | break; | |
453 | case 'ACK': | |
454 | if (capabilities.length > 0) { | |
455 | this.irc_connection.cap.enabled = capabilities; | |
456 | this.irc_connection.cap.requested = _.difference(this.irc_connection.cap.requested, capabilities); | |
457 | } | |
458 | if (this.irc_connection.cap.requested.length > 0) { | |
459 | this.irc_connection.write('CAP END'); | |
460 | this.irc_connection.cap_negotation = false; | |
461 | } | |
462 | break; | |
463 | case 'NAK': | |
464 | if (capabilities.length > 0) { | |
465 | this.irc_connection.cap.requested = _.difference(this.irc_connection.cap.requested, capabilities); | |
466 | } | |
467 | if (this.irc_connection.cap.requested.length > 0) { | |
468 | this.irc_connection.write('CAP END'); | |
469 | this.irc_connection.cap_negotation = false; | |
470 | } | |
471 | break; | |
472 | case 'LIST': | |
473 | // should we do anything here? | |
474 | break; | |
475 | } | |
476 | }, | |
a8bf3ea4 JA |
477 | 'ERROR': function (command) { |
478 | /*command.server = this.con_num; | |
479 | command.command = 'ERROR'; | |
2f1e8a71 | 480 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 481 | //websocket.sendClientEvent('irc_error', {error: 'error', reason: msg.trailing}); |
2f1e8a71 | 482 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'error', reason: command.trailing}); |
a8bf3ea4 JA |
483 | }, |
484 | ERR_LINKCHANNEL: function (command) { | |
485 | /*command.server = this.con_num; | |
486 | command.command = 'ERR_LINKCHANNEL'; | |
2f1e8a71 | 487 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 488 | //websocket.sendClientEvent('channel_redirect', {from: params[1], to: params[2]}); |
2f1e8a71 | 489 | this.client.sendIrcCommand('channel_redirect', {server: this.con_num, from: command.params[1], to: command.params[2]}); |
a8bf3ea4 JA |
490 | }, |
491 | ERR_NOSUCHNICK: function (command) { | |
492 | /*command.server = this.con_num; | |
493 | command.command = 'ERR_NOSUCHNICK'; | |
2f1e8a71 | 494 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 495 | //websocket.sendClientEvent('irc_error', {error: 'no_such_nick', nick: msg.params.split(" ")[1], reason: msg.trailing}); |
2f1e8a71 | 496 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'no_such_nick', nick: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
497 | }, |
498 | ERR_CANNOTSENDTOCHAN: function (command) { | |
499 | /*command.server = this.con_num; | |
500 | command.command = 'ERR_CANNOTSENDTOCHAN'; | |
2f1e8a71 | 501 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 502 | //websocket.sendClientEvent('irc_error', {error: 'cannot_send_to_chan', channel: msg.params.split(" ")[1], reason: msg.trailing}); |
2f1e8a71 | 503 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'cannot_send_to_chan', channel: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
504 | }, |
505 | ERR_TOOMANYCHANNELS: function (command) { | |
506 | /*command.server = this.con_num; | |
507 | command.command = 'ERR_TOOMANYCHANNELS'; | |
2f1e8a71 | 508 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 509 | //websocket.sendClientEvent('irc_error', {error: 'too_many_channels', channel: msg.params.split(" ")[1], reason: msg.trailing}); |
2f1e8a71 | 510 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'too_many_channels', channel: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
511 | }, |
512 | ERR_USERNOTINCHANNEL: function (command) { | |
513 | /*command.server = this.con_num; | |
514 | command.command = 'ERR_USERNOTINCHANNEL'; | |
2f1e8a71 | 515 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 516 | //websocket.sendClientEvent('irc_error', {error: 'user_not_in_channel', nick: params[0], channel: params[1], reason: msg.trainling}); |
2f1e8a71 | 517 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'user_not_in_channel', nick: command.params[0], channel: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
518 | }, |
519 | ERR_NOTONCHANNEL: function (command) { | |
520 | /*command.server = this.con_num; | |
521 | command.command = 'ERR_NOTONCHANNEL'; | |
2f1e8a71 | 522 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 523 | //websocket.sendClientEvent('irc_error', {error: 'not_on_channel', channel: msg.params.split(" ")[1], reason: msg.trailing}); |
2f1e8a71 | 524 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'not_on_channel', channel: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
525 | }, |
526 | ERR_CHANNELISFULL: function (command) { | |
527 | /*command.server = this.con_num; | |
528 | command.command = 'ERR_CHANNELISFULL'; | |
2f1e8a71 | 529 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 530 | //websocket.sendClientEvent('irc_error', {error: 'channel_is_full', channel: msg.params.split(" ")[1], reason: msg.trailing}); |
2f1e8a71 | 531 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'channel_is_full', channel: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
532 | }, |
533 | ERR_INVITEONLYCHAN: function (command) { | |
534 | /*command.server = this.con_num; | |
535 | command.command = 'ERR_INVITEONLYCHAN'; | |
2f1e8a71 | 536 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 537 | //websocket.sendClientEvent('irc_error', {error: 'invite_only_channel', channel: msg.params.split(" ")[1], reason: msg.trailing}); |
2f1e8a71 | 538 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'invite_only_channel', channel: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
539 | }, |
540 | ERR_BANNEDFROMCHAN: function (command) { | |
541 | /*command.server = this.con_num; | |
542 | command.command = 'ERR_BANNEDFROMCHAN'; | |
2f1e8a71 | 543 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 544 | //websocket.sendClientEvent('irc_error', {error: 'banned_from_channel', channel: msg.params.split(" ")[1], reason: msg.trailing}); |
2f1e8a71 | 545 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'banned_from_channel', channel: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
546 | }, |
547 | ERR_BADCHANNELKEY: function (command) { | |
548 | /*command.server = this.con_num; | |
549 | command.command = 'ERR_BADCHANNELKEY'; | |
2f1e8a71 | 550 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 551 | //websocket.sendClientEvent('irc_error', {error: 'bad_channel_key', channel: msg.params.split(" ")[1], reason: msg.trailing}); |
2f1e8a71 | 552 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'bad_channel_key', channel: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
553 | }, |
554 | ERR_CHANOPRIVSNEEDED: function (command) { | |
555 | /*command.server = this.con_num; | |
556 | command.command = 'ERR_CHANOPRIVSNEEDED'; | |
2f1e8a71 | 557 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 558 | //websocket.sendClientEvent('irc_error', {error: 'chanop_privs_needed', channel: msg.params.split(" ")[1], reason: msg.trailing}); |
2f1e8a71 | 559 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'chanop_privs_needed', channel: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
560 | }, |
561 | ERR_NICKNAMEINUSE: function (command) { | |
562 | /*command.server = this.con_num; | |
563 | command.command = 'ERR_NICKNAMEINUSE'; | |
2f1e8a71 | 564 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 | 565 | //websocket.sendClientEvent('irc_error', {error: 'nickname_in_use', nick: _.last(msg.params.split(" ")), reason: msg.trailing}); |
2f1e8a71 | 566 | this.client.sendIrcCommand('irc_error', {server: this.con_num, error: 'nickname_in_use', nick: command.params[1], reason: command.trailing}); |
a8bf3ea4 JA |
567 | }, |
568 | ERR_NOTREGISTERED: function (command) { | |
569 | /*command.server = this.con_num; | |
570 | command.command = 'ERR_NOTREGISTERED'; | |
2f1e8a71 | 571 | this.client.sendIrcCommand(command);*/ |
a8bf3ea4 JA |
572 | } |
573 | }; |