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