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
);
200 that
.set('cap', data
.cap
);
204 that
.set('nick', data
.nick
);
208 if (data
.nick
=== that
.get('nick')) {
209 that
.set('nick', data
.newnick
);
214 if (_kiwi.gateway.onSync && _kiwi.gateway.syncing) {
215 _kiwi.gateway.syncing = false;
216 _kiwi.gateway.onSync(item);
222 this.emit('_kiwi.' + data
.namespace, data
.data
);
229 * Sends data to the server
231 * @param {Object} data The data to send
232 * @param {Function} callback A callback function
234 this.sendData = function (data
, callback
) {
235 this.socket
.emit('irc', {server
: 0, data
: JSON
.stringify(data
)}, callback
);
239 * Sends a PRIVMSG message
240 * @param {String} target The target of the message (e.g. a channel or nick)
241 * @param {String} msg The message to send
242 * @param {Function} callback A callback function
244 this.privmsg = function (target
, msg
, callback
) {
253 this.sendData(data
, callback
);
257 * Sends a NOTICE message
258 * @param {String} target The target of the message (e.g. a channel or nick)
259 * @param {String} msg The message to send
260 * @param {Function} callback A callback function
262 this.notice = function (target
, msg
, callback
) {
271 this.sendData(data
, callback
);
275 * Sends a CTCP message
276 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
277 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
278 * @param {String} target The target of the message, e.g a channel or nick
279 * @param {String} params Additional paramaters
280 * @param {Function} callback A callback function
282 this.ctcp = function (request
, type
, target
, params
, callback
) {
293 this.sendData(data
, callback
);
297 * @param {String} target The target of the message (e.g. a channel or nick)
298 * @param {String} msg The message to send
299 * @param {Function} callback A callback function
301 this.action = function (target
, msg
, callback
) {
302 this.ctcp(true, 'ACTION', target
, msg
, callback
);
307 * @param {String} channel The channel to join
308 * @param {String} key The key to the channel
309 * @param {Function} callback A callback function
311 this.join = function (channel
, key
, callback
) {
320 this.sendData(data
, callback
);
325 * @param {String} channel The channel to part
326 * @param {Function} callback A callback function
328 this.part = function (channel
, callback
) {
336 this.sendData(data
, callback
);
340 * Queries or modifies a channell topic
341 * @param {String} channel The channel to query or modify
342 * @param {String} new_topic The new topic to set
343 * @param {Function} callback A callback function
345 this.topic = function (channel
, new_topic
, callback
) {
354 this.sendData(data
, callback
);
358 * Kicks a user from a channel
359 * @param {String} channel The channel to kick the user from
360 * @param {String} nick The nick of the user to kick
361 * @param {String} reason The reason for kicking the user
362 * @param {Function} callback A callback function
364 this.kick = function (channel
, nick
, reason
, callback
) {
374 this.sendData(data
, callback
);
378 * Disconnects us from the server
379 * @param {String} msg The quit message to send to the IRC server
380 * @param {Function} callback A callback function
382 this.quit = function (msg
, callback
) {
391 this.sendData(data
, callback
);
395 * Sends a string unmodified to the IRC server
396 * @param {String} data The data to send to the IRC server
397 * @param {Function} callback A callback function
399 this.raw = function (data
, callback
) {
407 this.sendData(data
, callback
);
411 * Changes our nickname
412 * @param {String} new_nick Our new nickname
413 * @param {Function} callback A callback function
415 this.changeNick = function (new_nick
, callback
) {
423 this.sendData(data
, callback
);
427 * Sends data to a fellow Kiwi IRC user
428 * @param {String} target The nick of the Kiwi IRC user to send to
429 * @param {String} data The data to send
430 * @param {Function} callback A callback function
432 this.kiwi = function (target
, data
, callback
) {
441 this.sendData(data
, callback
);
445 return new (Backbone
.Model
.extend(this))(arguments
);