Updated client codebase
[KiwiIRC.git] / client / assets / dev / model_gateway.js
CommitLineData
696a66f8
D
1kiwi.model.Gateway = function () {\r
2\r
3 // Set to a reference to this object within initialize()\r
4 var that = null;\r
5\r
6 this.defaults = {\r
7 /**\r
8 * The name of the network\r
9 * @type String\r
10 */\r
11 name: 'Server',\r
12\r
13 /**\r
14 * The address (URL) of the network\r
15 * @type String\r
16 */\r
17 address: '',\r
18\r
19 /**\r
20 * The current nickname\r
21 * @type String\r
22 */\r
23 nick: '',\r
24\r
25 /**\r
26 * The channel prefix for this network\r
27 * @type String\r
28 */\r
29 channel_prefix: '#',\r
30\r
31 /**\r
32 * The user prefixes for channel owner/admin/op/voice etc. on this network\r
33 * @type Array\r
34 */\r
35 user_prefixes: ['~', '&', '@', '+'],\r
36\r
37 /**\r
38 * The URL to the Kiwi server\r
39 * @type String\r
40 */\r
41 kiwi_server: '//kiwi'\r
42 };\r
43\r
44\r
45 this.initialize = function () {\r
46 that = this;\r
47 \r
48 // For ease of access. The socket.io object\r
49 this.socket = this.get('socket');\r
50 };\r
51\r
52\r
53 /**\r
54 * Connects to the server\r
55 * @param {String} host The hostname or IP address of the IRC server to connect to\r
56 * @param {Number} port The port of the IRC server to connect to\r
57 * @param {Boolean} ssl Whether or not to connect to the IRC server using SSL\r
58 * @param {String} password The password to supply to the IRC server during registration\r
59 * @param {Function} callback A callback function to be invoked once Kiwi's server has connected to the IRC server\r
60 */\r
61 this.connect = function (host, port, ssl, password, callback) {\r
62 this.socket = io.connect(this.get('kiwi_server'), {\r
63 'try multiple transports': true,\r
64 'connect timeout': 3000,\r
65 'max reconnection attempts': 7,\r
66 'reconnection delay': 2000,\r
67 'sync disconnect on unload': false\r
68 });\r
69 this.socket.on('connect_failed', function (reason) {\r
70 // TODO: When does this even actually get fired? I can't find a case! ~Darren\r
71 console.debug('Unable to connect Socket.IO', reason);\r
72 console.log("kiwi.gateway.socket.on('connect_failed')");\r
73 //kiwi.front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\n' + reason, 'error');\r
74 this.socket.disconnect();\r
75 this.trigger("connect_fail", {reason: reason});\r
76 });\r
77\r
78 this.socket.on('error', function (e) {\r
79 console.log("kiwi.gateway.socket.on('error')", {reason: e});\r
80 that.trigger("connect_fail", {reason: e});\r
81 });\r
82\r
83 this.socket.on('connecting', function (transport_type) {\r
84 console.log("kiwi.gateway.socket.on('connecting')");\r
85 that.trigger("connecting");\r
86 });\r
87\r
88 /**\r
89 * Once connected to the kiwi server send the IRC connect command along\r
90 * with the IRC server details.\r
91 * A `connect` event is sent from the kiwi server once connected to the\r
92 * IRCD and the nick has been accepted.\r
93 */\r
94 this.socket.on('connect', function () {\r
95 this.emit('kiwi', {command: 'connect', nick: that.get('nick'), hostname: host, port: port, ssl: ssl, password:password}, function (err, server_num) {\r
96 if (!err) {\r
97 that.server_num = server_num;\r
98 console.log("kiwi.gateway.socket.on('connect')");\r
99 } else {\r
100 console.log("kiwi.gateway.socket.on('error')", {reason: err});\r
101 }\r
102 });\r
103 });\r
104\r
105 this.socket.on('too_many_connections', function () {\r
106 that.trigger("connect_fail", {reason: 'too_many_connections'});\r
107 });\r
108\r
109 this.socket.on('irc', function (data, callback) {\r
110 that.parse(data.command, data.data);\r
111 });\r
112\r
113 this.socket.on('disconnect', function () {\r
114 that.trigger("disconnect", {});\r
115 console.log("kiwi.gateway.socket.on('disconnect')");\r
116 });\r
117\r
118 this.socket.on('close', function () {\r
119 console.log("kiwi.gateway.socket.on('close')");\r
120 });\r
121\r
122 this.socket.on('reconnecting', function (reconnectionDelay, reconnectionAttempts) {\r
123 console.log("kiwi.gateway.socket.on('reconnecting')");\r
124 that.trigger("reconnecting", {delay: reconnectionDelay, attempts: reconnectionAttempts});\r
125 });\r
126\r
127 this.socket.on('reconnect_failed', function () {\r
128 console.log("kiwi.gateway.socket.on('reconnect_failed')");\r
129 });\r
130 };\r
131\r
132\r
133\r
134 this.isConnected = function () {\r
135 return this.socket.socket.connected;\r
136 };\r
137\r
138\r
139 /*\r
140 Events:\r
141 msg\r
142 action\r
143 server_connect\r
144 options\r
145 motd\r
146 notice\r
147 userlist\r
148 nick\r
149 join\r
150 topic\r
151 part\r
152 kick\r
153 quit\r
154 whois\r
155 syncchannel_redirect\r
156 debug\r
157 */\r
158 /**\r
159 * Parses the response from the server\r
160 */\r
161 this.parse = function (command, data) {\r
162 console.log('gateway event', command, data);\r
163 if (command !== undefined) {\r
164 that.trigger('on' + command, data);\r
165\r
166 switch (command) {\r
167 case 'options':\r
168 $.each(data.options, function (name, value) {\r
169 switch (name) {\r
170 case 'CHANTYPES':\r
171 // TODO: Check this. Why is it only getting the first char?\r
172 that.set('channel_prefix', value.join('').charAt(0));\r
173 break;\r
174 case 'NETWORK':\r
175 that.set('name', value);\r
176 break;\r
177 case 'PREFIX':\r
178 that.set('user_prefixes', value);\r
179 break;\r
180 }\r
181 });\r
182 break;\r
183\r
184 case 'connect':\r
185 that.set('nick', data.nick);\r
186 break;\r
187\r
188 case 'nick':\r
189 if (data.nick === that.get('nick')) {\r
190 that.set('nick', data.newnick);\r
191 }\r
192 break;\r
193 /*\r
194 case 'sync':\r
195 if (kiwi.gateway.onSync && kiwi.gateway.syncing) {\r
196 kiwi.gateway.syncing = false;\r
197 kiwi.gateway.onSync(item);\r
198 }\r
199 break;\r
200 */\r
201\r
202 case 'kiwi':\r
203 this.emit('kiwi.' + data.namespace, data.data);\r
204 break;\r
205 }\r
206 }\r
207 };\r
208\r
209 /**\r
210 * Sends data to the server\r
211 * @private\r
212 * @param {Object} data The data to send\r
213 * @param {Function} callback A callback function\r
214 */\r
215 this.sendData = function (data, callback) {\r
216 this.socket.emit('irc', {server: 0, data: JSON.stringify(data)}, callback);\r
217 };\r
218\r
219 /**\r
220 * Sends a PRIVMSG message\r
221 * @param {String} target The target of the message (e.g. a channel or nick)\r
222 * @param {String} msg The message to send\r
223 * @param {Function} callback A callback function\r
224 */\r
225 this.privmsg = function (target, msg, callback) {\r
226 var data = {\r
227 method: 'privmsg',\r
228 args: {\r
229 target: target,\r
230 msg: msg\r
231 }\r
232 };\r
233\r
234 this.sendData(data, callback);\r
235 };\r
236\r
237 /**\r
238 * Sends a NOTICE message\r
239 * @param {String} target The target of the message (e.g. a channel or nick)\r
240 * @param {String} msg The message to send\r
241 * @param {Function} callback A callback function\r
242 */\r
243 this.notice = function (target, msg, callback) {\r
244 var data = {\r
245 method: 'notice',\r
246 args: {\r
247 target: target,\r
248 msg: msg\r
249 }\r
250 };\r
251\r
252 this.sendData(data, callback);\r
253 };\r
254\r
255 /**\r
256 * Sends a CTCP message\r
257 * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)\r
258 * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.\r
259 * @param {String} target The target of the message, e.g a channel or nick\r
260 * @param {String} params Additional paramaters\r
261 * @param {Function} callback A callback function\r
262 */\r
263 this.ctcp = function (request, type, target, params, callback) {\r
264 var data = {\r
265 method: 'ctcp',\r
266 args: {\r
267 request: request,\r
268 type: type,\r
269 target: target,\r
270 params: params\r
271 }\r
272 };\r
273\r
274 this.sendData(data, callback);\r
275 };\r
276\r
277 /**\r
278 * @param {String} target The target of the message (e.g. a channel or nick)\r
279 * @param {String} msg The message to send\r
280 * @param {Function} callback A callback function\r
281 */\r
282 this.action = function (target, msg, callback) {\r
283 this.ctcp(true, 'ACTION', target, msg, callback);\r
284 };\r
285\r
286 /**\r
287 * Joins a channel\r
288 * @param {String} channel The channel to join\r
289 * @param {String} key The key to the channel\r
290 * @param {Function} callback A callback function\r
291 */\r
292 this.join = function (channel, key, callback) {\r
293 var data = {\r
294 method: 'join',\r
295 args: {\r
296 channel: channel,\r
297 key: key\r
298 }\r
299 };\r
300\r
301 this.sendData(data, callback);\r
302 };\r
303\r
304 /**\r
305 * Leaves a channel\r
306 * @param {String} channel The channel to part\r
307 * @param {Function} callback A callback function\r
308 */\r
309 this.part = function (channel, callback) {\r
310 var data = {\r
311 method: 'part',\r
312 args: {\r
313 channel: channel\r
314 }\r
315 };\r
316\r
317 this.sendData(data, callback);\r
318 };\r
319\r
320 /**\r
321 * Queries or modifies a channell topic\r
322 * @param {String} channel The channel to query or modify\r
323 * @param {String} new_topic The new topic to set\r
324 * @param {Function} callback A callback function\r
325 */\r
326 this.topic = function (channel, new_topic, callback) {\r
327 var data = {\r
328 method: 'topic',\r
329 args: {\r
330 channel: channel,\r
331 topic: new_topic\r
332 }\r
333 };\r
334\r
335 this.sendData(data, callback);\r
336 };\r
337\r
338 /**\r
339 * Kicks a user from a channel\r
340 * @param {String} channel The channel to kick the user from\r
341 * @param {String} nick The nick of the user to kick\r
342 * @param {String} reason The reason for kicking the user\r
343 * @param {Function} callback A callback function\r
344 */\r
345 this.kick = function (channel, nick, reason, callback) {\r
346 var data = {\r
347 method: 'kick',\r
348 args: {\r
349 channel: channel,\r
350 nick: nick,\r
351 reason: reason\r
352 }\r
353 };\r
354\r
355 this.sendData(data, callback);\r
356 };\r
357\r
358 /**\r
359 * Disconnects us from the server\r
360 * @param {String} msg The quit message to send to the IRC server\r
361 * @param {Function} callback A callback function\r
362 */\r
363 this.quit = function (msg, callback) {\r
364 msg = msg || "";\r
365 var data = {\r
366 method: 'quit',\r
367 args: {\r
368 message: msg\r
369 }\r
370 };\r
371\r
372 this.sendData(data, callback);\r
373 };\r
374\r
375 /**\r
376 * Sends a string unmodified to the IRC server\r
377 * @param {String} data The data to send to the IRC server\r
378 * @param {Function} callback A callback function\r
379 */\r
380 this.raw = function (data, callback) {\r
381 data = {\r
382 method: 'raw',\r
383 args: {\r
384 data: data\r
385 }\r
386 };\r
387\r
388 this.sendData(data, callback);\r
389 };\r
390\r
391 /**\r
392 * Changes our nickname\r
393 * @param {String} new_nick Our new nickname\r
394 * @param {Function} callback A callback function\r
395 */\r
396 this.changeNick = function (new_nick, callback) {\r
397 var data = {\r
398 method: 'nick',\r
399 args: {\r
400 nick: new_nick\r
401 }\r
402 };\r
403\r
404 this.sendData(data, callback);\r
405 };\r
406\r
407 /**\r
408 * Sends data to a fellow Kiwi IRC user\r
409 * @param {String} target The nick of the Kiwi IRC user to send to\r
410 * @param {String} data The data to send\r
411 * @param {Function} callback A callback function\r
412 */\r
413 this.kiwi = function (target, data, callback) {\r
414 data = {\r
415 method: 'kiwi',\r
416 args: {\r
417 target: target,\r
418 data: data\r
419 }\r
420 };\r
421\r
422 this.sendData(data, callback);\r
423 };\r
424\r
425\r
426 return new (Backbone.Model.extend(this))(arguments);\r
427};