summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/src/AbLDAPAttributeMap.jsm
blob: dae7b976302b60024613cdf92bab8df154939e7a (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
/* 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/. */

var EXPORTED_SYMBOLS = ["AbLDAPAttributeMap", "AbLDAPAttributeMapService"];

function AbLDAPAttributeMap() {
  this.mPropertyMap = {};
  this.mAttrMap = {};
}

AbLDAPAttributeMap.prototype = {
  getAttributeList(aProperty) {
    if (!(aProperty in this.mPropertyMap)) {
      return null;
    }

    // return the joined list
    return this.mPropertyMap[aProperty].join(",");
  },

  getAttributes(aProperty) {
    // fail if no entry for this
    if (!(aProperty in this.mPropertyMap)) {
      throw Components.Exception("", Cr.NS_ERROR_FAILURE);
    }
    return this.mPropertyMap[aProperty];
  },

  getFirstAttribute(aProperty) {
    // fail if no entry for this
    if (!(aProperty in this.mPropertyMap)) {
      return null;
    }

    return this.mPropertyMap[aProperty][0]?.replace(/\[(\d+)\]$/, "");
  },

  setAttributeList(aProperty, aAttributeList, aAllowInconsistencies) {
    var attrs = aAttributeList.split(",");

    // check to make sure this call won't allow multiple mappings to be
    // created, if requested
    if (!aAllowInconsistencies) {
      for (var attr of attrs) {
        if (attr in this.mAttrMap && this.mAttrMap[attr] != aProperty) {
          throw Components.Exception("", Cr.NS_ERROR_FAILURE);
        }
      }
    }

    // delete any attr mappings created by the existing property map entry
    if (aProperty in this.mPropertyMap) {
      for (attr of this.mPropertyMap[aProperty]) {
        delete this.mAttrMap[attr];
      }
    }

    // add these attrs to the attrmap
    for (attr of attrs) {
      this.mAttrMap[attr] = aProperty;
    }

    // add them to the property map
    this.mPropertyMap[aProperty] = attrs;
  },

  getProperty(aAttribute) {
    if (!(aAttribute in this.mAttrMap)) {
      return null;
    }

    return this.mAttrMap[aAttribute];
  },

  getAllCardAttributes() {
    var attrs = [];
    for (let attrArray of Object.entries(this.mPropertyMap)) {
      for (let attrName of attrArray) {
        attrName = attrName.toString().replace(/\[(\d+)\]$/, "");
        if (attrs.includes(attrName)) {
          continue;
        }
        attrs.push(attrName);
      }
    }

    if (!attrs.length) {
      throw Components.Exception("", Cr.NS_ERROR_FAILURE);
    }

    return attrs.join(",");
  },

  getAllCardProperties() {
    var props = [];
    for (var prop in this.mPropertyMap) {
      props.push(prop);
    }
    return props;
  },

  setFromPrefs(aPrefBranchName) {
    // get the right pref branch
    let branch = Services.prefs.getBranch(aPrefBranchName + ".");

    // get the list of children
    var children = branch.getChildList("");

    // do the actual sets
    for (var child of children) {
      this.setAttributeList(child, branch.getCharPref(child), true);
    }

    // ensure that everything is kosher
    this.checkState();
  },

  setCardPropertiesFromLDAPMessage(aMessage, aCard) {
    var cardValueWasSet = false;

    var msgAttrs = aMessage.getAttributes();

    // downcase the array for comparison
    function toLower(a) {
      return a.toLowerCase();
    }
    msgAttrs = msgAttrs.map(toLower);

    // deal with each addressbook property
    for (var prop in this.mPropertyMap) {
      // go through the list of possible attrs in precedence order
      for (var attr of this.mPropertyMap[prop]) {
        attr = attr.toLowerCase();
        // allow an index in attr
        let valueIndex = 0;
        const valueIndexMatch = /^(.+)\[(\d+)\]$/.exec(attr);
        if (valueIndexMatch !== null) {
          attr = valueIndexMatch[1];
          valueIndex = parseInt(valueIndexMatch[2]);
        }

        // find the first attr that exists in this message
        if (msgAttrs.includes(attr)) {
          try {
            var values = aMessage.getValues(attr);
            // strip out the optional label from the labeledURI
            if (attr == "labeleduri" && values[valueIndex]) {
              var index = values[valueIndex].indexOf(" ");
              if (index != -1) {
                values[valueIndex] = values[valueIndex].substring(0, index);
              }
            }
            aCard.setProperty(prop, values[valueIndex]);

            cardValueWasSet = true;
            break;
          } catch (ex) {
            // ignore any errors getting message values or setting card values
          }
        }
      }
    }

    if (!cardValueWasSet) {
      throw Components.Exception("", Cr.NS_ERROR_FAILURE);
    }
  },

  checkState() {
    var attrsSeen = [];

    for (var prop in this.mPropertyMap) {
      let attrArray = this.mPropertyMap[prop];
      for (var attr of attrArray) {
        // multiple attributes that mapped to the empty string are permitted
        if (!attr.length) {
          continue;
        }

        // if we've seen this before, there's a problem
        if (attrsSeen.includes(attr)) {
          throw Components.Exception("", Cr.NS_ERROR_FAILURE);
        }

        // remember that we've seen it now
        attrsSeen.push(attr);
      }
    }
  },

  QueryInterface: ChromeUtils.generateQI(["nsIAbLDAPAttributeMap"]),
};

function AbLDAPAttributeMapService() {}

AbLDAPAttributeMapService.prototype = {
  mAttrMaps: {},

  getMapForPrefBranch(aPrefBranchName) {
    // if we've already got this map, return it
    if (aPrefBranchName in this.mAttrMaps) {
      return this.mAttrMaps[aPrefBranchName];
    }

    // otherwise, try and create it
    var attrMap = new AbLDAPAttributeMap();
    attrMap.setFromPrefs("ldap_2.servers.default.attrmap");
    attrMap.setFromPrefs(aPrefBranchName + ".attrmap");

    // cache
    this.mAttrMaps[aPrefBranchName] = attrMap;

    // and return
    return attrMap;
  },

  QueryInterface: ChromeUtils.generateQI(["nsIAbLDAPAttributeMapService"]),
};