a2e16e79612e1fcf8cf3f2510229db7d740e9ad5
[KiwiIRC.git] / client / src / models / newconnection.js
1 _kiwi.model.NewConnection = Backbone.Collection.extend({
2 initialize: function() {
3 this.view = new _kiwi.view.ServerSelect({model: this});
4
5 this.view.bind('server_connect', this.onMakeConnection, this);
6
7 },
8
9
10 populateDefaultServerSettings: function () {
11 var parts;
12 var defaults = {
13 nick: '',
14 server: '',
15 port: 6667,
16 ssl: false,
17 channel: '',
18 channel_key: ''
19 };
20 var uricheck;
21
22
23 /**
24 * Get any settings set by the server
25 * These settings may be changed in the server selection dialog or via URL parameters
26 */
27 if (_kiwi.app.server_settings.client) {
28 if (_kiwi.app.server_settings.client.nick)
29 defaults.nick = _kiwi.app.server_settings.client.nick;
30
31 if (_kiwi.app.server_settings.client.server)
32 defaults.server = _kiwi.app.server_settings.client.server;
33
34 if (_kiwi.app.server_settings.client.port)
35 defaults.port = _kiwi.app.server_settings.client.port;
36
37 if (_kiwi.app.server_settings.client.ssl)
38 defaults.ssl = _kiwi.app.server_settings.client.ssl;
39
40 if (_kiwi.app.server_settings.client.channel)
41 defaults.channel = _kiwi.app.server_settings.client.channel;
42
43 if (_kiwi.app.server_settings.client.channel_key)
44 defaults.channel_key = _kiwi.app.server_settings.client.channel_key;
45 }
46
47
48
49 /**
50 * Get any settings passed in the URL
51 * These settings may be changed in the server selection dialog
52 */
53
54 // Any query parameters first
55 if (getQueryVariable('nick'))
56 defaults.nick = getQueryVariable('nick');
57
58 if (window.location.hash)
59 defaults.channel = window.location.hash;
60
61
62 // Process the URL part by part, extracting as we go
63 parts = window.location.pathname.toString().replace(_kiwi.app.get('base_path'), '').split('/');
64
65 if (parts.length > 0) {
66 parts.shift();
67
68 if (parts.length > 0 && parts[0]) {
69 // Check to see if we're dealing with an irc: uri, or whether we need to extract the server/channel info from the HTTP URL path.
70 uricheck = parts[0].substr(0, 7).toLowerCase();
71 if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) {
72 parts[0] = decodeURIComponent(parts[0]);
73 // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
74 uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);
75 /*
76 uricheck[1] = ssl (optional)
77 uricheck[2] = host
78 uricheck[3] = port (optional)
79 uricheck[4] = channel (optional)
80 uricheck[5] = channel key (optional, channel must also be set)
81 */
82 if (uricheck) {
83 if (typeof uricheck[1] !== 'undefined') {
84 defaults.ssl = true;
85 if (defaults.port === 6667) {
86 defaults.port = 6697;
87 }
88 }
89 defaults.server = uricheck[2];
90 if (typeof uricheck[3] !== 'undefined') {
91 defaults.port = uricheck[3];
92 }
93 if (typeof uricheck[4] !== 'undefined') {
94 defaults.channel = '#' + uricheck[4];
95 if (typeof uricheck[5] !== 'undefined') {
96 defaults.channel_key = uricheck[5];
97 }
98 }
99 }
100 parts = [];
101 } else {
102 // Extract the port+ssl if we find one
103 if (parts[0].search(/:/) > 0) {
104 defaults.port = parts[0].substring(parts[0].search(/:/) + 1);
105 defaults.server = parts[0].substring(0, parts[0].search(/:/));
106 if (defaults.port[0] === '+') {
107 defaults.port = parseInt(defaults.port.substring(1), 10);
108 defaults.ssl = true;
109 } else {
110 defaults.ssl = false;
111 }
112
113 } else {
114 defaults.server = parts[0];
115 }
116
117 parts.shift();
118 }
119 }
120
121 if (parts.length > 0 && parts[0]) {
122 defaults.channel = '#' + parts[0];
123 parts.shift();
124 }
125 }
126
127 // If any settings have been given by the server.. override any auto detected settings
128 /**
129 * Get any server restrictions as set in the server config
130 * These settings can not be changed in the server selection dialog
131 */
132 if (_kiwi.app.server_settings && _kiwi.app.server_settings.connection) {
133 if (_kiwi.app.server_settings.connection.server) {
134 defaults.server = _kiwi.app.server_settings.connection.server;
135 }
136
137 if (_kiwi.app.server_settings.connection.port) {
138 defaults.port = _kiwi.app.server_settings.connection.port;
139 }
140
141 if (_kiwi.app.server_settings.connection.ssl) {
142 defaults.ssl = _kiwi.app.server_settings.connection.ssl;
143 }
144
145 if (_kiwi.app.server_settings.connection.channel) {
146 defaults.channel = _kiwi.app.server_settings.connection.channel;
147 }
148
149 if (_kiwi.app.server_settings.connection.channel_key) {
150 defaults.channel_key = _kiwi.app.server_settings.connection.channel_key;
151 }
152
153 if (_kiwi.app.server_settings.connection.nick) {
154 defaults.nick = _kiwi.app.server_settings.connection.nick;
155 }
156 }
157
158 // Set any random numbers if needed
159 defaults.nick = defaults.nick.replace('?', Math.floor(Math.random() * 100000).toString());
160
161 if (getQueryVariable('encoding'))
162 defaults.encoding = getQueryVariable('encoding');
163
164 this.view.populateFields(defaults);
165 },
166
167
168 onMakeConnection: function(new_connection_event) {
169 var that = this,
170 transport_path = '',
171 auto_connect_details = new_connection_event;
172
173 this.view.networkConnecting();
174
175 // If not connected already, connect then send the IRC connect info
176 if (!_kiwi.gateway.isConnected()) {
177 _kiwi.gateway.connect(function() {
178 that.makeConnection(new_connection_event);
179 });
180
181 } else {
182 this.makeConnection(new_connection_event);
183 }
184
185
186 },
187
188
189 onKiwiServerNotFound: function() {
190 this.view.showError();
191 },
192
193
194 makeConnection: function(new_connection_event) {
195 var that = this;
196
197 this.connect_details = new_connection_event;
198
199 _kiwi.gateway.newConnection({
200 nick: new_connection_event.nick,
201 host: new_connection_event.server,
202 port: new_connection_event.port,
203 ssl: new_connection_event.ssl,
204 password: new_connection_event.password,
205 options: new_connection_event.options
206 }, function(err, network) {
207 that.onNewNetwork(err, network);
208 });
209 },
210
211
212 onNewNetwork: function(err, network) {
213 // Show any errors if given
214 if (err) {
215 this.view.showError(err);
216 }
217
218 if (network && this.connect_details) {
219 network.auto_join = {
220 channel: this.connect_details.channel,
221 key: this.connect_details.channel_key
222 };
223
224 this.trigger('new_network', network);
225 }
226 }
227 });