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
) {
62 this.socket
= io
.connect(this.get('kiwi_server'), {
63 'try multiple transports': true,
64 'connect timeout': 3000,
65 'max reconnection attempts': 7,
66 'reconnection delay': 2000,
67 'sync disconnect on unload': false
69 this.socket
.on('connect_failed', function (reason
) {
70 // TODO: When does this even actually get fired? I can't find a case! ~Darren
71 console
.debug('Unable to connect Socket.IO', reason
);
72 console
.log("kiwi.gateway.socket.on('connect_failed')");
73 //kiwi.front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\n' + reason, 'error');
74 this.socket
.disconnect();
75 this.trigger("connect_fail", {reason
: reason
});
78 this.socket
.on('error', function (e
) {
79 console
.log("kiwi.gateway.socket.on('error')", {reason
: e
});
80 that
.trigger("connect_fail", {reason
: e
});
83 this.socket
.on('connecting', function (transport_type
) {
84 console
.log("kiwi.gateway.socket.on('connecting')");
85 that
.trigger("connecting");
89 * Once connected to the kiwi server send the IRC connect command along
90 * with the IRC server details.
91 * A `connect` event is sent from the kiwi server once connected to the
92 * IRCD and the nick has been accepted.
94 this.socket
.on('connect', function () {
95 this.emit('kiwi', {command
: 'connect', nick
: that
.get('nick'), hostname
: host
, port
: port
, ssl
: ssl
, password
:password
}, function (err
, server_num
) {
97 that
.server_num
= server_num
;
98 console
.log("kiwi.gateway.socket.on('connect')");
100 console
.log("kiwi.gateway.socket.on('error')", {reason
: err
});
105 this.socket
.on('too_many_connections', function () {
106 that
.trigger("connect_fail", {reason
: 'too_many_connections'});
109 this.socket
.on('irc', function (data
, callback
) {
110 that
.parse(data
.command
, data
.data
);
113 this.socket
.on('disconnect', function () {
114 that
.trigger("disconnect", {});
115 console
.log("kiwi.gateway.socket.on('disconnect')");
118 this.socket
.on('close', function () {
119 console
.log("kiwi.gateway.socket.on('close')");
122 this.socket
.on('reconnecting', function (reconnectionDelay
, reconnectionAttempts
) {
123 console
.log("kiwi.gateway.socket.on('reconnecting')");
124 that
.trigger("reconnecting", {delay
: reconnectionDelay
, attempts
: reconnectionAttempts
});
127 this.socket
.on('reconnect_failed', function () {
128 console
.log("kiwi.gateway.socket.on('reconnect_failed')");
134 this.isConnected = function () {
135 return this.socket
.socket
.connected
;
159 * Parses the response from the server
161 this.parse = function (command
, data
) {
162 console
.log('gateway event', command
, data
);
163 if (command
!== undefined) {
164 that
.trigger('on' + command
, data
);
168 $.each(data
.options
, function (name
, value
) {
171 // TODO: Check this. Why is it only getting the first char?
172 that
.set('channel_prefix', value
.join('').charAt(0));
175 that
.set('name', value
);
178 that
.set('user_prefixes', value
);
185 that
.set('nick', data
.nick
);
189 if (data
.nick
=== that
.get('nick')) {
190 that
.set('nick', data
.newnick
);
195 if (kiwi.gateway.onSync && kiwi.gateway.syncing) {
196 kiwi.gateway.syncing = false;
197 kiwi.gateway.onSync(item);
203 this.emit('kiwi.' + data
.namespace, data
.data
);
210 * Sends data to the server
212 * @param {Object} data The data to send
213 * @param {Function} callback A callback function
215 this.sendData = function (data
, callback
) {
216 this.socket
.emit('irc', {server
: 0, data
: JSON
.stringify(data
)}, callback
);
220 * Sends a PRIVMSG message
221 * @param {String} target The target of the message (e.g. a channel or nick)
222 * @param {String} msg The message to send
223 * @param {Function} callback A callback function
225 this.privmsg = function (target
, msg
, callback
) {
234 this.sendData(data
, callback
);
238 * Sends a NOTICE 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.notice = function (target
, msg
, callback
) {
252 this.sendData(data
, callback
);
256 * Sends a CTCP message
257 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
258 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
259 * @param {String} target The target of the message, e.g a channel or nick
260 * @param {String} params Additional paramaters
261 * @param {Function} callback A callback function
263 this.ctcp = function (request
, type
, target
, params
, callback
) {
274 this.sendData(data
, callback
);
278 * @param {String} target The target of the message (e.g. a channel or nick)
279 * @param {String} msg The message to send
280 * @param {Function} callback A callback function
282 this.action = function (target
, msg
, callback
) {
283 this.ctcp(true, 'ACTION', target
, msg
, callback
);
288 * @param {String} channel The channel to join
289 * @param {String} key The key to the channel
290 * @param {Function} callback A callback function
292 this.join = function (channel
, key
, callback
) {
301 this.sendData(data
, callback
);
306 * @param {String} channel The channel to part
307 * @param {Function} callback A callback function
309 this.part = function (channel
, callback
) {
317 this.sendData(data
, callback
);
321 * Queries or modifies a channell topic
322 * @param {String} channel The channel to query or modify
323 * @param {String} new_topic The new topic to set
324 * @param {Function} callback A callback function
326 this.topic = function (channel
, new_topic
, callback
) {
335 this.sendData(data
, callback
);
339 * Kicks a user from a channel
340 * @param {String} channel The channel to kick the user from
341 * @param {String} nick The nick of the user to kick
342 * @param {String} reason The reason for kicking the user
343 * @param {Function} callback A callback function
345 this.kick = function (channel
, nick
, reason
, callback
) {
355 this.sendData(data
, callback
);
359 * Disconnects us from the server
360 * @param {String} msg The quit message to send to the IRC server
361 * @param {Function} callback A callback function
363 this.quit = function (msg
, callback
) {
372 this.sendData(data
, callback
);
376 * Sends a string unmodified to the IRC server
377 * @param {String} data The data to send to the IRC server
378 * @param {Function} callback A callback function
380 this.raw = function (data
, callback
) {
388 this.sendData(data
, callback
);
392 * Changes our nickname
393 * @param {String} new_nick Our new nickname
394 * @param {Function} callback A callback function
396 this.changeNick = function (new_nick
, callback
) {
404 this.sendData(data
, callback
);
408 * Sends data to a fellow Kiwi IRC user
409 * @param {String} target The nick of the Kiwi IRC user to send to
410 * @param {String} data The data to send
411 * @param {Function} callback A callback function
413 this.kiwi = function (target
, data
, callback
) {
422 this.sendData(data
, callback
);
426 return new (Backbone
.Model
.extend(this))(arguments
);