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