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