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