]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/panes/connect.js
Merge.
[irc/quakenet/qwebirc.git] / js / ui / panes / connect.js
CommitLineData
e89c812f
CP
1qwebirc.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
CP
62 exec("[name=loginconnect]", util.attachClick(this.__loginConnect.bind(this)));
63 exec("[name=login]", util.attachClick(this.__login.bind(this)));
e89c812f
CP
64 }.bind(this)});
65 r.get();
66 },
67 util: {
68 makeVisible: function(x) { x.setStyle("display", ""); },
65f2c94c 69 setVisible: function(y) { return function(x) { x.setStyle("display", y ? "" : "none"); }; },
e89c812f
CP
70 focus: function(x) { x.focus(); },
71 attachClick: function(fn) { return function(x) { x.addListener("click", fn); } },
72 setText: function(x) { return function(y) {
73 if(typeof y.value === "undefined") {
74 y.set("text", x);
75 } else {
76 y.value = x === null ? "" : x;
77 }
78 } }
79 },
80 validate: function() {
81 return this.__validate();
82 },
83 __connect: function(e) {
84 new Event(e).stop();
85 var data = this.validate();
86 if(data === false)
87 return;
66de775f 88
65f2c94c 89 this.__cancelLogin();
e89c812f
CP
90 this.fireEvent("close");
91 this.options.callback(data);
92 },
65f2c94c
CP
93 __cancelLogin: function(noUIModifications) {
94 if(this.__cancelLoginCallback)
95 this.__cancelLoginCallback(noUIModifications);
96 },
97 __loginConnect: function(e) {
98 new Event(e).stop();
99 if(this.validate() === false)
100 return;
101
102 this.__performLogin(function() {
103 var data = this.validate();
104 if(data === false) {
105 /* we're logged in -- show the normal join button */
106 this.util.exec("[name=connectbutton]", this.util.setVisible(true));
107 return;
108 }
109
110 this.fireEvent("close");
111 this.options.callback(data);
112 }.bind(this), "loginconnectbutton");
113 },
114 __login: function(e) {
115 new Event(e).stop();
116
117 this.__cancelLogin(true);
118
119 this.__performLogin(function() {
120 var focus = "connect";
121 if(!this.options.autoConnect) {
122 var nick = this.rootElement.getElement("input[name=nickname]").value, chan = this.rootElement.getElement("input[name=channels]").value;
123 if(!nick) {
124 focus = "nickname";
125 } else if(!chan) {
126 focus = "channels";
127 }
128 }
129 this.util.exec("[name=" + focus + "]", this.util.focus);
130 }.bind(this), "login");
131 },
132 __performLogin: function(callback, calleename) {
133 Cookie.write("jslogin", "1");
134
135 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");
136
137 if(handle === null || handle === undefined) {
138 Cookie.dispose("jslogin");
139// Cookie.write("redirect", document.location);
140// window.location = "auth?";
141 return;
142 }
143
144 var closeDetector = function() {
145 if(handle.closed)
146 this.__cancelLoginCallback();
147 }.bind(this);
148 var closeCallback = closeDetector.periodical(100);
149
150 this.__cancelLoginCallback = function(noUIModifications) {
151 $clear(closeCallback);
152
153 Cookie.dispose("jslogin");
154
155 try {
156 handle.close();
157 } catch(e) {
158 }
159
160 if(!noUIModifications) {
161 this.util.exec("[name=loggingin]", this.util.setVisible(false));
162 this.util.exec("[name=" + calleename + "]", this.util.setVisible(true));
163 }
164 this.__cancelLoginCallback = null;
165 }.bind(this);
166
167 this.util.exec("[name=loggingin]", this.util.setVisible(true));
168 this.util.exec("[name=" + calleename + "]", this.util.setVisible(false));
169
a205506c 170 __qwebircAuthCallback = function(username, expiry, serverNow) {
65f2c94c
CP
171 this.__cancelLoginCallback(true);
172
a205506c
CP
173 var now = new Date().getTime();
174 var offset = (serverNow * 1000) - now;
175 var ourExpiry = expiry * 1000 - offset;
176 Cookie.write("ticketexpiry", ourExpiry)
177
65f2c94c
CP
178 this.util.exec("[name=loggingin]", this.util.setVisible(false));
179 this.util.exec("[name=loginstatus]", this.util.setVisible(true));
180 this.util.exec("[name=authname]", this.util.setText(username));
181 callback();
182 }.bind(this);
183
184 },
e89c812f
CP
185 __validateConfirmData: function() {
186 return {nickname: this.options.initialNickname, autojoin: this.options.initialChannels};
187 },
188 __validateLoginData: function() {
189 var nick = this.rootElement.getElement("input[name=nickname]"), chan = this.rootElement.getElement("input[name=channels]");
66de775f 190
e89c812f
CP
191 var nickname = nick.value;
192 var chans = chan.value;
193 if(chans == "#") /* sorry channel "#" :P */
194 chans = "";
66de775f 195
e89c812f
CP
196 if(!nickname) {
197 alert("You must supply a nickname.");
198 nick.focus();
199 return false;
200 }
66de775f 201
fc25cf01
CP
202 var stripped = qwebirc.global.nicknameValidator.validate(nickname);
203 if(stripped != nickname) {
204 nick.value = stripped;
205 alert("Your nickname was invalid and has been corrected; please check your altered nickname and try again.");
206 nick.focus();
207 return false;
2f74dea9
CP
208 }
209
e89c812f
CP
210 var data = {nickname: nickname, autojoin: chans};
211 return data;
212 },
213 __buildPrettyChannels: function(node, channels) {
214 var c = channels.split(" ")[0].split(",");
215 node.appendChild(document.createTextNode("channel" + ((c.length>1)?"s":"") + " "));
216 for(var i=0;i<c.length;i++) {
217 if((c.length > 1) && (i == c.length - 1)) {
218 node.appendChild(document.createTextNode(" and "));
219 } else if(i > 0) {
220 node.appendChild(document.createTextNode(", "));
221 }
222 node.appendChild(new Element("b").set("text", c[i]));
2f74dea9 223 }
66de775f 224 }
e89c812f 225});
66de775f 226
e89c812f
CP
227qwebirc.ui.LoginBox2 = function(parentElement, callback, initialNickname, initialChannels, networkName) {
228/*
6ce70043
CP
229 if(qwebirc.auth.enabled()) {
230 if(qwebirc.auth.passAuth()) {
231 var authRow = createRow("Auth to services:");
232 var authCheckBox = qwebirc.util.createInput("checkbox", authRow, "connect_auth_to_services", false);
2f74dea9 233
6ce70043
CP
234 var usernameBox = new Element("input");
235 var usernameRow = createRow("Username:", usernameBox, {display: "none"})[0];
2f74dea9 236
6ce70043
CP
237 var passwordRow = createRow("Password:", null, {display: "none"});
238 var passwordBox = qwebirc.util.createInput("password", passwordRow[1], "connect_auth_password");
2f74dea9 239
6ce70043
CP
240 authCheckBox.addEvent("click", function(e) { qwebirc.ui.authShowHide(authCheckBox, authRow, usernameBox, usernameRow, passwordRow[0]) });
241 } else if(qwebirc.auth.bouncerAuth()) {
242 var passwordRow = createRow("Password:");
243 var passwordBox = qwebirc.util.createInput("password", passwordRow, "connect_auth_password");
244 }
2f74dea9 245 }
e89c812f
CP
246 */
247
66de775f
CP
248 var connbutton = new Element("input", {"type": "submit"});
249 connbutton.set("value", "Connect");
2f74dea9
CP
250 var r = createRow(undefined, connbutton);
251
66de775f
CP
252 form.addEvent("submit", function(e) {
253 new Event(e).stop();
66de775f 254
66de775f
CP
255 var nickname = nick.value;
256 var chans = chan.value;
257 if(chans == "#") /* sorry channel "#" :P */
258 chans = "";
259
260 if(!nickname) {
261 alert("You must supply a nickname.");
262 nick.focus();
263 return;
264 }
cc608160
CP
265 var stripped = qwebirc.global.nicknameValidator.validate(nickname);
266 if(stripped != nickname) {
267 nick.value = stripped;
268 alert("Your nickname was invalid and has been corrected; please check your altered nickname and press Connect again.");
269 nick.focus();
270 return;
271 }
272
2f74dea9 273 var data = {"nickname": nickname, "autojoin": chans};
6f848bbb
CP
274 if(qwebirc.auth.enabled()) {
275 if(qwebirc.auth.passAuth() && authCheckBox.checked) {
276 if(!usernameBox.value || !passwordBox.value) {
277 alert("You must supply your username and password in auth mode.");
278 if(!usernameBox.value) {
279 usernameBox.focus();
280 } else {
281 passwordBox.focus();
282 }
283 return;
6ce70043 284 }
6f848bbb
CP
285
286 data["serverPassword"] = usernameBox.value + " " + passwordBox.value;
287 } else if(qwebirc.auth.bouncerAuth()) {
288 if(!passwordBox.value) {
289 alert("You must supply a password.");
290 passwordBox.focus();
6ce70043
CP
291 return;
292 }
293
6f848bbb 294 data["serverPassword"] = passwordBox.value;
6ce70043 295 }
6ce70043 296 }
6ce70043
CP
297 parentElement.removeChild(outerbox);
298
2f74dea9 299 callback(data);
66de775f 300 }.bind(this));
6ce70043 301
66de775f
CP
302 nick.set("value", initialNickname);
303 chan.set("value", initialChannels);
304
b1a46dae 305 if(window == window.top)
9a1c691c 306 nick.focus();
cad18909 307}
2f74dea9
CP
308
309qwebirc.ui.authShowHide = function(checkbox, authRow, usernameBox, usernameRow, passwordRow) {
310 var visible = checkbox.checked;
311 var display = visible?null:"none";
312 usernameRow.setStyle("display", display);
313 passwordRow.setStyle("display", display);
314
315 if(visible) {
316// authRow.parentNode.setStyle("display", "none");
317 usernameBox.focus();
318 }
319}
65f2c94c
CP
320
321qwebirc.ui.isAuthRequired = (function() {
322 var args = qwebirc.util.parseURI(String(document.location));
323 var value = $defined(args) && args["authrequired"];
324 return function() {
325 return value && qwebirc.auth.enabled();
326 };
327})();
328