summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/modules/LDAPSyncQuery.jsm
blob: d92fea191f6ad1ea23564cc4fc546d246dfa9f54 (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
/* 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 = ["LDAPSyncQuery"];

/**
 * @implements {nsILDAPMessageListener}
 * @implements {nsILDAPSyncQuery}
 */
class LDAPSyncQuery {
  QueryInterface = ChromeUtils.generateQI([
    "nsILDAPMessageListener",
    "nsILDAPSyncQuery",
  ]);

  /** @see nsILDAPMessageListener */
  onLDAPInit() {
    this._operation = Cc[
      "@mozilla.org/network/ldap-operation;1"
    ].createInstance(Ci.nsILDAPOperation);
    this._operation.init(this._connection, this, null);
    this._operation.simpleBind("");
  }

  onLDAPMessage(msg) {
    switch (msg.type) {
      case Ci.nsILDAPMessage.RES_BIND:
        this._onLDAPBind(msg);
        break;
      case Ci.nsILDAPMessage.RES_SEARCH_ENTRY:
        this._onLDAPSearchEntry(msg);
        break;
      case Ci.nsILDAPMessage.RES_SEARCH_RESULT:
        this._onLDAPSearchResult(msg);
        break;
      default:
        break;
    }
  }

  onLDAPError(status, secInfo, location) {
    this._statusCode = status;
    this._finished = true;
  }

  /** @see nsILDAPSyncQuery */
  getQueryResults(ldapUrl, protocolVersion) {
    this._ldapUrl = ldapUrl;
    this._connection = Cc[
      "@mozilla.org/network/ldap-connection;1"
    ].createInstance(Ci.nsILDAPConnection);
    this._connection.init(ldapUrl, "", this, null, protocolVersion);

    this._statusCode = 0;
    this._result = "";
    this._finished = false;

    Services.tm.spinEventLoopUntil(
      "getQueryResults is a sync function",
      () => this._finished
    );
    if (this._statusCode) {
      throw Components.Exception("getQueryResults failed", this._statusCode);
    }
    return this._result;
  }

  /**
   * Handler of nsILDAPMessage.RES_BIND message.
   *
   * @param {nsILDAPMessage} msg - The received LDAP message.
   */
  _onLDAPBind(msg) {
    if (msg.errorCode != Ci.nsILDAPErrors.SUCCESS) {
      this._statusCode = msg.errorCode;
      this._finished = true;
      return;
    }
    this._operation.init(this._connection, this, null);
    this._operation.searchExt(
      this._ldapUrl.dn,
      this._ldapUrl.scope,
      this._ldapUrl.filter,
      this._ldapUrl.attributes,
      0,
      0
    );
  }

  /**
   * Handler of nsILDAPMessage.RES_SEARCH_ENTRY message.
   *
   * @param {nsILDAPMessage} msg - The received LDAP message.
   */
  _onLDAPSearchEntry(msg) {
    for (let attr of msg.getAttributes()) {
      for (let value of msg.getValues(attr)) {
        this._result += `\n${attr}=${value}`;
      }
    }
  }

  /**
   * Handler of nsILDAPMessage.RES_SEARCH_RESULT message.
   *
   * @param {nsILDAPMessage} msg - The received LDAP message.
   */
  _onLDAPSearchResult(msg) {
    this._finished = true;
  }
}