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