Commit | Line | Data |
---|---|---|
eaaf73b0 | 1 | _kiwi.model.Gateway = function () {\r |
696a66f8 D |
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 | |
8b0eb787 D |
62 | var resource;\r |
63 | \r | |
64 | // Work out the resource URL for socket.io\r | |
eaaf73b0 D |
65 | if (_kiwi.app.get('base_path').substr(0, 1) === '/') {\r |
66 | resource = _kiwi.app.get('base_path');\r | |
8b0eb787 D |
67 | resource = resource.substr(1, resource.length-1);\r |
68 | resource += '/transport';\r | |
69 | } else {\r | |
eaaf73b0 | 70 | resource = _kiwi.app.get('base_path') + '/transport';\r |
8b0eb787 D |
71 | }\r |
72 | \r | |
696a66f8 | 73 | this.socket = io.connect(this.get('kiwi_server'), {\r |
8b0eb787 D |
74 | 'resource': resource,\r |
75 | \r | |
696a66f8 D |
76 | 'try multiple transports': true,\r |
77 | 'connect timeout': 3000,\r | |
78 | 'max reconnection attempts': 7,\r | |
79 | 'reconnection delay': 2000,\r | |
80 | 'sync disconnect on unload': false\r | |
81 | });\r | |
82 | this.socket.on('connect_failed', function (reason) {\r | |
696a66f8 D |
83 | this.socket.disconnect();\r |
84 | this.trigger("connect_fail", {reason: reason});\r | |
85 | });\r | |
86 | \r | |
87 | this.socket.on('error', function (e) {\r | |
eaaf73b0 | 88 | console.log("_kiwi.gateway.socket.on('error')", {reason: e});\r |
696a66f8 D |
89 | that.trigger("connect_fail", {reason: e});\r |
90 | });\r | |
91 | \r | |
92 | this.socket.on('connecting', function (transport_type) {\r | |
eaaf73b0 | 93 | console.log("_kiwi.gateway.socket.on('connecting')");\r |
696a66f8 D |
94 | that.trigger("connecting");\r |
95 | });\r | |
96 | \r | |
97 | /**\r | |
98 | * Once connected to the kiwi server send the IRC connect command along\r | |
99 | * with the IRC server details.\r | |
100 | * A `connect` event is sent from the kiwi server once connected to the\r | |
101 | * IRCD and the nick has been accepted.\r | |
102 | */\r | |
103 | this.socket.on('connect', function () {\r | |
104 | this.emit('kiwi', {command: 'connect', nick: that.get('nick'), hostname: host, port: port, ssl: ssl, password:password}, function (err, server_num) {\r | |
105 | if (!err) {\r | |
106 | that.server_num = server_num;\r | |
eaaf73b0 | 107 | console.log("_kiwi.gateway.socket.on('connect')");\r |
696a66f8 | 108 | } else {\r |
eaaf73b0 | 109 | console.log("_kiwi.gateway.socket.on('error')", {reason: err});\r |
d2d91c10 | 110 | callback(err);\r |
696a66f8 D |
111 | }\r |
112 | });\r | |
113 | });\r | |
114 | \r | |
115 | this.socket.on('too_many_connections', function () {\r | |
116 | that.trigger("connect_fail", {reason: 'too_many_connections'});\r | |
117 | });\r | |
118 | \r | |
119 | this.socket.on('irc', function (data, callback) {\r | |
120 | that.parse(data.command, data.data);\r | |
121 | });\r | |
122 | \r | |
d2d91c10 D |
123 | this.socket.on('kiwi', function (data, callback) {\r |
124 | that.parseKiwi(data.command, data.data);\r | |
125 | });\r | |
126 | \r | |
696a66f8 D |
127 | this.socket.on('disconnect', function () {\r |
128 | that.trigger("disconnect", {});\r | |
eaaf73b0 | 129 | console.log("_kiwi.gateway.socket.on('disconnect')");\r |
696a66f8 D |
130 | });\r |
131 | \r | |
132 | this.socket.on('close', function () {\r | |
eaaf73b0 | 133 | console.log("_kiwi.gateway.socket.on('close')");\r |
696a66f8 D |
134 | });\r |
135 | \r | |
136 | this.socket.on('reconnecting', function (reconnectionDelay, reconnectionAttempts) {\r | |
eaaf73b0 | 137 | console.log("_kiwi.gateway.socket.on('reconnecting')");\r |
696a66f8 D |
138 | that.trigger("reconnecting", {delay: reconnectionDelay, attempts: reconnectionAttempts});\r |
139 | });\r | |
140 | \r | |
141 | this.socket.on('reconnect_failed', function () {\r | |
eaaf73b0 | 142 | console.log("_kiwi.gateway.socket.on('reconnect_failed')");\r |
696a66f8 D |
143 | });\r |
144 | };\r | |
145 | \r | |
146 | \r | |
147 | \r | |
148 | this.isConnected = function () {\r | |
149 | return this.socket.socket.connected;\r | |
150 | };\r | |
151 | \r | |
152 | \r | |
d2d91c10 D |
153 | \r |
154 | this.parseKiwi = function (command, data) {\r | |
155 | console.log('kiwi event', command, data);\r | |
156 | };\r | |
696a66f8 D |
157 | /*\r |
158 | Events:\r | |
159 | msg\r | |
160 | action\r | |
161 | server_connect\r | |
162 | options\r | |
163 | motd\r | |
164 | notice\r | |
165 | userlist\r | |
166 | nick\r | |
167 | join\r | |
168 | topic\r | |
169 | part\r | |
170 | kick\r | |
171 | quit\r | |
172 | whois\r | |
173 | syncchannel_redirect\r | |
174 | debug\r | |
175 | */\r | |
176 | /**\r | |
177 | * Parses the response from the server\r | |
178 | */\r | |
179 | this.parse = function (command, data) {\r | |
d2d91c10 | 180 | //console.log('gateway event', command, data);\r |
696a66f8 D |
181 | if (command !== undefined) {\r |
182 | that.trigger('on' + command, data);\r | |
183 | \r | |
184 | switch (command) {\r | |
185 | case 'options':\r | |
186 | $.each(data.options, function (name, value) {\r | |
187 | switch (name) {\r | |
188 | case 'CHANTYPES':\r | |
189 | // TODO: Check this. Why is it only getting the first char?\r | |
190 | that.set('channel_prefix', value.join('').charAt(0));\r | |
191 | break;\r | |
192 | case 'NETWORK':\r | |
193 | that.set('name', value);\r | |
194 | break;\r | |
195 | case 'PREFIX':\r | |
196 | that.set('user_prefixes', value);\r | |
197 | break;\r | |
198 | }\r | |
199 | });\r | |
dff8edba | 200 | that.set('cap', data.cap);\r |
696a66f8 D |
201 | break;\r |
202 | \r | |
203 | case 'connect':\r | |
204 | that.set('nick', data.nick);\r | |
205 | break;\r | |
206 | \r | |
207 | case 'nick':\r | |
208 | if (data.nick === that.get('nick')) {\r | |
209 | that.set('nick', data.newnick);\r | |
210 | }\r | |
211 | break;\r | |
212 | /*\r | |
213 | case 'sync':\r | |
eaaf73b0 D |
214 | if (_kiwi.gateway.onSync && _kiwi.gateway.syncing) {\r |
215 | _kiwi.gateway.syncing = false;\r | |
216 | _kiwi.gateway.onSync(item);\r | |
696a66f8 D |
217 | }\r |
218 | break;\r | |
219 | */\r | |
220 | \r | |
221 | case 'kiwi':\r | |
eaaf73b0 | 222 | this.emit('_kiwi.' + data.namespace, data.data);\r |
696a66f8 D |
223 | break;\r |
224 | }\r | |
225 | }\r | |
226 | };\r | |
227 | \r | |
228 | /**\r | |
229 | * Sends data to the server\r | |
230 | * @private\r | |
231 | * @param {Object} data The data to send\r | |
232 | * @param {Function} callback A callback function\r | |
233 | */\r | |
234 | this.sendData = function (data, callback) {\r | |
235 | this.socket.emit('irc', {server: 0, data: JSON.stringify(data)}, callback);\r | |
236 | };\r | |
237 | \r | |
238 | /**\r | |
239 | * Sends a PRIVMSG message\r | |
240 | * @param {String} target The target of the message (e.g. a channel or nick)\r | |
241 | * @param {String} msg The message to send\r | |
242 | * @param {Function} callback A callback function\r | |
243 | */\r | |
244 | this.privmsg = function (target, msg, callback) {\r | |
245 | var data = {\r | |
246 | method: 'privmsg',\r | |
247 | args: {\r | |
248 | target: target,\r | |
249 | msg: msg\r | |
250 | }\r | |
251 | };\r | |
252 | \r | |
253 | this.sendData(data, callback);\r | |
254 | };\r | |
255 | \r | |
256 | /**\r | |
257 | * Sends a NOTICE message\r | |
258 | * @param {String} target The target of the message (e.g. a channel or nick)\r | |
259 | * @param {String} msg The message to send\r | |
260 | * @param {Function} callback A callback function\r | |
261 | */\r | |
262 | this.notice = function (target, msg, callback) {\r | |
263 | var data = {\r | |
264 | method: 'notice',\r | |
265 | args: {\r | |
266 | target: target,\r | |
267 | msg: msg\r | |
268 | }\r | |
269 | };\r | |
270 | \r | |
271 | this.sendData(data, callback);\r | |
272 | };\r | |
273 | \r | |
274 | /**\r | |
275 | * Sends a CTCP message\r | |
276 | * @param {Boolean} request Indicates whether this is a CTCP request (true) or reply (false)\r | |
277 | * @param {String} type The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.\r | |
278 | * @param {String} target The target of the message, e.g a channel or nick\r | |
279 | * @param {String} params Additional paramaters\r | |
280 | * @param {Function} callback A callback function\r | |
281 | */\r | |
282 | this.ctcp = function (request, type, target, params, callback) {\r | |
283 | var data = {\r | |
284 | method: 'ctcp',\r | |
285 | args: {\r | |
286 | request: request,\r | |
287 | type: type,\r | |
288 | target: target,\r | |
289 | params: params\r | |
290 | }\r | |
291 | };\r | |
292 | \r | |
293 | this.sendData(data, callback);\r | |
294 | };\r | |
295 | \r | |
296 | /**\r | |
297 | * @param {String} target The target of the message (e.g. a channel or nick)\r | |
298 | * @param {String} msg The message to send\r | |
299 | * @param {Function} callback A callback function\r | |
300 | */\r | |
301 | this.action = function (target, msg, callback) {\r | |
302 | this.ctcp(true, 'ACTION', target, msg, callback);\r | |
303 | };\r | |
304 | \r | |
305 | /**\r | |
306 | * Joins a channel\r | |
307 | * @param {String} channel The channel to join\r | |
308 | * @param {String} key The key to the channel\r | |
309 | * @param {Function} callback A callback function\r | |
310 | */\r | |
311 | this.join = function (channel, key, callback) {\r | |
312 | var data = {\r | |
313 | method: 'join',\r | |
314 | args: {\r | |
315 | channel: channel,\r | |
316 | key: key\r | |
317 | }\r | |
318 | };\r | |
319 | \r | |
320 | this.sendData(data, callback);\r | |
321 | };\r | |
322 | \r | |
323 | /**\r | |
324 | * Leaves a channel\r | |
325 | * @param {String} channel The channel to part\r | |
326 | * @param {Function} callback A callback function\r | |
327 | */\r | |
328 | this.part = function (channel, callback) {\r | |
329 | var data = {\r | |
330 | method: 'part',\r | |
331 | args: {\r | |
332 | channel: channel\r | |
333 | }\r | |
334 | };\r | |
335 | \r | |
336 | this.sendData(data, callback);\r | |
337 | };\r | |
338 | \r | |
339 | /**\r | |
340 | * Queries or modifies a channell topic\r | |
341 | * @param {String} channel The channel to query or modify\r | |
342 | * @param {String} new_topic The new topic to set\r | |
343 | * @param {Function} callback A callback function\r | |
344 | */\r | |
345 | this.topic = function (channel, new_topic, callback) {\r | |
346 | var data = {\r | |
347 | method: 'topic',\r | |
348 | args: {\r | |
349 | channel: channel,\r | |
350 | topic: new_topic\r | |
351 | }\r | |
352 | };\r | |
353 | \r | |
354 | this.sendData(data, callback);\r | |
355 | };\r | |
356 | \r | |
357 | /**\r | |
358 | * Kicks a user from a channel\r | |
359 | * @param {String} channel The channel to kick the user from\r | |
360 | * @param {String} nick The nick of the user to kick\r | |
361 | * @param {String} reason The reason for kicking the user\r | |
362 | * @param {Function} callback A callback function\r | |
363 | */\r | |
364 | this.kick = function (channel, nick, reason, callback) {\r | |
365 | var data = {\r | |
366 | method: 'kick',\r | |
367 | args: {\r | |
368 | channel: channel,\r | |
369 | nick: nick,\r | |
370 | reason: reason\r | |
371 | }\r | |
372 | };\r | |
373 | \r | |
374 | this.sendData(data, callback);\r | |
375 | };\r | |
376 | \r | |
377 | /**\r | |
378 | * Disconnects us from the server\r | |
379 | * @param {String} msg The quit message to send to the IRC server\r | |
380 | * @param {Function} callback A callback function\r | |
381 | */\r | |
382 | this.quit = function (msg, callback) {\r | |
383 | msg = msg || "";\r | |
384 | var data = {\r | |
385 | method: 'quit',\r | |
386 | args: {\r | |
387 | message: msg\r | |
388 | }\r | |
389 | };\r | |
390 | \r | |
391 | this.sendData(data, callback);\r | |
392 | };\r | |
393 | \r | |
394 | /**\r | |
395 | * Sends a string unmodified to the IRC server\r | |
396 | * @param {String} data The data to send to the IRC server\r | |
397 | * @param {Function} callback A callback function\r | |
398 | */\r | |
399 | this.raw = function (data, callback) {\r | |
400 | data = {\r | |
401 | method: 'raw',\r | |
402 | args: {\r | |
403 | data: data\r | |
404 | }\r | |
405 | };\r | |
406 | \r | |
407 | this.sendData(data, callback);\r | |
408 | };\r | |
409 | \r | |
410 | /**\r | |
411 | * Changes our nickname\r | |
412 | * @param {String} new_nick Our new nickname\r | |
413 | * @param {Function} callback A callback function\r | |
414 | */\r | |
415 | this.changeNick = function (new_nick, callback) {\r | |
416 | var data = {\r | |
417 | method: 'nick',\r | |
418 | args: {\r | |
419 | nick: new_nick\r | |
420 | }\r | |
421 | };\r | |
422 | \r | |
423 | this.sendData(data, callback);\r | |
424 | };\r | |
425 | \r | |
426 | /**\r | |
427 | * Sends data to a fellow Kiwi IRC user\r | |
428 | * @param {String} target The nick of the Kiwi IRC user to send to\r | |
429 | * @param {String} data The data to send\r | |
430 | * @param {Function} callback A callback function\r | |
431 | */\r | |
432 | this.kiwi = function (target, data, callback) {\r | |
433 | data = {\r | |
434 | method: 'kiwi',\r | |
435 | args: {\r | |
436 | target: target,\r | |
437 | data: data\r | |
438 | }\r | |
439 | };\r | |
440 | \r | |
441 | this.sendData(data, callback);\r | |
442 | };\r | |
443 | \r | |
444 | \r | |
445 | return new (Backbone.Model.extend(this))(arguments);\r | |
446 | }; |