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