]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/qwebircinterface.js
Hacky support for foreground/background colours.
[irc/quakenet/qwebirc.git] / js / qwebircinterface.js
1 function 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
9 qwebirc.ui.Interface = new Class({
10 Implements: [Options],
11 options: {
12 initialNickname: "qwebirc" + Math.ceil(Math.random() * 100000),
13 initialChannels: "",
14 networkName: "ExampleNetwork",
15 networkServices: [],
16 loginRegex: null,
17 appTitle: "ExampleNetwork Web IRC",
18 searchURL: true,
19 theme: undefined,
20 baseURL: null,
21 hue: null,
22 saturation: null,
23 lightness: null,
24 thue: null,
25 tsaturation: null,
26 tlightness: null,
27 uiOptionsArg: null,
28 nickValidation: null,
29 dynamicBaseURL: "/",
30 staticBaseURL: "/"
31 },
32 initialize: function(element, ui, options) {
33 this.setOptions(options);
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 };
41
42 window.addEvent("domready", function() {
43 var callback = function(options) {
44 var IRC = new qwebirc.irc.IRCClient(options, ui_);
45 IRC.connect();
46 window.onbeforeunload = qwebirc_ui_onbeforeunload;
47 window.addEvent("unload", function() {
48 IRC.quit("Page closed");
49 });
50 };
51
52 var inick = null;
53 var ichans = this.options.initialChannels;
54 var autoConnect = false;
55
56 if(this.options.searchURL) {
57 var args = qwebirc.util.parseURI(String(document.location));
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");
65
66 if($defined(args["uio"]))
67 this.options.uiOptionsArg = args["uio"];
68
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"];
78
79 var canAutoConnect = false;
80
81 if(chans) {
82 var cdata = chans.split(" ");
83
84 chans = cdata[0].split(",");
85 var chans2 = [];
86
87 for(var i=0;i<chans.length;i++) {
88 chans2[i] = chans[i];
89
90 if(chans[i].charAt(0) != '#')
91 chans2[i] = "#" + chans2[i]
92 }
93 cdata[0] = chans2.join(",");
94 ichans = cdata.join(" ");
95 canAutoConnect = true;
96 }
97 }
98
99 if($defined(nick))
100 inick = this.randSub(nick);
101
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 }
126 }
127
128 var ui_ = new ui($(element), new qwebirc.ui.Theme(this.options.theme), this.options);
129
130 var usingAutoNick = !$defined(nick);
131 if(usingAutoNick && autoConnect)
132 inick = this.options.initialNickname;
133
134 var details = ui_.loginBox(callback, inick, ichans, autoConnect, usingAutoNick);
135 }.bind(this));
136 },
137 getHueArg: function(args, t) {
138 var hue = args[t + "hue"];
139 if(!$defined(hue))
140 return null;
141 hue = parseInt(hue);
142 if(hue > 360 || hue < 0)
143 return null;
144 return hue;
145 },
146 getSaturationArg: function(args, t) {
147 var saturation = args[t + "saturation"];
148 if(!$defined(saturation))
149 return null;
150 saturation = parseInt(saturation);
151 if(saturation > 100 || saturation < -100)
152 return null;
153 return saturation;
154 },
155 getLightnessArg: function(args, t) {
156 var lightness = args[t + "lightness"];
157 if(!$defined(lightness))
158 return null;
159 lightness = parseInt(lightness);
160 if(lightness > 100 || lightness < -100)
161 return null;
162 return lightness;
163 },
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
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;
243 }
244 });