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