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