]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/qwebircinterface.js
change faq pane into help pane
[irc/quakenet/qwebirc.git] / js / qwebircinterface.js
1 function qwebirc_ui_onbeforeunload(e) { /* IE sucks */
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 }
9 }
10
11 qwebirc.ui.Interface = new Class({
12 Implements: [Options],
13 options: {
14 initialNickname: "qwebirc" + Math.ceil(Math.random() * 100000),
15 initialChannels: "",
16 networkName: "ExampleNetwork",
17 networkServices: [],
18 loginRegex: null,
19 appTitle: "ExampleNetwork Web IRC",
20 searchURL: true,
21 theme: undefined,
22 baseURL: null,
23 hue: null,
24 saturation: null,
25 lightness: null,
26 thue: null,
27 tsaturation: null,
28 tlightness: null,
29 uiOptionsArg: null,
30 nickValidation: null,
31 helpURL: null,
32 dynamicBaseURL: "/",
33 staticBaseURL: "/"
34 },
35 initialize: function(element, ui, options) {
36 this.setOptions(options);
37
38 /* HACK */
39 qwebirc.global = {
40 dynamicBaseURL: options.dynamicBaseURL,
41 staticBaseURL: options.staticBaseURL,
42 helpURL: options.helpURL,
43 nicknameValidator: $defined(options.nickValidation) ? new qwebirc.irc.NicknameValidator(options.nickValidation) : new qwebirc.irc.DummyNicknameValidator()
44 };
45
46 window.addEvent("domready", function() {
47 var callback = function(options) {
48 var IRC = new qwebirc.irc.IRCClient(options, ui_);
49 IRC.connect();
50 window.onbeforeunload = qwebirc_ui_onbeforeunload;
51 window.addEvent("unload", function() {
52 IRC.quit("Page closed");
53 });
54 };
55
56 var inick = null;
57 var ichans = this.options.initialChannels;
58 var autoConnect = false;
59
60 if(this.options.searchURL) {
61 var args = qwebirc.util.parseURI(String(document.location));
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");
69
70 if($defined(args["uio"]))
71 this.options.uiOptionsArg = args["uio"];
72
73 var url = args["url"];
74 var chans, nick = args["nick"];
75
76 if($defined(url)) {
77 ichans = this.parseIRCURL(url);
78 if($defined(chans) && chans != "")
79 canAutoConnect = true;
80 } else {
81 chans = args["channels"];
82
83 var canAutoConnect = false;
84
85 if(chans) {
86 var cdata = chans.split(" ");
87
88 chans = cdata[0].split(",");
89 var chans2 = [];
90
91 for(var i=0;i<chans.length;i++) {
92 chans2[i] = chans[i];
93
94 var prefix = chans[i].charAt(0);
95 if(prefix != '#' && prefix != '&')
96 chans2[i] = "#" + chans2[i]
97 }
98 cdata[0] = chans2.join(",");
99 ichans = cdata.join(" ");
100 canAutoConnect = true;
101 }
102 }
103
104 if($defined(nick))
105 inick = this.randSub(nick);
106
107 if(args["randomnick"] && args["randomnick"] == 1)
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 != "")))) {
112 var p = args["prompt"];
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 }
131 }
132
133 var ui_ = new ui($(element), new qwebirc.ui.Theme(this.options.theme), this.options);
134
135 var usingAutoNick = !$defined(nick);
136 if(usingAutoNick && autoConnect)
137 inick = this.options.initialNickname;
138
139 var details = ui_.loginBox(callback, inick, ichans, autoConnect, usingAutoNick);
140 }.bind(this));
141 },
142 getHueArg: function(args, t) {
143 var hue = args[t + "hue"];
144 if(!$defined(hue))
145 return null;
146 hue = parseInt(hue);
147 if(hue > 360 || hue < 0)
148 return null;
149 return hue;
150 },
151 getSaturationArg: function(args, t) {
152 var saturation = args[t + "saturation"];
153 if(!$defined(saturation))
154 return null;
155 saturation = parseInt(saturation);
156 if(saturation > 100 || saturation < -100)
157 return null;
158 return saturation;
159 },
160 getLightnessArg: function(args, t) {
161 var lightness = args[t + "lightness"];
162 if(!$defined(lightness))
163 return null;
164 lightness = parseInt(lightness);
165 if(lightness > 100 || lightness < -100)
166 return null;
167 return lightness;
168 },
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
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)) {
225 for(var key_ in queryArgs) {
226 var value = queryArgs[key_];
227
228 if(key_ == "key") {
229 key = value;
230 needkey = true;
231 } else {
232 not_supported.push(key_);
233 }
234 }
235 }
236
237 if(needkey) {
238 if(!$defined(key))
239 key = prompt("Please enter the password for channel " + channel + ":");
240 if($defined(key))
241 channel = channel + " " + key;
242 }
243
244 if(not_supported.length > 0)
245 alert("The following IRC URL components were not accepted: " + not_supported.join(", ") + ".");
246
247 return channel;
248 }
249 });