1 _kiwi
.model
.NewConnection
= Backbone
.Collection
.extend({
2 initialize: function() {
3 this.view
= new _kiwi
.view
.ServerSelect({model
: this});
5 this.view
.bind('server_connect', this.onMakeConnection
, this);
10 populateDefaultServerSettings: function () {
24 * Get any settings set by the server
25 * These settings may be changed in the server selection dialog or via URL parameters
27 if (_kiwi
.app
.server_settings
.client
) {
28 if (_kiwi
.app
.server_settings
.client
.nick
)
29 defaults
.nick
= _kiwi
.app
.server_settings
.client
.nick
;
31 if (_kiwi
.app
.server_settings
.client
.server
)
32 defaults
.server
= _kiwi
.app
.server_settings
.client
.server
;
34 if (_kiwi
.app
.server_settings
.client
.port
)
35 defaults
.port
= _kiwi
.app
.server_settings
.client
.port
;
37 if (_kiwi
.app
.server_settings
.client
.ssl
)
38 defaults
.ssl
= _kiwi
.app
.server_settings
.client
.ssl
;
40 if (_kiwi
.app
.server_settings
.client
.channel
)
41 defaults
.channel
= _kiwi
.app
.server_settings
.client
.channel
;
43 if (_kiwi
.app
.server_settings
.client
.channel_key
)
44 defaults
.channel_key
= _kiwi
.app
.server_settings
.client
.channel_key
;
50 * Get any settings passed in the URL
51 * These settings may be changed in the server selection dialog
54 // Any query parameters first
55 if (getQueryVariable('nick'))
56 defaults
.nick
= getQueryVariable('nick');
58 if (window
.location
.hash
)
59 defaults
.channel
= window
.location
.hash
;
62 // Process the URL part by part, extracting as we go
63 parts
= window
.location
.pathname
.toString().replace(_kiwi
.app
.get('base_path'), '').split('/');
65 if (parts
.length
> 0) {
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]);
76 uricheck[1] = ssl (optional)
78 uricheck[3] = port (optional)
79 uricheck[4] = channel (optional)
80 uricheck[5] = channel key (optional, channel must also be set)
83 if (typeof uricheck
[1] !== 'undefined') {
85 if (defaults
.port
=== 6667) {
89 defaults
.server
= uricheck
[2];
90 if (typeof uricheck
[3] !== 'undefined') {
91 defaults
.port
= uricheck
[3];
93 if (typeof uricheck
[4] !== 'undefined') {
94 defaults
.channel
= '#' + uricheck
[4];
95 if (typeof uricheck
[5] !== 'undefined') {
96 defaults
.channel_key
= uricheck
[5];
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);
110 defaults
.ssl
= false;
114 defaults
.server
= parts
[0];
121 if (parts
.length
> 0 && parts
[0]) {
122 defaults
.channel
= '#' + parts
[0];
127 // If any settings have been given by the server.. override any auto detected settings
129 * Get any server restrictions as set in the server config
130 * These settings can not be changed in the server selection dialog
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
;
137 if (_kiwi
.app
.server_settings
.connection
.port
) {
138 defaults
.port
= _kiwi
.app
.server_settings
.connection
.port
;
141 if (_kiwi
.app
.server_settings
.connection
.ssl
) {
142 defaults
.ssl
= _kiwi
.app
.server_settings
.connection
.ssl
;
145 if (_kiwi
.app
.server_settings
.connection
.channel
) {
146 defaults
.channel
= _kiwi
.app
.server_settings
.connection
.channel
;
149 if (_kiwi
.app
.server_settings
.connection
.channel_key
) {
150 defaults
.channel_key
= _kiwi
.app
.server_settings
.connection
.channel_key
;
153 if (_kiwi
.app
.server_settings
.connection
.nick
) {
154 defaults
.nick
= _kiwi
.app
.server_settings
.connection
.nick
;
158 // Set any random numbers if needed
159 defaults
.nick
= defaults
.nick
.replace('?', Math
.floor(Math
.random() * 100000).toString());
161 if (getQueryVariable('encoding'))
162 defaults
.encoding
= getQueryVariable('encoding');
164 this.view
.populateFields(defaults
);
168 onMakeConnection: function(new_connection_event
) {
171 auto_connect_details
= new_connection_event
;
173 this.view
.networkConnecting();
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
);
182 this.makeConnection(new_connection_event
);
189 onKiwiServerNotFound: function() {
190 this.view
.showError();
194 makeConnection: function(new_connection_event
) {
197 this.connect_details
= new_connection_event
;
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
);
212 onNewNetwork: function(err
, network
) {
213 // Show any errors if given
215 this.view
.showError(err
);
218 if (network
&& this.connect_details
) {
219 network
.auto_join
= {
220 channel
: this.connect_details
.channel
,
221 key
: this.connect_details
.channel_key
224 this.trigger('new_network', network
);