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

source: modules/vI_identityData.js @ 994fa9

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

added smtp-Label and Account Description

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