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