1 _kiwi
.model
.Gateway = function () {
3 // Set to a reference to this object within initialize()
6 this.initialize = function () {
9 // For ease of access. The socket.io object
10 this.socket
= this.get('socket');
12 this.applyEventHandlers();
14 // Used to check if a disconnection was unplanned
15 this.disconnect_requested
= false;
19 this.applyEventHandlers = function () {
21 kiwi.gateway.on('message:#channel', my_function);
22 kiwi.gateway.on('message:somenick', my_function);
24 kiwi.gateway.on('notice:#channel', my_function);
25 kiwi.gateway.on('action:somenick', my_function);
27 kiwi.gateway.on('join:#channel', my_function);
28 kiwi.gateway.on('part:#channel', my_function);
29 kiwi.gateway.on('quit', my_function);
33 // Some easier handler events
34 this.on('onmsg', function (event
) {
36 connection
= _kiwi
.app
.connections
.getByConnectionId(event
.server
),
37 is_pm
= (event
.channel
.toLowerCase() == connection
.get('nick').toLowerCase());
39 source
= is_pm
? event
.nick
: event
.channel
;
41 that
.trigger('message:' + source
, event
);
42 that
.trigger('message', event
);
45 that
.trigger('pm:' + source
, event
);
46 that
.trigger('pm', event
);
51 this.on('onnotice', function (event
) {
52 // The notice towards a channel or a query window?
53 var source
= event
.target
|| event
.nick
;
55 this.trigger('notice:' + source
, event
);
56 this.trigger('notice', event
);
60 this.on('onaction', function (event
) {
62 connection
= _kiwi
.app
.connections
.getByConnectionId(event
.server
),
63 is_pm
= (event
.channel
.toLowerCase() == connection
.get('nick').toLowerCase());
65 source
= is_pm
? event
.nick
: event
.channel
;
67 that
.trigger('action:' + source
, event
);
70 that
.trigger('action:' + source
, event
);
71 that
.trigger('action', event
);
76 this.on('ontopic', function (event
) {
77 that
.trigger('topic:' + event
.channel
, event
);
78 that
.trigger('topic', event
);
82 this.on('onjoin', function (event
) {
83 that
.trigger('join:' + event
.channel
, event
);
84 that
.trigger('join', event
);
91 this.reconnect = function (callback
) {
95 this.disconnect_requested
= true;
99 this.connect(callback
);
105 * Connects to the server
106 * @param {Function} callback A callback function to be invoked once Kiwi's server has connected to the IRC server
108 this.connect = function (callback
) {
109 this.connect_callback
= callback
;
111 // Keep note of the server we are connecting to
112 this.set('kiwi_server', _kiwi
.app
.kiwi_server
);
114 this.socket
= new EngineioTools
.ReconnectingSocket(this.get('kiwi_server'), {
115 path
: _kiwi
.app
.get('base_path') + '/transport',
116 reconnect_max_attempts
: 5,
117 reconnect_delay
: 2000
120 this.rpc
= new EngineioTools
.Rpc(this.socket
);
122 this.socket
.on('connect_failed', function (reason
) {
123 this.socket
.disconnect();
124 this.trigger("connect_fail", {reason
: reason
});
127 this.socket
.on('error', function (e
) {
128 console
.log("_kiwi.gateway.socket.on('error')", {reason
: e
});
129 if (that
.connect_callback
) {
130 that
.connect_callback(e
);
131 delete that
.connect_callback
;
134 that
.trigger("connect_fail", {reason
: e
});
137 this.socket
.on('connecting', function (transport_type
) {
138 console
.log("_kiwi.gateway.socket.on('connecting')");
139 that
.trigger("connecting");
143 * Once connected to the kiwi server send the IRC connect command along
144 * with the IRC server details.
145 * A `connect` event is sent from the kiwi server once connected to the
146 * IRCD and the nick has been accepted.
148 this.socket
.on('open', function () {
149 // Reset the disconnect_requested flag
150 that
.disconnect_requested
= false;
152 console
.log("_kiwi.gateway.socket.on('open')");
155 this.rpc
.on('too_many_connections', function () {
156 that
.trigger("connect_fail", {reason
: 'too_many_connections'});
159 this.rpc
.on('irc', function (response
, data
) {
160 that
.parse(data
.command
, data
.data
);
163 this.rpc
.on('kiwi', function (response
, data
) {
164 that
.parseKiwi(data
.command
, data
.data
);
167 this.socket
.on('close', function () {
168 that
.trigger("disconnect", {});
169 console
.log("_kiwi.gateway.socket.on('close')");
172 this.socket
.on('reconnecting', function (status
) {
173 console
.log("_kiwi.gateway.socket.on('reconnecting')");
174 that
.trigger("reconnecting", {delay
: status
.delay
, attempts
: status
.attempts
});
177 this.socket
.on('reconnecting_failed', function () {
178 console
.log("_kiwi.gateway.socket.on('reconnect_failed')");
184 * Return a new network object with the new connection details
186 this.newConnection = function(connection_info
, callback_fn
) {
189 // If not connected, connect first then re-call this function
190 if (!this.isConnected()) {
191 this.connect(function(err
) {
197 that
.newConnection(connection_info
, callback_fn
);
203 this.makeIrcConnection(connection_info
, function(err
, server_num
) {
207 if (!_kiwi
.app
.connections
.getByConnectionId(server_num
)){
209 connection_id
: server_num
,
210 nick
: connection_info
.nick
,
211 address
: connection_info
.host
,
212 port
: connection_info
.port
,
213 ssl
: connection_info
.ssl
,
214 password
: connection_info
.password
216 connection
= new _kiwi
.model
.Network(inf
);
217 _kiwi
.app
.connections
.add(connection
);
220 console
.log("_kiwi.gateway.socket.on('connect')", connection
);
221 callback_fn
&& callback_fn(err
, connection
);
224 console
.log("_kiwi.gateway.socket.on('error')", {reason
: err
});
225 callback_fn
&& callback_fn(err
);
232 * Make a new IRC connection and return its connection ID
234 this.makeIrcConnection = function(connection_info
, callback_fn
) {
237 nick
: connection_info
.nick
,
238 hostname
: connection_info
.host
,
239 port
: connection_info
.port
,
240 ssl
: connection_info
.ssl
,
241 password
: connection_info
.password
244 connection_info
.options
= connection_info
.options
|| {};
246 // A few optional parameters
247 if (connection_info
.options
.encoding
)
248 server_info
.encoding
= connection_info
.options
.encoding
;
250 this.rpc
.call('kiwi', server_info
, function (err
, server_num
) {
252 callback_fn
&& callback_fn(err
, server_num
);
255 callback_fn
&& callback_fn(err
);
261 this.isConnected = function () {
262 // TODO: Check this. Might want to use .readyState
268 this.parseKiwi = function (command
, data
) {
269 var client_info_data
;
271 this.trigger('kiwi:' + command
, data
);
272 this.trigger('kiwi', data
);
276 // Send some info on this client to the server
278 command
: 'client_info',
279 build_version
: _kiwi
.global
.build_version
281 this.rpc
.call('kiwi', client_info_data
);
283 this.connect_callback
&& this.connect_callback();
284 delete this.connect_callback
;
309 * Parses the response from the server
311 this.parse = function (command
, data
) {
312 //console.log('gateway event', command, data);
314 if (command
!== undefined) {
317 $.each(data
.options
, function (name
, value
) {
320 that
.set('channel_prefix', value
.join(''));
323 that
.set('name', value
);
326 that
.set('user_prefixes', value
);
330 that
.set('cap', data
.cap
);
335 if (_kiwi.gateway.onSync && _kiwi.gateway.syncing) {
336 _kiwi.gateway.syncing = false;
337 _kiwi.gateway.onSync(item);
343 this.emit('_kiwi.' + data
.namespace, data
.data
);
349 if (typeof data
.server
!== 'undefined') {
350 that
.trigger('connection:' + data
.server
.toString(), {
356 // Trigger the global events (Mainly legacy now)
357 that
.trigger('on' + command
, data
);
361 * Sends data to the server
363 * @param {Object} data The data to send
364 * @param {Function} callback A callback function
366 this.sendData = function (connection_id
, data
, callback
) {
367 if (typeof connection_id
=== 'undefined' || connection_id
=== null)
368 connection_id
= _kiwi
.app
.connections
.active_connection
.get('connection_id');
371 server
: connection_id
,
372 data
: JSON
.stringify(data
)
374 this.rpc
.call('irc', data_buffer
, callback
);
378 * Sends a PRIVMSG message
379 * @param {String} target The target of the message (e.g. a channel or nick)
380 * @param {String} msg The message to send
381 * @param {Function} callback A callback function
383 this.privmsg = function (connection_id
, target
, msg
, callback
) {
392 this.sendData(connection_id
, data
, callback
);
396 * Sends a NOTICE message
397 * @param {String} target The target of the message (e.g. a channel or nick)
398 * @param {String} msg The message to send
399 * @param {Function} callback A callback function
401 this.notice = function (connection_id
, target
, msg
, callback
) {
410 this.sendData(connection_id
, data
, callback
);
414 * Sends a CTCP message
415 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
416 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
417 * @param {String} target The target of the message, e.g a channel or nick
418 * @param {String} params Additional paramaters
419 * @param {Function} callback A callback function
421 this.ctcp = function (connection_id
, request
, type
, target
, params
, callback
) {
432 this.sendData(connection_id
, data
, callback
);
436 * @param {String} target The target of the message (e.g. a channel or nick)
437 * @param {String} msg The message to send
438 * @param {Function} callback A callback function
440 this.action = function (connection_id
, target
, msg
, callback
) {
441 this.ctcp(connection_id
, true, 'ACTION', target
, msg
, callback
);
446 * @param {String} channel The channel to join
447 * @param {String} key The key to the channel
448 * @param {Function} callback A callback function
450 this.join = function (connection_id
, channel
, key
, callback
) {
459 this.sendData(connection_id
, data
, callback
);
463 * Retrieves channel information
465 this.channelInfo = function (connection_id
, channel
, callback
) {
467 method
: 'channel_info',
473 this.sendData(connection_id
, data
, callback
);
478 * @param {String} channel The channel to part
479 * @param {Function} callback A callback function
481 this.part = function (connection_id
, channel
, callback
) {
489 this.sendData(connection_id
, data
, callback
);
493 * Queries or modifies a channell topic
494 * @param {String} channel The channel to query or modify
495 * @param {String} new_topic The new topic to set
496 * @param {Function} callback A callback function
498 this.topic = function (connection_id
, channel
, new_topic
, callback
) {
507 this.sendData(connection_id
, data
, callback
);
511 * Kicks a user from a channel
512 * @param {String} channel The channel to kick the user from
513 * @param {String} nick The nick of the user to kick
514 * @param {String} reason The reason for kicking the user
515 * @param {Function} callback A callback function
517 this.kick = function (connection_id
, channel
, nick
, reason
, callback
) {
527 this.sendData(connection_id
, data
, callback
);
531 * Disconnects us from the server
532 * @param {String} msg The quit message to send to the IRC server
533 * @param {Function} callback A callback function
535 this.quit = function (connection_id
, msg
, callback
) {
544 this.sendData(connection_id
, data
, callback
);
548 * Sends a string unmodified to the IRC server
549 * @param {String} data The data to send to the IRC server
550 * @param {Function} callback A callback function
552 this.raw = function (connection_id
, data
, callback
) {
560 this.sendData(connection_id
, data
, callback
);
564 * Changes our nickname
565 * @param {String} new_nick Our new nickname
566 * @param {Function} callback A callback function
568 this.changeNick = function (connection_id
, new_nick
, callback
) {
576 this.sendData(connection_id
, data
, callback
);
580 * Sets a mode for a target
582 this.mode = function (connection_id
, target
, mode_string
, callback
) {
586 data
: 'MODE ' + target
+ ' ' + mode_string
590 this.sendData(connection_id
, data
, callback
);
595 * Sends ENCODING change request to server.
596 * @param {String} new_encoding The new proposed encode
597 * @param {Fucntion} callback A callback function
599 this.setEncoding = function (connection_id
, new_encoding
, callback
) {
603 encoding
: new_encoding
606 this.sendData(connection_id
, data
, callback
);
610 * Sends data to a fellow Kiwi IRC user
611 * @param {String} target The nick of the Kiwi IRC user to send to
612 * @param {String} data The data to send
613 * @param {Function} callback A callback function
615 this.kiwi = function (target
, data
, callback
) {
624 this.sendData(data
, callback
);
628 return new (Backbone
.Model
.extend(this))(arguments
);