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