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');
54 * Connects to the server
55 * @param {String} host The hostname or IP address of the IRC server to connect to
56 * @param {Number} port The port of the IRC server to connect to
57 * @param {Boolean} ssl Whether or not to connect to the IRC server using SSL
58 * @param {String} password The password to supply to the IRC server during registration
59 * @param {Function} callback A callback function to be invoked once Kiwi's server has connected to the IRC server
61 this.connect = function (host
, port
, ssl
, password
, callback
) {
64 // Work out the resource URL for socket.io
65 if (kiwi
.app
.get('base_path').substr(0, 1) === '/') {
66 resource
= kiwi
.app
.get('base_path');
67 resource
= resource
.substr(1, resource
.length
-1);
68 resource
+= '/transport';
70 resource
= kiwi
.app
.get('base_path') + '/transport';
73 this.socket
= io
.connect(this.get('kiwi_server'), {
76 'try multiple transports': true,
77 'connect timeout': 3000,
78 'max reconnection attempts': 7,
79 'reconnection delay': 2000,
80 'sync disconnect on unload': false
82 this.socket
.on('connect_failed', function (reason
) {
83 this.socket
.disconnect();
84 this.trigger("connect_fail", {reason
: reason
});
87 this.socket
.on('error', function (e
) {
88 console
.log("kiwi.gateway.socket.on('error')", {reason
: e
});
89 that
.trigger("connect_fail", {reason
: e
});
92 this.socket
.on('connecting', function (transport_type
) {
93 console
.log("kiwi.gateway.socket.on('connecting')");
94 that
.trigger("connecting");
98 * Once connected to the kiwi server send the IRC connect command along
99 * with the IRC server details.
100 * A `connect` event is sent from the kiwi server once connected to the
101 * IRCD and the nick has been accepted.
103 this.socket
.on('connect', function () {
104 this.emit('kiwi', {command
: 'connect', nick
: that
.get('nick'), hostname
: host
, port
: port
, ssl
: ssl
, password
:password
}, function (err
, server_num
) {
106 that
.server_num
= server_num
;
107 console
.log("kiwi.gateway.socket.on('connect')");
109 console
.log("kiwi.gateway.socket.on('error')", {reason
: err
});
115 this.socket
.on('too_many_connections', function () {
116 that
.trigger("connect_fail", {reason
: 'too_many_connections'});
119 this.socket
.on('irc', function (data
, callback
) {
120 that
.parse(data
.command
, data
.data
);
123 this.socket
.on('kiwi', function (data
, callback
) {
124 that
.parseKiwi(data
.command
, data
.data
);
127 this.socket
.on('disconnect', function () {
128 that
.trigger("disconnect", {});
129 console
.log("kiwi.gateway.socket.on('disconnect')");
132 this.socket
.on('close', function () {
133 console
.log("kiwi.gateway.socket.on('close')");
136 this.socket
.on('reconnecting', function (reconnectionDelay
, reconnectionAttempts
) {
137 console
.log("kiwi.gateway.socket.on('reconnecting')");
138 that
.trigger("reconnecting", {delay
: reconnectionDelay
, attempts
: reconnectionAttempts
});
141 this.socket
.on('reconnect_failed', function () {
142 console
.log("kiwi.gateway.socket.on('reconnect_failed')");
148 this.isConnected = function () {
149 return this.socket
.socket
.connected
;
154 this.parseKiwi = function (command
, data
) {
155 console
.log('kiwi event', command
, data
);
177 * Parses the response from the server
179 this.parse = function (command
, data
) {
180 //console.log('gateway event', command, data);
181 if (command
!== undefined) {
182 that
.trigger('on' + command
, data
);
186 $.each(data
.options
, function (name
, value
) {
189 // TODO: Check this. Why is it only getting the first char?
190 that
.set('channel_prefix', value
.join('').charAt(0));
193 that
.set('name', value
);
196 that
.set('user_prefixes', value
);
203 that
.set('nick', data
.nick
);
207 if (data
.nick
=== that
.get('nick')) {
208 that
.set('nick', data
.newnick
);
213 if (kiwi.gateway.onSync && kiwi.gateway.syncing) {
214 kiwi.gateway.syncing = false;
215 kiwi.gateway.onSync(item);
221 this.emit('kiwi.' + data
.namespace, data
.data
);
228 * Sends data to the server
230 * @param {Object} data The data to send
231 * @param {Function} callback A callback function
233 this.sendData = function (data
, callback
) {
234 this.socket
.emit('irc', {server
: 0, data
: JSON
.stringify(data
)}, callback
);
238 * Sends a PRIVMSG message
239 * @param {String} target The target of the message (e.g. a channel or nick)
240 * @param {String} msg The message to send
241 * @param {Function} callback A callback function
243 this.privmsg = function (target
, msg
, callback
) {
252 this.sendData(data
, callback
);
256 * Sends a NOTICE message
257 * @param {String} target The target of the message (e.g. a channel or nick)
258 * @param {String} msg The message to send
259 * @param {Function} callback A callback function
261 this.notice = function (target
, msg
, callback
) {
270 this.sendData(data
, callback
);
274 * Sends a CTCP message
275 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
276 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
277 * @param {String} target The target of the message, e.g a channel or nick
278 * @param {String} params Additional paramaters
279 * @param {Function} callback A callback function
281 this.ctcp = function (request
, type
, target
, params
, callback
) {
292 this.sendData(data
, callback
);
296 * @param {String} target The target of the message (e.g. a channel or nick)
297 * @param {String} msg The message to send
298 * @param {Function} callback A callback function
300 this.action = function (target
, msg
, callback
) {
301 this.ctcp(true, 'ACTION', target
, msg
, callback
);
306 * @param {String} channel The channel to join
307 * @param {String} key The key to the channel
308 * @param {Function} callback A callback function
310 this.join = function (channel
, key
, callback
) {
319 this.sendData(data
, callback
);
324 * @param {String} channel The channel to part
325 * @param {Function} callback A callback function
327 this.part = function (channel
, callback
) {
335 this.sendData(data
, callback
);
339 * Queries or modifies a channell topic
340 * @param {String} channel The channel to query or modify
341 * @param {String} new_topic The new topic to set
342 * @param {Function} callback A callback function
344 this.topic = function (channel
, new_topic
, callback
) {
353 this.sendData(data
, callback
);
357 * Kicks a user from a channel
358 * @param {String} channel The channel to kick the user from
359 * @param {String} nick The nick of the user to kick
360 * @param {String} reason The reason for kicking the user
361 * @param {Function} callback A callback function
363 this.kick = function (channel
, nick
, reason
, callback
) {
373 this.sendData(data
, callback
);
377 * Disconnects us from the server
378 * @param {String} msg The quit message to send to the IRC server
379 * @param {Function} callback A callback function
381 this.quit = function (msg
, callback
) {
390 this.sendData(data
, callback
);
394 * Sends a string unmodified to the IRC server
395 * @param {String} data The data to send to the IRC server
396 * @param {Function} callback A callback function
398 this.raw = function (data
, callback
) {
406 this.sendData(data
, callback
);
410 * Changes our nickname
411 * @param {String} new_nick Our new nickname
412 * @param {Function} callback A callback function
414 this.changeNick = function (new_nick
, callback
) {
422 this.sendData(data
, callback
);
426 * Sends data to a fellow Kiwi IRC user
427 * @param {String} target The nick of the Kiwi IRC user to send to
428 * @param {String} data The data to send
429 * @param {Function} callback A callback function
431 this.kiwi = function (target
, data
, callback
) {
440 this.sendData(data
, callback
);
444 return new (Backbone
.Model
.extend(this))(arguments
);