home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / messenger.jar / content / messenger / ispUtils.js < prev    next >
Text File  |  2001-02-14  |  5KB  |  175 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  * Alec Flett <alecf@netscape.com>
  22.  */
  23.  
  24. // using the rdf service extensively here
  25. var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
  26.  
  27. // all the RDF resources we'll be retrieving
  28. var NC = "http://home.netscape.com/NC-rdf#";
  29. var Server              = rdf.GetResource(NC + "Server");
  30. var SmtpServer          = rdf.GetResource(NC + "SmtpServer");
  31. var ServerHost          = rdf.GetResource(NC + "ServerHost");
  32. var ServerType          = rdf.GetResource(NC + "ServerType");
  33. var PrefixIsUsername    = rdf.GetResource(NC + "PrefixIsUsername");
  34. var UseAuthenticatedSmtp= rdf.GetResource(NC + "UseAuthenticatedSmtp");
  35.  
  36. // this is possibly expensive, not sure what to do here
  37. var ispDefaults;
  38.  
  39. var nsIRDFResource = Components.interfaces.nsIRDFResource;
  40. var nsIRDFLiteral = Components.interfaces.nsIRDFLiteral;
  41.  
  42. var ispRoot = rdf.GetResource("NC:ispinfo");
  43.  
  44. // given an ISP's domain URI, look up all relevant information about it
  45. function getIspDefaultsForUri(domainURI)
  46. {
  47.     if (!ispDefaults) 
  48.         ispDefaults = rdf.GetDataSource("rdf:ispdefaults");
  49.     
  50.     var domainRes = rdf.GetResource(domainURI);
  51.  
  52.     var result = dataSourceToObject(ispDefaults, domainRes);
  53.  
  54.     if (!result) return null;
  55.     
  56.     // add this extra attribute which is the domain itself
  57.     var domainData = domainURI.split(':');
  58.     if (domainData.length > 1)
  59.     result.domain = domainData[1];
  60.     
  61.     return result;
  62. }
  63.  
  64. // construct an ISP's domain URI based on it's domain
  65. // (i.e. turns isp.com -> domain:isp.com)
  66. function getIspDefaultsForDomain(domain) {
  67.     domainURI = "domain:" + domain;
  68.     return getIspDefaultsForUri(domainURI);
  69. }
  70.  
  71. // Given an email address (like "joe@isp.com") look up 
  72. function getIspDefaultsForEmail(email) {
  73.  
  74.     var emailData = getEmailInfo(email);
  75.  
  76.     var ispData;
  77.     if (emailData)
  78.         ispData = getIspDefaultsForDomain(emailData.domain);
  79.  
  80.     prefillIspData(ispData, email);
  81.  
  82.     return ispData;
  83. }
  84.  
  85. // given an email address, split it into username and domain
  86. // return in an associative array
  87. function getEmailInfo(email) {
  88.     if (!email) return null;
  89.     
  90.     var result = new Object;
  91.     
  92.     var emailData = email.split('@');
  93.     
  94.     if (emailData.length != 2) {
  95.         dump("bad e-mail address!\n");
  96.         return null;
  97.     }
  98.     
  99.     // all the variables we'll be returning
  100.     result.username = emailData[0];
  101.     result.domain = emailData[1];
  102.  
  103.     return result;
  104. }
  105.  
  106. function prefillIspData(ispData, email, fullName) {
  107.     if (!ispData) return;
  108.  
  109.     // make sure these objects exist
  110.     if (!ispData.identity) ispData.identity = new Object;
  111.     if (!ispData.incomingServer) ispData.incomingServer = new Object;
  112.  
  113.     // fill in e-mail if it's not already there
  114.     if (email && !ispData.identity.email)
  115.         ispData.identity.email = email;
  116.  
  117.     var emailData = getEmailInfo(email);
  118.     if (emailData) {
  119.  
  120.         // fill in the username (assuming the ISP doesn't prevent it)
  121.         if (!ispData.incomingServer.userName &&
  122.             !ispData.incomingServer.noDefaultUsername)
  123.             ispData.incomingServer.username = emailData.username;
  124.     }
  125.     
  126. }
  127.  
  128. // this function will extract an entire datasource into a giant
  129. // associative array for easy retrieval from JS
  130. var NClength = NC.length;
  131. function dataSourceToObject(datasource, root)
  132. {
  133.     var result = null;
  134.     var arcs = datasource.ArcLabelsOut(root);
  135.  
  136.     while (arcs.hasMoreElements()) {
  137.         var arc = arcs.getNext().QueryInterface(nsIRDFResource);
  138.  
  139.         var arcName = arc.Value;
  140.         arcName = arcName.substring(NClength, arcName.length);
  141.  
  142.         if (!result) result = new Object;
  143.         
  144.         var target = datasource.GetTarget(root, arc, true);
  145.         
  146.         var value;
  147.         var targetHasChildren = false;
  148.         try {
  149.             target = target.QueryInterface(nsIRDFResource);
  150.             targetHasChildren = true;
  151.         } catch (ex) {
  152.             target = target.QueryInterface(nsIRDFLiteral);
  153.         }
  154.  
  155.         if (targetHasChildren)
  156.             value = dataSourceToObject(datasource, target);
  157.         else {
  158.             value = target.Value;
  159.  
  160.             // fixup booleans/numbers/etc
  161.             if (value == "true") value = true;
  162.             else if (value == "false") value = false;
  163.             else {
  164.                 var num = Number(value);
  165.                 if (!isNaN(num)) value = num;
  166.             }
  167.         }
  168.             
  169.         
  170.         // add this value
  171.         result[arcName] = value;
  172.     }
  173.     return result;
  174. }
  175.