summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/modules/LDAPDirectory.jsm
blob: 758cde2ed9e9c22390e40e896f2184514850957b (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* 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/. */

const EXPORTED_SYMBOLS = ["LDAPDirectory"];

const { AddrBookDirectory } = ChromeUtils.import(
  "resource:///modules/AddrBookDirectory.jsm"
);
const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);
const { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(lazy, {
  QueryStringToExpression: "resource:///modules/QueryStringToExpression.jsm",
});

/**
 * @implements {nsIAbLDAPDirectory}
 * @implements {nsIAbDirectory}
 */

class LDAPDirectory extends AddrBookDirectory {
  QueryInterface = ChromeUtils.generateQI([
    "nsIAbLDAPDirectory",
    "nsIAbDirectory",
  ]);

  init(uri) {
    this._uri = uri;

    let searchIndex = uri.indexOf("?");
    this._dirPrefId = uri.substr(
      "moz-abldapdirectory://".length,
      searchIndex == -1 ? undefined : searchIndex
    );

    super.init(uri);
  }

  get readOnly() {
    return true;
  }

  get isRemote() {
    return true;
  }

  get isSecure() {
    return this.lDAPURL.scheme == "ldaps";
  }

  get propertiesChromeURI() {
    return "chrome://messenger/content/addressbook/pref-directory-add.xhtml";
  }

  get dirType() {
    return Ci.nsIAbManager.LDAP_DIRECTORY_TYPE;
  }

  get replicationFileName() {
    return this.getStringValue("filename");
  }

  set replicationFileName(value) {
    this.setStringValue("filename", value);
  }

  get replicationFile() {
    return lazy.FileUtils.getFile("ProfD", [this.replicationFileName]);
  }

  get protocolVersion() {
    return this.getStringValue("protocolVersion", "3") == "3"
      ? Ci.nsILDAPConnection.VERSION3
      : Ci.nsILDAPConnection.VERSION2;
  }

  set protocolVersion(value) {
    this.setStringValue(
      "protocolVersion",
      value == Ci.nsILDAPConnection.VERSION3 ? "3" : "2"
    );
  }

  get saslMechanism() {
    return this.getStringValue("auth.saslmech");
  }

  set saslMechanism(value) {
    this.setStringValue("auth.saslmech", value);
  }

  get authDn() {
    return this.getStringValue("auth.dn");
  }

  set authDn(value) {
    this.setStringValue("auth.dn", value);
  }

  get maxHits() {
    return this.getIntValue("maxHits", 100);
  }

  set maxHits(value) {
    this.setIntValue("maxHits", value);
  }

  get attributeMap() {
    let mapSvc = Cc[
      "@mozilla.org/addressbook/ldap-attribute-map-service;1"
    ].createInstance(Ci.nsIAbLDAPAttributeMapService);
    return mapSvc.getMapForPrefBranch(this._dirPrefId);
  }

  get lDAPURL() {
    let uri = this.getStringValue("uri") || `ldap://${this._uri.slice(22)}`;
    return Services.io.newURI(uri).QueryInterface(Ci.nsILDAPURL);
  }

  set lDAPURL(uri) {
    this.setStringValue("uri", uri.spec);
  }

  get childCardCount() {
    return 0;
  }

  get childCards() {
    if (Services.io.offline) {
      return this.replicationDB.childCards;
    }
    return super.childCards;
  }

  /**
   * @see {AddrBookDirectory}
   */
  get cards() {
    return new Map();
  }

  /**
   * @see {AddrBookDirectory}
   */
  get lists() {
    return new Map();
  }

  get replicationDB() {
    this._replicationDB?.cleanUp();
    this._replicationDB = Cc[
      "@mozilla.org/addressbook/directory;1?type=jsaddrbook"
    ].createInstance(Ci.nsIAbDirectory);
    this._replicationDB.init(`jsaddrbook://${this.replicationFileName}`);
    return this._replicationDB;
  }

  getCardFromProperty(property, value, caseSensitive) {
    return null;
  }

  search(queryString, searchString, listener) {
    if (Services.io.offline) {
      this.replicationDB.search(queryString, searchString, listener);
      return;
    }
    this._query = Cc[
      "@mozilla.org/addressbook/ldap-directory-query;1"
    ].createInstance(Ci.nsIAbDirectoryQuery);

    let args = Cc[
      "@mozilla.org/addressbook/directory/query-arguments;1"
    ].createInstance(Ci.nsIAbDirectoryQueryArguments);
    args.expression = lazy.QueryStringToExpression.convert(queryString);
    args.querySubDirectories = true;
    args.typeSpecificArg = this.attributeMap;

    this._query.doQuery(this, args, listener, this.maxHits, 0);
  }

  useForAutocomplete(identityKey) {
    // If we're online, then don't allow search during local autocomplete - must
    // use the separate LDAP autocomplete session due to the current interfaces
    let useDirectory = Services.prefs.getBoolPref(
      "ldap_2.autoComplete.useDirectory",
      false
    );
    if (!Services.io.offline || (!useDirectory && !identityKey)) {
      return false;
    }

    let prefName = "";
    if (identityKey) {
      // If we have an identity string, try and find out the required directory
      // server.
      let identity = MailServices.accounts.getIdentity(identityKey);
      if (identity.overrideGlobalPref) {
        prefName = identity.directoryServer;
      }
      if (!prefName && !useDirectory) {
        return false;
      }
    }
    if (!prefName) {
      prefName = Services.prefs.getCharPref(
        "ldap_2.autoComplete.directoryServer"
      );
    }
    if (prefName == this.dirPrefId) {
      return this.replicationFile.exists();
    }

    return false;
  }
}

LDAPDirectory.prototype.classID = Components.ID(
  "{8683e821-f1b0-476d-ac15-07771c79bb11}"
);