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.open-ircnet.net", 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++)
120 var lowerNetName = userNetworkList[i].name.toLowerCase();
121 networks[lowerNetName] = userNetworkList[i];
122 networks[lowerNetName].name = lowerNetName;
125 /* Flag up all networks that are built-in, so they can be handled properly.
126 * We need to do this last so that it ensures networks overridden by the
127 * user's networks.txt are still flagged properly.
129 for (var i = 0; i < builtInNames.length; i++)
130 networks[builtInNames[i]].isBuiltIn = true;
132 // Push network list over to client.networkList.
133 client.networkList = new Array();
134 for (var name in networks)
135 client.networkList.push(networks[name]);
137 // Sync to client.networks.
138 networksSyncFromList();
141 function networksSyncToList()
143 // Stores indexes of networks that should be kept.
144 var networkMap = new Object();
146 // Copy to and update client.networkList from client.networks.
147 for (var name in client.networks)
149 var net = client.networks[name];
150 /* Skip temporary networks, as they're created to wrap standalone
156 // Find the network in the networkList, if it exists.
158 for (var i = 0; i < client.networkList.length; i++)
160 if (client.networkList[i].name == name)
162 listNet = client.networkList[i];
163 networkMap[i] = true;
168 // Network not in list, so construct a shiny new one.
171 var listNet = { name: name, displayName: name, isupportsKey: "" };
173 // Collect the RPL_ISUPPORTS "network name" if available.
174 if (("primServ" in net) && net.primServ &&
175 ("supports" in net.primServ) && net.primServ.supports &&
176 ("network" in net.primServ.supports))
178 listNet.isupportsKey = net.primServ.supports["network"];
181 client.networkList.push(listNet);
182 networkMap[client.networkList.length - 1] = true;
185 // Populate server list (no merging here).
186 listNet.servers = new Array();
187 for (i = 0; i < net.serverList.length; i++)
189 var serv = net.serverList[i];
191 // Find this server in the list...
193 for (var j = 0; j < listNet.servers.length; j++)
195 if ((serv.hostname == listNet.servers[j].hostname) &&
196 (serv.port == listNet.servers[j].port))
198 listServ = listNet.servers[j];
203 // ...and add a new one if it isn't found.
204 if (listServ == null)
206 listServ = { hostname: serv.hostname, port: serv.port,
207 isSecure: serv.isSecure, password: null };
208 listNet.servers.push(listServ);
211 listServ.isSecure = serv.isSecure;
212 // Update the saved password (!! FIXME: plaintext password !!).
213 listServ.password = serv.password;
217 // Remove any no-longer existing networks.
218 var index = 0; // (current pointer into client.networkList)
219 var mapIndex = 0; // (original position pointer)
220 while (index < client.networkList.length)
222 if (mapIndex in networkMap)
229 var listNet = client.networkList[index];
230 // Not seen this network in the client.networks collection.
231 if (("isBuiltIn" in listNet) && listNet.isBuiltIn)
233 // Network is a built-in. Replace with dummy.
234 client.networkList[index] = { name: listNet.name, isDeleted: true };
239 // Network is not a built-in, just nuke.
240 // Note: don't do index++ here because we've removed the item.
241 client.networkList.splice(index, 1);
247 function networksSyncFromList()
249 var networkMap = new Object();
251 // Copy to and update client.networks from client.networkList.
252 for (var i = 0; i < client.networkList.length; i++)
254 var listNet = client.networkList[i];
255 networkMap[listNet.name] = true;
257 if ("isDeleted" in listNet)
259 /* This is a dummy entry that indicates a removed built-in network.
260 * Remove the network from client.networks if it exists, and then
261 * skip onto the next...
263 if (listNet.name in client.networks)
264 client.removeNetwork(listNet.name);
268 // Create new network object if necessary.
270 if (!(listNet.name in client.networks))
271 client.addNetwork(listNet.name, []);
273 // Get network object and make sure server list is empty.
274 net = client.networks[listNet.name];
275 net.clearServerList();
277 // Make sure real network knows if it is a built-in one.
278 if ("isBuiltIn" in listNet)
279 net.isBuiltIn = listNet.isBuiltIn;
281 // Update server list.
282 for (var j = 0; j < listNet.servers.length; j++)
284 var listServ = listNet.servers[j];
286 // Make sure these exist.
287 if (!("isSecure" in listServ))
288 listServ.isSecure = false;
289 if (!("password" in listServ))
290 listServ.password = null;
292 // NOTE: this must match the name given by CIRCServer.
293 var servName = listServ.hostname + ":" + listServ.port;
296 if (!(servName in net.servers))
298 net.addServer(listServ.hostname, listServ.port,
299 listServ.isSecure, listServ.password);
301 serv = net.servers[servName];
303 serv.isSecure = listServ.isSecure;
304 serv.password = listServ.password;
308 // Remove network objects that aren't in networkList.
309 for (var name in client.networks)
311 // Skip temporary networks, as they don't matter.
312 if (client.networks[name].temporary)
314 if (!(name in networkMap))
315 client.removeNetwork(name);
319 function networksSaveList()
323 var networksFile = new nsLocalFile(client.prefs["profilePath"]);
324 networksFile.append("networks.txt");
325 var networksLoader = new TextSerializer(networksFile);
326 if (networksLoader.open(">"))
328 networksLoader.serialize(client.networkList);
329 networksLoader.close();
334 display("ERROR: " + formatException(ex), MT_ERROR);