Irc component renamed; Kiwi network events;
[KiwiIRC.git] / client / assets / dev / model_gateway.js
1 _kiwi.model.Gateway = function () {
2
3 // Set to a reference to this object within initialize()
4 var that = null;
5
6 this.defaults = {
7 /**
8 * The name of the network
9 * @type String
10 */
11 name: 'Server',
12
13 /**
14 * The address (URL) of the network
15 * @type String
16 */
17 address: '',
18
19 /**
20 * The current nickname
21 * @type String
22 */
23 nick: '',
24
25 /**
26 * The channel prefix for this network
27 * @type String
28 */
29 channel_prefix: '#',
30
31 /**
32 * The user prefixes for channel owner/admin/op/voice etc. on this network
33 * @type Array
34 */
35 user_prefixes: ['~', '&', '@', '+'],
36
37 /**
38 * The URL to the Kiwi server
39 * @type String
40 */
41 kiwi_server: '//kiwi'
42 };
43
44
45 this.initialize = function () {
46 that = this;
47
48 // For ease of access. The socket.io object
49 this.socket = this.get('socket');
50
51 this.applyEventHandlers();
52 };
53
54
55 this.applyEventHandlers = function () {
56 /*
57 kiwi.gateway.on('message:#channel', my_function);
58 kiwi.gateway.on('message:somenick', my_function);
59
60 kiwi.gateway.on('notice:#channel', my_function);
61 kiwi.gateway.on('action:somenick', my_function);
62
63 kiwi.gateway.on('join:#channel', my_function);
64 kiwi.gateway.on('part:#channel', my_function);
65 kiwi.gateway.on('quit', my_function);
66 */
67 var that = this;
68
69 // Some easier handler events
70 this.on('onmsg', function (event) {
71 var source,
72 is_pm = (event.channel == that.get('nick'));
73
74 source = is_pm ? event.nick : event.channel;
75
76 that.trigger('message:' + source, event);
77 that.trigger('message', event);
78
79 if (is_pm) {
80 that.trigger('pm:' + source, event);
81 that.trigger('pm', event);
82 }
83 }, this);
84
85
86 this.on('onnotice', function (event) {
87 // The notice towards a channel or a query window?
88 var source = event.target || event.nick;
89
90 this.trigger('notice:' + source, event);
91 this.trigger('notice', event);
92 }, this);
93
94
95 this.on('onaction', function (event) {
96 var source,
97 is_pm = (event.channel == that.get('nick'));
98
99 source = is_pm ? event.nick : event.channel;
100
101 that.trigger('action:' + source, event);
102
103 if (is_pm) {
104 that.trigger('action:' + source, event);
105 that.trigger('action', event);
106 }
107 }, this);
108
109
110 this.on('ontopic', function (event) {
111 that.trigger('topic:' + event.channel, event);
112 that.trigger('topic', event);
113 });
114 };
115
116
117
118 /**
119 * Connects to the server
120 * @param {String} host The hostname or IP address of the IRC server to connect to
121 * @param {Number} port The port of the IRC server to connect to
122 * @param {Boolean} ssl Whether or not to connect to the IRC server using SSL
123 * @param {String} password The password to supply to the IRC server during registration
124 * @param {Function} callback A callback function to be invoked once Kiwi's server has connected to the IRC server
125 */
126 this.connect = function (host, port, ssl, password, callback) {
127 var resource;
128
129 // Work out the resource URL for socket.io
130 if (_kiwi.app.get('base_path').substr(0, 1) === '/') {
131 resource = _kiwi.app.get('base_path');
132 resource = resource.substr(1, resource.length-1);
133 resource += '/transport';
134 } else {
135 resource = _kiwi.app.get('base_path') + '/transport';
136 }
137
138 this.socket = io.connect(this.get('kiwi_server'), {
139 'resource': resource,
140
141 'try multiple transports': true,
142 'connect timeout': 3000,
143 'max reconnection attempts': 7,
144 'reconnection delay': 2000,
145 'sync disconnect on unload': false
146 });
147 this.socket.on('connect_failed', function (reason) {
148 this.socket.disconnect();
149 this.trigger("connect_fail", {reason: reason});
150 });
151
152 this.socket.on('error', function (e) {
153 console.log("_kiwi.gateway.socket.on('error')", {reason: e});
154 that.trigger("connect_fail", {reason: e});
155 });
156
157 this.socket.on('connecting', function (transport_type) {
158 console.log("_kiwi.gateway.socket.on('connecting')");
159 that.trigger("connecting");
160 });
161
162 /**
163 * Once connected to the kiwi server send the IRC connect command along
164 * with the IRC server details.
165 * A `connect` event is sent from the kiwi server once connected to the
166 * IRCD and the nick has been accepted.
167 */
168 this.socket.on('connect', function () {
169 this.emit('kiwi', {command: 'connect', nick: that.get('nick'), hostname: host, port: port, ssl: ssl, password:password}, function (err, server_num) {
170 if (!err) {
171 that.server_num = server_num;
172 console.log("_kiwi.gateway.socket.on('connect')");
173 } else {
174 console.log("_kiwi.gateway.socket.on('error')", {reason: err});
175 callback(err);
176 }
177 });
178 });
179
180 this.socket.on('too_many_connections', function () {
181 that.trigger("connect_fail", {reason: 'too_many_connections'});
182 });
183
184 this.socket.on('irc', function (data, callback) {
185 that.parse(data.command, data.data);
186 });
187
188 this.socket.on('kiwi', function (data, callback) {
189 that.parseKiwi(data.command, data.data);
190 });
191
192 this.socket.on('disconnect', function () {
193 that.trigger("disconnect", {});
194 console.log("_kiwi.gateway.socket.on('disconnect')");
195 });
196
197 this.socket.on('close', function () {
198 console.log("_kiwi.gateway.socket.on('close')");
199 });
200
201 this.socket.on('reconnecting', function (reconnectionDelay, reconnectionAttempts) {
202 console.log("_kiwi.gateway.socket.on('reconnecting')");
203 that.trigger("reconnecting", {delay: reconnectionDelay, attempts: reconnectionAttempts});
204 });
205
206 this.socket.on('reconnect_failed', function () {
207 console.log("_kiwi.gateway.socket.on('reconnect_failed')");
208 });
209 };
210
211
212
213 this.isConnected = function () {
214 return this.socket.socket.connected;
215 };
216
217
218
219 this.parseKiwi = function (command, data) {
220 this.trigger('kiwi:' + command, data);
221 this.trigger('kiwi', data);
222 };
223 /*
224 Events:
225 msg
226 action
227 server_connect
228 options
229 motd
230 notice
231 userlist
232 nick
233 join
234 topic
235 part
236 kick
237 quit
238 whois
239 syncchannel_redirect
240 debug
241 */
242 /**
243 * Parses the response from the server
244 */
245 this.parse = function (command, data) {
246 //console.log('gateway event', command, data);
247 if (command !== undefined) {
248 that.trigger('on' + command, data);
249
250 switch (command) {
251 case 'options':
252 $.each(data.options, function (name, value) {
253 switch (name) {
254 case 'CHANTYPES':
255 // TODO: Check this. Why is it only getting the first char?
256 that.set('channel_prefix', value.join('').charAt(0));
257 break;
258 case 'NETWORK':
259 that.set('name', value);
260 break;
261 case 'PREFIX':
262 that.set('user_prefixes', value);
263 break;
264 }
265 });
266 that.set('cap', data.cap);
267 break;
268
269 case 'connect':
270 that.set('nick', data.nick);
271 break;
272
273 case 'nick':
274 if (data.nick === that.get('nick')) {
275 that.set('nick', data.newnick);
276 }
277 break;
278 /*
279 case 'sync':
280 if (_kiwi.gateway.onSync && _kiwi.gateway.syncing) {
281 _kiwi.gateway.syncing = false;
282 _kiwi.gateway.onSync(item);
283 }
284 break;
285 */
286
287 case 'kiwi':
288 this.emit('_kiwi.' + data.namespace, data.data);
289 break;
290 }
291 }
292 };
293
294 /**
295 * Sends data to the server
296 * @private
297 * @param {Object} data The data to send
298 * @param {Function} callback A callback function
299 */
300 this.sendData = function (data, callback) {
301 this.socket.emit('irc', {server: 0, data: JSON.stringify(data)}, callback);
302 };
303
304 /**
305 * Sends a PRIVMSG message
306 * @param {String} target The target of the message (e.g. a channel or nick)
307 * @param {String} msg The message to send
308 * @param {Function} callback A callback function
309 */
310 this.privmsg = function (target, msg, callback) {
311 var data = {
312 method: 'privmsg',
313 args: {
314 target: target,
315 msg: msg
316 }
317 };
318
319 this.sendData(data, callback);
320 };
321
322 /**
323 * Sends a NOTICE message
324 * @param {String} target The target of the message (e.g. a channel or nick)
325 * @param {String} msg The message to send
326 * @param {Function} callback A callback function
327 */
328 this.notice = function (target, msg, callback) {
329 var data = {
330 method: 'notice',
331 args: {
332 target: target,
333 msg: msg
334 }
335 };
336
337 this.sendData(data, callback);
338 };
339
340 /**
341 * Sends a CTCP message
342 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
343 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
344 * @param {String} target The target of the message, e.g a channel or nick
345 * @param {String} params Additional paramaters
346 * @param {Function} callback A callback function
347 */
348 this.ctcp = function (request, type, target, params, callback) {
349 var data = {
350 method: 'ctcp',
351 args: {
352 request: request,
353 type: type,
354 target: target,
355 params: params
356 }
357 };
358
359 this.sendData(data, callback);
360 };
361
362 /**
363 * @param {String} target The target of the message (e.g. a channel or nick)
364 * @param {String} msg The message to send
365 * @param {Function} callback A callback function
366 */
367 this.action = function (target, msg, callback) {
368 this.ctcp(true, 'ACTION', target, msg, callback);
369 };
370
371 /**
372 * Joins a channel
373 * @param {String} channel The channel to join
374 * @param {String} key The key to the channel
375 * @param {Function} callback A callback function
376 */
377 this.join = function (channel, key, callback) {
378 var data = {
379 method: 'join',
380 args: {
381 channel: channel,
382 key: key
383 }
384 };
385
386 this.sendData(data, callback);
387 };
388
389 /**
390 * Leaves a channel
391 * @param {String} channel The channel to part
392 * @param {Function} callback A callback function
393 */
394 this.part = function (channel, callback) {
395 var data = {
396 method: 'part',
397 args: {
398 channel: channel
399 }
400 };
401
402 this.sendData(data, callback);
403 };
404
405 /**
406 * Queries or modifies a channell topic
407 * @param {String} channel The channel to query or modify
408 * @param {String} new_topic The new topic to set
409 * @param {Function} callback A callback function
410 */
411 this.topic = function (channel, new_topic, callback) {
412 var data = {
413 method: 'topic',
414 args: {
415 channel: channel,
416 topic: new_topic
417 }
418 };
419
420 this.sendData(data, callback);
421 };
422
423 /**
424 * Kicks a user from a channel
425 * @param {String} channel The channel to kick the user from
426 * @param {String} nick The nick of the user to kick
427 * @param {String} reason The reason for kicking the user
428 * @param {Function} callback A callback function
429 */
430 this.kick = function (channel, nick, reason, callback) {
431 var data = {
432 method: 'kick',
433 args: {
434 channel: channel,
435 nick: nick,
436 reason: reason
437 }
438 };
439
440 this.sendData(data, callback);
441 };
442
443 /**
444 * Disconnects us from the server
445 * @param {String} msg The quit message to send to the IRC server
446 * @param {Function} callback A callback function
447 */
448 this.quit = function (msg, callback) {
449 msg = msg || "";
450 var data = {
451 method: 'quit',
452 args: {
453 message: msg
454 }
455 };
456
457 this.sendData(data, callback);
458 };
459
460 /**
461 * Sends a string unmodified to the IRC server
462 * @param {String} data The data to send to the IRC server
463 * @param {Function} callback A callback function
464 */
465 this.raw = function (data, callback) {
466 data = {
467 method: 'raw',
468 args: {
469 data: data
470 }
471 };
472
473 this.sendData(data, callback);
474 };
475
476 /**
477 * Changes our nickname
478 * @param {String} new_nick Our new nickname
479 * @param {Function} callback A callback function
480 */
481 this.changeNick = function (new_nick, callback) {
482 var data = {
483 method: 'nick',
484 args: {
485 nick: new_nick
486 }
487 };
488
489 this.sendData(data, callback);
490 };
491
492 /**
493 * Sends data to a fellow Kiwi IRC user
494 * @param {String} target The nick of the Kiwi IRC user to send to
495 * @param {String} data The data to send
496 * @param {Function} callback A callback function
497 */
498 this.kiwi = function (target, data, callback) {
499 data = {
500 method: 'kiwi',
501 args: {
502 target: target,
503 data: data
504 }
505 };
506
507 this.sendData(data, callback);
508 };
509
510
511 return new (Backbone.Model.extend(this))(arguments);
512 };