]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/qwebircinterface.js
support CHANTYPES
[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 var prefix = chans[i].charAt(0);
93 if(prefix != '#' && prefix != '&')
94 chans2[i] = "#" + chans2[i]
95 }
96 cdata[0] = chans2.join(",");
97 ichans = cdata.join(" ");
98 canAutoConnect = true;
99 }
100 }
101
102 if($defined(nick))
103 inick = this.randSub(nick);
104
105 if(args["randomnick"] && args["randomnick"] == 1)
106 inick = this.options.initialNickname;
107
108 /* we only consider autoconnecting if the nick hasn't been supplied, or it has and it's not "" */
109 if(canAutoConnect && (!$defined(inick) || ($defined(inick) && (inick != "")))) {
110 var p = args["prompt"];
111 var pdefault = false;
112
113 if(!$defined(p) || p == "") {
114 pdefault = true;
115 p = false;
116 } else if(p == "0") {
117 p = false;
118 } else {
119 p = true;
120 }
121
122 /* autoconnect if we have channels and nick but only if prompt != 1 */
123 if($defined(inick) && !p) {
124 autoConnect = true;
125 } else if(!pdefault && !p) { /* OR if prompt=0, but not prompt=(nothing) */
126 autoConnect = true;
127 }
128 }
129 }
130
131 var ui_ = new ui($(element), new qwebirc.ui.Theme(this.options.theme), this.options);
132
133 var usingAutoNick = !$defined(nick);
134 if(usingAutoNick && autoConnect)
135 inick = this.options.initialNickname;
136
137 var details = ui_.loginBox(callback, inick, ichans, autoConnect, usingAutoNick);
138 }.bind(this));
139 },
140 getHueArg: function(args, t) {
141 var hue = args[t + "hue"];
142 if(!$defined(hue))
143 return null;
144 hue = parseInt(hue);
145 if(hue > 360 || hue < 0)
146 return null;
147 return hue;
148 },
149 getSaturationArg: function(args, t) {
150 var saturation = args[t + "saturation"];
151 if(!$defined(saturation))
152 return null;
153 saturation = parseInt(saturation);
154 if(saturation > 100 || saturation < -100)
155 return null;
156 return saturation;
157 },
158 getLightnessArg: function(args, t) {
159 var lightness = args[t + "lightness"];
160 if(!$defined(lightness))
161 return null;
162 lightness = parseInt(lightness);
163 if(lightness > 100 || lightness < -100)
164 return null;
165 return lightness;
166 },
167 randSub: function(nick) {
168 var getDigit = function() { return Math.floor(Math.random() * 10); }
169
170 return nick.split("").map(function(v) {
171 if(v == ".") {
172 return getDigit();
173 } else {
174 return v;
175 }
176 }).join("");
177
178 },
179 parseIRCURL: function(url) {
180 if(url.indexOf(":") == 0)
181 return;
182 var schemeComponents = url.splitMax(":", 2);
183 if(schemeComponents[0].toLowerCase() != "irc" && schemeComponents[0].toLowerCase() != "ircs") {
184 alert("Bad IRC URL scheme.");
185 return;
186 }
187
188 if(url.indexOf("/") == 0) {
189 /* irc: */
190 return;
191 }
192
193 var pathComponents = url.splitMax("/", 4);
194 if(pathComponents.length < 4 || pathComponents[3] == "") {
195 /* irc://abc */
196 return;
197 }
198
199 var args, queryArgs;
200 if(pathComponents[3].indexOf("?") > -1) {
201 queryArgs = qwebirc.util.parseURI(pathComponents[3]);
202 args = pathComponents[3].splitMax("?", 2)[0];
203 } else {
204 args = pathComponents[3];
205 }
206 var parts = args.split(",");
207
208 var channel = parts[0];
209 if(channel.charAt(0) != "#")
210 channel = "#" + channel;
211
212 var not_supported = [], needkey = false, key;
213 for(var i=1;i<parts.length;i++) {
214 var value = parts[i];
215 if(value == "needkey") {
216 needkey = true;
217 } else {
218 not_supported.push(value);
219 }
220 }
221
222 if($defined(queryArgs)) {
223 for(var key_ in queryArgs) {
224 var value = queryArgs[key_];
225
226 if(key_ == "key") {
227 key = value;
228 needkey = true;
229 } else {
230 not_supported.push(key_);
231 }
232 }
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;
246 }
247 });