]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/qwebircinterface.js
Use Google's mootools package.
[irc/quakenet/qwebirc.git] / js / qwebircinterface.js
CommitLineData
791c2c6c
CP
1function 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
e20e5a6b 9qwebirc.ui.Interface = new Class({
d65fe45f
CP
10 Implements: [Options],
11 options: {
12 initialNickname: "qwebirc" + Math.ceil(Math.random() * 100000),
13 initialChannels: "",
348574ee
CP
14 networkName: "ExampleNetwork",
15 networkServices: [],
16 loginRegex: null,
17 appTitle: "ExampleNetwork Web IRC",
66de775f 18 searchURL: true,
2dfab0e1 19 theme: undefined,
4dd199c3 20 baseURL: null,
fbe5af77 21 hue: null,
fab4aaa6
CP
22 saturation: null,
23 lightness: null,
c0f2f367 24 uiOptionsArg: null,
cc608160 25 nickValidation: null,
fbe5af77
CP
26 dynamicBaseURL: "/",
27 staticBaseURL: "/"
d65fe45f
CP
28 },
29 initialize: function(element, ui, options) {
30 this.setOptions(options);
cc608160
CP
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 };
d65fe45f
CP
38
39 window.addEvent("domready", function() {
66de775f 40 var callback = function(options) {
e20e5a6b 41 var IRC = new qwebirc.irc.IRCClient(options, ui_);
d65fe45f 42 IRC.connect();
791c2c6c
CP
43 window.onbeforeunload = qwebirc_ui_onbeforeunload;
44 window.addEvent("unload", function() {
d65fe45f
CP
45 IRC.quit("Page closed");
46 });
66de775f
CP
47 };
48
6500b600
CP
49 var inick = null;
50 var ichans = this.options.initialChannels;
51 var autoConnect = false;
52
66de775f 53 if(this.options.searchURL) {
e20e5a6b 54 var args = qwebirc.util.parseURI(String(document.location));
4dd199c3 55 this.options.hue = this.getHueArg(args);
fab4aaa6
CP
56 this.options.saturation = this.getSaturationArg(args);
57 this.options.lightness = this.getLightnessArg(args);
c0f2f367
CP
58
59 if($defined(args["uio"]))
60 this.options.uiOptionsArg = args["uio"];
61
8d614417
CP
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"];
66de775f 71
8d614417 72 var canAutoConnect = false;
6500b600 73
8d614417
CP
74 if(chans) {
75 var cdata = chans.split(" ");
290c60db 76
8d614417
CP
77 chans = cdata[0].split(",");
78 var chans2 = [];
66de775f 79
29453513 80 for(var i=0;i<chans.length;i++) {
8d614417 81 chans2[i] = chans[i];
66de775f 82
8d614417
CP
83 if(chans[i].charAt(0) != '#')
84 chans2[i] = "#" + chans2[i]
85 }
86 cdata[0] = chans2.join(",");
87 ichans = cdata.join(" ");
88 canAutoConnect = true;
66de775f 89 }
66de775f
CP
90 }
91
6500b600 92 if($defined(nick))
05d4b0b5
CP
93 inick = this.randSub(nick);
94
6500b600
CP
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 }
66de775f 119 }
6500b600 120
4dd199c3
CP
121 var ui_ = new ui($(element), new qwebirc.ui.Theme(this.options.theme), this.options);
122
6500b600
CP
123 var usingAutoNick = !$defined(nick);
124 if(usingAutoNick && autoConnect)
125 inick = this.options.initialNickname;
bede573a 126
6500b600 127 var details = ui_.loginBox(callback, inick, ichans, autoConnect, usingAutoNick);
d65fe45f 128 }.bind(this));
05d4b0b5 129 },
4dd199c3
CP
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 },
fab4aaa6
CP
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 },
05d4b0b5
CP
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
8d614417
CP
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;
d65fe45f
CP
236 }
237});