]>
Commit | Line | Data |
---|---|---|
e89c812f CP |
1 | qwebirc.ui.ConnectPane = new Class({ |
2 | Implements: [Events], | |
3 | initialize: function(parent, options) { | |
4 | var callback = options.callback, initialNickname = options.initialNickname, initialChannels = options.initialChannels, networkName = options.networkName, autoConnect = options.autoConnect, autoNick = options.autoNick; | |
5 | this.options = options; | |
65f2c94c | 6 | this.__windowName = "authgate_" + Math.floor(Math.random() * 100000); |
e89c812f CP |
7 | |
8 | var delayfn = function() { parent.set("html", "<div class=\"loading\">Loading. . .</div>"); }; | |
9 | var cb = delayfn.delay(500); | |
10 | ||
11 | var r = qwebirc.ui.RequestTransformHTML({url: qwebirc.global.staticBaseURL + "panes/connect.html", update: parent, onSuccess: function() { | |
12 | $clear(cb); | |
13 | ||
14 | var box = (autoConnect ? "confirm" : "login"); | |
15 | var rootElement = parent.getElement("[name=" + box + "box]"); | |
16 | this.rootElement = rootElement; | |
17 | ||
18 | this.util.exec = function(n, x) { rootElement.getElements(n).each(x); }; | |
19 | var util = this.util; | |
20 | var exec = util.exec; | |
21 | util.makeVisible(rootElement); | |
22 | ||
23 | exec("[name=nickname]", util.setText(initialNickname)); | |
24 | exec("[name=channels]", util.setText(initialChannels)); | |
25 | exec("[name=prettychannels]", function(node) { this.__buildPrettyChannels(node, initialChannels); }.bind(this)); | |
26 | exec("[name=networkname]", util.setText(networkName)); | |
27 | ||
28 | var focus = "connect"; | |
29 | if(autoConnect) { | |
30 | if(!autoNick) | |
31 | exec("[name=nickselected]", util.makeVisible); | |
32 | ||
33 | this.__validate = this.__validateConfirmData; | |
34 | } else { | |
35 | if(!initialNickname) { | |
36 | focus = "nickname"; | |
37 | } else if(initialNickname && !initialChannels) { | |
38 | focus = "channels"; | |
39 | } | |
40 | ||
41 | this.__validate = this.__validateLoginData; | |
42 | } | |
43 | ||
65f2c94c CP |
44 | if(qwebirc.auth.loggedin()) { |
45 | exec("[name=authname]", util.setText(qwebirc.auth.loggedin())); | |
46 | exec("[name=connectbutton]", util.makeVisible); | |
47 | exec("[name=loginstatus]", util.makeVisible); | |
48 | } else { | |
49 | if(qwebirc.ui.isAuthRequired()) { | |
50 | exec("[name=loginconnectbutton]", util.makeVisible); | |
51 | if(focus == "connect") | |
52 | focus = "loginconnect"; | |
53 | } else { | |
54 | exec("[name=connectbutton]", util.makeVisible); | |
55 | exec("[name=loginbutton]", util.makeVisible); | |
56 | } | |
57 | } | |
58 | ||
00bd0d60 CP |
59 | if(window == window.top) /* don't focus when we're iframe'd */ |
60 | exec("[name=" + focus + "]", util.focus); | |
e89c812f | 61 | exec("[name=connect]", util.attachClick(this.__connect.bind(this))); |
65f2c94c | 62 | exec("[name=loginconnect]", util.attachClick(this.__loginConnect.bind(this))); |
3bf9d70e | 63 | |
65f2c94c | 64 | exec("[name=login]", util.attachClick(this.__login.bind(this))); |
3bf9d70e CP |
65 | |
66 | if(qwebirc.ui.isHideAuth()) | |
67 | exec("[name=login]", util.setVisible(false)); | |
e89c812f CP |
68 | }.bind(this)}); |
69 | r.get(); | |
70 | }, | |
71 | util: { | |
72 | makeVisible: function(x) { x.setStyle("display", ""); }, | |
65f2c94c | 73 | setVisible: function(y) { return function(x) { x.setStyle("display", y ? "" : "none"); }; }, |
e89c812f CP |
74 | focus: function(x) { x.focus(); }, |
75 | attachClick: function(fn) { return function(x) { x.addListener("click", fn); } }, | |
76 | setText: function(x) { return function(y) { | |
77 | if(typeof y.value === "undefined") { | |
78 | y.set("text", x); | |
79 | } else { | |
80 | y.value = x === null ? "" : x; | |
81 | } | |
82 | } } | |
83 | }, | |
84 | validate: function() { | |
85 | return this.__validate(); | |
86 | }, | |
87 | __connect: function(e) { | |
88 | new Event(e).stop(); | |
89 | var data = this.validate(); | |
90 | if(data === false) | |
91 | return; | |
66de775f | 92 | |
65f2c94c | 93 | this.__cancelLogin(); |
e89c812f CP |
94 | this.fireEvent("close"); |
95 | this.options.callback(data); | |
96 | }, | |
65f2c94c CP |
97 | __cancelLogin: function(noUIModifications) { |
98 | if(this.__cancelLoginCallback) | |
99 | this.__cancelLoginCallback(noUIModifications); | |
100 | }, | |
101 | __loginConnect: function(e) { | |
102 | new Event(e).stop(); | |
103 | if(this.validate() === false) | |
104 | return; | |
105 | ||
106 | this.__performLogin(function() { | |
107 | var data = this.validate(); | |
108 | if(data === false) { | |
109 | /* we're logged in -- show the normal join button */ | |
110 | this.util.exec("[name=connectbutton]", this.util.setVisible(true)); | |
111 | return; | |
112 | } | |
113 | ||
114 | this.fireEvent("close"); | |
115 | this.options.callback(data); | |
116 | }.bind(this), "loginconnectbutton"); | |
117 | }, | |
118 | __login: function(e) { | |
119 | new Event(e).stop(); | |
120 | ||
121 | this.__cancelLogin(true); | |
122 | ||
123 | this.__performLogin(function() { | |
124 | var focus = "connect"; | |
125 | if(!this.options.autoConnect) { | |
126 | var nick = this.rootElement.getElement("input[name=nickname]").value, chan = this.rootElement.getElement("input[name=channels]").value; | |
127 | if(!nick) { | |
128 | focus = "nickname"; | |
129 | } else if(!chan) { | |
130 | focus = "channels"; | |
131 | } | |
132 | } | |
133 | this.util.exec("[name=" + focus + "]", this.util.focus); | |
134 | }.bind(this), "login"); | |
135 | }, | |
136 | __performLogin: function(callback, calleename) { | |
137 | Cookie.write("jslogin", "1"); | |
138 | ||
139 | var handle = window.open("/auth", this.__windowName, "status=0,toolbar=0,location=1,menubar=0,directories=0,resizable=0,scrollbars=1,height=280,width=550"); | |
140 | ||
141 | if(handle === null || handle === undefined) { | |
142 | Cookie.dispose("jslogin"); | |
143 | // Cookie.write("redirect", document.location); | |
144 | // window.location = "auth?"; | |
145 | return; | |
146 | } | |
147 | ||
148 | var closeDetector = function() { | |
149 | if(handle.closed) | |
150 | this.__cancelLoginCallback(); | |
151 | }.bind(this); | |
152 | var closeCallback = closeDetector.periodical(100); | |
153 | ||
154 | this.__cancelLoginCallback = function(noUIModifications) { | |
155 | $clear(closeCallback); | |
156 | ||
157 | Cookie.dispose("jslogin"); | |
158 | ||
159 | try { | |
160 | handle.close(); | |
161 | } catch(e) { | |
162 | } | |
163 | ||
164 | if(!noUIModifications) { | |
165 | this.util.exec("[name=loggingin]", this.util.setVisible(false)); | |
166 | this.util.exec("[name=" + calleename + "]", this.util.setVisible(true)); | |
167 | } | |
168 | this.__cancelLoginCallback = null; | |
169 | }.bind(this); | |
170 | ||
171 | this.util.exec("[name=loggingin]", this.util.setVisible(true)); | |
172 | this.util.exec("[name=" + calleename + "]", this.util.setVisible(false)); | |
173 | ||
a205506c | 174 | __qwebircAuthCallback = function(username, expiry, serverNow) { |
65f2c94c CP |
175 | this.__cancelLoginCallback(true); |
176 | ||
a205506c CP |
177 | var now = new Date().getTime(); |
178 | var offset = (serverNow * 1000) - now; | |
179 | var ourExpiry = expiry * 1000 - offset; | |
180 | Cookie.write("ticketexpiry", ourExpiry) | |
181 | ||
65f2c94c CP |
182 | this.util.exec("[name=loggingin]", this.util.setVisible(false)); |
183 | this.util.exec("[name=loginstatus]", this.util.setVisible(true)); | |
184 | this.util.exec("[name=authname]", this.util.setText(username)); | |
185 | callback(); | |
186 | }.bind(this); | |
187 | ||
188 | }, | |
e89c812f CP |
189 | __validateConfirmData: function() { |
190 | return {nickname: this.options.initialNickname, autojoin: this.options.initialChannels}; | |
191 | }, | |
192 | __validateLoginData: function() { | |
193 | var nick = this.rootElement.getElement("input[name=nickname]"), chan = this.rootElement.getElement("input[name=channels]"); | |
66de775f | 194 | |
e89c812f CP |
195 | var nickname = nick.value; |
196 | var chans = chan.value; | |
197 | if(chans == "#") /* sorry channel "#" :P */ | |
198 | chans = ""; | |
66de775f | 199 | |
e89c812f CP |
200 | if(!nickname) { |
201 | alert("You must supply a nickname."); | |
202 | nick.focus(); | |
203 | return false; | |
204 | } | |
66de775f | 205 | |
fc25cf01 CP |
206 | var stripped = qwebirc.global.nicknameValidator.validate(nickname); |
207 | if(stripped != nickname) { | |
208 | nick.value = stripped; | |
209 | alert("Your nickname was invalid and has been corrected; please check your altered nickname and try again."); | |
210 | nick.focus(); | |
211 | return false; | |
2f74dea9 CP |
212 | } |
213 | ||
e89c812f CP |
214 | var data = {nickname: nickname, autojoin: chans}; |
215 | return data; | |
216 | }, | |
217 | __buildPrettyChannels: function(node, channels) { | |
218 | var c = channels.split(" ")[0].split(","); | |
219 | node.appendChild(document.createTextNode("channel" + ((c.length>1)?"s":"") + " ")); | |
220 | for(var i=0;i<c.length;i++) { | |
221 | if((c.length > 1) && (i == c.length - 1)) { | |
222 | node.appendChild(document.createTextNode(" and ")); | |
223 | } else if(i > 0) { | |
224 | node.appendChild(document.createTextNode(", ")); | |
225 | } | |
226 | node.appendChild(new Element("b").set("text", c[i])); | |
2f74dea9 | 227 | } |
66de775f | 228 | } |
e89c812f | 229 | }); |
66de775f | 230 | |
e89c812f CP |
231 | qwebirc.ui.LoginBox2 = function(parentElement, callback, initialNickname, initialChannels, networkName) { |
232 | /* | |
6ce70043 CP |
233 | if(qwebirc.auth.enabled()) { |
234 | if(qwebirc.auth.passAuth()) { | |
235 | var authRow = createRow("Auth to services:"); | |
236 | var authCheckBox = qwebirc.util.createInput("checkbox", authRow, "connect_auth_to_services", false); | |
2f74dea9 | 237 | |
6ce70043 CP |
238 | var usernameBox = new Element("input"); |
239 | var usernameRow = createRow("Username:", usernameBox, {display: "none"})[0]; | |
2f74dea9 | 240 | |
6ce70043 CP |
241 | var passwordRow = createRow("Password:", null, {display: "none"}); |
242 | var passwordBox = qwebirc.util.createInput("password", passwordRow[1], "connect_auth_password"); | |
2f74dea9 | 243 | |
6ce70043 CP |
244 | authCheckBox.addEvent("click", function(e) { qwebirc.ui.authShowHide(authCheckBox, authRow, usernameBox, usernameRow, passwordRow[0]) }); |
245 | } else if(qwebirc.auth.bouncerAuth()) { | |
246 | var passwordRow = createRow("Password:"); | |
247 | var passwordBox = qwebirc.util.createInput("password", passwordRow, "connect_auth_password"); | |
248 | } | |
2f74dea9 | 249 | } |
e89c812f CP |
250 | */ |
251 | ||
66de775f CP |
252 | var connbutton = new Element("input", {"type": "submit"}); |
253 | connbutton.set("value", "Connect"); | |
2f74dea9 CP |
254 | var r = createRow(undefined, connbutton); |
255 | ||
66de775f CP |
256 | form.addEvent("submit", function(e) { |
257 | new Event(e).stop(); | |
66de775f | 258 | |
66de775f CP |
259 | var nickname = nick.value; |
260 | var chans = chan.value; | |
261 | if(chans == "#") /* sorry channel "#" :P */ | |
262 | chans = ""; | |
263 | ||
264 | if(!nickname) { | |
265 | alert("You must supply a nickname."); | |
266 | nick.focus(); | |
267 | return; | |
268 | } | |
cc608160 CP |
269 | var stripped = qwebirc.global.nicknameValidator.validate(nickname); |
270 | if(stripped != nickname) { | |
271 | nick.value = stripped; | |
272 | alert("Your nickname was invalid and has been corrected; please check your altered nickname and press Connect again."); | |
273 | nick.focus(); | |
274 | return; | |
275 | } | |
276 | ||
2f74dea9 | 277 | var data = {"nickname": nickname, "autojoin": chans}; |
6f848bbb CP |
278 | if(qwebirc.auth.enabled()) { |
279 | if(qwebirc.auth.passAuth() && authCheckBox.checked) { | |
280 | if(!usernameBox.value || !passwordBox.value) { | |
281 | alert("You must supply your username and password in auth mode."); | |
282 | if(!usernameBox.value) { | |
283 | usernameBox.focus(); | |
284 | } else { | |
285 | passwordBox.focus(); | |
286 | } | |
287 | return; | |
6ce70043 | 288 | } |
6f848bbb CP |
289 | |
290 | data["serverPassword"] = usernameBox.value + " " + passwordBox.value; | |
291 | } else if(qwebirc.auth.bouncerAuth()) { | |
292 | if(!passwordBox.value) { | |
293 | alert("You must supply a password."); | |
294 | passwordBox.focus(); | |
6ce70043 CP |
295 | return; |
296 | } | |
297 | ||
6f848bbb | 298 | data["serverPassword"] = passwordBox.value; |
6ce70043 | 299 | } |
6ce70043 | 300 | } |
6ce70043 CP |
301 | parentElement.removeChild(outerbox); |
302 | ||
2f74dea9 | 303 | callback(data); |
66de775f | 304 | }.bind(this)); |
6ce70043 | 305 | |
66de775f CP |
306 | nick.set("value", initialNickname); |
307 | chan.set("value", initialChannels); | |
308 | ||
b1a46dae | 309 | if(window == window.top) |
9a1c691c | 310 | nick.focus(); |
cad18909 | 311 | } |
2f74dea9 CP |
312 | |
313 | qwebirc.ui.authShowHide = function(checkbox, authRow, usernameBox, usernameRow, passwordRow) { | |
314 | var visible = checkbox.checked; | |
315 | var display = visible?null:"none"; | |
316 | usernameRow.setStyle("display", display); | |
317 | passwordRow.setStyle("display", display); | |
318 | ||
319 | if(visible) { | |
320 | // authRow.parentNode.setStyle("display", "none"); | |
321 | usernameBox.focus(); | |
322 | } | |
323 | } | |
65f2c94c CP |
324 | |
325 | qwebirc.ui.isAuthRequired = (function() { | |
326 | var args = qwebirc.util.parseURI(String(document.location)); | |
86100f08 | 327 | var value = $defined(args) && args.get("authrequired"); |
65f2c94c CP |
328 | return function() { |
329 | return value && qwebirc.auth.enabled(); | |
330 | }; | |
331 | })(); | |
332 | ||
3bf9d70e CP |
333 | qwebirc.ui.isHideAuth = (function() { |
334 | var args = qwebirc.util.parseURI(String(document.location)); | |
86100f08 | 335 | var value = $defined(args) && args.get("hideauth"); |
3bf9d70e CP |
336 | return function() { |
337 | return value; | |
338 | }; | |
339 | })(); | |
340 |