summaryrefslogtreecommitdiffstats
path: root/mailnews/base/prefs/content/ispUtils.js
blob: 7735d8a471f67d0a75b06972ed6aea4f68aeecd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// using the rdf service extensively here
var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);

// all the RDF resources we'll be retrieving
var NC = "http://home.netscape.com/NC-rdf#";
var Server              = rdf.GetResource(NC + "Server");
var SmtpServer          = rdf.GetResource(NC + "SmtpServer");
var ServerHost          = rdf.GetResource(NC + "ServerHost");
var ServerType          = rdf.GetResource(NC + "ServerType");
var PrefixIsUsername    = rdf.GetResource(NC + "PrefixIsUsername");
var UseAuthenticatedSmtp= rdf.GetResource(NC + "UseAuthenticatedSmtp");

// this is possibly expensive, not sure what to do here
var ispDefaults;

var nsIRDFResource = Components.interfaces.nsIRDFResource;
var nsIRDFLiteral = Components.interfaces.nsIRDFLiteral;

var ispRoot = rdf.GetResource("NC:ispinfo");

// given an ISP's domain URI, look up all relevant information about it
function getIspDefaultsForUri(domainURI)
{
    if (!ispDefaults) 
        ispDefaults = rdf.GetDataSource("rdf:ispdefaults");

    var domainRes = rdf.GetResource(domainURI);

    var result = dataSourceToObject(ispDefaults, domainRes);

    if (!result) return null;

    // The domainURI should be in the format domain:example.com. (Where 
    // example.com is the domain name to use for all email addresses). If
    // it does not match this pattern, then it is possible no domain
    // has been specified, so we should leave it uninitialized.
    if (domainURI.startsWith("domain:")) {
        // add this extra attribute which is the domain itself
        var domainData = domainURI.split(':');
        if (domainData.length > 1) {
          // To faciltate distributing two different account types for one ISP,
          // it's possible to add parameters to the domain URI 
          // - e.g. domain:example.com?type=imap.
          // This is necessary so RDF doesn't think they're the same.

          // Save the domain, but only the part up to the (possible) question mark.
          result.domain = domainData[1].replace(/\?.*/, "");
        }
    }
    return result;
}

// construct an ISP's domain URI based on it's domain
// (i.e. turns example.com -> domain:example.com)
function getIspDefaultsForDomain(domain) {
    let domainURI = "domain:" + domain;
    return getIspDefaultsForUri(domainURI);
}

// Given an email address (like "joe@example.com") look up 
function getIspDefaultsForEmail(email) {

    var emailData = getEmailInfo(email);

    var ispData = null;
    if (emailData)
        ispData = getIspDefaultsForDomain(emailData.domain);

    prefillIspData(ispData, email);

    return ispData;
}

// given an email address, split it into username and domain
// return in an associative array
function getEmailInfo(email) {
    if (!email) return null;

    var result = new Object;

    var emailData = email.split('@');

    if (emailData.length != 2) {
        dump("bad e-mail address!\n");
        return null;
    }

    // all the variables we'll be returning
    result.username = emailData[0];
    result.domain = emailData[1];

    return result;
}

function prefillIspData(ispData, email, fullName) {
    if (!ispData) return;

    // make sure these objects exist
    if (!ispData.identity) ispData.identity = new Object;
    if (!ispData.incomingServer) ispData.incomingServer = new Object;

    // fill in e-mail if it's not already there
    if (email && !ispData.identity.email)
        ispData.identity.email = email;

    var emailData = getEmailInfo(email);
    if (emailData) {

        // fill in the username (assuming the ISP doesn't prevent it)
        if (!ispData.incomingServer.userName &&
            !ispData.incomingServer.noDefaultUsername)
            ispData.incomingServer.username = emailData.username;
    }
}

// this function will extract an entire datasource into a giant
// associative array for easy retrieval from JS
var NClength = NC.length;
function dataSourceToObject(datasource, root)
{
    var result = null;
    var arcs = datasource.ArcLabelsOut(root);

    while (arcs.hasMoreElements()) {
        var arc = arcs.getNext().QueryInterface(nsIRDFResource);

        var arcName = arc.Value;
        arcName = arcName.substring(NClength, arcName.length);

        if (!result) result = new Object;

        var target = datasource.GetTarget(root, arc, true);

        var value;
        var targetHasChildren = false;
        try {
            target = target.QueryInterface(nsIRDFResource);
            targetHasChildren = true;
        } catch (ex) {
            target = target.QueryInterface(nsIRDFLiteral);
        }

        if (targetHasChildren)
            value = dataSourceToObject(datasource, target);
        else {
            value = target.Value;

            // fixup booleans/numbers/etc
            if (value == "true") value = true;
            else if (value == "false") value = false;
            else {
                var num = Number(value);
                if (!isNaN(num)) value = num;
            }
        }

        // add this value
        result[arcName] = value;
    }
    return result;
}