]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/qwebircinterface.js
iframes should not have borders -- breaks page layout
[irc/quakenet/qwebirc.git] / js / qwebircinterface.js
CommitLineData
791c2c6c 1function 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 11qwebirc.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 31 dynamicBaseURL: "/",
77e5d85f 32 staticBaseURL: "/",
13afa38f
CP
33 cloak: false,
34 logoURL: null,
35 accountWhoisCommand: null
d65fe45f
CP
36 },
37 initialize: function(element, ui, options) {
38 this.setOptions(options);
48a9e89a 39 var extractHost = function() {
bd512865
CP
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);
75ac24a3 49
48a9e89a
CP
50 if(QWEBIRC_DEBUG && uri.endsWith(".html")) {
51 var last = uri.lastIndexOf("/");
52 uri = uri.substring(0, last + 1);
53 }
75ac24a3
CP
54 if(uri.substr(uri.length - 1) != "/")
55 uri = uri + "/";
56
57 return uri;
58 };
59
48a9e89a 60 options.baseURL = extractHost();
cc608160
CP
61
62 /* HACK */
63 qwebirc.global = {
64 dynamicBaseURL: options.dynamicBaseURL,
65 staticBaseURL: options.staticBaseURL,
62d18cb0 66 baseURL: options.baseURL,
cc608160
CP
67 nicknameValidator: $defined(options.nickValidation) ? new qwebirc.irc.NicknameValidator(options.nickValidation) : new qwebirc.irc.DummyNicknameValidator()
68 };
d65fe45f
CP
69
70 window.addEvent("domready", function() {
66de775f 71 var callback = function(options) {
77e5d85f 72 options.cloak = ui_.options.cloak;
e20e5a6b 73 var IRC = new qwebirc.irc.IRCClient(options, ui_);
d65fe45f 74 IRC.connect();
791c2c6c
CP
75 window.onbeforeunload = qwebirc_ui_onbeforeunload;
76 window.addEvent("unload", function() {
d65fe45f
CP
77 IRC.quit("Page closed");
78 });
66de775f
CP
79 };
80
6500b600
CP
81 var inick = null;
82 var ichans = this.options.initialChannels;
83 var autoConnect = false;
84
66de775f 85 if(this.options.searchURL) {
e20e5a6b 86 var args = qwebirc.util.parseURI(String(document.location));
656385a2
CP
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");
c0f2f367 94
ea29e3d7
CP
95 if(args.contains("uio"))
96 this.options.uiOptionsArg = args.get("uio");
c0f2f367 97
ea29e3d7
CP
98 var url = args.get("url");
99 var chans, nick = args.get("nick");
8d614417
CP
100
101 if($defined(url)) {
102 ichans = this.parseIRCURL(url);
103 if($defined(chans) && chans != "")
104 canAutoConnect = true;
105 } else {
ea29e3d7 106 chans = args.get("channels");
66de775f 107
8d614417 108 var canAutoConnect = false;
6500b600 109
8d614417
CP
110 if(chans) {
111 var cdata = chans.split(" ");
290c60db 112
8d614417
CP
113 chans = cdata[0].split(",");
114 var chans2 = [];
66de775f 115
29453513 116 for(var i=0;i<chans.length;i++) {
8d614417 117 chans2[i] = chans[i];
531be34e
CP
118
119 var prefix = chans[i].charAt(0);
120 if(prefix != '#' && prefix != '&')
8d614417
CP
121 chans2[i] = "#" + chans2[i]
122 }
123 cdata[0] = chans2.join(",");
124 ichans = cdata.join(" ");
125 canAutoConnect = true;
66de775f 126 }
66de775f
CP
127 }
128
6500b600 129 if($defined(nick))
05d4b0b5
CP
130 inick = this.randSub(nick);
131
ea29e3d7 132 if(args.contains("randomnick") && args.get("randomnick") == 1)
6500b600 133 inick = this.options.initialNickname;
77e5d85f
CP
134
135 if(args.contains("cloak") && args.get("cloak") == 1)
136 this.options.cloak = true;
137
6500b600
CP
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 != "")))) {
ea29e3d7 140 var p = args.get("prompt");
6500b600
CP
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 }
66de775f 159 }
6500b600 160
4dd199c3
CP
161 var ui_ = new ui($(element), new qwebirc.ui.Theme(this.options.theme), this.options);
162
6500b600
CP
163 var usingAutoNick = !$defined(nick);
164 if(usingAutoNick && autoConnect)
165 inick = this.options.initialNickname;
bede573a 166
6500b600 167 var details = ui_.loginBox(callback, inick, ichans, autoConnect, usingAutoNick);
d65fe45f 168 }.bind(this));
05d4b0b5 169 },
656385a2 170 getHueArg: function(args, t) {
ea29e3d7 171 var hue = args.get(t + "hue");
4dd199c3
CP
172 if(!$defined(hue))
173 return null;
174 hue = parseInt(hue);
175 if(hue > 360 || hue < 0)
176 return null;
177 return hue;
178 },
656385a2 179 getSaturationArg: function(args, t) {
ea29e3d7 180 var saturation = args.get(t + "saturation");
fab4aaa6
CP
181 if(!$defined(saturation))
182 return null;
183 saturation = parseInt(saturation);
184 if(saturation > 100 || saturation < -100)
185 return null;
186 return saturation;
187 },
656385a2 188 getLightnessArg: function(args, t) {
ea29e3d7 189 var lightness = args.get(t + "lightness");
fab4aaa6
CP
190 if(!$defined(lightness))
191 return null;
192 lightness = parseInt(lightness);
193 if(lightness > 100 || lightness < -100)
194 return null;
195 return lightness;
196 },
05d4b0b5
CP
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
8d614417
CP
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)) {
ea29e3d7 253 queryArgs.each(function(key_, value) {
8d614417
CP
254 if(key_ == "key") {
255 key = value;
256 needkey = true;
257 } else {
258 not_supported.push(key_);
259 }
ea29e3d7 260 });
8d614417
CP
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;
d65fe45f
CP
274 }
275});