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

/**
 * @implements {nsILDAPService}
 */
class LDAPService {
  QueryInterface = ChromeUtils.generateQI(["nsILDAPService"]);

  createFilter(maxSize, pattern, prefix, suffix, attr, value) {
    let words = value.split(" ");
    // Get the Mth to Nth words.
    function getMtoN(m, n) {
      n = n || m;
      return words.slice(m - 1, n).join(" ");
    }

    let filter = prefix;
    pattern.replaceAll("%a", attr);
    while (pattern) {
      let index = pattern.indexOf("%v");
      if (index == -1) {
        filter += pattern;
        pattern = "";
      } else {
        filter += pattern.slice(0, index);
        // Get the three characters after %v.
        let [c1, c2, c3] = pattern.slice(index + 2, index + 5);
        if (c1 >= "1" && c1 <= "9") {
          if (c2 == "$") {
            // %v$: means the last word
            filter += getMtoN(words.length);
            pattern = pattern.slice(index + 3);
          } else if (c2 == "-") {
            if (c3 >= "1" && c3 <= "9") {
              // %vM-N: means from the Mth to the Nth word
              filter += getMtoN(c1, c3);
              pattern = pattern.slice(index + 5);
            } else {
              // %vN-: means from the Nth to the last word
              filter += getMtoN(c1, words.length);
              pattern = pattern.slice(index + 4);
            }
          } else {
            // %vN: means the Nth word
            filter += getMtoN(c1);
            pattern = pattern.slice(index + 3);
          }
        } else {
          // %v: means the entire search value
          filter += value;
          pattern = pattern.slice(index + 2);
        }
      }
    }
    filter += suffix;
    return filter.length > maxSize ? "" : filter;
  }
}

LDAPService.prototype.classID = Components.ID(
  "{e8b59b32-f83f-4d5f-8eb5-e3c1e5de0d47}"
);