]>
Commit | Line | Data |
---|---|---|
791c2c6c CP |
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 | ||
e20e5a6b | 9 | qwebirc.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 CP |
20 | baseURL: null, |
21 | hue: null | |
d65fe45f CP |
22 | }, |
23 | initialize: function(element, ui, options) { | |
24 | this.setOptions(options); | |
25 | ||
26 | window.addEvent("domready", function() { | |
66de775f | 27 | var callback = function(options) { |
e20e5a6b | 28 | var IRC = new qwebirc.irc.IRCClient(options, ui_); |
d65fe45f | 29 | IRC.connect(); |
791c2c6c CP |
30 | window.onbeforeunload = qwebirc_ui_onbeforeunload; |
31 | window.addEvent("unload", function() { | |
d65fe45f CP |
32 | IRC.quit("Page closed"); |
33 | }); | |
66de775f CP |
34 | }; |
35 | ||
6500b600 CP |
36 | var inick = null; |
37 | var ichans = this.options.initialChannels; | |
38 | var autoConnect = false; | |
39 | ||
66de775f | 40 | if(this.options.searchURL) { |
e20e5a6b | 41 | var args = qwebirc.util.parseURI(String(document.location)); |
4dd199c3 | 42 | this.options.hue = this.getHueArg(args); |
8d614417 CP |
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"]; | |
66de775f | 52 | |
8d614417 | 53 | var canAutoConnect = false; |
6500b600 | 54 | |
8d614417 CP |
55 | if(chans) { |
56 | var cdata = chans.split(" "); | |
290c60db | 57 | |
8d614417 CP |
58 | chans = cdata[0].split(","); |
59 | var chans2 = []; | |
66de775f | 60 | |
29453513 | 61 | for(var i=0;i<chans.length;i++) { |
8d614417 | 62 | chans2[i] = chans[i]; |
66de775f | 63 | |
8d614417 CP |
64 | if(chans[i].charAt(0) != '#') |
65 | chans2[i] = "#" + chans2[i] | |
66 | } | |
67 | cdata[0] = chans2.join(","); | |
68 | ichans = cdata.join(" "); | |
69 | canAutoConnect = true; | |
66de775f | 70 | } |
66de775f CP |
71 | } |
72 | ||
6500b600 | 73 | if($defined(nick)) |
05d4b0b5 CP |
74 | inick = this.randSub(nick); |
75 | ||
6500b600 CP |
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 | } | |
66de775f | 100 | } |
6500b600 | 101 | |
4dd199c3 CP |
102 | var ui_ = new ui($(element), new qwebirc.ui.Theme(this.options.theme), this.options); |
103 | ||
6500b600 CP |
104 | var usingAutoNick = !$defined(nick); |
105 | if(usingAutoNick && autoConnect) | |
106 | inick = this.options.initialNickname; | |
bede573a | 107 | |
6500b600 | 108 | var details = ui_.loginBox(callback, inick, ichans, autoConnect, usingAutoNick); |
d65fe45f | 109 | }.bind(this)); |
05d4b0b5 | 110 | }, |
4dd199c3 CP |
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 | }, | |
05d4b0b5 CP |
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 | ||
8d614417 CP |
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; | |
d65fe45f CP |
199 | } |
200 | }); |