1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is ChatZilla.
16 * The Initial Developer of the Original Code is James Ross.
17 * Portions created by the Initial Developer are Copyright (C) 2006
18 * the Initial Developer. All Rights Reserved.
21 * James Ross <silver@warwickcompsoc.co.uk>
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 function initNetworks()
39 var networks = new Object();
41 // Set up default network list.
42 networks["moznet"] = {
43 displayName: "moznet",
44 isupportsKey: "Mozilla",
45 servers: [{hostname: "irc.mozilla.org", port:6667},
46 {hostname: "irc.mozilla.org", port:6697, isSecure: true}]};
47 networks["hybridnet"] = {
48 displayName: "hybridnet",
50 servers: [{hostname: "irc.ssc.net", port: 6667}]};
51 networks["slashnet"] = {
52 displayName: "slashnet",
54 servers: [{hostname: "irc.slashnet.org", port:6667}]};
55 networks["dalnet"] = {
56 displayName: "dalnet",
58 servers: [{hostname: "irc.dal.net", port:6667}]};
59 networks["undernet"] = {
60 displayName: "undernet",
62 servers: [{hostname: "irc.undernet.org", port:6667}]};
63 networks["webbnet"] = {
64 displayName: "webbnet",
66 servers: [{hostname: "irc.webbnet.info", port:6667}]};
67 networks["quakenet"] = {
68 displayName: "quakenet",
70 servers: [{hostname: "irc.quakenet.org", port:6667}]};
71 networks["ircnet"] = {
72 displayName: "ircnet",
74 servers: [{hostname: "irc.ircnet.com", port:6667}]};
75 networks["freenode"] = {
76 displayName: "freenode",
78 servers: [{hostname: "irc.freenode.net", port:6667}]};
79 networks["serenia"] = {
80 displayName: "serenia",
82 servers: [{hostname: "chat.serenia.net", port:9999, isSecure: true}]};
86 servers: [{hostname: "irc.prison.net", port: 6667},
87 {hostname: "irc.magic.ca", port: 6667}]};
88 networks["hispano"] = {
89 displayName: "hispano",
91 servers: [{hostname: "irc.irc-hispano.org", port: 6667}]};
93 for (var name in networks)
94 networks[name].name = name;
96 var builtInNames = keys(networks);
98 var userNetworkList = new Array();
100 // Load the user's network list.
101 var networksFile = new nsLocalFile(client.prefs["profilePath"]);
102 networksFile.append("networks.txt");
103 if (networksFile.exists())
105 var networksLoader = new TextSerializer(networksFile);
106 if (networksLoader.open("<"))
108 var item = networksLoader.deserialize();
109 if (isinstance(item, Array))
110 userNetworkList = item;
112 dd("Malformed networks file!");
113 networksLoader.close();
117 // Merge the user's network list with the default ones.
118 for (var i = 0; i < userNetworkList.length; i++)
119 networks[userNetworkList[i].name] = userNetworkList[i];
121 /* Flag up all networks that are built-in, so they can be handled properly.
122 * We need to do this last so that it ensures networks overridden by the
123 * user's networks.txt are still flagged properly.
125 for (var i = 0; i < builtInNames.length; i++)
126 networks[builtInNames[i]].isBuiltIn = true;
128 // Push network list over to client.networkList.
129 client.networkList = new Array();
130 for (var name in networks)
131 client.networkList.push(networks[name]);
133 // Sync to client.networks.
134 networksSyncFromList();
137 function networksSyncToList()
139 // Stores indexes of networks that should be kept.
140 var networkMap = new Object();
142 // Copy to and update client.networkList from client.networks.
143 for (var name in client.networks)
145 var net = client.networks[name];
146 /* Skip temporary networks, as they're created to wrap standalone
152 // Find the network in the networkList, if it exists.
154 for (var i = 0; i < client.networkList.length; i++)
156 if (client.networkList[i].name == name)
158 listNet = client.networkList[i];
159 networkMap[i] = true;
164 // Network not in list, so construct a shiny new one.
167 var listNet = { name: name, displayName: name, isupportsKey: "" };
169 // Collect the RPL_ISUPPORTS "network name" if available.
170 if (("primServ" in net) && net.primServ &&
171 ("supports" in net.primServ) && net.primServ.supports &&
172 ("network" in net.primServ.supports))
174 listNet.isupportsKey = net.primServ.supports["network"];
177 client.networkList.push(listNet);
178 networkMap[client.networkList.length - 1] = true;
181 // Populate server list (no merging here).
182 listNet.servers = new Array();
183 for (i = 0; i < net.serverList.length; i++)
185 var serv = net.serverList[i];
187 // Find this server in the list...
189 for (var j = 0; j < listNet.servers.length; j++)
191 if ((serv.hostname == listNet.servers[j].hostname) &&
192 (serv.port == listNet.servers[j].port))
194 listServ = listNet.servers[j];
199 // ...and add a new one if it isn't found.
200 if (listServ == null)
202 listServ = { hostname: serv.hostname, port: serv.port,
203 isSecure: serv.isSecure, password: null };
204 listNet.servers.push(listServ);
207 listServ.isSecure = serv.isSecure;
208 // Update the saved password (!! FIXME: plaintext password !!).
209 listServ.password = serv.password;
213 // Remove any no-longer existing networks.
214 var index = 0; // (current pointer into client.networkList)
215 var mapIndex = 0; // (original position pointer)
216 while (index < client.networkList.length)
218 if (mapIndex in networkMap)
225 var listNet = client.networkList[index];
226 // Not seen this network in the client.networks collection.
227 if (("isBuiltIn" in listNet) && listNet.isBuiltIn)
229 // Network is a built-in. Replace with dummy.
230 client.networkList[index] = { name: listNet.name, isDeleted: true };
235 // Network is not a built-in, just nuke.
236 // Note: don't do index++ here because we've removed the item.
237 client.networkList.splice(index, 1);
243 function networksSyncFromList()
245 var networkMap = new Object();
247 // Copy to and update client.networks from client.networkList.
248 for (var i = 0; i < client.networkList.length; i++)
250 var listNet = client.networkList[i];
251 networkMap[listNet.name] = true;
253 if ("isDeleted" in listNet)
255 /* This is a dummy entry that indicates a removed built-in network.
256 * Remove the network from client.networks if it exists, and then
257 * skip onto the next...
259 if (listNet.name in client.networks)
260 client.removeNetwork(listNet.name);
264 // Create new network object if necessary.
266 if (!(listNet.name in client.networks))
267 client.addNetwork(listNet.name, []);
269 // Get network object and make sure server list is empty.
270 net = client.networks[listNet.name];
271 net.clearServerList();
273 // Make sure real network knows if it is a built-in one.
274 if ("isBuiltIn" in listNet)
275 net.isBuiltIn = listNet.isBuiltIn;
277 // Update server list.
278 for (var j = 0; j < listNet.servers.length; j++)
280 var listServ = listNet.servers[j];
282 // Make sure these exist.
283 if (!("isSecure" in listServ))
284 listServ.isSecure = false;
285 if (!("password" in listServ))
286 listServ.password = null;
288 // NOTE: this must match the name given by CIRCServer.
289 var servName = listServ.hostname + ":" + listServ.port;
292 if (!(servName in net.servers))
294 net.addServer(listServ.hostname, listServ.port,
295 listServ.isSecure, listServ.password);
297 serv = net.servers[servName];
299 serv.isSecure = listServ.isSecure;
300 serv.password = listServ.password;
304 // Remove network objects that aren't in networkList.
305 for (var name in client.networks)
307 // Skip temporary networks, as they don't matter.
308 if (client.networks[name].temporary)
310 if (!(name in networkMap))
311 client.removeNetwork(name);
315 function networksSaveList()
319 var networksFile = new nsLocalFile(client.prefs["profilePath"]);
320 networksFile.append("networks.txt");
321 var networksLoader = new TextSerializer(networksFile);
322 if (networksLoader.open(">"))
324 networksLoader.serialize(client.networkList);
325 networksLoader.close();
330 display("ERROR: " + formatException(ex), MT_ERROR);