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