1 kiwi
.model
.Gateway
= Backbone
.Model
.extend(new (function () {
6 * The name of the network
12 * The address (URL) of the network
18 * The current nickname
24 * The channel prefix for this network
30 * The user prefixes for channel owner/admin/op/voice etc. on this network
33 user_prefixes
: ['~', '&', '@', '+'],
36 * The URL to the Kiwi server
39 //kiwi_server: '//kiwi'
40 kiwi_server
: document
.location
.protocol
+ '//' + document
.location
.host
+ '/kiwi'
44 this.initialize = function () {
45 // Update `that` with this new Model object
48 // For ease of access. The socket.io object
49 this.socket
= this.get('socket');
51 this.server_num
= null;
53 // Global variable? ~Jack
59 * Connects to the server
60 * @param {String} host The hostname or IP address of the IRC server to connect to
61 * @param {Number} port The port of the IRC server to connect to
62 * @param {Boolean} ssl Whether or not to connect to the IRC server using SSL
63 * @param {String} password The password to supply to the IRC server during registration
64 * @param {Function} callback A callback function to be invoked once Kiwi's server has connected to the IRC server
66 this.connect = function (host
, port
, ssl
, password
, callback
) {
67 this.socket
= io
.connect(this.get('kiwi_server'), {
68 'try multiple transports': true,
69 'connect timeout': 3000,
70 'max reconnection attempts': 7,
71 'reconnection delay': 2000
73 this.socket
.on('connect_failed', function (reason
) {
74 // TODO: When does this even actually get fired? I can't find a case! ~Darren
75 console
.debug('Unable to connect Socket.IO', reason
);
76 console
.log("kiwi.gateway.socket.on('connect_failed')");
77 //kiwi.front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\n' + reason, 'error');
78 this.socket
.disconnect();
79 this.emit("connect_fail", {reason
: reason
});
82 this.socket
.on('error', function (e
) {
83 this.emit("connect_fail", {reason
: e
});
84 console
.log("kiwi.gateway.socket.on('error')", {reason
: e
});
87 this.socket
.on('connecting', function (transport_type
) {
88 console
.log("kiwi.gateway.socket.on('connecting')");
89 this.emit("connecting");
92 this.socket
.on('connect', function () {
93 //{command: 'connect', nick: kiwi.gateway.nick, hostname: host, port: port, ssl: ssl, password: password}
94 this.emit('kiwi', {command
: 'connect', nick
: that
.get('nick'), hostname
: host
, port
: port
, ssl
: ssl
, password
:password
}, function (err
, server_num
) {
95 console
.log('err, server_num', 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 this.emit("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 this.emit("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 this.emit("reconnecting", {delay
: reconnectionDelay
, attempts
: reconnectionAttempts
});
127 this.socket
.on('reconnect_failed', function () {
128 console
.log("kiwi.gateway.socket.on('reconnect_failed')");
153 * Parses the response from the server
155 this.parse = function (command
, data
) {
156 console
.log('gateway event', command
, data
);
157 if (command
!== undefined) {
158 that
.trigger('on' + command
, data
);
162 $.each(data
.options
, function (name
, value
) {
165 // TODO: Check this. Why is it only getting the first char?
166 that
.set('channel_prefix', value
.join('').charAt(0));
169 that
.set('name', value
);
172 that
.set('user_prefixes', value
);
179 that
.set('nick', data
.nick
);
183 that
.set('nick', data
.newnick
);
187 if (kiwi.gateway.onSync && kiwi.gateway.syncing) {
188 kiwi.gateway.syncing = false;
189 kiwi.gateway.onSync(item);
195 this.emit('kiwi.' + data
.namespace, data
);
202 * Sends data to the server
204 * @param {Object} data The data to send
205 * @param {Function} callback A callback function
207 this.sendData = function (data
, callback
) {
208 //this.socket.emit('message', {sid: this.session_id, data: JSON.stringify(data)}, callback);
209 //kiwi.gateway.socket.emit('irc', {server: this.server_num, data: $.toJSON(data)}, callback);
210 this.socket
.emit('irc', {server
: this.server_num
, data
: JSON
.stringify(data
)}, callback
);
214 * Sends a PRIVMSG message
215 * @param {String} target The target of the message (e.g. a channel or nick)
216 * @param {String} msg The message to send
217 * @param {Function} callback A callback function
219 this.privmsg = function (target
, msg
, callback
) {
228 this.sendData(data
, callback
);
232 * Sends a NOTICE message
233 * @param {String} target The target of the message (e.g. a channel or nick)
234 * @param {String} msg The message to send
235 * @param {Function} callback A callback function
237 this.notice = function (target
, msg
, callback
) {
246 this.sendData(data
, callback
);
250 * Sends a CTCP message
251 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)
252 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
253 * @param {String} target The target of the message, e.g a channel or nick
254 * @param {String} params Additional paramaters
255 * @param {Function} callback A callback function
257 this.ctcp = function (request
, type
, target
, params
, callback
) {
268 this.sendData(data
, callback
);
272 * @param {String} target The target of the message (e.g. a channel or nick)
273 * @param {String} msg The message to send
274 * @param {Function} callback A callback function
276 this.action = function (target
, msg
, callback
) {
277 this.ctcp(true, 'ACTION', target
, msg
, callback
);
282 * @param {String} channel The channel to join
283 * @param {String} key The key to the channel
284 * @param {Function} callback A callback function
286 this.join = function (channel
, key
, callback
) {
290 params
: [channel
, key
]
294 this.sendData(data
, callback
);
299 * @param {String} channel The channel to part
300 * @param {Function} callback A callback function
302 this.part = function (channel
, callback
) {
310 this.sendData(data
, callback
);
314 * Queries or modifies a channell topic
315 * @param {String} channel The channel to query or modify
316 * @param {String} new_topic The new topic to set
317 * @param {Function} callback A callback function
319 this.topic = function (channel
, new_topic
, callback
) {
328 this.sendData(data
, callback
);
332 * Kicks a user from a channel
333 * @param {String} channel The channel to kick the user from
334 * @param {String} nick The nick of the user to kick
335 * @param {String} reason The reason for kicking the user
336 * @param {Function} callback A callback function
338 this.kick = function (channel
, nick
, reason
, callback
) {
342 params
: [channel
, nick
],
347 this.sendData(data
, callback
);
351 * Disconnects us from the server
352 * @param {String} msg The quit message to send to the IRC server
353 * @param {Function} callback A callback function
355 this.quit = function (msg
, callback
) {
364 this.sendData(data
, callback
);
368 * Sends a string unmodified to the IRC server
369 * @param {String} data The data to send to the IRC server
370 * @param {Function} callback A callback function
372 this.raw = function (data
, callback
) {
380 this.sendData(data
, callback
);
384 * Changes our nickname
385 * @param {String} new_nick Our new nickname
386 * @param {Function} callback A callback function
388 this.changeNick = function (new_nick
, callback
) {
396 this.sendData(data
, callback
);
400 * Sends data to a fellow Kiwi IRC user
401 * @param {String} target The nick of the Kiwi IRC user to send to
402 * @param {String} data The data to send
403 * @param {Function} callback A callback function
405 this.kiwi = function (target
, data
, callback
) {
414 this.sendData(data
, callback
);