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