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