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 // Used to check if a disconnection was unplanned
13 this.disconnect_requested
= false;
18 this.reconnect = function (callback
) {
22 this.disconnect_requested
= true;
26 this.connect(callback
);
32 * Connects to the server
33 * @param {Function} callback A callback function to be invoked once Kiwi's server has connected to the IRC server
35 this.connect = function (callback
) {
36 this.connect_callback
= callback
;
38 // Keep note of the server we are connecting to
39 this.set('kiwi_server', _kiwi
.app
.kiwi_server
);
41 this.socket
= new EngineioTools
.ReconnectingSocket(this.get('kiwi_server'), {
42 transports
: _kiwi
.app
.server_settings
.transports
|| ['websocket', 'polling'],
43 path
: _kiwi
.app
.get('base_path') + '/transport',
44 reconnect_max_attempts
: 5,
48 this.rpc
= new EngineioTools
.Rpc(this.socket
);
50 this.socket
.on('connect_failed', function (reason
) {
51 this.socket
.disconnect();
52 this.trigger("connect_fail", {reason
: reason
});
55 this.socket
.on('error', function (e
) {
56 console
.log("_kiwi.gateway.socket.on('error')", {reason
: e
});
57 if (that
.connect_callback
) {
58 that
.connect_callback(e
);
59 delete that
.connect_callback
;
62 that
.trigger("connect_fail", {reason
: e
});
65 this.socket
.on('connecting', function (transport_type
) {
66 console
.log("_kiwi.gateway.socket.on('connecting')");
67 that
.trigger("connecting");
71 * Once connected to the kiwi server send the IRC connect command along
72 * with the IRC server details.
73 * A `connect` event is sent from the kiwi server once connected to the
74 * IRCD and the nick has been accepted.
76 this.socket
.on('open', function () {
77 // Reset the disconnect_requested flag
78 that
.disconnect_requested
= false;
80 console
.log("_kiwi.gateway.socket.on('open')");
83 this.rpc
.on('too_many_connections', function () {
84 that
.trigger("connect_fail", {reason
: 'too_many_connections'});
87 this.rpc
.on('irc', function (response
, data
) {
88 that
.parse(data
.command
, data
.data
);
91 this.rpc
.on('kiwi', function (response
, data
) {
92 that
.parseKiwi(data
.command
, data
.data
);
95 this.socket
.on('close', function () {
96 that
.trigger("disconnect", {});
97 console
.log("_kiwi.gateway.socket.on('close')");
100 this.socket
.on('reconnecting', function (status
) {
101 console
.log("_kiwi.gateway.socket.on('reconnecting')");
102 that
.trigger("reconnecting", {delay
: status
.delay
, attempts
: status
.attempts
});
105 this.socket
.on('reconnecting_failed', function () {
106 console
.log("_kiwi.gateway.socket.on('reconnect_failed')");
112 * Return a new network object with the new connection details
114 this.newConnection = function(connection_info
, callback_fn
) {
117 // If not connected, connect first then re-call this function
118 if (!this.isConnected()) {
119 this.connect(function(err
) {
125 that
.newConnection(connection_info
, callback_fn
);
131 this.makeIrcConnection(connection_info
, function(err
, server_num
) {
135 if (!_kiwi
.app
.connections
.getByConnectionId(server_num
)){
137 connection_id
: server_num
,
138 nick
: connection_info
.nick
,
139 address
: connection_info
.host
,
140 port
: connection_info
.port
,
141 ssl
: connection_info
.ssl
,
142 password
: connection_info
.password
144 connection
= new _kiwi
.model
.Network(inf
);
145 _kiwi
.app
.connections
.add(connection
);
148 console
.log("_kiwi.gateway.socket.on('connect')", connection
);
149 callback_fn
&& callback_fn(err
, connection
);
152 console
.log("_kiwi.gateway.socket.on('error')", {reason
: err
});
153 callback_fn
&& callback_fn(err
);
160 * Make a new IRC connection and return its connection ID
162 this.makeIrcConnection = function(connection_info
, callback_fn
) {
165 nick
: connection_info
.nick
,
166 hostname
: connection_info
.host
,
167 port
: connection_info
.port
,
168 ssl
: connection_info
.ssl
,
169 password
: connection_info
.password
172 connection_info
.options
= connection_info
.options
|| {};
174 // A few optional parameters
175 if (connection_info
.options
.encoding
)
176 server_info
.encoding
= connection_info
.options
.encoding
;
178 this.rpc
.call('kiwi', server_info
, function (err
, server_num
) {
180 callback_fn
&& callback_fn(err
, server_num
);
183 callback_fn
&& callback_fn(err
);
189 this.isConnected = function () {
190 // TODO: Check this. Might want to use .readyState
196 this.parseKiwi = function (command
, data
) {
197 var client_info_data
;
199 this.trigger('kiwi:' + command
, data
);
200 this.trigger('kiwi', data
);
204 // Send some info on this client to the server
206 command
: 'client_info',
207 build_version
: _kiwi
.global
.build_version
209 this.rpc
.call('kiwi', client_info_data
);
211 this.connect_callback
&& this.connect_callback();
212 delete this.connect_callback
;
237 * Parses the response from the server
239 this.parse = function (command
, data
) {
240 //console.log('gateway event', command, data);
242 if (command
!== undefined) {
245 $.each(data
.options
, function (name
, value
) {
248 that
.set('channel_prefix', value
.join(''));
251 that
.set('name', value
);
254 that
.set('user_prefixes', value
);
258 that
.set('cap', data
.cap
);
263 if (_kiwi.gateway.onSync && _kiwi.gateway.syncing) {
264 _kiwi.gateway.syncing = false;
265 _kiwi.gateway.onSync(item);
271 this.emit('_kiwi.' + data
.namespace, data
.data
);
277 // Trigger the connection specific events (used by Network objects)
278 if (typeof data
.server
!== 'undefined') {
279 that
.trigger('connection:' + data
.server
.toString(), {
285 // Trigger the global events
286 that
.trigger(command
, data
);
290 * Sends data to the server
292 * @param {Object} data The data to send
293 * @param {Function} callback A callback function
295 this.sendData = function (connection_id
, data
, callback
) {
296 if (typeof connection_id
=== 'undefined' || connection_id
=== null)
297 connection_id
= _kiwi
.app
.connections
.active_connection
.get('connection_id');
300 server
: connection_id
,
301 data
: JSON
.stringify(data
)
303 this.rpc
.call('irc', data_buffer
, callback
);
307 * Sends a PRIVMSG message
308 * @param {String} target The target of the message (e.g. a channel or nick)
309 * @param {String} msg The message to send
310 * @param {Function} callback A callback function
312 this.privmsg = function (connection_id
, target
, msg
, callback
) {
321 this.sendData(connection_id
, data
, callback
);
325 * Sends a NOTICE message
326 * @param {String} target The target of the message (e.g. a channel or nick)
327 * @param {String} msg The message to send
328 * @param {Function} callback A callback function
330 this.notice = function (connection_id
, target
, msg
, callback
) {
339 this.sendData(connection_id
, data
, callback
);
343 * Sends a CTCP message
344 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
345 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
346 * @param {String} target The target of the message, e.g a channel or nick
347 * @param {String} params Additional paramaters
348 * @param {Function} callback A callback function
350 this.ctcp = function (connection_id
, request
, type
, target
, params
, callback
) {
361 this.sendData(connection_id
, data
, callback
);
365 * @param {String} target The target of the message (e.g. a channel or nick)
366 * @param {String} msg The message to send
367 * @param {Function} callback A callback function
369 this.action = function (connection_id
, target
, msg
, callback
) {
370 this.ctcp(connection_id
, true, 'ACTION', target
, msg
, callback
);
375 * @param {String} channel The channel to join
376 * @param {String} key The key to the channel
377 * @param {Function} callback A callback function
379 this.join = function (connection_id
, channel
, key
, callback
) {
388 this.sendData(connection_id
, data
, callback
);
392 * Retrieves channel information
394 this.channelInfo = function (connection_id
, channel
, callback
) {
396 method
: 'channel_info',
402 this.sendData(connection_id
, data
, callback
);
407 * @param {String} channel The channel to part
408 * @param {Function} callback A callback function
410 this.part = function (connection_id
, channel
, callback
) {
418 this.sendData(connection_id
, data
, callback
);
422 * Queries or modifies a channell topic
423 * @param {String} channel The channel to query or modify
424 * @param {String} new_topic The new topic to set
425 * @param {Function} callback A callback function
427 this.topic = function (connection_id
, channel
, new_topic
, callback
) {
436 this.sendData(connection_id
, data
, callback
);
440 * Kicks a user from a channel
441 * @param {String} channel The channel to kick the user from
442 * @param {String} nick The nick of the user to kick
443 * @param {String} reason The reason for kicking the user
444 * @param {Function} callback A callback function
446 this.kick = function (connection_id
, channel
, nick
, reason
, callback
) {
456 this.sendData(connection_id
, data
, callback
);
460 * Disconnects us from the server
461 * @param {String} msg The quit message to send to the IRC server
462 * @param {Function} callback A callback function
464 this.quit = function (connection_id
, msg
, callback
) {
473 this.sendData(connection_id
, data
, callback
);
477 * Sends a string unmodified to the IRC server
478 * @param {String} data The data to send to the IRC server
479 * @param {Function} callback A callback function
481 this.raw = function (connection_id
, data
, callback
) {
489 this.sendData(connection_id
, data
, callback
);
493 * Changes our nickname
494 * @param {String} new_nick Our new nickname
495 * @param {Function} callback A callback function
497 this.changeNick = function (connection_id
, new_nick
, callback
) {
505 this.sendData(connection_id
, data
, callback
);
509 * Sets a mode for a target
511 this.mode = function (connection_id
, target
, mode_string
, callback
) {
515 data
: 'MODE ' + target
+ ' ' + mode_string
519 this.sendData(connection_id
, data
, callback
);
524 * Sends ENCODING change request to server.
525 * @param {String} new_encoding The new proposed encode
526 * @param {Fucntion} callback A callback function
528 this.setEncoding = function (connection_id
, new_encoding
, callback
) {
532 encoding
: new_encoding
535 this.sendData(connection_id
, data
, callback
);
539 * Sends data to a fellow Kiwi IRC user
540 * @param {String} target The nick of the Kiwi IRC user to send to
541 * @param {String} data The data to send
542 * @param {Function} callback A callback function
544 this.kiwi = function (target
, data
, callback
) {
553 this.sendData(data
, callback
);
557 return new (Backbone
.Model
.extend(this))(arguments
);