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