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