This is just some static backup of the original site, don't expect every link to work!

source: modules/vI_identityData.js

ng_0.9 0.10.3
Last change on this file was c6a1e7, checked in by rene <rene@…>, 4 years ago

use fullName even if it's empty, thats what we like to have

  • Property mode set to 100644
File size: 18.8 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2    This program is free software; you can redistribute it and/or modify
3    it under the terms of the GNU General Public License as published by
4    the Free Software Foundation; either version 2 of the License, or
5    (at your option) any later version.
6
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15
16    The Original Code is the Virtual Identity Extension.
17
18    The Initial Developer of the Original Code is Rene Ejury.
19    Portions created by the Initial Developer are Copyright (C) 2007
20    the Initial Developer. All Rights Reserved.
21
22    Contributor(s):
23 * ***** END LICENSE BLOCK ***** */
24
25var EXPORTED_SYMBOLS = ["identityCollection", "identityData", "identityDataExtras", "DEFAULT_SMTP_TAG", "NO_SMTP_TAG"]
26
27const DEFAULT_SMTP_TAG = "vI_useDefaultSMTP"
28const NO_SMTP_TAG = "vI_noStoredSMTP"
29
30Components.utils.import("resource://v_identity/vI_log.js");
31let Log = setupLogging("virtualIdentity.identityData");
32Components.utils.import("resource://v_identity/vI_prefs.js");
33Components.utils.import("resource://v_identity/vI_accountUtils.js");
34
35Components.utils.import("resource://v_identity/vI_identityDataExtras.js");
36Components.utils.import("resource://v_identity/identityDataExtras/returnReceipt.js");
37Components.utils.import("resource://v_identity/identityDataExtras/messageFormat.js");
38Components.utils.import("resource://v_identity/identityDataExtras/characterEncoding.js");
39Components.utils.import("resource://v_identity/identityDataExtras/sMimeEncryption.js");
40Components.utils.import("resource://v_identity/identityDataExtras/sMimeSignature.js");
41
42ChromeUtils.import("resource:///modules/mailServices.js");
43
44function identityData(currentWindow, email, fullName, id, extras, sideDescription, existingID) {
45  this._currentWindow = currentWindow;
46  this._email = email ? email : "";
47  this._emailParsed = false;
48  this._fullName = fullName ? fullName : "";
49  this.id = new idObj(id);
50  if (extras) this.extras = extras;
51  else this.extras = new identityDataExtras(currentWindow);
52  this.comp = { // holds the results of the last comparison for later creation of a compareMatrix
53    compareID: null,
54    equals: {
55      fullName: {},
56      email: {},
57      id: {},
58      extras: {}
59    }
60  }
61  if (sideDescription) this.sideDescription = sideDescription;
62  if (existingID) this.existingID = existingID;
63  else if (this.id.value) this.sideDescription = " - " + this.id.value;
64  this.stringBundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
65    .getService(Components.interfaces.nsIStringBundleService)
66    .createBundle("chrome://v_identity/locale/v_identity.properties");
67}
68identityData.prototype = {
69  _email: null, // internal email-field might contain combinedName (until first queried via email)
70  _fullName: null,
71  _emailParsed: null,
72  _currentWindow: null,
73  id: null,
74  extras: null,
75  sideDescription: null,
76  existingID: null, // indicates that this is a pre-defined Identity, which might handled slightly differently
77
78  stringBundle: null,
79  comp: null,
80
81  parseEmail: function () {
82    if (this._emailParsed) return;
83    // parse email and move any additional parts to fullName
84    if (this._email.match(/<\s*[^>\s]*@[^>\s]*\s*>/) || this._email.match(/<?\s*[^>\s]*@[^>\s]*\s*>?/) || this._email.match(/$/)) {
85      this._fullName += RegExp.leftContext + RegExp.rightContext;
86      this._email = RegExp.lastMatch;
87      //            Log.debug("parseEmail _fullName = '" + this._fullName + "'");
88      //            Log.debug("parseEmail _email =    '" + this._email + "'");
89    }
90    this._emailParsed = true;
91  },
92  get email() {
93    this.parseEmail();
94    return (this._email ? this._email.replace(/\s+|<|>/g, "") : "");
95  },
96  set email(email) {
97    this._email = email;
98    this._emailParsed = false;
99  },
100
101  cleanName: function (fullName) {
102    //      Log.debug("cleanName init '" + fullName + "'");
103    var _fullName = fullName.replace(/^\s+|\s+$/g, "");
104    if (_fullName.search(/^\".+\"$|^'.+'$/g) != -1) {
105      _fullName = this.cleanName(_fullName.replace(/^\"(.+)\"$|^'(.+)'$/g, "$1$2"));
106    }
107    //      Log.debug("cleanName done '" + _fullName + "'");
108    return _fullName;
109  },
110
111  get fullName() {
112    this.parseEmail();
113    return (this._fullName ? this.cleanName(this._fullName) : "")
114  },
115  set fullName(fullName) {
116    this._fullName = fullName;
117  },
118
119  get combinedName() {
120    var fullName = this.fullName;
121    var email = this.email;
122    return fullName ? fullName + (email ? " <" + email + ">" : "") : email
123  },
124  set combinedName(combinedName) {
125    this._email = combinedName;
126    this._fullName = "";
127    this._emailParsed = false;
128  },
129
130  __makeHtml: function (string) {
131    return string ? string.replace(/>/g, "&gt;").replace(/</g, "&lt;") : ""
132  },
133  get idHtml() {
134    return this.__makeHtml(this.id.value);
135  },
136  get smtpHtml() {
137    return this.__makeHtml(this.id.smtpServerName);
138  },
139  get fullNameHtml() {
140    return this.__makeHtml(this.fullName);
141  },
142  get emailHtml() {
143    return this.__makeHtml(this.email);
144  },
145  get combinedNameHtml() {
146    return this.__makeHtml(this.combinedName);
147  },
148
149  get idLabel() {
150    return this.stringBundle.GetStringFromName("vident.identityData.baseID")
151  },
152  get smtpLabel() {
153    return this.stringBundle.GetStringFromName("vident.identityData.SMTP")
154  },
155  get fullNameLabel() {
156    return this.stringBundle.GetStringFromName("vident.identityData.Name")
157  },
158  get emailLabel() {
159    return this.stringBundle.GetStringFromName("vident.identityData.Address")
160  },
161
162  // creates an Duplicate of the current IdentityData, cause usually we are working with a pointer
163  getDuplicate: function () {
164    return new identityData(this._currentWindow, this.email, this.fullName, this.id.key, this.extras ? this.extras.getDuplicate() : null,
165      this.sideDescription, this.existingID);
166  },
167 
168  takeOverAvailableData: function(identityData) {
169    if (identityData.email) {
170      this.email = identityData.email;
171      this.fullName = identityData.fullName;
172    }
173    if (identityData.id.key)
174      this.id.key = identityData.id.key;
175    if (identityData.sideDescription)
176      this.sideDescription = identityData.sideDescription;
177    if (identityData.extras)
178      this.extras.copy(identityData.extras);
179  },
180
181  // copys all values of an identity. This way we can create a new object with a different document-context
182  copy: function (identityData) {
183    this.email = identityData.email;
184    this.fullName = identityData.fullName;
185    this.id.key = identityData.id.key;
186    this.sideDescription = identityData.sideDescription;
187    if (this.extras) this.extras.copy(identityData.extras);
188    // don't copy the currentWindow value
189  },
190
191  // dependent on MsgComposeCommands, should/will only be called in ComposeDialog
192  isExistingIdentity: function (ignoreFullNameWhileComparing) {
193    var intenseDebug = false;
194    if (intenseDebug)
195      Log.debug("isExistingIdentity: ignoreFullNameWhileComparing='" + ignoreFullNameWhileComparing + "'");
196    //      Log.debug("base: fullName.toLowerCase()='" + this.fullName + "' email.toLowerCase()='" + this.email + "'");
197
198    var ignoreFullNameMatchKey = null;
199    var accounts = getAccountsArray();
200    for (let acc = 0; acc < accounts.length; acc++) {
201      let account = accounts[acc];
202      try {
203        prefroot.getBoolPref("mail.account." + account.key + ".vIdentity");
204        continue;
205      } catch (e) {};
206      let identities = getIdentitiesArray(account);
207      for (let i = 0; i < identities.length; i++) {
208        let identity = identities[i];
209        //              Log.debug("comp: fullName.toLowerCase()='" + identity.fullName.toLowerCase() + "' email.toLowerCase()='" + identity.email.toLowerCase() + "'");
210        var email = this.email ? this.email : ""; // might be null if no identity is set
211        var idEmail = identity.email ? identity.email : ""; // might be null if no identity is set
212        if (email.toLowerCase() == idEmail.toLowerCase()) {
213          // if fullName matches, than this is a final match
214          if (this.fullName.toLowerCase() == identity.fullName.toLowerCase()) {
215            if (intenseDebug)
216              Log.debug("isExistingIdentity: " + this.combinedName + " found, id='" + identity.key + "'");
217            return identity.key; // return key and stop searching
218          }
219          // if fullNames don't match, remember the key but continue to search for full match
220          else if (!ignoreFullNameMatchKey) ignoreFullNameMatchKey = identity.key;
221        }
222      }
223    }
224
225    if (ignoreFullNameWhileComparing && ignoreFullNameMatchKey) {
226      if (intenseDebug)
227        Log.debug("isExistingIdentity: " + this.combinedName + " found, id='" + ignoreFullNameMatchKey + "' (without FullName match)");
228      return ignoreFullNameMatchKey;
229    }
230
231    if (intenseDebug)
232      Log.debug("isExistingIdentity: " + this.combinedName + " not found");
233    return null;
234  },
235
236  // dependent on MsgComposeCommands, should/will only be called in ComposeDialog
237  hasMatchingDomainIdentity: function () {
238    Log.debug("hasMatchingDomainIdentity");
239
240    var domainArray = this.email.match(/@[^@]+$/);
241    if (!domainArray) {
242      Log.debug("hasMatchingDomainIdentity found no domain for email " + this.email);
243      return;
244    }
245    Log.debug("hasMatchingDomainIdentity searching for domain " + domainArray[0]);
246
247    var accounts = getAccountsArray();
248    for (let acc = 0; acc < accounts.length; acc++) {
249      let account = accounts[acc];
250      try {
251        prefroot.getBoolPref("mail.account." + account.key + ".vIdentity");
252        continue;
253      } catch (e) {};
254      let identities = getIdentitiesArray(account);
255      for (let i = 0; i < identities.length; i++) {
256        let identity = identities[i];
257        var idDomainArray = identity.email.match(/@[^@]+$/);
258        if (!idDomainArray) continue;
259        //                 Log.debug("comp: domain.toLowerCase()='" + domainArray[0].toLowerCase() + "' idDomain.toLowerCase()='" + idDomainArray[0].toLowerCase() + "'");
260        if (domainArray[0].toLowerCase() == idDomainArray[0].toLowerCase()) {
261          // if domain matches, everything is perfect!
262          Log.debug("hasMatchingDomainIdentity: found matching id for domain '" + domainArray[0] + "'");
263          return identity.key; // return key and stop searching
264        }
265      }
266    }
267    Log.debug("hasMatchingDomainIdentity: '" + domainArray[0] + "' not found");
268    return null;
269  },
270
271  equals: function (compareIdentityData) {
272    if (!compareIdentityData)
273      return false;
274   
275    var intenseDebug = false;
276    if (intenseDebug) Log.debug("compareIdentityData");
277   
278    this.comp.compareID = compareIdentityData;
279
280    this.comp.equals.fullName = (((this.fullName) ? this.fullName.toLowerCase() : null) == ((compareIdentityData.fullName) ? compareIdentityData.fullName.toLowerCase() : null));
281    if (intenseDebug && !this.comp.equals.fullName) {
282            Log.debug("fullName not equal ('" + ((this.fullName)?this.fullName.toLowerCase():null) + "' != '" + ((compareIdentityData.fullName)?compareIdentityData.fullName.toLowerCase():null) + "')");
283    }
284    this.comp.equals.email = (((this.email) ? this.email.toLowerCase() : null) == ((compareIdentityData.email) ? compareIdentityData.email.toLowerCase() : null));
285    if (intenseDebug && !this.comp.equals.email) {
286            Log.debug("email not equal ('" + ((this.email)?this.email.toLowerCase():null) + "' != '" + ((compareIdentityData.email)?compareIdentityData.email.toLowerCase():null) + "')");
287    }
288
289    this.comp.equals.id = this.id.equal(compareIdentityData.id);
290    if (intenseDebug && !this.comp.equals.id) {
291            Log.debug("id not equal ('" + this.id + "' != '" + compareIdentityData.id + "')");
292    }
293
294    this.comp.equals.extras = this.extras ? this.extras.equal(compareIdentityData.extras) : true;
295    if (intenseDebug && !this.comp.equals.extras) {
296            Log.debug("extras not equal");
297    }
298
299    return (this.comp.equals.fullName && this.comp.equals.email && this.comp.equals.id && this.comp.equals.extras);
300  },
301
302  equalsIdentity: function (compareIdentityData, getCompareMatrix) {
303    var equal = this.equals(compareIdentityData);
304    var compareMatrix = null;
305    // generate CompareMatrix only if asked and non-equal
306    if (getCompareMatrix && !equal) compareMatrix = this.getCompareMatrix();
307    return {
308      equal: equal,
309      compareMatrix: compareMatrix
310    };
311  },
312
313  getCompareMatrix: function () {
314    const Items = Array("fullName", "email", "id");
315    var string = "";
316    var saveBaseId = vIprefs.get("storage_store_base_id");
317    for (let item of Items) {
318      var classEqual = (this.comp.equals[item]) ? "equal" : "unequal";
319      var classIgnore = ((!saveBaseId) && (item == "id")) ? " ignoreValues" : ""
320      string += "<tr>" +
321        "<td class='col1 " + classEqual + "'>" + this[item + "Label"] + "</td>" +
322        "<td class='col2 " + classEqual + classIgnore + "'>" + this.comp.compareID[item + "Html"] + "</td>" +
323        "<td class='col3 " + classEqual + classIgnore + "'>" + this[item + "Html"] + "</td>" +
324        "</tr>"
325    }
326    string += this.extras ? this.extras.getCompareMatrix() : "";
327    return string;
328  },
329
330  getMatrix: function () {
331    var string = "";
332    if (this["idHtml"]) {
333        string = "<tr><td class='col1'>" + this["idLabel"] + ":</td>" +
334        "<td class='col2'>" + this["idHtml"] + "</td></tr>" +
335        "<tr><td class='col1'>" + this["smtpLabel"] + ":</td>" +
336        "<td class='col2'>" + this["smtpHtml"] + "</td></tr>";
337    }
338    string += this.extras ? this.extras.getMatrix() : "";
339    return string;
340  }
341}
342
343function identityCollection() {
344  this.number = 0;
345  this.identityDataCollection = {};
346  this.menuItems = {};
347}
348identityCollection.prototype = {
349  number: null,
350  identityDataCollection: null,
351  menuItems: null,
352
353  mergeWithoutDuplicates: function (addIdentityCollection) {
354    for (var index = 0; index < addIdentityCollection.number; index++)
355      this.addWithoutDuplicates(addIdentityCollection.identityDataCollection[index])
356  },
357
358  dropIdentity: function (index) {
359    Log.debug("dropping address from inputList: " + this.identityDataCollection[index].combinedName);
360    while (index < (this.number - 1)) {
361      this.identityDataCollection[index] = this.identityDataCollection[++index];
362    };
363    this.identityDataCollection[--this.number] = null;
364  },
365
366  addWithoutDuplicates: function (identityData) {
367    if (!identityData) return;
368    for (var index = 0; index < this.number; index++) {
369      if (this.identityDataCollection[index].email == identityData.email &&
370        (!this.identityDataCollection[index].id.key || !identityData.id.key ||
371          this.identityDataCollection[index].id.key == identityData.id.key)) {
372        // found, so check if we can use the Name of the new field
373        if (this.identityDataCollection[index].fullName == "" && identityData.fullName != "") {
374          this.identityDataCollection[index].fullName = identityData.fullName;
375          Log.debug("added fullName '" + identityData.fullName + "' to stored email '" + this.identityDataCollection[index].email + "'")
376        }
377        // check if id_key or extras can be used
378        // only try this once, for the first Identity where id is set)
379        if (!this.identityDataCollection[index].id.key && identityData.id.key) {
380          this.identityDataCollection[index].id.key = identityData.id.key;
381          this.identityDataCollection[index].extras = identityData.extras;
382          Log.debug("added id '" + identityData.id.value + "' (+extras) to stored email '" + this.identityDataCollection[index].email + "'")
383        }
384        return;
385      }
386    }
387    Log.debug("add new address to result: " + identityData.combinedName)
388    this.identityDataCollection[index] = identityData;
389    this.number = index + 1;
390  },
391
392  // this is used to completely use the conten of another identityCollection, but without changing all pointers
393  // see for instance vI.smartIdentity.__filterAddresses
394  takeOver: function (newIdentityCollection) {
395    this.number = newIdentityCollection.number
396    this.identityDataCollection = newIdentityCollection.identityDataCollection
397  }
398};
399
400function idObj(key) {
401  this._key = key;
402}
403idObj.prototype = {
404  _key: null,
405  _value: null,
406  _accountkey: null,
407  _accountIncomingServerPrettyName: null,
408  _accountManager: Components.classes["@mozilla.org/messenger/account-manager;1"]
409    .getService(Components.interfaces.nsIMsgAccountManager),
410
411  set key(key) {
412    this._key = key;
413    this._value = null;
414  },
415  get key() {
416    if (this._value == null) var dummy = this.value;
417    return this._key
418  },
419  get accountkey() {
420    if (this._value == null) var dummy = this.value;
421    return this._accountkey
422  },
423 
424  get accountIncomingServerPrettyName() {
425    if (this._value == null) var dummy = this.value;
426    return this._accountIncomingServerPrettyName
427  },
428 
429  get value() {
430    if (this._value == null) {
431      this._value = "";
432      // if this worked we are having at least seamonkey 1.17
433      let accounts = getAccountsArray();
434      for (let acc = 0; acc < accounts.length; acc++) {
435        let account = accounts[acc];
436        let identities = getIdentitiesArray(account);
437        if (identities.length == 0)
438          continue;
439        for (let i = 0; i < identities.length; i++) {
440          let identity = identities[i];
441          if (this._key == identity.key) {
442            this._value = identity.identityName;
443            this._accountkey = account.key;
444            this._accountIncomingServerPrettyName = account.incomingServer.prettyName;
445            break;
446          }
447        }
448      }
449      if (!this._value) {
450        this._key = null;
451        this._accountkey = null;
452      }
453    }
454    return this._value;
455  },
456
457  get smtpServerKey() {
458    if (!this.key)
459      return null;
460
461    var identity = this._accountManager.getIdentity(this.key);
462    if (identity) {
463      if (identity.smtpServerKey)
464        return identity.smtpServerKey;
465      else
466        return MailServices.smtp.defaultServer.key
467    }
468    return null;
469  },
470 
471  get smtpServerName() {
472    if (!this.smtpServerKey)
473      return null;
474    var servers = MailServices.smtp.servers;
475
476    var smtpName;
477    while (servers && servers.hasMoreElements()) {
478      var server = servers.getNext();
479      if (server instanceof Components.interfaces.nsISmtpServer &&
480        !server.redirectorType && this.smtpServerKey == server.key) {
481        smtpName = server.description ? server.description : server.hostname;
482        break;
483      }
484    }
485    return smtpName;
486  },
487 
488  equal: function (compareIdObj) {
489    if (!this.key || !compareIdObj.key) return true;
490    if (this.key != compareIdObj.key) {
491      //       Log.debug("id not equal ('" + this.key + "' != '" + compareIdObj.key + "')");
492    }
493    return (this.key == compareIdObj.key);
494  }
495}
Note: See TracBrowser for help on using the repository browser.