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