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