a13771c05b1da31d5425ccafd281c20a21bcaeb5
1 _kiwi
.model
.Gateway = function () {
3 // Set to a reference to this object within initialize()
8 * The name of the network
14 * The address (URL) of the network
20 * The current nickname
26 * The channel prefix for this network
32 * The user prefixes for channel owner/admin/op/voice etc. on this network
35 user_prefixes
: ['~', '&', '@', '+'],
38 * The URL to the Kiwi server
41 kiwi_server
: '//kiwi',
44 * List of nicks we are ignoring
51 this.initialize = function () {
54 // For ease of access. The socket.io object
55 this.socket
= this.get('socket');
57 this.applyEventHandlers();
61 this.applyEventHandlers = function () {
63 kiwi.gateway.on('message:#channel', my_function);
64 kiwi.gateway.on('message:somenick', my_function);
66 kiwi.gateway.on('notice:#channel', my_function);
67 kiwi.gateway.on('action:somenick', my_function);
69 kiwi.gateway.on('join:#channel', my_function);
70 kiwi.gateway.on('part:#channel', my_function);
71 kiwi.gateway.on('quit', my_function);
75 // Some easier handler events
76 this.on('onmsg', function (event
) {
78 connection
= _kiwi
.app
.connections
.getByConnectionId(event
.server
),
79 is_pm
= (event
.channel
== connection
.get('nick'));
81 source
= is_pm
? event
.nick
: event
.channel
;
83 that
.trigger('message:' + source
, event
);
84 that
.trigger('message', event
);
87 that
.trigger('pm:' + source
, event
);
88 that
.trigger('pm', event
);
93 this.on('onnotice', function (event
) {
94 // The notice towards a channel or a query window?
95 var source
= event
.target
|| event
.nick
;
97 this.trigger('notice:' + source
, event
);
98 this.trigger('notice', event
);
102 this.on('onaction', function (event
) {
104 connection
= _kiwi
.app
.connections
.getByConnectionId(event
.server
),
105 is_pm
= (event
.channel
== connection
.get('nick'));
107 source
= is_pm
? event
.nick
: event
.channel
;
109 that
.trigger('action:' + source
, event
);
112 that
.trigger('action:' + source
, event
);
113 that
.trigger('action', event
);
118 this.on('ontopic', function (event
) {
119 that
.trigger('topic:' + event
.channel
, event
);
120 that
.trigger('topic', event
);
124 this.on('onjoin', function (event
) {
125 that
.trigger('join:' + event
.channel
, event
);
126 that
.trigger('join', event
);
134 * Connects to the server
135 * @param {Function} callback A callback function to be invoked once Kiwi's server has connected to the IRC server
137 this.connect = function (callback
) {
140 // Work out the resource URL for socket.io
141 if (_kiwi
.app
.get('base_path').substr(0, 1) === '/') {
142 resource
= _kiwi
.app
.get('base_path');
143 resource
= resource
.substr(1, resource
.length
-1);
144 resource
+= '/transport';
146 resource
= _kiwi
.app
.get('base_path') + '/transport';
149 this.socket
= io
.connect(this.get('kiwi_server'), {
150 'resource': resource
,
152 'try multiple transports': true,
153 'connect timeout': 3000,
154 'max reconnection attempts': 7,
155 'reconnection delay': 2000,
156 'sync disconnect on unload': false
158 this.socket
.on('connect_failed', function (reason
) {
159 this.socket
.disconnect();
160 this.trigger("connect_fail", {reason
: reason
});
163 this.socket
.on('error', function (e
) {
164 console
.log("_kiwi.gateway.socket.on('error')", {reason
: e
});
165 that
.trigger("connect_fail", {reason
: e
});
168 this.socket
.on('connecting', function (transport_type
) {
169 console
.log("_kiwi.gateway.socket.on('connecting')");
170 that
.trigger("connecting");
174 * Once connected to the kiwi server send the IRC connect command along
175 * with the IRC server details.
176 * A `connect` event is sent from the kiwi server once connected to the
177 * IRCD and the nick has been accepted.
179 this.socket
.on('connect', function () {
180 callback
&& callback();
182 this.emit('kiwi', {command: 'connect', nick: that.get('nick'), hostname: host, port: port, ssl: ssl, password:password}, function (err, server_num) {
184 that.server_num = server_num;
185 console.log("_kiwi.gateway.socket.on('connect')");
187 console.log("_kiwi.gateway.socket.on('error')", {reason: err});
194 this.socket
.on('too_many_connections', function () {
195 that
.trigger("connect_fail", {reason
: 'too_many_connections'});
198 this.socket
.on('irc', function (data
, callback
) {
199 that
.parse(data
.command
, data
.data
);
202 this.socket
.on('kiwi', function (data
, callback
) {
203 that
.parseKiwi(data
.command
, data
.data
);
206 this.socket
.on('disconnect', function () {
207 that
.trigger("disconnect", {});
208 console
.log("_kiwi.gateway.socket.on('disconnect')");
211 this.socket
.on('close', function () {
212 console
.log("_kiwi.gateway.socket.on('close')");
215 this.socket
.on('reconnecting', function (reconnectionDelay
, reconnectionAttempts
) {
216 console
.log("_kiwi.gateway.socket.on('reconnecting')");
217 that
.trigger("reconnecting", {delay
: reconnectionDelay
, attempts
: reconnectionAttempts
});
220 this.socket
.on('reconnect_failed', function () {
221 console
.log("_kiwi.gateway.socket.on('reconnect_failed')");
227 this.newConnection = function(connection_info
, callback_fn
) {
231 this.socket
.emit('kiwi', {command
: 'connect', nick
: h
.nick
, hostname
: h
.host
, port
: h
.port
, ssl
: h
.ssl
, password
: h
.password
}, function (err
, server_num
) {
235 if (!_kiwi
.app
.connections
.getByConnectionId(server_num
)){
236 connection
= new _kiwi
.model
.Network({connection_id
: server_num
});
237 _kiwi
.app
.connections
.add(connection
);
240 console
.log("_kiwi.gateway.socket.on('connect')");
241 callback_fn
&& callback_fn(err
, connection
);
244 console
.log("_kiwi.gateway.socket.on('error')", {reason
: err
});
245 callback_fn
&& callback_fn(err
);
250 this.isConnected = function () {
251 return this.socket
.socket
.connected
;
256 this.parseKiwi = function (command
, data
) {
257 this.trigger('kiwi:' + command
, data
);
258 this.trigger('kiwi', data
);
280 * Parses the response from the server
282 this.parse = function (command
, data
) {
283 //console.log('gateway event', command, data);
285 if (command
!== undefined) {
288 $.each(data
.options
, function (name
, value
) {
291 that
.set('channel_prefix', value
.join(''));
294 that
.set('name', value
);
297 that
.set('user_prefixes', value
);
301 that
.set('cap', data
.cap
);
306 if (_kiwi.gateway.onSync && _kiwi.gateway.syncing) {
307 _kiwi.gateway.syncing = false;
308 _kiwi.gateway.onSync(item);
314 this.emit('_kiwi.' + data
.namespace, data
.data
);
320 if (typeof data
.server
!== 'undefined') {
321 that
.trigger('connection:' + data
.server
.toString(), {
327 // Trigger the global events (Mainly legacy now)
328 that
.trigger('on' + command
, data
);
332 * Sends data to the server
334 * @param {Object} data The data to send
335 * @param {Function} callback A callback function
337 this.sendData = function (connection_id
, data
, callback
) {
338 if (typeof connection_id
=== 'undefined' || connection_id
=== null)
339 connection_id
= _kiwi
.app
.connections
.active_connection
.get('connection_id');
342 server
: connection_id
,
343 data
: JSON
.stringify(data
)
345 this.socket
.emit('irc', data_buffer
, callback
);
349 * Sends a PRIVMSG message
350 * @param {String} target The target of the message (e.g. a channel or nick)
351 * @param {String} msg The message to send
352 * @param {Function} callback A callback function
354 this.privmsg = function (connection_id
, target
, msg
, callback
) {
363 this.sendData(connection_id
, data
, callback
);
367 * Sends a NOTICE message
368 * @param {String} target The target of the message (e.g. a channel or nick)
369 * @param {String} msg The message to send
370 * @param {Function} callback A callback function
372 this.notice = function (connection_id
, target
, msg
, callback
) {
381 this.sendData(connection_id
, data
, callback
);
385 * Sends a CTCP message
386 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
387 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
388 * @param {String} target The target of the message, e.g a channel or nick
389 * @param {String} params Additional paramaters
390 * @param {Function} callback A callback function
392 this.ctcp = function (connection_id
, request
, type
, target
, params
, callback
) {
403 this.sendData(connection_id
, data
, callback
);
407 * @param {String} target The target of the message (e.g. a channel or nick)
408 * @param {String} msg The message to send
409 * @param {Function} callback A callback function
411 this.action = function (connection_id
, target
, msg
, callback
) {
412 this.ctcp(connection_id
, true, 'ACTION', target
, msg
, callback
);
417 * @param {String} channel The channel to join
418 * @param {String} key The key to the channel
419 * @param {Function} callback A callback function
421 this.join = function (connection_id
, channel
, key
, callback
) {
430 this.sendData(connection_id
, data
, callback
);
435 * @param {String} channel The channel to part
436 * @param {Function} callback A callback function
438 this.part = function (connection_id
, channel
, callback
) {
446 this.sendData(connection_id
, data
, callback
);
450 * Queries or modifies a channell topic
451 * @param {String} channel The channel to query or modify
452 * @param {String} new_topic The new topic to set
453 * @param {Function} callback A callback function
455 this.topic = function (connection_id
, channel
, new_topic
, callback
) {
464 this.sendData(connection_id
, data
, callback
);
468 * Kicks a user from a channel
469 * @param {String} channel The channel to kick the user from
470 * @param {String} nick The nick of the user to kick
471 * @param {String} reason The reason for kicking the user
472 * @param {Function} callback A callback function
474 this.kick = function (connection_id
, channel
, nick
, reason
, callback
) {
484 this.sendData(connection_id
, data
, callback
);
488 * Disconnects us from the server
489 * @param {String} msg The quit message to send to the IRC server
490 * @param {Function} callback A callback function
492 this.quit = function (connection_id
, msg
, callback
) {
501 this.sendData(connection_id
, data
, callback
);
505 * Sends a string unmodified to the IRC server
506 * @param {String} data The data to send to the IRC server
507 * @param {Function} callback A callback function
509 this.raw = function (connection_id
, data
, callback
) {
517 this.sendData(connection_id
, data
, callback
);
521 * Changes our nickname
522 * @param {String} new_nick Our new nickname
523 * @param {Function} callback A callback function
525 this.changeNick = function (connection_id
, new_nick
, callback
) {
533 this.sendData(connection_id
, data
, callback
);
537 * Sends ENCODING change request to server.
538 * @param {String} new_encoding The new proposed encode
539 * @param {Fucntion} callback A callback function
541 this.setEncoding = function (connection_id
, new_encoding
, callback
) {
545 encoding
: new_encoding
549 this.sendData(connection_id
, data
, callback
);
553 * Sends data to a fellow Kiwi IRC user
554 * @param {String} target The nick of the Kiwi IRC user to send to
555 * @param {String} data The data to send
556 * @param {Function} callback A callback function
558 this.kiwi = function (target
, data
, callback
) {
567 this.sendData(data
, callback
);
570 // Check a nick alongside our ignore list
571 this.isNickIgnored = function (nick
) {
572 var idx
, list
= this.get('ignore_list');
575 for (idx
= 0; idx
< list
.length
; idx
++) {
576 pattern
= list
[idx
].replace(/([.+^$[\]\\(){}|-])/g, "\\$1")
580 regex
= new RegExp(pattern
, 'i');
581 if (regex
.test(nick
)) return true;
588 return new (Backbone
.Model
.extend(this))(arguments
);