9db724b248ef97fb9d2f1957209d222a9bdeca85
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 is_pm
= (event
.channel
== that
.get('nick'));
80 source
= is_pm
? event
.nick
: event
.channel
;
82 that
.trigger('message:' + source
, event
);
83 that
.trigger('message', event
);
86 that
.trigger('pm:' + source
, event
);
87 that
.trigger('pm', event
);
92 this.on('onnotice', function (event
) {
93 // The notice towards a channel or a query window?
94 var source
= event
.target
|| event
.nick
;
96 this.trigger('notice:' + source
, event
);
97 this.trigger('notice', event
);
101 this.on('onaction', function (event
) {
103 is_pm
= (event
.channel
== that
.get('nick'));
105 source
= is_pm
? event
.nick
: event
.channel
;
107 that
.trigger('action:' + source
, event
);
110 that
.trigger('action:' + source
, event
);
111 that
.trigger('action', event
);
116 this.on('ontopic', function (event
) {
117 that
.trigger('topic:' + event
.channel
, event
);
118 that
.trigger('topic', event
);
122 this.on('onjoin', function (event
) {
123 that
.trigger('join:' + event
.channel
, event
);
124 that
.trigger('join', event
);
132 * Connects to the server
133 * @param {String} host The hostname or IP address of the IRC server to connect to
134 * @param {Number} port The port of the IRC server to connect to
135 * @param {Boolean} ssl Whether or not to connect to the IRC server using SSL
136 * @param {String} password The password to supply to the IRC server during registration
137 * @param {Function} callback A callback function to be invoked once Kiwi's server has connected to the IRC server
139 this.connect = function (host
, port
, ssl
, password
, callback
) {
142 // Work out the resource URL for socket.io
143 if (_kiwi
.app
.get('base_path').substr(0, 1) === '/') {
144 resource
= _kiwi
.app
.get('base_path');
145 resource
= resource
.substr(1, resource
.length
-1);
146 resource
+= '/transport';
148 resource
= _kiwi
.app
.get('base_path') + '/transport';
151 this.socket
= io
.connect(this.get('kiwi_server'), {
152 'resource': resource
,
154 'try multiple transports': true,
155 'connect timeout': 3000,
156 'max reconnection attempts': 7,
157 'reconnection delay': 2000,
158 'sync disconnect on unload': false
160 this.socket
.on('connect_failed', function (reason
) {
161 this.socket
.disconnect();
162 this.trigger("connect_fail", {reason
: reason
});
165 this.socket
.on('error', function (e
) {
166 console
.log("_kiwi.gateway.socket.on('error')", {reason
: e
});
167 that
.trigger("connect_fail", {reason
: e
});
170 this.socket
.on('connecting', function (transport_type
) {
171 console
.log("_kiwi.gateway.socket.on('connecting')");
172 that
.trigger("connecting");
176 * Once connected to the kiwi server send the IRC connect command along
177 * with the IRC server details.
178 * A `connect` event is sent from the kiwi server once connected to the
179 * IRCD and the nick has been accepted.
181 this.socket
.on('connect', function () {
183 nick
: that
.get('nick'),
190 this.emit('kiwi', {command: 'connect', nick: that.get('nick'), hostname: host, port: port, ssl: ssl, password:password}, function (err, server_num) {
192 that.server_num = server_num;
193 console.log("_kiwi.gateway.socket.on('connect')");
195 console.log("_kiwi.gateway.socket.on('error')", {reason: err});
202 this.socket
.on('too_many_connections', function () {
203 that
.trigger("connect_fail", {reason
: 'too_many_connections'});
206 this.socket
.on('irc', function (data
, callback
) {
207 that
.parse(data
.command
, data
.data
);
210 this.socket
.on('kiwi', function (data
, callback
) {
211 that
.parseKiwi(data
.command
, data
.data
);
214 this.socket
.on('disconnect', function () {
215 that
.trigger("disconnect", {});
216 console
.log("_kiwi.gateway.socket.on('disconnect')");
219 this.socket
.on('close', function () {
220 console
.log("_kiwi.gateway.socket.on('close')");
223 this.socket
.on('reconnecting', function (reconnectionDelay
, reconnectionAttempts
) {
224 console
.log("_kiwi.gateway.socket.on('reconnecting')");
225 that
.trigger("reconnecting", {delay
: reconnectionDelay
, attempts
: reconnectionAttempts
});
228 this.socket
.on('reconnect_failed', function () {
229 console
.log("_kiwi.gateway.socket.on('reconnect_failed')");
235 this.newConnection = function(connection_info
, callback_fn
) {
239 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
) {
241 // TODO: Remove this whole premature connection thing when panel code is tidied
242 if (server_num
!= 0 && !_kiwi
.app
.connections
.getByConnectionId(server_num
)){
243 _kiwi
.app
.connections
.add(new _kiwi
.model
.Network({connection_id
: server_num
}));
246 console
.log("_kiwi.gateway.socket.on('connect')");
247 callback_fn
&& callback_fn(err
);
250 console
.log("_kiwi.gateway.socket.on('error')", {reason
: err
});
251 callback_fn
&& callback_fn(err
);
258 this.isConnected = function () {
259 return this.socket
.socket
.connected
;
264 this.parseKiwi = function (command
, data
) {
265 this.trigger('kiwi:' + command
, data
);
266 this.trigger('kiwi', data
);
288 * Parses the response from the server
290 this.parse = function (command
, data
) {
291 //console.log('gateway event', command, data);
293 if (command
!== undefined) {
294 that
.trigger('on' + command
, data
);
298 $.each(data
.options
, function (name
, value
) {
301 that
.set('channel_prefix', value
.join(''));
304 that
.set('name', value
);
307 that
.set('user_prefixes', value
);
311 that
.set('cap', data
.cap
);
315 that
.set('nick', data
.nick
);
319 if (data
.nick
=== that
.get('nick')) {
320 that
.set('nick', data
.newnick
);
325 if (_kiwi.gateway.onSync && _kiwi.gateway.syncing) {
326 _kiwi.gateway.syncing = false;
327 _kiwi.gateway.onSync(item);
333 this.emit('_kiwi.' + data
.namespace, data
.data
);
339 if (typeof data
.server
!== 'undefined') {
340 that
.trigger('connection:' + data
.server
.toString(), {
348 * Sends data to the server
350 * @param {Object} data The data to send
351 * @param {Function} callback A callback function
353 this.sendData = function (data
, callback
) {
355 server
: _kiwi
.app
.connections
.active_connection
.get('connection_id'),
356 data
: JSON
.stringify(data
)
359 this.socket
.emit('irc', data_buffer
, callback
);
363 * Sends a PRIVMSG message
364 * @param {String} target The target of the message (e.g. a channel or nick)
365 * @param {String} msg The message to send
366 * @param {Function} callback A callback function
368 this.privmsg = function (target
, msg
, callback
) {
377 this.sendData(data
, callback
);
381 * Sends a NOTICE message
382 * @param {String} target The target of the message (e.g. a channel or nick)
383 * @param {String} msg The message to send
384 * @param {Function} callback A callback function
386 this.notice = function (target
, msg
, callback
) {
395 this.sendData(data
, callback
);
399 * Sends a CTCP message
400 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
401 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
402 * @param {String} target The target of the message, e.g a channel or nick
403 * @param {String} params Additional paramaters
404 * @param {Function} callback A callback function
406 this.ctcp = function (request
, type
, target
, params
, callback
) {
417 this.sendData(data
, callback
);
421 * @param {String} target The target of the message (e.g. a channel or nick)
422 * @param {String} msg The message to send
423 * @param {Function} callback A callback function
425 this.action = function (target
, msg
, callback
) {
426 this.ctcp(true, 'ACTION', target
, msg
, callback
);
431 * @param {String} channel The channel to join
432 * @param {String} key The key to the channel
433 * @param {Function} callback A callback function
435 this.join = function (channel
, key
, callback
) {
444 this.sendData(data
, callback
);
449 * @param {String} channel The channel to part
450 * @param {Function} callback A callback function
452 this.part = function (channel
, callback
) {
460 this.sendData(data
, callback
);
464 * Queries or modifies a channell topic
465 * @param {String} channel The channel to query or modify
466 * @param {String} new_topic The new topic to set
467 * @param {Function} callback A callback function
469 this.topic = function (channel
, new_topic
, callback
) {
478 this.sendData(data
, callback
);
482 * Kicks a user from a channel
483 * @param {String} channel The channel to kick the user from
484 * @param {String} nick The nick of the user to kick
485 * @param {String} reason The reason for kicking the user
486 * @param {Function} callback A callback function
488 this.kick = function (channel
, nick
, reason
, callback
) {
498 this.sendData(data
, callback
);
502 * Disconnects us from the server
503 * @param {String} msg The quit message to send to the IRC server
504 * @param {Function} callback A callback function
506 this.quit = function (msg
, callback
) {
515 this.sendData(data
, callback
);
519 * Sends a string unmodified to the IRC server
520 * @param {String} data The data to send to the IRC server
521 * @param {Function} callback A callback function
523 this.raw = function (data
, callback
) {
531 this.sendData(data
, callback
);
535 * Changes our nickname
536 * @param {String} new_nick Our new nickname
537 * @param {Function} callback A callback function
539 this.changeNick = function (new_nick
, callback
) {
547 this.sendData(data
, callback
);
551 * Sends data to a fellow Kiwi IRC user
552 * @param {String} target The nick of the Kiwi IRC user to send to
553 * @param {String} data The data to send
554 * @param {Function} callback A callback function
556 this.kiwi = function (target
, data
, callback
) {
565 this.sendData(data
, callback
);
569 // Check a nick alongside our ignore list
570 this.isNickIgnored = function (nick
) {
571 var idx
, list
= this.get('ignore_list');
574 for (idx
= 0; idx
< list
.length
; idx
++) {
575 pattern
= list
[idx
].replace(/([.+^$[\]\\(){}|-])/g, "\\$1")
579 regex
= new RegExp(pattern
, 'i');
580 if (regex
.test(nick
)) return true;
587 return new (Backbone
.Model
.extend(this))(arguments
);