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