]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/qwebircinterface.js
Merge.
[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 uiOptionsArg: null,
25 dynamicBaseURL: "/",
26 staticBaseURL: "/"
27 },
28 initialize: function(element, ui, options) {
29 qwebirc.global = {dynamicBaseURL: options.dynamicBaseURL, staticBaseURL: options.staticBaseURL}; /* HACK */
30
31 this.setOptions(options);
32
33 window.addEvent("domready", function() {
34 var callback = function(options) {
35 var IRC = new qwebirc.irc.IRCClient(options, ui_);
36 IRC.connect();
37 window.onbeforeunload = qwebirc_ui_onbeforeunload;
38 window.addEvent("unload", function() {
39 IRC.quit("Page closed");
40 });
41 };
42
43 var inick = null;
44 var ichans = this.options.initialChannels;
45 var autoConnect = false;
46
47 if(this.options.searchURL) {
48 var args = qwebirc.util.parseURI(String(document.location));
49 this.options.hue = this.getHueArg(args);
50 this.options.saturation = this.getSaturationArg(args);
51 this.options.lightness = this.getLightnessArg(args);
52
53 if($defined(args["uio"]))
54 this.options.uiOptionsArg = args["uio"];
55
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"];
65
66 var canAutoConnect = false;
67
68 if(chans) {
69 var cdata = chans.split(" ");
70
71 chans = cdata[0].split(",");
72 var chans2 = [];
73
74 for(var i=0;i<chans.length;i++) {
75 chans2[i] = chans[i];
76
77 if(chans[i].charAt(0) != '#')
78 chans2[i] = "#" + chans2[i]
79 }
80 cdata[0] = chans2.join(",");
81 ichans = cdata.join(" ");
82 canAutoConnect = true;
83 }
84 }
85
86 if($defined(nick))
87 inick = this.randSub(nick);
88
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 }
113 }
114
115 var ui_ = new ui($(element), new qwebirc.ui.Theme(this.options.theme), this.options);
116
117 var usingAutoNick = !$defined(nick);
118 if(usingAutoNick && autoConnect)
119 inick = this.options.initialNickname;
120
121 var details = ui_.loginBox(callback, inick, ichans, autoConnect, usingAutoNick);
122 }.bind(this));
123 },
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 },
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 },
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
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;
230 }
231 });