]>
Commit | Line | Data |
---|---|---|
1 | function qwebirc_ui_onbeforeunload(e) { /* IE sucks */ | |
2 | if(qwebirc.connected) { | |
3 | var message = "This action will close all active IRC connections."; | |
4 | var e = e || window.event; | |
5 | if(e) | |
6 | e.returnValue = message; | |
7 | return message; | |
8 | } | |
9 | } | |
10 | ||
11 | qwebirc.ui.Interface = new Class({ | |
12 | Implements: [Options], | |
13 | options: { | |
14 | initialNickname: null, | |
15 | initialChannels: null, | |
16 | networkName: "ExampleNetwork", | |
17 | networkServices: [], | |
18 | loginRegex: null, | |
19 | appTitle: "ExampleNetwork Web IRC", | |
20 | searchURL: true, | |
21 | theme: undefined, | |
22 | baseURL: null, | |
23 | hue: null, | |
24 | saturation: null, | |
25 | lightness: null, | |
26 | thue: null, | |
27 | tsaturation: null, | |
28 | tlightness: null, | |
29 | uiOptionsArg: null, | |
30 | nickValidation: null, | |
31 | dynamicBaseURL: "/", | |
32 | staticBaseURL: "/", | |
33 | dynamicConfiguration: false, | |
34 | logoURL: null, | |
35 | accountWhoisCommand: null | |
36 | }, | |
37 | initialize: function(element, ui, options) { | |
38 | this.setOptions(options); | |
39 | var extractHost = function() { | |
40 | var uri = document.location.href; | |
41 | ||
42 | /* IE6 doesn't have document.origin ... */ | |
43 | var start = uri.indexOf('?'); | |
44 | if(start != -1) | |
45 | uri = uri.substring(0, start); | |
46 | var start = uri.indexOf('#'); | |
47 | if(start != -1) | |
48 | uri = uri.substring(0, start); | |
49 | ||
50 | if(QWEBIRC_DEBUG && uri.endsWith(".html")) { | |
51 | var last = uri.lastIndexOf("/"); | |
52 | uri = uri.substring(0, last + 1); | |
53 | } | |
54 | if(uri.substr(uri.length - 1) != "/") | |
55 | uri = uri + "/"; | |
56 | ||
57 | return uri; | |
58 | }; | |
59 | ||
60 | options.baseURL = extractHost(); | |
61 | ||
62 | /* HACK */ | |
63 | qwebirc.global = { | |
64 | dynamicBaseURL: options.dynamicBaseURL, | |
65 | dynamicConfiguration: options.dynamicConfiguration, | |
66 | staticBaseURL: options.staticBaseURL, | |
67 | baseURL: options.baseURL, | |
68 | nicknameValidator: $defined(options.nickValidation) ? new qwebirc.irc.NicknameValidator(options.nickValidation) : new qwebirc.irc.DummyNicknameValidator(), | |
69 | dynamicConfigurationLoaded: false | |
70 | }; | |
71 | ||
72 | window.addEvent("domready", function() { | |
73 | var callback = function(options) { | |
74 | var IRC = new qwebirc.irc.IRCClient(options, ui_); | |
75 | IRC.connect(); | |
76 | window.onbeforeunload = qwebirc_ui_onbeforeunload; | |
77 | window.addEvent("unload", function() { | |
78 | IRC.quit("Page closed"); | |
79 | }); | |
80 | }; | |
81 | ||
82 | var inick = null; | |
83 | var ichans = this.options.initialChannels; | |
84 | var autoConnect = false; | |
85 | ||
86 | if(this.options.searchURL) { | |
87 | var args = qwebirc.util.parseURI(String(document.location)); | |
88 | this.options.hue = this.getHueArg(args, ""); | |
89 | this.options.saturation = this.getSaturationArg(args, ""); | |
90 | this.options.lightness = this.getLightnessArg(args, ""); | |
91 | ||
92 | this.options.thue = this.getHueArg(args, "t"); | |
93 | this.options.tsaturation = this.getSaturationArg(args, "t"); | |
94 | this.options.tlightness = this.getLightnessArg(args, "t"); | |
95 | ||
96 | if(args.contains("uio")) | |
97 | this.options.uiOptionsArg = args.get("uio"); | |
98 | ||
99 | var url = args.get("url"); | |
100 | var chans, nick = args.get("nick"); | |
101 | ||
102 | if($defined(url)) { | |
103 | ichans = this.parseIRCURL(url); | |
104 | if($defined(chans) && chans != "") | |
105 | canAutoConnect = true; | |
106 | } else { | |
107 | chans = args.get("channels"); | |
108 | ||
109 | var canAutoConnect = false; | |
110 | ||
111 | if(chans) { | |
112 | var cdata = chans.split(" "); | |
113 | ||
114 | chans = cdata[0].split(","); | |
115 | var chans2 = []; | |
116 | ||
117 | for(var i=0;i<chans.length;i++) { | |
118 | chans2[i] = chans[i]; | |
119 | ||
120 | var prefix = chans[i].charAt(0); | |
121 | if(prefix != '#' && prefix != '&') | |
122 | chans2[i] = "#" + chans2[i] | |
123 | } | |
124 | cdata[0] = chans2.join(","); | |
125 | ichans = cdata.join(" "); | |
126 | canAutoConnect = true; | |
127 | } | |
128 | } | |
129 | ||
130 | if($defined(nick)) | |
131 | inick = this.randSub(nick); | |
132 | ||
133 | if(args.contains("randomnick") && args.get("randomnick") == 1) | |
134 | inick = this.options.initialNickname; | |
135 | ||
136 | /* we only consider autoconnecting if the nick hasn't been supplied, or it has and it's not "" */ | |
137 | if(canAutoConnect && (!$defined(inick) || ($defined(inick) && (inick != "")))) { | |
138 | var p = args.get("prompt"); | |
139 | var pdefault = false; | |
140 | ||
141 | if(!$defined(p) || p == "") { | |
142 | pdefault = true; | |
143 | p = false; | |
144 | } else if(p == "0") { | |
145 | p = false; | |
146 | } else { | |
147 | p = true; | |
148 | } | |
149 | ||
150 | /* autoconnect if we have channels and nick but only if prompt != 1 */ | |
151 | if($defined(inick) && !p) { | |
152 | autoConnect = true; | |
153 | } else if(!pdefault && !p) { /* OR if prompt=0, but not prompt=(nothing) */ | |
154 | autoConnect = true; | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | var ui_ = new ui($(element), new qwebirc.ui.Theme(this.options.theme), this.options); | |
160 | ||
161 | var usingAutoNick = !$defined(nick); | |
162 | if(usingAutoNick && autoConnect) | |
163 | inick = this.options.initialNickname; | |
164 | ||
165 | var details = ui_.loginBox(callback, inick, ichans, autoConnect, usingAutoNick); | |
166 | }.bind(this)); | |
167 | }, | |
168 | getHueArg: function(args, t) { | |
169 | var hue = args.get(t + "hue"); | |
170 | if(!$defined(hue)) | |
171 | return null; | |
172 | hue = parseInt(hue); | |
173 | if(hue > 360 || hue < 0) | |
174 | return null; | |
175 | return hue; | |
176 | }, | |
177 | getSaturationArg: function(args, t) { | |
178 | var saturation = args.get(t + "saturation"); | |
179 | if(!$defined(saturation)) | |
180 | return null; | |
181 | saturation = parseInt(saturation); | |
182 | if(saturation > 100 || saturation < -100) | |
183 | return null; | |
184 | return saturation; | |
185 | }, | |
186 | getLightnessArg: function(args, t) { | |
187 | var lightness = args.get(t + "lightness"); | |
188 | if(!$defined(lightness)) | |
189 | return null; | |
190 | lightness = parseInt(lightness); | |
191 | if(lightness > 100 || lightness < -100) | |
192 | return null; | |
193 | return lightness; | |
194 | }, | |
195 | randSub: function(nick) { | |
196 | var getDigit = function() { return Math.floor(Math.random() * 10); } | |
197 | ||
198 | return nick.split("").map(function(v) { | |
199 | if(v == ".") { | |
200 | return getDigit(); | |
201 | } else { | |
202 | return v; | |
203 | } | |
204 | }).join(""); | |
205 | ||
206 | }, | |
207 | parseIRCURL: function(url) { | |
208 | if(url.indexOf(":") == 0) | |
209 | return; | |
210 | var schemeComponents = url.splitMax(":", 2); | |
211 | if(schemeComponents[0].toLowerCase() != "irc" && schemeComponents[0].toLowerCase() != "ircs") { | |
212 | alert("Bad IRC URL scheme."); | |
213 | return; | |
214 | } | |
215 | ||
216 | if(url.indexOf("/") == 0) { | |
217 | /* irc: */ | |
218 | return; | |
219 | } | |
220 | ||
221 | var pathComponents = url.splitMax("/", 4); | |
222 | if(pathComponents.length < 4 || pathComponents[3] == "") { | |
223 | /* irc://abc */ | |
224 | return; | |
225 | } | |
226 | ||
227 | var args, queryArgs; | |
228 | if(pathComponents[3].indexOf("?") > -1) { | |
229 | queryArgs = qwebirc.util.parseURI(pathComponents[3]); | |
230 | args = pathComponents[3].splitMax("?", 2)[0]; | |
231 | } else { | |
232 | args = pathComponents[3]; | |
233 | } | |
234 | var parts = args.split(","); | |
235 | ||
236 | var channel = parts[0]; | |
237 | if(channel.charAt(0) != "#") | |
238 | channel = "#" + channel; | |
239 | ||
240 | var not_supported = [], needkey = false, key; | |
241 | for(var i=1;i<parts.length;i++) { | |
242 | var value = parts[i]; | |
243 | if(value == "needkey") { | |
244 | needkey = true; | |
245 | } else { | |
246 | not_supported.push(value); | |
247 | } | |
248 | } | |
249 | ||
250 | if($defined(queryArgs)) { | |
251 | queryArgs.each(function(key_, value) { | |
252 | if(key_ == "key") { | |
253 | key = value; | |
254 | needkey = true; | |
255 | } else { | |
256 | not_supported.push(key_); | |
257 | } | |
258 | }); | |
259 | } | |
260 | ||
261 | if(needkey) { | |
262 | if(!$defined(key)) | |
263 | key = prompt("Please enter the password for channel " + channel + ":"); | |
264 | if($defined(key)) | |
265 | channel = channel + " " + key; | |
266 | } | |
267 | ||
268 | if(not_supported.length > 0) | |
269 | alert("The following IRC URL components were not accepted: " + not_supported.join(", ") + "."); | |
270 | ||
271 | return channel; | |
272 | } | |
273 | }); | |
274 | ||
275 | qwebirc.ui.requireDynamicConfiguration = function(callback) { | |
276 | if (!qwebirc.global.dynamicConfiguration || qwebirc.global.dynamicConfigurationLoaded) { | |
277 | callback(); | |
278 | return; | |
279 | } | |
280 | ||
281 | var r = new Request.JSON({url: qwebirc.global.dynamicBaseURL + "configuration", onSuccess: function(data) { | |
282 | qwebirc.global.dynamicBaseURL = data["dynamicBaseURL"]; | |
283 | ||
284 | callback(); | |
285 | }}); | |
286 | r.get(); | |
287 | }; |