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
45 this.initialize = function () {
48 // For ease of access. The socket.io object
49 this.socket
= this.get('socket');
51 this.applyEventHandlers();
55 this.applyEventHandlers = function () {
57 kiwi.gateway.on('message:#channel', my_function);
58 kiwi.gateway.on('message:somenick', my_function);
60 kiwi.gateway.on('notice:#channel', my_function);
61 kiwi.gateway.on('action:somenick', my_function);
63 kiwi.gateway.on('join:#channel', my_function);
64 kiwi.gateway.on('part:#channel', my_function);
65 kiwi.gateway.on('quit', my_function);
69 // Some easier handler events
70 this.on('onmsg', function (event
) {
72 is_pm
= (event
.channel
== that
.get('nick'));
74 source
= is_pm
? event
.nick
: event
.channel
;
76 that
.trigger('message:' + source
, event
);
77 that
.trigger('message', event
);
80 that
.trigger('pm:' + source
, event
);
81 that
.trigger('pm', event
);
86 this.on('onnotice', function (event
) {
87 // The notice towards a channel or a query window?
88 var source
= event
.target
|| event
.nick
;
90 this.trigger('notice:' + source
, event
);
91 this.trigger('notice', event
);
95 this.on('onaction', function (event
) {
97 is_pm
= (event
.channel
== that
.get('nick'));
99 source
= is_pm
? event
.nick
: event
.channel
;
101 that
.trigger('action:' + source
, event
);
104 that
.trigger('action:' + source
, event
);
105 that
.trigger('action', event
);
110 this.on('ontopic', function (event
) {
111 that
.trigger('topic:' + event
.channel
, event
);
112 that
.trigger('topic', event
);
119 * Connects to the server
120 * @param {String} host The hostname or IP address of the IRC server to connect to
121 * @param {Number} port The port of the IRC server to connect to
122 * @param {Boolean} ssl Whether or not to connect to the IRC server using SSL
123 * @param {String} password The password to supply to the IRC server during registration
124 * @param {Function} callback A callback function to be invoked once Kiwi's server has connected to the IRC server
126 this.connect = function (host
, port
, ssl
, password
, callback
) {
129 // Work out the resource URL for socket.io
130 if (_kiwi
.app
.get('base_path').substr(0, 1) === '/') {
131 resource
= _kiwi
.app
.get('base_path');
132 resource
= resource
.substr(1, resource
.length
-1);
133 resource
+= '/transport';
135 resource
= _kiwi
.app
.get('base_path') + '/transport';
138 this.socket
= io
.connect(this.get('kiwi_server'), {
139 'resource': resource
,
141 'try multiple transports': true,
142 'connect timeout': 3000,
143 'max reconnection attempts': 7,
144 'reconnection delay': 2000,
145 'sync disconnect on unload': false
147 this.socket
.on('connect_failed', function (reason
) {
148 this.socket
.disconnect();
149 this.trigger("connect_fail", {reason
: reason
});
152 this.socket
.on('error', function (e
) {
153 console
.log("_kiwi.gateway.socket.on('error')", {reason
: e
});
154 that
.trigger("connect_fail", {reason
: e
});
157 this.socket
.on('connecting', function (transport_type
) {
158 console
.log("_kiwi.gateway.socket.on('connecting')");
159 that
.trigger("connecting");
163 * Once connected to the kiwi server send the IRC connect command along
164 * with the IRC server details.
165 * A `connect` event is sent from the kiwi server once connected to the
166 * IRCD and the nick has been accepted.
168 this.socket
.on('connect', function () {
169 this.emit('kiwi', {command
: 'connect', nick
: that
.get('nick'), hostname
: host
, port
: port
, ssl
: ssl
, password
:password
}, function (err
, server_num
) {
171 that
.server_num
= server_num
;
172 console
.log("_kiwi.gateway.socket.on('connect')");
174 console
.log("_kiwi.gateway.socket.on('error')", {reason
: err
});
180 this.socket
.on('too_many_connections', function () {
181 that
.trigger("connect_fail", {reason
: 'too_many_connections'});
184 this.socket
.on('irc', function (data
, callback
) {
185 that
.parse(data
.command
, data
.data
);
188 this.socket
.on('kiwi', function (data
, callback
) {
189 that
.parseKiwi(data
.command
, data
.data
);
192 this.socket
.on('disconnect', function () {
193 that
.trigger("disconnect", {});
194 console
.log("_kiwi.gateway.socket.on('disconnect')");
197 this.socket
.on('close', function () {
198 console
.log("_kiwi.gateway.socket.on('close')");
201 this.socket
.on('reconnecting', function (reconnectionDelay
, reconnectionAttempts
) {
202 console
.log("_kiwi.gateway.socket.on('reconnecting')");
203 that
.trigger("reconnecting", {delay
: reconnectionDelay
, attempts
: reconnectionAttempts
});
206 this.socket
.on('reconnect_failed', function () {
207 console
.log("_kiwi.gateway.socket.on('reconnect_failed')");
213 this.isConnected = function () {
214 return this.socket
.socket
.connected
;
219 this.parseKiwi = function (command
, data
) {
220 this.trigger('kiwi:' + command
, data
);
221 this.trigger('kiwi', data
);
243 * Parses the response from the server
245 this.parse = function (command
, data
) {
246 //console.log('gateway event', command, data);
247 if (command
!== undefined) {
248 that
.trigger('on' + command
, data
);
252 $.each(data
.options
, function (name
, value
) {
255 // TODO: Check this. Why is it only getting the first char?
256 that
.set('channel_prefix', value
.join('').charAt(0));
259 that
.set('name', value
);
262 that
.set('user_prefixes', value
);
266 that
.set('cap', data
.cap
);
270 that
.set('nick', data
.nick
);
274 if (data
.nick
=== that
.get('nick')) {
275 that
.set('nick', data
.newnick
);
280 if (_kiwi.gateway.onSync && _kiwi.gateway.syncing) {
281 _kiwi.gateway.syncing = false;
282 _kiwi.gateway.onSync(item);
288 this.emit('_kiwi.' + data
.namespace, data
.data
);
295 * Sends data to the server
297 * @param {Object} data The data to send
298 * @param {Function} callback A callback function
300 this.sendData = function (data
, callback
) {
301 this.socket
.emit('irc', {server
: 0, data
: JSON
.stringify(data
)}, callback
);
305 * Sends a PRIVMSG message
306 * @param {String} target The target of the message (e.g. a channel or nick)
307 * @param {String} msg The message to send
308 * @param {Function} callback A callback function
310 this.privmsg = function (target
, msg
, callback
) {
319 this.sendData(data
, callback
);
323 * Sends a NOTICE message
324 * @param {String} target The target of the message (e.g. a channel or nick)
325 * @param {String} msg The message to send
326 * @param {Function} callback A callback function
328 this.notice = function (target
, msg
, callback
) {
337 this.sendData(data
, callback
);
341 * Sends a CTCP message
342 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
343 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
344 * @param {String} target The target of the message, e.g a channel or nick
345 * @param {String} params Additional paramaters
346 * @param {Function} callback A callback function
348 this.ctcp = function (request
, type
, target
, params
, callback
) {
359 this.sendData(data
, callback
);
363 * @param {String} target The target of the message (e.g. a channel or nick)
364 * @param {String} msg The message to send
365 * @param {Function} callback A callback function
367 this.action = function (target
, msg
, callback
) {
368 this.ctcp(true, 'ACTION', target
, msg
, callback
);
373 * @param {String} channel The channel to join
374 * @param {String} key The key to the channel
375 * @param {Function} callback A callback function
377 this.join = function (channel
, key
, callback
) {
386 this.sendData(data
, callback
);
391 * @param {String} channel The channel to part
392 * @param {Function} callback A callback function
394 this.part = function (channel
, callback
) {
402 this.sendData(data
, callback
);
406 * Queries or modifies a channell topic
407 * @param {String} channel The channel to query or modify
408 * @param {String} new_topic The new topic to set
409 * @param {Function} callback A callback function
411 this.topic = function (channel
, new_topic
, callback
) {
420 this.sendData(data
, callback
);
424 * Kicks a user from a channel
425 * @param {String} channel The channel to kick the user from
426 * @param {String} nick The nick of the user to kick
427 * @param {String} reason The reason for kicking the user
428 * @param {Function} callback A callback function
430 this.kick = function (channel
, nick
, reason
, callback
) {
440 this.sendData(data
, callback
);
444 * Disconnects us from the server
445 * @param {String} msg The quit message to send to the IRC server
446 * @param {Function} callback A callback function
448 this.quit = function (msg
, callback
) {
457 this.sendData(data
, callback
);
461 * Sends a string unmodified to the IRC server
462 * @param {String} data The data to send to the IRC server
463 * @param {Function} callback A callback function
465 this.raw = function (data
, callback
) {
473 this.sendData(data
, callback
);
477 * Changes our nickname
478 * @param {String} new_nick Our new nickname
479 * @param {Function} callback A callback function
481 this.changeNick = function (new_nick
, callback
) {
489 this.sendData(data
, callback
);
493 * Sends data to a fellow Kiwi IRC user
494 * @param {String} target The nick of the Kiwi IRC user to send to
495 * @param {String} data The data to send
496 * @param {Function} callback A callback function
498 this.kiwi = function (target
, data
, callback
) {
507 this.sendData(data
, callback
);
511 return new (Backbone
.Model
.extend(this))(arguments
);