1a534c338508b44aee9704e0fd5795b00a4843ef
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 () {
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
});
193 this.socket
.on('too_many_connections', function () {
194 that
.trigger("connect_fail", {reason
: 'too_many_connections'});
197 this.socket
.on('irc', function (data
, callback
) {
198 that
.parse(data
.command
, data
.data
);
201 this.socket
.on('kiwi', function (data
, callback
) {
202 that
.parseKiwi(data
.command
, data
.data
);
205 this.socket
.on('disconnect', function () {
206 that
.trigger("disconnect", {});
207 console
.log("_kiwi.gateway.socket.on('disconnect')");
210 this.socket
.on('close', function () {
211 console
.log("_kiwi.gateway.socket.on('close')");
214 this.socket
.on('reconnecting', function (reconnectionDelay
, reconnectionAttempts
) {
215 console
.log("_kiwi.gateway.socket.on('reconnecting')");
216 that
.trigger("reconnecting", {delay
: reconnectionDelay
, attempts
: reconnectionAttempts
});
219 this.socket
.on('reconnect_failed', function () {
220 console
.log("_kiwi.gateway.socket.on('reconnect_failed')");
226 this.isConnected = function () {
227 return this.socket
.socket
.connected
;
232 this.parseKiwi = function (command
, data
) {
233 this.trigger('kiwi:' + command
, data
);
234 this.trigger('kiwi', data
);
256 * Parses the response from the server
258 this.parse = function (command
, data
) {
259 //console.log('gateway event', command, data);
261 if (command
!== undefined) {
262 that
.trigger('on' + command
, data
);
266 $.each(data
.options
, function (name
, value
) {
269 that
.set('channel_prefix', value
.join(''));
272 that
.set('name', value
);
275 that
.set('user_prefixes', value
);
279 that
.set('cap', data
.cap
);
283 if (!_kiwi
.app
.connections
.getByConnectionId(data
.server
)) {
284 _kiwi
.app
.connections
.add(new _kiwi
.model
.Network({connection_id
: data
.server
}));
286 that
.set('nick', data
.nick
);
290 if (data
.nick
=== that
.get('nick')) {
291 that
.set('nick', data
.newnick
);
296 if (_kiwi.gateway.onSync && _kiwi.gateway.syncing) {
297 _kiwi.gateway.syncing = false;
298 _kiwi.gateway.onSync(item);
304 this.emit('_kiwi.' + data
.namespace, data
.data
);
310 if (typeof data
.server
!== 'undefined') {
311 that
.trigger('connection:' + data
.server
.toString(), {
319 * Sends data to the server
321 * @param {Object} data The data to send
322 * @param {Function} callback A callback function
324 this.sendData = function (data
, callback
) {
326 server
: _kiwi
.app
.connections
.active_connection
.get('connection_id'),
327 data
: JSON
.stringify(data
)
330 this.socket
.emit('irc', data_buffer
, callback
);
334 * Sends a PRIVMSG message
335 * @param {String} target The target of the message (e.g. a channel or nick)
336 * @param {String} msg The message to send
337 * @param {Function} callback A callback function
339 this.privmsg = function (target
, msg
, callback
) {
348 this.sendData(data
, callback
);
352 * Sends a NOTICE message
353 * @param {String} target The target of the message (e.g. a channel or nick)
354 * @param {String} msg The message to send
355 * @param {Function} callback A callback function
357 this.notice = function (target
, msg
, callback
) {
366 this.sendData(data
, callback
);
370 * Sends a CTCP message
371 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
372 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
373 * @param {String} target The target of the message, e.g a channel or nick
374 * @param {String} params Additional paramaters
375 * @param {Function} callback A callback function
377 this.ctcp = function (request
, type
, target
, params
, callback
) {
388 this.sendData(data
, callback
);
392 * @param {String} target The target of the message (e.g. a channel or nick)
393 * @param {String} msg The message to send
394 * @param {Function} callback A callback function
396 this.action = function (target
, msg
, callback
) {
397 this.ctcp(true, 'ACTION', target
, msg
, callback
);
402 * @param {String} channel The channel to join
403 * @param {String} key The key to the channel
404 * @param {Function} callback A callback function
406 this.join = function (channel
, key
, callback
) {
415 this.sendData(data
, callback
);
420 * @param {String} channel The channel to part
421 * @param {Function} callback A callback function
423 this.part = function (channel
, callback
) {
431 this.sendData(data
, callback
);
435 * Queries or modifies a channell topic
436 * @param {String} channel The channel to query or modify
437 * @param {String} new_topic The new topic to set
438 * @param {Function} callback A callback function
440 this.topic = function (channel
, new_topic
, callback
) {
449 this.sendData(data
, callback
);
453 * Kicks a user from a channel
454 * @param {String} channel The channel to kick the user from
455 * @param {String} nick The nick of the user to kick
456 * @param {String} reason The reason for kicking the user
457 * @param {Function} callback A callback function
459 this.kick = function (channel
, nick
, reason
, callback
) {
469 this.sendData(data
, callback
);
473 * Disconnects us from the server
474 * @param {String} msg The quit message to send to the IRC server
475 * @param {Function} callback A callback function
477 this.quit = function (msg
, callback
) {
486 this.sendData(data
, callback
);
490 * Sends a string unmodified to the IRC server
491 * @param {String} data The data to send to the IRC server
492 * @param {Function} callback A callback function
494 this.raw = function (data
, callback
) {
502 this.sendData(data
, callback
);
506 * Changes our nickname
507 * @param {String} new_nick Our new nickname
508 * @param {Function} callback A callback function
510 this.changeNick = function (new_nick
, callback
) {
518 this.sendData(data
, callback
);
522 * Sends data to a fellow Kiwi IRC user
523 * @param {String} target The nick of the Kiwi IRC user to send to
524 * @param {String} data The data to send
525 * @param {Function} callback A callback function
527 this.kiwi = function (target
, data
, callback
) {
536 this.sendData(data
, callback
);
540 // Check a nick alongside our ignore list
541 this.isNickIgnored = function (nick
) {
542 var idx
, list
= this.get('ignore_list');
545 for (idx
= 0; idx
< list
.length
; idx
++) {
546 pattern
= list
[idx
].replace(/([.+^$[\]\\(){}|-])/g, "\\$1")
550 regex
= new RegExp(pattern
, 'i');
551 if (regex
.test(nick
)) return true;
558 return new (Backbone
.Model
.extend(this))(arguments
);